dasd_eer.c 20 KB

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