dasd_eer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Character device driver for extended error reporting.
  4. *
  5. * Copyright IBM Corp. 2005
  6. * extended error reporting for DASD ECKD devices
  7. * Author(s): Stefan Weinhuber <wein@de.ibm.com>
  8. */
  9. #include <linux/export.h>
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/device.h>
  17. #include <linux/poll.h>
  18. #include <linux/mutex.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/atomic.h>
  23. #include <asm/ebcdic.h>
  24. #include "dasd_int.h"
  25. #include "dasd_eckd.h"
  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 timespec64 ts;
  265. struct dasd_eer_header header;
  266. unsigned long flags;
  267. struct eerbuffer *eerb;
  268. char *sense;
  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 (dasd_get_sense(&temp_cqr->irb))
  273. data_size += 32;
  274. header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
  275. header.trigger = trigger;
  276. ktime_get_real_ts64(&ts);
  277. header.tv_sec = ts.tv_sec;
  278. header.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  279. strscpy(header.busid, dev_name(&device->cdev->dev),
  280. 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. sense = dasd_get_sense(&temp_cqr->irb);
  287. if (sense)
  288. dasd_eer_write_buffer(eerb, sense, 32);
  289. }
  290. dasd_eer_write_buffer(eerb, "EOR", 4);
  291. }
  292. spin_unlock_irqrestore(&bufferlock, flags);
  293. wake_up_interruptible(&dasd_eer_read_wait_queue);
  294. }
  295. /*
  296. * This function writes a DASD_EER_STATECHANGE trigger.
  297. */
  298. static void dasd_eer_write_snss_trigger(struct dasd_device *device,
  299. struct dasd_ccw_req *cqr,
  300. int trigger)
  301. {
  302. int data_size;
  303. int snss_rc;
  304. struct timespec64 ts;
  305. struct dasd_eer_header header;
  306. unsigned long flags;
  307. struct eerbuffer *eerb;
  308. snss_rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  309. if (snss_rc)
  310. data_size = 0;
  311. else
  312. data_size = SNSS_DATA_SIZE;
  313. header.total_size = sizeof(header) + data_size + 4; /* "EOR" */
  314. header.trigger = DASD_EER_STATECHANGE;
  315. ktime_get_real_ts64(&ts);
  316. header.tv_sec = ts.tv_sec;
  317. header.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  318. strscpy(header.busid, dev_name(&device->cdev->dev),
  319. DASD_EER_BUSID_SIZE);
  320. spin_lock_irqsave(&bufferlock, flags);
  321. list_for_each_entry(eerb, &bufferlist, list) {
  322. dasd_eer_start_record(eerb, header.total_size);
  323. dasd_eer_write_buffer(eerb, (char *) &header , sizeof(header));
  324. if (!snss_rc)
  325. dasd_eer_write_buffer(eerb, cqr->data, SNSS_DATA_SIZE);
  326. dasd_eer_write_buffer(eerb, "EOR", 4);
  327. }
  328. spin_unlock_irqrestore(&bufferlock, flags);
  329. wake_up_interruptible(&dasd_eer_read_wait_queue);
  330. }
  331. /*
  332. * This function is called for all triggers. It calls the appropriate
  333. * function that writes the actual trigger records.
  334. */
  335. void dasd_eer_write(struct dasd_device *device, struct dasd_ccw_req *cqr,
  336. unsigned int id)
  337. {
  338. if (!device->eer_cqr)
  339. return;
  340. switch (id) {
  341. case DASD_EER_FATALERROR:
  342. case DASD_EER_PPRCSUSPEND:
  343. dasd_eer_write_standard_trigger(device, cqr, id);
  344. break;
  345. case DASD_EER_NOPATH:
  346. case DASD_EER_NOSPC:
  347. case DASD_EER_AUTOQUIESCE:
  348. dasd_eer_write_standard_trigger(device, NULL, id);
  349. break;
  350. case DASD_EER_STATECHANGE:
  351. dasd_eer_write_snss_trigger(device, cqr, id);
  352. break;
  353. default: /* unknown trigger, so we write it without any sense data */
  354. dasd_eer_write_standard_trigger(device, NULL, id);
  355. break;
  356. }
  357. }
  358. EXPORT_SYMBOL(dasd_eer_write);
  359. /*
  360. * Start a sense subsystem status request.
  361. * Needs to be called with the device held.
  362. */
  363. void dasd_eer_snss(struct dasd_device *device)
  364. {
  365. struct dasd_ccw_req *cqr;
  366. cqr = device->eer_cqr;
  367. if (!cqr) /* Device not eer enabled. */
  368. return;
  369. if (test_and_set_bit(DASD_FLAG_EER_IN_USE, &device->flags)) {
  370. /* Sense subsystem status request in use. */
  371. set_bit(DASD_FLAG_EER_SNSS, &device->flags);
  372. return;
  373. }
  374. /* cdev is already locked, can't use dasd_add_request_head */
  375. clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
  376. cqr->status = DASD_CQR_QUEUED;
  377. list_add(&cqr->devlist, &device->ccw_queue);
  378. dasd_schedule_device_bh(device);
  379. }
  380. /*
  381. * Callback function for use with sense subsystem status request.
  382. */
  383. static void dasd_eer_snss_cb(struct dasd_ccw_req *cqr, void *data)
  384. {
  385. struct dasd_device *device = cqr->startdev;
  386. unsigned long flags;
  387. dasd_eer_write(device, cqr, DASD_EER_STATECHANGE);
  388. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  389. if (device->eer_cqr == cqr) {
  390. clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
  391. if (test_bit(DASD_FLAG_EER_SNSS, &device->flags))
  392. /* Another SNSS has been requested in the meantime. */
  393. dasd_eer_snss(device);
  394. cqr = NULL;
  395. }
  396. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  397. if (cqr)
  398. /*
  399. * Extended error recovery has been switched off while
  400. * the SNSS request was running. It could even have
  401. * been switched off and on again in which case there
  402. * is a new ccw in device->eer_cqr. Free the "old"
  403. * snss request now.
  404. */
  405. dasd_sfree_request(cqr, device);
  406. }
  407. /*
  408. * Enable error reporting on a given device.
  409. */
  410. int dasd_eer_enable(struct dasd_device *device)
  411. {
  412. struct dasd_ccw_req *cqr = NULL;
  413. unsigned long flags;
  414. struct ccw1 *ccw;
  415. int rc = 0;
  416. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  417. if (device->eer_cqr)
  418. goto out;
  419. else if (!device->discipline ||
  420. strcmp(device->discipline->name, "ECKD"))
  421. rc = -EMEDIUMTYPE;
  422. else if (test_bit(DASD_FLAG_OFFLINE, &device->flags))
  423. rc = -EBUSY;
  424. if (rc)
  425. goto out;
  426. cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* SNSS */,
  427. SNSS_DATA_SIZE, device, NULL);
  428. if (IS_ERR(cqr)) {
  429. rc = -ENOMEM;
  430. cqr = NULL;
  431. goto out;
  432. }
  433. cqr->startdev = device;
  434. cqr->retries = 255;
  435. cqr->expires = 10 * HZ;
  436. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  437. set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
  438. ccw = cqr->cpaddr;
  439. ccw->cmd_code = DASD_ECKD_CCW_SNSS;
  440. ccw->count = SNSS_DATA_SIZE;
  441. ccw->flags = 0;
  442. ccw->cda = virt_to_dma32(cqr->data);
  443. cqr->buildclk = get_tod_clock();
  444. cqr->status = DASD_CQR_FILLED;
  445. cqr->callback = dasd_eer_snss_cb;
  446. if (!device->eer_cqr) {
  447. device->eer_cqr = cqr;
  448. cqr = NULL;
  449. }
  450. out:
  451. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  452. if (cqr)
  453. dasd_sfree_request(cqr, device);
  454. return rc;
  455. }
  456. /*
  457. * Disable error reporting on a given device.
  458. */
  459. void dasd_eer_disable(struct dasd_device *device)
  460. {
  461. struct dasd_ccw_req *cqr;
  462. unsigned long flags;
  463. int in_use;
  464. if (!device->eer_cqr)
  465. return;
  466. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  467. cqr = device->eer_cqr;
  468. device->eer_cqr = NULL;
  469. clear_bit(DASD_FLAG_EER_SNSS, &device->flags);
  470. in_use = test_and_clear_bit(DASD_FLAG_EER_IN_USE, &device->flags);
  471. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  472. if (cqr && !in_use)
  473. dasd_sfree_request(cqr, device);
  474. }
  475. /*
  476. * SECTION: the device operations
  477. */
  478. /*
  479. * On the one side we need a lock to access our internal buffer, on the
  480. * other side a copy_to_user can sleep. So we need to copy the data we have
  481. * to transfer in a readbuffer, which is protected by the readbuffer_mutex.
  482. */
  483. static char readbuffer[PAGE_SIZE];
  484. static DEFINE_MUTEX(readbuffer_mutex);
  485. static int dasd_eer_open(struct inode *inp, struct file *filp)
  486. {
  487. struct eerbuffer *eerb;
  488. unsigned long flags;
  489. eerb = kzalloc_obj(struct eerbuffer);
  490. if (!eerb)
  491. return -ENOMEM;
  492. eerb->buffer_page_count = eer_pages;
  493. if (eerb->buffer_page_count < 1 ||
  494. eerb->buffer_page_count > INT_MAX / PAGE_SIZE) {
  495. kfree(eerb);
  496. DBF_EVENT(DBF_WARNING, "can't open device since module "
  497. "parameter eer_pages is smaller than 1 or"
  498. " bigger than %d", (int)(INT_MAX / PAGE_SIZE));
  499. return -EINVAL;
  500. }
  501. eerb->buffersize = eerb->buffer_page_count * PAGE_SIZE;
  502. eerb->buffer = kmalloc_array(eerb->buffer_page_count, sizeof(char *),
  503. GFP_KERNEL);
  504. if (!eerb->buffer) {
  505. kfree(eerb);
  506. return -ENOMEM;
  507. }
  508. if (dasd_eer_allocate_buffer_pages(eerb->buffer,
  509. eerb->buffer_page_count)) {
  510. kfree(eerb->buffer);
  511. kfree(eerb);
  512. return -ENOMEM;
  513. }
  514. filp->private_data = eerb;
  515. spin_lock_irqsave(&bufferlock, flags);
  516. list_add(&eerb->list, &bufferlist);
  517. spin_unlock_irqrestore(&bufferlock, flags);
  518. return nonseekable_open(inp,filp);
  519. }
  520. static int dasd_eer_close(struct inode *inp, struct file *filp)
  521. {
  522. struct eerbuffer *eerb;
  523. unsigned long flags;
  524. eerb = (struct eerbuffer *) filp->private_data;
  525. spin_lock_irqsave(&bufferlock, flags);
  526. list_del(&eerb->list);
  527. spin_unlock_irqrestore(&bufferlock, flags);
  528. dasd_eer_free_buffer_pages(eerb->buffer, eerb->buffer_page_count);
  529. kfree(eerb->buffer);
  530. kfree(eerb);
  531. return 0;
  532. }
  533. static ssize_t dasd_eer_read(struct file *filp, char __user *buf,
  534. size_t count, loff_t *ppos)
  535. {
  536. int tc,rc;
  537. int tailcount,effective_count;
  538. unsigned long flags;
  539. struct eerbuffer *eerb;
  540. eerb = (struct eerbuffer *) filp->private_data;
  541. if (mutex_lock_interruptible(&readbuffer_mutex))
  542. return -ERESTARTSYS;
  543. spin_lock_irqsave(&bufferlock, flags);
  544. if (eerb->residual < 0) { /* the remainder of this record */
  545. /* has been deleted */
  546. eerb->residual = 0;
  547. spin_unlock_irqrestore(&bufferlock, flags);
  548. mutex_unlock(&readbuffer_mutex);
  549. return -EIO;
  550. } else if (eerb->residual > 0) {
  551. /* OK we still have a second half of a record to deliver */
  552. effective_count = min(eerb->residual, (int) count);
  553. eerb->residual -= effective_count;
  554. } else {
  555. tc = 0;
  556. while (!tc) {
  557. tc = dasd_eer_read_buffer(eerb, (char *) &tailcount,
  558. sizeof(tailcount));
  559. if (!tc) {
  560. /* no data available */
  561. spin_unlock_irqrestore(&bufferlock, flags);
  562. mutex_unlock(&readbuffer_mutex);
  563. if (filp->f_flags & O_NONBLOCK)
  564. return -EAGAIN;
  565. rc = wait_event_interruptible(
  566. dasd_eer_read_wait_queue,
  567. eerb->head != eerb->tail);
  568. if (rc)
  569. return rc;
  570. if (mutex_lock_interruptible(&readbuffer_mutex))
  571. return -ERESTARTSYS;
  572. spin_lock_irqsave(&bufferlock, flags);
  573. }
  574. }
  575. WARN_ON(tc != sizeof(tailcount));
  576. effective_count = min(tailcount,(int)count);
  577. eerb->residual = tailcount - effective_count;
  578. }
  579. tc = dasd_eer_read_buffer(eerb, readbuffer, effective_count);
  580. WARN_ON(tc != effective_count);
  581. spin_unlock_irqrestore(&bufferlock, flags);
  582. if (copy_to_user(buf, readbuffer, effective_count)) {
  583. mutex_unlock(&readbuffer_mutex);
  584. return -EFAULT;
  585. }
  586. mutex_unlock(&readbuffer_mutex);
  587. return effective_count;
  588. }
  589. static __poll_t dasd_eer_poll(struct file *filp, poll_table *ptable)
  590. {
  591. __poll_t mask;
  592. unsigned long flags;
  593. struct eerbuffer *eerb;
  594. eerb = (struct eerbuffer *) filp->private_data;
  595. poll_wait(filp, &dasd_eer_read_wait_queue, ptable);
  596. spin_lock_irqsave(&bufferlock, flags);
  597. if (eerb->head != eerb->tail)
  598. mask = EPOLLIN | EPOLLRDNORM ;
  599. else
  600. mask = 0;
  601. spin_unlock_irqrestore(&bufferlock, flags);
  602. return mask;
  603. }
  604. static const struct file_operations dasd_eer_fops = {
  605. .open = &dasd_eer_open,
  606. .release = &dasd_eer_close,
  607. .read = &dasd_eer_read,
  608. .poll = &dasd_eer_poll,
  609. .owner = THIS_MODULE,
  610. .llseek = noop_llseek,
  611. };
  612. static struct miscdevice *dasd_eer_dev = NULL;
  613. int __init dasd_eer_init(void)
  614. {
  615. int rc;
  616. dasd_eer_dev = kzalloc_obj(*dasd_eer_dev);
  617. if (!dasd_eer_dev)
  618. return -ENOMEM;
  619. dasd_eer_dev->minor = MISC_DYNAMIC_MINOR;
  620. dasd_eer_dev->name = "dasd_eer";
  621. dasd_eer_dev->fops = &dasd_eer_fops;
  622. rc = misc_register(dasd_eer_dev);
  623. if (rc) {
  624. kfree(dasd_eer_dev);
  625. dasd_eer_dev = NULL;
  626. DBF_EVENT(DBF_ERR, "%s", "dasd_eer_init could not "
  627. "register misc device");
  628. return rc;
  629. }
  630. return 0;
  631. }
  632. void dasd_eer_exit(void)
  633. {
  634. if (dasd_eer_dev) {
  635. misc_deregister(dasd_eer_dev);
  636. kfree(dasd_eer_dev);
  637. dasd_eer_dev = NULL;
  638. }
  639. }