ide-probe.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. u16 *id = drive->id;
  49. id[ATA_ID_CUR_CYLS] = id[ATA_ID_CYLS] = drive->cyl;
  50. id[ATA_ID_CUR_HEADS] = id[ATA_ID_HEADS] = drive->head;
  51. id[ATA_ID_CUR_SECTORS] = id[ATA_ID_SECTORS] = drive->sect;
  52. }
  53. static void ide_disk_init_chs(ide_drive_t *drive)
  54. {
  55. u16 *id = drive->id;
  56. /* Extract geometry if we did not already have one for the drive */
  57. if (!drive->cyl || !drive->head || !drive->sect) {
  58. drive->cyl = drive->bios_cyl = id[ATA_ID_CYLS];
  59. drive->head = drive->bios_head = id[ATA_ID_HEADS];
  60. drive->sect = drive->bios_sect = id[ATA_ID_SECTORS];
  61. }
  62. /* Handle logical geometry translation by the drive */
  63. if (ata_id_current_chs_valid(id)) {
  64. drive->cyl = id[ATA_ID_CUR_CYLS];
  65. drive->head = id[ATA_ID_CUR_HEADS];
  66. drive->sect = id[ATA_ID_CUR_SECTORS];
  67. }
  68. /* Use physical geometry if what we have still makes no sense */
  69. if (drive->head > 16 && id[ATA_ID_HEADS] && id[ATA_ID_HEADS] <= 16) {
  70. drive->cyl = id[ATA_ID_CYLS];
  71. drive->head = id[ATA_ID_HEADS];
  72. drive->sect = id[ATA_ID_SECTORS];
  73. }
  74. }
  75. static void ide_disk_init_mult_count(ide_drive_t *drive)
  76. {
  77. u16 *id = drive->id;
  78. u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
  79. if (max_multsect) {
  80. if ((max_multsect / 2) > 1)
  81. id[ATA_ID_MULTSECT] = max_multsect | 0x100;
  82. else
  83. id[ATA_ID_MULTSECT] &= ~0x1ff;
  84. drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
  85. if (drive->mult_req)
  86. drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
  87. }
  88. }
  89. static void ide_classify_ata_dev(ide_drive_t *drive)
  90. {
  91. u16 *id = drive->id;
  92. char *m = (char *)&id[ATA_ID_PROD];
  93. int is_cfa = ata_id_is_cfa(id);
  94. /* CF devices are *not* removable in Linux definition of the term */
  95. if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
  96. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  97. drive->media = ide_disk;
  98. if (!ata_id_has_unload(drive->id))
  99. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  100. printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
  101. is_cfa ? "CFA" : "ATA");
  102. }
  103. static void ide_classify_atapi_dev(ide_drive_t *drive)
  104. {
  105. u16 *id = drive->id;
  106. char *m = (char *)&id[ATA_ID_PROD];
  107. u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
  108. printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
  109. switch (type) {
  110. case ide_floppy:
  111. if (!strstr(m, "CD-ROM")) {
  112. if (!strstr(m, "oppy") &&
  113. !strstr(m, "poyp") &&
  114. !strstr(m, "ZIP"))
  115. printk(KERN_CONT "cdrom or floppy?, assuming ");
  116. if (drive->media != ide_cdrom) {
  117. printk(KERN_CONT "FLOPPY");
  118. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  119. break;
  120. }
  121. }
  122. /* Early cdrom models used zero */
  123. type = ide_cdrom;
  124. case ide_cdrom:
  125. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  126. #ifdef CONFIG_PPC
  127. /* kludge for Apple PowerBook internal zip */
  128. if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
  129. printk(KERN_CONT "FLOPPY");
  130. type = ide_floppy;
  131. break;
  132. }
  133. #endif
  134. printk(KERN_CONT "CD/DVD-ROM");
  135. break;
  136. case ide_tape:
  137. printk(KERN_CONT "TAPE");
  138. break;
  139. case ide_optical:
  140. printk(KERN_CONT "OPTICAL");
  141. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  142. break;
  143. default:
  144. printk(KERN_CONT "UNKNOWN (type %d)", type);
  145. break;
  146. }
  147. printk(KERN_CONT " drive\n");
  148. drive->media = type;
  149. /* an ATAPI device ignores DRDY */
  150. drive->ready_stat = 0;
  151. if (ata_id_cdb_intr(id))
  152. drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
  153. drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
  154. /* we don't do head unloading on ATAPI devices */
  155. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  156. }
  157. /**
  158. * do_identify - identify a drive
  159. * @drive: drive to identify
  160. * @cmd: command used
  161. * @id: buffer for IDENTIFY data
  162. *
  163. * Called when we have issued a drive identify command to
  164. * read and parse the results. This function is run with
  165. * interrupts disabled.
  166. */
  167. static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
  168. {
  169. ide_hwif_t *hwif = drive->hwif;
  170. char *m = (char *)&id[ATA_ID_PROD];
  171. unsigned long flags;
  172. int bswap = 1;
  173. /* local CPU only; some systems need this */
  174. local_irq_save(flags);
  175. /* read 512 bytes of id info */
  176. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  177. local_irq_restore(flags);
  178. drive->dev_flags |= IDE_DFLAG_ID_READ;
  179. #ifdef DEBUG
  180. printk(KERN_INFO "%s: dumping identify data\n", drive->name);
  181. ide_dump_identify((u8 *)id);
  182. #endif
  183. ide_fix_driveid(id);
  184. /*
  185. * ATA_CMD_ID_ATA returns little-endian info,
  186. * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
  187. */
  188. if (cmd == ATA_CMD_ID_ATAPI) {
  189. if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
  190. (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
  191. (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
  192. /* Vertos drives may still be weird */
  193. bswap ^= 1;
  194. }
  195. ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
  196. ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
  197. ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
  198. /* we depend on this a lot! */
  199. m[ATA_ID_PROD_LEN - 1] = '\0';
  200. if (strstr(m, "E X A B Y T E N E S T"))
  201. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  202. else
  203. drive->dev_flags |= IDE_DFLAG_PRESENT;
  204. }
  205. /**
  206. * ide_dev_read_id - send ATA/ATAPI IDENTIFY command
  207. * @drive: drive to identify
  208. * @cmd: command to use
  209. * @id: buffer for IDENTIFY data
  210. * @irq_ctx: flag set when called from the IRQ context
  211. *
  212. * Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
  213. *
  214. * Returns: 0 device was identified
  215. * 1 device timed-out (no response to identify request)
  216. * 2 device aborted the command (refused to identify itself)
  217. */
  218. int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id, int irq_ctx)
  219. {
  220. ide_hwif_t *hwif = drive->hwif;
  221. struct ide_io_ports *io_ports = &hwif->io_ports;
  222. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  223. int use_altstatus = 0, rc;
  224. unsigned long timeout;
  225. u8 s = 0, a = 0;
  226. /*
  227. * Disable device IRQ. Otherwise we'll get spurious interrupts
  228. * during the identify phase that the IRQ handler isn't expecting.
  229. */
  230. if (io_ports->ctl_addr)
  231. tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
  232. /* take a deep breath */
  233. if (irq_ctx)
  234. mdelay(50);
  235. else
  236. msleep(50);
  237. if (io_ports->ctl_addr &&
  238. (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
  239. a = tp_ops->read_altstatus(hwif);
  240. s = tp_ops->read_status(hwif);
  241. if ((a ^ s) & ~ATA_SENSE)
  242. /* ancient Seagate drives, broken interfaces */
  243. printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
  244. "instead of ALTSTATUS(0x%02x)\n",
  245. drive->name, s, a);
  246. else
  247. /* use non-intrusive polling */
  248. use_altstatus = 1;
  249. }
  250. /* set features register for atapi
  251. * identify command to be sure of reply
  252. */
  253. if (cmd == ATA_CMD_ID_ATAPI) {
  254. struct ide_taskfile tf;
  255. memset(&tf, 0, sizeof(tf));
  256. /* disable DMA & overlap */
  257. tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
  258. }
  259. /* ask drive for ID */
  260. tp_ops->exec_command(hwif, cmd);
  261. timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
  262. /* wait for IRQ and ATA_DRQ */
  263. if (irq_ctx) {
  264. rc = __ide_wait_stat(drive, ATA_DRQ, BAD_R_STAT, timeout, &s);
  265. if (rc)
  266. return 1;
  267. } else {
  268. rc = ide_busy_sleep(drive, timeout, use_altstatus);
  269. if (rc)
  270. return 1;
  271. msleep(50);
  272. s = tp_ops->read_status(hwif);
  273. }
  274. if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
  275. /* drive returned ID */
  276. do_identify(drive, cmd, id);
  277. /* drive responded with ID */
  278. rc = 0;
  279. /* clear drive IRQ */
  280. (void)tp_ops->read_status(hwif);
  281. } else {
  282. /* drive refused ID */
  283. rc = 2;
  284. }
  285. return rc;
  286. }
  287. int ide_busy_sleep(ide_drive_t *drive, unsigned long timeout, int altstatus)
  288. {
  289. ide_hwif_t *hwif = drive->hwif;
  290. u8 stat;
  291. timeout += jiffies;
  292. do {
  293. msleep(50); /* give drive a breather */
  294. stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
  295. : hwif->tp_ops->read_status(hwif);
  296. if ((stat & ATA_BUSY) == 0)
  297. return 0;
  298. } while (time_before(jiffies, timeout));
  299. printk(KERN_ERR "%s: timeout in %s\n", drive->name, __func__);
  300. return 1; /* drive timed-out */
  301. }
  302. static u8 ide_read_device(ide_drive_t *drive)
  303. {
  304. struct ide_taskfile tf;
  305. drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
  306. return tf.device;
  307. }
  308. /**
  309. * do_probe - probe an IDE device
  310. * @drive: drive to probe
  311. * @cmd: command to use
  312. *
  313. * do_probe() has the difficult job of finding a drive if it exists,
  314. * without getting hung up if it doesn't exist, without trampling on
  315. * ethernet cards, and without leaving any IRQs dangling to haunt us later.
  316. *
  317. * If a drive is "known" to exist (from CMOS or kernel parameters),
  318. * but does not respond right away, the probe will "hang in there"
  319. * for the maximum wait time (about 30 seconds), otherwise it will
  320. * exit much more quickly.
  321. *
  322. * Returns: 0 device was identified
  323. * 1 device timed-out (no response to identify request)
  324. * 2 device aborted the command (refused to identify itself)
  325. * 3 bad status from device (possible for ATAPI drives)
  326. * 4 probe was not attempted because failure was obvious
  327. */
  328. static int do_probe (ide_drive_t *drive, u8 cmd)
  329. {
  330. ide_hwif_t *hwif = drive->hwif;
  331. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  332. u16 *id = drive->id;
  333. int rc;
  334. u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
  335. /* avoid waiting for inappropriate probes */
  336. if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
  337. return 4;
  338. #ifdef DEBUG
  339. printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
  340. drive->name, present, drive->media,
  341. (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
  342. #endif
  343. /* needed for some systems
  344. * (e.g. crw9624 as drive0 with disk as slave)
  345. */
  346. msleep(50);
  347. tp_ops->dev_select(drive);
  348. msleep(50);
  349. if (ide_read_device(drive) != drive->select && present == 0) {
  350. if (drive->dn & 1) {
  351. /* exit with drive0 selected */
  352. tp_ops->dev_select(hwif->devices[0]);
  353. /* allow ATA_BUSY to assert & clear */
  354. msleep(50);
  355. }
  356. /* no i/f present: mmm.. this should be a 4 -ml */
  357. return 3;
  358. }
  359. stat = tp_ops->read_status(hwif);
  360. if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
  361. present || cmd == ATA_CMD_ID_ATAPI) {
  362. rc = ide_dev_read_id(drive, cmd, id, 0);
  363. if (rc)
  364. /* failed: try again */
  365. rc = ide_dev_read_id(drive, cmd, id, 0);
  366. stat = tp_ops->read_status(hwif);
  367. if (stat == (ATA_BUSY | ATA_DRDY))
  368. return 4;
  369. if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
  370. printk(KERN_ERR "%s: no response (status = 0x%02x), "
  371. "resetting drive\n", drive->name, stat);
  372. msleep(50);
  373. tp_ops->dev_select(drive);
  374. msleep(50);
  375. tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
  376. (void)ide_busy_sleep(drive, WAIT_WORSTCASE, 0);
  377. rc = ide_dev_read_id(drive, cmd, id, 0);
  378. }
  379. /* ensure drive IRQ is clear */
  380. stat = tp_ops->read_status(hwif);
  381. if (rc == 1)
  382. printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
  383. drive->name, stat);
  384. } else {
  385. /* not present or maybe ATAPI */
  386. rc = 3;
  387. }
  388. if (drive->dn & 1) {
  389. /* exit with drive0 selected */
  390. tp_ops->dev_select(hwif->devices[0]);
  391. msleep(50);
  392. /* ensure drive irq is clear */
  393. (void)tp_ops->read_status(hwif);
  394. }
  395. return rc;
  396. }
  397. /**
  398. * probe_for_drives - upper level drive probe
  399. * @drive: drive to probe for
  400. *
  401. * probe_for_drive() tests for existence of a given drive using do_probe()
  402. * and presents things to the user as needed.
  403. *
  404. * Returns: 0 no device was found
  405. * 1 device was found
  406. * (note: IDE_DFLAG_PRESENT might still be not set)
  407. */
  408. static u8 probe_for_drive(ide_drive_t *drive)
  409. {
  410. char *m;
  411. int rc;
  412. u8 cmd;
  413. drive->dev_flags &= ~IDE_DFLAG_ID_READ;
  414. m = (char *)&drive->id[ATA_ID_PROD];
  415. strcpy(m, "UNKNOWN");
  416. /* skip probing? */
  417. if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
  418. /* if !(success||timed-out) */
  419. cmd = ATA_CMD_ID_ATA;
  420. rc = do_probe(drive, cmd);
  421. if (rc >= 2) {
  422. /* look for ATAPI device */
  423. cmd = ATA_CMD_ID_ATAPI;
  424. rc = do_probe(drive, cmd);
  425. }
  426. if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
  427. return 0;
  428. /* identification failed? */
  429. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
  430. if (drive->media == ide_disk) {
  431. printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
  432. drive->name, drive->cyl,
  433. drive->head, drive->sect);
  434. } else if (drive->media == ide_cdrom) {
  435. printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
  436. } else {
  437. /* nuke it */
  438. printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
  439. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  440. }
  441. } else {
  442. if (cmd == ATA_CMD_ID_ATAPI)
  443. ide_classify_atapi_dev(drive);
  444. else
  445. ide_classify_ata_dev(drive);
  446. }
  447. }
  448. if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
  449. return 0;
  450. /* The drive wasn't being helpful. Add generic info only */
  451. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
  452. generic_id(drive);
  453. return 1;
  454. }
  455. if (drive->media == ide_disk) {
  456. ide_disk_init_chs(drive);
  457. ide_disk_init_mult_count(drive);
  458. }
  459. return 1;
  460. }
  461. static void hwif_release_dev(struct device *dev)
  462. {
  463. ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
  464. complete(&hwif->gendev_rel_comp);
  465. }
  466. static int ide_register_port(ide_hwif_t *hwif)
  467. {
  468. int ret;
  469. /* register with global device tree */
  470. dev_set_name(&hwif->gendev, "%s", hwif->name);
  471. dev_set_drvdata(&hwif->gendev, hwif);
  472. if (hwif->gendev.parent == NULL)
  473. hwif->gendev.parent = hwif->dev;
  474. hwif->gendev.release = hwif_release_dev;
  475. ret = device_register(&hwif->gendev);
  476. if (ret < 0) {
  477. printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
  478. __func__, ret);
  479. goto out;
  480. }
  481. hwif->portdev = device_create(ide_port_class, &hwif->gendev,
  482. MKDEV(0, 0), hwif, "%s", hwif->name);
  483. if (IS_ERR(hwif->portdev)) {
  484. ret = PTR_ERR(hwif->portdev);
  485. device_unregister(&hwif->gendev);
  486. }
  487. out:
  488. return ret;
  489. }
  490. /**
  491. * ide_port_wait_ready - wait for port to become ready
  492. * @hwif: IDE port
  493. *
  494. * This is needed on some PPCs and a bunch of BIOS-less embedded
  495. * platforms. Typical cases are:
  496. *
  497. * - The firmware hard reset the disk before booting the kernel,
  498. * the drive is still doing it's poweron-reset sequence, that
  499. * can take up to 30 seconds.
  500. *
  501. * - The firmware does nothing (or no firmware), the device is
  502. * still in POST state (same as above actually).
  503. *
  504. * - Some CD/DVD/Writer combo drives tend to drive the bus during
  505. * their reset sequence even when they are non-selected slave
  506. * devices, thus preventing discovery of the main HD.
  507. *
  508. * Doing this wait-for-non-busy should not harm any existing
  509. * configuration and fix some issues like the above.
  510. *
  511. * BenH.
  512. *
  513. * Returns 0 on success, error code (< 0) otherwise.
  514. */
  515. static int ide_port_wait_ready(ide_hwif_t *hwif)
  516. {
  517. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  518. ide_drive_t *drive;
  519. int i, rc;
  520. printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
  521. /* Let HW settle down a bit from whatever init state we
  522. * come from */
  523. mdelay(2);
  524. /* Wait for BSY bit to go away, spec timeout is 30 seconds,
  525. * I know of at least one disk who takes 31 seconds, I use 35
  526. * here to be safe
  527. */
  528. rc = ide_wait_not_busy(hwif, 35000);
  529. if (rc)
  530. return rc;
  531. /* Now make sure both master & slave are ready */
  532. ide_port_for_each_dev(i, drive, hwif) {
  533. /* Ignore disks that we will not probe for later. */
  534. if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
  535. (drive->dev_flags & IDE_DFLAG_PRESENT)) {
  536. tp_ops->dev_select(drive);
  537. tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
  538. mdelay(2);
  539. rc = ide_wait_not_busy(hwif, 35000);
  540. if (rc)
  541. goto out;
  542. } else
  543. printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
  544. drive->name);
  545. }
  546. out:
  547. /* Exit function with master reselected (let's be sane) */
  548. if (i)
  549. tp_ops->dev_select(hwif->devices[0]);
  550. return rc;
  551. }
  552. /**
  553. * ide_undecoded_slave - look for bad CF adapters
  554. * @dev1: slave device
  555. *
  556. * Analyse the drives on the interface and attempt to decide if we
  557. * have the same drive viewed twice. This occurs with crap CF adapters
  558. * and PCMCIA sometimes.
  559. */
  560. void ide_undecoded_slave(ide_drive_t *dev1)
  561. {
  562. ide_drive_t *dev0 = dev1->hwif->devices[0];
  563. if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
  564. return;
  565. /* If the models don't match they are not the same product */
  566. if (strcmp((char *)&dev0->id[ATA_ID_PROD],
  567. (char *)&dev1->id[ATA_ID_PROD]))
  568. return;
  569. /* Serial numbers do not match */
  570. if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
  571. (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
  572. return;
  573. /* No serial number, thankfully very rare for CF */
  574. if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
  575. return;
  576. /* Appears to be an IDE flash adapter with decode bugs */
  577. printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
  578. dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
  579. }
  580. EXPORT_SYMBOL_GPL(ide_undecoded_slave);
  581. static int ide_probe_port(ide_hwif_t *hwif)
  582. {
  583. ide_drive_t *drive;
  584. unsigned int irqd;
  585. int i, rc = -ENODEV;
  586. BUG_ON(hwif->present);
  587. if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
  588. (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
  589. return -EACCES;
  590. /*
  591. * We must always disable IRQ, as probe_for_drive will assert IRQ, but
  592. * we'll install our IRQ driver much later...
  593. */
  594. irqd = hwif->irq;
  595. if (irqd)
  596. disable_irq(hwif->irq);
  597. if (ide_port_wait_ready(hwif) == -EBUSY)
  598. printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
  599. /*
  600. * Second drive should only exist if first drive was found,
  601. * but a lot of cdrom drives are configured as single slaves.
  602. */
  603. ide_port_for_each_dev(i, drive, hwif) {
  604. (void) probe_for_drive(drive);
  605. if (drive->dev_flags & IDE_DFLAG_PRESENT)
  606. rc = 0;
  607. }
  608. /*
  609. * Use cached IRQ number. It might be (and is...) changed by probe
  610. * code above
  611. */
  612. if (irqd)
  613. enable_irq(irqd);
  614. return rc;
  615. }
  616. static void ide_port_tune_devices(ide_hwif_t *hwif)
  617. {
  618. const struct ide_port_ops *port_ops = hwif->port_ops;
  619. ide_drive_t *drive;
  620. int i;
  621. ide_port_for_each_present_dev(i, drive, hwif) {
  622. ide_check_nien_quirk_list(drive);
  623. if (port_ops && port_ops->quirkproc)
  624. port_ops->quirkproc(drive);
  625. }
  626. ide_port_for_each_present_dev(i, drive, hwif) {
  627. ide_set_max_pio(drive);
  628. drive->dev_flags |= IDE_DFLAG_NICE1;
  629. if (hwif->dma_ops)
  630. ide_set_dma(drive);
  631. }
  632. }
  633. /*
  634. * init request queue
  635. */
  636. static int ide_init_queue(ide_drive_t *drive)
  637. {
  638. struct request_queue *q;
  639. ide_hwif_t *hwif = drive->hwif;
  640. int max_sectors = 256;
  641. int max_sg_entries = PRD_ENTRIES;
  642. /*
  643. * Our default set up assumes the normal IDE case,
  644. * that is 64K segmenting, standard PRD setup
  645. * and LBA28. Some drivers then impose their own
  646. * limits and LBA48 we could raise it but as yet
  647. * do not.
  648. */
  649. q = blk_init_queue_node(do_ide_request, NULL, hwif_to_node(hwif));
  650. if (!q)
  651. return 1;
  652. q->queuedata = drive;
  653. blk_queue_segment_boundary(q, 0xffff);
  654. if (hwif->rqsize < max_sectors)
  655. max_sectors = hwif->rqsize;
  656. blk_queue_max_hw_sectors(q, max_sectors);
  657. #ifdef CONFIG_PCI
  658. /* When we have an IOMMU, we may have a problem where pci_map_sg()
  659. * creates segments that don't completely match our boundary
  660. * requirements and thus need to be broken up again. Because it
  661. * doesn't align properly either, we may actually have to break up
  662. * to more segments than what was we got in the first place, a max
  663. * worst case is twice as many.
  664. * This will be fixed once we teach pci_map_sg() about our boundary
  665. * requirements, hopefully soon. *FIXME*
  666. */
  667. if (!PCI_DMA_BUS_IS_PHYS)
  668. max_sg_entries >>= 1;
  669. #endif /* CONFIG_PCI */
  670. blk_queue_max_segments(q, max_sg_entries);
  671. /* assign drive queue */
  672. drive->queue = q;
  673. /* needs drive->queue to be set */
  674. ide_toggle_bounce(drive, 1);
  675. return 0;
  676. }
  677. static DEFINE_MUTEX(ide_cfg_mtx);
  678. /*
  679. * For any present drive:
  680. * - allocate the block device queue
  681. */
  682. static int ide_port_setup_devices(ide_hwif_t *hwif)
  683. {
  684. ide_drive_t *drive;
  685. int i, j = 0;
  686. mutex_lock(&ide_cfg_mtx);
  687. ide_port_for_each_present_dev(i, drive, hwif) {
  688. if (ide_init_queue(drive)) {
  689. printk(KERN_ERR "ide: failed to init %s\n",
  690. drive->name);
  691. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  692. continue;
  693. }
  694. j++;
  695. }
  696. mutex_unlock(&ide_cfg_mtx);
  697. return j;
  698. }
  699. static void ide_host_enable_irqs(struct ide_host *host)
  700. {
  701. ide_hwif_t *hwif;
  702. int i;
  703. ide_host_for_each_port(i, hwif, host) {
  704. if (hwif == NULL)
  705. continue;
  706. /* clear any pending IRQs */
  707. hwif->tp_ops->read_status(hwif);
  708. /* unmask IRQs */
  709. if (hwif->io_ports.ctl_addr)
  710. hwif->tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
  711. }
  712. }
  713. /*
  714. * This routine sets up the IRQ for an IDE interface.
  715. */
  716. static int init_irq (ide_hwif_t *hwif)
  717. {
  718. struct ide_io_ports *io_ports = &hwif->io_ports;
  719. struct ide_host *host = hwif->host;
  720. irq_handler_t irq_handler = host->irq_handler;
  721. int sa = host->irq_flags;
  722. if (irq_handler == NULL)
  723. irq_handler = ide_intr;
  724. if (!host->get_lock)
  725. if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
  726. goto out_up;
  727. #if !defined(__mc68000__)
  728. printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
  729. io_ports->data_addr, io_ports->status_addr,
  730. io_ports->ctl_addr, hwif->irq);
  731. #else
  732. printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
  733. io_ports->data_addr, hwif->irq);
  734. #endif /* __mc68000__ */
  735. if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
  736. printk(KERN_CONT " (serialized)");
  737. printk(KERN_CONT "\n");
  738. return 0;
  739. out_up:
  740. return 1;
  741. }
  742. static int ata_lock(dev_t dev, void *data)
  743. {
  744. /* FIXME: we want to pin hwif down */
  745. return 0;
  746. }
  747. static struct kobject *ata_probe(dev_t dev, int *part, void *data)
  748. {
  749. ide_hwif_t *hwif = data;
  750. int unit = *part >> PARTN_BITS;
  751. ide_drive_t *drive = hwif->devices[unit];
  752. if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
  753. return NULL;
  754. if (drive->media == ide_disk)
  755. request_module("ide-disk");
  756. if (drive->media == ide_cdrom || drive->media == ide_optical)
  757. request_module("ide-cd");
  758. if (drive->media == ide_tape)
  759. request_module("ide-tape");
  760. if (drive->media == ide_floppy)
  761. request_module("ide-floppy");
  762. return NULL;
  763. }
  764. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  765. {
  766. struct gendisk *p = data;
  767. *part &= (1 << PARTN_BITS) - 1;
  768. return &disk_to_dev(p)->kobj;
  769. }
  770. static int exact_lock(dev_t dev, void *data)
  771. {
  772. struct gendisk *p = data;
  773. if (!get_disk(p))
  774. return -1;
  775. return 0;
  776. }
  777. void ide_register_region(struct gendisk *disk)
  778. {
  779. blk_register_region(MKDEV(disk->major, disk->first_minor),
  780. disk->minors, NULL, exact_match, exact_lock, disk);
  781. }
  782. EXPORT_SYMBOL_GPL(ide_register_region);
  783. void ide_unregister_region(struct gendisk *disk)
  784. {
  785. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  786. disk->minors);
  787. }
  788. EXPORT_SYMBOL_GPL(ide_unregister_region);
  789. void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
  790. {
  791. ide_hwif_t *hwif = drive->hwif;
  792. unsigned int unit = drive->dn & 1;
  793. disk->major = hwif->major;
  794. disk->first_minor = unit << PARTN_BITS;
  795. sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
  796. disk->queue = drive->queue;
  797. }
  798. EXPORT_SYMBOL_GPL(ide_init_disk);
  799. static void drive_release_dev (struct device *dev)
  800. {
  801. ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
  802. ide_proc_unregister_device(drive);
  803. blk_cleanup_queue(drive->queue);
  804. drive->queue = NULL;
  805. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  806. complete(&drive->gendev_rel_comp);
  807. }
  808. static int hwif_init(ide_hwif_t *hwif)
  809. {
  810. if (!hwif->irq) {
  811. printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
  812. return 0;
  813. }
  814. if (register_blkdev(hwif->major, hwif->name))
  815. return 0;
  816. if (!hwif->sg_max_nents)
  817. hwif->sg_max_nents = PRD_ENTRIES;
  818. hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
  819. GFP_KERNEL);
  820. if (!hwif->sg_table) {
  821. printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
  822. goto out;
  823. }
  824. sg_init_table(hwif->sg_table, hwif->sg_max_nents);
  825. if (init_irq(hwif)) {
  826. printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
  827. hwif->name, hwif->irq);
  828. goto out;
  829. }
  830. blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
  831. THIS_MODULE, ata_probe, ata_lock, hwif);
  832. return 1;
  833. out:
  834. unregister_blkdev(hwif->major, hwif->name);
  835. return 0;
  836. }
  837. static void hwif_register_devices(ide_hwif_t *hwif)
  838. {
  839. ide_drive_t *drive;
  840. unsigned int i;
  841. ide_port_for_each_present_dev(i, drive, hwif) {
  842. struct device *dev = &drive->gendev;
  843. int ret;
  844. dev_set_name(dev, "%u.%u", hwif->index, i);
  845. dev_set_drvdata(dev, drive);
  846. dev->parent = &hwif->gendev;
  847. dev->bus = &ide_bus_type;
  848. dev->release = drive_release_dev;
  849. ret = device_register(dev);
  850. if (ret < 0)
  851. printk(KERN_WARNING "IDE: %s: device_register error: "
  852. "%d\n", __func__, ret);
  853. }
  854. }
  855. static void ide_port_init_devices(ide_hwif_t *hwif)
  856. {
  857. const struct ide_port_ops *port_ops = hwif->port_ops;
  858. ide_drive_t *drive;
  859. int i;
  860. ide_port_for_each_dev(i, drive, hwif) {
  861. drive->dn = i + hwif->channel * 2;
  862. if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
  863. drive->io_32bit = 1;
  864. if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
  865. drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
  866. if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
  867. drive->dev_flags |= IDE_DFLAG_UNMASK;
  868. if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
  869. drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
  870. drive->pio_mode = XFER_PIO_0;
  871. if (port_ops && port_ops->init_dev)
  872. port_ops->init_dev(drive);
  873. }
  874. }
  875. static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
  876. const struct ide_port_info *d)
  877. {
  878. hwif->channel = port;
  879. hwif->chipset = d->chipset ? d->chipset : ide_pci;
  880. if (d->init_iops)
  881. d->init_iops(hwif);
  882. /* ->host_flags may be set by ->init_iops (or even earlier...) */
  883. hwif->host_flags |= d->host_flags;
  884. hwif->pio_mask = d->pio_mask;
  885. if (d->tp_ops)
  886. hwif->tp_ops = d->tp_ops;
  887. /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
  888. if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
  889. hwif->port_ops = d->port_ops;
  890. hwif->swdma_mask = d->swdma_mask;
  891. hwif->mwdma_mask = d->mwdma_mask;
  892. hwif->ultra_mask = d->udma_mask;
  893. if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
  894. int rc;
  895. hwif->dma_ops = d->dma_ops;
  896. if (d->init_dma)
  897. rc = d->init_dma(hwif, d);
  898. else
  899. rc = ide_hwif_setup_dma(hwif, d);
  900. if (rc < 0) {
  901. printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
  902. hwif->dma_ops = NULL;
  903. hwif->dma_base = 0;
  904. hwif->swdma_mask = 0;
  905. hwif->mwdma_mask = 0;
  906. hwif->ultra_mask = 0;
  907. }
  908. }
  909. if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
  910. ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
  911. hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
  912. if (d->max_sectors)
  913. hwif->rqsize = d->max_sectors;
  914. else {
  915. if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
  916. (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
  917. hwif->rqsize = 256;
  918. else
  919. hwif->rqsize = 65536;
  920. }
  921. /* call chipset specific routine for each enabled port */
  922. if (d->init_hwif)
  923. d->init_hwif(hwif);
  924. }
  925. static void ide_port_cable_detect(ide_hwif_t *hwif)
  926. {
  927. const struct ide_port_ops *port_ops = hwif->port_ops;
  928. if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
  929. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  930. hwif->cbl = port_ops->cable_detect(hwif);
  931. }
  932. }
  933. static const u8 ide_hwif_to_major[] =
  934. { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
  935. IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
  936. static void ide_port_init_devices_data(ide_hwif_t *hwif)
  937. {
  938. ide_drive_t *drive;
  939. int i;
  940. ide_port_for_each_dev(i, drive, hwif) {
  941. u8 j = (hwif->index * MAX_DRIVES) + i;
  942. u16 *saved_id = drive->id;
  943. memset(drive, 0, sizeof(*drive));
  944. memset(saved_id, 0, SECTOR_SIZE);
  945. drive->id = saved_id;
  946. drive->media = ide_disk;
  947. drive->select = (i << 4) | ATA_DEVICE_OBS;
  948. drive->hwif = hwif;
  949. drive->ready_stat = ATA_DRDY;
  950. drive->bad_wstat = BAD_W_STAT;
  951. drive->special_flags = IDE_SFLAG_RECALIBRATE |
  952. IDE_SFLAG_SET_GEOMETRY;
  953. drive->name[0] = 'h';
  954. drive->name[1] = 'd';
  955. drive->name[2] = 'a' + j;
  956. drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
  957. INIT_LIST_HEAD(&drive->list);
  958. init_completion(&drive->gendev_rel_comp);
  959. }
  960. }
  961. static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
  962. {
  963. /* fill in any non-zero initial values */
  964. hwif->index = index;
  965. hwif->major = ide_hwif_to_major[index];
  966. hwif->name[0] = 'i';
  967. hwif->name[1] = 'd';
  968. hwif->name[2] = 'e';
  969. hwif->name[3] = '0' + index;
  970. spin_lock_init(&hwif->lock);
  971. init_timer(&hwif->timer);
  972. hwif->timer.function = &ide_timer_expiry;
  973. hwif->timer.data = (unsigned long)hwif;
  974. init_completion(&hwif->gendev_rel_comp);
  975. hwif->tp_ops = &default_tp_ops;
  976. ide_port_init_devices_data(hwif);
  977. }
  978. static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
  979. {
  980. memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
  981. hwif->irq = hw->irq;
  982. hwif->dev = hw->dev;
  983. hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
  984. hwif->config_data = hw->config;
  985. }
  986. static unsigned int ide_indexes;
  987. /**
  988. * ide_find_port_slot - find free port slot
  989. * @d: IDE port info
  990. *
  991. * Return the new port slot index or -ENOENT if we are out of free slots.
  992. */
  993. static int ide_find_port_slot(const struct ide_port_info *d)
  994. {
  995. int idx = -ENOENT;
  996. u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
  997. u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
  998. /*
  999. * Claim an unassigned slot.
  1000. *
  1001. * Give preference to claiming other slots before claiming ide0/ide1,
  1002. * just in case there's another interface yet-to-be-scanned
  1003. * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
  1004. *
  1005. * Unless there is a bootable card that does not use the standard
  1006. * ports 0x1f0/0x170 (the ide0/ide1 defaults).
  1007. */
  1008. mutex_lock(&ide_cfg_mtx);
  1009. if (bootable) {
  1010. if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
  1011. idx = ffz(ide_indexes | i);
  1012. } else {
  1013. if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
  1014. idx = ffz(ide_indexes | 3);
  1015. else if ((ide_indexes & 3) != 3)
  1016. idx = ffz(ide_indexes);
  1017. }
  1018. if (idx >= 0)
  1019. ide_indexes |= (1 << idx);
  1020. mutex_unlock(&ide_cfg_mtx);
  1021. return idx;
  1022. }
  1023. static void ide_free_port_slot(int idx)
  1024. {
  1025. mutex_lock(&ide_cfg_mtx);
  1026. ide_indexes &= ~(1 << idx);
  1027. mutex_unlock(&ide_cfg_mtx);
  1028. }
  1029. static void ide_port_free_devices(ide_hwif_t *hwif)
  1030. {
  1031. ide_drive_t *drive;
  1032. int i;
  1033. ide_port_for_each_dev(i, drive, hwif) {
  1034. kfree(drive->id);
  1035. kfree(drive);
  1036. }
  1037. }
  1038. static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
  1039. {
  1040. int i;
  1041. for (i = 0; i < MAX_DRIVES; i++) {
  1042. ide_drive_t *drive;
  1043. drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
  1044. if (drive == NULL)
  1045. goto out_nomem;
  1046. /*
  1047. * In order to keep things simple we have an id
  1048. * block for all drives at all times. If the device
  1049. * is pre ATA or refuses ATA/ATAPI identify we
  1050. * will add faked data to this.
  1051. *
  1052. * Also note that 0 everywhere means "can't do X"
  1053. */
  1054. drive->id = kzalloc_node(SECTOR_SIZE, GFP_KERNEL, node);
  1055. if (drive->id == NULL)
  1056. goto out_nomem;
  1057. hwif->devices[i] = drive;
  1058. }
  1059. return 0;
  1060. out_nomem:
  1061. ide_port_free_devices(hwif);
  1062. return -ENOMEM;
  1063. }
  1064. struct ide_host *ide_host_alloc(const struct ide_port_info *d,
  1065. struct ide_hw **hws, unsigned int n_ports)
  1066. {
  1067. struct ide_host *host;
  1068. struct device *dev = hws[0] ? hws[0]->dev : NULL;
  1069. int node = dev ? dev_to_node(dev) : -1;
  1070. int i;
  1071. host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
  1072. if (host == NULL)
  1073. return NULL;
  1074. for (i = 0; i < n_ports; i++) {
  1075. ide_hwif_t *hwif;
  1076. int idx;
  1077. if (hws[i] == NULL)
  1078. continue;
  1079. hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
  1080. if (hwif == NULL)
  1081. continue;
  1082. if (ide_port_alloc_devices(hwif, node) < 0) {
  1083. kfree(hwif);
  1084. continue;
  1085. }
  1086. idx = ide_find_port_slot(d);
  1087. if (idx < 0) {
  1088. printk(KERN_ERR "%s: no free slot for interface\n",
  1089. d ? d->name : "ide");
  1090. ide_port_free_devices(hwif);
  1091. kfree(hwif);
  1092. continue;
  1093. }
  1094. ide_init_port_data(hwif, idx);
  1095. hwif->host = host;
  1096. host->ports[i] = hwif;
  1097. host->n_ports++;
  1098. }
  1099. if (host->n_ports == 0) {
  1100. kfree(host);
  1101. return NULL;
  1102. }
  1103. host->dev[0] = dev;
  1104. if (d) {
  1105. host->init_chipset = d->init_chipset;
  1106. host->get_lock = d->get_lock;
  1107. host->release_lock = d->release_lock;
  1108. host->host_flags = d->host_flags;
  1109. host->irq_flags = d->irq_flags;
  1110. }
  1111. return host;
  1112. }
  1113. EXPORT_SYMBOL_GPL(ide_host_alloc);
  1114. static void ide_port_free(ide_hwif_t *hwif)
  1115. {
  1116. ide_port_free_devices(hwif);
  1117. ide_free_port_slot(hwif->index);
  1118. kfree(hwif);
  1119. }
  1120. static void ide_disable_port(ide_hwif_t *hwif)
  1121. {
  1122. struct ide_host *host = hwif->host;
  1123. int i;
  1124. printk(KERN_INFO "%s: disabling port\n", hwif->name);
  1125. for (i = 0; i < MAX_HOST_PORTS; i++) {
  1126. if (host->ports[i] == hwif) {
  1127. host->ports[i] = NULL;
  1128. host->n_ports--;
  1129. }
  1130. }
  1131. ide_port_free(hwif);
  1132. }
  1133. int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
  1134. struct ide_hw **hws)
  1135. {
  1136. ide_hwif_t *hwif, *mate = NULL;
  1137. int i, j = 0;
  1138. ide_host_for_each_port(i, hwif, host) {
  1139. if (hwif == NULL) {
  1140. mate = NULL;
  1141. continue;
  1142. }
  1143. ide_init_port_hw(hwif, hws[i]);
  1144. ide_port_apply_params(hwif);
  1145. if ((i & 1) && mate) {
  1146. hwif->mate = mate;
  1147. mate->mate = hwif;
  1148. }
  1149. mate = (i & 1) ? NULL : hwif;
  1150. ide_init_port(hwif, i & 1, d);
  1151. ide_port_cable_detect(hwif);
  1152. hwif->port_flags |= IDE_PFLAG_PROBING;
  1153. ide_port_init_devices(hwif);
  1154. }
  1155. ide_host_for_each_port(i, hwif, host) {
  1156. if (hwif == NULL)
  1157. continue;
  1158. if (ide_probe_port(hwif) == 0)
  1159. hwif->present = 1;
  1160. hwif->port_flags &= ~IDE_PFLAG_PROBING;
  1161. if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
  1162. hwif->mate == NULL || hwif->mate->present == 0) {
  1163. if (ide_register_port(hwif)) {
  1164. ide_disable_port(hwif);
  1165. continue;
  1166. }
  1167. }
  1168. if (hwif->present)
  1169. ide_port_tune_devices(hwif);
  1170. }
  1171. ide_host_enable_irqs(host);
  1172. ide_host_for_each_port(i, hwif, host) {
  1173. if (hwif == NULL)
  1174. continue;
  1175. if (hwif_init(hwif) == 0) {
  1176. printk(KERN_INFO "%s: failed to initialize IDE "
  1177. "interface\n", hwif->name);
  1178. device_unregister(&hwif->gendev);
  1179. ide_disable_port(hwif);
  1180. continue;
  1181. }
  1182. if (hwif->present)
  1183. if (ide_port_setup_devices(hwif) == 0) {
  1184. hwif->present = 0;
  1185. continue;
  1186. }
  1187. j++;
  1188. ide_acpi_init_port(hwif);
  1189. if (hwif->present)
  1190. ide_acpi_port_init_devices(hwif);
  1191. }
  1192. ide_host_for_each_port(i, hwif, host) {
  1193. if (hwif == NULL)
  1194. continue;
  1195. ide_sysfs_register_port(hwif);
  1196. ide_proc_register_port(hwif);
  1197. if (hwif->present) {
  1198. ide_proc_port_register_devices(hwif);
  1199. hwif_register_devices(hwif);
  1200. }
  1201. }
  1202. return j ? 0 : -1;
  1203. }
  1204. EXPORT_SYMBOL_GPL(ide_host_register);
  1205. int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
  1206. unsigned int n_ports, struct ide_host **hostp)
  1207. {
  1208. struct ide_host *host;
  1209. int rc;
  1210. host = ide_host_alloc(d, hws, n_ports);
  1211. if (host == NULL)
  1212. return -ENOMEM;
  1213. rc = ide_host_register(host, d, hws);
  1214. if (rc) {
  1215. ide_host_free(host);
  1216. return rc;
  1217. }
  1218. if (hostp)
  1219. *hostp = host;
  1220. return 0;
  1221. }
  1222. EXPORT_SYMBOL_GPL(ide_host_add);
  1223. static void __ide_port_unregister_devices(ide_hwif_t *hwif)
  1224. {
  1225. ide_drive_t *drive;
  1226. int i;
  1227. ide_port_for_each_present_dev(i, drive, hwif) {
  1228. device_unregister(&drive->gendev);
  1229. wait_for_completion(&drive->gendev_rel_comp);
  1230. }
  1231. }
  1232. void ide_port_unregister_devices(ide_hwif_t *hwif)
  1233. {
  1234. mutex_lock(&ide_cfg_mtx);
  1235. __ide_port_unregister_devices(hwif);
  1236. hwif->present = 0;
  1237. ide_port_init_devices_data(hwif);
  1238. mutex_unlock(&ide_cfg_mtx);
  1239. }
  1240. EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
  1241. /**
  1242. * ide_unregister - free an IDE interface
  1243. * @hwif: IDE interface
  1244. *
  1245. * Perform the final unregister of an IDE interface.
  1246. *
  1247. * Locking:
  1248. * The caller must not hold the IDE locks.
  1249. *
  1250. * It is up to the caller to be sure there is no pending I/O here,
  1251. * and that the interface will not be reopened (present/vanishing
  1252. * locking isn't yet done BTW).
  1253. */
  1254. static void ide_unregister(ide_hwif_t *hwif)
  1255. {
  1256. BUG_ON(in_interrupt());
  1257. BUG_ON(irqs_disabled());
  1258. mutex_lock(&ide_cfg_mtx);
  1259. if (hwif->present) {
  1260. __ide_port_unregister_devices(hwif);
  1261. hwif->present = 0;
  1262. }
  1263. ide_proc_unregister_port(hwif);
  1264. if (!hwif->host->get_lock)
  1265. free_irq(hwif->irq, hwif);
  1266. device_unregister(hwif->portdev);
  1267. device_unregister(&hwif->gendev);
  1268. wait_for_completion(&hwif->gendev_rel_comp);
  1269. /*
  1270. * Remove us from the kernel's knowledge
  1271. */
  1272. blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
  1273. kfree(hwif->sg_table);
  1274. unregister_blkdev(hwif->major, hwif->name);
  1275. ide_release_dma_engine(hwif);
  1276. mutex_unlock(&ide_cfg_mtx);
  1277. }
  1278. void ide_host_free(struct ide_host *host)
  1279. {
  1280. ide_hwif_t *hwif;
  1281. int i;
  1282. ide_host_for_each_port(i, hwif, host) {
  1283. if (hwif)
  1284. ide_port_free(hwif);
  1285. }
  1286. kfree(host);
  1287. }
  1288. EXPORT_SYMBOL_GPL(ide_host_free);
  1289. void ide_host_remove(struct ide_host *host)
  1290. {
  1291. ide_hwif_t *hwif;
  1292. int i;
  1293. ide_host_for_each_port(i, hwif, host) {
  1294. if (hwif)
  1295. ide_unregister(hwif);
  1296. }
  1297. ide_host_free(host);
  1298. }
  1299. EXPORT_SYMBOL_GPL(ide_host_remove);
  1300. void ide_port_scan(ide_hwif_t *hwif)
  1301. {
  1302. int rc;
  1303. ide_port_apply_params(hwif);
  1304. ide_port_cable_detect(hwif);
  1305. hwif->port_flags |= IDE_PFLAG_PROBING;
  1306. ide_port_init_devices(hwif);
  1307. rc = ide_probe_port(hwif);
  1308. hwif->port_flags &= ~IDE_PFLAG_PROBING;
  1309. if (rc < 0)
  1310. return;
  1311. hwif->present = 1;
  1312. ide_port_tune_devices(hwif);
  1313. ide_port_setup_devices(hwif);
  1314. ide_acpi_port_init_devices(hwif);
  1315. hwif_register_devices(hwif);
  1316. ide_proc_port_register_devices(hwif);
  1317. }
  1318. EXPORT_SYMBOL_GPL(ide_port_scan);