libata-sff.c 84 KB

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