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 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/miscdevice.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/device.h>
16 #include <linux/poll.h>
17 #include <linux/mutex.h>
18 #include <linux/smp_lock.h>
19 #include <linux/err.h>
21 #include <asm/uaccess.h>
22 #include <asm/atomic.h>
23 #include <asm/ebcdic.h>
26 #include "dasd_eckd.h"
30 #endif /* PRINTK_HEADER */
31 #define PRINTK_HEADER "dasd(eer):"
34 * SECTION: the internal buffer
38 * The internal buffer is meant to store obaque blobs of data, so it does
39 * not know of higher level concepts like triggers.
40 * It consists of a number of pages that are used as a ringbuffer. Each data
41 * blob is stored in a simple record that consists of an integer, which
42 * contains the size of the following data, and the data bytes themselfes.
44 * To allow for multiple independent readers we create one internal buffer
45 * each time the device is opened and destroy the buffer when the file is
46 * closed again. The number of pages used for this buffer is determined by
47 * the module parmeter eer_pages.
49 * One record can be written to a buffer by using the functions
50 * - dasd_eer_start_record (one time per record to write the size to the
51 * buffer and reserve the space for the data)
52 * - dasd_eer_write_buffer (one or more times per record to write the data)
53 * The data can be written in several steps but you will have to compute
54 * the total size up front for the invocation of dasd_eer_start_record.
55 * If the ringbuffer is full, dasd_eer_start_record will remove the required
56 * number of old records.
58 * A record is typically read in two steps, first read the integer that
59 * specifies the size of the following data, then read the data.
61 * - dasd_eer_read_buffer
63 * For all mentioned functions you need to get the bufferlock first and keep
64 * it until a complete record is written or read.
66 * All information necessary to keep track of an internal buffer is kept in
67 * a struct eerbuffer. The buffer specific to a file pointer is strored in
68 * the private_data field of that file. To be able to write data to all
69 * existing buffers, each buffer is also added to the bufferlist.
70 * If the user does not want to read a complete record in one go, we have to
71 * keep track of the rest of the record. residual stores the number of bytes
72 * that are still to deliver. If the rest of the record is invalidated between
73 * two reads then residual will be set to -1 so that the next read will fail.
74 * All entries in the eerbuffer structure are protected with the bufferlock.
75 * To avoid races between writing to a buffer on the one side and creating
76 * and destroying buffers on the other side, the bufferlock must also be used
77 * to protect the bufferlist.
80 static int eer_pages = 5;
81 module_param(eer_pages, int, S_IRUGO|S_IWUSR);
84 struct list_head list;
87 int buffer_page_count;
93 static LIST_HEAD(bufferlist);
94 static DEFINE_SPINLOCK(bufferlock);
95 static DECLARE_WAIT_QUEUE_HEAD(dasd_eer_read_wait_queue);
98 * How many free bytes are available on the buffer.
99 * Needs to be called with bufferlock held.
101 static int dasd_eer_get_free_bytes(struct eerbuffer *eerb)
103 if (eerb->head < eerb->tail)
104 return eerb->tail - eerb->head - 1;
105 return eerb->buffersize - eerb->head + eerb->tail -1;
109 * How many bytes of buffer space are used.
110 * Needs to be called with bufferlock held.
112 static int dasd_eer_get_filled_bytes(struct eerbuffer *eerb)
115 if (eerb->head >= eerb->tail)
116 return eerb->head - eerb->tail;
117 return eerb->buffersize - eerb->tail + eerb->head;
121 * The dasd_eer_write_buffer function just copies count bytes of data
122 * to the buffer. Make sure to call dasd_eer_start_record first, to
123 * make sure that enough free space is available.
124 * Needs to be called with bufferlock held.
126 static void dasd_eer_write_buffer(struct eerbuffer *eerb,
127 char *data, int count)
130 unsigned long headindex,localhead;
131 unsigned long rest, len;
137 headindex = eerb->head / PAGE_SIZE;
138 localhead = eerb->head % PAGE_SIZE;
139 len = min(rest, PAGE_SIZE - localhead);
140 memcpy(eerb->buffer[headindex]+localhead, nextdata, len);
144 if (eerb->head == eerb->buffersize)
145 eerb->head = 0; /* wrap around */
146 BUG_ON(eerb->head > eerb->buffersize);
151 * Needs to be called with bufferlock held.
153 static int dasd_eer_read_buffer(struct eerbuffer *eerb, char *data, int count)
156 unsigned long tailindex,localtail;
157 unsigned long rest, len, finalcount;
160 finalcount = min(count, dasd_eer_get_filled_bytes(eerb));
164 tailindex = eerb->tail / PAGE_SIZE;
165 localtail = eerb->tail % PAGE_SIZE;
166 len = min(rest, PAGE_SIZE - localtail);
167 memcpy(nextdata, eerb->buffer[tailindex] + localtail, len);
171 if (eerb->tail == eerb->buffersize)
172 eerb->tail = 0; /* wrap around */
173 BUG_ON(eerb->tail > eerb->buffersize);
179 * Whenever you want to write a blob of data to the internal buffer you
180 * have to start by using this function first. It will write the number
181 * of bytes that will be written to the buffer. If necessary it will remove
182 * old records to make room for the new one.
183 * Needs to be called with bufferlock held.
185 static int dasd_eer_start_record(struct eerbuffer *eerb, int count)
189 if (count + sizeof(count) > eerb->buffersize)
191 while (dasd_eer_get_free_bytes(eerb) < count + sizeof(count)) {
192 if (eerb->residual > 0) {
193 eerb->tail += eerb->residual;
194 if (eerb->tail >= eerb->buffersize)
195 eerb->tail -= eerb->buffersize;
198 dasd_eer_read_buffer(eerb, (char *) &tailcount,
200 eerb->tail += tailcount;
201 if (eerb->tail >= eerb->buffersize)
202 eerb->tail -= eerb->buffersize;
204 dasd_eer_write_buffer(eerb, (char*) &count, sizeof(count));
210 * Release pages that are not used anymore.
212 static void dasd_eer_free_buffer_pages(char **buf, int no_pages)
216 for (i = 0; i < no_pages; i++)
217 free_page((unsigned long) buf[i]);
221 * Allocate a new set of memory pages.
223 static int dasd_eer_allocate_buffer_pages(char **buf, int no_pages)
227 for (i = 0; i < no_pages; i++) {
228 buf[i] = (char *) get_zeroed_page(GFP_KERNEL);
230 dasd_eer_free_buffer_pages(buf, i);
238 * SECTION: The extended error reporting functionality
242 * When a DASD device driver wants to report an error, it calls the
243 * function dasd_eer_write and gives the respective trigger ID as
244 * parameter. Currently there are four kinds of triggers:
246 * DASD_EER_FATALERROR: all kinds of unrecoverable I/O problems
247 * DASD_EER_PPRCSUSPEND: PPRC was suspended
248 * DASD_EER_NOPATH: There is no path to the device left.
249 * DASD_EER_STATECHANGE: The state of the device has changed.
251 * For the first three triggers all required information can be supplied by
252 * the caller. For these triggers a record is written by the function
253 * dasd_eer_write_standard_trigger.
255 * The DASD_EER_STATECHANGE trigger is special since a sense subsystem
256 * status ccw need to be executed to gather the necessary sense data first.
257 * The dasd_eer_snss function will queue the SNSS request and the request
258 * callback will then call dasd_eer_write with the DASD_EER_STATCHANGE
261 * To avoid memory allocations at runtime, the necessary memory is allocated
262 * when the extended error reporting is enabled for a device (by
263 * dasd_eer_probe). There is one sense subsystem status request for each
264 * eer enabled DASD device. The presence of the cqr in device->eer_cqr
265 * indicates that eer is enable for the device. The use of the snss request
266 * is protected by the DASD_FLAG_EER_IN_USE bit. When this flag indicates
267 * that the cqr is currently in use, dasd_eer_snss cannot start a second
268 * request but sets the DASD_FLAG_EER_SNSS flag instead. The callback of
269 * the SNSS request will check the bit and call dasd_eer_snss again.
272 #define SNSS_DATA_SIZE 44
274 #define DASD_EER_BUSID_SIZE 10
275 struct dasd_eer_header {
280 char busid[DASD_EER_BUSID_SIZE];
281 } __attribute__ ((packed));
284 * The following function can be used for those triggers that have
285 * all necessary data available when the function is called.
286 * If the parameter cqr is not NULL, the chain of requests will be searched
287 * for valid sense data, and all valid sense data sets will be added to
290 static void dasd_eer_write_standard_trigger(struct dasd_device *device,
291 struct dasd_ccw_req *cqr,
294 struct dasd_ccw_req *temp_cqr;
297 struct dasd_eer_header header;
299 struct eerbuffer *eerb;
301 /* go through cqr chain and count the valid sense data sets */
303 for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers)
304 if (temp_cqr->irb.esw.esw0.erw.cons)
307 header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
308 header.trigger = trigger;
309 do_gettimeofday(&tv);
310 header.tv_sec = tv.tv_sec;
311 header.tv_usec = tv.tv_usec;
312 strncpy(header.busid, device->cdev->dev.bus_id, DASD_EER_BUSID_SIZE);
314 spin_lock_irqsave(&bufferlock, flags);
315 list_for_each_entry(eerb, &bufferlist, list) {
316 dasd_eer_start_record(eerb, header.total_size);
317 dasd_eer_write_buffer(eerb, (char *) &header, sizeof(header));
318 for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers)
319 if (temp_cqr->irb.esw.esw0.erw.cons)
320 dasd_eer_write_buffer(eerb, cqr->irb.ecw, 32);
321 dasd_eer_write_buffer(eerb, "EOR", 4);
323 spin_unlock_irqrestore(&bufferlock, flags);
324 wake_up_interruptible(&dasd_eer_read_wait_queue);
328 * This function writes a DASD_EER_STATECHANGE trigger.
330 static void dasd_eer_write_snss_trigger(struct dasd_device *device,
331 struct dasd_ccw_req *cqr,
337 struct dasd_eer_header header;
339 struct eerbuffer *eerb;
341 snss_rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
345 data_size = SNSS_DATA_SIZE;
347 header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
348 header.trigger = DASD_EER_STATECHANGE;
349 do_gettimeofday(&tv);
350 header.tv_sec = tv.tv_sec;
351 header.tv_usec = tv.tv_usec;
352 strncpy(header.busid, device->cdev->dev.bus_id, DASD_EER_BUSID_SIZE);
354 spin_lock_irqsave(&bufferlock, flags);
355 list_for_each_entry(eerb, &bufferlist, list) {
356 dasd_eer_start_record(eerb, header.total_size);
357 dasd_eer_write_buffer(eerb, (char *) &header , sizeof(header));
359 dasd_eer_write_buffer(eerb, cqr->data, SNSS_DATA_SIZE);
360 dasd_eer_write_buffer(eerb, "EOR", 4);
362 spin_unlock_irqrestore(&bufferlock, flags);
363 wake_up_interruptible(&dasd_eer_read_wait_queue);
367 * This function is called for all triggers. It calls the appropriate
368 * function that writes the actual trigger records.
370 void dasd_eer_write(struct dasd_device *device, struct dasd_ccw_req *cqr,
373 if (!device->eer_cqr)
376 case DASD_EER_FATALERROR:
377 case DASD_EER_PPRCSUSPEND:
378 dasd_eer_write_standard_trigger(device, cqr, id);
380 case DASD_EER_NOPATH:
381 dasd_eer_write_standard_trigger(device, NULL, id);
383 case DASD_EER_STATECHANGE:
384 dasd_eer_write_snss_trigger(device, cqr, id);
386 default: /* unknown trigger, so we write it without any sense data */
387 dasd_eer_write_standard_trigger(device, NULL, id);
391 EXPORT_SYMBOL(dasd_eer_write);
394 * Start a sense subsystem status request.
395 * Needs to be called with the device held.
397 void dasd_eer_snss(struct dasd_device *device)
399 struct dasd_ccw_req *cqr;
401 cqr = device->eer_cqr;
402 if (!cqr) /* Device not eer enabled. */
404 if (test_and_set_bit(DASD_FLAG_EER_IN_USE, &device->flags)) {
405 /* Sense subsystem status request in use. */
406 set_bit(DASD_FLAG_EER_SNSS, &device->flags);
409 /* cdev is already locked, can't use dasd_add_request_head */
410 clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
411 cqr->status = DASD_CQR_QUEUED;
412 list_add(&cqr->devlist, &device->ccw_queue);
413 dasd_schedule_device_bh(device);
417 * Callback function for use with sense subsystem status request.
419 static void dasd_eer_snss_cb(struct dasd_ccw_req *cqr, void *data)
421 struct dasd_device *device = cqr->startdev;
424 dasd_eer_write(device, cqr, DASD_EER_STATECHANGE);
425 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
426 if (device->eer_cqr == cqr) {
427 clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
428 if (test_bit(DASD_FLAG_EER_SNSS, &device->flags))
429 /* Another SNSS has been requested in the meantime. */
430 dasd_eer_snss(device);
433 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
436 * Extended error recovery has been switched off while
437 * the SNSS request was running. It could even have
438 * been switched off and on again in which case there
439 * is a new ccw in device->eer_cqr. Free the "old"
442 dasd_kfree_request(cqr, device);
446 * Enable error reporting on a given device.
448 int dasd_eer_enable(struct dasd_device *device)
450 struct dasd_ccw_req *cqr;
456 if (!device->discipline || strcmp(device->discipline->name, "ECKD"))
457 return -EPERM; /* FIXME: -EMEDIUMTYPE ? */
459 cqr = dasd_kmalloc_request("ECKD", 1 /* SNSS */,
460 SNSS_DATA_SIZE, device);
464 cqr->startdev = device;
466 cqr->expires = 10 * HZ;
467 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
469 cqr->cpaddr->cmd_code = DASD_ECKD_CCW_SNSS;
470 cqr->cpaddr->count = SNSS_DATA_SIZE;
471 cqr->cpaddr->flags = 0;
472 cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
474 cqr->buildclk = get_clock();
475 cqr->status = DASD_CQR_FILLED;
476 cqr->callback = dasd_eer_snss_cb;
478 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
479 if (!device->eer_cqr) {
480 device->eer_cqr = cqr;
483 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
485 dasd_kfree_request(cqr, device);
490 * Disable error reporting on a given device.
492 void dasd_eer_disable(struct dasd_device *device)
494 struct dasd_ccw_req *cqr;
498 if (!device->eer_cqr)
500 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
501 cqr = device->eer_cqr;
502 device->eer_cqr = NULL;
503 clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
504 in_use = test_and_clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
505 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
507 dasd_kfree_request(cqr, device);
511 * SECTION: the device operations
515 * On the one side we need a lock to access our internal buffer, on the
516 * other side a copy_to_user can sleep. So we need to copy the data we have
517 * to transfer in a readbuffer, which is protected by the readbuffer_mutex.
519 static char readbuffer[PAGE_SIZE];
520 static DEFINE_MUTEX(readbuffer_mutex);
522 static int dasd_eer_open(struct inode *inp, struct file *filp)
524 struct eerbuffer *eerb;
527 eerb = kzalloc(sizeof(struct eerbuffer), GFP_KERNEL);
531 eerb->buffer_page_count = eer_pages;
532 if (eerb->buffer_page_count < 1 ||
533 eerb->buffer_page_count > INT_MAX / PAGE_SIZE) {
535 MESSAGE(KERN_WARNING, "can't open device since module "
536 "parameter eer_pages is smaller then 1 or"
537 " bigger then %d", (int)(INT_MAX / PAGE_SIZE));
541 eerb->buffersize = eerb->buffer_page_count * PAGE_SIZE;
542 eerb->buffer = kmalloc(eerb->buffer_page_count * sizeof(char *),
549 if (dasd_eer_allocate_buffer_pages(eerb->buffer,
550 eerb->buffer_page_count)) {
556 filp->private_data = eerb;
557 spin_lock_irqsave(&bufferlock, flags);
558 list_add(&eerb->list, &bufferlist);
559 spin_unlock_irqrestore(&bufferlock, flags);
562 return nonseekable_open(inp,filp);
565 static int dasd_eer_close(struct inode *inp, struct file *filp)
567 struct eerbuffer *eerb;
570 eerb = (struct eerbuffer *) filp->private_data;
571 spin_lock_irqsave(&bufferlock, flags);
572 list_del(&eerb->list);
573 spin_unlock_irqrestore(&bufferlock, flags);
574 dasd_eer_free_buffer_pages(eerb->buffer, eerb->buffer_page_count);
581 static ssize_t dasd_eer_read(struct file *filp, char __user *buf,
582 size_t count, loff_t *ppos)
585 int tailcount,effective_count;
587 struct eerbuffer *eerb;
589 eerb = (struct eerbuffer *) filp->private_data;
590 if (mutex_lock_interruptible(&readbuffer_mutex))
593 spin_lock_irqsave(&bufferlock, flags);
595 if (eerb->residual < 0) { /* the remainder of this record */
596 /* has been deleted */
598 spin_unlock_irqrestore(&bufferlock, flags);
599 mutex_unlock(&readbuffer_mutex);
601 } else if (eerb->residual > 0) {
602 /* OK we still have a second half of a record to deliver */
603 effective_count = min(eerb->residual, (int) count);
604 eerb->residual -= effective_count;
608 tc = dasd_eer_read_buffer(eerb, (char *) &tailcount,
611 /* no data available */
612 spin_unlock_irqrestore(&bufferlock, flags);
613 mutex_unlock(&readbuffer_mutex);
614 if (filp->f_flags & O_NONBLOCK)
616 rc = wait_event_interruptible(
617 dasd_eer_read_wait_queue,
618 eerb->head != eerb->tail);
621 if (mutex_lock_interruptible(&readbuffer_mutex))
623 spin_lock_irqsave(&bufferlock, flags);
626 WARN_ON(tc != sizeof(tailcount));
627 effective_count = min(tailcount,(int)count);
628 eerb->residual = tailcount - effective_count;
631 tc = dasd_eer_read_buffer(eerb, readbuffer, effective_count);
632 WARN_ON(tc != effective_count);
634 spin_unlock_irqrestore(&bufferlock, flags);
636 if (copy_to_user(buf, readbuffer, effective_count)) {
637 mutex_unlock(&readbuffer_mutex);
641 mutex_unlock(&readbuffer_mutex);
642 return effective_count;
645 static unsigned int dasd_eer_poll(struct file *filp, poll_table *ptable)
649 struct eerbuffer *eerb;
651 eerb = (struct eerbuffer *) filp->private_data;
652 poll_wait(filp, &dasd_eer_read_wait_queue, ptable);
653 spin_lock_irqsave(&bufferlock, flags);
654 if (eerb->head != eerb->tail)
655 mask = POLLIN | POLLRDNORM ;
658 spin_unlock_irqrestore(&bufferlock, flags);
662 static const struct file_operations dasd_eer_fops = {
663 .open = &dasd_eer_open,
664 .release = &dasd_eer_close,
665 .read = &dasd_eer_read,
666 .poll = &dasd_eer_poll,
667 .owner = THIS_MODULE,
670 static struct miscdevice *dasd_eer_dev = NULL;
672 int __init dasd_eer_init(void)
676 dasd_eer_dev = kzalloc(sizeof(*dasd_eer_dev), GFP_KERNEL);
680 dasd_eer_dev->minor = MISC_DYNAMIC_MINOR;
681 dasd_eer_dev->name = "dasd_eer";
682 dasd_eer_dev->fops = &dasd_eer_fops;
684 rc = misc_register(dasd_eer_dev);
688 MESSAGE(KERN_ERR, "%s", "dasd_eer_init could not "
689 "register misc device");
696 void dasd_eer_exit(void)
699 WARN_ON(misc_deregister(dasd_eer_dev) != 0);