dasd_diag.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * File...........: linux/drivers/s390/block/dasd_diag.c
  3. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  4. * Based on.......: linux/drivers/s390/block/mdisk.c
  5. * ...............: by Hartmunt Penner <hpenner@de.ibm.com>
  6. * Bugreports.to..: <Linux390@de.ibm.com>
  7. * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999,2000
  8. *
  9. * $Revision: 1.42 $
  10. */
  11. #include <linux/config.h>
  12. #include <linux/stddef.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/hdreg.h> /* HDIO_GETGEO */
  16. #include <linux/bio.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <asm/dasd.h>
  20. #include <asm/debug.h>
  21. #include <asm/ebcdic.h>
  22. #include <asm/io.h>
  23. #include <asm/s390_ext.h>
  24. #include <asm/todclk.h>
  25. #include "dasd_int.h"
  26. #include "dasd_diag.h"
  27. #ifdef PRINTK_HEADER
  28. #undef PRINTK_HEADER
  29. #endif /* PRINTK_HEADER */
  30. #define PRINTK_HEADER "dasd(diag):"
  31. MODULE_LICENSE("GPL");
  32. struct dasd_discipline dasd_diag_discipline;
  33. struct dasd_diag_private {
  34. struct dasd_diag_characteristics rdc_data;
  35. struct dasd_diag_rw_io iob;
  36. struct dasd_diag_init_io iib;
  37. unsigned int pt_block;
  38. };
  39. struct dasd_diag_req {
  40. int block_count;
  41. struct dasd_diag_bio bio[0];
  42. };
  43. static __inline__ int
  44. dia250(void *iob, int cmd)
  45. {
  46. int rc;
  47. __asm__ __volatile__(" lhi %0,3\n"
  48. " lr 0,%2\n"
  49. " diag 0,%1,0x250\n"
  50. "0: ipm %0\n"
  51. " srl %0,28\n"
  52. " or %0,1\n"
  53. "1:\n"
  54. #ifndef CONFIG_ARCH_S390X
  55. ".section __ex_table,\"a\"\n"
  56. " .align 4\n"
  57. " .long 0b,1b\n"
  58. ".previous\n"
  59. #else
  60. ".section __ex_table,\"a\"\n"
  61. " .align 8\n"
  62. " .quad 0b,1b\n"
  63. ".previous\n"
  64. #endif
  65. : "=&d" (rc)
  66. : "d" (cmd), "d" ((void *) __pa(iob))
  67. : "0", "1", "cc");
  68. return rc;
  69. }
  70. static __inline__ int
  71. mdsk_init_io(struct dasd_device * device, int blocksize, int offset, int size)
  72. {
  73. struct dasd_diag_private *private;
  74. struct dasd_diag_init_io *iib;
  75. int rc;
  76. private = (struct dasd_diag_private *) device->private;
  77. iib = &private->iib;
  78. memset(iib, 0, sizeof (struct dasd_diag_init_io));
  79. iib->dev_nr = _ccw_device_get_device_number(device->cdev);
  80. iib->block_size = blocksize;
  81. iib->offset = offset;
  82. iib->start_block = 0;
  83. iib->end_block = size;
  84. rc = dia250(iib, INIT_BIO);
  85. return rc & 3;
  86. }
  87. static __inline__ int
  88. mdsk_term_io(struct dasd_device * device)
  89. {
  90. struct dasd_diag_private *private;
  91. struct dasd_diag_init_io *iib;
  92. int rc;
  93. private = (struct dasd_diag_private *) device->private;
  94. iib = &private->iib;
  95. memset(iib, 0, sizeof (struct dasd_diag_init_io));
  96. iib->dev_nr = _ccw_device_get_device_number(device->cdev);
  97. rc = dia250(iib, TERM_BIO);
  98. return rc & 3;
  99. }
  100. static int
  101. dasd_start_diag(struct dasd_ccw_req * cqr)
  102. {
  103. struct dasd_device *device;
  104. struct dasd_diag_private *private;
  105. struct dasd_diag_req *dreq;
  106. int rc;
  107. device = cqr->device;
  108. private = (struct dasd_diag_private *) device->private;
  109. dreq = (struct dasd_diag_req *) cqr->data;
  110. private->iob.dev_nr = _ccw_device_get_device_number(device->cdev);
  111. private->iob.key = 0;
  112. private->iob.flags = 2; /* do asynchronous io */
  113. private->iob.block_count = dreq->block_count;
  114. private->iob.interrupt_params = (u32)(addr_t) cqr;
  115. private->iob.bio_list = __pa(dreq->bio);
  116. cqr->startclk = get_clock();
  117. rc = dia250(&private->iob, RW_BIO);
  118. if (rc > 8) {
  119. DEV_MESSAGE(KERN_WARNING, device, "dia250 returned CC %d", rc);
  120. cqr->status = DASD_CQR_ERROR;
  121. } else if (rc == 0) {
  122. cqr->status = DASD_CQR_DONE;
  123. dasd_schedule_bh(device);
  124. } else {
  125. cqr->status = DASD_CQR_IN_IO;
  126. rc = 0;
  127. }
  128. return rc;
  129. }
  130. static void
  131. dasd_ext_handler(struct pt_regs *regs, __u16 code)
  132. {
  133. struct dasd_ccw_req *cqr, *next;
  134. struct dasd_device *device;
  135. unsigned long long expires;
  136. unsigned long flags;
  137. char status;
  138. int ip;
  139. /*
  140. * Get the external interruption subcode. VM stores
  141. * this in the 'cpu address' field associated with
  142. * the external interrupt. For diag 250 the subcode
  143. * needs to be 3.
  144. */
  145. if ((S390_lowcore.cpu_addr & 0xff00) != 0x0300)
  146. return;
  147. status = *((char *) &S390_lowcore.ext_params + 5);
  148. ip = S390_lowcore.ext_params;
  149. if (!ip) { /* no intparm: unsolicited interrupt */
  150. MESSAGE(KERN_DEBUG, "%s", "caught unsolicited interrupt");
  151. return;
  152. }
  153. cqr = (struct dasd_ccw_req *)(addr_t) ip;
  154. device = (struct dasd_device *) cqr->device;
  155. if (strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  156. DEV_MESSAGE(KERN_WARNING, device,
  157. " magic number of dasd_ccw_req 0x%08X doesn't"
  158. " match discipline 0x%08X",
  159. cqr->magic, *(int *) (&device->discipline->name));
  160. return;
  161. }
  162. /* get irq lock to modify request queue */
  163. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  164. cqr->stopclk = get_clock();
  165. expires = 0;
  166. if (status == 0) {
  167. cqr->status = DASD_CQR_DONE;
  168. /* Start first request on queue if possible -> fast_io. */
  169. if (!list_empty(&device->ccw_queue)) {
  170. next = list_entry(device->ccw_queue.next,
  171. struct dasd_ccw_req, list);
  172. if (next->status == DASD_CQR_QUEUED) {
  173. if (dasd_start_diag(next) == 0)
  174. expires = next->expires;
  175. else
  176. DEV_MESSAGE(KERN_WARNING, device, "%s",
  177. "Interrupt fastpath "
  178. "failed!");
  179. }
  180. }
  181. } else
  182. cqr->status = DASD_CQR_FAILED;
  183. if (expires != 0)
  184. dasd_set_timer(device, expires);
  185. else
  186. dasd_clear_timer(device);
  187. dasd_schedule_bh(device);
  188. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  189. }
  190. static int
  191. dasd_diag_check_device(struct dasd_device *device)
  192. {
  193. struct dasd_diag_private *private;
  194. struct dasd_diag_characteristics *rdc_data;
  195. struct dasd_diag_bio bio;
  196. long *label;
  197. int sb, bsize;
  198. int rc;
  199. private = (struct dasd_diag_private *) device->private;
  200. if (private == NULL) {
  201. private = kmalloc(sizeof(struct dasd_diag_private),GFP_KERNEL);
  202. if (private == NULL) {
  203. DEV_MESSAGE(KERN_WARNING, device, "%s",
  204. "memory allocation failed for private data");
  205. return -ENOMEM;
  206. }
  207. device->private = (void *) private;
  208. }
  209. /* Read Device Characteristics */
  210. rdc_data = (void *) &(private->rdc_data);
  211. rdc_data->dev_nr = _ccw_device_get_device_number(device->cdev);
  212. rdc_data->rdc_len = sizeof (struct dasd_diag_characteristics);
  213. rc = diag210((struct diag210 *) rdc_data);
  214. if (rc)
  215. return -ENOTSUPP;
  216. /* Figure out position of label block */
  217. switch (private->rdc_data.vdev_class) {
  218. case DEV_CLASS_FBA:
  219. private->pt_block = 1;
  220. break;
  221. case DEV_CLASS_ECKD:
  222. private->pt_block = 2;
  223. break;
  224. default:
  225. return -ENOTSUPP;
  226. }
  227. DBF_DEV_EVENT(DBF_INFO, device,
  228. "%04X: %04X on real %04X/%02X",
  229. rdc_data->dev_nr,
  230. rdc_data->vdev_type,
  231. rdc_data->rdev_type, rdc_data->rdev_model);
  232. /* terminate all outstanding operations */
  233. mdsk_term_io(device);
  234. /* figure out blocksize of device */
  235. label = (long *) get_zeroed_page(GFP_KERNEL);
  236. if (label == NULL) {
  237. DEV_MESSAGE(KERN_WARNING, device, "%s",
  238. "No memory to allocate initialization request");
  239. return -ENOMEM;
  240. }
  241. /* try all sizes - needed for ECKD devices */
  242. for (bsize = 512; bsize <= PAGE_SIZE; bsize <<= 1) {
  243. mdsk_init_io(device, bsize, 0, 64);
  244. memset(&bio, 0, sizeof (struct dasd_diag_bio));
  245. bio.type = MDSK_READ_REQ;
  246. bio.block_number = private->pt_block + 1;
  247. bio.buffer = __pa(label);
  248. memset(&private->iob, 0, sizeof (struct dasd_diag_rw_io));
  249. private->iob.dev_nr = rdc_data->dev_nr;
  250. private->iob.key = 0;
  251. private->iob.flags = 0; /* do synchronous io */
  252. private->iob.block_count = 1;
  253. private->iob.interrupt_params = 0;
  254. private->iob.bio_list = __pa(&bio);
  255. if (dia250(&private->iob, RW_BIO) == 0)
  256. break;
  257. mdsk_term_io(device);
  258. }
  259. if (bsize <= PAGE_SIZE && label[0] == 0xc3d4e2f1) {
  260. /* get formatted blocksize from label block */
  261. bsize = (int) label[3];
  262. device->blocks = label[7];
  263. device->bp_block = bsize;
  264. device->s2b_shift = 0; /* bits to shift 512 to get a block */
  265. for (sb = 512; sb < bsize; sb = sb << 1)
  266. device->s2b_shift++;
  267. DEV_MESSAGE(KERN_INFO, device,
  268. "capacity (%dkB blks): %ldkB",
  269. (device->bp_block >> 10),
  270. (device->blocks << device->s2b_shift) >> 1);
  271. rc = 0;
  272. } else {
  273. if (bsize > PAGE_SIZE)
  274. DEV_MESSAGE(KERN_WARNING, device, "%s",
  275. "DIAG access failed");
  276. else
  277. DEV_MESSAGE(KERN_WARNING, device, "%s",
  278. "volume is not CMS formatted");
  279. rc = -EMEDIUMTYPE;
  280. }
  281. free_page((long) label);
  282. return rc;
  283. }
  284. static int
  285. dasd_diag_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
  286. {
  287. if (dasd_check_blocksize(device->bp_block) != 0)
  288. return -EINVAL;
  289. geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
  290. geo->heads = 16;
  291. geo->sectors = 128 >> device->s2b_shift;
  292. return 0;
  293. }
  294. static dasd_era_t
  295. dasd_diag_examine_error(struct dasd_ccw_req * cqr, struct irb * stat)
  296. {
  297. return dasd_era_fatal;
  298. }
  299. static dasd_erp_fn_t
  300. dasd_diag_erp_action(struct dasd_ccw_req * cqr)
  301. {
  302. return dasd_default_erp_action;
  303. }
  304. static dasd_erp_fn_t
  305. dasd_diag_erp_postaction(struct dasd_ccw_req * cqr)
  306. {
  307. return dasd_default_erp_postaction;
  308. }
  309. static struct dasd_ccw_req *
  310. dasd_diag_build_cp(struct dasd_device * device, struct request *req)
  311. {
  312. struct dasd_ccw_req *cqr;
  313. struct dasd_diag_req *dreq;
  314. struct dasd_diag_bio *dbio;
  315. struct bio *bio;
  316. struct bio_vec *bv;
  317. char *dst;
  318. int count, datasize;
  319. sector_t recid, first_rec, last_rec;
  320. unsigned blksize, off;
  321. unsigned char rw_cmd;
  322. int i;
  323. if (rq_data_dir(req) == READ)
  324. rw_cmd = MDSK_READ_REQ;
  325. else if (rq_data_dir(req) == WRITE)
  326. rw_cmd = MDSK_WRITE_REQ;
  327. else
  328. return ERR_PTR(-EINVAL);
  329. blksize = device->bp_block;
  330. /* Calculate record id of first and last block. */
  331. first_rec = req->sector >> device->s2b_shift;
  332. last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
  333. /* Check struct bio and count the number of blocks for the request. */
  334. count = 0;
  335. rq_for_each_bio(bio, req) {
  336. bio_for_each_segment(bv, bio, i) {
  337. if (bv->bv_len & (blksize - 1))
  338. /* Fba can only do full blocks. */
  339. return ERR_PTR(-EINVAL);
  340. count += bv->bv_len >> (device->s2b_shift + 9);
  341. }
  342. }
  343. /* Paranoia. */
  344. if (count != last_rec - first_rec + 1)
  345. return ERR_PTR(-EINVAL);
  346. /* Build the request */
  347. datasize = sizeof(struct dasd_diag_req) +
  348. count*sizeof(struct dasd_diag_bio);
  349. cqr = dasd_smalloc_request(dasd_diag_discipline.name, 0,
  350. datasize, device);
  351. if (IS_ERR(cqr))
  352. return cqr;
  353. dreq = (struct dasd_diag_req *) cqr->data;
  354. dreq->block_count = count;
  355. dbio = dreq->bio;
  356. recid = first_rec;
  357. rq_for_each_bio(bio, req) {
  358. bio_for_each_segment(bv, bio, i) {
  359. dst = page_address(bv->bv_page) + bv->bv_offset;
  360. for (off = 0; off < bv->bv_len; off += blksize) {
  361. memset(dbio, 0, sizeof (struct dasd_diag_bio));
  362. dbio->type = rw_cmd;
  363. dbio->block_number = recid + 1;
  364. dbio->buffer = __pa(dst);
  365. dbio++;
  366. dst += blksize;
  367. recid++;
  368. }
  369. }
  370. }
  371. cqr->buildclk = get_clock();
  372. cqr->device = device;
  373. cqr->expires = 50 * HZ; /* 50 seconds */
  374. cqr->status = DASD_CQR_FILLED;
  375. return cqr;
  376. }
  377. static int
  378. dasd_diag_free_cp(struct dasd_ccw_req *cqr, struct request *req)
  379. {
  380. int status;
  381. status = cqr->status == DASD_CQR_DONE;
  382. dasd_sfree_request(cqr, cqr->device);
  383. return status;
  384. }
  385. static int
  386. dasd_diag_fill_info(struct dasd_device * device,
  387. struct dasd_information2_t * info)
  388. {
  389. struct dasd_diag_private *private;
  390. private = (struct dasd_diag_private *) device->private;
  391. info->label_block = private->pt_block;
  392. info->FBA_layout = 1;
  393. info->format = DASD_FORMAT_LDL;
  394. info->characteristics_size = sizeof (struct dasd_diag_characteristics);
  395. memcpy(info->characteristics,
  396. &((struct dasd_diag_private *) device->private)->rdc_data,
  397. sizeof (struct dasd_diag_characteristics));
  398. info->confdata_size = 0;
  399. return 0;
  400. }
  401. static void
  402. dasd_diag_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
  403. struct irb *stat)
  404. {
  405. DEV_MESSAGE(KERN_ERR, device, "%s",
  406. "dump sense not available for DIAG data");
  407. }
  408. /*
  409. * max_blocks is dependent on the amount of storage that is available
  410. * in the static io buffer for each device. Currently each device has
  411. * 8192 bytes (=2 pages). dasd diag is only relevant for 31 bit.
  412. * The struct dasd_ccw_req has 96 bytes, the struct dasd_diag_req has
  413. * 8 bytes and the struct dasd_diag_bio for each block has 16 bytes.
  414. * That makes:
  415. * (8192 - 96 - 8) / 16 = 505.5 blocks at maximum.
  416. * We want to fit two into the available memory so that we can immediately
  417. * start the next request if one finishes off. That makes 252.75 blocks
  418. * for one request. Give a little safety and the result is 240.
  419. */
  420. struct dasd_discipline dasd_diag_discipline = {
  421. .owner = THIS_MODULE,
  422. .name = "DIAG",
  423. .ebcname = "DIAG",
  424. .max_blocks = 240,
  425. .check_device = dasd_diag_check_device,
  426. .fill_geometry = dasd_diag_fill_geometry,
  427. .start_IO = dasd_start_diag,
  428. .examine_error = dasd_diag_examine_error,
  429. .erp_action = dasd_diag_erp_action,
  430. .erp_postaction = dasd_diag_erp_postaction,
  431. .build_cp = dasd_diag_build_cp,
  432. .free_cp = dasd_diag_free_cp,
  433. .dump_sense = dasd_diag_dump_sense,
  434. .fill_info = dasd_diag_fill_info,
  435. };
  436. static int __init
  437. dasd_diag_init(void)
  438. {
  439. if (!MACHINE_IS_VM) {
  440. MESSAGE_LOG(KERN_INFO,
  441. "Machine is not VM: %s "
  442. "discipline not initializing",
  443. dasd_diag_discipline.name);
  444. return -EINVAL;
  445. }
  446. ASCEBC(dasd_diag_discipline.ebcname, 4);
  447. ctl_set_bit(0, 9);
  448. register_external_interrupt(0x2603, dasd_ext_handler);
  449. dasd_diag_discipline_pointer = &dasd_diag_discipline;
  450. return 0;
  451. }
  452. static void __exit
  453. dasd_diag_cleanup(void)
  454. {
  455. if (!MACHINE_IS_VM) {
  456. MESSAGE_LOG(KERN_INFO,
  457. "Machine is not VM: %s "
  458. "discipline not cleaned",
  459. dasd_diag_discipline.name);
  460. return;
  461. }
  462. unregister_external_interrupt(0x2603, dasd_ext_handler);
  463. ctl_clear_bit(0, 9);
  464. dasd_diag_discipline_pointer = NULL;
  465. }
  466. module_init(dasd_diag_init);
  467. module_exit(dasd_diag_cleanup);
  468. /*
  469. * Overrides for Emacs so that we follow Linus's tabbing style.
  470. * Emacs will notice this stuff at the end of the file and automatically
  471. * adjust the settings for this buffer only. This must remain at the end
  472. * of the file.
  473. * ---------------------------------------------------------------------------
  474. * Local variables:
  475. * c-indent-level: 4
  476. * c-brace-imaginary-offset: 0
  477. * c-brace-offset: -4
  478. * c-argdecl-indent: 4
  479. * c-label-offset: -4
  480. * c-continued-statement-offset: 4
  481. * c-continued-brace-offset: 0
  482. * indent-tabs-mode: 1
  483. * tab-width: 8
  484. * End:
  485. */