libata-sff.c 74 KB

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