hd.c 20 KB

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