hd.c 19 KB

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