xref: /openbmc/linux/drivers/s390/char/raw3270.c (revision ecfb9f40)
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 void
115 raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
116 {
117 	if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
118 		cp[0] = (addr >> 8) & 0x3f;
119 		cp[1] = addr & 0xff;
120 	} else {
121 		cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
122 		cp[1] = raw3270_ebcgraf[addr & 0x3f];
123 	}
124 }
125 
126 /*
127  * Allocate a new 3270 ccw request
128  */
129 struct raw3270_request *
130 raw3270_request_alloc(size_t size)
131 {
132 	struct raw3270_request *rq;
133 
134 	/* Allocate request structure */
135 	rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
136 	if (!rq)
137 		return ERR_PTR(-ENOMEM);
138 
139 	/* alloc output buffer. */
140 	if (size > 0) {
141 		rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
142 		if (!rq->buffer) {
143 			kfree(rq);
144 			return ERR_PTR(-ENOMEM);
145 		}
146 	}
147 	rq->size = size;
148 	INIT_LIST_HEAD(&rq->list);
149 
150 	/*
151 	 * Setup ccw.
152 	 */
153 	rq->ccw.cda = __pa(rq->buffer);
154 	rq->ccw.flags = CCW_FLAG_SLI;
155 
156 	return rq;
157 }
158 
159 /*
160  * Free 3270 ccw request
161  */
162 void
163 raw3270_request_free (struct raw3270_request *rq)
164 {
165 	kfree(rq->buffer);
166 	kfree(rq);
167 }
168 
169 /*
170  * Reset request to initial state.
171  */
172 void
173 raw3270_request_reset(struct raw3270_request *rq)
174 {
175 	BUG_ON(!list_empty(&rq->list));
176 	rq->ccw.cmd_code = 0;
177 	rq->ccw.count = 0;
178 	rq->ccw.cda = __pa(rq->buffer);
179 	rq->ccw.flags = CCW_FLAG_SLI;
180 	rq->rescnt = 0;
181 	rq->rc = 0;
182 }
183 
184 /*
185  * Set command code to ccw of a request.
186  */
187 void
188 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
189 {
190 	rq->ccw.cmd_code = cmd;
191 }
192 
193 /*
194  * Add data fragment to output buffer.
195  */
196 int
197 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
198 {
199 	if (size + rq->ccw.count > rq->size)
200 		return -E2BIG;
201 	memcpy(rq->buffer + rq->ccw.count, data, size);
202 	rq->ccw.count += size;
203 	return 0;
204 }
205 
206 /*
207  * Set address/length pair to ccw of a request.
208  */
209 void
210 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
211 {
212 	rq->ccw.cda = __pa(data);
213 	rq->ccw.count = size;
214 }
215 
216 /*
217  * Set idal buffer to ccw of a request.
218  */
219 void
220 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
221 {
222 	rq->ccw.cda = __pa(ib->data);
223 	rq->ccw.count = ib->size;
224 	rq->ccw.flags |= CCW_FLAG_IDA;
225 }
226 
227 /*
228  * Add the request to the request queue, try to start it if the
229  * 3270 device is idle. Return without waiting for end of i/o.
230  */
231 static int
232 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
233 		struct raw3270_request *rq)
234 {
235 	rq->view = view;
236 	raw3270_get_view(view);
237 	if (list_empty(&rp->req_queue) &&
238 	    !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
239 		/* No other requests are on the queue. Start this one. */
240 		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
241 					       (unsigned long) rq, 0, 0);
242 		if (rq->rc) {
243 			raw3270_put_view(view);
244 			return rq->rc;
245 		}
246 	}
247 	list_add_tail(&rq->list, &rp->req_queue);
248 	return 0;
249 }
250 
251 int
252 raw3270_view_active(struct raw3270_view *view)
253 {
254 	struct raw3270 *rp = view->dev;
255 
256 	return rp && rp->view == view;
257 }
258 
259 int
260 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
261 {
262 	unsigned long flags;
263 	struct raw3270 *rp;
264 	int rc;
265 
266 	spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
267 	rp = view->dev;
268 	if (!rp || rp->view != view)
269 		rc = -EACCES;
270 	else if (!raw3270_state_ready(rp))
271 		rc = -EBUSY;
272 	else
273 		rc =  __raw3270_start(rp, view, rq);
274 	spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
275 	return rc;
276 }
277 
278 int
279 raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
280 {
281 	struct raw3270 *rp;
282 	int rc;
283 
284 	rp = view->dev;
285 	if (!rp || rp->view != view)
286 		rc = -EACCES;
287 	else if (!raw3270_state_ready(rp))
288 		rc = -EBUSY;
289 	else
290 		rc =  __raw3270_start(rp, view, rq);
291 	return rc;
292 }
293 
294 int
295 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
296 {
297 	struct raw3270 *rp;
298 
299 	rp = view->dev;
300 	rq->view = view;
301 	raw3270_get_view(view);
302 	list_add_tail(&rq->list, &rp->req_queue);
303 	return 0;
304 }
305 
306 /*
307  * 3270 interrupt routine, called from the ccw_device layer
308  */
309 static void
310 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
311 {
312 	struct raw3270 *rp;
313 	struct raw3270_view *view;
314 	struct raw3270_request *rq;
315 
316 	rp = dev_get_drvdata(&cdev->dev);
317 	if (!rp)
318 		return;
319 	rq = (struct raw3270_request *) intparm;
320 	view = rq ? rq->view : rp->view;
321 
322 	if (!IS_ERR(irb)) {
323 		/* Handle CE-DE-UE and subsequent UDE */
324 		if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END)
325 			clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
326 		if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END |
327 					    DEV_STAT_DEV_END |
328 					    DEV_STAT_UNIT_EXCEP))
329 			set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
330 		/* Handle disconnected devices */
331 		if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
332 		    (irb->ecw[0] & SNS0_INTERVENTION_REQ)) {
333 			set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
334 			if (rp->state > RAW3270_STATE_RESET)
335 				__raw3270_disconnect(rp);
336 		}
337 		/* Call interrupt handler of the view */
338 		if (view)
339 			view->fn->intv(view, rq, irb);
340 	}
341 
342 	if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags))
343 		/* Device busy, do not start I/O */
344 		return;
345 
346 	if (rq && !list_empty(&rq->list)) {
347 		/* The request completed, remove from queue and do callback. */
348 		list_del_init(&rq->list);
349 		if (rq->callback)
350 			rq->callback(rq, rq->callback_data);
351 		/* Do put_device for get_device in raw3270_start. */
352 		raw3270_put_view(view);
353 	}
354 
355 	/*
356 	 * Try to start each request on request queue until one is
357 	 * started successful.
358 	 */
359 	while (!list_empty(&rp->req_queue)) {
360 		rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
361 		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
362 					  (unsigned long) rq, 0, 0);
363 		if (rq->rc == 0)
364 			break;
365 		/* Start failed. Remove request and do callback. */
366 		list_del_init(&rq->list);
367 		if (rq->callback)
368 			rq->callback(rq, rq->callback_data);
369 		/* Do put_device for get_device in raw3270_start. */
370 		raw3270_put_view(view);
371 	}
372 }
373 
374 /*
375  * To determine the size of the 3270 device we need to do:
376  * 1) send a 'read partition' data stream to the device
377  * 2) wait for the attn interrupt that precedes the query reply
378  * 3) do a read modified to get the query reply
379  * To make things worse we have to cope with intervention
380  * required (3270 device switched to 'stand-by') and command
381  * rejects (old devices that can't do 'read partition').
382  */
383 struct raw3270_ua {	/* Query Reply structure for Usable Area */
384 	struct {	/* Usable Area Query Reply Base */
385 		short l;	/* Length of this structured field */
386 		char  sfid;	/* 0x81 if Query Reply */
387 		char  qcode;	/* 0x81 if Usable Area */
388 		char  flags0;
389 		char  flags1;
390 		short w;	/* Width of usable area */
391 		short h;	/* Heigth of usavle area */
392 		char  units;	/* 0x00:in; 0x01:mm */
393 		int   xr;
394 		int   yr;
395 		char  aw;
396 		char  ah;
397 		short buffsz;	/* Character buffer size, bytes */
398 		char  xmin;
399 		char  ymin;
400 		char  xmax;
401 		char  ymax;
402 	} __attribute__ ((packed)) uab;
403 	struct {	/* Alternate Usable Area Self-Defining Parameter */
404 		char  l;	/* Length of this Self-Defining Parm */
405 		char  sdpid;	/* 0x02 if Alternate Usable Area */
406 		char  res;
407 		char  auaid;	/* 0x01 is Id for the A U A */
408 		short wauai;	/* Width of AUAi */
409 		short hauai;	/* Height of AUAi */
410 		char  auaunits;	/* 0x00:in, 0x01:mm */
411 		int   auaxr;
412 		int   auayr;
413 		char  awauai;
414 		char  ahauai;
415 	} __attribute__ ((packed)) aua;
416 } __attribute__ ((packed));
417 
418 static void
419 raw3270_size_device_vm(struct raw3270 *rp)
420 {
421 	int rc, model;
422 	struct ccw_dev_id dev_id;
423 	struct diag210 diag_data;
424 
425 	ccw_device_get_id(rp->cdev, &dev_id);
426 	diag_data.vrdcdvno = dev_id.devno;
427 	diag_data.vrdclen = sizeof(struct diag210);
428 	rc = diag210(&diag_data);
429 	model = diag_data.vrdccrmd;
430 	/* Use default model 2 if the size could not be detected */
431 	if (rc || model < 2 || model > 5)
432 		model = 2;
433 	switch (model) {
434 	case 2:
435 		rp->model = model;
436 		rp->rows = 24;
437 		rp->cols = 80;
438 		break;
439 	case 3:
440 		rp->model = model;
441 		rp->rows = 32;
442 		rp->cols = 80;
443 		break;
444 	case 4:
445 		rp->model = model;
446 		rp->rows = 43;
447 		rp->cols = 80;
448 		break;
449 	case 5:
450 		rp->model = model;
451 		rp->rows = 27;
452 		rp->cols = 132;
453 		break;
454 	}
455 }
456 
457 static void
458 raw3270_size_device(struct raw3270 *rp)
459 {
460 	struct raw3270_ua *uap;
461 
462 	/* Got a Query Reply */
463 	uap = (struct raw3270_ua *) (rp->init_data + 1);
464 	/* Paranoia check. */
465 	if (rp->init_readmod.rc || rp->init_data[0] != 0x88 ||
466 	    uap->uab.qcode != 0x81) {
467 		/* Couldn't detect size. Use default model 2. */
468 		rp->model = 2;
469 		rp->rows = 24;
470 		rp->cols = 80;
471 		return;
472 	}
473 	/* Copy rows/columns of default Usable Area */
474 	rp->rows = uap->uab.h;
475 	rp->cols = uap->uab.w;
476 	/* Check for 14 bit addressing */
477 	if ((uap->uab.flags0 & 0x0d) == 0x01)
478 		set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
479 	/* Check for Alternate Usable Area */
480 	if (uap->uab.l == sizeof(struct raw3270_ua) &&
481 	    uap->aua.sdpid == 0x02) {
482 		rp->rows = uap->aua.hauai;
483 		rp->cols = uap->aua.wauai;
484 	}
485 	/* Try to find a model. */
486 	rp->model = 0;
487 	if (rp->rows == 24 && rp->cols == 80)
488 		rp->model = 2;
489 	if (rp->rows == 32 && rp->cols == 80)
490 		rp->model = 3;
491 	if (rp->rows == 43 && rp->cols == 80)
492 		rp->model = 4;
493 	if (rp->rows == 27 && rp->cols == 132)
494 		rp->model = 5;
495 }
496 
497 static void
498 raw3270_size_device_done(struct raw3270 *rp)
499 {
500 	struct raw3270_view *view;
501 
502 	rp->view = NULL;
503 	rp->state = RAW3270_STATE_READY;
504 	/* Notify views about new size */
505 	list_for_each_entry(view, &rp->view_list, list)
506 		if (view->fn->resize)
507 			view->fn->resize(view, rp->model, rp->rows, rp->cols);
508 	/* Setup processing done, now activate a view */
509 	list_for_each_entry(view, &rp->view_list, list) {
510 		rp->view = view;
511 		if (view->fn->activate(view) == 0)
512 			break;
513 		rp->view = NULL;
514 	}
515 }
516 
517 static void
518 raw3270_read_modified_cb(struct raw3270_request *rq, void *data)
519 {
520 	struct raw3270 *rp = rq->view->dev;
521 
522 	raw3270_size_device(rp);
523 	raw3270_size_device_done(rp);
524 }
525 
526 static void
527 raw3270_read_modified(struct raw3270 *rp)
528 {
529 	if (rp->state != RAW3270_STATE_W4ATTN)
530 		return;
531 	/* Use 'read modified' to get the result of a read partition. */
532 	memset(&rp->init_readmod, 0, sizeof(rp->init_readmod));
533 	memset(&rp->init_data, 0, sizeof(rp->init_data));
534 	rp->init_readmod.ccw.cmd_code = TC_READMOD;
535 	rp->init_readmod.ccw.flags = CCW_FLAG_SLI;
536 	rp->init_readmod.ccw.count = sizeof(rp->init_data);
537 	rp->init_readmod.ccw.cda = (__u32) __pa(rp->init_data);
538 	rp->init_readmod.callback = raw3270_read_modified_cb;
539 	rp->state = RAW3270_STATE_READMOD;
540 	raw3270_start_irq(&rp->init_view, &rp->init_readmod);
541 }
542 
543 static void
544 raw3270_writesf_readpart(struct raw3270 *rp)
545 {
546 	static const unsigned char wbuf[] =
547 		{ 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
548 
549 	/* Store 'read partition' data stream to init_data */
550 	memset(&rp->init_readpart, 0, sizeof(rp->init_readpart));
551 	memset(&rp->init_data, 0, sizeof(rp->init_data));
552 	memcpy(&rp->init_data, wbuf, sizeof(wbuf));
553 	rp->init_readpart.ccw.cmd_code = TC_WRITESF;
554 	rp->init_readpart.ccw.flags = CCW_FLAG_SLI;
555 	rp->init_readpart.ccw.count = sizeof(wbuf);
556 	rp->init_readpart.ccw.cda = (__u32) __pa(&rp->init_data);
557 	rp->state = RAW3270_STATE_W4ATTN;
558 	raw3270_start_irq(&rp->init_view, &rp->init_readpart);
559 }
560 
561 /*
562  * Device reset
563  */
564 static void
565 raw3270_reset_device_cb(struct raw3270_request *rq, void *data)
566 {
567 	struct raw3270 *rp = rq->view->dev;
568 
569 	if (rp->state != RAW3270_STATE_RESET)
570 		return;
571 	if (rq->rc) {
572 		/* Reset command failed. */
573 		rp->state = RAW3270_STATE_INIT;
574 	} else if (MACHINE_IS_VM) {
575 		raw3270_size_device_vm(rp);
576 		raw3270_size_device_done(rp);
577 	} else
578 		raw3270_writesf_readpart(rp);
579 	memset(&rp->init_reset, 0, sizeof(rp->init_reset));
580 }
581 
582 static int
583 __raw3270_reset_device(struct raw3270 *rp)
584 {
585 	int rc;
586 
587 	/* Check if reset is already pending */
588 	if (rp->init_reset.view)
589 		return -EBUSY;
590 	/* Store reset data stream to init_data/init_reset */
591 	rp->init_data[0] = TW_KR;
592 	rp->init_reset.ccw.cmd_code = TC_EWRITEA;
593 	rp->init_reset.ccw.flags = CCW_FLAG_SLI;
594 	rp->init_reset.ccw.count = 1;
595 	rp->init_reset.ccw.cda = (__u32) __pa(rp->init_data);
596 	rp->init_reset.callback = raw3270_reset_device_cb;
597 	rc = __raw3270_start(rp, &rp->init_view, &rp->init_reset);
598 	if (rc == 0 && rp->state == RAW3270_STATE_INIT)
599 		rp->state = RAW3270_STATE_RESET;
600 	return rc;
601 }
602 
603 static int
604 raw3270_reset_device(struct raw3270 *rp)
605 {
606 	unsigned long flags;
607 	int rc;
608 
609 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
610 	rc = __raw3270_reset_device(rp);
611 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
612 	return rc;
613 }
614 
615 int
616 raw3270_reset(struct raw3270_view *view)
617 {
618 	struct raw3270 *rp;
619 	int rc;
620 
621 	rp = view->dev;
622 	if (!rp || rp->view != view)
623 		rc = -EACCES;
624 	else if (!raw3270_state_ready(rp))
625 		rc = -EBUSY;
626 	else
627 		rc = raw3270_reset_device(view->dev);
628 	return rc;
629 }
630 
631 static void
632 __raw3270_disconnect(struct raw3270 *rp)
633 {
634 	struct raw3270_request *rq;
635 	struct raw3270_view *view;
636 
637 	rp->state = RAW3270_STATE_INIT;
638 	rp->view = &rp->init_view;
639 	/* Cancel all queued requests */
640 	while (!list_empty(&rp->req_queue)) {
641 		rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
642 		view = rq->view;
643 		rq->rc = -EACCES;
644 		list_del_init(&rq->list);
645 		if (rq->callback)
646 			rq->callback(rq, rq->callback_data);
647 		raw3270_put_view(view);
648 	}
649 	/* Start from scratch */
650 	__raw3270_reset_device(rp);
651 }
652 
653 static void
654 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
655 		 struct irb *irb)
656 {
657 	struct raw3270 *rp;
658 
659 	if (rq) {
660 		if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
661 			if (irb->ecw[0] & SNS0_CMD_REJECT)
662 				rq->rc = -EOPNOTSUPP;
663 			else
664 				rq->rc = -EIO;
665 		}
666 	}
667 	if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
668 		/* Queue read modified after attention interrupt */
669 		rp = view->dev;
670 		raw3270_read_modified(rp);
671 	}
672 }
673 
674 static struct raw3270_fn raw3270_init_fn = {
675 	.intv = raw3270_init_irq
676 };
677 
678 /*
679  * Setup new 3270 device.
680  */
681 static int
682 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
683 {
684 	struct list_head *l;
685 	struct raw3270 *tmp;
686 	int minor;
687 
688 	memset(rp, 0, sizeof(struct raw3270));
689 	/* Copy ebcdic -> ascii translation table. */
690 	memcpy(ascebc, _ascebc, 256);
691 	if (tubxcorrect) {
692 		/* correct brackets and circumflex */
693 		ascebc['['] = 0xad;
694 		ascebc[']'] = 0xbd;
695 		ascebc['^'] = 0xb0;
696 	}
697 	rp->ascebc = ascebc;
698 
699 	/* Set defaults. */
700 	rp->rows = 24;
701 	rp->cols = 80;
702 
703 	INIT_LIST_HEAD(&rp->req_queue);
704 	INIT_LIST_HEAD(&rp->view_list);
705 
706 	rp->init_view.dev = rp;
707 	rp->init_view.fn = &raw3270_init_fn;
708 	rp->view = &rp->init_view;
709 
710 	/*
711 	 * Add device to list and find the smallest unused minor
712 	 * number for it. Note: there is no device with minor 0,
713 	 * see special case for fs3270.c:fs3270_open().
714 	 */
715 	mutex_lock(&raw3270_mutex);
716 	/* Keep the list sorted. */
717 	minor = RAW3270_FIRSTMINOR;
718 	rp->minor = -1;
719 	list_for_each(l, &raw3270_devices) {
720 		tmp = list_entry(l, struct raw3270, list);
721 		if (tmp->minor > minor) {
722 			rp->minor = minor;
723 			__list_add(&rp->list, l->prev, l);
724 			break;
725 		}
726 		minor++;
727 	}
728 	if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
729 		rp->minor = minor;
730 		list_add_tail(&rp->list, &raw3270_devices);
731 	}
732 	mutex_unlock(&raw3270_mutex);
733 	/* No free minor number? Then give up. */
734 	if (rp->minor == -1)
735 		return -EUSERS;
736 	rp->cdev = cdev;
737 	dev_set_drvdata(&cdev->dev, rp);
738 	cdev->handler = raw3270_irq;
739 	return 0;
740 }
741 
742 #ifdef CONFIG_TN3270_CONSOLE
743 /* Tentative definition - see below for actual definition. */
744 static struct ccw_driver raw3270_ccw_driver;
745 
746 static inline int raw3270_state_final(struct raw3270 *rp)
747 {
748 	return rp->state == RAW3270_STATE_INIT ||
749 		rp->state == RAW3270_STATE_READY;
750 }
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