libata-sff.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876
  1. /*
  2. * libata-sff.c - helper library for PCI IDE BMDMA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2006 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/pci.h>
  36. #include <linux/libata.h>
  37. #include <linux/highmem.h>
  38. #include "libata.h"
  39. const struct ata_port_operations ata_sff_port_ops = {
  40. .inherits = &ata_base_port_ops,
  41. .qc_prep = ata_sff_qc_prep,
  42. .qc_issue = ata_sff_qc_issue,
  43. .qc_fill_rtf = ata_sff_qc_fill_rtf,
  44. .freeze = ata_sff_freeze,
  45. .thaw = ata_sff_thaw,
  46. .prereset = ata_sff_prereset,
  47. .softreset = ata_sff_softreset,
  48. .hardreset = sata_sff_hardreset,
  49. .postreset = ata_sff_postreset,
  50. .error_handler = ata_sff_error_handler,
  51. .post_internal_cmd = ata_sff_post_internal_cmd,
  52. .sff_dev_select = ata_sff_dev_select,
  53. .sff_check_status = ata_sff_check_status,
  54. .sff_tf_load = ata_sff_tf_load,
  55. .sff_tf_read = ata_sff_tf_read,
  56. .sff_exec_command = ata_sff_exec_command,
  57. .sff_data_xfer = ata_sff_data_xfer,
  58. .sff_irq_on = ata_sff_irq_on,
  59. .sff_irq_clear = ata_sff_irq_clear,
  60. .port_start = ata_sff_port_start,
  61. };
  62. const struct ata_port_operations ata_bmdma_port_ops = {
  63. .inherits = &ata_sff_port_ops,
  64. .mode_filter = ata_bmdma_mode_filter,
  65. .bmdma_setup = ata_bmdma_setup,
  66. .bmdma_start = ata_bmdma_start,
  67. .bmdma_stop = ata_bmdma_stop,
  68. .bmdma_status = ata_bmdma_status,
  69. };
  70. /**
  71. * ata_fill_sg - Fill PCI IDE PRD table
  72. * @qc: Metadata associated with taskfile to be transferred
  73. *
  74. * Fill PCI IDE PRD (scatter-gather) table with segments
  75. * associated with the current disk command.
  76. *
  77. * LOCKING:
  78. * spin_lock_irqsave(host lock)
  79. *
  80. */
  81. static void ata_fill_sg(struct ata_queued_cmd *qc)
  82. {
  83. struct ata_port *ap = qc->ap;
  84. struct scatterlist *sg;
  85. unsigned int si, pi;
  86. pi = 0;
  87. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  88. u32 addr, offset;
  89. u32 sg_len, len;
  90. /* determine if physical DMA addr spans 64K boundary.
  91. * Note h/w doesn't support 64-bit, so we unconditionally
  92. * truncate dma_addr_t to u32.
  93. */
  94. addr = (u32) sg_dma_address(sg);
  95. sg_len = sg_dma_len(sg);
  96. while (sg_len) {
  97. offset = addr & 0xffff;
  98. len = sg_len;
  99. if ((offset + sg_len) > 0x10000)
  100. len = 0x10000 - offset;
  101. ap->prd[pi].addr = cpu_to_le32(addr);
  102. ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
  103. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  104. pi++;
  105. sg_len -= len;
  106. addr += len;
  107. }
  108. }
  109. ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  110. }
  111. /**
  112. * ata_fill_sg_dumb - Fill PCI IDE PRD table
  113. * @qc: Metadata associated with taskfile to be transferred
  114. *
  115. * Fill PCI IDE PRD (scatter-gather) table with segments
  116. * associated with the current disk command. Perform the fill
  117. * so that we avoid writing any length 64K records for
  118. * controllers that don't follow the spec.
  119. *
  120. * LOCKING:
  121. * spin_lock_irqsave(host lock)
  122. *
  123. */
  124. static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
  125. {
  126. struct ata_port *ap = qc->ap;
  127. struct scatterlist *sg;
  128. unsigned int si, pi;
  129. pi = 0;
  130. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  131. u32 addr, offset;
  132. u32 sg_len, len, blen;
  133. /* determine if physical DMA addr spans 64K boundary.
  134. * Note h/w doesn't support 64-bit, so we unconditionally
  135. * truncate dma_addr_t to u32.
  136. */
  137. addr = (u32) sg_dma_address(sg);
  138. sg_len = sg_dma_len(sg);
  139. while (sg_len) {
  140. offset = addr & 0xffff;
  141. len = sg_len;
  142. if ((offset + sg_len) > 0x10000)
  143. len = 0x10000 - offset;
  144. blen = len & 0xffff;
  145. ap->prd[pi].addr = cpu_to_le32(addr);
  146. if (blen == 0) {
  147. /* Some PATA chipsets like the CS5530 can't
  148. cope with 0x0000 meaning 64K as the spec says */
  149. ap->prd[pi].flags_len = cpu_to_le32(0x8000);
  150. blen = 0x8000;
  151. ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
  152. }
  153. ap->prd[pi].flags_len = cpu_to_le32(blen);
  154. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  155. pi++;
  156. sg_len -= len;
  157. addr += len;
  158. }
  159. }
  160. ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  161. }
  162. /**
  163. * ata_sff_qc_prep - Prepare taskfile for submission
  164. * @qc: Metadata associated with taskfile to be prepared
  165. *
  166. * Prepare ATA taskfile for submission.
  167. *
  168. * LOCKING:
  169. * spin_lock_irqsave(host lock)
  170. */
  171. void ata_sff_qc_prep(struct ata_queued_cmd *qc)
  172. {
  173. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  174. return;
  175. ata_fill_sg(qc);
  176. }
  177. /**
  178. * ata_sff_dumb_qc_prep - Prepare taskfile for submission
  179. * @qc: Metadata associated with taskfile to be prepared
  180. *
  181. * Prepare ATA taskfile for submission.
  182. *
  183. * LOCKING:
  184. * spin_lock_irqsave(host lock)
  185. */
  186. void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
  187. {
  188. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  189. return;
  190. ata_fill_sg_dumb(qc);
  191. }
  192. /**
  193. * ata_sff_check_status - Read device status reg & clear interrupt
  194. * @ap: port where the device is
  195. *
  196. * Reads ATA taskfile status register for currently-selected device
  197. * and return its value. This also clears pending interrupts
  198. * from this device
  199. *
  200. * LOCKING:
  201. * Inherited from caller.
  202. */
  203. u8 ata_sff_check_status(struct ata_port *ap)
  204. {
  205. return ioread8(ap->ioaddr.status_addr);
  206. }
  207. /**
  208. * ata_sff_altstatus - Read device alternate status reg
  209. * @ap: port where the device is
  210. *
  211. * Reads ATA taskfile alternate status register for
  212. * currently-selected device and return its value.
  213. *
  214. * Note: may NOT be used as the check_altstatus() entry in
  215. * ata_port_operations.
  216. *
  217. * LOCKING:
  218. * Inherited from caller.
  219. */
  220. static u8 ata_sff_altstatus(struct ata_port *ap)
  221. {
  222. if (ap->ops->sff_check_altstatus)
  223. return ap->ops->sff_check_altstatus(ap);
  224. return ioread8(ap->ioaddr.altstatus_addr);
  225. }
  226. /**
  227. * ata_sff_irq_status - Check if the device is busy
  228. * @ap: port where the device is
  229. *
  230. * Determine if the port is currently busy. Uses altstatus
  231. * if available in order to avoid clearing shared IRQ status
  232. * when finding an IRQ source. Non ctl capable devices don't
  233. * share interrupt lines fortunately for us.
  234. *
  235. * LOCKING:
  236. * Inherited from caller.
  237. */
  238. static u8 ata_sff_irq_status(struct ata_port *ap)
  239. {
  240. u8 status;
  241. if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
  242. status = ata_sff_altstatus(ap);
  243. /* Not us: We are busy */
  244. if (status & ATA_BUSY)
  245. return status;
  246. }
  247. /* Clear INTRQ latch */
  248. status = ap->ops->sff_check_status(ap);
  249. return status;
  250. }
  251. /**
  252. * ata_sff_sync - Flush writes
  253. * @ap: Port to wait for.
  254. *
  255. * CAUTION:
  256. * If we have an mmio device with no ctl and no altstatus
  257. * method this will fail. No such devices are known to exist.
  258. *
  259. * LOCKING:
  260. * Inherited from caller.
  261. */
  262. static void ata_sff_sync(struct ata_port *ap)
  263. {
  264. if (ap->ops->sff_check_altstatus)
  265. ap->ops->sff_check_altstatus(ap);
  266. else if (ap->ioaddr.altstatus_addr)
  267. ioread8(ap->ioaddr.altstatus_addr);
  268. }
  269. /**
  270. * ata_sff_pause - Flush writes and wait 400nS
  271. * @ap: Port to pause for.
  272. *
  273. * CAUTION:
  274. * If we have an mmio device with no ctl and no altstatus
  275. * method this will fail. No such devices are known to exist.
  276. *
  277. * LOCKING:
  278. * Inherited from caller.
  279. */
  280. void ata_sff_pause(struct ata_port *ap)
  281. {
  282. ata_sff_sync(ap);
  283. ndelay(400);
  284. }
  285. /**
  286. * ata_sff_dma_pause - Pause before commencing DMA
  287. * @ap: Port to pause for.
  288. *
  289. * Perform I/O fencing and ensure sufficient cycle delays occur
  290. * for the HDMA1:0 transition
  291. */
  292. void ata_sff_dma_pause(struct ata_port *ap)
  293. {
  294. if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
  295. /* An altstatus read will cause the needed delay without
  296. messing up the IRQ status */
  297. ata_sff_altstatus(ap);
  298. return;
  299. }
  300. /* There are no DMA controllers without ctl. BUG here to ensure
  301. we never violate the HDMA1:0 transition timing and risk
  302. corruption. */
  303. BUG();
  304. }
  305. /**
  306. * ata_sff_busy_sleep - sleep until BSY clears, or timeout
  307. * @ap: port containing status register to be polled
  308. * @tmout_pat: impatience timeout in msecs
  309. * @tmout: overall timeout in msecs
  310. *
  311. * Sleep until ATA Status register bit BSY clears,
  312. * or a timeout occurs.
  313. *
  314. * LOCKING:
  315. * Kernel thread context (may sleep).
  316. *
  317. * RETURNS:
  318. * 0 on success, -errno otherwise.
  319. */
  320. int ata_sff_busy_sleep(struct ata_port *ap,
  321. unsigned long tmout_pat, unsigned long tmout)
  322. {
  323. unsigned long timer_start, timeout;
  324. u8 status;
  325. status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
  326. timer_start = jiffies;
  327. timeout = ata_deadline(timer_start, tmout_pat);
  328. while (status != 0xff && (status & ATA_BUSY) &&
  329. time_before(jiffies, timeout)) {
  330. msleep(50);
  331. status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
  332. }
  333. if (status != 0xff && (status & ATA_BUSY))
  334. ata_port_printk(ap, KERN_WARNING,
  335. "port is slow to respond, please be patient "
  336. "(Status 0x%x)\n", status);
  337. timeout = ata_deadline(timer_start, tmout);
  338. while (status != 0xff && (status & ATA_BUSY) &&
  339. time_before(jiffies, timeout)) {
  340. msleep(50);
  341. status = ap->ops->sff_check_status(ap);
  342. }
  343. if (status == 0xff)
  344. return -ENODEV;
  345. if (status & ATA_BUSY) {
  346. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  347. "(%lu secs, Status 0x%x)\n",
  348. DIV_ROUND_UP(tmout, 1000), status);
  349. return -EBUSY;
  350. }
  351. return 0;
  352. }
  353. static int ata_sff_check_ready(struct ata_link *link)
  354. {
  355. u8 status = link->ap->ops->sff_check_status(link->ap);
  356. return ata_check_ready(status);
  357. }
  358. /**
  359. * ata_sff_wait_ready - sleep until BSY clears, or timeout
  360. * @link: SFF link to wait ready status for
  361. * @deadline: deadline jiffies for the operation
  362. *
  363. * Sleep until ATA Status register bit BSY clears, or timeout
  364. * occurs.
  365. *
  366. * LOCKING:
  367. * Kernel thread context (may sleep).
  368. *
  369. * RETURNS:
  370. * 0 on success, -errno otherwise.
  371. */
  372. int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
  373. {
  374. return ata_wait_ready(link, deadline, ata_sff_check_ready);
  375. }
  376. /**
  377. * ata_sff_dev_select - Select device 0/1 on ATA bus
  378. * @ap: ATA channel to manipulate
  379. * @device: ATA device (numbered from zero) to select
  380. *
  381. * Use the method defined in the ATA specification to
  382. * make either device 0, or device 1, active on the
  383. * ATA channel. Works with both PIO and MMIO.
  384. *
  385. * May be used as the dev_select() entry in ata_port_operations.
  386. *
  387. * LOCKING:
  388. * caller.
  389. */
  390. void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
  391. {
  392. u8 tmp;
  393. if (device == 0)
  394. tmp = ATA_DEVICE_OBS;
  395. else
  396. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  397. iowrite8(tmp, ap->ioaddr.device_addr);
  398. ata_sff_pause(ap); /* needed; also flushes, for mmio */
  399. }
  400. /**
  401. * ata_dev_select - Select device 0/1 on ATA bus
  402. * @ap: ATA channel to manipulate
  403. * @device: ATA device (numbered from zero) to select
  404. * @wait: non-zero to wait for Status register BSY bit to clear
  405. * @can_sleep: non-zero if context allows sleeping
  406. *
  407. * Use the method defined in the ATA specification to
  408. * make either device 0, or device 1, active on the
  409. * ATA channel.
  410. *
  411. * This is a high-level version of ata_sff_dev_select(), which
  412. * additionally provides the services of inserting the proper
  413. * pauses and status polling, where needed.
  414. *
  415. * LOCKING:
  416. * caller.
  417. */
  418. void ata_dev_select(struct ata_port *ap, unsigned int device,
  419. unsigned int wait, unsigned int can_sleep)
  420. {
  421. if (ata_msg_probe(ap))
  422. ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
  423. "device %u, wait %u\n", device, wait);
  424. if (wait)
  425. ata_wait_idle(ap);
  426. ap->ops->sff_dev_select(ap, device);
  427. if (wait) {
  428. if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
  429. msleep(150);
  430. ata_wait_idle(ap);
  431. }
  432. }
  433. /**
  434. * ata_sff_irq_on - Enable interrupts on a port.
  435. * @ap: Port on which interrupts are enabled.
  436. *
  437. * Enable interrupts on a legacy IDE device using MMIO or PIO,
  438. * wait for idle, clear any pending interrupts.
  439. *
  440. * LOCKING:
  441. * Inherited from caller.
  442. */
  443. u8 ata_sff_irq_on(struct ata_port *ap)
  444. {
  445. struct ata_ioports *ioaddr = &ap->ioaddr;
  446. u8 tmp;
  447. ap->ctl &= ~ATA_NIEN;
  448. ap->last_ctl = ap->ctl;
  449. if (ioaddr->ctl_addr)
  450. iowrite8(ap->ctl, ioaddr->ctl_addr);
  451. tmp = ata_wait_idle(ap);
  452. ap->ops->sff_irq_clear(ap);
  453. return tmp;
  454. }
  455. /**
  456. * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
  457. * @ap: Port associated with this ATA transaction.
  458. *
  459. * Clear interrupt and error flags in DMA status register.
  460. *
  461. * May be used as the irq_clear() entry in ata_port_operations.
  462. *
  463. * LOCKING:
  464. * spin_lock_irqsave(host lock)
  465. */
  466. void ata_sff_irq_clear(struct ata_port *ap)
  467. {
  468. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  469. if (!mmio)
  470. return;
  471. iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
  472. }
  473. /**
  474. * ata_sff_tf_load - send taskfile registers to host controller
  475. * @ap: Port to which output is sent
  476. * @tf: ATA taskfile register set
  477. *
  478. * Outputs ATA taskfile to standard ATA host controller.
  479. *
  480. * LOCKING:
  481. * Inherited from caller.
  482. */
  483. void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  484. {
  485. struct ata_ioports *ioaddr = &ap->ioaddr;
  486. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  487. if (tf->ctl != ap->last_ctl) {
  488. if (ioaddr->ctl_addr)
  489. iowrite8(tf->ctl, ioaddr->ctl_addr);
  490. ap->last_ctl = tf->ctl;
  491. ata_wait_idle(ap);
  492. }
  493. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  494. WARN_ON(!ioaddr->ctl_addr);
  495. iowrite8(tf->hob_feature, ioaddr->feature_addr);
  496. iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
  497. iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
  498. iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
  499. iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
  500. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  501. tf->hob_feature,
  502. tf->hob_nsect,
  503. tf->hob_lbal,
  504. tf->hob_lbam,
  505. tf->hob_lbah);
  506. }
  507. if (is_addr) {
  508. iowrite8(tf->feature, ioaddr->feature_addr);
  509. iowrite8(tf->nsect, ioaddr->nsect_addr);
  510. iowrite8(tf->lbal, ioaddr->lbal_addr);
  511. iowrite8(tf->lbam, ioaddr->lbam_addr);
  512. iowrite8(tf->lbah, ioaddr->lbah_addr);
  513. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  514. tf->feature,
  515. tf->nsect,
  516. tf->lbal,
  517. tf->lbam,
  518. tf->lbah);
  519. }
  520. if (tf->flags & ATA_TFLAG_DEVICE) {
  521. iowrite8(tf->device, ioaddr->device_addr);
  522. VPRINTK("device 0x%X\n", tf->device);
  523. }
  524. ata_wait_idle(ap);
  525. }
  526. /**
  527. * ata_sff_tf_read - input device's ATA taskfile shadow registers
  528. * @ap: Port from which input is read
  529. * @tf: ATA taskfile register set for storing input
  530. *
  531. * Reads ATA taskfile registers for currently-selected device
  532. * into @tf. Assumes the device has a fully SFF compliant task file
  533. * layout and behaviour. If you device does not (eg has a different
  534. * status method) then you will need to provide a replacement tf_read
  535. *
  536. * LOCKING:
  537. * Inherited from caller.
  538. */
  539. void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  540. {
  541. struct ata_ioports *ioaddr = &ap->ioaddr;
  542. tf->command = ata_sff_check_status(ap);
  543. tf->feature = ioread8(ioaddr->error_addr);
  544. tf->nsect = ioread8(ioaddr->nsect_addr);
  545. tf->lbal = ioread8(ioaddr->lbal_addr);
  546. tf->lbam = ioread8(ioaddr->lbam_addr);
  547. tf->lbah = ioread8(ioaddr->lbah_addr);
  548. tf->device = ioread8(ioaddr->device_addr);
  549. if (tf->flags & ATA_TFLAG_LBA48) {
  550. if (likely(ioaddr->ctl_addr)) {
  551. iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  552. tf->hob_feature = ioread8(ioaddr->error_addr);
  553. tf->hob_nsect = ioread8(ioaddr->nsect_addr);
  554. tf->hob_lbal = ioread8(ioaddr->lbal_addr);
  555. tf->hob_lbam = ioread8(ioaddr->lbam_addr);
  556. tf->hob_lbah = ioread8(ioaddr->lbah_addr);
  557. iowrite8(tf->ctl, ioaddr->ctl_addr);
  558. ap->last_ctl = tf->ctl;
  559. } else
  560. WARN_ON(1);
  561. }
  562. }
  563. /**
  564. * ata_sff_exec_command - issue ATA command to host controller
  565. * @ap: port to which command is being issued
  566. * @tf: ATA taskfile register set
  567. *
  568. * Issues ATA command, with proper synchronization with interrupt
  569. * handler / other threads.
  570. *
  571. * LOCKING:
  572. * spin_lock_irqsave(host lock)
  573. */
  574. void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  575. {
  576. DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  577. iowrite8(tf->command, ap->ioaddr.command_addr);
  578. ata_sff_pause(ap);
  579. }
  580. /**
  581. * ata_tf_to_host - issue ATA taskfile to host controller
  582. * @ap: port to which command is being issued
  583. * @tf: ATA taskfile register set
  584. *
  585. * Issues ATA taskfile register set to ATA host controller,
  586. * with proper synchronization with interrupt handler and
  587. * other threads.
  588. *
  589. * LOCKING:
  590. * spin_lock_irqsave(host lock)
  591. */
  592. static inline void ata_tf_to_host(struct ata_port *ap,
  593. const struct ata_taskfile *tf)
  594. {
  595. ap->ops->sff_tf_load(ap, tf);
  596. ap->ops->sff_exec_command(ap, tf);
  597. }
  598. /**
  599. * ata_sff_data_xfer - Transfer data by PIO
  600. * @dev: device to target
  601. * @buf: data buffer
  602. * @buflen: buffer length
  603. * @rw: read/write
  604. *
  605. * Transfer data from/to the device data register by PIO.
  606. *
  607. * LOCKING:
  608. * Inherited from caller.
  609. *
  610. * RETURNS:
  611. * Bytes consumed.
  612. */
  613. unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
  614. unsigned int buflen, int rw)
  615. {
  616. struct ata_port *ap = dev->link->ap;
  617. void __iomem *data_addr = ap->ioaddr.data_addr;
  618. unsigned int words = buflen >> 1;
  619. /* Transfer multiple of 2 bytes */
  620. if (rw == READ)
  621. ioread16_rep(data_addr, buf, words);
  622. else
  623. iowrite16_rep(data_addr, buf, words);
  624. /* Transfer trailing 1 byte, if any. */
  625. if (unlikely(buflen & 0x01)) {
  626. __le16 align_buf[1] = { 0 };
  627. unsigned char *trailing_buf = buf + buflen - 1;
  628. if (rw == READ) {
  629. align_buf[0] = cpu_to_le16(ioread16(data_addr));
  630. memcpy(trailing_buf, align_buf, 1);
  631. } else {
  632. memcpy(align_buf, trailing_buf, 1);
  633. iowrite16(le16_to_cpu(align_buf[0]), data_addr);
  634. }
  635. words++;
  636. }
  637. return words << 1;
  638. }
  639. /**
  640. * ata_sff_data_xfer_noirq - Transfer data by PIO
  641. * @dev: device to target
  642. * @buf: data buffer
  643. * @buflen: buffer length
  644. * @rw: read/write
  645. *
  646. * Transfer data from/to the device data register by PIO. Do the
  647. * transfer with interrupts disabled.
  648. *
  649. * LOCKING:
  650. * Inherited from caller.
  651. *
  652. * RETURNS:
  653. * Bytes consumed.
  654. */
  655. unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  656. unsigned int buflen, int rw)
  657. {
  658. unsigned long flags;
  659. unsigned int consumed;
  660. local_irq_save(flags);
  661. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  662. local_irq_restore(flags);
  663. return consumed;
  664. }
  665. /**
  666. * ata_pio_sector - Transfer a sector of data.
  667. * @qc: Command on going
  668. *
  669. * Transfer qc->sect_size bytes of data from/to the ATA device.
  670. *
  671. * LOCKING:
  672. * Inherited from caller.
  673. */
  674. static void ata_pio_sector(struct ata_queued_cmd *qc)
  675. {
  676. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  677. struct ata_port *ap = qc->ap;
  678. struct page *page;
  679. unsigned int offset;
  680. unsigned char *buf;
  681. if (qc->curbytes == qc->nbytes - qc->sect_size)
  682. ap->hsm_task_state = HSM_ST_LAST;
  683. page = sg_page(qc->cursg);
  684. offset = qc->cursg->offset + qc->cursg_ofs;
  685. /* get the current page and offset */
  686. page = nth_page(page, (offset >> PAGE_SHIFT));
  687. offset %= PAGE_SIZE;
  688. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  689. if (PageHighMem(page)) {
  690. unsigned long flags;
  691. /* FIXME: use a bounce buffer */
  692. local_irq_save(flags);
  693. buf = kmap_atomic(page, KM_IRQ0);
  694. /* do the actual data transfer */
  695. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  696. do_write);
  697. kunmap_atomic(buf, KM_IRQ0);
  698. local_irq_restore(flags);
  699. } else {
  700. buf = page_address(page);
  701. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  702. do_write);
  703. }
  704. qc->curbytes += qc->sect_size;
  705. qc->cursg_ofs += qc->sect_size;
  706. if (qc->cursg_ofs == qc->cursg->length) {
  707. qc->cursg = sg_next(qc->cursg);
  708. qc->cursg_ofs = 0;
  709. }
  710. }
  711. /**
  712. * ata_pio_sectors - Transfer one or many sectors.
  713. * @qc: Command on going
  714. *
  715. * Transfer one or many sectors of data from/to the
  716. * ATA device for the DRQ request.
  717. *
  718. * LOCKING:
  719. * Inherited from caller.
  720. */
  721. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  722. {
  723. if (is_multi_taskfile(&qc->tf)) {
  724. /* READ/WRITE MULTIPLE */
  725. unsigned int nsect;
  726. WARN_ON(qc->dev->multi_count == 0);
  727. nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
  728. qc->dev->multi_count);
  729. while (nsect--)
  730. ata_pio_sector(qc);
  731. } else
  732. ata_pio_sector(qc);
  733. ata_sff_sync(qc->ap); /* flush */
  734. }
  735. /**
  736. * atapi_send_cdb - Write CDB bytes to hardware
  737. * @ap: Port to which ATAPI device is attached.
  738. * @qc: Taskfile currently active
  739. *
  740. * When device has indicated its readiness to accept
  741. * a CDB, this function is called. Send the CDB.
  742. *
  743. * LOCKING:
  744. * caller.
  745. */
  746. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  747. {
  748. /* send SCSI cdb */
  749. DPRINTK("send cdb\n");
  750. WARN_ON(qc->dev->cdb_len < 12);
  751. ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  752. ata_sff_sync(ap);
  753. /* FIXME: If the CDB is for DMA do we need to do the transition delay
  754. or is bmdma_start guaranteed to do it ? */
  755. switch (qc->tf.protocol) {
  756. case ATAPI_PROT_PIO:
  757. ap->hsm_task_state = HSM_ST;
  758. break;
  759. case ATAPI_PROT_NODATA:
  760. ap->hsm_task_state = HSM_ST_LAST;
  761. break;
  762. case ATAPI_PROT_DMA:
  763. ap->hsm_task_state = HSM_ST_LAST;
  764. /* initiate bmdma */
  765. ap->ops->bmdma_start(qc);
  766. break;
  767. }
  768. }
  769. /**
  770. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  771. * @qc: Command on going
  772. * @bytes: number of bytes
  773. *
  774. * Transfer Transfer data from/to the ATAPI device.
  775. *
  776. * LOCKING:
  777. * Inherited from caller.
  778. *
  779. */
  780. static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  781. {
  782. int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
  783. struct ata_port *ap = qc->ap;
  784. struct ata_device *dev = qc->dev;
  785. struct ata_eh_info *ehi = &dev->link->eh_info;
  786. struct scatterlist *sg;
  787. struct page *page;
  788. unsigned char *buf;
  789. unsigned int offset, count, consumed;
  790. next_sg:
  791. sg = qc->cursg;
  792. if (unlikely(!sg)) {
  793. ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
  794. "buf=%u cur=%u bytes=%u",
  795. qc->nbytes, qc->curbytes, bytes);
  796. return -1;
  797. }
  798. page = sg_page(sg);
  799. offset = sg->offset + qc->cursg_ofs;
  800. /* get the current page and offset */
  801. page = nth_page(page, (offset >> PAGE_SHIFT));
  802. offset %= PAGE_SIZE;
  803. /* don't overrun current sg */
  804. count = min(sg->length - qc->cursg_ofs, bytes);
  805. /* don't cross page boundaries */
  806. count = min(count, (unsigned int)PAGE_SIZE - offset);
  807. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  808. if (PageHighMem(page)) {
  809. unsigned long flags;
  810. /* FIXME: use bounce buffer */
  811. local_irq_save(flags);
  812. buf = kmap_atomic(page, KM_IRQ0);
  813. /* do the actual data transfer */
  814. consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
  815. kunmap_atomic(buf, KM_IRQ0);
  816. local_irq_restore(flags);
  817. } else {
  818. buf = page_address(page);
  819. consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
  820. }
  821. bytes -= min(bytes, consumed);
  822. qc->curbytes += count;
  823. qc->cursg_ofs += count;
  824. if (qc->cursg_ofs == sg->length) {
  825. qc->cursg = sg_next(qc->cursg);
  826. qc->cursg_ofs = 0;
  827. }
  828. /* consumed can be larger than count only for the last transfer */
  829. WARN_ON(qc->cursg && count != consumed);
  830. if (bytes)
  831. goto next_sg;
  832. return 0;
  833. }
  834. /**
  835. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  836. * @qc: Command on going
  837. *
  838. * Transfer Transfer data from/to the ATAPI device.
  839. *
  840. * LOCKING:
  841. * Inherited from caller.
  842. */
  843. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  844. {
  845. struct ata_port *ap = qc->ap;
  846. struct ata_device *dev = qc->dev;
  847. struct ata_eh_info *ehi = &dev->link->eh_info;
  848. unsigned int ireason, bc_lo, bc_hi, bytes;
  849. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  850. /* Abuse qc->result_tf for temp storage of intermediate TF
  851. * here to save some kernel stack usage.
  852. * For normal completion, qc->result_tf is not relevant. For
  853. * error, qc->result_tf is later overwritten by ata_qc_complete().
  854. * So, the correctness of qc->result_tf is not affected.
  855. */
  856. ap->ops->sff_tf_read(ap, &qc->result_tf);
  857. ireason = qc->result_tf.nsect;
  858. bc_lo = qc->result_tf.lbam;
  859. bc_hi = qc->result_tf.lbah;
  860. bytes = (bc_hi << 8) | bc_lo;
  861. /* shall be cleared to zero, indicating xfer of data */
  862. if (unlikely(ireason & (1 << 0)))
  863. goto atapi_check;
  864. /* make sure transfer direction matches expected */
  865. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  866. if (unlikely(do_write != i_write))
  867. goto atapi_check;
  868. if (unlikely(!bytes))
  869. goto atapi_check;
  870. VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
  871. if (unlikely(__atapi_pio_bytes(qc, bytes)))
  872. goto err_out;
  873. ata_sff_sync(ap); /* flush */
  874. return;
  875. atapi_check:
  876. ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
  877. ireason, bytes);
  878. err_out:
  879. qc->err_mask |= AC_ERR_HSM;
  880. ap->hsm_task_state = HSM_ST_ERR;
  881. }
  882. /**
  883. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  884. * @ap: the target ata_port
  885. * @qc: qc on going
  886. *
  887. * RETURNS:
  888. * 1 if ok in workqueue, 0 otherwise.
  889. */
  890. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  891. {
  892. if (qc->tf.flags & ATA_TFLAG_POLLING)
  893. return 1;
  894. if (ap->hsm_task_state == HSM_ST_FIRST) {
  895. if (qc->tf.protocol == ATA_PROT_PIO &&
  896. (qc->tf.flags & ATA_TFLAG_WRITE))
  897. return 1;
  898. if (ata_is_atapi(qc->tf.protocol) &&
  899. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  900. return 1;
  901. }
  902. return 0;
  903. }
  904. /**
  905. * ata_hsm_qc_complete - finish a qc running on standard HSM
  906. * @qc: Command to complete
  907. * @in_wq: 1 if called from workqueue, 0 otherwise
  908. *
  909. * Finish @qc which is running on standard HSM.
  910. *
  911. * LOCKING:
  912. * If @in_wq is zero, spin_lock_irqsave(host lock).
  913. * Otherwise, none on entry and grabs host lock.
  914. */
  915. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  916. {
  917. struct ata_port *ap = qc->ap;
  918. unsigned long flags;
  919. if (ap->ops->error_handler) {
  920. if (in_wq) {
  921. spin_lock_irqsave(ap->lock, flags);
  922. /* EH might have kicked in while host lock is
  923. * released.
  924. */
  925. qc = ata_qc_from_tag(ap, qc->tag);
  926. if (qc) {
  927. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  928. ap->ops->sff_irq_on(ap);
  929. ata_qc_complete(qc);
  930. } else
  931. ata_port_freeze(ap);
  932. }
  933. spin_unlock_irqrestore(ap->lock, flags);
  934. } else {
  935. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  936. ata_qc_complete(qc);
  937. else
  938. ata_port_freeze(ap);
  939. }
  940. } else {
  941. if (in_wq) {
  942. spin_lock_irqsave(ap->lock, flags);
  943. ap->ops->sff_irq_on(ap);
  944. ata_qc_complete(qc);
  945. spin_unlock_irqrestore(ap->lock, flags);
  946. } else
  947. ata_qc_complete(qc);
  948. }
  949. }
  950. /**
  951. * ata_sff_hsm_move - move the HSM to the next state.
  952. * @ap: the target ata_port
  953. * @qc: qc on going
  954. * @status: current device status
  955. * @in_wq: 1 if called from workqueue, 0 otherwise
  956. *
  957. * RETURNS:
  958. * 1 when poll next status needed, 0 otherwise.
  959. */
  960. int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  961. u8 status, int in_wq)
  962. {
  963. struct ata_eh_info *ehi = &ap->link.eh_info;
  964. unsigned long flags = 0;
  965. int poll_next;
  966. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  967. /* Make sure ata_sff_qc_issue() does not throw things
  968. * like DMA polling into the workqueue. Notice that
  969. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  970. */
  971. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  972. fsm_start:
  973. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  974. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  975. switch (ap->hsm_task_state) {
  976. case HSM_ST_FIRST:
  977. /* Send first data block or PACKET CDB */
  978. /* If polling, we will stay in the work queue after
  979. * sending the data. Otherwise, interrupt handler
  980. * takes over after sending the data.
  981. */
  982. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  983. /* check device status */
  984. if (unlikely((status & ATA_DRQ) == 0)) {
  985. /* handle BSY=0, DRQ=0 as error */
  986. if (likely(status & (ATA_ERR | ATA_DF)))
  987. /* device stops HSM for abort/error */
  988. qc->err_mask |= AC_ERR_DEV;
  989. else {
  990. /* HSM violation. Let EH handle this */
  991. ata_ehi_push_desc(ehi,
  992. "ST_FIRST: !(DRQ|ERR|DF)");
  993. qc->err_mask |= AC_ERR_HSM;
  994. }
  995. ap->hsm_task_state = HSM_ST_ERR;
  996. goto fsm_start;
  997. }
  998. /* Device should not ask for data transfer (DRQ=1)
  999. * when it finds something wrong.
  1000. * We ignore DRQ here and stop the HSM by
  1001. * changing hsm_task_state to HSM_ST_ERR and
  1002. * let the EH abort the command or reset the device.
  1003. */
  1004. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1005. /* Some ATAPI tape drives forget to clear the ERR bit
  1006. * when doing the next command (mostly request sense).
  1007. * We ignore ERR here to workaround and proceed sending
  1008. * the CDB.
  1009. */
  1010. if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
  1011. ata_ehi_push_desc(ehi, "ST_FIRST: "
  1012. "DRQ=1 with device error, "
  1013. "dev_stat 0x%X", status);
  1014. qc->err_mask |= AC_ERR_HSM;
  1015. ap->hsm_task_state = HSM_ST_ERR;
  1016. goto fsm_start;
  1017. }
  1018. }
  1019. /* Send the CDB (atapi) or the first data block (ata pio out).
  1020. * During the state transition, interrupt handler shouldn't
  1021. * be invoked before the data transfer is complete and
  1022. * hsm_task_state is changed. Hence, the following locking.
  1023. */
  1024. if (in_wq)
  1025. spin_lock_irqsave(ap->lock, flags);
  1026. if (qc->tf.protocol == ATA_PROT_PIO) {
  1027. /* PIO data out protocol.
  1028. * send first data block.
  1029. */
  1030. /* ata_pio_sectors() might change the state
  1031. * to HSM_ST_LAST. so, the state is changed here
  1032. * before ata_pio_sectors().
  1033. */
  1034. ap->hsm_task_state = HSM_ST;
  1035. ata_pio_sectors(qc);
  1036. } else
  1037. /* send CDB */
  1038. atapi_send_cdb(ap, qc);
  1039. if (in_wq)
  1040. spin_unlock_irqrestore(ap->lock, flags);
  1041. /* if polling, ata_pio_task() handles the rest.
  1042. * otherwise, interrupt handler takes over from here.
  1043. */
  1044. break;
  1045. case HSM_ST:
  1046. /* complete command or read/write the data register */
  1047. if (qc->tf.protocol == ATAPI_PROT_PIO) {
  1048. /* ATAPI PIO protocol */
  1049. if ((status & ATA_DRQ) == 0) {
  1050. /* No more data to transfer or device error.
  1051. * Device error will be tagged in HSM_ST_LAST.
  1052. */
  1053. ap->hsm_task_state = HSM_ST_LAST;
  1054. goto fsm_start;
  1055. }
  1056. /* Device should not ask for data transfer (DRQ=1)
  1057. * when it finds something wrong.
  1058. * We ignore DRQ here and stop the HSM by
  1059. * changing hsm_task_state to HSM_ST_ERR and
  1060. * let the EH abort the command or reset the device.
  1061. */
  1062. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1063. ata_ehi_push_desc(ehi, "ST-ATAPI: "
  1064. "DRQ=1 with device error, "
  1065. "dev_stat 0x%X", status);
  1066. qc->err_mask |= AC_ERR_HSM;
  1067. ap->hsm_task_state = HSM_ST_ERR;
  1068. goto fsm_start;
  1069. }
  1070. atapi_pio_bytes(qc);
  1071. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  1072. /* bad ireason reported by device */
  1073. goto fsm_start;
  1074. } else {
  1075. /* ATA PIO protocol */
  1076. if (unlikely((status & ATA_DRQ) == 0)) {
  1077. /* handle BSY=0, DRQ=0 as error */
  1078. if (likely(status & (ATA_ERR | ATA_DF))) {
  1079. /* device stops HSM for abort/error */
  1080. qc->err_mask |= AC_ERR_DEV;
  1081. /* If diagnostic failed and this is
  1082. * IDENTIFY, it's likely a phantom
  1083. * device. Mark hint.
  1084. */
  1085. if (qc->dev->horkage &
  1086. ATA_HORKAGE_DIAGNOSTIC)
  1087. qc->err_mask |=
  1088. AC_ERR_NODEV_HINT;
  1089. } else {
  1090. /* HSM violation. Let EH handle this.
  1091. * Phantom devices also trigger this
  1092. * condition. Mark hint.
  1093. */
  1094. ata_ehi_push_desc(ehi, "ST-ATA: "
  1095. "DRQ=1 with device error, "
  1096. "dev_stat 0x%X", status);
  1097. qc->err_mask |= AC_ERR_HSM |
  1098. AC_ERR_NODEV_HINT;
  1099. }
  1100. ap->hsm_task_state = HSM_ST_ERR;
  1101. goto fsm_start;
  1102. }
  1103. /* For PIO reads, some devices may ask for
  1104. * data transfer (DRQ=1) alone with ERR=1.
  1105. * We respect DRQ here and transfer one
  1106. * block of junk data before changing the
  1107. * hsm_task_state to HSM_ST_ERR.
  1108. *
  1109. * For PIO writes, ERR=1 DRQ=1 doesn't make
  1110. * sense since the data block has been
  1111. * transferred to the device.
  1112. */
  1113. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1114. /* data might be corrputed */
  1115. qc->err_mask |= AC_ERR_DEV;
  1116. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  1117. ata_pio_sectors(qc);
  1118. status = ata_wait_idle(ap);
  1119. }
  1120. if (status & (ATA_BUSY | ATA_DRQ)) {
  1121. ata_ehi_push_desc(ehi, "ST-ATA: "
  1122. "BUSY|DRQ persists on ERR|DF, "
  1123. "dev_stat 0x%X", status);
  1124. qc->err_mask |= AC_ERR_HSM;
  1125. }
  1126. /* ata_pio_sectors() might change the
  1127. * state to HSM_ST_LAST. so, the state
  1128. * is changed after ata_pio_sectors().
  1129. */
  1130. ap->hsm_task_state = HSM_ST_ERR;
  1131. goto fsm_start;
  1132. }
  1133. ata_pio_sectors(qc);
  1134. if (ap->hsm_task_state == HSM_ST_LAST &&
  1135. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  1136. /* all data read */
  1137. status = ata_wait_idle(ap);
  1138. goto fsm_start;
  1139. }
  1140. }
  1141. poll_next = 1;
  1142. break;
  1143. case HSM_ST_LAST:
  1144. if (unlikely(!ata_ok(status))) {
  1145. qc->err_mask |= __ac_err_mask(status);
  1146. ap->hsm_task_state = HSM_ST_ERR;
  1147. goto fsm_start;
  1148. }
  1149. /* no more data to transfer */
  1150. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  1151. ap->print_id, qc->dev->devno, status);
  1152. WARN_ON(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
  1153. ap->hsm_task_state = HSM_ST_IDLE;
  1154. /* complete taskfile transaction */
  1155. ata_hsm_qc_complete(qc, in_wq);
  1156. poll_next = 0;
  1157. break;
  1158. case HSM_ST_ERR:
  1159. ap->hsm_task_state = HSM_ST_IDLE;
  1160. /* complete taskfile transaction */
  1161. ata_hsm_qc_complete(qc, in_wq);
  1162. poll_next = 0;
  1163. break;
  1164. default:
  1165. poll_next = 0;
  1166. BUG();
  1167. }
  1168. return poll_next;
  1169. }
  1170. void ata_pio_task(struct work_struct *work)
  1171. {
  1172. struct ata_port *ap =
  1173. container_of(work, struct ata_port, port_task.work);
  1174. struct ata_queued_cmd *qc = ap->port_task_data;
  1175. u8 status;
  1176. int poll_next;
  1177. fsm_start:
  1178. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  1179. /*
  1180. * This is purely heuristic. This is a fast path.
  1181. * Sometimes when we enter, BSY will be cleared in
  1182. * a chk-status or two. If not, the drive is probably seeking
  1183. * or something. Snooze for a couple msecs, then
  1184. * chk-status again. If still busy, queue delayed work.
  1185. */
  1186. status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
  1187. if (status & ATA_BUSY) {
  1188. msleep(2);
  1189. status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
  1190. if (status & ATA_BUSY) {
  1191. ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
  1192. return;
  1193. }
  1194. }
  1195. /* move the HSM */
  1196. poll_next = ata_sff_hsm_move(ap, qc, status, 1);
  1197. /* another command or interrupt handler
  1198. * may be running at this point.
  1199. */
  1200. if (poll_next)
  1201. goto fsm_start;
  1202. }
  1203. /**
  1204. * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
  1205. * @qc: command to issue to device
  1206. *
  1207. * Using various libata functions and hooks, this function
  1208. * starts an ATA command. ATA commands are grouped into
  1209. * classes called "protocols", and issuing each type of protocol
  1210. * is slightly different.
  1211. *
  1212. * May be used as the qc_issue() entry in ata_port_operations.
  1213. *
  1214. * LOCKING:
  1215. * spin_lock_irqsave(host lock)
  1216. *
  1217. * RETURNS:
  1218. * Zero on success, AC_ERR_* mask on failure
  1219. */
  1220. unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
  1221. {
  1222. struct ata_port *ap = qc->ap;
  1223. /* Use polling pio if the LLD doesn't handle
  1224. * interrupt driven pio and atapi CDB interrupt.
  1225. */
  1226. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  1227. switch (qc->tf.protocol) {
  1228. case ATA_PROT_PIO:
  1229. case ATA_PROT_NODATA:
  1230. case ATAPI_PROT_PIO:
  1231. case ATAPI_PROT_NODATA:
  1232. qc->tf.flags |= ATA_TFLAG_POLLING;
  1233. break;
  1234. case ATAPI_PROT_DMA:
  1235. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  1236. /* see ata_dma_blacklisted() */
  1237. BUG();
  1238. break;
  1239. default:
  1240. break;
  1241. }
  1242. }
  1243. /* select the device */
  1244. ata_dev_select(ap, qc->dev->devno, 1, 0);
  1245. /* start the command */
  1246. switch (qc->tf.protocol) {
  1247. case ATA_PROT_NODATA:
  1248. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1249. ata_qc_set_polling(qc);
  1250. ata_tf_to_host(ap, &qc->tf);
  1251. ap->hsm_task_state = HSM_ST_LAST;
  1252. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1253. ata_pio_queue_task(ap, qc, 0);
  1254. break;
  1255. case ATA_PROT_DMA:
  1256. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  1257. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1258. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1259. ap->ops->bmdma_start(qc); /* initiate bmdma */
  1260. ap->hsm_task_state = HSM_ST_LAST;
  1261. break;
  1262. case ATA_PROT_PIO:
  1263. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1264. ata_qc_set_polling(qc);
  1265. ata_tf_to_host(ap, &qc->tf);
  1266. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  1267. /* PIO data out protocol */
  1268. ap->hsm_task_state = HSM_ST_FIRST;
  1269. ata_pio_queue_task(ap, qc, 0);
  1270. /* always send first data block using
  1271. * the ata_pio_task() codepath.
  1272. */
  1273. } else {
  1274. /* PIO data in protocol */
  1275. ap->hsm_task_state = HSM_ST;
  1276. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1277. ata_pio_queue_task(ap, qc, 0);
  1278. /* if polling, ata_pio_task() handles the rest.
  1279. * otherwise, interrupt handler takes over from here.
  1280. */
  1281. }
  1282. break;
  1283. case ATAPI_PROT_PIO:
  1284. case ATAPI_PROT_NODATA:
  1285. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1286. ata_qc_set_polling(qc);
  1287. ata_tf_to_host(ap, &qc->tf);
  1288. ap->hsm_task_state = HSM_ST_FIRST;
  1289. /* send cdb by polling if no cdb interrupt */
  1290. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  1291. (qc->tf.flags & ATA_TFLAG_POLLING))
  1292. ata_pio_queue_task(ap, qc, 0);
  1293. break;
  1294. case ATAPI_PROT_DMA:
  1295. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  1296. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1297. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1298. ap->hsm_task_state = HSM_ST_FIRST;
  1299. /* send cdb by polling if no cdb interrupt */
  1300. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1301. ata_pio_queue_task(ap, qc, 0);
  1302. break;
  1303. default:
  1304. WARN_ON(1);
  1305. return AC_ERR_SYSTEM;
  1306. }
  1307. return 0;
  1308. }
  1309. /**
  1310. * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
  1311. * @qc: qc to fill result TF for
  1312. *
  1313. * @qc is finished and result TF needs to be filled. Fill it
  1314. * using ->sff_tf_read.
  1315. *
  1316. * LOCKING:
  1317. * spin_lock_irqsave(host lock)
  1318. *
  1319. * RETURNS:
  1320. * true indicating that result TF is successfully filled.
  1321. */
  1322. bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
  1323. {
  1324. qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
  1325. return true;
  1326. }
  1327. /**
  1328. * ata_sff_host_intr - Handle host interrupt for given (port, task)
  1329. * @ap: Port on which interrupt arrived (possibly...)
  1330. * @qc: Taskfile currently active in engine
  1331. *
  1332. * Handle host interrupt for given queued command. Currently,
  1333. * only DMA interrupts are handled. All other commands are
  1334. * handled via polling with interrupts disabled (nIEN bit).
  1335. *
  1336. * LOCKING:
  1337. * spin_lock_irqsave(host lock)
  1338. *
  1339. * RETURNS:
  1340. * One if interrupt was handled, zero if not (shared irq).
  1341. */
  1342. inline unsigned int ata_sff_host_intr(struct ata_port *ap,
  1343. struct ata_queued_cmd *qc)
  1344. {
  1345. struct ata_eh_info *ehi = &ap->link.eh_info;
  1346. u8 status, host_stat = 0;
  1347. VPRINTK("ata%u: protocol %d task_state %d\n",
  1348. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  1349. /* Check whether we are expecting interrupt in this state */
  1350. switch (ap->hsm_task_state) {
  1351. case HSM_ST_FIRST:
  1352. /* Some pre-ATAPI-4 devices assert INTRQ
  1353. * at this state when ready to receive CDB.
  1354. */
  1355. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  1356. * The flag was turned on only for atapi devices. No
  1357. * need to check ata_is_atapi(qc->tf.protocol) again.
  1358. */
  1359. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1360. goto idle_irq;
  1361. break;
  1362. case HSM_ST_LAST:
  1363. if (qc->tf.protocol == ATA_PROT_DMA ||
  1364. qc->tf.protocol == ATAPI_PROT_DMA) {
  1365. /* check status of DMA engine */
  1366. host_stat = ap->ops->bmdma_status(ap);
  1367. VPRINTK("ata%u: host_stat 0x%X\n",
  1368. ap->print_id, host_stat);
  1369. /* if it's not our irq... */
  1370. if (!(host_stat & ATA_DMA_INTR))
  1371. goto idle_irq;
  1372. /* before we do anything else, clear DMA-Start bit */
  1373. ap->ops->bmdma_stop(qc);
  1374. if (unlikely(host_stat & ATA_DMA_ERR)) {
  1375. /* error when transfering data to/from memory */
  1376. qc->err_mask |= AC_ERR_HOST_BUS;
  1377. ap->hsm_task_state = HSM_ST_ERR;
  1378. }
  1379. }
  1380. break;
  1381. case HSM_ST:
  1382. break;
  1383. default:
  1384. goto idle_irq;
  1385. }
  1386. /* check main status, clearing INTRQ if needed */
  1387. status = ata_sff_irq_status(ap);
  1388. if (status & ATA_BUSY)
  1389. goto idle_irq;
  1390. /* ack bmdma irq events */
  1391. ap->ops->sff_irq_clear(ap);
  1392. ata_sff_hsm_move(ap, qc, status, 0);
  1393. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  1394. qc->tf.protocol == ATAPI_PROT_DMA))
  1395. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  1396. return 1; /* irq handled */
  1397. idle_irq:
  1398. ap->stats.idle_irq++;
  1399. #ifdef ATA_IRQ_TRAP
  1400. if ((ap->stats.idle_irq % 1000) == 0) {
  1401. ap->ops->sff_check_status(ap);
  1402. ap->ops->sff_irq_clear(ap);
  1403. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  1404. return 1;
  1405. }
  1406. #endif
  1407. return 0; /* irq not handled */
  1408. }
  1409. /**
  1410. * ata_sff_interrupt - Default ATA host interrupt handler
  1411. * @irq: irq line (unused)
  1412. * @dev_instance: pointer to our ata_host information structure
  1413. *
  1414. * Default interrupt handler for PCI IDE devices. Calls
  1415. * ata_sff_host_intr() for each port that is not disabled.
  1416. *
  1417. * LOCKING:
  1418. * Obtains host lock during operation.
  1419. *
  1420. * RETURNS:
  1421. * IRQ_NONE or IRQ_HANDLED.
  1422. */
  1423. irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
  1424. {
  1425. struct ata_host *host = dev_instance;
  1426. unsigned int i;
  1427. unsigned int handled = 0;
  1428. unsigned long flags;
  1429. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  1430. spin_lock_irqsave(&host->lock, flags);
  1431. for (i = 0; i < host->n_ports; i++) {
  1432. struct ata_port *ap;
  1433. ap = host->ports[i];
  1434. if (ap &&
  1435. !(ap->flags & ATA_FLAG_DISABLED)) {
  1436. struct ata_queued_cmd *qc;
  1437. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1438. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  1439. (qc->flags & ATA_QCFLAG_ACTIVE))
  1440. handled |= ata_sff_host_intr(ap, qc);
  1441. }
  1442. }
  1443. spin_unlock_irqrestore(&host->lock, flags);
  1444. return IRQ_RETVAL(handled);
  1445. }
  1446. /**
  1447. * ata_sff_freeze - Freeze SFF controller port
  1448. * @ap: port to freeze
  1449. *
  1450. * Freeze BMDMA controller port.
  1451. *
  1452. * LOCKING:
  1453. * Inherited from caller.
  1454. */
  1455. void ata_sff_freeze(struct ata_port *ap)
  1456. {
  1457. struct ata_ioports *ioaddr = &ap->ioaddr;
  1458. ap->ctl |= ATA_NIEN;
  1459. ap->last_ctl = ap->ctl;
  1460. if (ioaddr->ctl_addr)
  1461. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1462. /* Under certain circumstances, some controllers raise IRQ on
  1463. * ATA_NIEN manipulation. Also, many controllers fail to mask
  1464. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  1465. */
  1466. ap->ops->sff_check_status(ap);
  1467. ap->ops->sff_irq_clear(ap);
  1468. }
  1469. /**
  1470. * ata_sff_thaw - Thaw SFF controller port
  1471. * @ap: port to thaw
  1472. *
  1473. * Thaw SFF controller port.
  1474. *
  1475. * LOCKING:
  1476. * Inherited from caller.
  1477. */
  1478. void ata_sff_thaw(struct ata_port *ap)
  1479. {
  1480. /* clear & re-enable interrupts */
  1481. ap->ops->sff_check_status(ap);
  1482. ap->ops->sff_irq_clear(ap);
  1483. ap->ops->sff_irq_on(ap);
  1484. }
  1485. /**
  1486. * ata_sff_prereset - prepare SFF link for reset
  1487. * @link: SFF link to be reset
  1488. * @deadline: deadline jiffies for the operation
  1489. *
  1490. * SFF link @link is about to be reset. Initialize it. It first
  1491. * calls ata_std_prereset() and wait for !BSY if the port is
  1492. * being softreset.
  1493. *
  1494. * LOCKING:
  1495. * Kernel thread context (may sleep)
  1496. *
  1497. * RETURNS:
  1498. * 0 on success, -errno otherwise.
  1499. */
  1500. int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
  1501. {
  1502. struct ata_eh_context *ehc = &link->eh_context;
  1503. int rc;
  1504. rc = ata_std_prereset(link, deadline);
  1505. if (rc)
  1506. return rc;
  1507. /* if we're about to do hardreset, nothing more to do */
  1508. if (ehc->i.action & ATA_EH_HARDRESET)
  1509. return 0;
  1510. /* wait for !BSY if we don't know that no device is attached */
  1511. if (!ata_link_offline(link)) {
  1512. rc = ata_sff_wait_ready(link, deadline);
  1513. if (rc && rc != -ENODEV) {
  1514. ata_link_printk(link, KERN_WARNING, "device not ready "
  1515. "(errno=%d), forcing hardreset\n", rc);
  1516. ehc->i.action |= ATA_EH_HARDRESET;
  1517. }
  1518. }
  1519. return 0;
  1520. }
  1521. /**
  1522. * ata_devchk - PATA device presence detection
  1523. * @ap: ATA channel to examine
  1524. * @device: Device to examine (starting at zero)
  1525. *
  1526. * This technique was originally described in
  1527. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  1528. * later found its way into the ATA/ATAPI spec.
  1529. *
  1530. * Write a pattern to the ATA shadow registers,
  1531. * and if a device is present, it will respond by
  1532. * correctly storing and echoing back the
  1533. * ATA shadow register contents.
  1534. *
  1535. * LOCKING:
  1536. * caller.
  1537. */
  1538. static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  1539. {
  1540. struct ata_ioports *ioaddr = &ap->ioaddr;
  1541. u8 nsect, lbal;
  1542. ap->ops->sff_dev_select(ap, device);
  1543. iowrite8(0x55, ioaddr->nsect_addr);
  1544. iowrite8(0xaa, ioaddr->lbal_addr);
  1545. iowrite8(0xaa, ioaddr->nsect_addr);
  1546. iowrite8(0x55, ioaddr->lbal_addr);
  1547. iowrite8(0x55, ioaddr->nsect_addr);
  1548. iowrite8(0xaa, ioaddr->lbal_addr);
  1549. nsect = ioread8(ioaddr->nsect_addr);
  1550. lbal = ioread8(ioaddr->lbal_addr);
  1551. if ((nsect == 0x55) && (lbal == 0xaa))
  1552. return 1; /* we found a device */
  1553. return 0; /* nothing found */
  1554. }
  1555. /**
  1556. * ata_sff_dev_classify - Parse returned ATA device signature
  1557. * @dev: ATA device to classify (starting at zero)
  1558. * @present: device seems present
  1559. * @r_err: Value of error register on completion
  1560. *
  1561. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  1562. * an ATA/ATAPI-defined set of values is placed in the ATA
  1563. * shadow registers, indicating the results of device detection
  1564. * and diagnostics.
  1565. *
  1566. * Select the ATA device, and read the values from the ATA shadow
  1567. * registers. Then parse according to the Error register value,
  1568. * and the spec-defined values examined by ata_dev_classify().
  1569. *
  1570. * LOCKING:
  1571. * caller.
  1572. *
  1573. * RETURNS:
  1574. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  1575. */
  1576. unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
  1577. u8 *r_err)
  1578. {
  1579. struct ata_port *ap = dev->link->ap;
  1580. struct ata_taskfile tf;
  1581. unsigned int class;
  1582. u8 err;
  1583. ap->ops->sff_dev_select(ap, dev->devno);
  1584. memset(&tf, 0, sizeof(tf));
  1585. ap->ops->sff_tf_read(ap, &tf);
  1586. err = tf.feature;
  1587. if (r_err)
  1588. *r_err = err;
  1589. /* see if device passed diags: continue and warn later */
  1590. if (err == 0)
  1591. /* diagnostic fail : do nothing _YET_ */
  1592. dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
  1593. else if (err == 1)
  1594. /* do nothing */ ;
  1595. else if ((dev->devno == 0) && (err == 0x81))
  1596. /* do nothing */ ;
  1597. else
  1598. return ATA_DEV_NONE;
  1599. /* determine if device is ATA or ATAPI */
  1600. class = ata_dev_classify(&tf);
  1601. if (class == ATA_DEV_UNKNOWN) {
  1602. /* If the device failed diagnostic, it's likely to
  1603. * have reported incorrect device signature too.
  1604. * Assume ATA device if the device seems present but
  1605. * device signature is invalid with diagnostic
  1606. * failure.
  1607. */
  1608. if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
  1609. class = ATA_DEV_ATA;
  1610. else
  1611. class = ATA_DEV_NONE;
  1612. } else if ((class == ATA_DEV_ATA) &&
  1613. (ap->ops->sff_check_status(ap) == 0))
  1614. class = ATA_DEV_NONE;
  1615. return class;
  1616. }
  1617. /**
  1618. * ata_sff_wait_after_reset - wait for devices to become ready after reset
  1619. * @link: SFF link which is just reset
  1620. * @devmask: mask of present devices
  1621. * @deadline: deadline jiffies for the operation
  1622. *
  1623. * Wait devices attached to SFF @link to become ready after
  1624. * reset. It contains preceding 150ms wait to avoid accessing TF
  1625. * status register too early.
  1626. *
  1627. * LOCKING:
  1628. * Kernel thread context (may sleep).
  1629. *
  1630. * RETURNS:
  1631. * 0 on success, -ENODEV if some or all of devices in @devmask
  1632. * don't seem to exist. -errno on other errors.
  1633. */
  1634. int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
  1635. unsigned long deadline)
  1636. {
  1637. struct ata_port *ap = link->ap;
  1638. struct ata_ioports *ioaddr = &ap->ioaddr;
  1639. unsigned int dev0 = devmask & (1 << 0);
  1640. unsigned int dev1 = devmask & (1 << 1);
  1641. int rc, ret = 0;
  1642. msleep(ATA_WAIT_AFTER_RESET);
  1643. /* always check readiness of the master device */
  1644. rc = ata_sff_wait_ready(link, deadline);
  1645. /* -ENODEV means the odd clown forgot the D7 pulldown resistor
  1646. * and TF status is 0xff, bail out on it too.
  1647. */
  1648. if (rc)
  1649. return rc;
  1650. /* if device 1 was found in ata_devchk, wait for register
  1651. * access briefly, then wait for BSY to clear.
  1652. */
  1653. if (dev1) {
  1654. int i;
  1655. ap->ops->sff_dev_select(ap, 1);
  1656. /* Wait for register access. Some ATAPI devices fail
  1657. * to set nsect/lbal after reset, so don't waste too
  1658. * much time on it. We're gonna wait for !BSY anyway.
  1659. */
  1660. for (i = 0; i < 2; i++) {
  1661. u8 nsect, lbal;
  1662. nsect = ioread8(ioaddr->nsect_addr);
  1663. lbal = ioread8(ioaddr->lbal_addr);
  1664. if ((nsect == 1) && (lbal == 1))
  1665. break;
  1666. msleep(50); /* give drive a breather */
  1667. }
  1668. rc = ata_sff_wait_ready(link, deadline);
  1669. if (rc) {
  1670. if (rc != -ENODEV)
  1671. return rc;
  1672. ret = rc;
  1673. }
  1674. }
  1675. /* is all this really necessary? */
  1676. ap->ops->sff_dev_select(ap, 0);
  1677. if (dev1)
  1678. ap->ops->sff_dev_select(ap, 1);
  1679. if (dev0)
  1680. ap->ops->sff_dev_select(ap, 0);
  1681. return ret;
  1682. }
  1683. static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
  1684. unsigned long deadline)
  1685. {
  1686. struct ata_ioports *ioaddr = &ap->ioaddr;
  1687. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  1688. /* software reset. causes dev0 to be selected */
  1689. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1690. udelay(20); /* FIXME: flush */
  1691. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  1692. udelay(20); /* FIXME: flush */
  1693. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1694. /* wait the port to become ready */
  1695. return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
  1696. }
  1697. /**
  1698. * ata_sff_softreset - reset host port via ATA SRST
  1699. * @link: ATA link to reset
  1700. * @classes: resulting classes of attached devices
  1701. * @deadline: deadline jiffies for the operation
  1702. *
  1703. * Reset host port using ATA SRST.
  1704. *
  1705. * LOCKING:
  1706. * Kernel thread context (may sleep)
  1707. *
  1708. * RETURNS:
  1709. * 0 on success, -errno otherwise.
  1710. */
  1711. int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
  1712. unsigned long deadline)
  1713. {
  1714. struct ata_port *ap = link->ap;
  1715. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1716. unsigned int devmask = 0;
  1717. int rc;
  1718. u8 err;
  1719. DPRINTK("ENTER\n");
  1720. /* determine if device 0/1 are present */
  1721. if (ata_devchk(ap, 0))
  1722. devmask |= (1 << 0);
  1723. if (slave_possible && ata_devchk(ap, 1))
  1724. devmask |= (1 << 1);
  1725. /* select device 0 again */
  1726. ap->ops->sff_dev_select(ap, 0);
  1727. /* issue bus reset */
  1728. DPRINTK("about to softreset, devmask=%x\n", devmask);
  1729. rc = ata_bus_softreset(ap, devmask, deadline);
  1730. /* if link is occupied, -ENODEV too is an error */
  1731. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  1732. ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
  1733. return rc;
  1734. }
  1735. /* determine by signature whether we have ATA or ATAPI devices */
  1736. classes[0] = ata_sff_dev_classify(&link->device[0],
  1737. devmask & (1 << 0), &err);
  1738. if (slave_possible && err != 0x81)
  1739. classes[1] = ata_sff_dev_classify(&link->device[1],
  1740. devmask & (1 << 1), &err);
  1741. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  1742. return 0;
  1743. }
  1744. /**
  1745. * sata_sff_hardreset - reset host port via SATA phy reset
  1746. * @link: link to reset
  1747. * @class: resulting class of attached device
  1748. * @deadline: deadline jiffies for the operation
  1749. *
  1750. * SATA phy-reset host port using DET bits of SControl register,
  1751. * wait for !BSY and classify the attached device.
  1752. *
  1753. * LOCKING:
  1754. * Kernel thread context (may sleep)
  1755. *
  1756. * RETURNS:
  1757. * 0 on success, -errno otherwise.
  1758. */
  1759. int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
  1760. unsigned long deadline)
  1761. {
  1762. struct ata_eh_context *ehc = &link->eh_context;
  1763. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  1764. bool online;
  1765. int rc;
  1766. rc = sata_link_hardreset(link, timing, deadline, &online,
  1767. ata_sff_check_ready);
  1768. if (online)
  1769. *class = ata_sff_dev_classify(link->device, 1, NULL);
  1770. DPRINTK("EXIT, class=%u\n", *class);
  1771. return rc;
  1772. }
  1773. /**
  1774. * ata_sff_postreset - SFF postreset callback
  1775. * @link: the target SFF ata_link
  1776. * @classes: classes of attached devices
  1777. *
  1778. * This function is invoked after a successful reset. It first
  1779. * calls ata_std_postreset() and performs SFF specific postreset
  1780. * processing.
  1781. *
  1782. * LOCKING:
  1783. * Kernel thread context (may sleep)
  1784. */
  1785. void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
  1786. {
  1787. struct ata_port *ap = link->ap;
  1788. ata_std_postreset(link, classes);
  1789. /* is double-select really necessary? */
  1790. if (classes[0] != ATA_DEV_NONE)
  1791. ap->ops->sff_dev_select(ap, 1);
  1792. if (classes[1] != ATA_DEV_NONE)
  1793. ap->ops->sff_dev_select(ap, 0);
  1794. /* bail out if no device is present */
  1795. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1796. DPRINTK("EXIT, no device\n");
  1797. return;
  1798. }
  1799. /* set up device control */
  1800. if (ap->ioaddr.ctl_addr)
  1801. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  1802. }
  1803. /**
  1804. * ata_sff_error_handler - Stock error handler for BMDMA controller
  1805. * @ap: port to handle error for
  1806. *
  1807. * Stock error handler for SFF controller. It can handle both
  1808. * PATA and SATA controllers. Many controllers should be able to
  1809. * use this EH as-is or with some added handling before and
  1810. * after.
  1811. *
  1812. * LOCKING:
  1813. * Kernel thread context (may sleep)
  1814. */
  1815. void ata_sff_error_handler(struct ata_port *ap)
  1816. {
  1817. ata_reset_fn_t softreset = ap->ops->softreset;
  1818. ata_reset_fn_t hardreset = ap->ops->hardreset;
  1819. struct ata_queued_cmd *qc;
  1820. unsigned long flags;
  1821. int thaw = 0;
  1822. qc = __ata_qc_from_tag(ap, ap->link.active_tag);
  1823. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  1824. qc = NULL;
  1825. /* reset PIO HSM and stop DMA engine */
  1826. spin_lock_irqsave(ap->lock, flags);
  1827. ap->hsm_task_state = HSM_ST_IDLE;
  1828. if (ap->ioaddr.bmdma_addr &&
  1829. qc && (qc->tf.protocol == ATA_PROT_DMA ||
  1830. qc->tf.protocol == ATAPI_PROT_DMA)) {
  1831. u8 host_stat;
  1832. host_stat = ap->ops->bmdma_status(ap);
  1833. /* BMDMA controllers indicate host bus error by
  1834. * setting DMA_ERR bit and timing out. As it wasn't
  1835. * really a timeout event, adjust error mask and
  1836. * cancel frozen state.
  1837. */
  1838. if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
  1839. qc->err_mask = AC_ERR_HOST_BUS;
  1840. thaw = 1;
  1841. }
  1842. ap->ops->bmdma_stop(qc);
  1843. }
  1844. ata_sff_sync(ap); /* FIXME: We don't need this */
  1845. ap->ops->sff_check_status(ap);
  1846. ap->ops->sff_irq_clear(ap);
  1847. spin_unlock_irqrestore(ap->lock, flags);
  1848. if (thaw)
  1849. ata_eh_thaw_port(ap);
  1850. /* PIO and DMA engines have been stopped, perform recovery */
  1851. /* Ignore ata_sff_softreset if ctl isn't accessible and
  1852. * built-in hardresets if SCR access isn't available.
  1853. */
  1854. if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
  1855. softreset = NULL;
  1856. if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
  1857. hardreset = NULL;
  1858. ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
  1859. ap->ops->postreset);
  1860. }
  1861. /**
  1862. * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
  1863. * @qc: internal command to clean up
  1864. *
  1865. * LOCKING:
  1866. * Kernel thread context (may sleep)
  1867. */
  1868. void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
  1869. {
  1870. struct ata_port *ap = qc->ap;
  1871. unsigned long flags;
  1872. spin_lock_irqsave(ap->lock, flags);
  1873. ap->hsm_task_state = HSM_ST_IDLE;
  1874. if (ap->ioaddr.bmdma_addr)
  1875. ata_bmdma_stop(qc);
  1876. spin_unlock_irqrestore(ap->lock, flags);
  1877. }
  1878. /**
  1879. * ata_sff_port_start - Set port up for dma.
  1880. * @ap: Port to initialize
  1881. *
  1882. * Called just after data structures for each port are
  1883. * initialized. Allocates space for PRD table if the device
  1884. * is DMA capable SFF.
  1885. *
  1886. * May be used as the port_start() entry in ata_port_operations.
  1887. *
  1888. * LOCKING:
  1889. * Inherited from caller.
  1890. */
  1891. int ata_sff_port_start(struct ata_port *ap)
  1892. {
  1893. if (ap->ioaddr.bmdma_addr)
  1894. return ata_port_start(ap);
  1895. return 0;
  1896. }
  1897. /**
  1898. * ata_sff_std_ports - initialize ioaddr with standard port offsets.
  1899. * @ioaddr: IO address structure to be initialized
  1900. *
  1901. * Utility function which initializes data_addr, error_addr,
  1902. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  1903. * device_addr, status_addr, and command_addr to standard offsets
  1904. * relative to cmd_addr.
  1905. *
  1906. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  1907. */
  1908. void ata_sff_std_ports(struct ata_ioports *ioaddr)
  1909. {
  1910. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  1911. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  1912. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  1913. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  1914. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  1915. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  1916. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  1917. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  1918. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  1919. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  1920. }
  1921. unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
  1922. unsigned long xfer_mask)
  1923. {
  1924. /* Filter out DMA modes if the device has been configured by
  1925. the BIOS as PIO only */
  1926. if (adev->link->ap->ioaddr.bmdma_addr == NULL)
  1927. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  1928. return xfer_mask;
  1929. }
  1930. /**
  1931. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  1932. * @qc: Info associated with this ATA transaction.
  1933. *
  1934. * LOCKING:
  1935. * spin_lock_irqsave(host lock)
  1936. */
  1937. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  1938. {
  1939. struct ata_port *ap = qc->ap;
  1940. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  1941. u8 dmactl;
  1942. /* load PRD table addr. */
  1943. mb(); /* make sure PRD table writes are visible to controller */
  1944. iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  1945. /* specify data direction, triple-check start bit is clear */
  1946. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1947. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  1948. if (!rw)
  1949. dmactl |= ATA_DMA_WR;
  1950. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1951. /* issue r/w command */
  1952. ap->ops->sff_exec_command(ap, &qc->tf);
  1953. }
  1954. /**
  1955. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  1956. * @qc: Info associated with this ATA transaction.
  1957. *
  1958. * LOCKING:
  1959. * spin_lock_irqsave(host lock)
  1960. */
  1961. void ata_bmdma_start(struct ata_queued_cmd *qc)
  1962. {
  1963. struct ata_port *ap = qc->ap;
  1964. u8 dmactl;
  1965. /* start host DMA transaction */
  1966. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1967. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1968. /* Strictly, one may wish to issue an ioread8() here, to
  1969. * flush the mmio write. However, control also passes
  1970. * to the hardware at this point, and it will interrupt
  1971. * us when we are to resume control. So, in effect,
  1972. * we don't care when the mmio write flushes.
  1973. * Further, a read of the DMA status register _immediately_
  1974. * following the write may not be what certain flaky hardware
  1975. * is expected, so I think it is best to not add a readb()
  1976. * without first all the MMIO ATA cards/mobos.
  1977. * Or maybe I'm just being paranoid.
  1978. *
  1979. * FIXME: The posting of this write means I/O starts are
  1980. * unneccessarily delayed for MMIO
  1981. */
  1982. }
  1983. /**
  1984. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  1985. * @qc: Command we are ending DMA for
  1986. *
  1987. * Clears the ATA_DMA_START flag in the dma control register
  1988. *
  1989. * May be used as the bmdma_stop() entry in ata_port_operations.
  1990. *
  1991. * LOCKING:
  1992. * spin_lock_irqsave(host lock)
  1993. */
  1994. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  1995. {
  1996. struct ata_port *ap = qc->ap;
  1997. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  1998. /* clear start/stop bit */
  1999. iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  2000. mmio + ATA_DMA_CMD);
  2001. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  2002. ata_sff_dma_pause(ap);
  2003. }
  2004. /**
  2005. * ata_bmdma_status - Read PCI IDE BMDMA status
  2006. * @ap: Port associated with this ATA transaction.
  2007. *
  2008. * Read and return BMDMA status register.
  2009. *
  2010. * May be used as the bmdma_status() entry in ata_port_operations.
  2011. *
  2012. * LOCKING:
  2013. * spin_lock_irqsave(host lock)
  2014. */
  2015. u8 ata_bmdma_status(struct ata_port *ap)
  2016. {
  2017. return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  2018. }
  2019. /**
  2020. * ata_bus_reset - reset host port and associated ATA channel
  2021. * @ap: port to reset
  2022. *
  2023. * This is typically the first time we actually start issuing
  2024. * commands to the ATA channel. We wait for BSY to clear, then
  2025. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2026. * result. Determine what devices, if any, are on the channel
  2027. * by looking at the device 0/1 error register. Look at the signature
  2028. * stored in each device's taskfile registers, to determine if
  2029. * the device is ATA or ATAPI.
  2030. *
  2031. * LOCKING:
  2032. * PCI/etc. bus probe sem.
  2033. * Obtains host lock.
  2034. *
  2035. * SIDE EFFECTS:
  2036. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2037. *
  2038. * DEPRECATED:
  2039. * This function is only for drivers which still use old EH and
  2040. * will be removed soon.
  2041. */
  2042. void ata_bus_reset(struct ata_port *ap)
  2043. {
  2044. struct ata_device *device = ap->link.device;
  2045. struct ata_ioports *ioaddr = &ap->ioaddr;
  2046. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2047. u8 err;
  2048. unsigned int dev0, dev1 = 0, devmask = 0;
  2049. int rc;
  2050. DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
  2051. /* determine if device 0/1 are present */
  2052. if (ap->flags & ATA_FLAG_SATA_RESET)
  2053. dev0 = 1;
  2054. else {
  2055. dev0 = ata_devchk(ap, 0);
  2056. if (slave_possible)
  2057. dev1 = ata_devchk(ap, 1);
  2058. }
  2059. if (dev0)
  2060. devmask |= (1 << 0);
  2061. if (dev1)
  2062. devmask |= (1 << 1);
  2063. /* select device 0 again */
  2064. ap->ops->sff_dev_select(ap, 0);
  2065. /* issue bus reset */
  2066. if (ap->flags & ATA_FLAG_SRST) {
  2067. rc = ata_bus_softreset(ap, devmask,
  2068. ata_deadline(jiffies, 40000));
  2069. if (rc && rc != -ENODEV)
  2070. goto err_out;
  2071. }
  2072. /*
  2073. * determine by signature whether we have ATA or ATAPI devices
  2074. */
  2075. device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
  2076. if ((slave_possible) && (err != 0x81))
  2077. device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
  2078. /* is double-select really necessary? */
  2079. if (device[1].class != ATA_DEV_NONE)
  2080. ap->ops->sff_dev_select(ap, 1);
  2081. if (device[0].class != ATA_DEV_NONE)
  2082. ap->ops->sff_dev_select(ap, 0);
  2083. /* if no devices were detected, disable this port */
  2084. if ((device[0].class == ATA_DEV_NONE) &&
  2085. (device[1].class == ATA_DEV_NONE))
  2086. goto err_out;
  2087. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2088. /* set up device control for ATA_FLAG_SATA_RESET */
  2089. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2090. }
  2091. DPRINTK("EXIT\n");
  2092. return;
  2093. err_out:
  2094. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2095. ata_port_disable(ap);
  2096. DPRINTK("EXIT\n");
  2097. }
  2098. #ifdef CONFIG_PCI
  2099. /**
  2100. * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
  2101. * @pdev: PCI device
  2102. *
  2103. * Some PCI ATA devices report simplex mode but in fact can be told to
  2104. * enter non simplex mode. This implements the necessary logic to
  2105. * perform the task on such devices. Calling it on other devices will
  2106. * have -undefined- behaviour.
  2107. */
  2108. int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
  2109. {
  2110. unsigned long bmdma = pci_resource_start(pdev, 4);
  2111. u8 simplex;
  2112. if (bmdma == 0)
  2113. return -ENOENT;
  2114. simplex = inb(bmdma + 0x02);
  2115. outb(simplex & 0x60, bmdma + 0x02);
  2116. simplex = inb(bmdma + 0x02);
  2117. if (simplex & 0x80)
  2118. return -EOPNOTSUPP;
  2119. return 0;
  2120. }
  2121. /**
  2122. * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
  2123. * @host: target ATA host
  2124. *
  2125. * Acquire PCI BMDMA resources and initialize @host accordingly.
  2126. *
  2127. * LOCKING:
  2128. * Inherited from calling layer (may sleep).
  2129. *
  2130. * RETURNS:
  2131. * 0 on success, -errno otherwise.
  2132. */
  2133. int ata_pci_bmdma_init(struct ata_host *host)
  2134. {
  2135. struct device *gdev = host->dev;
  2136. struct pci_dev *pdev = to_pci_dev(gdev);
  2137. int i, rc;
  2138. /* No BAR4 allocation: No DMA */
  2139. if (pci_resource_start(pdev, 4) == 0)
  2140. return 0;
  2141. /* TODO: If we get no DMA mask we should fall back to PIO */
  2142. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  2143. if (rc)
  2144. return rc;
  2145. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  2146. if (rc)
  2147. return rc;
  2148. /* request and iomap DMA region */
  2149. rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
  2150. if (rc) {
  2151. dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
  2152. return -ENOMEM;
  2153. }
  2154. host->iomap = pcim_iomap_table(pdev);
  2155. for (i = 0; i < 2; i++) {
  2156. struct ata_port *ap = host->ports[i];
  2157. void __iomem *bmdma = host->iomap[4] + 8 * i;
  2158. if (ata_port_is_dummy(ap))
  2159. continue;
  2160. ap->ioaddr.bmdma_addr = bmdma;
  2161. if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  2162. (ioread8(bmdma + 2) & 0x80))
  2163. host->flags |= ATA_HOST_SIMPLEX;
  2164. ata_port_desc(ap, "bmdma 0x%llx",
  2165. (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
  2166. }
  2167. return 0;
  2168. }
  2169. static int ata_resources_present(struct pci_dev *pdev, int port)
  2170. {
  2171. int i;
  2172. /* Check the PCI resources for this channel are enabled */
  2173. port = port * 2;
  2174. for (i = 0; i < 2; i ++) {
  2175. if (pci_resource_start(pdev, port + i) == 0 ||
  2176. pci_resource_len(pdev, port + i) == 0)
  2177. return 0;
  2178. }
  2179. return 1;
  2180. }
  2181. /**
  2182. * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
  2183. * @host: target ATA host
  2184. *
  2185. * Acquire native PCI ATA resources for @host and initialize the
  2186. * first two ports of @host accordingly. Ports marked dummy are
  2187. * skipped and allocation failure makes the port dummy.
  2188. *
  2189. * Note that native PCI resources are valid even for legacy hosts
  2190. * as we fix up pdev resources array early in boot, so this
  2191. * function can be used for both native and legacy SFF hosts.
  2192. *
  2193. * LOCKING:
  2194. * Inherited from calling layer (may sleep).
  2195. *
  2196. * RETURNS:
  2197. * 0 if at least one port is initialized, -ENODEV if no port is
  2198. * available.
  2199. */
  2200. int ata_pci_sff_init_host(struct ata_host *host)
  2201. {
  2202. struct device *gdev = host->dev;
  2203. struct pci_dev *pdev = to_pci_dev(gdev);
  2204. unsigned int mask = 0;
  2205. int i, rc;
  2206. /* request, iomap BARs and init port addresses accordingly */
  2207. for (i = 0; i < 2; i++) {
  2208. struct ata_port *ap = host->ports[i];
  2209. int base = i * 2;
  2210. void __iomem * const *iomap;
  2211. if (ata_port_is_dummy(ap))
  2212. continue;
  2213. /* Discard disabled ports. Some controllers show
  2214. * their unused channels this way. Disabled ports are
  2215. * made dummy.
  2216. */
  2217. if (!ata_resources_present(pdev, i)) {
  2218. ap->ops = &ata_dummy_port_ops;
  2219. continue;
  2220. }
  2221. rc = pcim_iomap_regions(pdev, 0x3 << base,
  2222. dev_driver_string(gdev));
  2223. if (rc) {
  2224. dev_printk(KERN_WARNING, gdev,
  2225. "failed to request/iomap BARs for port %d "
  2226. "(errno=%d)\n", i, rc);
  2227. if (rc == -EBUSY)
  2228. pcim_pin_device(pdev);
  2229. ap->ops = &ata_dummy_port_ops;
  2230. continue;
  2231. }
  2232. host->iomap = iomap = pcim_iomap_table(pdev);
  2233. ap->ioaddr.cmd_addr = iomap[base];
  2234. ap->ioaddr.altstatus_addr =
  2235. ap->ioaddr.ctl_addr = (void __iomem *)
  2236. ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
  2237. ata_sff_std_ports(&ap->ioaddr);
  2238. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  2239. (unsigned long long)pci_resource_start(pdev, base),
  2240. (unsigned long long)pci_resource_start(pdev, base + 1));
  2241. mask |= 1 << i;
  2242. }
  2243. if (!mask) {
  2244. dev_printk(KERN_ERR, gdev, "no available native port\n");
  2245. return -ENODEV;
  2246. }
  2247. return 0;
  2248. }
  2249. /**
  2250. * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
  2251. * @pdev: target PCI device
  2252. * @ppi: array of port_info, must be enough for two ports
  2253. * @r_host: out argument for the initialized ATA host
  2254. *
  2255. * Helper to allocate ATA host for @pdev, acquire all native PCI
  2256. * resources and initialize it accordingly in one go.
  2257. *
  2258. * LOCKING:
  2259. * Inherited from calling layer (may sleep).
  2260. *
  2261. * RETURNS:
  2262. * 0 on success, -errno otherwise.
  2263. */
  2264. int ata_pci_sff_prepare_host(struct pci_dev *pdev,
  2265. const struct ata_port_info * const * ppi,
  2266. struct ata_host **r_host)
  2267. {
  2268. struct ata_host *host;
  2269. int rc;
  2270. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  2271. return -ENOMEM;
  2272. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  2273. if (!host) {
  2274. dev_printk(KERN_ERR, &pdev->dev,
  2275. "failed to allocate ATA host\n");
  2276. rc = -ENOMEM;
  2277. goto err_out;
  2278. }
  2279. rc = ata_pci_sff_init_host(host);
  2280. if (rc)
  2281. goto err_out;
  2282. /* init DMA related stuff */
  2283. rc = ata_pci_bmdma_init(host);
  2284. if (rc)
  2285. goto err_bmdma;
  2286. devres_remove_group(&pdev->dev, NULL);
  2287. *r_host = host;
  2288. return 0;
  2289. err_bmdma:
  2290. /* This is necessary because PCI and iomap resources are
  2291. * merged and releasing the top group won't release the
  2292. * acquired resources if some of those have been acquired
  2293. * before entering this function.
  2294. */
  2295. pcim_iounmap_regions(pdev, 0xf);
  2296. err_out:
  2297. devres_release_group(&pdev->dev, NULL);
  2298. return rc;
  2299. }
  2300. /**
  2301. * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
  2302. * @host: target SFF ATA host
  2303. * @irq_handler: irq_handler used when requesting IRQ(s)
  2304. * @sht: scsi_host_template to use when registering the host
  2305. *
  2306. * This is the counterpart of ata_host_activate() for SFF ATA
  2307. * hosts. This separate helper is necessary because SFF hosts
  2308. * use two separate interrupts in legacy mode.
  2309. *
  2310. * LOCKING:
  2311. * Inherited from calling layer (may sleep).
  2312. *
  2313. * RETURNS:
  2314. * 0 on success, -errno otherwise.
  2315. */
  2316. int ata_pci_sff_activate_host(struct ata_host *host,
  2317. irq_handler_t irq_handler,
  2318. struct scsi_host_template *sht)
  2319. {
  2320. struct device *dev = host->dev;
  2321. struct pci_dev *pdev = to_pci_dev(dev);
  2322. const char *drv_name = dev_driver_string(host->dev);
  2323. int legacy_mode = 0, rc;
  2324. rc = ata_host_start(host);
  2325. if (rc)
  2326. return rc;
  2327. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  2328. u8 tmp8, mask;
  2329. /* TODO: What if one channel is in native mode ... */
  2330. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  2331. mask = (1 << 2) | (1 << 0);
  2332. if ((tmp8 & mask) != mask)
  2333. legacy_mode = 1;
  2334. #if defined(CONFIG_NO_ATA_LEGACY)
  2335. /* Some platforms with PCI limits cannot address compat
  2336. port space. In that case we punt if their firmware has
  2337. left a device in compatibility mode */
  2338. if (legacy_mode) {
  2339. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  2340. return -EOPNOTSUPP;
  2341. }
  2342. #endif
  2343. }
  2344. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2345. return -ENOMEM;
  2346. if (!legacy_mode && pdev->irq) {
  2347. rc = devm_request_irq(dev, pdev->irq, irq_handler,
  2348. IRQF_SHARED, drv_name, host);
  2349. if (rc)
  2350. goto out;
  2351. ata_port_desc(host->ports[0], "irq %d", pdev->irq);
  2352. ata_port_desc(host->ports[1], "irq %d", pdev->irq);
  2353. } else if (legacy_mode) {
  2354. if (!ata_port_is_dummy(host->ports[0])) {
  2355. rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
  2356. irq_handler, IRQF_SHARED,
  2357. drv_name, host);
  2358. if (rc)
  2359. goto out;
  2360. ata_port_desc(host->ports[0], "irq %d",
  2361. ATA_PRIMARY_IRQ(pdev));
  2362. }
  2363. if (!ata_port_is_dummy(host->ports[1])) {
  2364. rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
  2365. irq_handler, IRQF_SHARED,
  2366. drv_name, host);
  2367. if (rc)
  2368. goto out;
  2369. ata_port_desc(host->ports[1], "irq %d",
  2370. ATA_SECONDARY_IRQ(pdev));
  2371. }
  2372. }
  2373. rc = ata_host_register(host, sht);
  2374. out:
  2375. if (rc == 0)
  2376. devres_remove_group(dev, NULL);
  2377. else
  2378. devres_release_group(dev, NULL);
  2379. return rc;
  2380. }
  2381. /**
  2382. * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
  2383. * @pdev: Controller to be initialized
  2384. * @ppi: array of port_info, must be enough for two ports
  2385. * @sht: scsi_host_template to use when registering the host
  2386. * @host_priv: host private_data
  2387. *
  2388. * This is a helper function which can be called from a driver's
  2389. * xxx_init_one() probe function if the hardware uses traditional
  2390. * IDE taskfile registers.
  2391. *
  2392. * This function calls pci_enable_device(), reserves its register
  2393. * regions, sets the dma mask, enables bus master mode, and calls
  2394. * ata_device_add()
  2395. *
  2396. * ASSUMPTION:
  2397. * Nobody makes a single channel controller that appears solely as
  2398. * the secondary legacy port on PCI.
  2399. *
  2400. * LOCKING:
  2401. * Inherited from PCI layer (may sleep).
  2402. *
  2403. * RETURNS:
  2404. * Zero on success, negative on errno-based value on error.
  2405. */
  2406. int ata_pci_sff_init_one(struct pci_dev *pdev,
  2407. const struct ata_port_info * const * ppi,
  2408. struct scsi_host_template *sht, void *host_priv)
  2409. {
  2410. struct device *dev = &pdev->dev;
  2411. const struct ata_port_info *pi = NULL;
  2412. struct ata_host *host = NULL;
  2413. int i, rc;
  2414. DPRINTK("ENTER\n");
  2415. /* look up the first valid port_info */
  2416. for (i = 0; i < 2 && ppi[i]; i++) {
  2417. if (ppi[i]->port_ops != &ata_dummy_port_ops) {
  2418. pi = ppi[i];
  2419. break;
  2420. }
  2421. }
  2422. if (!pi) {
  2423. dev_printk(KERN_ERR, &pdev->dev,
  2424. "no valid port_info specified\n");
  2425. return -EINVAL;
  2426. }
  2427. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2428. return -ENOMEM;
  2429. rc = pcim_enable_device(pdev);
  2430. if (rc)
  2431. goto out;
  2432. /* prepare and activate SFF host */
  2433. rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
  2434. if (rc)
  2435. goto out;
  2436. host->private_data = host_priv;
  2437. pci_set_master(pdev);
  2438. rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
  2439. out:
  2440. if (rc == 0)
  2441. devres_remove_group(&pdev->dev, NULL);
  2442. else
  2443. devres_release_group(&pdev->dev, NULL);
  2444. return rc;
  2445. }
  2446. #endif /* CONFIG_PCI */
  2447. EXPORT_SYMBOL_GPL(ata_sff_port_ops);
  2448. EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
  2449. EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
  2450. EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
  2451. EXPORT_SYMBOL_GPL(ata_sff_dev_select);
  2452. EXPORT_SYMBOL_GPL(ata_sff_check_status);
  2453. EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
  2454. EXPORT_SYMBOL_GPL(ata_sff_pause);
  2455. EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
  2456. EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
  2457. EXPORT_SYMBOL_GPL(ata_sff_tf_load);
  2458. EXPORT_SYMBOL_GPL(ata_sff_tf_read);
  2459. EXPORT_SYMBOL_GPL(ata_sff_exec_command);
  2460. EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
  2461. EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
  2462. EXPORT_SYMBOL_GPL(ata_sff_irq_on);
  2463. EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
  2464. EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
  2465. EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
  2466. EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
  2467. EXPORT_SYMBOL_GPL(ata_sff_host_intr);
  2468. EXPORT_SYMBOL_GPL(ata_sff_interrupt);
  2469. EXPORT_SYMBOL_GPL(ata_sff_freeze);
  2470. EXPORT_SYMBOL_GPL(ata_sff_thaw);
  2471. EXPORT_SYMBOL_GPL(ata_sff_prereset);
  2472. EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
  2473. EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
  2474. EXPORT_SYMBOL_GPL(ata_sff_softreset);
  2475. EXPORT_SYMBOL_GPL(sata_sff_hardreset);
  2476. EXPORT_SYMBOL_GPL(ata_sff_postreset);
  2477. EXPORT_SYMBOL_GPL(ata_sff_error_handler);
  2478. EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
  2479. EXPORT_SYMBOL_GPL(ata_sff_port_start);
  2480. EXPORT_SYMBOL_GPL(ata_sff_std_ports);
  2481. EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
  2482. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  2483. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  2484. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  2485. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  2486. EXPORT_SYMBOL_GPL(ata_bus_reset);
  2487. #ifdef CONFIG_PCI
  2488. EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
  2489. EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
  2490. EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
  2491. EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
  2492. EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
  2493. EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
  2494. #endif /* CONFIG_PCI */