xref: /openbmc/linux/drivers/s390/char/raw3270.c (revision fadbafc1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * IBM/3270 Driver - core functions.
4  *
5  * Author(s):
6  *   Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7  *   Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
8  *     Copyright IBM Corp. 2003, 2009
9  */
10 
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/wait.h>
19 
20 #include <asm/ccwdev.h>
21 #include <asm/cio.h>
22 #include <asm/ebcdic.h>
23 #include <asm/diag.h>
24 
25 #include "raw3270.h"
26 
27 #include <linux/major.h>
28 #include <linux/kdev_t.h>
29 #include <linux/device.h>
30 #include <linux/mutex.h>
31 
32 struct class *class3270;
33 
34 /* The main 3270 data structure. */
35 struct raw3270 {
36 	struct list_head list;
37 	struct ccw_device *cdev;
38 	int minor;
39 
40 	short model, rows, cols;
41 	unsigned int state;
42 	unsigned long flags;
43 
44 	struct list_head req_queue;	/* Request queue. */
45 	struct list_head view_list;	/* List of available views. */
46 	struct raw3270_view *view;	/* Active view. */
47 
48 	struct timer_list timer;	/* Device timer. */
49 
50 	unsigned char *ascebc;		/* ascii -> ebcdic table */
51 
52 	struct raw3270_view init_view;
53 	struct raw3270_request init_reset;
54 	struct raw3270_request init_readpart;
55 	struct raw3270_request init_readmod;
56 	unsigned char init_data[256];
57 };
58 
59 /* raw3270->state */
60 #define RAW3270_STATE_INIT	0	/* Initial state */
61 #define RAW3270_STATE_RESET	1	/* Reset command is pending */
62 #define RAW3270_STATE_W4ATTN	2	/* Wait for attention interrupt */
63 #define RAW3270_STATE_READMOD	3	/* Read partition is pending */
64 #define RAW3270_STATE_READY	4	/* Device is usable by views */
65 
66 /* raw3270->flags */
67 #define RAW3270_FLAGS_14BITADDR	0	/* 14-bit buffer addresses */
68 #define RAW3270_FLAGS_BUSY	1	/* Device busy, leave it alone */
69 #define RAW3270_FLAGS_CONSOLE	2	/* Device is the console. */
70 
71 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
72 static DEFINE_MUTEX(raw3270_mutex);
73 
74 /* List of 3270 devices. */
75 static LIST_HEAD(raw3270_devices);
76 
77 /*
78  * Flag to indicate if the driver has been registered. Some operations
79  * like waiting for the end of i/o need to be done differently as long
80  * as the kernel is still starting up (console support).
81  */
82 static int raw3270_registered;
83 
84 /* Module parameters */
85 static bool tubxcorrect;
86 module_param(tubxcorrect, bool, 0);
87 
88 /*
89  * Wait queue for device init/delete, view delete.
90  */
91 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
92 
93 static void __raw3270_disconnect(struct raw3270 *rp);
94 
95 /*
96  * Encode array for 12 bit 3270 addresses.
97  */
98 static unsigned char raw3270_ebcgraf[64] =	{
99 	0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
100 	0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
101 	0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
102 	0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
103 	0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
104 	0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
105 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
106 	0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
107 };
108 
109 static inline int raw3270_state_ready(struct raw3270 *rp)
110 {
111 	return rp->state == RAW3270_STATE_READY;
112 }
113 
114 static inline int raw3270_state_final(struct raw3270 *rp)
115 {
116 	return rp->state == RAW3270_STATE_INIT ||
117 		rp->state == RAW3270_STATE_READY;
118 }
119 
120 void
121 raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
122 {
123 	if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
124 		cp[0] = (addr >> 8) & 0x3f;
125 		cp[1] = addr & 0xff;
126 	} else {
127 		cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
128 		cp[1] = raw3270_ebcgraf[addr & 0x3f];
129 	}
130 }
131 
132 /*
133  * Allocate a new 3270 ccw request
134  */
135 struct raw3270_request *
136 raw3270_request_alloc(size_t size)
137 {
138 	struct raw3270_request *rq;
139 
140 	/* Allocate request structure */
141 	rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
142 	if (!rq)
143 		return ERR_PTR(-ENOMEM);
144 
145 	/* alloc output buffer. */
146 	if (size > 0) {
147 		rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
148 		if (!rq->buffer) {
149 			kfree(rq);
150 			return ERR_PTR(-ENOMEM);
151 		}
152 	}
153 	rq->size = size;
154 	INIT_LIST_HEAD(&rq->list);
155 
156 	/*
157 	 * Setup ccw.
158 	 */
159 	rq->ccw.cda = __pa(rq->buffer);
160 	rq->ccw.flags = CCW_FLAG_SLI;
161 
162 	return rq;
163 }
164 
165 /*
166  * Free 3270 ccw request
167  */
168 void
169 raw3270_request_free (struct raw3270_request *rq)
170 {
171 	kfree(rq->buffer);
172 	kfree(rq);
173 }
174 
175 /*
176  * Reset request to initial state.
177  */
178 void
179 raw3270_request_reset(struct raw3270_request *rq)
180 {
181 	BUG_ON(!list_empty(&rq->list));
182 	rq->ccw.cmd_code = 0;
183 	rq->ccw.count = 0;
184 	rq->ccw.cda = __pa(rq->buffer);
185 	rq->ccw.flags = CCW_FLAG_SLI;
186 	rq->rescnt = 0;
187 	rq->rc = 0;
188 }
189 
190 /*
191  * Set command code to ccw of a request.
192  */
193 void
194 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
195 {
196 	rq->ccw.cmd_code = cmd;
197 }
198 
199 /*
200  * Add data fragment to output buffer.
201  */
202 int
203 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
204 {
205 	if (size + rq->ccw.count > rq->size)
206 		return -E2BIG;
207 	memcpy(rq->buffer + rq->ccw.count, data, size);
208 	rq->ccw.count += size;
209 	return 0;
210 }
211 
212 /*
213  * Set address/length pair to ccw of a request.
214  */
215 void
216 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
217 {
218 	rq->ccw.cda = __pa(data);
219 	rq->ccw.count = size;
220 }
221 
222 /*
223  * Set idal buffer to ccw of a request.
224  */
225 void
226 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
227 {
228 	rq->ccw.cda = __pa(ib->data);
229 	rq->ccw.count = ib->size;
230 	rq->ccw.flags |= CCW_FLAG_IDA;
231 }
232 
233 /*
234  * Add the request to the request queue, try to start it if the
235  * 3270 device is idle. Return without waiting for end of i/o.
236  */
237 static int
238 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
239 		struct raw3270_request *rq)
240 {
241 	rq->view = view;
242 	raw3270_get_view(view);
243 	if (list_empty(&rp->req_queue) &&
244 	    !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
245 		/* No other requests are on the queue. Start this one. */
246 		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
247 					       (unsigned long) rq, 0, 0);
248 		if (rq->rc) {
249 			raw3270_put_view(view);
250 			return rq->rc;
251 		}
252 	}
253 	list_add_tail(&rq->list, &rp->req_queue);
254 	return 0;
255 }
256 
257 int
258 raw3270_view_active(struct raw3270_view *view)
259 {
260 	struct raw3270 *rp = view->dev;
261 
262 	return rp && rp->view == view;
263 }
264 
265 int
266 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
267 {
268 	unsigned long flags;
269 	struct raw3270 *rp;
270 	int rc;
271 
272 	spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
273 	rp = view->dev;
274 	if (!rp || rp->view != view)
275 		rc = -EACCES;
276 	else if (!raw3270_state_ready(rp))
277 		rc = -EBUSY;
278 	else
279 		rc =  __raw3270_start(rp, view, rq);
280 	spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
281 	return rc;
282 }
283 
284 int
285 raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
286 {
287 	struct raw3270 *rp;
288 	int rc;
289 
290 	rp = view->dev;
291 	if (!rp || rp->view != view)
292 		rc = -EACCES;
293 	else if (!raw3270_state_ready(rp))
294 		rc = -EBUSY;
295 	else
296 		rc =  __raw3270_start(rp, view, rq);
297 	return rc;
298 }
299 
300 int
301 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
302 {
303 	struct raw3270 *rp;
304 
305 	rp = view->dev;
306 	rq->view = view;
307 	raw3270_get_view(view);
308 	list_add_tail(&rq->list, &rp->req_queue);
309 	return 0;
310 }
311 
312 /*
313  * 3270 interrupt routine, called from the ccw_device layer
314  */
315 static void
316 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
317 {
318 	struct raw3270 *rp;
319 	struct raw3270_view *view;
320 	struct raw3270_request *rq;
321 
322 	rp = dev_get_drvdata(&cdev->dev);
323 	if (!rp)
324 		return;
325 	rq = (struct raw3270_request *) intparm;
326 	view = rq ? rq->view : rp->view;
327 
328 	if (!IS_ERR(irb)) {
329 		/* Handle CE-DE-UE and subsequent UDE */
330 		if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END)
331 			clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
332 		if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END |
333 					    DEV_STAT_DEV_END |
334 					    DEV_STAT_UNIT_EXCEP))
335 			set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
336 		/* Handle disconnected devices */
337 		if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
338 		    (irb->ecw[0] & SNS0_INTERVENTION_REQ)) {
339 			set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
340 			if (rp->state > RAW3270_STATE_RESET)
341 				__raw3270_disconnect(rp);
342 		}
343 		/* Call interrupt handler of the view */
344 		if (view)
345 			view->fn->intv(view, rq, irb);
346 	}
347 
348 	if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags))
349 		/* Device busy, do not start I/O */
350 		return;
351 
352 	if (rq && !list_empty(&rq->list)) {
353 		/* The request completed, remove from queue and do callback. */
354 		list_del_init(&rq->list);
355 		if (rq->callback)
356 			rq->callback(rq, rq->callback_data);
357 		/* Do put_device for get_device in raw3270_start. */
358 		raw3270_put_view(view);
359 	}
360 
361 	/*
362 	 * Try to start each request on request queue until one is
363 	 * started successful.
364 	 */
365 	while (!list_empty(&rp->req_queue)) {
366 		rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
367 		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
368 					  (unsigned long) rq, 0, 0);
369 		if (rq->rc == 0)
370 			break;
371 		/* Start failed. Remove request and do callback. */
372 		list_del_init(&rq->list);
373 		if (rq->callback)
374 			rq->callback(rq, rq->callback_data);
375 		/* Do put_device for get_device in raw3270_start. */
376 		raw3270_put_view(view);
377 	}
378 }
379 
380 /*
381  * To determine the size of the 3270 device we need to do:
382  * 1) send a 'read partition' data stream to the device
383  * 2) wait for the attn interrupt that precedes the query reply
384  * 3) do a read modified to get the query reply
385  * To make things worse we have to cope with intervention
386  * required (3270 device switched to 'stand-by') and command
387  * rejects (old devices that can't do 'read partition').
388  */
389 struct raw3270_ua {	/* Query Reply structure for Usable Area */
390 	struct {	/* Usable Area Query Reply Base */
391 		short l;	/* Length of this structured field */
392 		char  sfid;	/* 0x81 if Query Reply */
393 		char  qcode;	/* 0x81 if Usable Area */
394 		char  flags0;
395 		char  flags1;
396 		short w;	/* Width of usable area */
397 		short h;	/* Heigth of usavle area */
398 		char  units;	/* 0x00:in; 0x01:mm */
399 		int   xr;
400 		int   yr;
401 		char  aw;
402 		char  ah;
403 		short buffsz;	/* Character buffer size, bytes */
404 		char  xmin;
405 		char  ymin;
406 		char  xmax;
407 		char  ymax;
408 	} __attribute__ ((packed)) uab;
409 	struct {	/* Alternate Usable Area Self-Defining Parameter */
410 		char  l;	/* Length of this Self-Defining Parm */
411 		char  sdpid;	/* 0x02 if Alternate Usable Area */
412 		char  res;
413 		char  auaid;	/* 0x01 is Id for the A U A */
414 		short wauai;	/* Width of AUAi */
415 		short hauai;	/* Height of AUAi */
416 		char  auaunits;	/* 0x00:in, 0x01:mm */
417 		int   auaxr;
418 		int   auayr;
419 		char  awauai;
420 		char  ahauai;
421 	} __attribute__ ((packed)) aua;
422 } __attribute__ ((packed));
423 
424 static void
425 raw3270_size_device_vm(struct raw3270 *rp)
426 {
427 	int rc, model;
428 	struct ccw_dev_id dev_id;
429 	struct diag210 diag_data;
430 
431 	ccw_device_get_id(rp->cdev, &dev_id);
432 	diag_data.vrdcdvno = dev_id.devno;
433 	diag_data.vrdclen = sizeof(struct diag210);
434 	rc = diag210(&diag_data);
435 	model = diag_data.vrdccrmd;
436 	/* Use default model 2 if the size could not be detected */
437 	if (rc || model < 2 || model > 5)
438 		model = 2;
439 	switch (model) {
440 	case 2:
441 		rp->model = model;
442 		rp->rows = 24;
443 		rp->cols = 80;
444 		break;
445 	case 3:
446 		rp->model = model;
447 		rp->rows = 32;
448 		rp->cols = 80;
449 		break;
450 	case 4:
451 		rp->model = model;
452 		rp->rows = 43;
453 		rp->cols = 80;
454 		break;
455 	case 5:
456 		rp->model = model;
457 		rp->rows = 27;
458 		rp->cols = 132;
459 		break;
460 	}
461 }
462 
463 static void
464 raw3270_size_device(struct raw3270 *rp)
465 {
466 	struct raw3270_ua *uap;
467 
468 	/* Got a Query Reply */
469 	uap = (struct raw3270_ua *) (rp->init_data + 1);
470 	/* Paranoia check. */
471 	if (rp->init_readmod.rc || rp->init_data[0] != 0x88 ||
472 	    uap->uab.qcode != 0x81) {
473 		/* Couldn't detect size. Use default model 2. */
474 		rp->model = 2;
475 		rp->rows = 24;
476 		rp->cols = 80;
477 		return;
478 	}
479 	/* Copy rows/columns of default Usable Area */
480 	rp->rows = uap->uab.h;
481 	rp->cols = uap->uab.w;
482 	/* Check for 14 bit addressing */
483 	if ((uap->uab.flags0 & 0x0d) == 0x01)
484 		set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
485 	/* Check for Alternate Usable Area */
486 	if (uap->uab.l == sizeof(struct raw3270_ua) &&
487 	    uap->aua.sdpid == 0x02) {
488 		rp->rows = uap->aua.hauai;
489 		rp->cols = uap->aua.wauai;
490 	}
491 	/* Try to find a model. */
492 	rp->model = 0;
493 	if (rp->rows == 24 && rp->cols == 80)
494 		rp->model = 2;
495 	if (rp->rows == 32 && rp->cols == 80)
496 		rp->model = 3;
497 	if (rp->rows == 43 && rp->cols == 80)
498 		rp->model = 4;
499 	if (rp->rows == 27 && rp->cols == 132)
500 		rp->model = 5;
501 }
502 
503 static void
504 raw3270_size_device_done(struct raw3270 *rp)
505 {
506 	struct raw3270_view *view;
507 
508 	rp->view = NULL;
509 	rp->state = RAW3270_STATE_READY;
510 	/* Notify views about new size */
511 	list_for_each_entry(view, &rp->view_list, list)
512 		if (view->fn->resize)
513 			view->fn->resize(view, rp->model, rp->rows, rp->cols);
514 	/* Setup processing done, now activate a view */
515 	list_for_each_entry(view, &rp->view_list, list) {
516 		rp->view = view;
517 		if (view->fn->activate(view) == 0)
518 			break;
519 		rp->view = NULL;
520 	}
521 }
522 
523 static void
524 raw3270_read_modified_cb(struct raw3270_request *rq, void *data)
525 {
526 	struct raw3270 *rp = rq->view->dev;
527 
528 	raw3270_size_device(rp);
529 	raw3270_size_device_done(rp);
530 }
531 
532 static void
533 raw3270_read_modified(struct raw3270 *rp)
534 {
535 	if (rp->state != RAW3270_STATE_W4ATTN)
536 		return;
537 	/* Use 'read modified' to get the result of a read partition. */
538 	memset(&rp->init_readmod, 0, sizeof(rp->init_readmod));
539 	memset(&rp->init_data, 0, sizeof(rp->init_data));
540 	rp->init_readmod.ccw.cmd_code = TC_READMOD;
541 	rp->init_readmod.ccw.flags = CCW_FLAG_SLI;
542 	rp->init_readmod.ccw.count = sizeof(rp->init_data);
543 	rp->init_readmod.ccw.cda = (__u32) __pa(rp->init_data);
544 	rp->init_readmod.callback = raw3270_read_modified_cb;
545 	rp->state = RAW3270_STATE_READMOD;
546 	raw3270_start_irq(&rp->init_view, &rp->init_readmod);
547 }
548 
549 static void
550 raw3270_writesf_readpart(struct raw3270 *rp)
551 {
552 	static const unsigned char wbuf[] =
553 		{ 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
554 
555 	/* Store 'read partition' data stream to init_data */
556 	memset(&rp->init_readpart, 0, sizeof(rp->init_readpart));
557 	memset(&rp->init_data, 0, sizeof(rp->init_data));
558 	memcpy(&rp->init_data, wbuf, sizeof(wbuf));
559 	rp->init_readpart.ccw.cmd_code = TC_WRITESF;
560 	rp->init_readpart.ccw.flags = CCW_FLAG_SLI;
561 	rp->init_readpart.ccw.count = sizeof(wbuf);
562 	rp->init_readpart.ccw.cda = (__u32) __pa(&rp->init_data);
563 	rp->state = RAW3270_STATE_W4ATTN;
564 	raw3270_start_irq(&rp->init_view, &rp->init_readpart);
565 }
566 
567 /*
568  * Device reset
569  */
570 static void
571 raw3270_reset_device_cb(struct raw3270_request *rq, void *data)
572 {
573 	struct raw3270 *rp = rq->view->dev;
574 
575 	if (rp->state != RAW3270_STATE_RESET)
576 		return;
577 	if (rq->rc) {
578 		/* Reset command failed. */
579 		rp->state = RAW3270_STATE_INIT;
580 	} else if (MACHINE_IS_VM) {
581 		raw3270_size_device_vm(rp);
582 		raw3270_size_device_done(rp);
583 	} else
584 		raw3270_writesf_readpart(rp);
585 	memset(&rp->init_reset, 0, sizeof(rp->init_reset));
586 }
587 
588 static int
589 __raw3270_reset_device(struct raw3270 *rp)
590 {
591 	int rc;
592 
593 	/* Check if reset is already pending */
594 	if (rp->init_reset.view)
595 		return -EBUSY;
596 	/* Store reset data stream to init_data/init_reset */
597 	rp->init_data[0] = TW_KR;
598 	rp->init_reset.ccw.cmd_code = TC_EWRITEA;
599 	rp->init_reset.ccw.flags = CCW_FLAG_SLI;
600 	rp->init_reset.ccw.count = 1;
601 	rp->init_reset.ccw.cda = (__u32) __pa(rp->init_data);
602 	rp->init_reset.callback = raw3270_reset_device_cb;
603 	rc = __raw3270_start(rp, &rp->init_view, &rp->init_reset);
604 	if (rc == 0 && rp->state == RAW3270_STATE_INIT)
605 		rp->state = RAW3270_STATE_RESET;
606 	return rc;
607 }
608 
609 static int
610 raw3270_reset_device(struct raw3270 *rp)
611 {
612 	unsigned long flags;
613 	int rc;
614 
615 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
616 	rc = __raw3270_reset_device(rp);
617 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
618 	return rc;
619 }
620 
621 int
622 raw3270_reset(struct raw3270_view *view)
623 {
624 	struct raw3270 *rp;
625 	int rc;
626 
627 	rp = view->dev;
628 	if (!rp || rp->view != view)
629 		rc = -EACCES;
630 	else if (!raw3270_state_ready(rp))
631 		rc = -EBUSY;
632 	else
633 		rc = raw3270_reset_device(view->dev);
634 	return rc;
635 }
636 
637 static void
638 __raw3270_disconnect(struct raw3270 *rp)
639 {
640 	struct raw3270_request *rq;
641 	struct raw3270_view *view;
642 
643 	rp->state = RAW3270_STATE_INIT;
644 	rp->view = &rp->init_view;
645 	/* Cancel all queued requests */
646 	while (!list_empty(&rp->req_queue)) {
647 		rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
648 		view = rq->view;
649 		rq->rc = -EACCES;
650 		list_del_init(&rq->list);
651 		if (rq->callback)
652 			rq->callback(rq, rq->callback_data);
653 		raw3270_put_view(view);
654 	}
655 	/* Start from scratch */
656 	__raw3270_reset_device(rp);
657 }
658 
659 static void
660 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
661 		 struct irb *irb)
662 {
663 	struct raw3270 *rp;
664 
665 	if (rq) {
666 		if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
667 			if (irb->ecw[0] & SNS0_CMD_REJECT)
668 				rq->rc = -EOPNOTSUPP;
669 			else
670 				rq->rc = -EIO;
671 		}
672 	}
673 	if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
674 		/* Queue read modified after attention interrupt */
675 		rp = view->dev;
676 		raw3270_read_modified(rp);
677 	}
678 }
679 
680 static struct raw3270_fn raw3270_init_fn = {
681 	.intv = raw3270_init_irq
682 };
683 
684 /*
685  * Setup new 3270 device.
686  */
687 static int
688 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
689 {
690 	struct list_head *l;
691 	struct raw3270 *tmp;
692 	int minor;
693 
694 	memset(rp, 0, sizeof(struct raw3270));
695 	/* Copy ebcdic -> ascii translation table. */
696 	memcpy(ascebc, _ascebc, 256);
697 	if (tubxcorrect) {
698 		/* correct brackets and circumflex */
699 		ascebc['['] = 0xad;
700 		ascebc[']'] = 0xbd;
701 		ascebc['^'] = 0xb0;
702 	}
703 	rp->ascebc = ascebc;
704 
705 	/* Set defaults. */
706 	rp->rows = 24;
707 	rp->cols = 80;
708 
709 	INIT_LIST_HEAD(&rp->req_queue);
710 	INIT_LIST_HEAD(&rp->view_list);
711 
712 	rp->init_view.dev = rp;
713 	rp->init_view.fn = &raw3270_init_fn;
714 	rp->view = &rp->init_view;
715 
716 	/*
717 	 * Add device to list and find the smallest unused minor
718 	 * number for it. Note: there is no device with minor 0,
719 	 * see special case for fs3270.c:fs3270_open().
720 	 */
721 	mutex_lock(&raw3270_mutex);
722 	/* Keep the list sorted. */
723 	minor = RAW3270_FIRSTMINOR;
724 	rp->minor = -1;
725 	list_for_each(l, &raw3270_devices) {
726 		tmp = list_entry(l, struct raw3270, list);
727 		if (tmp->minor > minor) {
728 			rp->minor = minor;
729 			__list_add(&rp->list, l->prev, l);
730 			break;
731 		}
732 		minor++;
733 	}
734 	if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
735 		rp->minor = minor;
736 		list_add_tail(&rp->list, &raw3270_devices);
737 	}
738 	mutex_unlock(&raw3270_mutex);
739 	/* No free minor number? Then give up. */
740 	if (rp->minor == -1)
741 		return -EUSERS;
742 	rp->cdev = cdev;
743 	dev_set_drvdata(&cdev->dev, rp);
744 	cdev->handler = raw3270_irq;
745 	return 0;
746 }
747 
748 #ifdef CONFIG_TN3270_CONSOLE
749 /* Tentative definition - see below for actual definition. */
750 static struct ccw_driver raw3270_ccw_driver;
751 
752 /*
753  * Setup 3270 device configured as console.
754  */
755 struct raw3270 __init *raw3270_setup_console(void)
756 {
757 	struct ccw_device *cdev;
758 	unsigned long flags;
759 	struct raw3270 *rp;
760 	char *ascebc;
761 	int rc;
762 
763 	cdev = ccw_device_create_console(&raw3270_ccw_driver);
764 	if (IS_ERR(cdev))
765 		return ERR_CAST(cdev);
766 
767 	rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
768 	ascebc = kzalloc(256, GFP_KERNEL);
769 	rc = raw3270_setup_device(cdev, rp, ascebc);
770 	if (rc)
771 		return ERR_PTR(rc);
772 	set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
773 
774 	rc = ccw_device_enable_console(cdev);
775 	if (rc) {
776 		ccw_device_destroy_console(cdev);
777 		return ERR_PTR(rc);
778 	}
779 
780 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
781 	do {
782 		__raw3270_reset_device(rp);
783 		while (!raw3270_state_final(rp)) {
784 			ccw_device_wait_idle(rp->cdev);
785 			barrier();
786 		}
787 	} while (rp->state != RAW3270_STATE_READY);
788 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
789 	return rp;
790 }
791 
792 void
793 raw3270_wait_cons_dev(struct raw3270 *rp)
794 {
795 	unsigned long flags;
796 
797 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
798 	ccw_device_wait_idle(rp->cdev);
799 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
800 }
801 
802 #endif
803 
804 /*
805  * Create a 3270 device structure.
806  */
807 static struct raw3270 *
808 raw3270_create_device(struct ccw_device *cdev)
809 {
810 	struct raw3270 *rp;
811 	char *ascebc;
812 	int rc;
813 
814 	rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
815 	if (!rp)
816 		return ERR_PTR(-ENOMEM);
817 	ascebc = kmalloc(256, GFP_KERNEL);
818 	if (!ascebc) {
819 		kfree(rp);
820 		return ERR_PTR(-ENOMEM);
821 	}
822 	rc = raw3270_setup_device(cdev, rp, ascebc);
823 	if (rc) {
824 		kfree(rp->ascebc);
825 		kfree(rp);
826 		rp = ERR_PTR(rc);
827 	}
828 	/* Get reference to ccw_device structure. */
829 	get_device(&cdev->dev);
830 	return rp;
831 }
832 
833 /*
834  * This helper just validates that it is safe to activate a
835  * view in the panic() context, due to locking restrictions.
836  */
837 int raw3270_view_lock_unavailable(struct raw3270_view *view)
838 {
839 	struct raw3270 *rp = view->dev;
840 
841 	if (!rp)
842 		return -ENODEV;
843 	if (spin_is_locked(get_ccwdev_lock(rp->cdev)))
844 		return -EBUSY;
845 	return 0;
846 }
847 
848 /*
849  * Activate a view.
850  */
851 int
852 raw3270_activate_view(struct raw3270_view *view)
853 {
854 	struct raw3270 *rp;
855 	struct raw3270_view *oldview, *nv;
856 	unsigned long flags;
857 	int rc;
858 
859 	rp = view->dev;
860 	if (!rp)
861 		return -ENODEV;
862 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
863 	if (rp->view == view)
864 		rc = 0;
865 	else if (!raw3270_state_ready(rp))
866 		rc = -EBUSY;
867 	else {
868 		oldview = NULL;
869 		if (rp->view && rp->view->fn->deactivate) {
870 			oldview = rp->view;
871 			oldview->fn->deactivate(oldview);
872 		}
873 		rp->view = view;
874 		rc = view->fn->activate(view);
875 		if (rc) {
876 			/* Didn't work. Try to reactivate the old view. */
877 			rp->view = oldview;
878 			if (!oldview || oldview->fn->activate(oldview) != 0) {
879 				/* Didn't work as well. Try any other view. */
880 				list_for_each_entry(nv, &rp->view_list, list)
881 					if (nv != view && nv != oldview) {
882 						rp->view = nv;
883 						if (nv->fn->activate(nv) == 0)
884 							break;
885 						rp->view = NULL;
886 					}
887 			}
888 		}
889 	}
890 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
891 	return rc;
892 }
893 
894 /*
895  * Deactivate current view.
896  */
897 void
898 raw3270_deactivate_view(struct raw3270_view *view)
899 {
900 	unsigned long flags;
901 	struct raw3270 *rp;
902 
903 	rp = view->dev;
904 	if (!rp)
905 		return;
906 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
907 	if (rp->view == view) {
908 		view->fn->deactivate(view);
909 		rp->view = NULL;
910 		/* Move deactivated view to end of list. */
911 		list_del_init(&view->list);
912 		list_add_tail(&view->list, &rp->view_list);
913 		/* Try to activate another view. */
914 		if (raw3270_state_ready(rp)) {
915 			list_for_each_entry(view, &rp->view_list, list) {
916 				rp->view = view;
917 				if (view->fn->activate(view) == 0)
918 					break;
919 				rp->view = NULL;
920 			}
921 		}
922 	}
923 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
924 }
925 
926 /*
927  * Add view to device with minor "minor".
928  */
929 int
930 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor, int subclass)
931 {
932 	unsigned long flags;
933 	struct raw3270 *rp;
934 	int rc;
935 
936 	if (minor <= 0)
937 		return -ENODEV;
938 	mutex_lock(&raw3270_mutex);
939 	rc = -ENODEV;
940 	list_for_each_entry(rp, &raw3270_devices, list) {
941 		if (rp->minor != minor)
942 			continue;
943 		spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
944 		atomic_set(&view->ref_count, 2);
945 		view->dev = rp;
946 		view->fn = fn;
947 		view->model = rp->model;
948 		view->rows = rp->rows;
949 		view->cols = rp->cols;
950 		view->ascebc = rp->ascebc;
951 		spin_lock_init(&view->lock);
952 		lockdep_set_subclass(&view->lock, subclass);
953 		list_add(&view->list, &rp->view_list);
954 		rc = 0;
955 		spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
956 		break;
957 	}
958 	mutex_unlock(&raw3270_mutex);
959 	return rc;
960 }
961 
962 /*
963  * Find specific view of device with minor "minor".
964  */
965 struct raw3270_view *
966 raw3270_find_view(struct raw3270_fn *fn, int minor)
967 {
968 	struct raw3270 *rp;
969 	struct raw3270_view *view, *tmp;
970 	unsigned long flags;
971 
972 	mutex_lock(&raw3270_mutex);
973 	view = ERR_PTR(-ENODEV);
974 	list_for_each_entry(rp, &raw3270_devices, list) {
975 		if (rp->minor != minor)
976 			continue;
977 		spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
978 		list_for_each_entry(tmp, &rp->view_list, list) {
979 			if (tmp->fn == fn) {
980 				raw3270_get_view(tmp);
981 				view = tmp;
982 				break;
983 			}
984 		}
985 		spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
986 		break;
987 	}
988 	mutex_unlock(&raw3270_mutex);
989 	return view;
990 }
991 
992 /*
993  * Remove view from device and free view structure via call to view->fn->free.
994  */
995 void
996 raw3270_del_view(struct raw3270_view *view)
997 {
998 	unsigned long flags;
999 	struct raw3270 *rp;
1000 	struct raw3270_view *nv;
1001 
1002 	rp = view->dev;
1003 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1004 	if (rp->view == view) {
1005 		view->fn->deactivate(view);
1006 		rp->view = NULL;
1007 	}
1008 	list_del_init(&view->list);
1009 	if (!rp->view && raw3270_state_ready(rp)) {
1010 		/* Try to activate another view. */
1011 		list_for_each_entry(nv, &rp->view_list, list) {
1012 			if (nv->fn->activate(nv) == 0) {
1013 				rp->view = nv;
1014 				break;
1015 			}
1016 		}
1017 	}
1018 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1019 	/* Wait for reference counter to drop to zero. */
1020 	atomic_dec(&view->ref_count);
1021 	wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1022 	if (view->fn->free)
1023 		view->fn->free(view);
1024 }
1025 
1026 /*
1027  * Remove a 3270 device structure.
1028  */
1029 static void
1030 raw3270_delete_device(struct raw3270 *rp)
1031 {
1032 	struct ccw_device *cdev;
1033 
1034 	/* Remove from device chain. */
1035 	mutex_lock(&raw3270_mutex);
1036 	list_del_init(&rp->list);
1037 	mutex_unlock(&raw3270_mutex);
1038 
1039 	/* Disconnect from ccw_device. */
1040 	cdev = rp->cdev;
1041 	rp->cdev = NULL;
1042 	dev_set_drvdata(&cdev->dev, NULL);
1043 	cdev->handler = NULL;
1044 
1045 	/* Put ccw_device structure. */
1046 	put_device(&cdev->dev);
1047 
1048 	/* Now free raw3270 structure. */
1049 	kfree(rp->ascebc);
1050 	kfree(rp);
1051 }
1052 
1053 static int
1054 raw3270_probe (struct ccw_device *cdev)
1055 {
1056 	return 0;
1057 }
1058 
1059 /*
1060  * Additional attributes for a 3270 device
1061  */
1062 static ssize_t
1063 raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
1064 {
1065 	return sysfs_emit(buf, "%i\n",
1066 			  ((struct raw3270 *)dev_get_drvdata(dev))->model);
1067 }
1068 static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
1069 
1070 static ssize_t
1071 raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
1072 {
1073 	return sysfs_emit(buf, "%i\n",
1074 			  ((struct raw3270 *)dev_get_drvdata(dev))->rows);
1075 }
1076 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
1077 
1078 static ssize_t
1079 raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
1080 {
1081 	return sysfs_emit(buf, "%i\n",
1082 			  ((struct raw3270 *)dev_get_drvdata(dev))->cols);
1083 }
1084 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
1085 
1086 static struct attribute * raw3270_attrs[] = {
1087 	&dev_attr_model.attr,
1088 	&dev_attr_rows.attr,
1089 	&dev_attr_columns.attr,
1090 	NULL,
1091 };
1092 
1093 static const struct attribute_group raw3270_attr_group = {
1094 	.attrs = raw3270_attrs,
1095 };
1096 
1097 static int raw3270_create_attributes(struct raw3270 *rp)
1098 {
1099 	return sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1100 }
1101 
1102 /*
1103  * Notifier for device addition/removal
1104  */
1105 static LIST_HEAD(raw3270_notifier);
1106 
1107 int raw3270_register_notifier(struct raw3270_notifier *notifier)
1108 {
1109 	struct raw3270 *rp;
1110 
1111 	mutex_lock(&raw3270_mutex);
1112 	list_add_tail(&notifier->list, &raw3270_notifier);
1113 	list_for_each_entry(rp, &raw3270_devices, list)
1114 		notifier->create(rp->minor);
1115 	mutex_unlock(&raw3270_mutex);
1116 	return 0;
1117 }
1118 
1119 void raw3270_unregister_notifier(struct raw3270_notifier *notifier)
1120 {
1121 	struct raw3270 *rp;
1122 
1123 	mutex_lock(&raw3270_mutex);
1124 	list_for_each_entry(rp, &raw3270_devices, list)
1125 		notifier->destroy(rp->minor);
1126 	list_del(&notifier->list);
1127 	mutex_unlock(&raw3270_mutex);
1128 }
1129 
1130 /*
1131  * Set 3270 device online.
1132  */
1133 static int
1134 raw3270_set_online (struct ccw_device *cdev)
1135 {
1136 	struct raw3270_notifier *np;
1137 	struct raw3270 *rp;
1138 	int rc;
1139 
1140 	rp = raw3270_create_device(cdev);
1141 	if (IS_ERR(rp))
1142 		return PTR_ERR(rp);
1143 	rc = raw3270_create_attributes(rp);
1144 	if (rc)
1145 		goto failure;
1146 	raw3270_reset_device(rp);
1147 	mutex_lock(&raw3270_mutex);
1148 	list_for_each_entry(np, &raw3270_notifier, list)
1149 		np->create(rp->minor);
1150 	mutex_unlock(&raw3270_mutex);
1151 	return 0;
1152 
1153 failure:
1154 	raw3270_delete_device(rp);
1155 	return rc;
1156 }
1157 
1158 /*
1159  * Remove 3270 device structure.
1160  */
1161 static void
1162 raw3270_remove (struct ccw_device *cdev)
1163 {
1164 	unsigned long flags;
1165 	struct raw3270 *rp;
1166 	struct raw3270_view *v;
1167 	struct raw3270_notifier *np;
1168 
1169 	rp = dev_get_drvdata(&cdev->dev);
1170 	/*
1171 	 * _remove is the opposite of _probe; it's probe that
1172 	 * should set up rp.  raw3270_remove gets entered for
1173 	 * devices even if they haven't been varied online.
1174 	 * Thus, rp may validly be NULL here.
1175 	 */
1176 	if (rp == NULL)
1177 		return;
1178 
1179 	sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1180 
1181 	/* Deactivate current view and remove all views. */
1182 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1183 	if (rp->view) {
1184 		if (rp->view->fn->deactivate)
1185 			rp->view->fn->deactivate(rp->view);
1186 		rp->view = NULL;
1187 	}
1188 	while (!list_empty(&rp->view_list)) {
1189 		v = list_entry(rp->view_list.next, struct raw3270_view, list);
1190 		if (v->fn->release)
1191 			v->fn->release(v);
1192 		spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1193 		raw3270_del_view(v);
1194 		spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1195 	}
1196 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1197 
1198 	mutex_lock(&raw3270_mutex);
1199 	list_for_each_entry(np, &raw3270_notifier, list)
1200 		np->destroy(rp->minor);
1201 	mutex_unlock(&raw3270_mutex);
1202 
1203 	/* Reset 3270 device. */
1204 	raw3270_reset_device(rp);
1205 	/* And finally remove it. */
1206 	raw3270_delete_device(rp);
1207 }
1208 
1209 /*
1210  * Set 3270 device offline.
1211  */
1212 static int
1213 raw3270_set_offline (struct ccw_device *cdev)
1214 {
1215 	struct raw3270 *rp;
1216 
1217 	rp = dev_get_drvdata(&cdev->dev);
1218 	if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1219 		return -EBUSY;
1220 	raw3270_remove(cdev);
1221 	return 0;
1222 }
1223 
1224 static struct ccw_device_id raw3270_id[] = {
1225 	{ CCW_DEVICE(0x3270, 0) },
1226 	{ CCW_DEVICE(0x3271, 0) },
1227 	{ CCW_DEVICE(0x3272, 0) },
1228 	{ CCW_DEVICE(0x3273, 0) },
1229 	{ CCW_DEVICE(0x3274, 0) },
1230 	{ CCW_DEVICE(0x3275, 0) },
1231 	{ CCW_DEVICE(0x3276, 0) },
1232 	{ CCW_DEVICE(0x3277, 0) },
1233 	{ CCW_DEVICE(0x3278, 0) },
1234 	{ CCW_DEVICE(0x3279, 0) },
1235 	{ CCW_DEVICE(0x3174, 0) },
1236 	{ /* end of list */ },
1237 };
1238 
1239 static struct ccw_driver raw3270_ccw_driver = {
1240 	.driver = {
1241 		.name	= "3270",
1242 		.owner	= THIS_MODULE,
1243 	},
1244 	.ids		= raw3270_id,
1245 	.probe		= &raw3270_probe,
1246 	.remove		= &raw3270_remove,
1247 	.set_online	= &raw3270_set_online,
1248 	.set_offline	= &raw3270_set_offline,
1249 	.int_class	= IRQIO_C70,
1250 };
1251 
1252 static int
1253 raw3270_init(void)
1254 {
1255 	struct raw3270 *rp;
1256 	int rc;
1257 
1258 	if (raw3270_registered)
1259 		return 0;
1260 	raw3270_registered = 1;
1261 	rc = ccw_driver_register(&raw3270_ccw_driver);
1262 	if (rc == 0) {
1263 		/* Create attributes for early (= console) device. */
1264 		mutex_lock(&raw3270_mutex);
1265 		class3270 = class_create(THIS_MODULE, "3270");
1266 		list_for_each_entry(rp, &raw3270_devices, list) {
1267 			get_device(&rp->cdev->dev);
1268 			raw3270_create_attributes(rp);
1269 		}
1270 		mutex_unlock(&raw3270_mutex);
1271 	}
1272 	return rc;
1273 }
1274 
1275 static void
1276 raw3270_exit(void)
1277 {
1278 	ccw_driver_unregister(&raw3270_ccw_driver);
1279 	class_destroy(class3270);
1280 }
1281 
1282 MODULE_LICENSE("GPL");
1283 
1284 module_init(raw3270_init);
1285 module_exit(raw3270_exit);
1286 
1287 EXPORT_SYMBOL(class3270);
1288 EXPORT_SYMBOL(raw3270_request_alloc);
1289 EXPORT_SYMBOL(raw3270_request_free);
1290 EXPORT_SYMBOL(raw3270_request_reset);
1291 EXPORT_SYMBOL(raw3270_request_set_cmd);
1292 EXPORT_SYMBOL(raw3270_request_add_data);
1293 EXPORT_SYMBOL(raw3270_request_set_data);
1294 EXPORT_SYMBOL(raw3270_request_set_idal);
1295 EXPORT_SYMBOL(raw3270_buffer_address);
1296 EXPORT_SYMBOL(raw3270_add_view);
1297 EXPORT_SYMBOL(raw3270_del_view);
1298 EXPORT_SYMBOL(raw3270_find_view);
1299 EXPORT_SYMBOL(raw3270_activate_view);
1300 EXPORT_SYMBOL(raw3270_deactivate_view);
1301 EXPORT_SYMBOL(raw3270_start);
1302 EXPORT_SYMBOL(raw3270_start_locked);
1303 EXPORT_SYMBOL(raw3270_start_irq);
1304 EXPORT_SYMBOL(raw3270_reset);
1305 EXPORT_SYMBOL(raw3270_register_notifier);
1306 EXPORT_SYMBOL(raw3270_unregister_notifier);
1307 EXPORT_SYMBOL(raw3270_wait_queue);
1308