ide-probe.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. /*
  2. * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
  3. * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
  4. */
  5. /*
  6. * Mostly written by Mark Lord <mlord@pobox.com>
  7. * and Gadi Oxman <gadio@netvision.net.il>
  8. * and Andre Hedrick <andre@linux-ide.org>
  9. *
  10. * See linux/MAINTAINERS for address of current maintainer.
  11. *
  12. * This is the IDE probe module, as evolved from hd.c and ide.c.
  13. *
  14. * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
  15. * by Andrea Arcangeli
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/kernel.h>
  21. #include <linux/timer.h>
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/major.h>
  25. #include <linux/errno.h>
  26. #include <linux/genhd.h>
  27. #include <linux/slab.h>
  28. #include <linux/delay.h>
  29. #include <linux/ide.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/kmod.h>
  32. #include <linux/pci.h>
  33. #include <linux/scatterlist.h>
  34. #include <asm/byteorder.h>
  35. #include <asm/irq.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. /**
  39. * generic_id - add a generic drive id
  40. * @drive: drive to make an ID block for
  41. *
  42. * Add a fake id field to the drive we are passed. This allows
  43. * use to skip a ton of NULL checks (which people always miss)
  44. * and make drive properties unconditional outside of this file
  45. */
  46. static void generic_id(ide_drive_t *drive)
  47. {
  48. drive->id->cyls = drive->cyl;
  49. drive->id->heads = drive->head;
  50. drive->id->sectors = drive->sect;
  51. drive->id->cur_cyls = drive->cyl;
  52. drive->id->cur_heads = drive->head;
  53. drive->id->cur_sectors = drive->sect;
  54. }
  55. static void ide_disk_init_chs(ide_drive_t *drive)
  56. {
  57. struct hd_driveid *id = drive->id;
  58. /* Extract geometry if we did not already have one for the drive */
  59. if (!drive->cyl || !drive->head || !drive->sect) {
  60. drive->cyl = drive->bios_cyl = id->cyls;
  61. drive->head = drive->bios_head = id->heads;
  62. drive->sect = drive->bios_sect = id->sectors;
  63. }
  64. /* Handle logical geometry translation by the drive */
  65. if ((id->field_valid & 1) && id->cur_cyls &&
  66. id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
  67. drive->cyl = id->cur_cyls;
  68. drive->head = id->cur_heads;
  69. drive->sect = id->cur_sectors;
  70. }
  71. /* Use physical geometry if what we have still makes no sense */
  72. if (drive->head > 16 && id->heads && id->heads <= 16) {
  73. drive->cyl = id->cyls;
  74. drive->head = id->heads;
  75. drive->sect = id->sectors;
  76. }
  77. }
  78. static void ide_disk_init_mult_count(ide_drive_t *drive)
  79. {
  80. struct hd_driveid *id = drive->id;
  81. drive->mult_count = 0;
  82. if (id->max_multsect) {
  83. #ifdef CONFIG_IDEDISK_MULTI_MODE
  84. id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
  85. id->multsect_valid = id->multsect ? 1 : 0;
  86. drive->mult_req = id->multsect_valid ? id->max_multsect : 0;
  87. drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
  88. #else /* original, pre IDE-NFG, per request of AC */
  89. drive->mult_req = 0;
  90. if (drive->mult_req > id->max_multsect)
  91. drive->mult_req = id->max_multsect;
  92. if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
  93. drive->special.b.set_multmode = 1;
  94. #endif
  95. }
  96. }
  97. /**
  98. * do_identify - identify a drive
  99. * @drive: drive to identify
  100. * @cmd: command used
  101. *
  102. * Called when we have issued a drive identify command to
  103. * read and parse the results. This function is run with
  104. * interrupts disabled.
  105. */
  106. static inline void do_identify (ide_drive_t *drive, u8 cmd)
  107. {
  108. ide_hwif_t *hwif = HWIF(drive);
  109. int bswap = 1;
  110. struct hd_driveid *id;
  111. id = drive->id;
  112. /* read 512 bytes of id info */
  113. hwif->input_data(drive, NULL, id, SECTOR_SIZE);
  114. drive->id_read = 1;
  115. local_irq_enable();
  116. #ifdef DEBUG
  117. printk(KERN_INFO "%s: dumping identify data\n", drive->name);
  118. ide_dump_identify((u8 *)id);
  119. #endif
  120. ide_fix_driveid(id);
  121. #if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
  122. /*
  123. * EATA SCSI controllers do a hardware ATA emulation:
  124. * Ignore them if there is a driver for them available.
  125. */
  126. if ((id->model[0] == 'P' && id->model[1] == 'M') ||
  127. (id->model[0] == 'S' && id->model[1] == 'K')) {
  128. printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
  129. goto err_misc;
  130. }
  131. #endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */
  132. /*
  133. * WIN_IDENTIFY returns little-endian info,
  134. * WIN_PIDENTIFY *usually* returns little-endian info.
  135. */
  136. if (cmd == WIN_PIDENTIFY) {
  137. if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
  138. || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
  139. || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
  140. /* Vertos drives may still be weird */
  141. bswap ^= 1;
  142. }
  143. ide_fixstring(id->model, sizeof(id->model), bswap);
  144. ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
  145. ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
  146. /* we depend on this a lot! */
  147. id->model[sizeof(id->model)-1] = '\0';
  148. if (strstr(id->model, "E X A B Y T E N E S T"))
  149. goto err_misc;
  150. printk("%s: %s, ", drive->name, id->model);
  151. drive->present = 1;
  152. drive->dead = 0;
  153. /*
  154. * Check for an ATAPI device
  155. */
  156. if (cmd == WIN_PIDENTIFY) {
  157. u8 type = (id->config >> 8) & 0x1f;
  158. printk("ATAPI ");
  159. switch (type) {
  160. case ide_floppy:
  161. if (!strstr(id->model, "CD-ROM")) {
  162. if (!strstr(id->model, "oppy") &&
  163. !strstr(id->model, "poyp") &&
  164. !strstr(id->model, "ZIP"))
  165. printk("cdrom or floppy?, assuming ");
  166. if (drive->media != ide_cdrom) {
  167. printk ("FLOPPY");
  168. drive->removable = 1;
  169. break;
  170. }
  171. }
  172. /* Early cdrom models used zero */
  173. type = ide_cdrom;
  174. case ide_cdrom:
  175. drive->removable = 1;
  176. #ifdef CONFIG_PPC
  177. /* kludge for Apple PowerBook internal zip */
  178. if (!strstr(id->model, "CD-ROM") &&
  179. strstr(id->model, "ZIP")) {
  180. printk ("FLOPPY");
  181. type = ide_floppy;
  182. break;
  183. }
  184. #endif
  185. printk ("CD/DVD-ROM");
  186. break;
  187. case ide_tape:
  188. printk ("TAPE");
  189. break;
  190. case ide_optical:
  191. printk ("OPTICAL");
  192. drive->removable = 1;
  193. break;
  194. default:
  195. printk("UNKNOWN (type %d)", type);
  196. break;
  197. }
  198. printk (" drive\n");
  199. drive->media = type;
  200. /* an ATAPI device ignores DRDY */
  201. drive->ready_stat = 0;
  202. return;
  203. }
  204. /*
  205. * Not an ATAPI device: looks like a "regular" hard disk
  206. */
  207. /*
  208. * 0x848a = CompactFlash device
  209. * These are *not* removable in Linux definition of the term
  210. */
  211. if ((id->config != 0x848a) && (id->config & (1<<7)))
  212. drive->removable = 1;
  213. drive->media = ide_disk;
  214. printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
  215. return;
  216. err_misc:
  217. kfree(id);
  218. drive->present = 0;
  219. return;
  220. }
  221. /**
  222. * actual_try_to_identify - send ata/atapi identify
  223. * @drive: drive to identify
  224. * @cmd: command to use
  225. *
  226. * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
  227. * and waits for a response. It also monitors irqs while this is
  228. * happening, in hope of automatically determining which one is
  229. * being used by the interface.
  230. *
  231. * Returns: 0 device was identified
  232. * 1 device timed-out (no response to identify request)
  233. * 2 device aborted the command (refused to identify itself)
  234. */
  235. static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
  236. {
  237. ide_hwif_t *hwif = HWIF(drive);
  238. struct ide_io_ports *io_ports = &hwif->io_ports;
  239. int use_altstatus = 0, rc;
  240. unsigned long timeout;
  241. u8 s = 0, a = 0;
  242. /* take a deep breath */
  243. msleep(50);
  244. if (io_ports->ctl_addr) {
  245. a = ide_read_altstatus(drive);
  246. s = ide_read_status(drive);
  247. if ((a ^ s) & ~INDEX_STAT)
  248. /* ancient Seagate drives, broken interfaces */
  249. printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
  250. "instead of ALTSTATUS(0x%02x)\n",
  251. drive->name, s, a);
  252. else
  253. /* use non-intrusive polling */
  254. use_altstatus = 1;
  255. }
  256. /* set features register for atapi
  257. * identify command to be sure of reply
  258. */
  259. if ((cmd == WIN_PIDENTIFY))
  260. /* disable dma & overlap */
  261. hwif->OUTB(0, io_ports->feature_addr);
  262. /* ask drive for ID */
  263. hwif->OUTBSYNC(drive, cmd, io_ports->command_addr);
  264. timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
  265. timeout += jiffies;
  266. do {
  267. if (time_after(jiffies, timeout)) {
  268. /* drive timed-out */
  269. return 1;
  270. }
  271. /* give drive a breather */
  272. msleep(50);
  273. s = use_altstatus ? ide_read_altstatus(drive)
  274. : ide_read_status(drive);
  275. } while (s & BUSY_STAT);
  276. /* wait for IRQ and DRQ_STAT */
  277. msleep(50);
  278. s = ide_read_status(drive);
  279. if (OK_STAT(s, DRQ_STAT, BAD_R_STAT)) {
  280. unsigned long flags;
  281. /* local CPU only; some systems need this */
  282. local_irq_save(flags);
  283. /* drive returned ID */
  284. do_identify(drive, cmd);
  285. /* drive responded with ID */
  286. rc = 0;
  287. /* clear drive IRQ */
  288. (void)ide_read_status(drive);
  289. local_irq_restore(flags);
  290. } else {
  291. /* drive refused ID */
  292. rc = 2;
  293. }
  294. return rc;
  295. }
  296. /**
  297. * try_to_identify - try to identify a drive
  298. * @drive: drive to probe
  299. * @cmd: command to use
  300. *
  301. * Issue the identify command and then do IRQ probing to
  302. * complete the identification when needed by finding the
  303. * IRQ the drive is attached to
  304. */
  305. static int try_to_identify (ide_drive_t *drive, u8 cmd)
  306. {
  307. ide_hwif_t *hwif = HWIF(drive);
  308. int retval;
  309. int autoprobe = 0;
  310. unsigned long cookie = 0;
  311. /*
  312. * Disable device irq unless we need to
  313. * probe for it. Otherwise we'll get spurious
  314. * interrupts during the identify-phase that
  315. * the irq handler isn't expecting.
  316. */
  317. if (hwif->io_ports.ctl_addr) {
  318. if (!hwif->irq) {
  319. autoprobe = 1;
  320. cookie = probe_irq_on();
  321. }
  322. ide_set_irq(drive, autoprobe);
  323. }
  324. retval = actual_try_to_identify(drive, cmd);
  325. if (autoprobe) {
  326. int irq;
  327. ide_set_irq(drive, 0);
  328. /* clear drive IRQ */
  329. (void)ide_read_status(drive);
  330. udelay(5);
  331. irq = probe_irq_off(cookie);
  332. if (!hwif->irq) {
  333. if (irq > 0) {
  334. hwif->irq = irq;
  335. } else {
  336. /* Mmmm.. multiple IRQs..
  337. * don't know which was ours
  338. */
  339. printk("%s: IRQ probe failed (0x%lx)\n",
  340. drive->name, cookie);
  341. }
  342. }
  343. }
  344. return retval;
  345. }
  346. static int ide_busy_sleep(ide_hwif_t *hwif)
  347. {
  348. unsigned long timeout = jiffies + WAIT_WORSTCASE;
  349. u8 stat;
  350. do {
  351. msleep(50);
  352. stat = hwif->INB(hwif->io_ports.status_addr);
  353. if ((stat & BUSY_STAT) == 0)
  354. return 0;
  355. } while (time_before(jiffies, timeout));
  356. return 1;
  357. }
  358. /**
  359. * do_probe - probe an IDE device
  360. * @drive: drive to probe
  361. * @cmd: command to use
  362. *
  363. * do_probe() has the difficult job of finding a drive if it exists,
  364. * without getting hung up if it doesn't exist, without trampling on
  365. * ethernet cards, and without leaving any IRQs dangling to haunt us later.
  366. *
  367. * If a drive is "known" to exist (from CMOS or kernel parameters),
  368. * but does not respond right away, the probe will "hang in there"
  369. * for the maximum wait time (about 30 seconds), otherwise it will
  370. * exit much more quickly.
  371. *
  372. * Returns: 0 device was identified
  373. * 1 device timed-out (no response to identify request)
  374. * 2 device aborted the command (refused to identify itself)
  375. * 3 bad status from device (possible for ATAPI drives)
  376. * 4 probe was not attempted because failure was obvious
  377. */
  378. static int do_probe (ide_drive_t *drive, u8 cmd)
  379. {
  380. ide_hwif_t *hwif = HWIF(drive);
  381. struct ide_io_ports *io_ports = &hwif->io_ports;
  382. int rc;
  383. u8 stat;
  384. if (drive->present) {
  385. /* avoid waiting for inappropriate probes */
  386. if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
  387. return 4;
  388. }
  389. #ifdef DEBUG
  390. printk("probing for %s: present=%d, media=%d, probetype=%s\n",
  391. drive->name, drive->present, drive->media,
  392. (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
  393. #endif
  394. /* needed for some systems
  395. * (e.g. crw9624 as drive0 with disk as slave)
  396. */
  397. msleep(50);
  398. SELECT_DRIVE(drive);
  399. msleep(50);
  400. if (hwif->INB(io_ports->device_addr) != drive->select.all &&
  401. !drive->present) {
  402. if (drive->select.b.unit != 0) {
  403. /* exit with drive0 selected */
  404. SELECT_DRIVE(&hwif->drives[0]);
  405. /* allow BUSY_STAT to assert & clear */
  406. msleep(50);
  407. }
  408. /* no i/f present: mmm.. this should be a 4 -ml */
  409. return 3;
  410. }
  411. stat = ide_read_status(drive);
  412. if (OK_STAT(stat, READY_STAT, BUSY_STAT) ||
  413. drive->present || cmd == WIN_PIDENTIFY) {
  414. /* send cmd and wait */
  415. if ((rc = try_to_identify(drive, cmd))) {
  416. /* failed: try again */
  417. rc = try_to_identify(drive,cmd);
  418. }
  419. stat = ide_read_status(drive);
  420. if (stat == (BUSY_STAT | READY_STAT))
  421. return 4;
  422. if (rc == 1 && cmd == WIN_PIDENTIFY) {
  423. printk(KERN_ERR "%s: no response (status = 0x%02x), "
  424. "resetting drive\n", drive->name, stat);
  425. msleep(50);
  426. hwif->OUTB(drive->select.all, io_ports->device_addr);
  427. msleep(50);
  428. hwif->OUTBSYNC(drive, WIN_SRST, io_ports->command_addr);
  429. (void)ide_busy_sleep(hwif);
  430. rc = try_to_identify(drive, cmd);
  431. }
  432. /* ensure drive IRQ is clear */
  433. stat = ide_read_status(drive);
  434. if (rc == 1)
  435. printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
  436. drive->name, stat);
  437. } else {
  438. /* not present or maybe ATAPI */
  439. rc = 3;
  440. }
  441. if (drive->select.b.unit != 0) {
  442. /* exit with drive0 selected */
  443. SELECT_DRIVE(&hwif->drives[0]);
  444. msleep(50);
  445. /* ensure drive irq is clear */
  446. (void)ide_read_status(drive);
  447. }
  448. return rc;
  449. }
  450. /*
  451. *
  452. */
  453. static void enable_nest (ide_drive_t *drive)
  454. {
  455. ide_hwif_t *hwif = HWIF(drive);
  456. u8 stat;
  457. printk("%s: enabling %s -- ", hwif->name, drive->id->model);
  458. SELECT_DRIVE(drive);
  459. msleep(50);
  460. hwif->OUTBSYNC(drive, EXABYTE_ENABLE_NEST, hwif->io_ports.command_addr);
  461. if (ide_busy_sleep(hwif)) {
  462. printk(KERN_CONT "failed (timeout)\n");
  463. return;
  464. }
  465. msleep(50);
  466. stat = ide_read_status(drive);
  467. if (!OK_STAT(stat, 0, BAD_STAT))
  468. printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
  469. else
  470. printk(KERN_CONT "success\n");
  471. /* if !(success||timed-out) */
  472. if (do_probe(drive, WIN_IDENTIFY) >= 2) {
  473. /* look for ATAPI device */
  474. (void) do_probe(drive, WIN_PIDENTIFY);
  475. }
  476. }
  477. /**
  478. * probe_for_drives - upper level drive probe
  479. * @drive: drive to probe for
  480. *
  481. * probe_for_drive() tests for existence of a given drive using do_probe()
  482. * and presents things to the user as needed.
  483. *
  484. * Returns: 0 no device was found
  485. * 1 device was found (note: drive->present might
  486. * still be 0)
  487. */
  488. static inline u8 probe_for_drive (ide_drive_t *drive)
  489. {
  490. /*
  491. * In order to keep things simple we have an id
  492. * block for all drives at all times. If the device
  493. * is pre ATA or refuses ATA/ATAPI identify we
  494. * will add faked data to this.
  495. *
  496. * Also note that 0 everywhere means "can't do X"
  497. */
  498. drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
  499. drive->id_read = 0;
  500. if(drive->id == NULL)
  501. {
  502. printk(KERN_ERR "ide: out of memory for id data.\n");
  503. return 0;
  504. }
  505. strcpy(drive->id->model, "UNKNOWN");
  506. /* skip probing? */
  507. if (!drive->noprobe)
  508. {
  509. /* if !(success||timed-out) */
  510. if (do_probe(drive, WIN_IDENTIFY) >= 2) {
  511. /* look for ATAPI device */
  512. (void) do_probe(drive, WIN_PIDENTIFY);
  513. }
  514. if (!drive->present)
  515. /* drive not found */
  516. return 0;
  517. if (strstr(drive->id->model, "E X A B Y T E N E S T"))
  518. enable_nest(drive);
  519. /* identification failed? */
  520. if (!drive->id_read) {
  521. if (drive->media == ide_disk) {
  522. printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
  523. drive->name, drive->cyl,
  524. drive->head, drive->sect);
  525. } else if (drive->media == ide_cdrom) {
  526. printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
  527. } else {
  528. /* nuke it */
  529. printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
  530. drive->present = 0;
  531. }
  532. }
  533. /* drive was found */
  534. }
  535. if(!drive->present)
  536. return 0;
  537. /* The drive wasn't being helpful. Add generic info only */
  538. if (drive->id_read == 0) {
  539. generic_id(drive);
  540. return 1;
  541. }
  542. if (drive->media == ide_disk) {
  543. ide_disk_init_chs(drive);
  544. ide_disk_init_mult_count(drive);
  545. }
  546. return drive->present;
  547. }
  548. static void hwif_release_dev (struct device *dev)
  549. {
  550. ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
  551. complete(&hwif->gendev_rel_comp);
  552. }
  553. static int ide_register_port(ide_hwif_t *hwif)
  554. {
  555. int ret;
  556. /* register with global device tree */
  557. strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
  558. hwif->gendev.driver_data = hwif;
  559. if (hwif->gendev.parent == NULL) {
  560. if (hwif->dev)
  561. hwif->gendev.parent = hwif->dev;
  562. else
  563. /* Would like to do = &device_legacy */
  564. hwif->gendev.parent = NULL;
  565. }
  566. hwif->gendev.release = hwif_release_dev;
  567. ret = device_register(&hwif->gendev);
  568. if (ret < 0) {
  569. printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
  570. __func__, ret);
  571. goto out;
  572. }
  573. get_device(&hwif->gendev);
  574. hwif->portdev = device_create(ide_port_class, &hwif->gendev,
  575. MKDEV(0, 0), hwif->name);
  576. if (IS_ERR(hwif->portdev)) {
  577. ret = PTR_ERR(hwif->portdev);
  578. device_unregister(&hwif->gendev);
  579. }
  580. dev_set_drvdata(hwif->portdev, hwif);
  581. out:
  582. return ret;
  583. }
  584. /**
  585. * ide_port_wait_ready - wait for port to become ready
  586. * @hwif: IDE port
  587. *
  588. * This is needed on some PPCs and a bunch of BIOS-less embedded
  589. * platforms. Typical cases are:
  590. *
  591. * - The firmware hard reset the disk before booting the kernel,
  592. * the drive is still doing it's poweron-reset sequence, that
  593. * can take up to 30 seconds.
  594. *
  595. * - The firmware does nothing (or no firmware), the device is
  596. * still in POST state (same as above actually).
  597. *
  598. * - Some CD/DVD/Writer combo drives tend to drive the bus during
  599. * their reset sequence even when they are non-selected slave
  600. * devices, thus preventing discovery of the main HD.
  601. *
  602. * Doing this wait-for-non-busy should not harm any existing
  603. * configuration and fix some issues like the above.
  604. *
  605. * BenH.
  606. *
  607. * Returns 0 on success, error code (< 0) otherwise.
  608. */
  609. static int ide_port_wait_ready(ide_hwif_t *hwif)
  610. {
  611. int unit, rc;
  612. printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
  613. /* Let HW settle down a bit from whatever init state we
  614. * come from */
  615. mdelay(2);
  616. /* Wait for BSY bit to go away, spec timeout is 30 seconds,
  617. * I know of at least one disk who takes 31 seconds, I use 35
  618. * here to be safe
  619. */
  620. rc = ide_wait_not_busy(hwif, 35000);
  621. if (rc)
  622. return rc;
  623. /* Now make sure both master & slave are ready */
  624. for (unit = 0; unit < MAX_DRIVES; unit++) {
  625. ide_drive_t *drive = &hwif->drives[unit];
  626. /* Ignore disks that we will not probe for later. */
  627. if (!drive->noprobe || drive->present) {
  628. SELECT_DRIVE(drive);
  629. ide_set_irq(drive, 1);
  630. mdelay(2);
  631. rc = ide_wait_not_busy(hwif, 35000);
  632. if (rc)
  633. goto out;
  634. } else
  635. printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
  636. drive->name);
  637. }
  638. out:
  639. /* Exit function with master reselected (let's be sane) */
  640. if (unit)
  641. SELECT_DRIVE(&hwif->drives[0]);
  642. return rc;
  643. }
  644. /**
  645. * ide_undecoded_slave - look for bad CF adapters
  646. * @drive1: drive
  647. *
  648. * Analyse the drives on the interface and attempt to decide if we
  649. * have the same drive viewed twice. This occurs with crap CF adapters
  650. * and PCMCIA sometimes.
  651. */
  652. void ide_undecoded_slave(ide_drive_t *drive1)
  653. {
  654. ide_drive_t *drive0 = &drive1->hwif->drives[0];
  655. if ((drive1->dn & 1) == 0 || drive0->present == 0)
  656. return;
  657. /* If the models don't match they are not the same product */
  658. if (strcmp(drive0->id->model, drive1->id->model))
  659. return;
  660. /* Serial numbers do not match */
  661. if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
  662. return;
  663. /* No serial number, thankfully very rare for CF */
  664. if (drive0->id->serial_no[0] == 0)
  665. return;
  666. /* Appears to be an IDE flash adapter with decode bugs */
  667. printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
  668. drive1->present = 0;
  669. }
  670. EXPORT_SYMBOL_GPL(ide_undecoded_slave);
  671. static int ide_probe_port(ide_hwif_t *hwif)
  672. {
  673. unsigned long flags;
  674. unsigned int irqd;
  675. int unit, rc = -ENODEV;
  676. BUG_ON(hwif->present);
  677. if (hwif->drives[0].noprobe && hwif->drives[1].noprobe)
  678. return -EACCES;
  679. /*
  680. * We must always disable IRQ, as probe_for_drive will assert IRQ, but
  681. * we'll install our IRQ driver much later...
  682. */
  683. irqd = hwif->irq;
  684. if (irqd)
  685. disable_irq(hwif->irq);
  686. local_irq_set(flags);
  687. if (ide_port_wait_ready(hwif) == -EBUSY)
  688. printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
  689. /*
  690. * Second drive should only exist if first drive was found,
  691. * but a lot of cdrom drives are configured as single slaves.
  692. */
  693. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  694. ide_drive_t *drive = &hwif->drives[unit];
  695. drive->dn = (hwif->channel ? 2 : 0) + unit;
  696. (void) probe_for_drive(drive);
  697. if (drive->present)
  698. rc = 0;
  699. }
  700. local_irq_restore(flags);
  701. /*
  702. * Use cached IRQ number. It might be (and is...) changed by probe
  703. * code above
  704. */
  705. if (irqd)
  706. enable_irq(irqd);
  707. return rc;
  708. }
  709. static void ide_port_tune_devices(ide_hwif_t *hwif)
  710. {
  711. const struct ide_port_ops *port_ops = hwif->port_ops;
  712. int unit;
  713. for (unit = 0; unit < MAX_DRIVES; unit++) {
  714. ide_drive_t *drive = &hwif->drives[unit];
  715. if (drive->present && port_ops && port_ops->quirkproc)
  716. port_ops->quirkproc(drive);
  717. }
  718. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  719. ide_drive_t *drive = &hwif->drives[unit];
  720. if (drive->present) {
  721. ide_set_max_pio(drive);
  722. drive->nice1 = 1;
  723. if (hwif->dma_ops)
  724. ide_set_dma(drive);
  725. }
  726. }
  727. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  728. ide_drive_t *drive = &hwif->drives[unit];
  729. if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
  730. drive->no_io_32bit = 1;
  731. else
  732. drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
  733. }
  734. }
  735. #if MAX_HWIFS > 1
  736. /*
  737. * save_match() is used to simplify logic in init_irq() below.
  738. *
  739. * A loophole here is that we may not know about a particular
  740. * hwif's irq until after that hwif is actually probed/initialized..
  741. * This could be a problem for the case where an hwif is on a
  742. * dual interface that requires serialization (eg. cmd640) and another
  743. * hwif using one of the same irqs is initialized beforehand.
  744. *
  745. * This routine detects and reports such situations, but does not fix them.
  746. */
  747. static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
  748. {
  749. ide_hwif_t *m = *match;
  750. if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
  751. if (!new->hwgroup)
  752. return;
  753. printk("%s: potential irq problem with %s and %s\n",
  754. hwif->name, new->name, m->name);
  755. }
  756. if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
  757. *match = new;
  758. }
  759. #endif /* MAX_HWIFS > 1 */
  760. /*
  761. * init request queue
  762. */
  763. static int ide_init_queue(ide_drive_t *drive)
  764. {
  765. struct request_queue *q;
  766. ide_hwif_t *hwif = HWIF(drive);
  767. int max_sectors = 256;
  768. int max_sg_entries = PRD_ENTRIES;
  769. /*
  770. * Our default set up assumes the normal IDE case,
  771. * that is 64K segmenting, standard PRD setup
  772. * and LBA28. Some drivers then impose their own
  773. * limits and LBA48 we could raise it but as yet
  774. * do not.
  775. */
  776. q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
  777. if (!q)
  778. return 1;
  779. q->queuedata = drive;
  780. blk_queue_segment_boundary(q, 0xffff);
  781. if (hwif->rqsize < max_sectors)
  782. max_sectors = hwif->rqsize;
  783. blk_queue_max_sectors(q, max_sectors);
  784. #ifdef CONFIG_PCI
  785. /* When we have an IOMMU, we may have a problem where pci_map_sg()
  786. * creates segments that don't completely match our boundary
  787. * requirements and thus need to be broken up again. Because it
  788. * doesn't align properly either, we may actually have to break up
  789. * to more segments than what was we got in the first place, a max
  790. * worst case is twice as many.
  791. * This will be fixed once we teach pci_map_sg() about our boundary
  792. * requirements, hopefully soon. *FIXME*
  793. */
  794. if (!PCI_DMA_BUS_IS_PHYS)
  795. max_sg_entries >>= 1;
  796. #endif /* CONFIG_PCI */
  797. blk_queue_max_hw_segments(q, max_sg_entries);
  798. blk_queue_max_phys_segments(q, max_sg_entries);
  799. /* assign drive queue */
  800. drive->queue = q;
  801. /* needs drive->queue to be set */
  802. ide_toggle_bounce(drive, 1);
  803. return 0;
  804. }
  805. static void ide_add_drive_to_hwgroup(ide_drive_t *drive)
  806. {
  807. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  808. spin_lock_irq(&ide_lock);
  809. if (!hwgroup->drive) {
  810. /* first drive for hwgroup. */
  811. drive->next = drive;
  812. hwgroup->drive = drive;
  813. hwgroup->hwif = HWIF(hwgroup->drive);
  814. } else {
  815. drive->next = hwgroup->drive->next;
  816. hwgroup->drive->next = drive;
  817. }
  818. spin_unlock_irq(&ide_lock);
  819. }
  820. /*
  821. * For any present drive:
  822. * - allocate the block device queue
  823. * - link drive into the hwgroup
  824. */
  825. static void ide_port_setup_devices(ide_hwif_t *hwif)
  826. {
  827. int i;
  828. mutex_lock(&ide_cfg_mtx);
  829. for (i = 0; i < MAX_DRIVES; i++) {
  830. ide_drive_t *drive = &hwif->drives[i];
  831. if (!drive->present)
  832. continue;
  833. if (ide_init_queue(drive)) {
  834. printk(KERN_ERR "ide: failed to init %s\n",
  835. drive->name);
  836. continue;
  837. }
  838. ide_add_drive_to_hwgroup(drive);
  839. }
  840. mutex_unlock(&ide_cfg_mtx);
  841. }
  842. /*
  843. * This routine sets up the irq for an ide interface, and creates a new
  844. * hwgroup for the irq/hwif if none was previously assigned.
  845. *
  846. * Much of the code is for correctly detecting/handling irq sharing
  847. * and irq serialization situations. This is somewhat complex because
  848. * it handles static as well as dynamic (PCMCIA) IDE interfaces.
  849. */
  850. static int init_irq (ide_hwif_t *hwif)
  851. {
  852. struct ide_io_ports *io_ports = &hwif->io_ports;
  853. unsigned int index;
  854. ide_hwgroup_t *hwgroup;
  855. ide_hwif_t *match = NULL;
  856. BUG_ON(in_interrupt());
  857. BUG_ON(irqs_disabled());
  858. BUG_ON(hwif == NULL);
  859. mutex_lock(&ide_cfg_mtx);
  860. hwif->hwgroup = NULL;
  861. #if MAX_HWIFS > 1
  862. /*
  863. * Group up with any other hwifs that share our irq(s).
  864. */
  865. for (index = 0; index < MAX_HWIFS; index++) {
  866. ide_hwif_t *h = &ide_hwifs[index];
  867. if (h->hwgroup) { /* scan only initialized hwif's */
  868. if (hwif->irq == h->irq) {
  869. hwif->sharing_irq = h->sharing_irq = 1;
  870. if (hwif->chipset != ide_pci ||
  871. h->chipset != ide_pci) {
  872. save_match(hwif, h, &match);
  873. }
  874. }
  875. if (hwif->serialized) {
  876. if (hwif->mate && hwif->mate->irq == h->irq)
  877. save_match(hwif, h, &match);
  878. }
  879. if (h->serialized) {
  880. if (h->mate && hwif->irq == h->mate->irq)
  881. save_match(hwif, h, &match);
  882. }
  883. }
  884. }
  885. #endif /* MAX_HWIFS > 1 */
  886. /*
  887. * If we are still without a hwgroup, then form a new one
  888. */
  889. if (match) {
  890. hwgroup = match->hwgroup;
  891. hwif->hwgroup = hwgroup;
  892. /*
  893. * Link us into the hwgroup.
  894. * This must be done early, do ensure that unexpected_intr
  895. * can find the hwif and prevent irq storms.
  896. * No drives are attached to the new hwif, choose_drive
  897. * can't do anything stupid (yet).
  898. * Add ourself as the 2nd entry to the hwgroup->hwif
  899. * linked list, the first entry is the hwif that owns
  900. * hwgroup->handler - do not change that.
  901. */
  902. spin_lock_irq(&ide_lock);
  903. hwif->next = hwgroup->hwif->next;
  904. hwgroup->hwif->next = hwif;
  905. BUG_ON(hwif->next == hwif);
  906. spin_unlock_irq(&ide_lock);
  907. } else {
  908. hwgroup = kmalloc_node(sizeof(*hwgroup), GFP_KERNEL|__GFP_ZERO,
  909. hwif_to_node(hwif));
  910. if (hwgroup == NULL)
  911. goto out_up;
  912. hwif->hwgroup = hwgroup;
  913. hwgroup->hwif = hwif->next = hwif;
  914. init_timer(&hwgroup->timer);
  915. hwgroup->timer.function = &ide_timer_expiry;
  916. hwgroup->timer.data = (unsigned long) hwgroup;
  917. }
  918. /*
  919. * Allocate the irq, if not already obtained for another hwif
  920. */
  921. if (!match || match->irq != hwif->irq) {
  922. int sa = 0;
  923. #if defined(__mc68000__)
  924. sa = IRQF_SHARED;
  925. #endif /* __mc68000__ */
  926. if (IDE_CHIPSET_IS_PCI(hwif->chipset))
  927. sa = IRQF_SHARED;
  928. if (io_ports->ctl_addr)
  929. /* clear nIEN */
  930. hwif->OUTB(0x08, io_ports->ctl_addr);
  931. if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
  932. goto out_unlink;
  933. }
  934. if (!hwif->rqsize) {
  935. if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
  936. (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
  937. hwif->rqsize = 256;
  938. else
  939. hwif->rqsize = 65536;
  940. }
  941. #if !defined(__mc68000__)
  942. printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
  943. io_ports->data_addr, io_ports->status_addr,
  944. io_ports->ctl_addr, hwif->irq);
  945. #else
  946. printk("%s at 0x%08lx on irq %d", hwif->name,
  947. io_ports->data_addr, hwif->irq);
  948. #endif /* __mc68000__ */
  949. if (match)
  950. printk(" (%sed with %s)",
  951. hwif->sharing_irq ? "shar" : "serializ", match->name);
  952. printk("\n");
  953. mutex_unlock(&ide_cfg_mtx);
  954. return 0;
  955. out_unlink:
  956. ide_remove_port_from_hwgroup(hwif);
  957. out_up:
  958. mutex_unlock(&ide_cfg_mtx);
  959. return 1;
  960. }
  961. static int ata_lock(dev_t dev, void *data)
  962. {
  963. /* FIXME: we want to pin hwif down */
  964. return 0;
  965. }
  966. static struct kobject *ata_probe(dev_t dev, int *part, void *data)
  967. {
  968. ide_hwif_t *hwif = data;
  969. int unit = *part >> PARTN_BITS;
  970. ide_drive_t *drive = &hwif->drives[unit];
  971. if (!drive->present)
  972. return NULL;
  973. if (drive->media == ide_disk)
  974. request_module("ide-disk");
  975. if (drive->scsi)
  976. request_module("ide-scsi");
  977. if (drive->media == ide_cdrom || drive->media == ide_optical)
  978. request_module("ide-cd");
  979. if (drive->media == ide_tape)
  980. request_module("ide-tape");
  981. if (drive->media == ide_floppy)
  982. request_module("ide-floppy");
  983. return NULL;
  984. }
  985. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  986. {
  987. struct gendisk *p = data;
  988. *part &= (1 << PARTN_BITS) - 1;
  989. return &p->dev.kobj;
  990. }
  991. static int exact_lock(dev_t dev, void *data)
  992. {
  993. struct gendisk *p = data;
  994. if (!get_disk(p))
  995. return -1;
  996. return 0;
  997. }
  998. void ide_register_region(struct gendisk *disk)
  999. {
  1000. blk_register_region(MKDEV(disk->major, disk->first_minor),
  1001. disk->minors, NULL, exact_match, exact_lock, disk);
  1002. }
  1003. EXPORT_SYMBOL_GPL(ide_register_region);
  1004. void ide_unregister_region(struct gendisk *disk)
  1005. {
  1006. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  1007. disk->minors);
  1008. }
  1009. EXPORT_SYMBOL_GPL(ide_unregister_region);
  1010. void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
  1011. {
  1012. ide_hwif_t *hwif = drive->hwif;
  1013. unsigned int unit = (drive->select.all >> 4) & 1;
  1014. disk->major = hwif->major;
  1015. disk->first_minor = unit << PARTN_BITS;
  1016. sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
  1017. disk->queue = drive->queue;
  1018. }
  1019. EXPORT_SYMBOL_GPL(ide_init_disk);
  1020. static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
  1021. {
  1022. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  1023. if (drive == drive->next) {
  1024. /* special case: last drive from hwgroup. */
  1025. BUG_ON(hwgroup->drive != drive);
  1026. hwgroup->drive = NULL;
  1027. } else {
  1028. ide_drive_t *walk;
  1029. walk = hwgroup->drive;
  1030. while (walk->next != drive)
  1031. walk = walk->next;
  1032. walk->next = drive->next;
  1033. if (hwgroup->drive == drive) {
  1034. hwgroup->drive = drive->next;
  1035. hwgroup->hwif = hwgroup->drive->hwif;
  1036. }
  1037. }
  1038. BUG_ON(hwgroup->drive == drive);
  1039. }
  1040. static void drive_release_dev (struct device *dev)
  1041. {
  1042. ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
  1043. ide_proc_unregister_device(drive);
  1044. spin_lock_irq(&ide_lock);
  1045. ide_remove_drive_from_hwgroup(drive);
  1046. kfree(drive->id);
  1047. drive->id = NULL;
  1048. drive->present = 0;
  1049. /* Messed up locking ... */
  1050. spin_unlock_irq(&ide_lock);
  1051. blk_cleanup_queue(drive->queue);
  1052. spin_lock_irq(&ide_lock);
  1053. drive->queue = NULL;
  1054. spin_unlock_irq(&ide_lock);
  1055. complete(&drive->gendev_rel_comp);
  1056. }
  1057. #ifndef ide_default_irq
  1058. #define ide_default_irq(irq) 0
  1059. #endif
  1060. static int hwif_init(ide_hwif_t *hwif)
  1061. {
  1062. int old_irq;
  1063. if (!hwif->irq) {
  1064. hwif->irq = ide_default_irq(hwif->io_ports.data_addr);
  1065. if (!hwif->irq) {
  1066. printk("%s: DISABLED, NO IRQ\n", hwif->name);
  1067. return 0;
  1068. }
  1069. }
  1070. if (register_blkdev(hwif->major, hwif->name))
  1071. return 0;
  1072. if (!hwif->sg_max_nents)
  1073. hwif->sg_max_nents = PRD_ENTRIES;
  1074. hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
  1075. GFP_KERNEL);
  1076. if (!hwif->sg_table) {
  1077. printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
  1078. goto out;
  1079. }
  1080. sg_init_table(hwif->sg_table, hwif->sg_max_nents);
  1081. if (init_irq(hwif) == 0)
  1082. goto done;
  1083. old_irq = hwif->irq;
  1084. /*
  1085. * It failed to initialise. Find the default IRQ for
  1086. * this port and try that.
  1087. */
  1088. hwif->irq = ide_default_irq(hwif->io_ports.data_addr);
  1089. if (!hwif->irq) {
  1090. printk("%s: Disabled unable to get IRQ %d.\n",
  1091. hwif->name, old_irq);
  1092. goto out;
  1093. }
  1094. if (init_irq(hwif)) {
  1095. printk("%s: probed IRQ %d and default IRQ %d failed.\n",
  1096. hwif->name, old_irq, hwif->irq);
  1097. goto out;
  1098. }
  1099. printk("%s: probed IRQ %d failed, using default.\n",
  1100. hwif->name, hwif->irq);
  1101. done:
  1102. blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
  1103. THIS_MODULE, ata_probe, ata_lock, hwif);
  1104. return 1;
  1105. out:
  1106. unregister_blkdev(hwif->major, hwif->name);
  1107. return 0;
  1108. }
  1109. static void hwif_register_devices(ide_hwif_t *hwif)
  1110. {
  1111. unsigned int i;
  1112. for (i = 0; i < MAX_DRIVES; i++) {
  1113. ide_drive_t *drive = &hwif->drives[i];
  1114. struct device *dev = &drive->gendev;
  1115. int ret;
  1116. if (!drive->present)
  1117. continue;
  1118. ide_add_generic_settings(drive);
  1119. snprintf(dev->bus_id, BUS_ID_SIZE, "%u.%u", hwif->index, i);
  1120. dev->parent = &hwif->gendev;
  1121. dev->bus = &ide_bus_type;
  1122. dev->driver_data = drive;
  1123. dev->release = drive_release_dev;
  1124. ret = device_register(dev);
  1125. if (ret < 0)
  1126. printk(KERN_WARNING "IDE: %s: device_register error: "
  1127. "%d\n", __func__, ret);
  1128. }
  1129. }
  1130. static void ide_port_init_devices(ide_hwif_t *hwif)
  1131. {
  1132. const struct ide_port_ops *port_ops = hwif->port_ops;
  1133. int i;
  1134. for (i = 0; i < MAX_DRIVES; i++) {
  1135. ide_drive_t *drive = &hwif->drives[i];
  1136. if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
  1137. drive->io_32bit = 1;
  1138. if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
  1139. drive->unmask = 1;
  1140. if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
  1141. drive->no_unmask = 1;
  1142. }
  1143. if (port_ops && port_ops->port_init_devs)
  1144. port_ops->port_init_devs(hwif);
  1145. }
  1146. static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
  1147. const struct ide_port_info *d)
  1148. {
  1149. if (d->chipset != ide_etrax100)
  1150. hwif->channel = port;
  1151. if (d->chipset)
  1152. hwif->chipset = d->chipset;
  1153. if (d->init_iops)
  1154. d->init_iops(hwif);
  1155. if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
  1156. (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
  1157. hwif->irq = port ? 15 : 14;
  1158. /* ->host_flags may be set by ->init_iops (or even earlier...) */
  1159. hwif->host_flags |= d->host_flags;
  1160. hwif->pio_mask = d->pio_mask;
  1161. /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
  1162. if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
  1163. hwif->port_ops = d->port_ops;
  1164. hwif->swdma_mask = d->swdma_mask;
  1165. hwif->mwdma_mask = d->mwdma_mask;
  1166. hwif->ultra_mask = d->udma_mask;
  1167. if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
  1168. int rc;
  1169. if (d->init_dma)
  1170. rc = d->init_dma(hwif, d);
  1171. else
  1172. rc = ide_hwif_setup_dma(hwif, d);
  1173. if (rc < 0) {
  1174. printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
  1175. hwif->swdma_mask = 0;
  1176. hwif->mwdma_mask = 0;
  1177. hwif->ultra_mask = 0;
  1178. } else if (d->dma_ops)
  1179. hwif->dma_ops = d->dma_ops;
  1180. }
  1181. if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
  1182. ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base)) {
  1183. if (hwif->mate)
  1184. hwif->mate->serialized = hwif->serialized = 1;
  1185. }
  1186. if (d->host_flags & IDE_HFLAG_RQSIZE_256)
  1187. hwif->rqsize = 256;
  1188. /* call chipset specific routine for each enabled port */
  1189. if (d->init_hwif)
  1190. d->init_hwif(hwif);
  1191. }
  1192. static void ide_port_cable_detect(ide_hwif_t *hwif)
  1193. {
  1194. const struct ide_port_ops *port_ops = hwif->port_ops;
  1195. if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
  1196. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  1197. hwif->cbl = port_ops->cable_detect(hwif);
  1198. }
  1199. }
  1200. static ssize_t store_delete_devices(struct device *portdev,
  1201. struct device_attribute *attr,
  1202. const char *buf, size_t n)
  1203. {
  1204. ide_hwif_t *hwif = dev_get_drvdata(portdev);
  1205. if (strncmp(buf, "1", n))
  1206. return -EINVAL;
  1207. ide_port_unregister_devices(hwif);
  1208. return n;
  1209. };
  1210. static DEVICE_ATTR(delete_devices, S_IWUSR, NULL, store_delete_devices);
  1211. static ssize_t store_scan(struct device *portdev,
  1212. struct device_attribute *attr,
  1213. const char *buf, size_t n)
  1214. {
  1215. ide_hwif_t *hwif = dev_get_drvdata(portdev);
  1216. if (strncmp(buf, "1", n))
  1217. return -EINVAL;
  1218. ide_port_unregister_devices(hwif);
  1219. ide_port_scan(hwif);
  1220. return n;
  1221. };
  1222. static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
  1223. static struct device_attribute *ide_port_attrs[] = {
  1224. &dev_attr_delete_devices,
  1225. &dev_attr_scan,
  1226. NULL
  1227. };
  1228. static int ide_sysfs_register_port(ide_hwif_t *hwif)
  1229. {
  1230. int i, rc;
  1231. for (i = 0; ide_port_attrs[i]; i++) {
  1232. rc = device_create_file(hwif->portdev, ide_port_attrs[i]);
  1233. if (rc)
  1234. break;
  1235. }
  1236. return rc;
  1237. }
  1238. /**
  1239. * ide_find_port_slot - find free ide_hwifs[] slot
  1240. * @d: IDE port info
  1241. *
  1242. * Return the new hwif. If we are out of free slots return NULL.
  1243. */
  1244. ide_hwif_t *ide_find_port_slot(const struct ide_port_info *d)
  1245. {
  1246. ide_hwif_t *hwif;
  1247. int i;
  1248. u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
  1249. /*
  1250. * Claim an unassigned slot.
  1251. *
  1252. * Give preference to claiming other slots before claiming ide0/ide1,
  1253. * just in case there's another interface yet-to-be-scanned
  1254. * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
  1255. *
  1256. * Unless there is a bootable card that does not use the standard
  1257. * ports 0x1f0/0x170 (the ide0/ide1 defaults).
  1258. */
  1259. if (bootable) {
  1260. i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
  1261. for (; i < MAX_HWIFS; i++) {
  1262. hwif = &ide_hwifs[i];
  1263. if (hwif->chipset == ide_unknown)
  1264. return hwif;
  1265. }
  1266. } else {
  1267. for (i = 2; i < MAX_HWIFS; i++) {
  1268. hwif = &ide_hwifs[i];
  1269. if (hwif->chipset == ide_unknown)
  1270. return hwif;
  1271. }
  1272. for (i = 0; i < 2 && i < MAX_HWIFS; i++) {
  1273. hwif = &ide_hwifs[i];
  1274. if (hwif->chipset == ide_unknown)
  1275. return hwif;
  1276. }
  1277. }
  1278. return NULL;
  1279. }
  1280. EXPORT_SYMBOL_GPL(ide_find_port_slot);
  1281. int ide_device_add_all(u8 *idx, const struct ide_port_info *d)
  1282. {
  1283. ide_hwif_t *hwif, *mate = NULL;
  1284. int i, rc = 0;
  1285. for (i = 0; i < MAX_HWIFS; i++) {
  1286. if (idx[i] == 0xff) {
  1287. mate = NULL;
  1288. continue;
  1289. }
  1290. hwif = &ide_hwifs[idx[i]];
  1291. ide_port_apply_params(hwif);
  1292. if (d == NULL) {
  1293. mate = NULL;
  1294. continue;
  1295. }
  1296. if (d->chipset != ide_etrax100 && (i & 1) && mate) {
  1297. hwif->mate = mate;
  1298. mate->mate = hwif;
  1299. }
  1300. mate = (i & 1) ? NULL : hwif;
  1301. ide_init_port(hwif, i & 1, d);
  1302. ide_port_cable_detect(hwif);
  1303. ide_port_init_devices(hwif);
  1304. }
  1305. for (i = 0; i < MAX_HWIFS; i++) {
  1306. if (idx[i] == 0xff)
  1307. continue;
  1308. hwif = &ide_hwifs[idx[i]];
  1309. if (ide_probe_port(hwif) == 0)
  1310. hwif->present = 1;
  1311. if (hwif->chipset != ide_4drives || !hwif->mate ||
  1312. !hwif->mate->present)
  1313. ide_register_port(hwif);
  1314. if (hwif->present)
  1315. ide_port_tune_devices(hwif);
  1316. }
  1317. for (i = 0; i < MAX_HWIFS; i++) {
  1318. if (idx[i] == 0xff)
  1319. continue;
  1320. hwif = &ide_hwifs[idx[i]];
  1321. if (hwif_init(hwif) == 0) {
  1322. printk(KERN_INFO "%s: failed to initialize IDE "
  1323. "interface\n", hwif->name);
  1324. hwif->present = 0;
  1325. rc = -1;
  1326. continue;
  1327. }
  1328. if (hwif->present)
  1329. ide_port_setup_devices(hwif);
  1330. ide_acpi_init(hwif);
  1331. if (hwif->present)
  1332. ide_acpi_port_init_devices(hwif);
  1333. }
  1334. for (i = 0; i < MAX_HWIFS; i++) {
  1335. if (idx[i] == 0xff)
  1336. continue;
  1337. hwif = &ide_hwifs[idx[i]];
  1338. if (hwif->chipset == ide_unknown)
  1339. hwif->chipset = ide_generic;
  1340. if (hwif->present)
  1341. hwif_register_devices(hwif);
  1342. }
  1343. for (i = 0; i < MAX_HWIFS; i++) {
  1344. if (idx[i] == 0xff)
  1345. continue;
  1346. hwif = &ide_hwifs[idx[i]];
  1347. ide_sysfs_register_port(hwif);
  1348. ide_proc_register_port(hwif);
  1349. if (hwif->present)
  1350. ide_proc_port_register_devices(hwif);
  1351. }
  1352. return rc;
  1353. }
  1354. EXPORT_SYMBOL_GPL(ide_device_add_all);
  1355. int ide_device_add(u8 idx[4], const struct ide_port_info *d)
  1356. {
  1357. u8 idx_all[MAX_HWIFS];
  1358. int i;
  1359. for (i = 0; i < MAX_HWIFS; i++)
  1360. idx_all[i] = (i < 4) ? idx[i] : 0xff;
  1361. return ide_device_add_all(idx_all, d);
  1362. }
  1363. EXPORT_SYMBOL_GPL(ide_device_add);
  1364. void ide_port_scan(ide_hwif_t *hwif)
  1365. {
  1366. ide_port_apply_params(hwif);
  1367. ide_port_cable_detect(hwif);
  1368. ide_port_init_devices(hwif);
  1369. if (ide_probe_port(hwif) < 0)
  1370. return;
  1371. hwif->present = 1;
  1372. ide_port_tune_devices(hwif);
  1373. ide_acpi_port_init_devices(hwif);
  1374. ide_port_setup_devices(hwif);
  1375. hwif_register_devices(hwif);
  1376. ide_proc_port_register_devices(hwif);
  1377. }
  1378. EXPORT_SYMBOL_GPL(ide_port_scan);
  1379. static void ide_legacy_init_one(u8 *idx, hw_regs_t *hw, u8 port_no,
  1380. const struct ide_port_info *d,
  1381. unsigned long config)
  1382. {
  1383. ide_hwif_t *hwif;
  1384. unsigned long base, ctl;
  1385. int irq;
  1386. if (port_no == 0) {
  1387. base = 0x1f0;
  1388. ctl = 0x3f6;
  1389. irq = 14;
  1390. } else {
  1391. base = 0x170;
  1392. ctl = 0x376;
  1393. irq = 15;
  1394. }
  1395. if (!request_region(base, 8, d->name)) {
  1396. printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
  1397. d->name, base, base + 7);
  1398. return;
  1399. }
  1400. if (!request_region(ctl, 1, d->name)) {
  1401. printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
  1402. d->name, ctl);
  1403. release_region(base, 8);
  1404. return;
  1405. }
  1406. ide_std_init_ports(hw, base, ctl);
  1407. hw->irq = irq;
  1408. hwif = ide_find_port_slot(d);
  1409. if (hwif) {
  1410. ide_init_port_hw(hwif, hw);
  1411. if (config)
  1412. hwif->config_data = config;
  1413. idx[port_no] = hwif->index;
  1414. }
  1415. }
  1416. int ide_legacy_device_add(const struct ide_port_info *d, unsigned long config)
  1417. {
  1418. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  1419. hw_regs_t hw[2];
  1420. memset(&hw, 0, sizeof(hw));
  1421. if ((d->host_flags & IDE_HFLAG_QD_2ND_PORT) == 0)
  1422. ide_legacy_init_one(idx, &hw[0], 0, d, config);
  1423. ide_legacy_init_one(idx, &hw[1], 1, d, config);
  1424. if (idx[0] == 0xff && idx[1] == 0xff &&
  1425. (d->host_flags & IDE_HFLAG_SINGLE))
  1426. return -ENOENT;
  1427. ide_device_add(idx, d);
  1428. return 0;
  1429. }
  1430. EXPORT_SYMBOL_GPL(ide_legacy_device_add);