hd.c 19 KB

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