ide-probe.c 38 KB

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