libata-sff.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074
  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 1 byte, if any. */
  649. if (unlikely(buflen & 0x01)) {
  650. __le16 align_buf[1] = { 0 };
  651. unsigned char *trailing_buf = buf + buflen - 1;
  652. if (rw == READ) {
  653. align_buf[0] = cpu_to_le16(ioread16(data_addr));
  654. memcpy(trailing_buf, align_buf, 1);
  655. } else {
  656. memcpy(align_buf, trailing_buf, 1);
  657. iowrite16(le16_to_cpu(align_buf[0]), data_addr);
  658. }
  659. words++;
  660. }
  661. return words << 1;
  662. }
  663. EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
  664. /**
  665. * ata_sff_data_xfer32 - Transfer data by PIO
  666. * @dev: device to target
  667. * @buf: data buffer
  668. * @buflen: buffer length
  669. * @rw: read/write
  670. *
  671. * Transfer data from/to the device data register by PIO using 32bit
  672. * I/O operations.
  673. *
  674. * LOCKING:
  675. * Inherited from caller.
  676. *
  677. * RETURNS:
  678. * Bytes consumed.
  679. */
  680. unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf,
  681. unsigned int buflen, int rw)
  682. {
  683. struct ata_port *ap = dev->link->ap;
  684. void __iomem *data_addr = ap->ioaddr.data_addr;
  685. unsigned int words = buflen >> 2;
  686. int slop = buflen & 3;
  687. if (!(ap->pflags & ATA_PFLAG_PIO32))
  688. return ata_sff_data_xfer(dev, buf, buflen, rw);
  689. /* Transfer multiple of 4 bytes */
  690. if (rw == READ)
  691. ioread32_rep(data_addr, buf, words);
  692. else
  693. iowrite32_rep(data_addr, buf, words);
  694. /* Transfer trailing bytes, if any */
  695. if (unlikely(slop)) {
  696. unsigned char pad[4];
  697. /* Point buf to the tail of buffer */
  698. buf += buflen - slop;
  699. /*
  700. * Use io*_rep() accessors here as well to avoid pointlessly
  701. * swapping bytes to and fro on the big endian machines...
  702. */
  703. if (rw == READ) {
  704. if (slop < 3)
  705. ioread16_rep(data_addr, pad, 1);
  706. else
  707. ioread32_rep(data_addr, pad, 1);
  708. memcpy(buf, pad, slop);
  709. } else {
  710. memcpy(pad, buf, slop);
  711. if (slop < 3)
  712. iowrite16_rep(data_addr, pad, 1);
  713. else
  714. iowrite32_rep(data_addr, pad, 1);
  715. }
  716. }
  717. return (buflen + 1) & ~1;
  718. }
  719. EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
  720. /**
  721. * ata_sff_data_xfer_noirq - Transfer data by PIO
  722. * @dev: device to target
  723. * @buf: data buffer
  724. * @buflen: buffer length
  725. * @rw: read/write
  726. *
  727. * Transfer data from/to the device data register by PIO. Do the
  728. * transfer with interrupts disabled.
  729. *
  730. * LOCKING:
  731. * Inherited from caller.
  732. *
  733. * RETURNS:
  734. * Bytes consumed.
  735. */
  736. unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  737. unsigned int buflen, int rw)
  738. {
  739. unsigned long flags;
  740. unsigned int consumed;
  741. local_irq_save(flags);
  742. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  743. local_irq_restore(flags);
  744. return consumed;
  745. }
  746. EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
  747. /**
  748. * ata_pio_sector - Transfer a sector of data.
  749. * @qc: Command on going
  750. *
  751. * Transfer qc->sect_size bytes of data from/to the ATA device.
  752. *
  753. * LOCKING:
  754. * Inherited from caller.
  755. */
  756. static void ata_pio_sector(struct ata_queued_cmd *qc)
  757. {
  758. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  759. struct ata_port *ap = qc->ap;
  760. struct page *page;
  761. unsigned int offset;
  762. unsigned char *buf;
  763. if (qc->curbytes == qc->nbytes - qc->sect_size)
  764. ap->hsm_task_state = HSM_ST_LAST;
  765. page = sg_page(qc->cursg);
  766. offset = qc->cursg->offset + qc->cursg_ofs;
  767. /* get the current page and offset */
  768. page = nth_page(page, (offset >> PAGE_SHIFT));
  769. offset %= PAGE_SIZE;
  770. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  771. if (PageHighMem(page)) {
  772. unsigned long flags;
  773. /* FIXME: use a bounce buffer */
  774. local_irq_save(flags);
  775. buf = kmap_atomic(page, KM_IRQ0);
  776. /* do the actual data transfer */
  777. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  778. do_write);
  779. kunmap_atomic(buf, KM_IRQ0);
  780. local_irq_restore(flags);
  781. } else {
  782. buf = page_address(page);
  783. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  784. do_write);
  785. }
  786. qc->curbytes += qc->sect_size;
  787. qc->cursg_ofs += qc->sect_size;
  788. if (qc->cursg_ofs == qc->cursg->length) {
  789. qc->cursg = sg_next(qc->cursg);
  790. qc->cursg_ofs = 0;
  791. }
  792. }
  793. /**
  794. * ata_pio_sectors - Transfer one or many sectors.
  795. * @qc: Command on going
  796. *
  797. * Transfer one or many sectors of data from/to the
  798. * ATA device for the DRQ request.
  799. *
  800. * LOCKING:
  801. * Inherited from caller.
  802. */
  803. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  804. {
  805. if (is_multi_taskfile(&qc->tf)) {
  806. /* READ/WRITE MULTIPLE */
  807. unsigned int nsect;
  808. WARN_ON_ONCE(qc->dev->multi_count == 0);
  809. nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
  810. qc->dev->multi_count);
  811. while (nsect--)
  812. ata_pio_sector(qc);
  813. } else
  814. ata_pio_sector(qc);
  815. ata_sff_sync(qc->ap); /* flush */
  816. }
  817. /**
  818. * atapi_send_cdb - Write CDB bytes to hardware
  819. * @ap: Port to which ATAPI device is attached.
  820. * @qc: Taskfile currently active
  821. *
  822. * When device has indicated its readiness to accept
  823. * a CDB, this function is called. Send the CDB.
  824. *
  825. * LOCKING:
  826. * caller.
  827. */
  828. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  829. {
  830. /* send SCSI cdb */
  831. DPRINTK("send cdb\n");
  832. WARN_ON_ONCE(qc->dev->cdb_len < 12);
  833. ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  834. ata_sff_sync(ap);
  835. /* FIXME: If the CDB is for DMA do we need to do the transition delay
  836. or is bmdma_start guaranteed to do it ? */
  837. switch (qc->tf.protocol) {
  838. case ATAPI_PROT_PIO:
  839. ap->hsm_task_state = HSM_ST;
  840. break;
  841. case ATAPI_PROT_NODATA:
  842. ap->hsm_task_state = HSM_ST_LAST;
  843. break;
  844. case ATAPI_PROT_DMA:
  845. ap->hsm_task_state = HSM_ST_LAST;
  846. /* initiate bmdma */
  847. ap->ops->bmdma_start(qc);
  848. break;
  849. }
  850. }
  851. /**
  852. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  853. * @qc: Command on going
  854. * @bytes: number of bytes
  855. *
  856. * Transfer Transfer data from/to the ATAPI device.
  857. *
  858. * LOCKING:
  859. * Inherited from caller.
  860. *
  861. */
  862. static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  863. {
  864. int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
  865. struct ata_port *ap = qc->ap;
  866. struct ata_device *dev = qc->dev;
  867. struct ata_eh_info *ehi = &dev->link->eh_info;
  868. struct scatterlist *sg;
  869. struct page *page;
  870. unsigned char *buf;
  871. unsigned int offset, count, consumed;
  872. next_sg:
  873. sg = qc->cursg;
  874. if (unlikely(!sg)) {
  875. ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
  876. "buf=%u cur=%u bytes=%u",
  877. qc->nbytes, qc->curbytes, bytes);
  878. return -1;
  879. }
  880. page = sg_page(sg);
  881. offset = sg->offset + qc->cursg_ofs;
  882. /* get the current page and offset */
  883. page = nth_page(page, (offset >> PAGE_SHIFT));
  884. offset %= PAGE_SIZE;
  885. /* don't overrun current sg */
  886. count = min(sg->length - qc->cursg_ofs, bytes);
  887. /* don't cross page boundaries */
  888. count = min(count, (unsigned int)PAGE_SIZE - offset);
  889. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  890. if (PageHighMem(page)) {
  891. unsigned long flags;
  892. /* FIXME: use bounce buffer */
  893. local_irq_save(flags);
  894. buf = kmap_atomic(page, KM_IRQ0);
  895. /* do the actual data transfer */
  896. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  897. count, rw);
  898. kunmap_atomic(buf, KM_IRQ0);
  899. local_irq_restore(flags);
  900. } else {
  901. buf = page_address(page);
  902. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  903. count, rw);
  904. }
  905. bytes -= min(bytes, consumed);
  906. qc->curbytes += count;
  907. qc->cursg_ofs += count;
  908. if (qc->cursg_ofs == sg->length) {
  909. qc->cursg = sg_next(qc->cursg);
  910. qc->cursg_ofs = 0;
  911. }
  912. /*
  913. * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed);
  914. * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN
  915. * check correctly as it doesn't know if it is the last request being
  916. * made. Somebody should implement a proper sanity check.
  917. */
  918. if (bytes)
  919. goto next_sg;
  920. return 0;
  921. }
  922. /**
  923. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  924. * @qc: Command on going
  925. *
  926. * Transfer Transfer data from/to the ATAPI device.
  927. *
  928. * LOCKING:
  929. * Inherited from caller.
  930. */
  931. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  932. {
  933. struct ata_port *ap = qc->ap;
  934. struct ata_device *dev = qc->dev;
  935. struct ata_eh_info *ehi = &dev->link->eh_info;
  936. unsigned int ireason, bc_lo, bc_hi, bytes;
  937. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  938. /* Abuse qc->result_tf for temp storage of intermediate TF
  939. * here to save some kernel stack usage.
  940. * For normal completion, qc->result_tf is not relevant. For
  941. * error, qc->result_tf is later overwritten by ata_qc_complete().
  942. * So, the correctness of qc->result_tf is not affected.
  943. */
  944. ap->ops->sff_tf_read(ap, &qc->result_tf);
  945. ireason = qc->result_tf.nsect;
  946. bc_lo = qc->result_tf.lbam;
  947. bc_hi = qc->result_tf.lbah;
  948. bytes = (bc_hi << 8) | bc_lo;
  949. /* shall be cleared to zero, indicating xfer of data */
  950. if (unlikely(ireason & (1 << 0)))
  951. goto atapi_check;
  952. /* make sure transfer direction matches expected */
  953. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  954. if (unlikely(do_write != i_write))
  955. goto atapi_check;
  956. if (unlikely(!bytes))
  957. goto atapi_check;
  958. VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
  959. if (unlikely(__atapi_pio_bytes(qc, bytes)))
  960. goto err_out;
  961. ata_sff_sync(ap); /* flush */
  962. return;
  963. atapi_check:
  964. ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
  965. ireason, bytes);
  966. err_out:
  967. qc->err_mask |= AC_ERR_HSM;
  968. ap->hsm_task_state = HSM_ST_ERR;
  969. }
  970. /**
  971. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  972. * @ap: the target ata_port
  973. * @qc: qc on going
  974. *
  975. * RETURNS:
  976. * 1 if ok in workqueue, 0 otherwise.
  977. */
  978. static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
  979. struct ata_queued_cmd *qc)
  980. {
  981. if (qc->tf.flags & ATA_TFLAG_POLLING)
  982. return 1;
  983. if (ap->hsm_task_state == HSM_ST_FIRST) {
  984. if (qc->tf.protocol == ATA_PROT_PIO &&
  985. (qc->tf.flags & ATA_TFLAG_WRITE))
  986. return 1;
  987. if (ata_is_atapi(qc->tf.protocol) &&
  988. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. /**
  994. * ata_hsm_qc_complete - finish a qc running on standard HSM
  995. * @qc: Command to complete
  996. * @in_wq: 1 if called from workqueue, 0 otherwise
  997. *
  998. * Finish @qc which is running on standard HSM.
  999. *
  1000. * LOCKING:
  1001. * If @in_wq is zero, spin_lock_irqsave(host lock).
  1002. * Otherwise, none on entry and grabs host lock.
  1003. */
  1004. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  1005. {
  1006. struct ata_port *ap = qc->ap;
  1007. unsigned long flags;
  1008. if (ap->ops->error_handler) {
  1009. if (in_wq) {
  1010. spin_lock_irqsave(ap->lock, flags);
  1011. /* EH might have kicked in while host lock is
  1012. * released.
  1013. */
  1014. qc = ata_qc_from_tag(ap, qc->tag);
  1015. if (qc) {
  1016. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  1017. ap->ops->sff_irq_on(ap);
  1018. ata_qc_complete(qc);
  1019. } else
  1020. ata_port_freeze(ap);
  1021. }
  1022. spin_unlock_irqrestore(ap->lock, flags);
  1023. } else {
  1024. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  1025. ata_qc_complete(qc);
  1026. else
  1027. ata_port_freeze(ap);
  1028. }
  1029. } else {
  1030. if (in_wq) {
  1031. spin_lock_irqsave(ap->lock, flags);
  1032. ap->ops->sff_irq_on(ap);
  1033. ata_qc_complete(qc);
  1034. spin_unlock_irqrestore(ap->lock, flags);
  1035. } else
  1036. ata_qc_complete(qc);
  1037. }
  1038. }
  1039. /**
  1040. * ata_sff_hsm_move - move the HSM to the next state.
  1041. * @ap: the target ata_port
  1042. * @qc: qc on going
  1043. * @status: current device status
  1044. * @in_wq: 1 if called from workqueue, 0 otherwise
  1045. *
  1046. * RETURNS:
  1047. * 1 when poll next status needed, 0 otherwise.
  1048. */
  1049. int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  1050. u8 status, int in_wq)
  1051. {
  1052. struct ata_eh_info *ehi = &ap->link.eh_info;
  1053. unsigned long flags = 0;
  1054. int poll_next;
  1055. WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  1056. /* Make sure ata_sff_qc_issue() does not throw things
  1057. * like DMA polling into the workqueue. Notice that
  1058. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  1059. */
  1060. WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
  1061. fsm_start:
  1062. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  1063. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  1064. switch (ap->hsm_task_state) {
  1065. case HSM_ST_FIRST:
  1066. /* Send first data block or PACKET CDB */
  1067. /* If polling, we will stay in the work queue after
  1068. * sending the data. Otherwise, interrupt handler
  1069. * takes over after sending the data.
  1070. */
  1071. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  1072. /* check device status */
  1073. if (unlikely((status & ATA_DRQ) == 0)) {
  1074. /* handle BSY=0, DRQ=0 as error */
  1075. if (likely(status & (ATA_ERR | ATA_DF)))
  1076. /* device stops HSM for abort/error */
  1077. qc->err_mask |= AC_ERR_DEV;
  1078. else {
  1079. /* HSM violation. Let EH handle this */
  1080. ata_ehi_push_desc(ehi,
  1081. "ST_FIRST: !(DRQ|ERR|DF)");
  1082. qc->err_mask |= AC_ERR_HSM;
  1083. }
  1084. ap->hsm_task_state = HSM_ST_ERR;
  1085. goto fsm_start;
  1086. }
  1087. /* Device should not ask for data transfer (DRQ=1)
  1088. * when it finds something wrong.
  1089. * We ignore DRQ here and stop the HSM by
  1090. * changing hsm_task_state to HSM_ST_ERR and
  1091. * let the EH abort the command or reset the device.
  1092. */
  1093. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1094. /* Some ATAPI tape drives forget to clear the ERR bit
  1095. * when doing the next command (mostly request sense).
  1096. * We ignore ERR here to workaround and proceed sending
  1097. * the CDB.
  1098. */
  1099. if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
  1100. ata_ehi_push_desc(ehi, "ST_FIRST: "
  1101. "DRQ=1 with device error, "
  1102. "dev_stat 0x%X", status);
  1103. qc->err_mask |= AC_ERR_HSM;
  1104. ap->hsm_task_state = HSM_ST_ERR;
  1105. goto fsm_start;
  1106. }
  1107. }
  1108. /* Send the CDB (atapi) or the first data block (ata pio out).
  1109. * During the state transition, interrupt handler shouldn't
  1110. * be invoked before the data transfer is complete and
  1111. * hsm_task_state is changed. Hence, the following locking.
  1112. */
  1113. if (in_wq)
  1114. spin_lock_irqsave(ap->lock, flags);
  1115. if (qc->tf.protocol == ATA_PROT_PIO) {
  1116. /* PIO data out protocol.
  1117. * send first data block.
  1118. */
  1119. /* ata_pio_sectors() might change the state
  1120. * to HSM_ST_LAST. so, the state is changed here
  1121. * before ata_pio_sectors().
  1122. */
  1123. ap->hsm_task_state = HSM_ST;
  1124. ata_pio_sectors(qc);
  1125. } else
  1126. /* send CDB */
  1127. atapi_send_cdb(ap, qc);
  1128. if (in_wq)
  1129. spin_unlock_irqrestore(ap->lock, flags);
  1130. /* if polling, ata_pio_task() handles the rest.
  1131. * otherwise, interrupt handler takes over from here.
  1132. */
  1133. break;
  1134. case HSM_ST:
  1135. /* complete command or read/write the data register */
  1136. if (qc->tf.protocol == ATAPI_PROT_PIO) {
  1137. /* ATAPI PIO protocol */
  1138. if ((status & ATA_DRQ) == 0) {
  1139. /* No more data to transfer or device error.
  1140. * Device error will be tagged in HSM_ST_LAST.
  1141. */
  1142. ap->hsm_task_state = HSM_ST_LAST;
  1143. goto fsm_start;
  1144. }
  1145. /* Device should not ask for data transfer (DRQ=1)
  1146. * when it finds something wrong.
  1147. * We ignore DRQ here and stop the HSM by
  1148. * changing hsm_task_state to HSM_ST_ERR and
  1149. * let the EH abort the command or reset the device.
  1150. */
  1151. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1152. ata_ehi_push_desc(ehi, "ST-ATAPI: "
  1153. "DRQ=1 with device error, "
  1154. "dev_stat 0x%X", status);
  1155. qc->err_mask |= AC_ERR_HSM;
  1156. ap->hsm_task_state = HSM_ST_ERR;
  1157. goto fsm_start;
  1158. }
  1159. atapi_pio_bytes(qc);
  1160. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  1161. /* bad ireason reported by device */
  1162. goto fsm_start;
  1163. } else {
  1164. /* ATA PIO protocol */
  1165. if (unlikely((status & ATA_DRQ) == 0)) {
  1166. /* handle BSY=0, DRQ=0 as error */
  1167. if (likely(status & (ATA_ERR | ATA_DF))) {
  1168. /* device stops HSM for abort/error */
  1169. qc->err_mask |= AC_ERR_DEV;
  1170. /* If diagnostic failed and this is
  1171. * IDENTIFY, it's likely a phantom
  1172. * device. Mark hint.
  1173. */
  1174. if (qc->dev->horkage &
  1175. ATA_HORKAGE_DIAGNOSTIC)
  1176. qc->err_mask |=
  1177. AC_ERR_NODEV_HINT;
  1178. } else {
  1179. /* HSM violation. Let EH handle this.
  1180. * Phantom devices also trigger this
  1181. * condition. Mark hint.
  1182. */
  1183. ata_ehi_push_desc(ehi, "ST-ATA: "
  1184. "DRQ=0 without device error, "
  1185. "dev_stat 0x%X", status);
  1186. qc->err_mask |= AC_ERR_HSM |
  1187. AC_ERR_NODEV_HINT;
  1188. }
  1189. ap->hsm_task_state = HSM_ST_ERR;
  1190. goto fsm_start;
  1191. }
  1192. /* For PIO reads, some devices may ask for
  1193. * data transfer (DRQ=1) alone with ERR=1.
  1194. * We respect DRQ here and transfer one
  1195. * block of junk data before changing the
  1196. * hsm_task_state to HSM_ST_ERR.
  1197. *
  1198. * For PIO writes, ERR=1 DRQ=1 doesn't make
  1199. * sense since the data block has been
  1200. * transferred to the device.
  1201. */
  1202. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1203. /* data might be corrputed */
  1204. qc->err_mask |= AC_ERR_DEV;
  1205. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  1206. ata_pio_sectors(qc);
  1207. status = ata_wait_idle(ap);
  1208. }
  1209. if (status & (ATA_BUSY | ATA_DRQ)) {
  1210. ata_ehi_push_desc(ehi, "ST-ATA: "
  1211. "BUSY|DRQ persists on ERR|DF, "
  1212. "dev_stat 0x%X", status);
  1213. qc->err_mask |= AC_ERR_HSM;
  1214. }
  1215. /* There are oddball controllers with
  1216. * status register stuck at 0x7f and
  1217. * lbal/m/h at zero which makes it
  1218. * pass all other presence detection
  1219. * mechanisms we have. Set NODEV_HINT
  1220. * for it. Kernel bz#7241.
  1221. */
  1222. if (status == 0x7f)
  1223. qc->err_mask |= AC_ERR_NODEV_HINT;
  1224. /* ata_pio_sectors() might change the
  1225. * state to HSM_ST_LAST. so, the state
  1226. * is changed after ata_pio_sectors().
  1227. */
  1228. ap->hsm_task_state = HSM_ST_ERR;
  1229. goto fsm_start;
  1230. }
  1231. ata_pio_sectors(qc);
  1232. if (ap->hsm_task_state == HSM_ST_LAST &&
  1233. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  1234. /* all data read */
  1235. status = ata_wait_idle(ap);
  1236. goto fsm_start;
  1237. }
  1238. }
  1239. poll_next = 1;
  1240. break;
  1241. case HSM_ST_LAST:
  1242. if (unlikely(!ata_ok(status))) {
  1243. qc->err_mask |= __ac_err_mask(status);
  1244. ap->hsm_task_state = HSM_ST_ERR;
  1245. goto fsm_start;
  1246. }
  1247. /* no more data to transfer */
  1248. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  1249. ap->print_id, qc->dev->devno, status);
  1250. WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
  1251. ap->hsm_task_state = HSM_ST_IDLE;
  1252. /* complete taskfile transaction */
  1253. ata_hsm_qc_complete(qc, in_wq);
  1254. poll_next = 0;
  1255. break;
  1256. case HSM_ST_ERR:
  1257. ap->hsm_task_state = HSM_ST_IDLE;
  1258. /* complete taskfile transaction */
  1259. ata_hsm_qc_complete(qc, in_wq);
  1260. poll_next = 0;
  1261. break;
  1262. default:
  1263. poll_next = 0;
  1264. BUG();
  1265. }
  1266. return poll_next;
  1267. }
  1268. EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
  1269. void ata_pio_task(struct work_struct *work)
  1270. {
  1271. struct ata_port *ap =
  1272. container_of(work, struct ata_port, port_task.work);
  1273. struct ata_queued_cmd *qc = ap->port_task_data;
  1274. u8 status;
  1275. int poll_next;
  1276. fsm_start:
  1277. WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
  1278. /*
  1279. * This is purely heuristic. This is a fast path.
  1280. * Sometimes when we enter, BSY will be cleared in
  1281. * a chk-status or two. If not, the drive is probably seeking
  1282. * or something. Snooze for a couple msecs, then
  1283. * chk-status again. If still busy, queue delayed work.
  1284. */
  1285. status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
  1286. if (status & ATA_BUSY) {
  1287. msleep(2);
  1288. status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
  1289. if (status & ATA_BUSY) {
  1290. ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
  1291. return;
  1292. }
  1293. }
  1294. /* move the HSM */
  1295. poll_next = ata_sff_hsm_move(ap, qc, status, 1);
  1296. /* another command or interrupt handler
  1297. * may be running at this point.
  1298. */
  1299. if (poll_next)
  1300. goto fsm_start;
  1301. }
  1302. /**
  1303. * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
  1304. * @qc: command to issue to device
  1305. *
  1306. * Using various libata functions and hooks, this function
  1307. * starts an ATA command. ATA commands are grouped into
  1308. * classes called "protocols", and issuing each type of protocol
  1309. * is slightly different.
  1310. *
  1311. * May be used as the qc_issue() entry in ata_port_operations.
  1312. *
  1313. * LOCKING:
  1314. * spin_lock_irqsave(host lock)
  1315. *
  1316. * RETURNS:
  1317. * Zero on success, AC_ERR_* mask on failure
  1318. */
  1319. unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
  1320. {
  1321. struct ata_port *ap = qc->ap;
  1322. /* Use polling pio if the LLD doesn't handle
  1323. * interrupt driven pio and atapi CDB interrupt.
  1324. */
  1325. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  1326. switch (qc->tf.protocol) {
  1327. case ATA_PROT_PIO:
  1328. case ATA_PROT_NODATA:
  1329. case ATAPI_PROT_PIO:
  1330. case ATAPI_PROT_NODATA:
  1331. qc->tf.flags |= ATA_TFLAG_POLLING;
  1332. break;
  1333. case ATAPI_PROT_DMA:
  1334. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  1335. /* see ata_dma_blacklisted() */
  1336. BUG();
  1337. break;
  1338. default:
  1339. break;
  1340. }
  1341. }
  1342. /* select the device */
  1343. ata_dev_select(ap, qc->dev->devno, 1, 0);
  1344. /* start the command */
  1345. switch (qc->tf.protocol) {
  1346. case ATA_PROT_NODATA:
  1347. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1348. ata_qc_set_polling(qc);
  1349. ata_tf_to_host(ap, &qc->tf);
  1350. ap->hsm_task_state = HSM_ST_LAST;
  1351. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1352. ata_pio_queue_task(ap, qc, 0);
  1353. break;
  1354. case ATA_PROT_DMA:
  1355. WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
  1356. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1357. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1358. ap->ops->bmdma_start(qc); /* initiate bmdma */
  1359. ap->hsm_task_state = HSM_ST_LAST;
  1360. break;
  1361. case ATA_PROT_PIO:
  1362. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1363. ata_qc_set_polling(qc);
  1364. ata_tf_to_host(ap, &qc->tf);
  1365. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  1366. /* PIO data out protocol */
  1367. ap->hsm_task_state = HSM_ST_FIRST;
  1368. ata_pio_queue_task(ap, qc, 0);
  1369. /* always send first data block using
  1370. * the ata_pio_task() codepath.
  1371. */
  1372. } else {
  1373. /* PIO data in protocol */
  1374. ap->hsm_task_state = HSM_ST;
  1375. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1376. ata_pio_queue_task(ap, qc, 0);
  1377. /* if polling, ata_pio_task() handles the rest.
  1378. * otherwise, interrupt handler takes over from here.
  1379. */
  1380. }
  1381. break;
  1382. case ATAPI_PROT_PIO:
  1383. case ATAPI_PROT_NODATA:
  1384. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1385. ata_qc_set_polling(qc);
  1386. ata_tf_to_host(ap, &qc->tf);
  1387. ap->hsm_task_state = HSM_ST_FIRST;
  1388. /* send cdb by polling if no cdb interrupt */
  1389. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  1390. (qc->tf.flags & ATA_TFLAG_POLLING))
  1391. ata_pio_queue_task(ap, qc, 0);
  1392. break;
  1393. case ATAPI_PROT_DMA:
  1394. WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
  1395. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1396. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1397. ap->hsm_task_state = HSM_ST_FIRST;
  1398. /* send cdb by polling if no cdb interrupt */
  1399. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1400. ata_pio_queue_task(ap, qc, 0);
  1401. break;
  1402. default:
  1403. WARN_ON_ONCE(1);
  1404. return AC_ERR_SYSTEM;
  1405. }
  1406. return 0;
  1407. }
  1408. EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
  1409. /**
  1410. * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
  1411. * @qc: qc to fill result TF for
  1412. *
  1413. * @qc is finished and result TF needs to be filled. Fill it
  1414. * using ->sff_tf_read.
  1415. *
  1416. * LOCKING:
  1417. * spin_lock_irqsave(host lock)
  1418. *
  1419. * RETURNS:
  1420. * true indicating that result TF is successfully filled.
  1421. */
  1422. bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
  1423. {
  1424. qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
  1425. return true;
  1426. }
  1427. EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
  1428. /**
  1429. * ata_sff_host_intr - Handle host interrupt for given (port, task)
  1430. * @ap: Port on which interrupt arrived (possibly...)
  1431. * @qc: Taskfile currently active in engine
  1432. *
  1433. * Handle host interrupt for given queued command. Currently,
  1434. * only DMA interrupts are handled. All other commands are
  1435. * handled via polling with interrupts disabled (nIEN bit).
  1436. *
  1437. * LOCKING:
  1438. * spin_lock_irqsave(host lock)
  1439. *
  1440. * RETURNS:
  1441. * One if interrupt was handled, zero if not (shared irq).
  1442. */
  1443. unsigned int ata_sff_host_intr(struct ata_port *ap,
  1444. struct ata_queued_cmd *qc)
  1445. {
  1446. struct ata_eh_info *ehi = &ap->link.eh_info;
  1447. u8 status, host_stat = 0;
  1448. VPRINTK("ata%u: protocol %d task_state %d\n",
  1449. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  1450. /* Check whether we are expecting interrupt in this state */
  1451. switch (ap->hsm_task_state) {
  1452. case HSM_ST_FIRST:
  1453. /* Some pre-ATAPI-4 devices assert INTRQ
  1454. * at this state when ready to receive CDB.
  1455. */
  1456. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  1457. * The flag was turned on only for atapi devices. No
  1458. * need to check ata_is_atapi(qc->tf.protocol) again.
  1459. */
  1460. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1461. goto idle_irq;
  1462. break;
  1463. case HSM_ST_LAST:
  1464. if (qc->tf.protocol == ATA_PROT_DMA ||
  1465. qc->tf.protocol == ATAPI_PROT_DMA) {
  1466. /* check status of DMA engine */
  1467. host_stat = ap->ops->bmdma_status(ap);
  1468. VPRINTK("ata%u: host_stat 0x%X\n",
  1469. ap->print_id, host_stat);
  1470. /* if it's not our irq... */
  1471. if (!(host_stat & ATA_DMA_INTR))
  1472. goto idle_irq;
  1473. /* before we do anything else, clear DMA-Start bit */
  1474. ap->ops->bmdma_stop(qc);
  1475. if (unlikely(host_stat & ATA_DMA_ERR)) {
  1476. /* error when transfering data to/from memory */
  1477. qc->err_mask |= AC_ERR_HOST_BUS;
  1478. ap->hsm_task_state = HSM_ST_ERR;
  1479. }
  1480. }
  1481. break;
  1482. case HSM_ST:
  1483. break;
  1484. default:
  1485. goto idle_irq;
  1486. }
  1487. /* check main status, clearing INTRQ if needed */
  1488. status = ata_sff_irq_status(ap);
  1489. if (status & ATA_BUSY)
  1490. goto idle_irq;
  1491. /* ack bmdma irq events */
  1492. ap->ops->sff_irq_clear(ap);
  1493. ata_sff_hsm_move(ap, qc, status, 0);
  1494. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  1495. qc->tf.protocol == ATAPI_PROT_DMA))
  1496. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  1497. return 1; /* irq handled */
  1498. idle_irq:
  1499. ap->stats.idle_irq++;
  1500. #ifdef ATA_IRQ_TRAP
  1501. if ((ap->stats.idle_irq % 1000) == 0) {
  1502. ap->ops->sff_check_status(ap);
  1503. ap->ops->sff_irq_clear(ap);
  1504. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  1505. return 1;
  1506. }
  1507. #endif
  1508. return 0; /* irq not handled */
  1509. }
  1510. EXPORT_SYMBOL_GPL(ata_sff_host_intr);
  1511. /**
  1512. * ata_sff_interrupt - Default ATA host interrupt handler
  1513. * @irq: irq line (unused)
  1514. * @dev_instance: pointer to our ata_host information structure
  1515. *
  1516. * Default interrupt handler for PCI IDE devices. Calls
  1517. * ata_sff_host_intr() for each port that is not disabled.
  1518. *
  1519. * LOCKING:
  1520. * Obtains host lock during operation.
  1521. *
  1522. * RETURNS:
  1523. * IRQ_NONE or IRQ_HANDLED.
  1524. */
  1525. irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
  1526. {
  1527. struct ata_host *host = dev_instance;
  1528. unsigned int i;
  1529. unsigned int handled = 0;
  1530. unsigned long flags;
  1531. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  1532. spin_lock_irqsave(&host->lock, flags);
  1533. for (i = 0; i < host->n_ports; i++) {
  1534. struct ata_port *ap;
  1535. ap = host->ports[i];
  1536. if (ap &&
  1537. !(ap->flags & ATA_FLAG_DISABLED)) {
  1538. struct ata_queued_cmd *qc;
  1539. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1540. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  1541. (qc->flags & ATA_QCFLAG_ACTIVE))
  1542. handled |= ata_sff_host_intr(ap, qc);
  1543. }
  1544. }
  1545. spin_unlock_irqrestore(&host->lock, flags);
  1546. return IRQ_RETVAL(handled);
  1547. }
  1548. EXPORT_SYMBOL_GPL(ata_sff_interrupt);
  1549. /**
  1550. * ata_sff_lost_interrupt - Check for an apparent lost interrupt
  1551. * @ap: port that appears to have timed out
  1552. *
  1553. * Called from the libata error handlers when the core code suspects
  1554. * an interrupt has been lost. If it has complete anything we can and
  1555. * then return. Interface must support altstatus for this faster
  1556. * recovery to occur.
  1557. *
  1558. * Locking:
  1559. * Caller holds host lock
  1560. */
  1561. void ata_sff_lost_interrupt(struct ata_port *ap)
  1562. {
  1563. u8 status;
  1564. struct ata_queued_cmd *qc;
  1565. /* Only one outstanding command per SFF channel */
  1566. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1567. /* Check we have a live one.. */
  1568. if (qc == NULL || !(qc->flags & ATA_QCFLAG_ACTIVE))
  1569. return;
  1570. /* We cannot lose an interrupt on a polled command */
  1571. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1572. return;
  1573. /* See if the controller thinks it is still busy - if so the command
  1574. isn't a lost IRQ but is still in progress */
  1575. status = ata_sff_altstatus(ap);
  1576. if (status & ATA_BUSY)
  1577. return;
  1578. /* There was a command running, we are no longer busy and we have
  1579. no interrupt. */
  1580. ata_port_printk(ap, KERN_WARNING, "lost interrupt (Status 0x%x)\n",
  1581. status);
  1582. /* Run the host interrupt logic as if the interrupt had not been
  1583. lost */
  1584. ata_sff_host_intr(ap, qc);
  1585. }
  1586. EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
  1587. /**
  1588. * ata_sff_freeze - Freeze SFF controller port
  1589. * @ap: port to freeze
  1590. *
  1591. * Freeze BMDMA controller port.
  1592. *
  1593. * LOCKING:
  1594. * Inherited from caller.
  1595. */
  1596. void ata_sff_freeze(struct ata_port *ap)
  1597. {
  1598. struct ata_ioports *ioaddr = &ap->ioaddr;
  1599. ap->ctl |= ATA_NIEN;
  1600. ap->last_ctl = ap->ctl;
  1601. if (ioaddr->ctl_addr)
  1602. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1603. /* Under certain circumstances, some controllers raise IRQ on
  1604. * ATA_NIEN manipulation. Also, many controllers fail to mask
  1605. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  1606. */
  1607. ap->ops->sff_check_status(ap);
  1608. ap->ops->sff_irq_clear(ap);
  1609. }
  1610. EXPORT_SYMBOL_GPL(ata_sff_freeze);
  1611. /**
  1612. * ata_sff_thaw - Thaw SFF controller port
  1613. * @ap: port to thaw
  1614. *
  1615. * Thaw SFF controller port.
  1616. *
  1617. * LOCKING:
  1618. * Inherited from caller.
  1619. */
  1620. void ata_sff_thaw(struct ata_port *ap)
  1621. {
  1622. /* clear & re-enable interrupts */
  1623. ap->ops->sff_check_status(ap);
  1624. ap->ops->sff_irq_clear(ap);
  1625. ap->ops->sff_irq_on(ap);
  1626. }
  1627. EXPORT_SYMBOL_GPL(ata_sff_thaw);
  1628. /**
  1629. * ata_sff_prereset - prepare SFF link for reset
  1630. * @link: SFF link to be reset
  1631. * @deadline: deadline jiffies for the operation
  1632. *
  1633. * SFF link @link is about to be reset. Initialize it. It first
  1634. * calls ata_std_prereset() and wait for !BSY if the port is
  1635. * being softreset.
  1636. *
  1637. * LOCKING:
  1638. * Kernel thread context (may sleep)
  1639. *
  1640. * RETURNS:
  1641. * 0 on success, -errno otherwise.
  1642. */
  1643. int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
  1644. {
  1645. struct ata_eh_context *ehc = &link->eh_context;
  1646. int rc;
  1647. rc = ata_std_prereset(link, deadline);
  1648. if (rc)
  1649. return rc;
  1650. /* if we're about to do hardreset, nothing more to do */
  1651. if (ehc->i.action & ATA_EH_HARDRESET)
  1652. return 0;
  1653. /* wait for !BSY if we don't know that no device is attached */
  1654. if (!ata_link_offline(link)) {
  1655. rc = ata_sff_wait_ready(link, deadline);
  1656. if (rc && rc != -ENODEV) {
  1657. ata_link_printk(link, KERN_WARNING, "device not ready "
  1658. "(errno=%d), forcing hardreset\n", rc);
  1659. ehc->i.action |= ATA_EH_HARDRESET;
  1660. }
  1661. }
  1662. return 0;
  1663. }
  1664. EXPORT_SYMBOL_GPL(ata_sff_prereset);
  1665. /**
  1666. * ata_devchk - PATA device presence detection
  1667. * @ap: ATA channel to examine
  1668. * @device: Device to examine (starting at zero)
  1669. *
  1670. * This technique was originally described in
  1671. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  1672. * later found its way into the ATA/ATAPI spec.
  1673. *
  1674. * Write a pattern to the ATA shadow registers,
  1675. * and if a device is present, it will respond by
  1676. * correctly storing and echoing back the
  1677. * ATA shadow register contents.
  1678. *
  1679. * LOCKING:
  1680. * caller.
  1681. */
  1682. static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  1683. {
  1684. struct ata_ioports *ioaddr = &ap->ioaddr;
  1685. u8 nsect, lbal;
  1686. ap->ops->sff_dev_select(ap, device);
  1687. iowrite8(0x55, ioaddr->nsect_addr);
  1688. iowrite8(0xaa, ioaddr->lbal_addr);
  1689. iowrite8(0xaa, ioaddr->nsect_addr);
  1690. iowrite8(0x55, ioaddr->lbal_addr);
  1691. iowrite8(0x55, ioaddr->nsect_addr);
  1692. iowrite8(0xaa, ioaddr->lbal_addr);
  1693. nsect = ioread8(ioaddr->nsect_addr);
  1694. lbal = ioread8(ioaddr->lbal_addr);
  1695. if ((nsect == 0x55) && (lbal == 0xaa))
  1696. return 1; /* we found a device */
  1697. return 0; /* nothing found */
  1698. }
  1699. /**
  1700. * ata_sff_dev_classify - Parse returned ATA device signature
  1701. * @dev: ATA device to classify (starting at zero)
  1702. * @present: device seems present
  1703. * @r_err: Value of error register on completion
  1704. *
  1705. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  1706. * an ATA/ATAPI-defined set of values is placed in the ATA
  1707. * shadow registers, indicating the results of device detection
  1708. * and diagnostics.
  1709. *
  1710. * Select the ATA device, and read the values from the ATA shadow
  1711. * registers. Then parse according to the Error register value,
  1712. * and the spec-defined values examined by ata_dev_classify().
  1713. *
  1714. * LOCKING:
  1715. * caller.
  1716. *
  1717. * RETURNS:
  1718. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  1719. */
  1720. unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
  1721. u8 *r_err)
  1722. {
  1723. struct ata_port *ap = dev->link->ap;
  1724. struct ata_taskfile tf;
  1725. unsigned int class;
  1726. u8 err;
  1727. ap->ops->sff_dev_select(ap, dev->devno);
  1728. memset(&tf, 0, sizeof(tf));
  1729. ap->ops->sff_tf_read(ap, &tf);
  1730. err = tf.feature;
  1731. if (r_err)
  1732. *r_err = err;
  1733. /* see if device passed diags: continue and warn later */
  1734. if (err == 0)
  1735. /* diagnostic fail : do nothing _YET_ */
  1736. dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
  1737. else if (err == 1)
  1738. /* do nothing */ ;
  1739. else if ((dev->devno == 0) && (err == 0x81))
  1740. /* do nothing */ ;
  1741. else
  1742. return ATA_DEV_NONE;
  1743. /* determine if device is ATA or ATAPI */
  1744. class = ata_dev_classify(&tf);
  1745. if (class == ATA_DEV_UNKNOWN) {
  1746. /* If the device failed diagnostic, it's likely to
  1747. * have reported incorrect device signature too.
  1748. * Assume ATA device if the device seems present but
  1749. * device signature is invalid with diagnostic
  1750. * failure.
  1751. */
  1752. if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
  1753. class = ATA_DEV_ATA;
  1754. else
  1755. class = ATA_DEV_NONE;
  1756. } else if ((class == ATA_DEV_ATA) &&
  1757. (ap->ops->sff_check_status(ap) == 0))
  1758. class = ATA_DEV_NONE;
  1759. return class;
  1760. }
  1761. EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
  1762. /**
  1763. * ata_sff_wait_after_reset - wait for devices to become ready after reset
  1764. * @link: SFF link which is just reset
  1765. * @devmask: mask of present devices
  1766. * @deadline: deadline jiffies for the operation
  1767. *
  1768. * Wait devices attached to SFF @link to become ready after
  1769. * reset. It contains preceding 150ms wait to avoid accessing TF
  1770. * status register too early.
  1771. *
  1772. * LOCKING:
  1773. * Kernel thread context (may sleep).
  1774. *
  1775. * RETURNS:
  1776. * 0 on success, -ENODEV if some or all of devices in @devmask
  1777. * don't seem to exist. -errno on other errors.
  1778. */
  1779. int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
  1780. unsigned long deadline)
  1781. {
  1782. struct ata_port *ap = link->ap;
  1783. struct ata_ioports *ioaddr = &ap->ioaddr;
  1784. unsigned int dev0 = devmask & (1 << 0);
  1785. unsigned int dev1 = devmask & (1 << 1);
  1786. int rc, ret = 0;
  1787. msleep(ATA_WAIT_AFTER_RESET);
  1788. /* always check readiness of the master device */
  1789. rc = ata_sff_wait_ready(link, deadline);
  1790. /* -ENODEV means the odd clown forgot the D7 pulldown resistor
  1791. * and TF status is 0xff, bail out on it too.
  1792. */
  1793. if (rc)
  1794. return rc;
  1795. /* if device 1 was found in ata_devchk, wait for register
  1796. * access briefly, then wait for BSY to clear.
  1797. */
  1798. if (dev1) {
  1799. int i;
  1800. ap->ops->sff_dev_select(ap, 1);
  1801. /* Wait for register access. Some ATAPI devices fail
  1802. * to set nsect/lbal after reset, so don't waste too
  1803. * much time on it. We're gonna wait for !BSY anyway.
  1804. */
  1805. for (i = 0; i < 2; i++) {
  1806. u8 nsect, lbal;
  1807. nsect = ioread8(ioaddr->nsect_addr);
  1808. lbal = ioread8(ioaddr->lbal_addr);
  1809. if ((nsect == 1) && (lbal == 1))
  1810. break;
  1811. msleep(50); /* give drive a breather */
  1812. }
  1813. rc = ata_sff_wait_ready(link, deadline);
  1814. if (rc) {
  1815. if (rc != -ENODEV)
  1816. return rc;
  1817. ret = rc;
  1818. }
  1819. }
  1820. /* is all this really necessary? */
  1821. ap->ops->sff_dev_select(ap, 0);
  1822. if (dev1)
  1823. ap->ops->sff_dev_select(ap, 1);
  1824. if (dev0)
  1825. ap->ops->sff_dev_select(ap, 0);
  1826. return ret;
  1827. }
  1828. EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
  1829. static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
  1830. unsigned long deadline)
  1831. {
  1832. struct ata_ioports *ioaddr = &ap->ioaddr;
  1833. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  1834. /* software reset. causes dev0 to be selected */
  1835. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1836. udelay(20); /* FIXME: flush */
  1837. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  1838. udelay(20); /* FIXME: flush */
  1839. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1840. ap->last_ctl = ap->ctl;
  1841. /* wait the port to become ready */
  1842. return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
  1843. }
  1844. /**
  1845. * ata_sff_softreset - reset host port via ATA SRST
  1846. * @link: ATA link to reset
  1847. * @classes: resulting classes of attached devices
  1848. * @deadline: deadline jiffies for the operation
  1849. *
  1850. * Reset host port using ATA SRST.
  1851. *
  1852. * LOCKING:
  1853. * Kernel thread context (may sleep)
  1854. *
  1855. * RETURNS:
  1856. * 0 on success, -errno otherwise.
  1857. */
  1858. int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
  1859. unsigned long deadline)
  1860. {
  1861. struct ata_port *ap = link->ap;
  1862. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1863. unsigned int devmask = 0;
  1864. int rc;
  1865. u8 err;
  1866. DPRINTK("ENTER\n");
  1867. /* determine if device 0/1 are present */
  1868. if (ata_devchk(ap, 0))
  1869. devmask |= (1 << 0);
  1870. if (slave_possible && ata_devchk(ap, 1))
  1871. devmask |= (1 << 1);
  1872. /* select device 0 again */
  1873. ap->ops->sff_dev_select(ap, 0);
  1874. /* issue bus reset */
  1875. DPRINTK("about to softreset, devmask=%x\n", devmask);
  1876. rc = ata_bus_softreset(ap, devmask, deadline);
  1877. /* if link is occupied, -ENODEV too is an error */
  1878. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  1879. ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
  1880. return rc;
  1881. }
  1882. /* determine by signature whether we have ATA or ATAPI devices */
  1883. classes[0] = ata_sff_dev_classify(&link->device[0],
  1884. devmask & (1 << 0), &err);
  1885. if (slave_possible && err != 0x81)
  1886. classes[1] = ata_sff_dev_classify(&link->device[1],
  1887. devmask & (1 << 1), &err);
  1888. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  1889. return 0;
  1890. }
  1891. EXPORT_SYMBOL_GPL(ata_sff_softreset);
  1892. /**
  1893. * sata_sff_hardreset - reset host port via SATA phy reset
  1894. * @link: link to reset
  1895. * @class: resulting class of attached device
  1896. * @deadline: deadline jiffies for the operation
  1897. *
  1898. * SATA phy-reset host port using DET bits of SControl register,
  1899. * wait for !BSY and classify the attached device.
  1900. *
  1901. * LOCKING:
  1902. * Kernel thread context (may sleep)
  1903. *
  1904. * RETURNS:
  1905. * 0 on success, -errno otherwise.
  1906. */
  1907. int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
  1908. unsigned long deadline)
  1909. {
  1910. struct ata_eh_context *ehc = &link->eh_context;
  1911. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  1912. bool online;
  1913. int rc;
  1914. rc = sata_link_hardreset(link, timing, deadline, &online,
  1915. ata_sff_check_ready);
  1916. if (online)
  1917. *class = ata_sff_dev_classify(link->device, 1, NULL);
  1918. DPRINTK("EXIT, class=%u\n", *class);
  1919. return rc;
  1920. }
  1921. EXPORT_SYMBOL_GPL(sata_sff_hardreset);
  1922. /**
  1923. * ata_sff_postreset - SFF postreset callback
  1924. * @link: the target SFF ata_link
  1925. * @classes: classes of attached devices
  1926. *
  1927. * This function is invoked after a successful reset. It first
  1928. * calls ata_std_postreset() and performs SFF specific postreset
  1929. * processing.
  1930. *
  1931. * LOCKING:
  1932. * Kernel thread context (may sleep)
  1933. */
  1934. void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
  1935. {
  1936. struct ata_port *ap = link->ap;
  1937. ata_std_postreset(link, classes);
  1938. /* is double-select really necessary? */
  1939. if (classes[0] != ATA_DEV_NONE)
  1940. ap->ops->sff_dev_select(ap, 1);
  1941. if (classes[1] != ATA_DEV_NONE)
  1942. ap->ops->sff_dev_select(ap, 0);
  1943. /* bail out if no device is present */
  1944. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1945. DPRINTK("EXIT, no device\n");
  1946. return;
  1947. }
  1948. /* set up device control */
  1949. if (ap->ioaddr.ctl_addr) {
  1950. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  1951. ap->last_ctl = ap->ctl;
  1952. }
  1953. }
  1954. EXPORT_SYMBOL_GPL(ata_sff_postreset);
  1955. /**
  1956. * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers
  1957. * @qc: command
  1958. *
  1959. * Drain the FIFO and device of any stuck data following a command
  1960. * failing to complete. In some cases this is neccessary before a
  1961. * reset will recover the device.
  1962. *
  1963. */
  1964. void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
  1965. {
  1966. int count;
  1967. struct ata_port *ap;
  1968. /* We only need to flush incoming data when a command was running */
  1969. if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
  1970. return;
  1971. ap = qc->ap;
  1972. /* Drain up to 64K of data before we give up this recovery method */
  1973. for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
  1974. && count < 32768; count++)
  1975. ioread16(ap->ioaddr.data_addr);
  1976. /* Can become DEBUG later */
  1977. if (count)
  1978. ata_port_printk(ap, KERN_DEBUG,
  1979. "drained %d bytes to clear DRQ.\n", count);
  1980. }
  1981. EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
  1982. /**
  1983. * ata_sff_error_handler - Stock error handler for BMDMA controller
  1984. * @ap: port to handle error for
  1985. *
  1986. * Stock error handler for SFF controller. It can handle both
  1987. * PATA and SATA controllers. Many controllers should be able to
  1988. * use this EH as-is or with some added handling before and
  1989. * after.
  1990. *
  1991. * LOCKING:
  1992. * Kernel thread context (may sleep)
  1993. */
  1994. void ata_sff_error_handler(struct ata_port *ap)
  1995. {
  1996. ata_reset_fn_t softreset = ap->ops->softreset;
  1997. ata_reset_fn_t hardreset = ap->ops->hardreset;
  1998. struct ata_queued_cmd *qc;
  1999. unsigned long flags;
  2000. int thaw = 0;
  2001. qc = __ata_qc_from_tag(ap, ap->link.active_tag);
  2002. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  2003. qc = NULL;
  2004. /* reset PIO HSM and stop DMA engine */
  2005. spin_lock_irqsave(ap->lock, flags);
  2006. ap->hsm_task_state = HSM_ST_IDLE;
  2007. if (ap->ioaddr.bmdma_addr &&
  2008. qc && (qc->tf.protocol == ATA_PROT_DMA ||
  2009. qc->tf.protocol == ATAPI_PROT_DMA)) {
  2010. u8 host_stat;
  2011. host_stat = ap->ops->bmdma_status(ap);
  2012. /* BMDMA controllers indicate host bus error by
  2013. * setting DMA_ERR bit and timing out. As it wasn't
  2014. * really a timeout event, adjust error mask and
  2015. * cancel frozen state.
  2016. */
  2017. if (qc->err_mask == AC_ERR_TIMEOUT
  2018. && (host_stat & ATA_DMA_ERR)) {
  2019. qc->err_mask = AC_ERR_HOST_BUS;
  2020. thaw = 1;
  2021. }
  2022. ap->ops->bmdma_stop(qc);
  2023. }
  2024. ata_sff_sync(ap); /* FIXME: We don't need this */
  2025. ap->ops->sff_check_status(ap);
  2026. ap->ops->sff_irq_clear(ap);
  2027. /* We *MUST* do FIFO draining before we issue a reset as several
  2028. * devices helpfully clear their internal state and will lock solid
  2029. * if we touch the data port post reset. Pass qc in case anyone wants
  2030. * to do different PIO/DMA recovery or has per command fixups
  2031. */
  2032. if (ap->ops->drain_fifo)
  2033. ap->ops->drain_fifo(qc);
  2034. spin_unlock_irqrestore(ap->lock, flags);
  2035. if (thaw)
  2036. ata_eh_thaw_port(ap);
  2037. /* PIO and DMA engines have been stopped, perform recovery */
  2038. /* Ignore ata_sff_softreset if ctl isn't accessible and
  2039. * built-in hardresets if SCR access isn't available.
  2040. */
  2041. if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
  2042. softreset = NULL;
  2043. if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
  2044. hardreset = NULL;
  2045. ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
  2046. ap->ops->postreset);
  2047. }
  2048. EXPORT_SYMBOL_GPL(ata_sff_error_handler);
  2049. /**
  2050. * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
  2051. * @qc: internal command to clean up
  2052. *
  2053. * LOCKING:
  2054. * Kernel thread context (may sleep)
  2055. */
  2056. void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
  2057. {
  2058. struct ata_port *ap = qc->ap;
  2059. unsigned long flags;
  2060. spin_lock_irqsave(ap->lock, flags);
  2061. ap->hsm_task_state = HSM_ST_IDLE;
  2062. if (ap->ioaddr.bmdma_addr)
  2063. ata_bmdma_stop(qc);
  2064. spin_unlock_irqrestore(ap->lock, flags);
  2065. }
  2066. EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
  2067. /**
  2068. * ata_sff_port_start - Set port up for dma.
  2069. * @ap: Port to initialize
  2070. *
  2071. * Called just after data structures for each port are
  2072. * initialized. Allocates space for PRD table if the device
  2073. * is DMA capable SFF.
  2074. *
  2075. * May be used as the port_start() entry in ata_port_operations.
  2076. *
  2077. * LOCKING:
  2078. * Inherited from caller.
  2079. */
  2080. int ata_sff_port_start(struct ata_port *ap)
  2081. {
  2082. if (ap->ioaddr.bmdma_addr)
  2083. return ata_port_start(ap);
  2084. return 0;
  2085. }
  2086. EXPORT_SYMBOL_GPL(ata_sff_port_start);
  2087. /**
  2088. * ata_sff_port_start32 - Set port up for dma.
  2089. * @ap: Port to initialize
  2090. *
  2091. * Called just after data structures for each port are
  2092. * initialized. Allocates space for PRD table if the device
  2093. * is DMA capable SFF.
  2094. *
  2095. * May be used as the port_start() entry in ata_port_operations for
  2096. * devices that are capable of 32bit PIO.
  2097. *
  2098. * LOCKING:
  2099. * Inherited from caller.
  2100. */
  2101. int ata_sff_port_start32(struct ata_port *ap)
  2102. {
  2103. ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
  2104. if (ap->ioaddr.bmdma_addr)
  2105. return ata_port_start(ap);
  2106. return 0;
  2107. }
  2108. EXPORT_SYMBOL_GPL(ata_sff_port_start32);
  2109. /**
  2110. * ata_sff_std_ports - initialize ioaddr with standard port offsets.
  2111. * @ioaddr: IO address structure to be initialized
  2112. *
  2113. * Utility function which initializes data_addr, error_addr,
  2114. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  2115. * device_addr, status_addr, and command_addr to standard offsets
  2116. * relative to cmd_addr.
  2117. *
  2118. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  2119. */
  2120. void ata_sff_std_ports(struct ata_ioports *ioaddr)
  2121. {
  2122. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  2123. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  2124. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  2125. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  2126. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  2127. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  2128. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  2129. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  2130. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  2131. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  2132. }
  2133. EXPORT_SYMBOL_GPL(ata_sff_std_ports);
  2134. unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
  2135. unsigned long xfer_mask)
  2136. {
  2137. /* Filter out DMA modes if the device has been configured by
  2138. the BIOS as PIO only */
  2139. if (adev->link->ap->ioaddr.bmdma_addr == NULL)
  2140. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2141. return xfer_mask;
  2142. }
  2143. EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
  2144. /**
  2145. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  2146. * @qc: Info associated with this ATA transaction.
  2147. *
  2148. * LOCKING:
  2149. * spin_lock_irqsave(host lock)
  2150. */
  2151. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  2152. {
  2153. struct ata_port *ap = qc->ap;
  2154. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  2155. u8 dmactl;
  2156. /* load PRD table addr. */
  2157. mb(); /* make sure PRD table writes are visible to controller */
  2158. iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  2159. /* specify data direction, triple-check start bit is clear */
  2160. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2161. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  2162. if (!rw)
  2163. dmactl |= ATA_DMA_WR;
  2164. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2165. /* issue r/w command */
  2166. ap->ops->sff_exec_command(ap, &qc->tf);
  2167. }
  2168. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  2169. /**
  2170. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  2171. * @qc: Info associated with this ATA transaction.
  2172. *
  2173. * LOCKING:
  2174. * spin_lock_irqsave(host lock)
  2175. */
  2176. void ata_bmdma_start(struct ata_queued_cmd *qc)
  2177. {
  2178. struct ata_port *ap = qc->ap;
  2179. u8 dmactl;
  2180. /* start host DMA transaction */
  2181. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2182. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2183. /* Strictly, one may wish to issue an ioread8() here, to
  2184. * flush the mmio write. However, control also passes
  2185. * to the hardware at this point, and it will interrupt
  2186. * us when we are to resume control. So, in effect,
  2187. * we don't care when the mmio write flushes.
  2188. * Further, a read of the DMA status register _immediately_
  2189. * following the write may not be what certain flaky hardware
  2190. * is expected, so I think it is best to not add a readb()
  2191. * without first all the MMIO ATA cards/mobos.
  2192. * Or maybe I'm just being paranoid.
  2193. *
  2194. * FIXME: The posting of this write means I/O starts are
  2195. * unneccessarily delayed for MMIO
  2196. */
  2197. }
  2198. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  2199. /**
  2200. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  2201. * @qc: Command we are ending DMA for
  2202. *
  2203. * Clears the ATA_DMA_START flag in the dma control register
  2204. *
  2205. * May be used as the bmdma_stop() entry in ata_port_operations.
  2206. *
  2207. * LOCKING:
  2208. * spin_lock_irqsave(host lock)
  2209. */
  2210. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  2211. {
  2212. struct ata_port *ap = qc->ap;
  2213. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  2214. /* clear start/stop bit */
  2215. iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  2216. mmio + ATA_DMA_CMD);
  2217. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  2218. ata_sff_dma_pause(ap);
  2219. }
  2220. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  2221. /**
  2222. * ata_bmdma_status - Read PCI IDE BMDMA status
  2223. * @ap: Port associated with this ATA transaction.
  2224. *
  2225. * Read and return BMDMA status register.
  2226. *
  2227. * May be used as the bmdma_status() entry in ata_port_operations.
  2228. *
  2229. * LOCKING:
  2230. * spin_lock_irqsave(host lock)
  2231. */
  2232. u8 ata_bmdma_status(struct ata_port *ap)
  2233. {
  2234. return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  2235. }
  2236. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  2237. /**
  2238. * ata_bus_reset - reset host port and associated ATA channel
  2239. * @ap: port to reset
  2240. *
  2241. * This is typically the first time we actually start issuing
  2242. * commands to the ATA channel. We wait for BSY to clear, then
  2243. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2244. * result. Determine what devices, if any, are on the channel
  2245. * by looking at the device 0/1 error register. Look at the signature
  2246. * stored in each device's taskfile registers, to determine if
  2247. * the device is ATA or ATAPI.
  2248. *
  2249. * LOCKING:
  2250. * PCI/etc. bus probe sem.
  2251. * Obtains host lock.
  2252. *
  2253. * SIDE EFFECTS:
  2254. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2255. *
  2256. * DEPRECATED:
  2257. * This function is only for drivers which still use old EH and
  2258. * will be removed soon.
  2259. */
  2260. void ata_bus_reset(struct ata_port *ap)
  2261. {
  2262. struct ata_device *device = ap->link.device;
  2263. struct ata_ioports *ioaddr = &ap->ioaddr;
  2264. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2265. u8 err;
  2266. unsigned int dev0, dev1 = 0, devmask = 0;
  2267. int rc;
  2268. DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
  2269. /* determine if device 0/1 are present */
  2270. if (ap->flags & ATA_FLAG_SATA_RESET)
  2271. dev0 = 1;
  2272. else {
  2273. dev0 = ata_devchk(ap, 0);
  2274. if (slave_possible)
  2275. dev1 = ata_devchk(ap, 1);
  2276. }
  2277. if (dev0)
  2278. devmask |= (1 << 0);
  2279. if (dev1)
  2280. devmask |= (1 << 1);
  2281. /* select device 0 again */
  2282. ap->ops->sff_dev_select(ap, 0);
  2283. /* issue bus reset */
  2284. if (ap->flags & ATA_FLAG_SRST) {
  2285. rc = ata_bus_softreset(ap, devmask,
  2286. ata_deadline(jiffies, 40000));
  2287. if (rc && rc != -ENODEV)
  2288. goto err_out;
  2289. }
  2290. /*
  2291. * determine by signature whether we have ATA or ATAPI devices
  2292. */
  2293. device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
  2294. if ((slave_possible) && (err != 0x81))
  2295. device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
  2296. /* is double-select really necessary? */
  2297. if (device[1].class != ATA_DEV_NONE)
  2298. ap->ops->sff_dev_select(ap, 1);
  2299. if (device[0].class != ATA_DEV_NONE)
  2300. ap->ops->sff_dev_select(ap, 0);
  2301. /* if no devices were detected, disable this port */
  2302. if ((device[0].class == ATA_DEV_NONE) &&
  2303. (device[1].class == ATA_DEV_NONE))
  2304. goto err_out;
  2305. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2306. /* set up device control for ATA_FLAG_SATA_RESET */
  2307. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2308. ap->last_ctl = ap->ctl;
  2309. }
  2310. DPRINTK("EXIT\n");
  2311. return;
  2312. err_out:
  2313. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2314. ata_port_disable(ap);
  2315. DPRINTK("EXIT\n");
  2316. }
  2317. EXPORT_SYMBOL_GPL(ata_bus_reset);
  2318. #ifdef CONFIG_PCI
  2319. /**
  2320. * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
  2321. * @pdev: PCI device
  2322. *
  2323. * Some PCI ATA devices report simplex mode but in fact can be told to
  2324. * enter non simplex mode. This implements the necessary logic to
  2325. * perform the task on such devices. Calling it on other devices will
  2326. * have -undefined- behaviour.
  2327. */
  2328. int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
  2329. {
  2330. unsigned long bmdma = pci_resource_start(pdev, 4);
  2331. u8 simplex;
  2332. if (bmdma == 0)
  2333. return -ENOENT;
  2334. simplex = inb(bmdma + 0x02);
  2335. outb(simplex & 0x60, bmdma + 0x02);
  2336. simplex = inb(bmdma + 0x02);
  2337. if (simplex & 0x80)
  2338. return -EOPNOTSUPP;
  2339. return 0;
  2340. }
  2341. EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
  2342. /**
  2343. * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
  2344. * @host: target ATA host
  2345. *
  2346. * Acquire PCI BMDMA resources and initialize @host accordingly.
  2347. *
  2348. * LOCKING:
  2349. * Inherited from calling layer (may sleep).
  2350. *
  2351. * RETURNS:
  2352. * 0 on success, -errno otherwise.
  2353. */
  2354. int ata_pci_bmdma_init(struct ata_host *host)
  2355. {
  2356. struct device *gdev = host->dev;
  2357. struct pci_dev *pdev = to_pci_dev(gdev);
  2358. int i, rc;
  2359. /* No BAR4 allocation: No DMA */
  2360. if (pci_resource_start(pdev, 4) == 0)
  2361. return 0;
  2362. /* TODO: If we get no DMA mask we should fall back to PIO */
  2363. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  2364. if (rc)
  2365. return rc;
  2366. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  2367. if (rc)
  2368. return rc;
  2369. /* request and iomap DMA region */
  2370. rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
  2371. if (rc) {
  2372. dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
  2373. return -ENOMEM;
  2374. }
  2375. host->iomap = pcim_iomap_table(pdev);
  2376. for (i = 0; i < 2; i++) {
  2377. struct ata_port *ap = host->ports[i];
  2378. void __iomem *bmdma = host->iomap[4] + 8 * i;
  2379. if (ata_port_is_dummy(ap))
  2380. continue;
  2381. ap->ioaddr.bmdma_addr = bmdma;
  2382. if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  2383. (ioread8(bmdma + 2) & 0x80))
  2384. host->flags |= ATA_HOST_SIMPLEX;
  2385. ata_port_desc(ap, "bmdma 0x%llx",
  2386. (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
  2387. }
  2388. return 0;
  2389. }
  2390. EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
  2391. static int ata_resources_present(struct pci_dev *pdev, int port)
  2392. {
  2393. int i;
  2394. /* Check the PCI resources for this channel are enabled */
  2395. port = port * 2;
  2396. for (i = 0; i < 2; i++) {
  2397. if (pci_resource_start(pdev, port + i) == 0 ||
  2398. pci_resource_len(pdev, port + i) == 0)
  2399. return 0;
  2400. }
  2401. return 1;
  2402. }
  2403. /**
  2404. * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
  2405. * @host: target ATA host
  2406. *
  2407. * Acquire native PCI ATA resources for @host and initialize the
  2408. * first two ports of @host accordingly. Ports marked dummy are
  2409. * skipped and allocation failure makes the port dummy.
  2410. *
  2411. * Note that native PCI resources are valid even for legacy hosts
  2412. * as we fix up pdev resources array early in boot, so this
  2413. * function can be used for both native and legacy SFF hosts.
  2414. *
  2415. * LOCKING:
  2416. * Inherited from calling layer (may sleep).
  2417. *
  2418. * RETURNS:
  2419. * 0 if at least one port is initialized, -ENODEV if no port is
  2420. * available.
  2421. */
  2422. int ata_pci_sff_init_host(struct ata_host *host)
  2423. {
  2424. struct device *gdev = host->dev;
  2425. struct pci_dev *pdev = to_pci_dev(gdev);
  2426. unsigned int mask = 0;
  2427. int i, rc;
  2428. /* request, iomap BARs and init port addresses accordingly */
  2429. for (i = 0; i < 2; i++) {
  2430. struct ata_port *ap = host->ports[i];
  2431. int base = i * 2;
  2432. void __iomem * const *iomap;
  2433. if (ata_port_is_dummy(ap))
  2434. continue;
  2435. /* Discard disabled ports. Some controllers show
  2436. * their unused channels this way. Disabled ports are
  2437. * made dummy.
  2438. */
  2439. if (!ata_resources_present(pdev, i)) {
  2440. ap->ops = &ata_dummy_port_ops;
  2441. continue;
  2442. }
  2443. rc = pcim_iomap_regions(pdev, 0x3 << base,
  2444. dev_driver_string(gdev));
  2445. if (rc) {
  2446. dev_printk(KERN_WARNING, gdev,
  2447. "failed to request/iomap BARs for port %d "
  2448. "(errno=%d)\n", i, rc);
  2449. if (rc == -EBUSY)
  2450. pcim_pin_device(pdev);
  2451. ap->ops = &ata_dummy_port_ops;
  2452. continue;
  2453. }
  2454. host->iomap = iomap = pcim_iomap_table(pdev);
  2455. ap->ioaddr.cmd_addr = iomap[base];
  2456. ap->ioaddr.altstatus_addr =
  2457. ap->ioaddr.ctl_addr = (void __iomem *)
  2458. ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
  2459. ata_sff_std_ports(&ap->ioaddr);
  2460. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  2461. (unsigned long long)pci_resource_start(pdev, base),
  2462. (unsigned long long)pci_resource_start(pdev, base + 1));
  2463. mask |= 1 << i;
  2464. }
  2465. if (!mask) {
  2466. dev_printk(KERN_ERR, gdev, "no available native port\n");
  2467. return -ENODEV;
  2468. }
  2469. return 0;
  2470. }
  2471. EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
  2472. /**
  2473. * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
  2474. * @pdev: target PCI device
  2475. * @ppi: array of port_info, must be enough for two ports
  2476. * @r_host: out argument for the initialized ATA host
  2477. *
  2478. * Helper to allocate ATA host for @pdev, acquire all native PCI
  2479. * resources and initialize it accordingly in one go.
  2480. *
  2481. * LOCKING:
  2482. * Inherited from calling layer (may sleep).
  2483. *
  2484. * RETURNS:
  2485. * 0 on success, -errno otherwise.
  2486. */
  2487. int ata_pci_sff_prepare_host(struct pci_dev *pdev,
  2488. const struct ata_port_info * const *ppi,
  2489. struct ata_host **r_host)
  2490. {
  2491. struct ata_host *host;
  2492. int rc;
  2493. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  2494. return -ENOMEM;
  2495. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  2496. if (!host) {
  2497. dev_printk(KERN_ERR, &pdev->dev,
  2498. "failed to allocate ATA host\n");
  2499. rc = -ENOMEM;
  2500. goto err_out;
  2501. }
  2502. rc = ata_pci_sff_init_host(host);
  2503. if (rc)
  2504. goto err_out;
  2505. /* init DMA related stuff */
  2506. rc = ata_pci_bmdma_init(host);
  2507. if (rc)
  2508. goto err_bmdma;
  2509. devres_remove_group(&pdev->dev, NULL);
  2510. *r_host = host;
  2511. return 0;
  2512. err_bmdma:
  2513. /* This is necessary because PCI and iomap resources are
  2514. * merged and releasing the top group won't release the
  2515. * acquired resources if some of those have been acquired
  2516. * before entering this function.
  2517. */
  2518. pcim_iounmap_regions(pdev, 0xf);
  2519. err_out:
  2520. devres_release_group(&pdev->dev, NULL);
  2521. return rc;
  2522. }
  2523. EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
  2524. /**
  2525. * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
  2526. * @host: target SFF ATA host
  2527. * @irq_handler: irq_handler used when requesting IRQ(s)
  2528. * @sht: scsi_host_template to use when registering the host
  2529. *
  2530. * This is the counterpart of ata_host_activate() for SFF ATA
  2531. * hosts. This separate helper is necessary because SFF hosts
  2532. * use two separate interrupts in legacy mode.
  2533. *
  2534. * LOCKING:
  2535. * Inherited from calling layer (may sleep).
  2536. *
  2537. * RETURNS:
  2538. * 0 on success, -errno otherwise.
  2539. */
  2540. int ata_pci_sff_activate_host(struct ata_host *host,
  2541. irq_handler_t irq_handler,
  2542. struct scsi_host_template *sht)
  2543. {
  2544. struct device *dev = host->dev;
  2545. struct pci_dev *pdev = to_pci_dev(dev);
  2546. const char *drv_name = dev_driver_string(host->dev);
  2547. int legacy_mode = 0, rc;
  2548. rc = ata_host_start(host);
  2549. if (rc)
  2550. return rc;
  2551. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  2552. u8 tmp8, mask;
  2553. /* TODO: What if one channel is in native mode ... */
  2554. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  2555. mask = (1 << 2) | (1 << 0);
  2556. if ((tmp8 & mask) != mask)
  2557. legacy_mode = 1;
  2558. #if defined(CONFIG_NO_ATA_LEGACY)
  2559. /* Some platforms with PCI limits cannot address compat
  2560. port space. In that case we punt if their firmware has
  2561. left a device in compatibility mode */
  2562. if (legacy_mode) {
  2563. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  2564. return -EOPNOTSUPP;
  2565. }
  2566. #endif
  2567. }
  2568. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2569. return -ENOMEM;
  2570. if (!legacy_mode && pdev->irq) {
  2571. rc = devm_request_irq(dev, pdev->irq, irq_handler,
  2572. IRQF_SHARED, drv_name, host);
  2573. if (rc)
  2574. goto out;
  2575. ata_port_desc(host->ports[0], "irq %d", pdev->irq);
  2576. ata_port_desc(host->ports[1], "irq %d", pdev->irq);
  2577. } else if (legacy_mode) {
  2578. if (!ata_port_is_dummy(host->ports[0])) {
  2579. rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
  2580. irq_handler, IRQF_SHARED,
  2581. drv_name, host);
  2582. if (rc)
  2583. goto out;
  2584. ata_port_desc(host->ports[0], "irq %d",
  2585. ATA_PRIMARY_IRQ(pdev));
  2586. }
  2587. if (!ata_port_is_dummy(host->ports[1])) {
  2588. rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
  2589. irq_handler, IRQF_SHARED,
  2590. drv_name, host);
  2591. if (rc)
  2592. goto out;
  2593. ata_port_desc(host->ports[1], "irq %d",
  2594. ATA_SECONDARY_IRQ(pdev));
  2595. }
  2596. }
  2597. rc = ata_host_register(host, sht);
  2598. out:
  2599. if (rc == 0)
  2600. devres_remove_group(dev, NULL);
  2601. else
  2602. devres_release_group(dev, NULL);
  2603. return rc;
  2604. }
  2605. EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
  2606. /**
  2607. * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
  2608. * @pdev: Controller to be initialized
  2609. * @ppi: array of port_info, must be enough for two ports
  2610. * @sht: scsi_host_template to use when registering the host
  2611. * @host_priv: host private_data
  2612. *
  2613. * This is a helper function which can be called from a driver's
  2614. * xxx_init_one() probe function if the hardware uses traditional
  2615. * IDE taskfile registers.
  2616. *
  2617. * This function calls pci_enable_device(), reserves its register
  2618. * regions, sets the dma mask, enables bus master mode, and calls
  2619. * ata_device_add()
  2620. *
  2621. * ASSUMPTION:
  2622. * Nobody makes a single channel controller that appears solely as
  2623. * the secondary legacy port on PCI.
  2624. *
  2625. * LOCKING:
  2626. * Inherited from PCI layer (may sleep).
  2627. *
  2628. * RETURNS:
  2629. * Zero on success, negative on errno-based value on error.
  2630. */
  2631. int ata_pci_sff_init_one(struct pci_dev *pdev,
  2632. const struct ata_port_info * const *ppi,
  2633. struct scsi_host_template *sht, void *host_priv)
  2634. {
  2635. struct device *dev = &pdev->dev;
  2636. const struct ata_port_info *pi = NULL;
  2637. struct ata_host *host = NULL;
  2638. int i, rc;
  2639. DPRINTK("ENTER\n");
  2640. /* look up the first valid port_info */
  2641. for (i = 0; i < 2 && ppi[i]; i++) {
  2642. if (ppi[i]->port_ops != &ata_dummy_port_ops) {
  2643. pi = ppi[i];
  2644. break;
  2645. }
  2646. }
  2647. if (!pi) {
  2648. dev_printk(KERN_ERR, &pdev->dev,
  2649. "no valid port_info specified\n");
  2650. return -EINVAL;
  2651. }
  2652. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2653. return -ENOMEM;
  2654. rc = pcim_enable_device(pdev);
  2655. if (rc)
  2656. goto out;
  2657. /* prepare and activate SFF host */
  2658. rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
  2659. if (rc)
  2660. goto out;
  2661. host->private_data = host_priv;
  2662. pci_set_master(pdev);
  2663. rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
  2664. out:
  2665. if (rc == 0)
  2666. devres_remove_group(&pdev->dev, NULL);
  2667. else
  2668. devres_release_group(&pdev->dev, NULL);
  2669. return rc;
  2670. }
  2671. EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
  2672. #endif /* CONFIG_PCI */