dasd_eer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Character device driver for extended error reporting.
  3. *
  4. * Copyright (C) 2005 IBM Corporation
  5. * extended error reporting for DASD ECKD devices
  6. * Author(s): Stefan Weinhuber <wein@de.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "dasd-eckd"
  9. #include <linux/init.h>
  10. #include <linux/fs.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>
  20. #include <linux/slab.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/atomic.h>
  23. #include <asm/ebcdic.h>
  24. #include "dasd_int.h"
  25. #include "dasd_eckd.h"
  26. #ifdef PRINTK_HEADER
  27. #undef PRINTK_HEADER
  28. #endif /* PRINTK_HEADER */
  29. #define PRINTK_HEADER "dasd(eer):"
  30. /*
  31. * SECTION: the internal buffer
  32. */
  33. /*
  34. * The internal buffer is meant to store obaque blobs of data, so it does
  35. * not know of higher level concepts like triggers.
  36. * It consists of a number of pages that are used as a ringbuffer. Each data
  37. * blob is stored in a simple record that consists of an integer, which
  38. * contains the size of the following data, and the data bytes themselfes.
  39. *
  40. * To allow for multiple independent readers we create one internal buffer
  41. * each time the device is opened and destroy the buffer when the file is
  42. * closed again. The number of pages used for this buffer is determined by
  43. * the module parmeter eer_pages.
  44. *
  45. * One record can be written to a buffer by using the functions
  46. * - dasd_eer_start_record (one time per record to write the size to the
  47. * buffer and reserve the space for the data)
  48. * - dasd_eer_write_buffer (one or more times per record to write the data)
  49. * The data can be written in several steps but you will have to compute
  50. * the total size up front for the invocation of dasd_eer_start_record.
  51. * If the ringbuffer is full, dasd_eer_start_record will remove the required
  52. * number of old records.
  53. *
  54. * A record is typically read in two steps, first read the integer that
  55. * specifies the size of the following data, then read the data.
  56. * Both can be done by
  57. * - dasd_eer_read_buffer
  58. *
  59. * For all mentioned functions you need to get the bufferlock first and keep
  60. * it until a complete record is written or read.
  61. *
  62. * All information necessary to keep track of an internal buffer is kept in
  63. * a struct eerbuffer. The buffer specific to a file pointer is strored in
  64. * the private_data field of that file. To be able to write data to all
  65. * existing buffers, each buffer is also added to the bufferlist.
  66. * If the user does not want to read a complete record in one go, we have to
  67. * keep track of the rest of the record. residual stores the number of bytes
  68. * that are still to deliver. If the rest of the record is invalidated between
  69. * two reads then residual will be set to -1 so that the next read will fail.
  70. * All entries in the eerbuffer structure are protected with the bufferlock.
  71. * To avoid races between writing to a buffer on the one side and creating
  72. * and destroying buffers on the other side, the bufferlock must also be used
  73. * to protect the bufferlist.
  74. */
  75. static int eer_pages = 5;
  76. module_param(eer_pages, int, S_IRUGO|S_IWUSR);
  77. struct eerbuffer {
  78. struct list_head list;
  79. char **buffer;
  80. int buffersize;
  81. int buffer_page_count;
  82. int head;
  83. int tail;
  84. int residual;
  85. };
  86. static LIST_HEAD(bufferlist);
  87. static DEFINE_SPINLOCK(bufferlock);
  88. static DECLARE_WAIT_QUEUE_HEAD(dasd_eer_read_wait_queue);
  89. /*
  90. * How many free bytes are available on the buffer.
  91. * Needs to be called with bufferlock held.
  92. */
  93. static int dasd_eer_get_free_bytes(struct eerbuffer *eerb)
  94. {
  95. if (eerb->head < eerb->tail)
  96. return eerb->tail - eerb->head - 1;
  97. return eerb->buffersize - eerb->head + eerb->tail -1;
  98. }
  99. /*
  100. * How many bytes of buffer space are used.
  101. * Needs to be called with bufferlock held.
  102. */
  103. static int dasd_eer_get_filled_bytes(struct eerbuffer *eerb)
  104. {
  105. if (eerb->head >= eerb->tail)
  106. return eerb->head - eerb->tail;
  107. return eerb->buffersize - eerb->tail + eerb->head;
  108. }
  109. /*
  110. * The dasd_eer_write_buffer function just copies count bytes of data
  111. * to the buffer. Make sure to call dasd_eer_start_record first, to
  112. * make sure that enough free space is available.
  113. * Needs to be called with bufferlock held.
  114. */
  115. static void dasd_eer_write_buffer(struct eerbuffer *eerb,
  116. char *data, int count)
  117. {
  118. unsigned long headindex,localhead;
  119. unsigned long rest, len;
  120. char *nextdata;
  121. nextdata = data;
  122. rest = count;
  123. while (rest > 0) {
  124. headindex = eerb->head / PAGE_SIZE;
  125. localhead = eerb->head % PAGE_SIZE;
  126. len = min(rest, PAGE_SIZE - localhead);
  127. memcpy(eerb->buffer[headindex]+localhead, nextdata, len);
  128. nextdata += len;
  129. rest -= len;
  130. eerb->head += len;
  131. if (eerb->head == eerb->buffersize)
  132. eerb->head = 0; /* wrap around */
  133. BUG_ON(eerb->head > eerb->buffersize);
  134. }
  135. }
  136. /*
  137. * Needs to be called with bufferlock held.
  138. */
  139. static int dasd_eer_read_buffer(struct eerbuffer *eerb, char *data, int count)
  140. {
  141. unsigned long tailindex,localtail;
  142. unsigned long rest, len, finalcount;
  143. char *nextdata;
  144. finalcount = min(count, dasd_eer_get_filled_bytes(eerb));
  145. nextdata = data;
  146. rest = finalcount;
  147. while (rest > 0) {
  148. tailindex = eerb->tail / PAGE_SIZE;
  149. localtail = eerb->tail % PAGE_SIZE;
  150. len = min(rest, PAGE_SIZE - localtail);
  151. memcpy(nextdata, eerb->buffer[tailindex] + localtail, len);
  152. nextdata += len;
  153. rest -= len;
  154. eerb->tail += len;
  155. if (eerb->tail == eerb->buffersize)
  156. eerb->tail = 0; /* wrap around */
  157. BUG_ON(eerb->tail > eerb->buffersize);
  158. }
  159. return finalcount;
  160. }
  161. /*
  162. * Whenever you want to write a blob of data to the internal buffer you
  163. * have to start by using this function first. It will write the number
  164. * of bytes that will be written to the buffer. If necessary it will remove
  165. * old records to make room for the new one.
  166. * Needs to be called with bufferlock held.
  167. */
  168. static int dasd_eer_start_record(struct eerbuffer *eerb, int count)
  169. {
  170. int tailcount;
  171. if (count + sizeof(count) > eerb->buffersize)
  172. return -ENOMEM;
  173. while (dasd_eer_get_free_bytes(eerb) < count + sizeof(count)) {
  174. if (eerb->residual > 0) {
  175. eerb->tail += eerb->residual;
  176. if (eerb->tail >= eerb->buffersize)
  177. eerb->tail -= eerb->buffersize;
  178. eerb->residual = -1;
  179. }
  180. dasd_eer_read_buffer(eerb, (char *) &tailcount,
  181. sizeof(tailcount));
  182. eerb->tail += tailcount;
  183. if (eerb->tail >= eerb->buffersize)
  184. eerb->tail -= eerb->buffersize;
  185. }
  186. dasd_eer_write_buffer(eerb, (char*) &count, sizeof(count));
  187. return 0;
  188. };
  189. /*
  190. * Release pages that are not used anymore.
  191. */
  192. static void dasd_eer_free_buffer_pages(char **buf, int no_pages)
  193. {
  194. int i;
  195. for (i = 0; i < no_pages; i++)
  196. free_page((unsigned long) buf[i]);
  197. }
  198. /*
  199. * Allocate a new set of memory pages.
  200. */
  201. static int dasd_eer_allocate_buffer_pages(char **buf, int no_pages)
  202. {
  203. int i;
  204. for (i = 0; i < no_pages; i++) {
  205. buf[i] = (char *) get_zeroed_page(GFP_KERNEL);
  206. if (!buf[i]) {
  207. dasd_eer_free_buffer_pages(buf, i);
  208. return -ENOMEM;
  209. }
  210. }
  211. return 0;
  212. }
  213. /*
  214. * SECTION: The extended error reporting functionality
  215. */
  216. /*
  217. * When a DASD device driver wants to report an error, it calls the
  218. * function dasd_eer_write and gives the respective trigger ID as
  219. * parameter. Currently there are four kinds of triggers:
  220. *
  221. * DASD_EER_FATALERROR: all kinds of unrecoverable I/O problems
  222. * DASD_EER_PPRCSUSPEND: PPRC was suspended
  223. * DASD_EER_NOPATH: There is no path to the device left.
  224. * DASD_EER_STATECHANGE: The state of the device has changed.
  225. *
  226. * For the first three triggers all required information can be supplied by
  227. * the caller. For these triggers a record is written by the function
  228. * dasd_eer_write_standard_trigger.
  229. *
  230. * The DASD_EER_STATECHANGE trigger is special since a sense subsystem
  231. * status ccw need to be executed to gather the necessary sense data first.
  232. * The dasd_eer_snss function will queue the SNSS request and the request
  233. * callback will then call dasd_eer_write with the DASD_EER_STATCHANGE
  234. * trigger.
  235. *
  236. * To avoid memory allocations at runtime, the necessary memory is allocated
  237. * when the extended error reporting is enabled for a device (by
  238. * dasd_eer_probe). There is one sense subsystem status request for each
  239. * eer enabled DASD device. The presence of the cqr in device->eer_cqr
  240. * indicates that eer is enable for the device. The use of the snss request
  241. * is protected by the DASD_FLAG_EER_IN_USE bit. When this flag indicates
  242. * that the cqr is currently in use, dasd_eer_snss cannot start a second
  243. * request but sets the DASD_FLAG_EER_SNSS flag instead. The callback of
  244. * the SNSS request will check the bit and call dasd_eer_snss again.
  245. */
  246. #define SNSS_DATA_SIZE 44
  247. #define DASD_EER_BUSID_SIZE 10
  248. struct dasd_eer_header {
  249. __u32 total_size;
  250. __u32 trigger;
  251. __u64 tv_sec;
  252. __u64 tv_usec;
  253. char busid[DASD_EER_BUSID_SIZE];
  254. } __attribute__ ((packed));
  255. /*
  256. * The following function can be used for those triggers that have
  257. * all necessary data available when the function is called.
  258. * If the parameter cqr is not NULL, the chain of requests will be searched
  259. * for valid sense data, and all valid sense data sets will be added to
  260. * the triggers data.
  261. */
  262. static void dasd_eer_write_standard_trigger(struct dasd_device *device,
  263. struct dasd_ccw_req *cqr,
  264. int trigger)
  265. {
  266. struct dasd_ccw_req *temp_cqr;
  267. int data_size;
  268. struct timeval tv;
  269. struct dasd_eer_header header;
  270. unsigned long flags;
  271. struct eerbuffer *eerb;
  272. char *sense;
  273. /* go through cqr chain and count the valid sense data sets */
  274. data_size = 0;
  275. for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers)
  276. if (dasd_get_sense(&temp_cqr->irb))
  277. data_size += 32;
  278. header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
  279. header.trigger = trigger;
  280. do_gettimeofday(&tv);
  281. header.tv_sec = tv.tv_sec;
  282. header.tv_usec = tv.tv_usec;
  283. strncpy(header.busid, dev_name(&device->cdev->dev),
  284. DASD_EER_BUSID_SIZE);
  285. spin_lock_irqsave(&bufferlock, flags);
  286. list_for_each_entry(eerb, &bufferlist, list) {
  287. dasd_eer_start_record(eerb, header.total_size);
  288. dasd_eer_write_buffer(eerb, (char *) &header, sizeof(header));
  289. for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers) {
  290. sense = dasd_get_sense(&temp_cqr->irb);
  291. if (sense)
  292. dasd_eer_write_buffer(eerb, sense, 32);
  293. }
  294. dasd_eer_write_buffer(eerb, "EOR", 4);
  295. }
  296. spin_unlock_irqrestore(&bufferlock, flags);
  297. wake_up_interruptible(&dasd_eer_read_wait_queue);
  298. }
  299. /*
  300. * This function writes a DASD_EER_STATECHANGE trigger.
  301. */
  302. static void dasd_eer_write_snss_trigger(struct dasd_device *device,
  303. struct dasd_ccw_req *cqr,
  304. int trigger)
  305. {
  306. int data_size;
  307. int snss_rc;
  308. struct timeval tv;
  309. struct dasd_eer_header header;
  310. unsigned long flags;
  311. struct eerbuffer *eerb;
  312. snss_rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  313. if (snss_rc)
  314. data_size = 0;
  315. else
  316. data_size = SNSS_DATA_SIZE;
  317. header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
  318. header.trigger = DASD_EER_STATECHANGE;
  319. do_gettimeofday(&tv);
  320. header.tv_sec = tv.tv_sec;
  321. header.tv_usec = tv.tv_usec;
  322. strncpy(header.busid, dev_name(&device->cdev->dev),
  323. DASD_EER_BUSID_SIZE);
  324. spin_lock_irqsave(&bufferlock, flags);
  325. list_for_each_entry(eerb, &bufferlist, list) {
  326. dasd_eer_start_record(eerb, header.total_size);
  327. dasd_eer_write_buffer(eerb, (char *) &header , sizeof(header));
  328. if (!snss_rc)
  329. dasd_eer_write_buffer(eerb, cqr->data, SNSS_DATA_SIZE);
  330. dasd_eer_write_buffer(eerb, "EOR", 4);
  331. }
  332. spin_unlock_irqrestore(&bufferlock, flags);
  333. wake_up_interruptible(&dasd_eer_read_wait_queue);
  334. }
  335. /*
  336. * This function is called for all triggers. It calls the appropriate
  337. * function that writes the actual trigger records.
  338. */
  339. void dasd_eer_write(struct dasd_device *device, struct dasd_ccw_req *cqr,
  340. unsigned int id)
  341. {
  342. if (!device->eer_cqr)
  343. return;
  344. switch (id) {
  345. case DASD_EER_FATALERROR:
  346. case DASD_EER_PPRCSUSPEND:
  347. dasd_eer_write_standard_trigger(device, cqr, id);
  348. break;
  349. case DASD_EER_NOPATH:
  350. dasd_eer_write_standard_trigger(device, NULL, id);
  351. break;
  352. case DASD_EER_STATECHANGE:
  353. dasd_eer_write_snss_trigger(device, cqr, id);
  354. break;
  355. default: /* unknown trigger, so we write it without any sense data */
  356. dasd_eer_write_standard_trigger(device, NULL, id);
  357. break;
  358. }
  359. }
  360. EXPORT_SYMBOL(dasd_eer_write);
  361. /*
  362. * Start a sense subsystem status request.
  363. * Needs to be called with the device held.
  364. */
  365. void dasd_eer_snss(struct dasd_device *device)
  366. {
  367. struct dasd_ccw_req *cqr;
  368. cqr = device->eer_cqr;
  369. if (!cqr) /* Device not eer enabled. */
  370. return;
  371. if (test_and_set_bit(DASD_FLAG_EER_IN_USE, &device->flags)) {
  372. /* Sense subsystem status request in use. */
  373. set_bit(DASD_FLAG_EER_SNSS, &device->flags);
  374. return;
  375. }
  376. /* cdev is already locked, can't use dasd_add_request_head */
  377. clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
  378. cqr->status = DASD_CQR_QUEUED;
  379. list_add(&cqr->devlist, &device->ccw_queue);
  380. dasd_schedule_device_bh(device);
  381. }
  382. /*
  383. * Callback function for use with sense subsystem status request.
  384. */
  385. static void dasd_eer_snss_cb(struct dasd_ccw_req *cqr, void *data)
  386. {
  387. struct dasd_device *device = cqr->startdev;
  388. unsigned long flags;
  389. dasd_eer_write(device, cqr, DASD_EER_STATECHANGE);
  390. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  391. if (device->eer_cqr == cqr) {
  392. clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
  393. if (test_bit(DASD_FLAG_EER_SNSS, &device->flags))
  394. /* Another SNSS has been requested in the meantime. */
  395. dasd_eer_snss(device);
  396. cqr = NULL;
  397. }
  398. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  399. if (cqr)
  400. /*
  401. * Extended error recovery has been switched off while
  402. * the SNSS request was running. It could even have
  403. * been switched off and on again in which case there
  404. * is a new ccw in device->eer_cqr. Free the "old"
  405. * snss request now.
  406. */
  407. dasd_kfree_request(cqr, device);
  408. }
  409. /*
  410. * Enable error reporting on a given device.
  411. */
  412. int dasd_eer_enable(struct dasd_device *device)
  413. {
  414. struct dasd_ccw_req *cqr;
  415. unsigned long flags;
  416. struct ccw1 *ccw;
  417. if (device->eer_cqr)
  418. return 0;
  419. if (!device->discipline || strcmp(device->discipline->name, "ECKD"))
  420. return -EPERM; /* FIXME: -EMEDIUMTYPE ? */
  421. cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* SNSS */,
  422. SNSS_DATA_SIZE, device);
  423. if (IS_ERR(cqr))
  424. return -ENOMEM;
  425. cqr->startdev = device;
  426. cqr->retries = 255;
  427. cqr->expires = 10 * HZ;
  428. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  429. ccw = cqr->cpaddr;
  430. ccw->cmd_code = DASD_ECKD_CCW_SNSS;
  431. ccw->count = SNSS_DATA_SIZE;
  432. ccw->flags = 0;
  433. ccw->cda = (__u32)(addr_t) cqr->data;
  434. cqr->buildclk = get_clock();
  435. cqr->status = DASD_CQR_FILLED;
  436. cqr->callback = dasd_eer_snss_cb;
  437. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  438. if (!device->eer_cqr) {
  439. device->eer_cqr = cqr;
  440. cqr = NULL;
  441. }
  442. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  443. if (cqr)
  444. dasd_kfree_request(cqr, device);
  445. return 0;
  446. }
  447. /*
  448. * Disable error reporting on a given device.
  449. */
  450. void dasd_eer_disable(struct dasd_device *device)
  451. {
  452. struct dasd_ccw_req *cqr;
  453. unsigned long flags;
  454. int in_use;
  455. if (!device->eer_cqr)
  456. return;
  457. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  458. cqr = device->eer_cqr;
  459. device->eer_cqr = NULL;
  460. clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
  461. in_use = test_and_clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
  462. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  463. if (cqr && !in_use)
  464. dasd_kfree_request(cqr, device);
  465. }
  466. /*
  467. * SECTION: the device operations
  468. */
  469. /*
  470. * On the one side we need a lock to access our internal buffer, on the
  471. * other side a copy_to_user can sleep. So we need to copy the data we have
  472. * to transfer in a readbuffer, which is protected by the readbuffer_mutex.
  473. */
  474. static char readbuffer[PAGE_SIZE];
  475. static DEFINE_MUTEX(readbuffer_mutex);
  476. static int dasd_eer_open(struct inode *inp, struct file *filp)
  477. {
  478. struct eerbuffer *eerb;
  479. unsigned long flags;
  480. eerb = kzalloc(sizeof(struct eerbuffer), GFP_KERNEL);
  481. if (!eerb)
  482. return -ENOMEM;
  483. eerb->buffer_page_count = eer_pages;
  484. if (eerb->buffer_page_count < 1 ||
  485. eerb->buffer_page_count > INT_MAX / PAGE_SIZE) {
  486. kfree(eerb);
  487. DBF_EVENT(DBF_WARNING, "can't open device since module "
  488. "parameter eer_pages is smaller than 1 or"
  489. " bigger than %d", (int)(INT_MAX / PAGE_SIZE));
  490. return -EINVAL;
  491. }
  492. eerb->buffersize = eerb->buffer_page_count * PAGE_SIZE;
  493. eerb->buffer = kmalloc(eerb->buffer_page_count * sizeof(char *),
  494. GFP_KERNEL);
  495. if (!eerb->buffer) {
  496. kfree(eerb);
  497. return -ENOMEM;
  498. }
  499. if (dasd_eer_allocate_buffer_pages(eerb->buffer,
  500. eerb->buffer_page_count)) {
  501. kfree(eerb->buffer);
  502. kfree(eerb);
  503. return -ENOMEM;
  504. }
  505. filp->private_data = eerb;
  506. spin_lock_irqsave(&bufferlock, flags);
  507. list_add(&eerb->list, &bufferlist);
  508. spin_unlock_irqrestore(&bufferlock, flags);
  509. return nonseekable_open(inp,filp);
  510. }
  511. static int dasd_eer_close(struct inode *inp, struct file *filp)
  512. {
  513. struct eerbuffer *eerb;
  514. unsigned long flags;
  515. eerb = (struct eerbuffer *) filp->private_data;
  516. spin_lock_irqsave(&bufferlock, flags);
  517. list_del(&eerb->list);
  518. spin_unlock_irqrestore(&bufferlock, flags);
  519. dasd_eer_free_buffer_pages(eerb->buffer, eerb->buffer_page_count);
  520. kfree(eerb->buffer);
  521. kfree(eerb);
  522. return 0;
  523. }
  524. static ssize_t dasd_eer_read(struct file *filp, char __user *buf,
  525. size_t count, loff_t *ppos)
  526. {
  527. int tc,rc;
  528. int tailcount,effective_count;
  529. unsigned long flags;
  530. struct eerbuffer *eerb;
  531. eerb = (struct eerbuffer *) filp->private_data;
  532. if (mutex_lock_interruptible(&readbuffer_mutex))
  533. return -ERESTARTSYS;
  534. spin_lock_irqsave(&bufferlock, flags);
  535. if (eerb->residual < 0) { /* the remainder of this record */
  536. /* has been deleted */
  537. eerb->residual = 0;
  538. spin_unlock_irqrestore(&bufferlock, flags);
  539. mutex_unlock(&readbuffer_mutex);
  540. return -EIO;
  541. } else if (eerb->residual > 0) {
  542. /* OK we still have a second half of a record to deliver */
  543. effective_count = min(eerb->residual, (int) count);
  544. eerb->residual -= effective_count;
  545. } else {
  546. tc = 0;
  547. while (!tc) {
  548. tc = dasd_eer_read_buffer(eerb, (char *) &tailcount,
  549. sizeof(tailcount));
  550. if (!tc) {
  551. /* no data available */
  552. spin_unlock_irqrestore(&bufferlock, flags);
  553. mutex_unlock(&readbuffer_mutex);
  554. if (filp->f_flags & O_NONBLOCK)
  555. return -EAGAIN;
  556. rc = wait_event_interruptible(
  557. dasd_eer_read_wait_queue,
  558. eerb->head != eerb->tail);
  559. if (rc)
  560. return rc;
  561. if (mutex_lock_interruptible(&readbuffer_mutex))
  562. return -ERESTARTSYS;
  563. spin_lock_irqsave(&bufferlock, flags);
  564. }
  565. }
  566. WARN_ON(tc != sizeof(tailcount));
  567. effective_count = min(tailcount,(int)count);
  568. eerb->residual = tailcount - effective_count;
  569. }
  570. tc = dasd_eer_read_buffer(eerb, readbuffer, effective_count);
  571. WARN_ON(tc != effective_count);
  572. spin_unlock_irqrestore(&bufferlock, flags);
  573. if (copy_to_user(buf, readbuffer, effective_count)) {
  574. mutex_unlock(&readbuffer_mutex);
  575. return -EFAULT;
  576. }
  577. mutex_unlock(&readbuffer_mutex);
  578. return effective_count;
  579. }
  580. static unsigned int dasd_eer_poll(struct file *filp, poll_table *ptable)
  581. {
  582. unsigned int mask;
  583. unsigned long flags;
  584. struct eerbuffer *eerb;
  585. eerb = (struct eerbuffer *) filp->private_data;
  586. poll_wait(filp, &dasd_eer_read_wait_queue, ptable);
  587. spin_lock_irqsave(&bufferlock, flags);
  588. if (eerb->head != eerb->tail)
  589. mask = POLLIN | POLLRDNORM ;
  590. else
  591. mask = 0;
  592. spin_unlock_irqrestore(&bufferlock, flags);
  593. return mask;
  594. }
  595. static const struct file_operations dasd_eer_fops = {
  596. .open = &dasd_eer_open,
  597. .release = &dasd_eer_close,
  598. .read = &dasd_eer_read,
  599. .poll = &dasd_eer_poll,
  600. .owner = THIS_MODULE,
  601. };
  602. static struct miscdevice *dasd_eer_dev = NULL;
  603. int __init dasd_eer_init(void)
  604. {
  605. int rc;
  606. dasd_eer_dev = kzalloc(sizeof(*dasd_eer_dev), GFP_KERNEL);
  607. if (!dasd_eer_dev)
  608. return -ENOMEM;
  609. dasd_eer_dev->minor = MISC_DYNAMIC_MINOR;
  610. dasd_eer_dev->name = "dasd_eer";
  611. dasd_eer_dev->fops = &dasd_eer_fops;
  612. rc = misc_register(dasd_eer_dev);
  613. if (rc) {
  614. kfree(dasd_eer_dev);
  615. dasd_eer_dev = NULL;
  616. DBF_EVENT(DBF_ERR, "%s", "dasd_eer_init could not "
  617. "register misc device");
  618. return rc;
  619. }
  620. return 0;
  621. }
  622. void dasd_eer_exit(void)
  623. {
  624. if (dasd_eer_dev) {
  625. WARN_ON(misc_deregister(dasd_eer_dev) != 0);
  626. kfree(dasd_eer_dev);
  627. dasd_eer_dev = NULL;
  628. }
  629. }