dasd_eer.c 19 KB

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