dasd_eer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Character device driver for extended error reporting.
  3. *
  4. * Copyright IBM Corp. 2005
  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/err.h>
  19. #include <linux/slab.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/atomic.h>
  22. #include <asm/ebcdic.h>
  23. #include "dasd_int.h"
  24. #include "dasd_eckd.h"
  25. #ifdef PRINTK_HEADER
  26. #undef PRINTK_HEADER
  27. #endif /* PRINTK_HEADER */
  28. #define PRINTK_HEADER "dasd(eer):"
  29. /*
  30. * SECTION: the internal buffer
  31. */
  32. /*
  33. * The internal buffer is meant to store obaque blobs of data, so it does
  34. * not know of higher level concepts like triggers.
  35. * It consists of a number of pages that are used as a ringbuffer. Each data
  36. * blob is stored in a simple record that consists of an integer, which
  37. * contains the size of the following data, and the data bytes themselfes.
  38. *
  39. * To allow for multiple independent readers we create one internal buffer
  40. * each time the device is opened and destroy the buffer when the file is
  41. * closed again. The number of pages used for this buffer is determined by
  42. * the module parmeter eer_pages.
  43. *
  44. * One record can be written to a buffer by using the functions
  45. * - dasd_eer_start_record (one time per record to write the size to the
  46. * buffer and reserve the space for the data)
  47. * - dasd_eer_write_buffer (one or more times per record to write the data)
  48. * The data can be written in several steps but you will have to compute
  49. * the total size up front for the invocation of dasd_eer_start_record.
  50. * If the ringbuffer is full, dasd_eer_start_record will remove the required
  51. * number of old records.
  52. *
  53. * A record is typically read in two steps, first read the integer that
  54. * specifies the size of the following data, then read the data.
  55. * Both can be done by
  56. * - dasd_eer_read_buffer
  57. *
  58. * For all mentioned functions you need to get the bufferlock first and keep
  59. * it until a complete record is written or read.
  60. *
  61. * All information necessary to keep track of an internal buffer is kept in
  62. * a struct eerbuffer. The buffer specific to a file pointer is strored in
  63. * the private_data field of that file. To be able to write data to all
  64. * existing buffers, each buffer is also added to the bufferlist.
  65. * If the user does not want to read a complete record in one go, we have to
  66. * keep track of the rest of the record. residual stores the number of bytes
  67. * that are still to deliver. If the rest of the record is invalidated between
  68. * two reads then residual will be set to -1 so that the next read will fail.
  69. * All entries in the eerbuffer structure are protected with the bufferlock.
  70. * To avoid races between writing to a buffer on the one side and creating
  71. * and destroying buffers on the other side, the bufferlock must also be used
  72. * to protect the bufferlist.
  73. */
  74. static int eer_pages = 5;
  75. module_param(eer_pages, int, S_IRUGO|S_IWUSR);
  76. struct eerbuffer {
  77. struct list_head list;
  78. char **buffer;
  79. int buffersize;
  80. int buffer_page_count;
  81. int head;
  82. int tail;
  83. int residual;
  84. };
  85. static LIST_HEAD(bufferlist);
  86. static DEFINE_SPINLOCK(bufferlock);
  87. static DECLARE_WAIT_QUEUE_HEAD(dasd_eer_read_wait_queue);
  88. /*
  89. * How many free bytes are available on the buffer.
  90. * Needs to be called with bufferlock held.
  91. */
  92. static int dasd_eer_get_free_bytes(struct eerbuffer *eerb)
  93. {
  94. if (eerb->head < eerb->tail)
  95. return eerb->tail - eerb->head - 1;
  96. return eerb->buffersize - eerb->head + eerb->tail -1;
  97. }
  98. /*
  99. * How many bytes of buffer space are used.
  100. * Needs to be called with bufferlock held.
  101. */
  102. static int dasd_eer_get_filled_bytes(struct eerbuffer *eerb)
  103. {
  104. if (eerb->head >= eerb->tail)
  105. return eerb->head - eerb->tail;
  106. return eerb->buffersize - eerb->tail + eerb->head;
  107. }
  108. /*
  109. * The dasd_eer_write_buffer function just copies count bytes of data
  110. * to the buffer. Make sure to call dasd_eer_start_record first, to
  111. * make sure that enough free space is available.
  112. * Needs to be called with bufferlock held.
  113. */
  114. static void dasd_eer_write_buffer(struct eerbuffer *eerb,
  115. char *data, int count)
  116. {
  117. unsigned long headindex,localhead;
  118. unsigned long rest, len;
  119. char *nextdata;
  120. nextdata = data;
  121. rest = count;
  122. while (rest > 0) {
  123. headindex = eerb->head / PAGE_SIZE;
  124. localhead = eerb->head % PAGE_SIZE;
  125. len = min(rest, PAGE_SIZE - localhead);
  126. memcpy(eerb->buffer[headindex]+localhead, nextdata, len);
  127. nextdata += len;
  128. rest -= len;
  129. eerb->head += len;
  130. if (eerb->head == eerb->buffersize)
  131. eerb->head = 0; /* wrap around */
  132. BUG_ON(eerb->head > eerb->buffersize);
  133. }
  134. }
  135. /*
  136. * Needs to be called with bufferlock held.
  137. */
  138. static int dasd_eer_read_buffer(struct eerbuffer *eerb, char *data, int count)
  139. {
  140. unsigned long tailindex,localtail;
  141. unsigned long rest, len, finalcount;
  142. char *nextdata;
  143. finalcount = min(count, dasd_eer_get_filled_bytes(eerb));
  144. nextdata = data;
  145. rest = finalcount;
  146. while (rest > 0) {
  147. tailindex = eerb->tail / PAGE_SIZE;
  148. localtail = eerb->tail % PAGE_SIZE;
  149. len = min(rest, PAGE_SIZE - localtail);
  150. memcpy(nextdata, eerb->buffer[tailindex] + localtail, len);
  151. nextdata += len;
  152. rest -= len;
  153. eerb->tail += len;
  154. if (eerb->tail == eerb->buffersize)
  155. eerb->tail = 0; /* wrap around */
  156. BUG_ON(eerb->tail > eerb->buffersize);
  157. }
  158. return finalcount;
  159. }
  160. /*
  161. * Whenever you want to write a blob of data to the internal buffer you
  162. * have to start by using this function first. It will write the number
  163. * of bytes that will be written to the buffer. If necessary it will remove
  164. * old records to make room for the new one.
  165. * Needs to be called with bufferlock held.
  166. */
  167. static int dasd_eer_start_record(struct eerbuffer *eerb, int count)
  168. {
  169. int tailcount;
  170. if (count + sizeof(count) > eerb->buffersize)
  171. return -ENOMEM;
  172. while (dasd_eer_get_free_bytes(eerb) < count + sizeof(count)) {
  173. if (eerb->residual > 0) {
  174. eerb->tail += eerb->residual;
  175. if (eerb->tail >= eerb->buffersize)
  176. eerb->tail -= eerb->buffersize;
  177. eerb->residual = -1;
  178. }
  179. dasd_eer_read_buffer(eerb, (char *) &tailcount,
  180. sizeof(tailcount));
  181. eerb->tail += tailcount;
  182. if (eerb->tail >= eerb->buffersize)
  183. eerb->tail -= eerb->buffersize;
  184. }
  185. dasd_eer_write_buffer(eerb, (char*) &count, sizeof(count));
  186. return 0;
  187. };
  188. /*
  189. * Release pages that are not used anymore.
  190. */
  191. static void dasd_eer_free_buffer_pages(char **buf, int no_pages)
  192. {
  193. int i;
  194. for (i = 0; i < no_pages; i++)
  195. free_page((unsigned long) buf[i]);
  196. }
  197. /*
  198. * Allocate a new set of memory pages.
  199. */
  200. static int dasd_eer_allocate_buffer_pages(char **buf, int no_pages)
  201. {
  202. int i;
  203. for (i = 0; i < no_pages; i++) {
  204. buf[i] = (char *) get_zeroed_page(GFP_KERNEL);
  205. if (!buf[i]) {
  206. dasd_eer_free_buffer_pages(buf, i);
  207. return -ENOMEM;
  208. }
  209. }
  210. return 0;
  211. }
  212. /*
  213. * SECTION: The extended error reporting functionality
  214. */
  215. /*
  216. * When a DASD device driver wants to report an error, it calls the
  217. * function dasd_eer_write and gives the respective trigger ID as
  218. * parameter. Currently there are four kinds of triggers:
  219. *
  220. * DASD_EER_FATALERROR: all kinds of unrecoverable I/O problems
  221. * DASD_EER_PPRCSUSPEND: PPRC was suspended
  222. * DASD_EER_NOPATH: There is no path to the device left.
  223. * DASD_EER_STATECHANGE: The state of the device has changed.
  224. *
  225. * For the first three triggers all required information can be supplied by
  226. * the caller. For these triggers a record is written by the function
  227. * dasd_eer_write_standard_trigger.
  228. *
  229. * The DASD_EER_STATECHANGE trigger is special since a sense subsystem
  230. * status ccw need to be executed to gather the necessary sense data first.
  231. * The dasd_eer_snss function will queue the SNSS request and the request
  232. * callback will then call dasd_eer_write with the DASD_EER_STATCHANGE
  233. * trigger.
  234. *
  235. * To avoid memory allocations at runtime, the necessary memory is allocated
  236. * when the extended error reporting is enabled for a device (by
  237. * dasd_eer_probe). There is one sense subsystem status request for each
  238. * eer enabled DASD device. The presence of the cqr in device->eer_cqr
  239. * indicates that eer is enable for the device. The use of the snss request
  240. * is protected by the DASD_FLAG_EER_IN_USE bit. When this flag indicates
  241. * that the cqr is currently in use, dasd_eer_snss cannot start a second
  242. * request but sets the DASD_FLAG_EER_SNSS flag instead. The callback of
  243. * the SNSS request will check the bit and call dasd_eer_snss again.
  244. */
  245. #define SNSS_DATA_SIZE 44
  246. #define DASD_EER_BUSID_SIZE 10
  247. struct dasd_eer_header {
  248. __u32 total_size;
  249. __u32 trigger;
  250. __u64 tv_sec;
  251. __u64 tv_usec;
  252. char busid[DASD_EER_BUSID_SIZE];
  253. } __attribute__ ((packed));
  254. /*
  255. * The following function can be used for those triggers that have
  256. * all necessary data available when the function is called.
  257. * If the parameter cqr is not NULL, the chain of requests will be searched
  258. * for valid sense data, and all valid sense data sets will be added to
  259. * the triggers data.
  260. */
  261. static void dasd_eer_write_standard_trigger(struct dasd_device *device,
  262. struct dasd_ccw_req *cqr,
  263. int trigger)
  264. {
  265. struct dasd_ccw_req *temp_cqr;
  266. int data_size;
  267. struct timeval tv;
  268. struct dasd_eer_header header;
  269. unsigned long flags;
  270. struct eerbuffer *eerb;
  271. char *sense;
  272. /* go through cqr chain and count the valid sense data sets */
  273. data_size = 0;
  274. for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers)
  275. if (dasd_get_sense(&temp_cqr->irb))
  276. data_size += 32;
  277. header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
  278. header.trigger = trigger;
  279. do_gettimeofday(&tv);
  280. header.tv_sec = tv.tv_sec;
  281. header.tv_usec = tv.tv_usec;
  282. strncpy(header.busid, dev_name(&device->cdev->dev),
  283. DASD_EER_BUSID_SIZE);
  284. spin_lock_irqsave(&bufferlock, flags);
  285. list_for_each_entry(eerb, &bufferlist, list) {
  286. dasd_eer_start_record(eerb, header.total_size);
  287. dasd_eer_write_buffer(eerb, (char *) &header, sizeof(header));
  288. for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers) {
  289. sense = dasd_get_sense(&temp_cqr->irb);
  290. if (sense)
  291. dasd_eer_write_buffer(eerb, sense, 32);
  292. }
  293. dasd_eer_write_buffer(eerb, "EOR", 4);
  294. }
  295. spin_unlock_irqrestore(&bufferlock, flags);
  296. wake_up_interruptible(&dasd_eer_read_wait_queue);
  297. }
  298. /*
  299. * This function writes a DASD_EER_STATECHANGE trigger.
  300. */
  301. static void dasd_eer_write_snss_trigger(struct dasd_device *device,
  302. struct dasd_ccw_req *cqr,
  303. int trigger)
  304. {
  305. int data_size;
  306. int snss_rc;
  307. struct timeval tv;
  308. struct dasd_eer_header header;
  309. unsigned long flags;
  310. struct eerbuffer *eerb;
  311. snss_rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  312. if (snss_rc)
  313. data_size = 0;
  314. else
  315. data_size = SNSS_DATA_SIZE;
  316. header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
  317. header.trigger = DASD_EER_STATECHANGE;
  318. do_gettimeofday(&tv);
  319. header.tv_sec = tv.tv_sec;
  320. header.tv_usec = tv.tv_usec;
  321. strncpy(header.busid, dev_name(&device->cdev->dev),
  322. DASD_EER_BUSID_SIZE);
  323. spin_lock_irqsave(&bufferlock, flags);
  324. list_for_each_entry(eerb, &bufferlist, list) {
  325. dasd_eer_start_record(eerb, header.total_size);
  326. dasd_eer_write_buffer(eerb, (char *) &header , sizeof(header));
  327. if (!snss_rc)
  328. dasd_eer_write_buffer(eerb, cqr->data, SNSS_DATA_SIZE);
  329. dasd_eer_write_buffer(eerb, "EOR", 4);
  330. }
  331. spin_unlock_irqrestore(&bufferlock, flags);
  332. wake_up_interruptible(&dasd_eer_read_wait_queue);
  333. }
  334. /*
  335. * This function is called for all triggers. It calls the appropriate
  336. * function that writes the actual trigger records.
  337. */
  338. void dasd_eer_write(struct dasd_device *device, struct dasd_ccw_req *cqr,
  339. unsigned int id)
  340. {
  341. if (!device->eer_cqr)
  342. return;
  343. switch (id) {
  344. case DASD_EER_FATALERROR:
  345. case DASD_EER_PPRCSUSPEND:
  346. dasd_eer_write_standard_trigger(device, cqr, id);
  347. break;
  348. case DASD_EER_NOPATH:
  349. dasd_eer_write_standard_trigger(device, NULL, id);
  350. break;
  351. case DASD_EER_STATECHANGE:
  352. dasd_eer_write_snss_trigger(device, cqr, id);
  353. break;
  354. default: /* unknown trigger, so we write it without any sense data */
  355. dasd_eer_write_standard_trigger(device, NULL, id);
  356. break;
  357. }
  358. }
  359. EXPORT_SYMBOL(dasd_eer_write);
  360. /*
  361. * Start a sense subsystem status request.
  362. * Needs to be called with the device held.
  363. */
  364. void dasd_eer_snss(struct dasd_device *device)
  365. {
  366. struct dasd_ccw_req *cqr;
  367. cqr = device->eer_cqr;
  368. if (!cqr) /* Device not eer enabled. */
  369. return;
  370. if (test_and_set_bit(DASD_FLAG_EER_IN_USE, &device->flags)) {
  371. /* Sense subsystem status request in use. */
  372. set_bit(DASD_FLAG_EER_SNSS, &device->flags);
  373. return;
  374. }
  375. /* cdev is already locked, can't use dasd_add_request_head */
  376. clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
  377. cqr->status = DASD_CQR_QUEUED;
  378. list_add(&cqr->devlist, &device->ccw_queue);
  379. dasd_schedule_device_bh(device);
  380. }
  381. /*
  382. * Callback function for use with sense subsystem status request.
  383. */
  384. static void dasd_eer_snss_cb(struct dasd_ccw_req *cqr, void *data)
  385. {
  386. struct dasd_device *device = cqr->startdev;
  387. unsigned long flags;
  388. dasd_eer_write(device, cqr, DASD_EER_STATECHANGE);
  389. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  390. if (device->eer_cqr == cqr) {
  391. clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
  392. if (test_bit(DASD_FLAG_EER_SNSS, &device->flags))
  393. /* Another SNSS has been requested in the meantime. */
  394. dasd_eer_snss(device);
  395. cqr = NULL;
  396. }
  397. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  398. if (cqr)
  399. /*
  400. * Extended error recovery has been switched off while
  401. * the SNSS request was running. It could even have
  402. * been switched off and on again in which case there
  403. * is a new ccw in device->eer_cqr. Free the "old"
  404. * snss request now.
  405. */
  406. dasd_kfree_request(cqr, device);
  407. }
  408. /*
  409. * Enable error reporting on a given device.
  410. */
  411. int dasd_eer_enable(struct dasd_device *device)
  412. {
  413. struct dasd_ccw_req *cqr;
  414. unsigned long flags;
  415. struct ccw1 *ccw;
  416. if (device->eer_cqr)
  417. return 0;
  418. if (!device->discipline || strcmp(device->discipline->name, "ECKD"))
  419. return -EPERM; /* FIXME: -EMEDIUMTYPE ? */
  420. cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* SNSS */,
  421. SNSS_DATA_SIZE, device);
  422. if (IS_ERR(cqr))
  423. return -ENOMEM;
  424. cqr->startdev = device;
  425. cqr->retries = 255;
  426. cqr->expires = 10 * HZ;
  427. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  428. set_bit(DASD_CQR_ALLOW_SLOCK, &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_tod_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. .llseek = noop_llseek,
  602. };
  603. static struct miscdevice *dasd_eer_dev = NULL;
  604. int __init dasd_eer_init(void)
  605. {
  606. int rc;
  607. dasd_eer_dev = kzalloc(sizeof(*dasd_eer_dev), GFP_KERNEL);
  608. if (!dasd_eer_dev)
  609. return -ENOMEM;
  610. dasd_eer_dev->minor = MISC_DYNAMIC_MINOR;
  611. dasd_eer_dev->name = "dasd_eer";
  612. dasd_eer_dev->fops = &dasd_eer_fops;
  613. rc = misc_register(dasd_eer_dev);
  614. if (rc) {
  615. kfree(dasd_eer_dev);
  616. dasd_eer_dev = NULL;
  617. DBF_EVENT(DBF_ERR, "%s", "dasd_eer_init could not "
  618. "register misc device");
  619. return rc;
  620. }
  621. return 0;
  622. }
  623. void dasd_eer_exit(void)
  624. {
  625. if (dasd_eer_dev) {
  626. misc_deregister(dasd_eer_dev);
  627. kfree(dasd_eer_dev);
  628. dasd_eer_dev = NULL;
  629. }
  630. }