2 * Character device driver for extended error reporting.
4 * Copyright (C) 2005 IBM Corporation
5 * extended error reporting for DASD ECKD devices
6 * Author(s): Stefan Weinhuber <wein@de.ibm.com>
9 #define KMSG_COMPONENT "dasd"
11 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/device.h>
18 #include <linux/poll.h>
19 #include <linux/mutex.h>
20 #include <linux/smp_lock.h>
21 #include <linux/err.h>
23 #include <asm/uaccess.h>
24 #include <asm/atomic.h>
25 #include <asm/ebcdic.h>
28 #include "dasd_eckd.h"
32 #endif /* PRINTK_HEADER */
33 #define PRINTK_HEADER "dasd(eer):"
36 * SECTION: the internal buffer
40 * The internal buffer is meant to store obaque blobs of data, so it does
41 * not know of higher level concepts like triggers.
42 * It consists of a number of pages that are used as a ringbuffer. Each data
43 * blob is stored in a simple record that consists of an integer, which
44 * contains the size of the following data, and the data bytes themselfes.
46 * To allow for multiple independent readers we create one internal buffer
47 * each time the device is opened and destroy the buffer when the file is
48 * closed again. The number of pages used for this buffer is determined by
49 * the module parmeter eer_pages.
51 * One record can be written to a buffer by using the functions
52 * - dasd_eer_start_record (one time per record to write the size to the
53 * buffer and reserve the space for the data)
54 * - dasd_eer_write_buffer (one or more times per record to write the data)
55 * The data can be written in several steps but you will have to compute
56 * the total size up front for the invocation of dasd_eer_start_record.
57 * If the ringbuffer is full, dasd_eer_start_record will remove the required
58 * number of old records.
60 * A record is typically read in two steps, first read the integer that
61 * specifies the size of the following data, then read the data.
63 * - dasd_eer_read_buffer
65 * For all mentioned functions you need to get the bufferlock first and keep
66 * it until a complete record is written or read.
68 * All information necessary to keep track of an internal buffer is kept in
69 * a struct eerbuffer. The buffer specific to a file pointer is strored in
70 * the private_data field of that file. To be able to write data to all
71 * existing buffers, each buffer is also added to the bufferlist.
72 * If the user does not want to read a complete record in one go, we have to
73 * keep track of the rest of the record. residual stores the number of bytes
74 * that are still to deliver. If the rest of the record is invalidated between
75 * two reads then residual will be set to -1 so that the next read will fail.
76 * All entries in the eerbuffer structure are protected with the bufferlock.
77 * To avoid races between writing to a buffer on the one side and creating
78 * and destroying buffers on the other side, the bufferlock must also be used
79 * to protect the bufferlist.
82 static int eer_pages = 5;
83 module_param(eer_pages, int, S_IRUGO|S_IWUSR);
86 struct list_head list;
89 int buffer_page_count;
95 static LIST_HEAD(bufferlist);
96 static DEFINE_SPINLOCK(bufferlock);
97 static DECLARE_WAIT_QUEUE_HEAD(dasd_eer_read_wait_queue);
100 * How many free bytes are available on the buffer.
101 * Needs to be called with bufferlock held.
103 static int dasd_eer_get_free_bytes(struct eerbuffer *eerb)
105 if (eerb->head < eerb->tail)
106 return eerb->tail - eerb->head - 1;
107 return eerb->buffersize - eerb->head + eerb->tail -1;
111 * How many bytes of buffer space are used.
112 * Needs to be called with bufferlock held.
114 static int dasd_eer_get_filled_bytes(struct eerbuffer *eerb)
117 if (eerb->head >= eerb->tail)
118 return eerb->head - eerb->tail;
119 return eerb->buffersize - eerb->tail + eerb->head;
123 * The dasd_eer_write_buffer function just copies count bytes of data
124 * to the buffer. Make sure to call dasd_eer_start_record first, to
125 * make sure that enough free space is available.
126 * Needs to be called with bufferlock held.
128 static void dasd_eer_write_buffer(struct eerbuffer *eerb,
129 char *data, int count)
132 unsigned long headindex,localhead;
133 unsigned long rest, len;
139 headindex = eerb->head / PAGE_SIZE;
140 localhead = eerb->head % PAGE_SIZE;
141 len = min(rest, PAGE_SIZE - localhead);
142 memcpy(eerb->buffer[headindex]+localhead, nextdata, len);
146 if (eerb->head == eerb->buffersize)
147 eerb->head = 0; /* wrap around */
148 BUG_ON(eerb->head > eerb->buffersize);
153 * Needs to be called with bufferlock held.
155 static int dasd_eer_read_buffer(struct eerbuffer *eerb, char *data, int count)
158 unsigned long tailindex,localtail;
159 unsigned long rest, len, finalcount;
162 finalcount = min(count, dasd_eer_get_filled_bytes(eerb));
166 tailindex = eerb->tail / PAGE_SIZE;
167 localtail = eerb->tail % PAGE_SIZE;
168 len = min(rest, PAGE_SIZE - localtail);
169 memcpy(nextdata, eerb->buffer[tailindex] + localtail, len);
173 if (eerb->tail == eerb->buffersize)
174 eerb->tail = 0; /* wrap around */
175 BUG_ON(eerb->tail > eerb->buffersize);
181 * Whenever you want to write a blob of data to the internal buffer you
182 * have to start by using this function first. It will write the number
183 * of bytes that will be written to the buffer. If necessary it will remove
184 * old records to make room for the new one.
185 * Needs to be called with bufferlock held.
187 static int dasd_eer_start_record(struct eerbuffer *eerb, int count)
191 if (count + sizeof(count) > eerb->buffersize)
193 while (dasd_eer_get_free_bytes(eerb) < count + sizeof(count)) {
194 if (eerb->residual > 0) {
195 eerb->tail += eerb->residual;
196 if (eerb->tail >= eerb->buffersize)
197 eerb->tail -= eerb->buffersize;
200 dasd_eer_read_buffer(eerb, (char *) &tailcount,
202 eerb->tail += tailcount;
203 if (eerb->tail >= eerb->buffersize)
204 eerb->tail -= eerb->buffersize;
206 dasd_eer_write_buffer(eerb, (char*) &count, sizeof(count));
212 * Release pages that are not used anymore.
214 static void dasd_eer_free_buffer_pages(char **buf, int no_pages)
218 for (i = 0; i < no_pages; i++)
219 free_page((unsigned long) buf[i]);
223 * Allocate a new set of memory pages.
225 static int dasd_eer_allocate_buffer_pages(char **buf, int no_pages)
229 for (i = 0; i < no_pages; i++) {
230 buf[i] = (char *) get_zeroed_page(GFP_KERNEL);
232 dasd_eer_free_buffer_pages(buf, i);
240 * SECTION: The extended error reporting functionality
244 * When a DASD device driver wants to report an error, it calls the
245 * function dasd_eer_write and gives the respective trigger ID as
246 * parameter. Currently there are four kinds of triggers:
248 * DASD_EER_FATALERROR: all kinds of unrecoverable I/O problems
249 * DASD_EER_PPRCSUSPEND: PPRC was suspended
250 * DASD_EER_NOPATH: There is no path to the device left.
251 * DASD_EER_STATECHANGE: The state of the device has changed.
253 * For the first three triggers all required information can be supplied by
254 * the caller. For these triggers a record is written by the function
255 * dasd_eer_write_standard_trigger.
257 * The DASD_EER_STATECHANGE trigger is special since a sense subsystem
258 * status ccw need to be executed to gather the necessary sense data first.
259 * The dasd_eer_snss function will queue the SNSS request and the request
260 * callback will then call dasd_eer_write with the DASD_EER_STATCHANGE
263 * To avoid memory allocations at runtime, the necessary memory is allocated
264 * when the extended error reporting is enabled for a device (by
265 * dasd_eer_probe). There is one sense subsystem status request for each
266 * eer enabled DASD device. The presence of the cqr in device->eer_cqr
267 * indicates that eer is enable for the device. The use of the snss request
268 * is protected by the DASD_FLAG_EER_IN_USE bit. When this flag indicates
269 * that the cqr is currently in use, dasd_eer_snss cannot start a second
270 * request but sets the DASD_FLAG_EER_SNSS flag instead. The callback of
271 * the SNSS request will check the bit and call dasd_eer_snss again.
274 #define SNSS_DATA_SIZE 44
276 #define DASD_EER_BUSID_SIZE 10
277 struct dasd_eer_header {
282 char busid[DASD_EER_BUSID_SIZE];
283 } __attribute__ ((packed));
286 * The following function can be used for those triggers that have
287 * all necessary data available when the function is called.
288 * If the parameter cqr is not NULL, the chain of requests will be searched
289 * for valid sense data, and all valid sense data sets will be added to
292 static void dasd_eer_write_standard_trigger(struct dasd_device *device,
293 struct dasd_ccw_req *cqr,
296 struct dasd_ccw_req *temp_cqr;
299 struct dasd_eer_header header;
301 struct eerbuffer *eerb;
304 /* go through cqr chain and count the valid sense data sets */
306 for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers)
307 if (dasd_get_sense(&temp_cqr->irb))
310 header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
311 header.trigger = trigger;
312 do_gettimeofday(&tv);
313 header.tv_sec = tv.tv_sec;
314 header.tv_usec = tv.tv_usec;
315 strncpy(header.busid, dev_name(&device->cdev->dev),
316 DASD_EER_BUSID_SIZE);
318 spin_lock_irqsave(&bufferlock, flags);
319 list_for_each_entry(eerb, &bufferlist, list) {
320 dasd_eer_start_record(eerb, header.total_size);
321 dasd_eer_write_buffer(eerb, (char *) &header, sizeof(header));
322 for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers) {
323 sense = dasd_get_sense(&temp_cqr->irb);
325 dasd_eer_write_buffer(eerb, sense, 32);
327 dasd_eer_write_buffer(eerb, "EOR", 4);
329 spin_unlock_irqrestore(&bufferlock, flags);
330 wake_up_interruptible(&dasd_eer_read_wait_queue);
334 * This function writes a DASD_EER_STATECHANGE trigger.
336 static void dasd_eer_write_snss_trigger(struct dasd_device *device,
337 struct dasd_ccw_req *cqr,
343 struct dasd_eer_header header;
345 struct eerbuffer *eerb;
347 snss_rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
351 data_size = SNSS_DATA_SIZE;
353 header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
354 header.trigger = DASD_EER_STATECHANGE;
355 do_gettimeofday(&tv);
356 header.tv_sec = tv.tv_sec;
357 header.tv_usec = tv.tv_usec;
358 strncpy(header.busid, dev_name(&device->cdev->dev),
359 DASD_EER_BUSID_SIZE);
361 spin_lock_irqsave(&bufferlock, flags);
362 list_for_each_entry(eerb, &bufferlist, list) {
363 dasd_eer_start_record(eerb, header.total_size);
364 dasd_eer_write_buffer(eerb, (char *) &header , sizeof(header));
366 dasd_eer_write_buffer(eerb, cqr->data, SNSS_DATA_SIZE);
367 dasd_eer_write_buffer(eerb, "EOR", 4);
369 spin_unlock_irqrestore(&bufferlock, flags);
370 wake_up_interruptible(&dasd_eer_read_wait_queue);
374 * This function is called for all triggers. It calls the appropriate
375 * function that writes the actual trigger records.
377 void dasd_eer_write(struct dasd_device *device, struct dasd_ccw_req *cqr,
380 if (!device->eer_cqr)
383 case DASD_EER_FATALERROR:
384 case DASD_EER_PPRCSUSPEND:
385 dasd_eer_write_standard_trigger(device, cqr, id);
387 case DASD_EER_NOPATH:
388 dasd_eer_write_standard_trigger(device, NULL, id);
390 case DASD_EER_STATECHANGE:
391 dasd_eer_write_snss_trigger(device, cqr, id);
393 default: /* unknown trigger, so we write it without any sense data */
394 dasd_eer_write_standard_trigger(device, NULL, id);
398 EXPORT_SYMBOL(dasd_eer_write);
401 * Start a sense subsystem status request.
402 * Needs to be called with the device held.
404 void dasd_eer_snss(struct dasd_device *device)
406 struct dasd_ccw_req *cqr;
408 cqr = device->eer_cqr;
409 if (!cqr) /* Device not eer enabled. */
411 if (test_and_set_bit(DASD_FLAG_EER_IN_USE, &device->flags)) {
412 /* Sense subsystem status request in use. */
413 set_bit(DASD_FLAG_EER_SNSS, &device->flags);
416 /* cdev is already locked, can't use dasd_add_request_head */
417 clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
418 cqr->status = DASD_CQR_QUEUED;
419 list_add(&cqr->devlist, &device->ccw_queue);
420 dasd_schedule_device_bh(device);
424 * Callback function for use with sense subsystem status request.
426 static void dasd_eer_snss_cb(struct dasd_ccw_req *cqr, void *data)
428 struct dasd_device *device = cqr->startdev;
431 dasd_eer_write(device, cqr, DASD_EER_STATECHANGE);
432 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
433 if (device->eer_cqr == cqr) {
434 clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
435 if (test_bit(DASD_FLAG_EER_SNSS, &device->flags))
436 /* Another SNSS has been requested in the meantime. */
437 dasd_eer_snss(device);
440 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
443 * Extended error recovery has been switched off while
444 * the SNSS request was running. It could even have
445 * been switched off and on again in which case there
446 * is a new ccw in device->eer_cqr. Free the "old"
449 dasd_kfree_request(cqr, device);
453 * Enable error reporting on a given device.
455 int dasd_eer_enable(struct dasd_device *device)
457 struct dasd_ccw_req *cqr;
464 if (!device->discipline || strcmp(device->discipline->name, "ECKD"))
465 return -EPERM; /* FIXME: -EMEDIUMTYPE ? */
467 cqr = dasd_kmalloc_request("ECKD", 1 /* SNSS */,
468 SNSS_DATA_SIZE, device);
472 cqr->startdev = device;
474 cqr->expires = 10 * HZ;
475 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
478 ccw->cmd_code = DASD_ECKD_CCW_SNSS;
479 ccw->count = SNSS_DATA_SIZE;
481 ccw->cda = (__u32)(addr_t) cqr->data;
483 cqr->buildclk = get_clock();
484 cqr->status = DASD_CQR_FILLED;
485 cqr->callback = dasd_eer_snss_cb;
487 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
488 if (!device->eer_cqr) {
489 device->eer_cqr = cqr;
492 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
494 dasd_kfree_request(cqr, device);
499 * Disable error reporting on a given device.
501 void dasd_eer_disable(struct dasd_device *device)
503 struct dasd_ccw_req *cqr;
507 if (!device->eer_cqr)
509 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
510 cqr = device->eer_cqr;
511 device->eer_cqr = NULL;
512 clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
513 in_use = test_and_clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
514 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
516 dasd_kfree_request(cqr, device);
520 * SECTION: the device operations
524 * On the one side we need a lock to access our internal buffer, on the
525 * other side a copy_to_user can sleep. So we need to copy the data we have
526 * to transfer in a readbuffer, which is protected by the readbuffer_mutex.
528 static char readbuffer[PAGE_SIZE];
529 static DEFINE_MUTEX(readbuffer_mutex);
531 static int dasd_eer_open(struct inode *inp, struct file *filp)
533 struct eerbuffer *eerb;
536 eerb = kzalloc(sizeof(struct eerbuffer), GFP_KERNEL);
540 eerb->buffer_page_count = eer_pages;
541 if (eerb->buffer_page_count < 1 ||
542 eerb->buffer_page_count > INT_MAX / PAGE_SIZE) {
544 DBF_EVENT(DBF_WARNING, "can't open device since module "
545 "parameter eer_pages is smaller than 1 or"
546 " bigger than %d", (int)(INT_MAX / PAGE_SIZE));
550 eerb->buffersize = eerb->buffer_page_count * PAGE_SIZE;
551 eerb->buffer = kmalloc(eerb->buffer_page_count * sizeof(char *),
558 if (dasd_eer_allocate_buffer_pages(eerb->buffer,
559 eerb->buffer_page_count)) {
565 filp->private_data = eerb;
566 spin_lock_irqsave(&bufferlock, flags);
567 list_add(&eerb->list, &bufferlist);
568 spin_unlock_irqrestore(&bufferlock, flags);
571 return nonseekable_open(inp,filp);
574 static int dasd_eer_close(struct inode *inp, struct file *filp)
576 struct eerbuffer *eerb;
579 eerb = (struct eerbuffer *) filp->private_data;
580 spin_lock_irqsave(&bufferlock, flags);
581 list_del(&eerb->list);
582 spin_unlock_irqrestore(&bufferlock, flags);
583 dasd_eer_free_buffer_pages(eerb->buffer, eerb->buffer_page_count);
590 static ssize_t dasd_eer_read(struct file *filp, char __user *buf,
591 size_t count, loff_t *ppos)
594 int tailcount,effective_count;
596 struct eerbuffer *eerb;
598 eerb = (struct eerbuffer *) filp->private_data;
599 if (mutex_lock_interruptible(&readbuffer_mutex))
602 spin_lock_irqsave(&bufferlock, flags);
604 if (eerb->residual < 0) { /* the remainder of this record */
605 /* has been deleted */
607 spin_unlock_irqrestore(&bufferlock, flags);
608 mutex_unlock(&readbuffer_mutex);
610 } else if (eerb->residual > 0) {
611 /* OK we still have a second half of a record to deliver */
612 effective_count = min(eerb->residual, (int) count);
613 eerb->residual -= effective_count;
617 tc = dasd_eer_read_buffer(eerb, (char *) &tailcount,
620 /* no data available */
621 spin_unlock_irqrestore(&bufferlock, flags);
622 mutex_unlock(&readbuffer_mutex);
623 if (filp->f_flags & O_NONBLOCK)
625 rc = wait_event_interruptible(
626 dasd_eer_read_wait_queue,
627 eerb->head != eerb->tail);
630 if (mutex_lock_interruptible(&readbuffer_mutex))
632 spin_lock_irqsave(&bufferlock, flags);
635 WARN_ON(tc != sizeof(tailcount));
636 effective_count = min(tailcount,(int)count);
637 eerb->residual = tailcount - effective_count;
640 tc = dasd_eer_read_buffer(eerb, readbuffer, effective_count);
641 WARN_ON(tc != effective_count);
643 spin_unlock_irqrestore(&bufferlock, flags);
645 if (copy_to_user(buf, readbuffer, effective_count)) {
646 mutex_unlock(&readbuffer_mutex);
650 mutex_unlock(&readbuffer_mutex);
651 return effective_count;
654 static unsigned int dasd_eer_poll(struct file *filp, poll_table *ptable)
658 struct eerbuffer *eerb;
660 eerb = (struct eerbuffer *) filp->private_data;
661 poll_wait(filp, &dasd_eer_read_wait_queue, ptable);
662 spin_lock_irqsave(&bufferlock, flags);
663 if (eerb->head != eerb->tail)
664 mask = POLLIN | POLLRDNORM ;
667 spin_unlock_irqrestore(&bufferlock, flags);
671 static const struct file_operations dasd_eer_fops = {
672 .open = &dasd_eer_open,
673 .release = &dasd_eer_close,
674 .read = &dasd_eer_read,
675 .poll = &dasd_eer_poll,
676 .owner = THIS_MODULE,
679 static struct miscdevice *dasd_eer_dev = NULL;
681 int __init dasd_eer_init(void)
685 dasd_eer_dev = kzalloc(sizeof(*dasd_eer_dev), GFP_KERNEL);
689 dasd_eer_dev->minor = MISC_DYNAMIC_MINOR;
690 dasd_eer_dev->name = "dasd_eer";
691 dasd_eer_dev->fops = &dasd_eer_fops;
693 rc = misc_register(dasd_eer_dev);
697 DBF_EVENT(DBF_ERR, "%s", "dasd_eer_init could not "
698 "register misc device");
705 void dasd_eer_exit(void)
708 WARN_ON(misc_deregister(dasd_eer_dev) != 0);