libata-sff.c 79 KB

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