libata-sff.c 78 KB

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