hd.c 19 KB

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