dasd_eer.c 20 KB

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