hd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * This is the low-level hd interrupt support. It traverses the
  5. * request-list, using interrupts to jump between functions. As
  6. * all the functions are called within interrupts, we may not
  7. * sleep. Special care is recommended.
  8. *
  9. * modified by Drew Eckhardt to check nr of hd's from the CMOS.
  10. *
  11. * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  12. * in the early extended-partition checks and added DM partitions
  13. *
  14. * IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  15. * and general streamlining by Mark Lord.
  16. *
  17. * Removed 99% of above. Use Mark's ide driver for those options.
  18. * This is now a lightweight ST-506 driver. (Paul Gortmaker)
  19. *
  20. * Modified 1995 Russell King for ARM processor.
  21. *
  22. * Bugfix: max_sectors must be <= 255 or the wheels tend to come
  23. * off in a hurry once you queue things up - Paul G. 02/2001
  24. */
  25. /* Uncomment the following if you want verbose error reports. */
  26. /* #define VERBOSE_ERRORS */
  27. #include <linux/blkdev.h>
  28. #include <linux/errno.h>
  29. #include <linux/signal.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/timer.h>
  32. #include <linux/fs.h>
  33. #include <linux/kernel.h>
  34. #include <linux/genhd.h>
  35. #include <linux/slab.h>
  36. #include <linux/string.h>
  37. #include <linux/ioport.h>
  38. #include <linux/init.h>
  39. #include <linux/blkpg.h>
  40. #include <linux/ata.h>
  41. #include <linux/hdreg.h>
  42. #define HD_IRQ 14
  43. #define REALLY_SLOW_IO
  44. #include <asm/system.h>
  45. #include <asm/io.h>
  46. #include <asm/uaccess.h>
  47. #ifdef __arm__
  48. #undef HD_IRQ
  49. #endif
  50. #include <asm/irq.h>
  51. #ifdef __arm__
  52. #define HD_IRQ IRQ_HARDDISK
  53. #endif
  54. /* Hd controller regster ports */
  55. #define HD_DATA 0x1f0 /* _CTL when writing */
  56. #define HD_ERROR 0x1f1 /* see err-bits */
  57. #define HD_NSECTOR 0x1f2 /* nr of sectors to read/write */
  58. #define HD_SECTOR 0x1f3 /* starting sector */
  59. #define HD_LCYL 0x1f4 /* starting cylinder */
  60. #define HD_HCYL 0x1f5 /* high byte of starting cyl */
  61. #define HD_CURRENT 0x1f6 /* 101dhhhh , d=drive, hhhh=head */
  62. #define HD_STATUS 0x1f7 /* see status-bits */
  63. #define HD_FEATURE HD_ERROR /* same io address, read=error, write=feature */
  64. #define HD_PRECOMP HD_FEATURE /* obsolete use of this port - predates IDE */
  65. #define HD_COMMAND HD_STATUS /* same io address, read=status, write=cmd */
  66. #define HD_CMD 0x3f6 /* used for resets */
  67. #define HD_ALTSTATUS 0x3f6 /* same as HD_STATUS but doesn't clear irq */
  68. /* Bits of HD_STATUS */
  69. #define ERR_STAT 0x01
  70. #define INDEX_STAT 0x02
  71. #define ECC_STAT 0x04 /* Corrected error */
  72. #define DRQ_STAT 0x08
  73. #define SEEK_STAT 0x10
  74. #define SERVICE_STAT SEEK_STAT
  75. #define WRERR_STAT 0x20
  76. #define READY_STAT 0x40
  77. #define BUSY_STAT 0x80
  78. /* Bits for HD_ERROR */
  79. #define MARK_ERR 0x01 /* Bad address mark */
  80. #define TRK0_ERR 0x02 /* couldn't find track 0 */
  81. #define ABRT_ERR 0x04 /* Command aborted */
  82. #define MCR_ERR 0x08 /* media change request */
  83. #define ID_ERR 0x10 /* ID field not found */
  84. #define MC_ERR 0x20 /* media changed */
  85. #define ECC_ERR 0x40 /* Uncorrectable ECC error */
  86. #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
  87. #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
  88. static DEFINE_SPINLOCK(hd_lock);
  89. static struct request_queue *hd_queue;
  90. static struct request *hd_req;
  91. #define TIMEOUT_VALUE (6*HZ)
  92. #define HD_DELAY 0
  93. #define MAX_ERRORS 16 /* Max read/write errors/sector */
  94. #define RESET_FREQ 8 /* Reset controller every 8th retry */
  95. #define RECAL_FREQ 4 /* Recalibrate every 4th retry */
  96. #define MAX_HD 2
  97. #define STAT_OK (READY_STAT|SEEK_STAT)
  98. #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
  99. static void recal_intr(void);
  100. static void bad_rw_intr(void);
  101. static int reset;
  102. static int hd_error;
  103. /*
  104. * This struct defines the HD's and their types.
  105. */
  106. struct hd_i_struct {
  107. unsigned int head, sect, cyl, wpcom, lzone, ctl;
  108. int unit;
  109. int recalibrate;
  110. int special_op;
  111. };
  112. #ifdef HD_TYPE
  113. static struct hd_i_struct hd_info[] = { HD_TYPE };
  114. static int NR_HD = ARRAY_SIZE(hd_info);
  115. #else
  116. static struct hd_i_struct hd_info[MAX_HD];
  117. static int NR_HD;
  118. #endif
  119. static struct gendisk *hd_gendisk[MAX_HD];
  120. static struct timer_list device_timer;
  121. #define TIMEOUT_VALUE (6*HZ)
  122. #define SET_TIMER \
  123. do { \
  124. mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
  125. } while (0)
  126. static void (*do_hd)(void) = NULL;
  127. #define SET_HANDLER(x) \
  128. if ((do_hd = (x)) != NULL) \
  129. SET_TIMER; \
  130. else \
  131. del_timer(&device_timer);
  132. #if (HD_DELAY > 0)
  133. #include <asm/i8253.h>
  134. unsigned long last_req;
  135. unsigned long read_timer(void)
  136. {
  137. unsigned long t, flags;
  138. int i;
  139. spin_lock_irqsave(&i8253_lock, flags);
  140. t = jiffies * 11932;
  141. outb_p(0, 0x43);
  142. i = inb_p(0x40);
  143. i |= inb(0x40) << 8;
  144. spin_unlock_irqrestore(&i8253_lock, flags);
  145. return(t - i);
  146. }
  147. #endif
  148. static void __init hd_setup(char *str, int *ints)
  149. {
  150. int hdind = 0;
  151. if (ints[0] != 3)
  152. return;
  153. if (hd_info[0].head != 0)
  154. hdind = 1;
  155. hd_info[hdind].head = ints[2];
  156. hd_info[hdind].sect = ints[3];
  157. hd_info[hdind].cyl = ints[1];
  158. hd_info[hdind].wpcom = 0;
  159. hd_info[hdind].lzone = ints[1];
  160. hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
  161. NR_HD = hdind+1;
  162. }
  163. static bool hd_end_request(int err, unsigned int bytes)
  164. {
  165. if (__blk_end_request(hd_req, err, bytes))
  166. return true;
  167. hd_req = NULL;
  168. return false;
  169. }
  170. static bool hd_end_request_cur(int err)
  171. {
  172. return hd_end_request(err, blk_rq_cur_bytes(hd_req));
  173. }
  174. static void dump_status(const char *msg, unsigned int stat)
  175. {
  176. char *name = "hd?";
  177. if (hd_req)
  178. name = hd_req->rq_disk->disk_name;
  179. #ifdef VERBOSE_ERRORS
  180. printk("%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
  181. if (stat & BUSY_STAT) printk("Busy ");
  182. if (stat & READY_STAT) printk("DriveReady ");
  183. if (stat & WRERR_STAT) printk("WriteFault ");
  184. if (stat & SEEK_STAT) printk("SeekComplete ");
  185. if (stat & DRQ_STAT) printk("DataRequest ");
  186. if (stat & ECC_STAT) printk("CorrectedError ");
  187. if (stat & INDEX_STAT) printk("Index ");
  188. if (stat & ERR_STAT) printk("Error ");
  189. printk("}\n");
  190. if ((stat & ERR_STAT) == 0) {
  191. hd_error = 0;
  192. } else {
  193. hd_error = inb(HD_ERROR);
  194. printk("%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff);
  195. if (hd_error & BBD_ERR) printk("BadSector ");
  196. if (hd_error & ECC_ERR) printk("UncorrectableError ");
  197. if (hd_error & ID_ERR) printk("SectorIdNotFound ");
  198. if (hd_error & ABRT_ERR) printk("DriveStatusError ");
  199. if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
  200. if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
  201. printk("}");
  202. if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
  203. printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
  204. inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
  205. if (hd_req)
  206. printk(", sector=%ld", blk_rq_pos(hd_req));
  207. }
  208. printk("\n");
  209. }
  210. #else
  211. printk("%s: %s: status=0x%02x.\n", name, msg, stat & 0xff);
  212. if ((stat & ERR_STAT) == 0) {
  213. hd_error = 0;
  214. } else {
  215. hd_error = inb(HD_ERROR);
  216. printk("%s: %s: error=0x%02x.\n", name, msg, hd_error & 0xff);
  217. }
  218. #endif
  219. }
  220. static void check_status(void)
  221. {
  222. int i = inb_p(HD_STATUS);
  223. if (!OK_STATUS(i)) {
  224. dump_status("check_status", i);
  225. bad_rw_intr();
  226. }
  227. }
  228. static int controller_busy(void)
  229. {
  230. int retries = 100000;
  231. unsigned char status;
  232. do {
  233. status = inb_p(HD_STATUS);
  234. } while ((status & BUSY_STAT) && --retries);
  235. return status;
  236. }
  237. static int status_ok(void)
  238. {
  239. unsigned char status = inb_p(HD_STATUS);
  240. if (status & BUSY_STAT)
  241. return 1; /* Ancient, but does it make sense??? */
  242. if (status & WRERR_STAT)
  243. return 0;
  244. if (!(status & READY_STAT))
  245. return 0;
  246. if (!(status & SEEK_STAT))
  247. return 0;
  248. return 1;
  249. }
  250. static int controller_ready(unsigned int drive, unsigned int head)
  251. {
  252. int retry = 100;
  253. do {
  254. if (controller_busy() & BUSY_STAT)
  255. return 0;
  256. outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
  257. if (status_ok())
  258. return 1;
  259. } while (--retry);
  260. return 0;
  261. }
  262. static void hd_out(struct hd_i_struct *disk,
  263. unsigned int nsect,
  264. unsigned int sect,
  265. unsigned int head,
  266. unsigned int cyl,
  267. unsigned int cmd,
  268. void (*intr_addr)(void))
  269. {
  270. unsigned short port;
  271. #if (HD_DELAY > 0)
  272. while (read_timer() - last_req < HD_DELAY)
  273. /* nothing */;
  274. #endif
  275. if (reset)
  276. return;
  277. if (!controller_ready(disk->unit, head)) {
  278. reset = 1;
  279. return;
  280. }
  281. SET_HANDLER(intr_addr);
  282. outb_p(disk->ctl, HD_CMD);
  283. port = HD_DATA;
  284. outb_p(disk->wpcom >> 2, ++port);
  285. outb_p(nsect, ++port);
  286. outb_p(sect, ++port);
  287. outb_p(cyl, ++port);
  288. outb_p(cyl >> 8, ++port);
  289. outb_p(0xA0 | (disk->unit << 4) | head, ++port);
  290. outb_p(cmd, ++port);
  291. }
  292. static void hd_request (void);
  293. static int drive_busy(void)
  294. {
  295. unsigned int i;
  296. unsigned char c;
  297. for (i = 0; i < 500000 ; i++) {
  298. c = inb_p(HD_STATUS);
  299. if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
  300. return 0;
  301. }
  302. dump_status("reset timed out", c);
  303. return 1;
  304. }
  305. static void reset_controller(void)
  306. {
  307. int i;
  308. outb_p(4, HD_CMD);
  309. for (i = 0; i < 1000; i++) barrier();
  310. outb_p(hd_info[0].ctl & 0x0f, HD_CMD);
  311. for (i = 0; i < 1000; i++) barrier();
  312. if (drive_busy())
  313. printk("hd: controller still busy\n");
  314. else if ((hd_error = inb(HD_ERROR)) != 1)
  315. printk("hd: controller reset failed: %02x\n", hd_error);
  316. }
  317. static void reset_hd(void)
  318. {
  319. static int i;
  320. repeat:
  321. if (reset) {
  322. reset = 0;
  323. i = -1;
  324. reset_controller();
  325. } else {
  326. check_status();
  327. if (reset)
  328. goto repeat;
  329. }
  330. if (++i < NR_HD) {
  331. struct hd_i_struct *disk = &hd_info[i];
  332. disk->special_op = disk->recalibrate = 1;
  333. hd_out(disk, disk->sect, disk->sect, disk->head-1,
  334. disk->cyl, ATA_CMD_INIT_DEV_PARAMS, &reset_hd);
  335. if (reset)
  336. goto repeat;
  337. } else
  338. hd_request();
  339. }
  340. /*
  341. * Ok, don't know what to do with the unexpected interrupts: on some machines
  342. * doing a reset and a retry seems to result in an eternal loop. Right now I
  343. * ignore it, and just set the timeout.
  344. *
  345. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
  346. * drive enters "idle", "standby", or "sleep" mode, so if the status looks
  347. * "good", we just ignore the interrupt completely.
  348. */
  349. static void unexpected_hd_interrupt(void)
  350. {
  351. unsigned int stat = inb_p(HD_STATUS);
  352. if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
  353. dump_status("unexpected interrupt", stat);
  354. SET_TIMER;
  355. }
  356. }
  357. /*
  358. * bad_rw_intr() now tries to be a bit smarter and does things
  359. * according to the error returned by the controller.
  360. * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
  361. */
  362. static void bad_rw_intr(void)
  363. {
  364. struct request *req = hd_req;
  365. if (req != NULL) {
  366. struct hd_i_struct *disk = req->rq_disk->private_data;
  367. if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
  368. hd_end_request_cur(-EIO);
  369. disk->special_op = disk->recalibrate = 1;
  370. } else if (req->errors % RESET_FREQ == 0)
  371. reset = 1;
  372. else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0)
  373. disk->special_op = disk->recalibrate = 1;
  374. /* Otherwise just retry */
  375. }
  376. }
  377. static inline int wait_DRQ(void)
  378. {
  379. int retries;
  380. int stat;
  381. for (retries = 0; retries < 100000; retries++) {
  382. stat = inb_p(HD_STATUS);
  383. if (stat & DRQ_STAT)
  384. return 0;
  385. }
  386. dump_status("wait_DRQ", stat);
  387. return -1;
  388. }
  389. static void read_intr(void)
  390. {
  391. struct request *req;
  392. int i, retries = 100000;
  393. do {
  394. i = (unsigned) inb_p(HD_STATUS);
  395. if (i & BUSY_STAT)
  396. continue;
  397. if (!OK_STATUS(i))
  398. break;
  399. if (i & DRQ_STAT)
  400. goto ok_to_read;
  401. } while (--retries > 0);
  402. dump_status("read_intr", i);
  403. bad_rw_intr();
  404. hd_request();
  405. return;
  406. ok_to_read:
  407. req = hd_req;
  408. insw(HD_DATA, req->buffer, 256);
  409. #ifdef DEBUG
  410. printk("%s: read: sector %ld, remaining = %u, buffer=%p\n",
  411. req->rq_disk->disk_name, blk_rq_pos(req) + 1,
  412. blk_rq_sectors(req) - 1, req->buffer+512);
  413. #endif
  414. if (hd_end_request(0, 512)) {
  415. SET_HANDLER(&read_intr);
  416. return;
  417. }
  418. (void) inb_p(HD_STATUS);
  419. #if (HD_DELAY > 0)
  420. last_req = read_timer();
  421. #endif
  422. hd_request();
  423. }
  424. static void write_intr(void)
  425. {
  426. struct request *req = hd_req;
  427. int i;
  428. int retries = 100000;
  429. do {
  430. i = (unsigned) inb_p(HD_STATUS);
  431. if (i & BUSY_STAT)
  432. continue;
  433. if (!OK_STATUS(i))
  434. break;
  435. if ((blk_rq_sectors(req) <= 1) || (i & DRQ_STAT))
  436. goto ok_to_write;
  437. } while (--retries > 0);
  438. dump_status("write_intr", i);
  439. bad_rw_intr();
  440. hd_request();
  441. return;
  442. ok_to_write:
  443. if (hd_end_request(0, 512)) {
  444. SET_HANDLER(&write_intr);
  445. outsw(HD_DATA, req->buffer, 256);
  446. return;
  447. }
  448. #if (HD_DELAY > 0)
  449. last_req = read_timer();
  450. #endif
  451. hd_request();
  452. }
  453. static void recal_intr(void)
  454. {
  455. check_status();
  456. #if (HD_DELAY > 0)
  457. last_req = read_timer();
  458. #endif
  459. hd_request();
  460. }
  461. /*
  462. * This is another of the error-routines I don't know what to do with. The
  463. * best idea seems to just set reset, and start all over again.
  464. */
  465. static void hd_times_out(unsigned long dummy)
  466. {
  467. char *name;
  468. do_hd = NULL;
  469. if (!hd_req)
  470. return;
  471. spin_lock_irq(hd_queue->queue_lock);
  472. reset = 1;
  473. name = hd_req->rq_disk->disk_name;
  474. printk("%s: timeout\n", name);
  475. if (++hd_req->errors >= MAX_ERRORS) {
  476. #ifdef DEBUG
  477. printk("%s: too many errors\n", name);
  478. #endif
  479. hd_end_request_cur(-EIO);
  480. }
  481. hd_request();
  482. spin_unlock_irq(hd_queue->queue_lock);
  483. }
  484. static int do_special_op(struct hd_i_struct *disk, struct request *req)
  485. {
  486. if (disk->recalibrate) {
  487. disk->recalibrate = 0;
  488. hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr);
  489. return reset;
  490. }
  491. if (disk->head > 16) {
  492. printk("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
  493. hd_end_request_cur(-EIO);
  494. }
  495. disk->special_op = 0;
  496. return 1;
  497. }
  498. /*
  499. * The driver enables interrupts as much as possible. In order to do this,
  500. * (a) the device-interrupt is disabled before entering hd_request(),
  501. * and (b) the timeout-interrupt is disabled before the sti().
  502. *
  503. * Interrupts are still masked (by default) whenever we are exchanging
  504. * data/cmds with a drive, because some drives seem to have very poor
  505. * tolerance for latency during I/O. The IDE driver has support to unmask
  506. * interrupts for non-broken hardware, so use that driver if required.
  507. */
  508. static void hd_request(void)
  509. {
  510. unsigned int block, nsect, sec, track, head, cyl;
  511. struct hd_i_struct *disk;
  512. struct request *req;
  513. if (do_hd)
  514. return;
  515. repeat:
  516. del_timer(&device_timer);
  517. if (!hd_req) {
  518. hd_req = blk_fetch_request(hd_queue);
  519. if (!hd_req) {
  520. do_hd = NULL;
  521. return;
  522. }
  523. }
  524. req = hd_req;
  525. if (reset) {
  526. reset_hd();
  527. return;
  528. }
  529. disk = req->rq_disk->private_data;
  530. block = blk_rq_pos(req);
  531. nsect = blk_rq_sectors(req);
  532. if (block >= get_capacity(req->rq_disk) ||
  533. ((block+nsect) > get_capacity(req->rq_disk))) {
  534. printk("%s: bad access: block=%d, count=%d\n",
  535. req->rq_disk->disk_name, block, nsect);
  536. hd_end_request_cur(-EIO);
  537. goto repeat;
  538. }
  539. if (disk->special_op) {
  540. if (do_special_op(disk, req))
  541. goto repeat;
  542. return;
  543. }
  544. sec = block % disk->sect + 1;
  545. track = block / disk->sect;
  546. head = track % disk->head;
  547. cyl = track / disk->head;
  548. #ifdef DEBUG
  549. printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
  550. req->rq_disk->disk_name,
  551. req_data_dir(req) == READ ? "read" : "writ",
  552. cyl, head, sec, nsect, req->buffer);
  553. #endif
  554. if (blk_fs_request(req)) {
  555. switch (rq_data_dir(req)) {
  556. case READ:
  557. hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
  558. &read_intr);
  559. if (reset)
  560. goto repeat;
  561. break;
  562. case WRITE:
  563. hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE,
  564. &write_intr);
  565. if (reset)
  566. goto repeat;
  567. if (wait_DRQ()) {
  568. bad_rw_intr();
  569. goto repeat;
  570. }
  571. outsw(HD_DATA, req->buffer, 256);
  572. break;
  573. default:
  574. printk("unknown hd-command\n");
  575. hd_end_request_cur(-EIO);
  576. break;
  577. }
  578. }
  579. }
  580. static void do_hd_request(struct request_queue *q)
  581. {
  582. hd_request();
  583. }
  584. static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  585. {
  586. struct hd_i_struct *disk = bdev->bd_disk->private_data;
  587. geo->heads = disk->head;
  588. geo->sectors = disk->sect;
  589. geo->cylinders = disk->cyl;
  590. return 0;
  591. }
  592. /*
  593. * Releasing a block device means we sync() it, so that it can safely
  594. * be forgotten about...
  595. */
  596. static irqreturn_t hd_interrupt(int irq, void *dev_id)
  597. {
  598. void (*handler)(void) = do_hd;
  599. spin_lock(hd_queue->queue_lock);
  600. do_hd = NULL;
  601. del_timer(&device_timer);
  602. if (!handler)
  603. handler = unexpected_hd_interrupt;
  604. handler();
  605. spin_unlock(hd_queue->queue_lock);
  606. return IRQ_HANDLED;
  607. }
  608. static const struct block_device_operations hd_fops = {
  609. .getgeo = hd_getgeo,
  610. };
  611. /*
  612. * This is the hard disk IRQ description. The IRQF_DISABLED in sa_flags
  613. * means we run the IRQ-handler with interrupts disabled: this is bad for
  614. * interrupt latency, but anything else has led to problems on some
  615. * machines.
  616. *
  617. * We enable interrupts in some of the routines after making sure it's
  618. * safe.
  619. */
  620. static int __init hd_init(void)
  621. {
  622. int drive;
  623. if (register_blkdev(HD_MAJOR, "hd"))
  624. return -1;
  625. hd_queue = blk_init_queue(do_hd_request, &hd_lock);
  626. if (!hd_queue) {
  627. unregister_blkdev(HD_MAJOR, "hd");
  628. return -ENOMEM;
  629. }
  630. blk_queue_max_sectors(hd_queue, 255);
  631. init_timer(&device_timer);
  632. device_timer.function = hd_times_out;
  633. blk_queue_logical_block_size(hd_queue, 512);
  634. if (!NR_HD) {
  635. /*
  636. * We don't know anything about the drive. This means
  637. * that you *MUST* specify the drive parameters to the
  638. * kernel yourself.
  639. *
  640. * If we were on an i386, we used to read this info from
  641. * the BIOS or CMOS. This doesn't work all that well,
  642. * since this assumes that this is a primary or secondary
  643. * drive, and if we're using this legacy driver, it's
  644. * probably an auxilliary controller added to recover
  645. * legacy data off an ST-506 drive. Either way, it's
  646. * definitely safest to have the user explicitly specify
  647. * the information.
  648. */
  649. printk("hd: no drives specified - use hd=cyl,head,sectors"
  650. " on kernel command line\n");
  651. goto out;
  652. }
  653. for (drive = 0 ; drive < NR_HD ; drive++) {
  654. struct gendisk *disk = alloc_disk(64);
  655. struct hd_i_struct *p = &hd_info[drive];
  656. if (!disk)
  657. goto Enomem;
  658. disk->major = HD_MAJOR;
  659. disk->first_minor = drive << 6;
  660. disk->fops = &hd_fops;
  661. sprintf(disk->disk_name, "hd%c", 'a'+drive);
  662. disk->private_data = p;
  663. set_capacity(disk, p->head * p->sect * p->cyl);
  664. disk->queue = hd_queue;
  665. p->unit = drive;
  666. hd_gendisk[drive] = disk;
  667. printk("%s: %luMB, CHS=%d/%d/%d\n",
  668. disk->disk_name, (unsigned long)get_capacity(disk)/2048,
  669. p->cyl, p->head, p->sect);
  670. }
  671. if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) {
  672. printk("hd: unable to get IRQ%d for the hard disk driver\n",
  673. HD_IRQ);
  674. goto out1;
  675. }
  676. if (!request_region(HD_DATA, 8, "hd")) {
  677. printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
  678. goto out2;
  679. }
  680. if (!request_region(HD_CMD, 1, "hd(cmd)")) {
  681. printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
  682. goto out3;
  683. }
  684. /* Let them fly */
  685. for (drive = 0; drive < NR_HD; drive++)
  686. add_disk(hd_gendisk[drive]);
  687. return 0;
  688. out3:
  689. release_region(HD_DATA, 8);
  690. out2:
  691. free_irq(HD_IRQ, NULL);
  692. out1:
  693. for (drive = 0; drive < NR_HD; drive++)
  694. put_disk(hd_gendisk[drive]);
  695. NR_HD = 0;
  696. out:
  697. del_timer(&device_timer);
  698. unregister_blkdev(HD_MAJOR, "hd");
  699. blk_cleanup_queue(hd_queue);
  700. return -1;
  701. Enomem:
  702. while (drive--)
  703. put_disk(hd_gendisk[drive]);
  704. goto out;
  705. }
  706. static int __init parse_hd_setup(char *line)
  707. {
  708. int ints[6];
  709. (void) get_options(line, ARRAY_SIZE(ints), ints);
  710. hd_setup(NULL, ints);
  711. return 1;
  712. }
  713. __setup("hd=", parse_hd_setup);
  714. late_initcall(hd_init);