libata-sff.c 77 KB

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