dasd_eer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 <asm/uaccess.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. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  418. cqr->cpaddr->cmd_code = DASD_ECKD_CCW_SNSS;
  419. cqr->cpaddr->count = SNSS_DATA_SIZE;
  420. cqr->cpaddr->flags = 0;
  421. cqr->cpaddr->cda = (__u32)(addr_t) cqr->data;
  422. cqr->buildclk = get_clock();
  423. cqr->status = DASD_CQR_FILLED;
  424. cqr->callback = dasd_eer_snss_cb;
  425. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  426. if (!device->eer_cqr) {
  427. device->eer_cqr = cqr;
  428. cqr = NULL;
  429. }
  430. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  431. if (cqr)
  432. dasd_kfree_request(cqr, device);
  433. return 0;
  434. }
  435. /*
  436. * Disable error reporting on a given device.
  437. */
  438. void dasd_eer_disable(struct dasd_device *device)
  439. {
  440. struct dasd_ccw_req *cqr;
  441. unsigned long flags;
  442. int in_use;
  443. if (!device->eer_cqr)
  444. return;
  445. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  446. cqr = device->eer_cqr;
  447. device->eer_cqr = NULL;
  448. clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
  449. in_use = test_and_clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
  450. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  451. if (cqr && !in_use)
  452. dasd_kfree_request(cqr, device);
  453. }
  454. /*
  455. * SECTION: the device operations
  456. */
  457. /*
  458. * On the one side we need a lock to access our internal buffer, on the
  459. * other side a copy_to_user can sleep. So we need to copy the data we have
  460. * to transfer in a readbuffer, which is protected by the readbuffer_mutex.
  461. */
  462. static char readbuffer[PAGE_SIZE];
  463. static DEFINE_MUTEX(readbuffer_mutex);
  464. static int dasd_eer_open(struct inode *inp, struct file *filp)
  465. {
  466. struct eerbuffer *eerb;
  467. unsigned long flags;
  468. eerb = kzalloc(sizeof(struct eerbuffer), GFP_KERNEL);
  469. if (!eerb)
  470. return -ENOMEM;
  471. eerb->buffer_page_count = eer_pages;
  472. if (eerb->buffer_page_count < 1 ||
  473. eerb->buffer_page_count > INT_MAX / PAGE_SIZE) {
  474. kfree(eerb);
  475. MESSAGE(KERN_WARNING, "can't open device since module "
  476. "parameter eer_pages is smaller then 1 or"
  477. " bigger then %d", (int)(INT_MAX / PAGE_SIZE));
  478. return -EINVAL;
  479. }
  480. eerb->buffersize = eerb->buffer_page_count * PAGE_SIZE;
  481. eerb->buffer = kmalloc(eerb->buffer_page_count * sizeof(char *),
  482. GFP_KERNEL);
  483. if (!eerb->buffer) {
  484. kfree(eerb);
  485. return -ENOMEM;
  486. }
  487. if (dasd_eer_allocate_buffer_pages(eerb->buffer,
  488. eerb->buffer_page_count)) {
  489. kfree(eerb->buffer);
  490. kfree(eerb);
  491. return -ENOMEM;
  492. }
  493. filp->private_data = eerb;
  494. spin_lock_irqsave(&bufferlock, flags);
  495. list_add(&eerb->list, &bufferlist);
  496. spin_unlock_irqrestore(&bufferlock, flags);
  497. return nonseekable_open(inp,filp);
  498. }
  499. static int dasd_eer_close(struct inode *inp, struct file *filp)
  500. {
  501. struct eerbuffer *eerb;
  502. unsigned long flags;
  503. eerb = (struct eerbuffer *) filp->private_data;
  504. spin_lock_irqsave(&bufferlock, flags);
  505. list_del(&eerb->list);
  506. spin_unlock_irqrestore(&bufferlock, flags);
  507. dasd_eer_free_buffer_pages(eerb->buffer, eerb->buffer_page_count);
  508. kfree(eerb->buffer);
  509. kfree(eerb);
  510. return 0;
  511. }
  512. static ssize_t dasd_eer_read(struct file *filp, char __user *buf,
  513. size_t count, loff_t *ppos)
  514. {
  515. int tc,rc;
  516. int tailcount,effective_count;
  517. unsigned long flags;
  518. struct eerbuffer *eerb;
  519. eerb = (struct eerbuffer *) filp->private_data;
  520. if (mutex_lock_interruptible(&readbuffer_mutex))
  521. return -ERESTARTSYS;
  522. spin_lock_irqsave(&bufferlock, flags);
  523. if (eerb->residual < 0) { /* the remainder of this record */
  524. /* has been deleted */
  525. eerb->residual = 0;
  526. spin_unlock_irqrestore(&bufferlock, flags);
  527. mutex_unlock(&readbuffer_mutex);
  528. return -EIO;
  529. } else if (eerb->residual > 0) {
  530. /* OK we still have a second half of a record to deliver */
  531. effective_count = min(eerb->residual, (int) count);
  532. eerb->residual -= effective_count;
  533. } else {
  534. tc = 0;
  535. while (!tc) {
  536. tc = dasd_eer_read_buffer(eerb, (char *) &tailcount,
  537. sizeof(tailcount));
  538. if (!tc) {
  539. /* no data available */
  540. spin_unlock_irqrestore(&bufferlock, flags);
  541. mutex_unlock(&readbuffer_mutex);
  542. if (filp->f_flags & O_NONBLOCK)
  543. return -EAGAIN;
  544. rc = wait_event_interruptible(
  545. dasd_eer_read_wait_queue,
  546. eerb->head != eerb->tail);
  547. if (rc)
  548. return rc;
  549. if (mutex_lock_interruptible(&readbuffer_mutex))
  550. return -ERESTARTSYS;
  551. spin_lock_irqsave(&bufferlock, flags);
  552. }
  553. }
  554. WARN_ON(tc != sizeof(tailcount));
  555. effective_count = min(tailcount,(int)count);
  556. eerb->residual = tailcount - effective_count;
  557. }
  558. tc = dasd_eer_read_buffer(eerb, readbuffer, effective_count);
  559. WARN_ON(tc != effective_count);
  560. spin_unlock_irqrestore(&bufferlock, flags);
  561. if (copy_to_user(buf, readbuffer, effective_count)) {
  562. mutex_unlock(&readbuffer_mutex);
  563. return -EFAULT;
  564. }
  565. mutex_unlock(&readbuffer_mutex);
  566. return effective_count;
  567. }
  568. static unsigned int dasd_eer_poll(struct file *filp, poll_table *ptable)
  569. {
  570. unsigned int mask;
  571. unsigned long flags;
  572. struct eerbuffer *eerb;
  573. eerb = (struct eerbuffer *) filp->private_data;
  574. poll_wait(filp, &dasd_eer_read_wait_queue, ptable);
  575. spin_lock_irqsave(&bufferlock, flags);
  576. if (eerb->head != eerb->tail)
  577. mask = POLLIN | POLLRDNORM ;
  578. else
  579. mask = 0;
  580. spin_unlock_irqrestore(&bufferlock, flags);
  581. return mask;
  582. }
  583. static const struct file_operations dasd_eer_fops = {
  584. .open = &dasd_eer_open,
  585. .release = &dasd_eer_close,
  586. .read = &dasd_eer_read,
  587. .poll = &dasd_eer_poll,
  588. .owner = THIS_MODULE,
  589. };
  590. static struct miscdevice *dasd_eer_dev = NULL;
  591. int __init dasd_eer_init(void)
  592. {
  593. int rc;
  594. dasd_eer_dev = kzalloc(sizeof(*dasd_eer_dev), GFP_KERNEL);
  595. if (!dasd_eer_dev)
  596. return -ENOMEM;
  597. dasd_eer_dev->minor = MISC_DYNAMIC_MINOR;
  598. dasd_eer_dev->name = "dasd_eer";
  599. dasd_eer_dev->fops = &dasd_eer_fops;
  600. rc = misc_register(dasd_eer_dev);
  601. if (rc) {
  602. kfree(dasd_eer_dev);
  603. dasd_eer_dev = NULL;
  604. MESSAGE(KERN_ERR, "%s", "dasd_eer_init could not "
  605. "register misc device");
  606. return rc;
  607. }
  608. return 0;
  609. }
  610. void dasd_eer_exit(void)
  611. {
  612. if (dasd_eer_dev) {
  613. WARN_ON(misc_deregister(dasd_eer_dev) != 0);
  614. kfree(dasd_eer_dev);
  615. dasd_eer_dev = NULL;
  616. }
  617. }