libata-sff.c 79 KB

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