hd.c 19 KB

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