hd.c 19 KB

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