hd.c 19 KB

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