libata-sff.c 79 KB

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