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/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. end_request(req, 0);
  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. req->sector++;
  401. req->buffer += 512;
  402. req->errors = 0;
  403. i = --req->nr_sectors;
  404. --req->current_nr_sectors;
  405. #ifdef DEBUG
  406. printk("%s: read: sector %ld, remaining = %ld, buffer=%p\n",
  407. req->rq_disk->disk_name, req->sector, req->nr_sectors,
  408. req->buffer+512);
  409. #endif
  410. if (req->current_nr_sectors <= 0)
  411. end_request(req, 1);
  412. if (i > 0) {
  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. if (elv_next_request(QUEUE))
  421. hd_request();
  422. return;
  423. }
  424. static void write_intr(void)
  425. {
  426. struct request *req = CURRENT;
  427. int i;
  428. int retries = 100000;
  429. do {
  430. i = (unsigned) inb_p(HD_STATUS);
  431. if (i & BUSY_STAT)
  432. continue;
  433. if (!OK_STATUS(i))
  434. break;
  435. if ((req->nr_sectors <= 1) || (i & DRQ_STAT))
  436. goto ok_to_write;
  437. } while (--retries > 0);
  438. dump_status("write_intr", i);
  439. bad_rw_intr();
  440. hd_request();
  441. return;
  442. ok_to_write:
  443. req->sector++;
  444. i = --req->nr_sectors;
  445. --req->current_nr_sectors;
  446. req->buffer += 512;
  447. if (!i || (req->bio && req->current_nr_sectors <= 0))
  448. end_request(req, 1);
  449. if (i > 0) {
  450. SET_HANDLER(&write_intr);
  451. outsw(HD_DATA, req->buffer, 256);
  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. spin_lock_irq(hd_queue->queue_lock);
  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. hd_request();
  489. spin_unlock_irq(hd_queue->queue_lock);
  490. }
  491. static int do_special_op(struct hd_i_struct *disk, struct request *req)
  492. {
  493. if (disk->recalibrate) {
  494. disk->recalibrate = 0;
  495. hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr);
  496. return reset;
  497. }
  498. if (disk->head > 16) {
  499. printk("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
  500. end_request(req, 0);
  501. }
  502. disk->special_op = 0;
  503. return 1;
  504. }
  505. /*
  506. * The driver enables interrupts as much as possible. In order to do this,
  507. * (a) the device-interrupt is disabled before entering hd_request(),
  508. * and (b) the timeout-interrupt is disabled before the sti().
  509. *
  510. * Interrupts are still masked (by default) whenever we are exchanging
  511. * data/cmds with a drive, because some drives seem to have very poor
  512. * tolerance for latency during I/O. The IDE driver has support to unmask
  513. * interrupts for non-broken hardware, so use that driver if required.
  514. */
  515. static void hd_request(void)
  516. {
  517. unsigned int block, nsect, sec, track, head, cyl;
  518. struct hd_i_struct *disk;
  519. struct request *req;
  520. if (do_hd)
  521. return;
  522. repeat:
  523. del_timer(&device_timer);
  524. req = CURRENT;
  525. if (!req) {
  526. do_hd = NULL;
  527. return;
  528. }
  529. if (reset) {
  530. reset_hd();
  531. return;
  532. }
  533. disk = req->rq_disk->private_data;
  534. block = req->sector;
  535. nsect = req->nr_sectors;
  536. if (block >= get_capacity(req->rq_disk) ||
  537. ((block+nsect) > get_capacity(req->rq_disk))) {
  538. printk("%s: bad access: block=%d, count=%d\n",
  539. req->rq_disk->disk_name, block, nsect);
  540. end_request(req, 0);
  541. goto repeat;
  542. }
  543. if (disk->special_op) {
  544. if (do_special_op(disk, req))
  545. goto repeat;
  546. return;
  547. }
  548. sec = block % disk->sect + 1;
  549. track = block / disk->sect;
  550. head = track % disk->head;
  551. cyl = track / disk->head;
  552. #ifdef DEBUG
  553. printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
  554. req->rq_disk->disk_name,
  555. req_data_dir(req) == READ ? "read" : "writ",
  556. cyl, head, sec, nsect, req->buffer);
  557. #endif
  558. if (blk_fs_request(req)) {
  559. switch (rq_data_dir(req)) {
  560. case READ:
  561. hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
  562. &read_intr);
  563. if (reset)
  564. goto repeat;
  565. break;
  566. case WRITE:
  567. hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE,
  568. &write_intr);
  569. if (reset)
  570. goto repeat;
  571. if (wait_DRQ()) {
  572. bad_rw_intr();
  573. goto repeat;
  574. }
  575. outsw(HD_DATA, req->buffer, 256);
  576. break;
  577. default:
  578. printk("unknown hd-command\n");
  579. end_request(req, 0);
  580. break;
  581. }
  582. }
  583. }
  584. static void do_hd_request(struct request_queue *q)
  585. {
  586. hd_request();
  587. }
  588. static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  589. {
  590. struct hd_i_struct *disk = bdev->bd_disk->private_data;
  591. geo->heads = disk->head;
  592. geo->sectors = disk->sect;
  593. geo->cylinders = disk->cyl;
  594. return 0;
  595. }
  596. /*
  597. * Releasing a block device means we sync() it, so that it can safely
  598. * be forgotten about...
  599. */
  600. static irqreturn_t hd_interrupt(int irq, void *dev_id)
  601. {
  602. void (*handler)(void) = do_hd;
  603. spin_lock(hd_queue->queue_lock);
  604. do_hd = NULL;
  605. del_timer(&device_timer);
  606. if (!handler)
  607. handler = unexpected_hd_interrupt;
  608. handler();
  609. spin_unlock(hd_queue->queue_lock);
  610. return IRQ_HANDLED;
  611. }
  612. static struct block_device_operations hd_fops = {
  613. .getgeo = hd_getgeo,
  614. };
  615. /*
  616. * This is the hard disk IRQ description. The IRQF_DISABLED in sa_flags
  617. * means we run the IRQ-handler with interrupts disabled: this is bad for
  618. * interrupt latency, but anything else has led to problems on some
  619. * machines.
  620. *
  621. * We enable interrupts in some of the routines after making sure it's
  622. * safe.
  623. */
  624. static int __init hd_init(void)
  625. {
  626. int drive;
  627. if (register_blkdev(MAJOR_NR, "hd"))
  628. return -1;
  629. hd_queue = blk_init_queue(do_hd_request, &hd_lock);
  630. if (!hd_queue) {
  631. unregister_blkdev(MAJOR_NR, "hd");
  632. return -ENOMEM;
  633. }
  634. blk_queue_max_sectors(hd_queue, 255);
  635. init_timer(&device_timer);
  636. device_timer.function = hd_times_out;
  637. blk_queue_hardsect_size(hd_queue, 512);
  638. if (!NR_HD) {
  639. /*
  640. * We don't know anything about the drive. This means
  641. * that you *MUST* specify the drive parameters to the
  642. * kernel yourself.
  643. *
  644. * If we were on an i386, we used to read this info from
  645. * the BIOS or CMOS. This doesn't work all that well,
  646. * since this assumes that this is a primary or secondary
  647. * drive, and if we're using this legacy driver, it's
  648. * probably an auxilliary controller added to recover
  649. * legacy data off an ST-506 drive. Either way, it's
  650. * definitely safest to have the user explicitly specify
  651. * the information.
  652. */
  653. printk("hd: no drives specified - use hd=cyl,head,sectors"
  654. " on kernel command line\n");
  655. goto out;
  656. }
  657. for (drive = 0 ; drive < NR_HD ; drive++) {
  658. struct gendisk *disk = alloc_disk(64);
  659. struct hd_i_struct *p = &hd_info[drive];
  660. if (!disk)
  661. goto Enomem;
  662. disk->major = MAJOR_NR;
  663. disk->first_minor = drive << 6;
  664. disk->fops = &hd_fops;
  665. sprintf(disk->disk_name, "hd%c", 'a'+drive);
  666. disk->private_data = p;
  667. set_capacity(disk, p->head * p->sect * p->cyl);
  668. disk->queue = hd_queue;
  669. p->unit = drive;
  670. hd_gendisk[drive] = disk;
  671. printk("%s: %luMB, CHS=%d/%d/%d\n",
  672. disk->disk_name, (unsigned long)get_capacity(disk)/2048,
  673. p->cyl, p->head, p->sect);
  674. }
  675. if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) {
  676. printk("hd: unable to get IRQ%d for the hard disk driver\n",
  677. HD_IRQ);
  678. goto out1;
  679. }
  680. if (!request_region(HD_DATA, 8, "hd")) {
  681. printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
  682. goto out2;
  683. }
  684. if (!request_region(HD_CMD, 1, "hd(cmd)")) {
  685. printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
  686. goto out3;
  687. }
  688. /* Let them fly */
  689. for (drive = 0; drive < NR_HD; drive++)
  690. add_disk(hd_gendisk[drive]);
  691. return 0;
  692. out3:
  693. release_region(HD_DATA, 8);
  694. out2:
  695. free_irq(HD_IRQ, NULL);
  696. out1:
  697. for (drive = 0; drive < NR_HD; drive++)
  698. put_disk(hd_gendisk[drive]);
  699. NR_HD = 0;
  700. out:
  701. del_timer(&device_timer);
  702. unregister_blkdev(MAJOR_NR, "hd");
  703. blk_cleanup_queue(hd_queue);
  704. return -1;
  705. Enomem:
  706. while (drive--)
  707. put_disk(hd_gendisk[drive]);
  708. goto out;
  709. }
  710. static int __init parse_hd_setup(char *line)
  711. {
  712. int ints[6];
  713. (void) get_options(line, ARRAY_SIZE(ints), ints);
  714. hd_setup(NULL, ints);
  715. return 1;
  716. }
  717. __setup("hd=", parse_hd_setup);
  718. late_initcall(hd_init);