libata-sff.c 77 KB

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