libata-sff.c 72 KB

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