ide-probe.c 41 KB

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