ide-probe.c 40 KB

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