ide-probe.c 38 KB

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