ide-probe.c 38 KB

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