ide-probe.c 37 KB

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