dasd_eer.c 20 KB

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