libata-sff.c 78 KB

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