libata-sff.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336
  1. /*
  2. * libata-sff.c - helper library for PCI IDE BMDMA
  3. *
  4. * Maintained by: Tejun Heo <tj@kernel.org>
  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. if (ap->ops->error_handler) {
  885. if (in_wq) {
  886. /* EH might have kicked in while host lock is
  887. * released.
  888. */
  889. qc = ata_qc_from_tag(ap, qc->tag);
  890. if (qc) {
  891. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  892. ata_sff_irq_on(ap);
  893. ata_qc_complete(qc);
  894. } else
  895. ata_port_freeze(ap);
  896. }
  897. } else {
  898. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  899. ata_qc_complete(qc);
  900. else
  901. ata_port_freeze(ap);
  902. }
  903. } else {
  904. if (in_wq) {
  905. ata_sff_irq_on(ap);
  906. ata_qc_complete(qc);
  907. } else
  908. ata_qc_complete(qc);
  909. }
  910. }
  911. /**
  912. * ata_sff_hsm_move - move the HSM to the next state.
  913. * @ap: the target ata_port
  914. * @qc: qc on going
  915. * @status: current device status
  916. * @in_wq: 1 if called from workqueue, 0 otherwise
  917. *
  918. * RETURNS:
  919. * 1 when poll next status needed, 0 otherwise.
  920. */
  921. int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  922. u8 status, int in_wq)
  923. {
  924. struct ata_link *link = qc->dev->link;
  925. struct ata_eh_info *ehi = &link->eh_info;
  926. int poll_next;
  927. lockdep_assert_held(ap->lock);
  928. WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  929. /* Make sure ata_sff_qc_issue() does not throw things
  930. * like DMA polling into the workqueue. Notice that
  931. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  932. */
  933. WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
  934. fsm_start:
  935. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  936. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  937. switch (ap->hsm_task_state) {
  938. case HSM_ST_FIRST:
  939. /* Send first data block or PACKET CDB */
  940. /* If polling, we will stay in the work queue after
  941. * sending the data. Otherwise, interrupt handler
  942. * takes over after sending the data.
  943. */
  944. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  945. /* check device status */
  946. if (unlikely((status & ATA_DRQ) == 0)) {
  947. /* handle BSY=0, DRQ=0 as error */
  948. if (likely(status & (ATA_ERR | ATA_DF)))
  949. /* device stops HSM for abort/error */
  950. qc->err_mask |= AC_ERR_DEV;
  951. else {
  952. /* HSM violation. Let EH handle this */
  953. ata_ehi_push_desc(ehi,
  954. "ST_FIRST: !(DRQ|ERR|DF)");
  955. qc->err_mask |= AC_ERR_HSM;
  956. }
  957. ap->hsm_task_state = HSM_ST_ERR;
  958. goto fsm_start;
  959. }
  960. /* Device should not ask for data transfer (DRQ=1)
  961. * when it finds something wrong.
  962. * We ignore DRQ here and stop the HSM by
  963. * changing hsm_task_state to HSM_ST_ERR and
  964. * let the EH abort the command or reset the device.
  965. */
  966. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  967. /* Some ATAPI tape drives forget to clear the ERR bit
  968. * when doing the next command (mostly request sense).
  969. * We ignore ERR here to workaround and proceed sending
  970. * the CDB.
  971. */
  972. if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
  973. ata_ehi_push_desc(ehi, "ST_FIRST: "
  974. "DRQ=1 with device error, "
  975. "dev_stat 0x%X", status);
  976. qc->err_mask |= AC_ERR_HSM;
  977. ap->hsm_task_state = HSM_ST_ERR;
  978. goto fsm_start;
  979. }
  980. }
  981. if (qc->tf.protocol == ATA_PROT_PIO) {
  982. /* PIO data out protocol.
  983. * send first data block.
  984. */
  985. /* ata_pio_sectors() might change the state
  986. * to HSM_ST_LAST. so, the state is changed here
  987. * before ata_pio_sectors().
  988. */
  989. ap->hsm_task_state = HSM_ST;
  990. ata_pio_sectors(qc);
  991. } else
  992. /* send CDB */
  993. atapi_send_cdb(ap, qc);
  994. /* if polling, ata_sff_pio_task() handles the rest.
  995. * otherwise, interrupt handler takes over from here.
  996. */
  997. break;
  998. case HSM_ST:
  999. /* complete command or read/write the data register */
  1000. if (qc->tf.protocol == ATAPI_PROT_PIO) {
  1001. /* ATAPI PIO protocol */
  1002. if ((status & ATA_DRQ) == 0) {
  1003. /* No more data to transfer or device error.
  1004. * Device error will be tagged in HSM_ST_LAST.
  1005. */
  1006. ap->hsm_task_state = HSM_ST_LAST;
  1007. goto fsm_start;
  1008. }
  1009. /* Device should not ask for data transfer (DRQ=1)
  1010. * when it finds something wrong.
  1011. * We ignore DRQ here and stop the HSM by
  1012. * changing hsm_task_state to HSM_ST_ERR and
  1013. * let the EH abort the command or reset the device.
  1014. */
  1015. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1016. ata_ehi_push_desc(ehi, "ST-ATAPI: "
  1017. "DRQ=1 with device error, "
  1018. "dev_stat 0x%X", status);
  1019. qc->err_mask |= AC_ERR_HSM;
  1020. ap->hsm_task_state = HSM_ST_ERR;
  1021. goto fsm_start;
  1022. }
  1023. atapi_pio_bytes(qc);
  1024. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  1025. /* bad ireason reported by device */
  1026. goto fsm_start;
  1027. } else {
  1028. /* ATA PIO protocol */
  1029. if (unlikely((status & ATA_DRQ) == 0)) {
  1030. /* handle BSY=0, DRQ=0 as error */
  1031. if (likely(status & (ATA_ERR | ATA_DF))) {
  1032. /* device stops HSM for abort/error */
  1033. qc->err_mask |= AC_ERR_DEV;
  1034. /* If diagnostic failed and this is
  1035. * IDENTIFY, it's likely a phantom
  1036. * device. Mark hint.
  1037. */
  1038. if (qc->dev->horkage &
  1039. ATA_HORKAGE_DIAGNOSTIC)
  1040. qc->err_mask |=
  1041. AC_ERR_NODEV_HINT;
  1042. } else {
  1043. /* HSM violation. Let EH handle this.
  1044. * Phantom devices also trigger this
  1045. * condition. Mark hint.
  1046. */
  1047. ata_ehi_push_desc(ehi, "ST-ATA: "
  1048. "DRQ=0 without device error, "
  1049. "dev_stat 0x%X", status);
  1050. qc->err_mask |= AC_ERR_HSM |
  1051. AC_ERR_NODEV_HINT;
  1052. }
  1053. ap->hsm_task_state = HSM_ST_ERR;
  1054. goto fsm_start;
  1055. }
  1056. /* For PIO reads, some devices may ask for
  1057. * data transfer (DRQ=1) alone with ERR=1.
  1058. * We respect DRQ here and transfer one
  1059. * block of junk data before changing the
  1060. * hsm_task_state to HSM_ST_ERR.
  1061. *
  1062. * For PIO writes, ERR=1 DRQ=1 doesn't make
  1063. * sense since the data block has been
  1064. * transferred to the device.
  1065. */
  1066. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1067. /* data might be corrputed */
  1068. qc->err_mask |= AC_ERR_DEV;
  1069. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  1070. ata_pio_sectors(qc);
  1071. status = ata_wait_idle(ap);
  1072. }
  1073. if (status & (ATA_BUSY | ATA_DRQ)) {
  1074. ata_ehi_push_desc(ehi, "ST-ATA: "
  1075. "BUSY|DRQ persists on ERR|DF, "
  1076. "dev_stat 0x%X", status);
  1077. qc->err_mask |= AC_ERR_HSM;
  1078. }
  1079. /* There are oddball controllers with
  1080. * status register stuck at 0x7f and
  1081. * lbal/m/h at zero which makes it
  1082. * pass all other presence detection
  1083. * mechanisms we have. Set NODEV_HINT
  1084. * for it. Kernel bz#7241.
  1085. */
  1086. if (status == 0x7f)
  1087. qc->err_mask |= AC_ERR_NODEV_HINT;
  1088. /* ata_pio_sectors() might change the
  1089. * state to HSM_ST_LAST. so, the state
  1090. * is changed after ata_pio_sectors().
  1091. */
  1092. ap->hsm_task_state = HSM_ST_ERR;
  1093. goto fsm_start;
  1094. }
  1095. ata_pio_sectors(qc);
  1096. if (ap->hsm_task_state == HSM_ST_LAST &&
  1097. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  1098. /* all data read */
  1099. status = ata_wait_idle(ap);
  1100. goto fsm_start;
  1101. }
  1102. }
  1103. poll_next = 1;
  1104. break;
  1105. case HSM_ST_LAST:
  1106. if (unlikely(!ata_ok(status))) {
  1107. qc->err_mask |= __ac_err_mask(status);
  1108. ap->hsm_task_state = HSM_ST_ERR;
  1109. goto fsm_start;
  1110. }
  1111. /* no more data to transfer */
  1112. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  1113. ap->print_id, qc->dev->devno, status);
  1114. WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
  1115. ap->hsm_task_state = HSM_ST_IDLE;
  1116. /* complete taskfile transaction */
  1117. ata_hsm_qc_complete(qc, in_wq);
  1118. poll_next = 0;
  1119. break;
  1120. case HSM_ST_ERR:
  1121. ap->hsm_task_state = HSM_ST_IDLE;
  1122. /* complete taskfile transaction */
  1123. ata_hsm_qc_complete(qc, in_wq);
  1124. poll_next = 0;
  1125. break;
  1126. default:
  1127. poll_next = 0;
  1128. BUG();
  1129. }
  1130. return poll_next;
  1131. }
  1132. EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
  1133. void ata_sff_queue_work(struct work_struct *work)
  1134. {
  1135. queue_work(ata_sff_wq, work);
  1136. }
  1137. EXPORT_SYMBOL_GPL(ata_sff_queue_work);
  1138. void ata_sff_queue_delayed_work(struct delayed_work *dwork, unsigned long delay)
  1139. {
  1140. queue_delayed_work(ata_sff_wq, dwork, delay);
  1141. }
  1142. EXPORT_SYMBOL_GPL(ata_sff_queue_delayed_work);
  1143. void ata_sff_queue_pio_task(struct ata_link *link, unsigned long delay)
  1144. {
  1145. struct ata_port *ap = link->ap;
  1146. WARN_ON((ap->sff_pio_task_link != NULL) &&
  1147. (ap->sff_pio_task_link != link));
  1148. ap->sff_pio_task_link = link;
  1149. /* may fail if ata_sff_flush_pio_task() in progress */
  1150. ata_sff_queue_delayed_work(&ap->sff_pio_task, msecs_to_jiffies(delay));
  1151. }
  1152. EXPORT_SYMBOL_GPL(ata_sff_queue_pio_task);
  1153. void ata_sff_flush_pio_task(struct ata_port *ap)
  1154. {
  1155. DPRINTK("ENTER\n");
  1156. cancel_delayed_work_sync(&ap->sff_pio_task);
  1157. /*
  1158. * We wanna reset the HSM state to IDLE. If we do so without
  1159. * grabbing the port lock, critical sections protected by it which
  1160. * expect the HSM state to stay stable may get surprised. For
  1161. * example, we may set IDLE in between the time
  1162. * __ata_sff_port_intr() checks for HSM_ST_IDLE and before it calls
  1163. * ata_sff_hsm_move() causing ata_sff_hsm_move() to BUG().
  1164. */
  1165. spin_lock_irq(ap->lock);
  1166. ap->hsm_task_state = HSM_ST_IDLE;
  1167. spin_unlock_irq(ap->lock);
  1168. ap->sff_pio_task_link = NULL;
  1169. if (ata_msg_ctl(ap))
  1170. ata_port_dbg(ap, "%s: EXIT\n", __func__);
  1171. }
  1172. static void ata_sff_pio_task(struct work_struct *work)
  1173. {
  1174. struct ata_port *ap =
  1175. container_of(work, struct ata_port, sff_pio_task.work);
  1176. struct ata_link *link = ap->sff_pio_task_link;
  1177. struct ata_queued_cmd *qc;
  1178. u8 status;
  1179. int poll_next;
  1180. spin_lock_irq(ap->lock);
  1181. BUG_ON(ap->sff_pio_task_link == NULL);
  1182. /* qc can be NULL if timeout occurred */
  1183. qc = ata_qc_from_tag(ap, link->active_tag);
  1184. if (!qc) {
  1185. ap->sff_pio_task_link = NULL;
  1186. goto out_unlock;
  1187. }
  1188. fsm_start:
  1189. WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
  1190. /*
  1191. * This is purely heuristic. This is a fast path.
  1192. * Sometimes when we enter, BSY will be cleared in
  1193. * a chk-status or two. If not, the drive is probably seeking
  1194. * or something. Snooze for a couple msecs, then
  1195. * chk-status again. If still busy, queue delayed work.
  1196. */
  1197. status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
  1198. if (status & ATA_BUSY) {
  1199. spin_unlock_irq(ap->lock);
  1200. ata_msleep(ap, 2);
  1201. spin_lock_irq(ap->lock);
  1202. status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
  1203. if (status & ATA_BUSY) {
  1204. ata_sff_queue_pio_task(link, ATA_SHORT_PAUSE);
  1205. goto out_unlock;
  1206. }
  1207. }
  1208. /*
  1209. * hsm_move() may trigger another command to be processed.
  1210. * clean the link beforehand.
  1211. */
  1212. ap->sff_pio_task_link = NULL;
  1213. /* move the HSM */
  1214. poll_next = ata_sff_hsm_move(ap, qc, status, 1);
  1215. /* another command or interrupt handler
  1216. * may be running at this point.
  1217. */
  1218. if (poll_next)
  1219. goto fsm_start;
  1220. out_unlock:
  1221. spin_unlock_irq(ap->lock);
  1222. }
  1223. /**
  1224. * ata_sff_qc_issue - issue taskfile to a SFF controller
  1225. * @qc: command to issue to device
  1226. *
  1227. * This function issues a PIO or NODATA command to a SFF
  1228. * controller.
  1229. *
  1230. * LOCKING:
  1231. * spin_lock_irqsave(host lock)
  1232. *
  1233. * RETURNS:
  1234. * Zero on success, AC_ERR_* mask on failure
  1235. */
  1236. unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
  1237. {
  1238. struct ata_port *ap = qc->ap;
  1239. struct ata_link *link = qc->dev->link;
  1240. /* Use polling pio if the LLD doesn't handle
  1241. * interrupt driven pio and atapi CDB interrupt.
  1242. */
  1243. if (ap->flags & ATA_FLAG_PIO_POLLING)
  1244. qc->tf.flags |= ATA_TFLAG_POLLING;
  1245. /* select the device */
  1246. ata_dev_select(ap, qc->dev->devno, 1, 0);
  1247. /* start the command */
  1248. switch (qc->tf.protocol) {
  1249. case ATA_PROT_NODATA:
  1250. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1251. ata_qc_set_polling(qc);
  1252. ata_tf_to_host(ap, &qc->tf);
  1253. ap->hsm_task_state = HSM_ST_LAST;
  1254. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1255. ata_sff_queue_pio_task(link, 0);
  1256. break;
  1257. case ATA_PROT_PIO:
  1258. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1259. ata_qc_set_polling(qc);
  1260. ata_tf_to_host(ap, &qc->tf);
  1261. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  1262. /* PIO data out protocol */
  1263. ap->hsm_task_state = HSM_ST_FIRST;
  1264. ata_sff_queue_pio_task(link, 0);
  1265. /* always send first data block using the
  1266. * ata_sff_pio_task() codepath.
  1267. */
  1268. } else {
  1269. /* PIO data in protocol */
  1270. ap->hsm_task_state = HSM_ST;
  1271. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1272. ata_sff_queue_pio_task(link, 0);
  1273. /* if polling, ata_sff_pio_task() handles the
  1274. * rest. otherwise, interrupt handler takes
  1275. * over from here.
  1276. */
  1277. }
  1278. break;
  1279. case ATAPI_PROT_PIO:
  1280. case ATAPI_PROT_NODATA:
  1281. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1282. ata_qc_set_polling(qc);
  1283. ata_tf_to_host(ap, &qc->tf);
  1284. ap->hsm_task_state = HSM_ST_FIRST;
  1285. /* send cdb by polling if no cdb interrupt */
  1286. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  1287. (qc->tf.flags & ATA_TFLAG_POLLING))
  1288. ata_sff_queue_pio_task(link, 0);
  1289. break;
  1290. default:
  1291. return AC_ERR_SYSTEM;
  1292. }
  1293. return 0;
  1294. }
  1295. EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
  1296. /**
  1297. * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
  1298. * @qc: qc to fill result TF for
  1299. *
  1300. * @qc is finished and result TF needs to be filled. Fill it
  1301. * using ->sff_tf_read.
  1302. *
  1303. * LOCKING:
  1304. * spin_lock_irqsave(host lock)
  1305. *
  1306. * RETURNS:
  1307. * true indicating that result TF is successfully filled.
  1308. */
  1309. bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
  1310. {
  1311. qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
  1312. return true;
  1313. }
  1314. EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
  1315. static unsigned int ata_sff_idle_irq(struct ata_port *ap)
  1316. {
  1317. ap->stats.idle_irq++;
  1318. #ifdef ATA_IRQ_TRAP
  1319. if ((ap->stats.idle_irq % 1000) == 0) {
  1320. ap->ops->sff_check_status(ap);
  1321. if (ap->ops->sff_irq_clear)
  1322. ap->ops->sff_irq_clear(ap);
  1323. ata_port_warn(ap, "irq trap\n");
  1324. return 1;
  1325. }
  1326. #endif
  1327. return 0; /* irq not handled */
  1328. }
  1329. static unsigned int __ata_sff_port_intr(struct ata_port *ap,
  1330. struct ata_queued_cmd *qc,
  1331. bool hsmv_on_idle)
  1332. {
  1333. u8 status;
  1334. VPRINTK("ata%u: protocol %d task_state %d\n",
  1335. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  1336. /* Check whether we are expecting interrupt in this state */
  1337. switch (ap->hsm_task_state) {
  1338. case HSM_ST_FIRST:
  1339. /* Some pre-ATAPI-4 devices assert INTRQ
  1340. * at this state when ready to receive CDB.
  1341. */
  1342. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  1343. * The flag was turned on only for atapi devices. No
  1344. * need to check ata_is_atapi(qc->tf.protocol) again.
  1345. */
  1346. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1347. return ata_sff_idle_irq(ap);
  1348. break;
  1349. case HSM_ST_IDLE:
  1350. return ata_sff_idle_irq(ap);
  1351. default:
  1352. break;
  1353. }
  1354. /* check main status, clearing INTRQ if needed */
  1355. status = ata_sff_irq_status(ap);
  1356. if (status & ATA_BUSY) {
  1357. if (hsmv_on_idle) {
  1358. /* BMDMA engine is already stopped, we're screwed */
  1359. qc->err_mask |= AC_ERR_HSM;
  1360. ap->hsm_task_state = HSM_ST_ERR;
  1361. } else
  1362. return ata_sff_idle_irq(ap);
  1363. }
  1364. /* clear irq events */
  1365. if (ap->ops->sff_irq_clear)
  1366. ap->ops->sff_irq_clear(ap);
  1367. ata_sff_hsm_move(ap, qc, status, 0);
  1368. return 1; /* irq handled */
  1369. }
  1370. /**
  1371. * ata_sff_port_intr - Handle SFF port interrupt
  1372. * @ap: Port on which interrupt arrived (possibly...)
  1373. * @qc: Taskfile currently active in engine
  1374. *
  1375. * Handle port interrupt for given queued command.
  1376. *
  1377. * LOCKING:
  1378. * spin_lock_irqsave(host lock)
  1379. *
  1380. * RETURNS:
  1381. * One if interrupt was handled, zero if not (shared irq).
  1382. */
  1383. unsigned int ata_sff_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
  1384. {
  1385. return __ata_sff_port_intr(ap, qc, false);
  1386. }
  1387. EXPORT_SYMBOL_GPL(ata_sff_port_intr);
  1388. static inline irqreturn_t __ata_sff_interrupt(int irq, void *dev_instance,
  1389. unsigned int (*port_intr)(struct ata_port *, struct ata_queued_cmd *))
  1390. {
  1391. struct ata_host *host = dev_instance;
  1392. bool retried = false;
  1393. unsigned int i;
  1394. unsigned int handled, idle, polling;
  1395. unsigned long flags;
  1396. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  1397. spin_lock_irqsave(&host->lock, flags);
  1398. retry:
  1399. handled = idle = polling = 0;
  1400. for (i = 0; i < host->n_ports; i++) {
  1401. struct ata_port *ap = host->ports[i];
  1402. struct ata_queued_cmd *qc;
  1403. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1404. if (qc) {
  1405. if (!(qc->tf.flags & ATA_TFLAG_POLLING))
  1406. handled |= port_intr(ap, qc);
  1407. else
  1408. polling |= 1 << i;
  1409. } else
  1410. idle |= 1 << i;
  1411. }
  1412. /*
  1413. * If no port was expecting IRQ but the controller is actually
  1414. * asserting IRQ line, nobody cared will ensue. Check IRQ
  1415. * pending status if available and clear spurious IRQ.
  1416. */
  1417. if (!handled && !retried) {
  1418. bool retry = false;
  1419. for (i = 0; i < host->n_ports; i++) {
  1420. struct ata_port *ap = host->ports[i];
  1421. if (polling & (1 << i))
  1422. continue;
  1423. if (!ap->ops->sff_irq_check ||
  1424. !ap->ops->sff_irq_check(ap))
  1425. continue;
  1426. if (idle & (1 << i)) {
  1427. ap->ops->sff_check_status(ap);
  1428. if (ap->ops->sff_irq_clear)
  1429. ap->ops->sff_irq_clear(ap);
  1430. } else {
  1431. /* clear INTRQ and check if BUSY cleared */
  1432. if (!(ap->ops->sff_check_status(ap) & ATA_BUSY))
  1433. retry |= true;
  1434. /*
  1435. * With command in flight, we can't do
  1436. * sff_irq_clear() w/o racing with completion.
  1437. */
  1438. }
  1439. }
  1440. if (retry) {
  1441. retried = true;
  1442. goto retry;
  1443. }
  1444. }
  1445. spin_unlock_irqrestore(&host->lock, flags);
  1446. return IRQ_RETVAL(handled);
  1447. }
  1448. /**
  1449. * ata_sff_interrupt - Default SFF ATA host interrupt handler
  1450. * @irq: irq line (unused)
  1451. * @dev_instance: pointer to our ata_host information structure
  1452. *
  1453. * Default interrupt handler for PCI IDE devices. Calls
  1454. * ata_sff_port_intr() for each port that is not disabled.
  1455. *
  1456. * LOCKING:
  1457. * Obtains host lock during operation.
  1458. *
  1459. * RETURNS:
  1460. * IRQ_NONE or IRQ_HANDLED.
  1461. */
  1462. irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
  1463. {
  1464. return __ata_sff_interrupt(irq, dev_instance, ata_sff_port_intr);
  1465. }
  1466. EXPORT_SYMBOL_GPL(ata_sff_interrupt);
  1467. /**
  1468. * ata_sff_lost_interrupt - Check for an apparent lost interrupt
  1469. * @ap: port that appears to have timed out
  1470. *
  1471. * Called from the libata error handlers when the core code suspects
  1472. * an interrupt has been lost. If it has complete anything we can and
  1473. * then return. Interface must support altstatus for this faster
  1474. * recovery to occur.
  1475. *
  1476. * Locking:
  1477. * Caller holds host lock
  1478. */
  1479. void ata_sff_lost_interrupt(struct ata_port *ap)
  1480. {
  1481. u8 status;
  1482. struct ata_queued_cmd *qc;
  1483. /* Only one outstanding command per SFF channel */
  1484. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1485. /* We cannot lose an interrupt on a non-existent or polled command */
  1486. if (!qc || qc->tf.flags & ATA_TFLAG_POLLING)
  1487. return;
  1488. /* See if the controller thinks it is still busy - if so the command
  1489. isn't a lost IRQ but is still in progress */
  1490. status = ata_sff_altstatus(ap);
  1491. if (status & ATA_BUSY)
  1492. return;
  1493. /* There was a command running, we are no longer busy and we have
  1494. no interrupt. */
  1495. ata_port_warn(ap, "lost interrupt (Status 0x%x)\n",
  1496. status);
  1497. /* Run the host interrupt logic as if the interrupt had not been
  1498. lost */
  1499. ata_sff_port_intr(ap, qc);
  1500. }
  1501. EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
  1502. /**
  1503. * ata_sff_freeze - Freeze SFF controller port
  1504. * @ap: port to freeze
  1505. *
  1506. * Freeze SFF controller port.
  1507. *
  1508. * LOCKING:
  1509. * Inherited from caller.
  1510. */
  1511. void ata_sff_freeze(struct ata_port *ap)
  1512. {
  1513. ap->ctl |= ATA_NIEN;
  1514. ap->last_ctl = ap->ctl;
  1515. if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr)
  1516. ata_sff_set_devctl(ap, ap->ctl);
  1517. /* Under certain circumstances, some controllers raise IRQ on
  1518. * ATA_NIEN manipulation. Also, many controllers fail to mask
  1519. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  1520. */
  1521. ap->ops->sff_check_status(ap);
  1522. if (ap->ops->sff_irq_clear)
  1523. ap->ops->sff_irq_clear(ap);
  1524. }
  1525. EXPORT_SYMBOL_GPL(ata_sff_freeze);
  1526. /**
  1527. * ata_sff_thaw - Thaw SFF controller port
  1528. * @ap: port to thaw
  1529. *
  1530. * Thaw SFF controller port.
  1531. *
  1532. * LOCKING:
  1533. * Inherited from caller.
  1534. */
  1535. void ata_sff_thaw(struct ata_port *ap)
  1536. {
  1537. /* clear & re-enable interrupts */
  1538. ap->ops->sff_check_status(ap);
  1539. if (ap->ops->sff_irq_clear)
  1540. ap->ops->sff_irq_clear(ap);
  1541. ata_sff_irq_on(ap);
  1542. }
  1543. EXPORT_SYMBOL_GPL(ata_sff_thaw);
  1544. /**
  1545. * ata_sff_prereset - prepare SFF link for reset
  1546. * @link: SFF link to be reset
  1547. * @deadline: deadline jiffies for the operation
  1548. *
  1549. * SFF link @link is about to be reset. Initialize it. It first
  1550. * calls ata_std_prereset() and wait for !BSY if the port is
  1551. * being softreset.
  1552. *
  1553. * LOCKING:
  1554. * Kernel thread context (may sleep)
  1555. *
  1556. * RETURNS:
  1557. * 0 on success, -errno otherwise.
  1558. */
  1559. int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
  1560. {
  1561. struct ata_eh_context *ehc = &link->eh_context;
  1562. int rc;
  1563. rc = ata_std_prereset(link, deadline);
  1564. if (rc)
  1565. return rc;
  1566. /* if we're about to do hardreset, nothing more to do */
  1567. if (ehc->i.action & ATA_EH_HARDRESET)
  1568. return 0;
  1569. /* wait for !BSY if we don't know that no device is attached */
  1570. if (!ata_link_offline(link)) {
  1571. rc = ata_sff_wait_ready(link, deadline);
  1572. if (rc && rc != -ENODEV) {
  1573. ata_link_warn(link,
  1574. "device not ready (errno=%d), forcing hardreset\n",
  1575. rc);
  1576. ehc->i.action |= ATA_EH_HARDRESET;
  1577. }
  1578. }
  1579. return 0;
  1580. }
  1581. EXPORT_SYMBOL_GPL(ata_sff_prereset);
  1582. /**
  1583. * ata_devchk - PATA device presence detection
  1584. * @ap: ATA channel to examine
  1585. * @device: Device to examine (starting at zero)
  1586. *
  1587. * This technique was originally described in
  1588. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  1589. * later found its way into the ATA/ATAPI spec.
  1590. *
  1591. * Write a pattern to the ATA shadow registers,
  1592. * and if a device is present, it will respond by
  1593. * correctly storing and echoing back the
  1594. * ATA shadow register contents.
  1595. *
  1596. * LOCKING:
  1597. * caller.
  1598. */
  1599. static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  1600. {
  1601. struct ata_ioports *ioaddr = &ap->ioaddr;
  1602. u8 nsect, lbal;
  1603. ap->ops->sff_dev_select(ap, device);
  1604. iowrite8(0x55, ioaddr->nsect_addr);
  1605. iowrite8(0xaa, ioaddr->lbal_addr);
  1606. iowrite8(0xaa, ioaddr->nsect_addr);
  1607. iowrite8(0x55, ioaddr->lbal_addr);
  1608. iowrite8(0x55, ioaddr->nsect_addr);
  1609. iowrite8(0xaa, ioaddr->lbal_addr);
  1610. nsect = ioread8(ioaddr->nsect_addr);
  1611. lbal = ioread8(ioaddr->lbal_addr);
  1612. if ((nsect == 0x55) && (lbal == 0xaa))
  1613. return 1; /* we found a device */
  1614. return 0; /* nothing found */
  1615. }
  1616. /**
  1617. * ata_sff_dev_classify - Parse returned ATA device signature
  1618. * @dev: ATA device to classify (starting at zero)
  1619. * @present: device seems present
  1620. * @r_err: Value of error register on completion
  1621. *
  1622. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  1623. * an ATA/ATAPI-defined set of values is placed in the ATA
  1624. * shadow registers, indicating the results of device detection
  1625. * and diagnostics.
  1626. *
  1627. * Select the ATA device, and read the values from the ATA shadow
  1628. * registers. Then parse according to the Error register value,
  1629. * and the spec-defined values examined by ata_dev_classify().
  1630. *
  1631. * LOCKING:
  1632. * caller.
  1633. *
  1634. * RETURNS:
  1635. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  1636. */
  1637. unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
  1638. u8 *r_err)
  1639. {
  1640. struct ata_port *ap = dev->link->ap;
  1641. struct ata_taskfile tf;
  1642. unsigned int class;
  1643. u8 err;
  1644. ap->ops->sff_dev_select(ap, dev->devno);
  1645. memset(&tf, 0, sizeof(tf));
  1646. ap->ops->sff_tf_read(ap, &tf);
  1647. err = tf.feature;
  1648. if (r_err)
  1649. *r_err = err;
  1650. /* see if device passed diags: continue and warn later */
  1651. if (err == 0)
  1652. /* diagnostic fail : do nothing _YET_ */
  1653. dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
  1654. else if (err == 1)
  1655. /* do nothing */ ;
  1656. else if ((dev->devno == 0) && (err == 0x81))
  1657. /* do nothing */ ;
  1658. else
  1659. return ATA_DEV_NONE;
  1660. /* determine if device is ATA or ATAPI */
  1661. class = ata_dev_classify(&tf);
  1662. if (class == ATA_DEV_UNKNOWN) {
  1663. /* If the device failed diagnostic, it's likely to
  1664. * have reported incorrect device signature too.
  1665. * Assume ATA device if the device seems present but
  1666. * device signature is invalid with diagnostic
  1667. * failure.
  1668. */
  1669. if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
  1670. class = ATA_DEV_ATA;
  1671. else
  1672. class = ATA_DEV_NONE;
  1673. } else if ((class == ATA_DEV_ATA) &&
  1674. (ap->ops->sff_check_status(ap) == 0))
  1675. class = ATA_DEV_NONE;
  1676. return class;
  1677. }
  1678. EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
  1679. /**
  1680. * ata_sff_wait_after_reset - wait for devices to become ready after reset
  1681. * @link: SFF link which is just reset
  1682. * @devmask: mask of present devices
  1683. * @deadline: deadline jiffies for the operation
  1684. *
  1685. * Wait devices attached to SFF @link to become ready after
  1686. * reset. It contains preceding 150ms wait to avoid accessing TF
  1687. * status register too early.
  1688. *
  1689. * LOCKING:
  1690. * Kernel thread context (may sleep).
  1691. *
  1692. * RETURNS:
  1693. * 0 on success, -ENODEV if some or all of devices in @devmask
  1694. * don't seem to exist. -errno on other errors.
  1695. */
  1696. int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
  1697. unsigned long deadline)
  1698. {
  1699. struct ata_port *ap = link->ap;
  1700. struct ata_ioports *ioaddr = &ap->ioaddr;
  1701. unsigned int dev0 = devmask & (1 << 0);
  1702. unsigned int dev1 = devmask & (1 << 1);
  1703. int rc, ret = 0;
  1704. ata_msleep(ap, ATA_WAIT_AFTER_RESET);
  1705. /* always check readiness of the master device */
  1706. rc = ata_sff_wait_ready(link, deadline);
  1707. /* -ENODEV means the odd clown forgot the D7 pulldown resistor
  1708. * and TF status is 0xff, bail out on it too.
  1709. */
  1710. if (rc)
  1711. return rc;
  1712. /* if device 1 was found in ata_devchk, wait for register
  1713. * access briefly, then wait for BSY to clear.
  1714. */
  1715. if (dev1) {
  1716. int i;
  1717. ap->ops->sff_dev_select(ap, 1);
  1718. /* Wait for register access. Some ATAPI devices fail
  1719. * to set nsect/lbal after reset, so don't waste too
  1720. * much time on it. We're gonna wait for !BSY anyway.
  1721. */
  1722. for (i = 0; i < 2; i++) {
  1723. u8 nsect, lbal;
  1724. nsect = ioread8(ioaddr->nsect_addr);
  1725. lbal = ioread8(ioaddr->lbal_addr);
  1726. if ((nsect == 1) && (lbal == 1))
  1727. break;
  1728. ata_msleep(ap, 50); /* give drive a breather */
  1729. }
  1730. rc = ata_sff_wait_ready(link, deadline);
  1731. if (rc) {
  1732. if (rc != -ENODEV)
  1733. return rc;
  1734. ret = rc;
  1735. }
  1736. }
  1737. /* is all this really necessary? */
  1738. ap->ops->sff_dev_select(ap, 0);
  1739. if (dev1)
  1740. ap->ops->sff_dev_select(ap, 1);
  1741. if (dev0)
  1742. ap->ops->sff_dev_select(ap, 0);
  1743. return ret;
  1744. }
  1745. EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
  1746. static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
  1747. unsigned long deadline)
  1748. {
  1749. struct ata_ioports *ioaddr = &ap->ioaddr;
  1750. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  1751. if (ap->ioaddr.ctl_addr) {
  1752. /* software reset. causes dev0 to be selected */
  1753. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1754. udelay(20); /* FIXME: flush */
  1755. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  1756. udelay(20); /* FIXME: flush */
  1757. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1758. ap->last_ctl = ap->ctl;
  1759. }
  1760. /* wait the port to become ready */
  1761. return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
  1762. }
  1763. /**
  1764. * ata_sff_softreset - reset host port via ATA SRST
  1765. * @link: ATA link to reset
  1766. * @classes: resulting classes of attached devices
  1767. * @deadline: deadline jiffies for the operation
  1768. *
  1769. * Reset host port using ATA SRST.
  1770. *
  1771. * LOCKING:
  1772. * Kernel thread context (may sleep)
  1773. *
  1774. * RETURNS:
  1775. * 0 on success, -errno otherwise.
  1776. */
  1777. int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
  1778. unsigned long deadline)
  1779. {
  1780. struct ata_port *ap = link->ap;
  1781. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1782. unsigned int devmask = 0;
  1783. int rc;
  1784. u8 err;
  1785. DPRINTK("ENTER\n");
  1786. /* determine if device 0/1 are present */
  1787. if (ata_devchk(ap, 0))
  1788. devmask |= (1 << 0);
  1789. if (slave_possible && ata_devchk(ap, 1))
  1790. devmask |= (1 << 1);
  1791. /* select device 0 again */
  1792. ap->ops->sff_dev_select(ap, 0);
  1793. /* issue bus reset */
  1794. DPRINTK("about to softreset, devmask=%x\n", devmask);
  1795. rc = ata_bus_softreset(ap, devmask, deadline);
  1796. /* if link is occupied, -ENODEV too is an error */
  1797. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  1798. ata_link_err(link, "SRST failed (errno=%d)\n", rc);
  1799. return rc;
  1800. }
  1801. /* determine by signature whether we have ATA or ATAPI devices */
  1802. classes[0] = ata_sff_dev_classify(&link->device[0],
  1803. devmask & (1 << 0), &err);
  1804. if (slave_possible && err != 0x81)
  1805. classes[1] = ata_sff_dev_classify(&link->device[1],
  1806. devmask & (1 << 1), &err);
  1807. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  1808. return 0;
  1809. }
  1810. EXPORT_SYMBOL_GPL(ata_sff_softreset);
  1811. /**
  1812. * sata_sff_hardreset - reset host port via SATA phy reset
  1813. * @link: link to reset
  1814. * @class: resulting class of attached device
  1815. * @deadline: deadline jiffies for the operation
  1816. *
  1817. * SATA phy-reset host port using DET bits of SControl register,
  1818. * wait for !BSY and classify the attached device.
  1819. *
  1820. * LOCKING:
  1821. * Kernel thread context (may sleep)
  1822. *
  1823. * RETURNS:
  1824. * 0 on success, -errno otherwise.
  1825. */
  1826. int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
  1827. unsigned long deadline)
  1828. {
  1829. struct ata_eh_context *ehc = &link->eh_context;
  1830. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  1831. bool online;
  1832. int rc;
  1833. rc = sata_link_hardreset(link, timing, deadline, &online,
  1834. ata_sff_check_ready);
  1835. if (online)
  1836. *class = ata_sff_dev_classify(link->device, 1, NULL);
  1837. DPRINTK("EXIT, class=%u\n", *class);
  1838. return rc;
  1839. }
  1840. EXPORT_SYMBOL_GPL(sata_sff_hardreset);
  1841. /**
  1842. * ata_sff_postreset - SFF postreset callback
  1843. * @link: the target SFF ata_link
  1844. * @classes: classes of attached devices
  1845. *
  1846. * This function is invoked after a successful reset. It first
  1847. * calls ata_std_postreset() and performs SFF specific postreset
  1848. * processing.
  1849. *
  1850. * LOCKING:
  1851. * Kernel thread context (may sleep)
  1852. */
  1853. void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
  1854. {
  1855. struct ata_port *ap = link->ap;
  1856. ata_std_postreset(link, classes);
  1857. /* is double-select really necessary? */
  1858. if (classes[0] != ATA_DEV_NONE)
  1859. ap->ops->sff_dev_select(ap, 1);
  1860. if (classes[1] != ATA_DEV_NONE)
  1861. ap->ops->sff_dev_select(ap, 0);
  1862. /* bail out if no device is present */
  1863. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1864. DPRINTK("EXIT, no device\n");
  1865. return;
  1866. }
  1867. /* set up device control */
  1868. if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr) {
  1869. ata_sff_set_devctl(ap, ap->ctl);
  1870. ap->last_ctl = ap->ctl;
  1871. }
  1872. }
  1873. EXPORT_SYMBOL_GPL(ata_sff_postreset);
  1874. /**
  1875. * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers
  1876. * @qc: command
  1877. *
  1878. * Drain the FIFO and device of any stuck data following a command
  1879. * failing to complete. In some cases this is necessary before a
  1880. * reset will recover the device.
  1881. *
  1882. */
  1883. void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
  1884. {
  1885. int count;
  1886. struct ata_port *ap;
  1887. /* We only need to flush incoming data when a command was running */
  1888. if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
  1889. return;
  1890. ap = qc->ap;
  1891. /* Drain up to 64K of data before we give up this recovery method */
  1892. for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
  1893. && count < 65536; count += 2)
  1894. ioread16(ap->ioaddr.data_addr);
  1895. /* Can become DEBUG later */
  1896. if (count)
  1897. ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
  1898. }
  1899. EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
  1900. /**
  1901. * ata_sff_error_handler - Stock error handler for SFF controller
  1902. * @ap: port to handle error for
  1903. *
  1904. * Stock error handler for SFF controller. It can handle both
  1905. * PATA and SATA controllers. Many controllers should be able to
  1906. * use this EH as-is or with some added handling before and
  1907. * after.
  1908. *
  1909. * LOCKING:
  1910. * Kernel thread context (may sleep)
  1911. */
  1912. void ata_sff_error_handler(struct ata_port *ap)
  1913. {
  1914. ata_reset_fn_t softreset = ap->ops->softreset;
  1915. ata_reset_fn_t hardreset = ap->ops->hardreset;
  1916. struct ata_queued_cmd *qc;
  1917. unsigned long flags;
  1918. qc = __ata_qc_from_tag(ap, ap->link.active_tag);
  1919. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  1920. qc = NULL;
  1921. spin_lock_irqsave(ap->lock, flags);
  1922. /*
  1923. * We *MUST* do FIFO draining before we issue a reset as
  1924. * several devices helpfully clear their internal state and
  1925. * will lock solid if we touch the data port post reset. Pass
  1926. * qc in case anyone wants to do different PIO/DMA recovery or
  1927. * has per command fixups
  1928. */
  1929. if (ap->ops->sff_drain_fifo)
  1930. ap->ops->sff_drain_fifo(qc);
  1931. spin_unlock_irqrestore(ap->lock, flags);
  1932. /* ignore built-in hardresets if SCR access is not available */
  1933. if ((hardreset == sata_std_hardreset ||
  1934. hardreset == sata_sff_hardreset) && !sata_scr_valid(&ap->link))
  1935. hardreset = NULL;
  1936. ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
  1937. ap->ops->postreset);
  1938. }
  1939. EXPORT_SYMBOL_GPL(ata_sff_error_handler);
  1940. /**
  1941. * ata_sff_std_ports - initialize ioaddr with standard port offsets.
  1942. * @ioaddr: IO address structure to be initialized
  1943. *
  1944. * Utility function which initializes data_addr, error_addr,
  1945. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  1946. * device_addr, status_addr, and command_addr to standard offsets
  1947. * relative to cmd_addr.
  1948. *
  1949. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  1950. */
  1951. void ata_sff_std_ports(struct ata_ioports *ioaddr)
  1952. {
  1953. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  1954. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  1955. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  1956. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  1957. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  1958. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  1959. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  1960. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  1961. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  1962. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  1963. }
  1964. EXPORT_SYMBOL_GPL(ata_sff_std_ports);
  1965. #ifdef CONFIG_PCI
  1966. static int ata_resources_present(struct pci_dev *pdev, int port)
  1967. {
  1968. int i;
  1969. /* Check the PCI resources for this channel are enabled */
  1970. port = port * 2;
  1971. for (i = 0; i < 2; i++) {
  1972. if (pci_resource_start(pdev, port + i) == 0 ||
  1973. pci_resource_len(pdev, port + i) == 0)
  1974. return 0;
  1975. }
  1976. return 1;
  1977. }
  1978. /**
  1979. * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
  1980. * @host: target ATA host
  1981. *
  1982. * Acquire native PCI ATA resources for @host and initialize the
  1983. * first two ports of @host accordingly. Ports marked dummy are
  1984. * skipped and allocation failure makes the port dummy.
  1985. *
  1986. * Note that native PCI resources are valid even for legacy hosts
  1987. * as we fix up pdev resources array early in boot, so this
  1988. * function can be used for both native and legacy SFF hosts.
  1989. *
  1990. * LOCKING:
  1991. * Inherited from calling layer (may sleep).
  1992. *
  1993. * RETURNS:
  1994. * 0 if at least one port is initialized, -ENODEV if no port is
  1995. * available.
  1996. */
  1997. int ata_pci_sff_init_host(struct ata_host *host)
  1998. {
  1999. struct device *gdev = host->dev;
  2000. struct pci_dev *pdev = to_pci_dev(gdev);
  2001. unsigned int mask = 0;
  2002. int i, rc;
  2003. /* request, iomap BARs and init port addresses accordingly */
  2004. for (i = 0; i < 2; i++) {
  2005. struct ata_port *ap = host->ports[i];
  2006. int base = i * 2;
  2007. void __iomem * const *iomap;
  2008. if (ata_port_is_dummy(ap))
  2009. continue;
  2010. /* Discard disabled ports. Some controllers show
  2011. * their unused channels this way. Disabled ports are
  2012. * made dummy.
  2013. */
  2014. if (!ata_resources_present(pdev, i)) {
  2015. ap->ops = &ata_dummy_port_ops;
  2016. continue;
  2017. }
  2018. rc = pcim_iomap_regions(pdev, 0x3 << base,
  2019. dev_driver_string(gdev));
  2020. if (rc) {
  2021. dev_warn(gdev,
  2022. "failed to request/iomap BARs for port %d (errno=%d)\n",
  2023. i, rc);
  2024. if (rc == -EBUSY)
  2025. pcim_pin_device(pdev);
  2026. ap->ops = &ata_dummy_port_ops;
  2027. continue;
  2028. }
  2029. host->iomap = iomap = pcim_iomap_table(pdev);
  2030. ap->ioaddr.cmd_addr = iomap[base];
  2031. ap->ioaddr.altstatus_addr =
  2032. ap->ioaddr.ctl_addr = (void __iomem *)
  2033. ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
  2034. ata_sff_std_ports(&ap->ioaddr);
  2035. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  2036. (unsigned long long)pci_resource_start(pdev, base),
  2037. (unsigned long long)pci_resource_start(pdev, base + 1));
  2038. mask |= 1 << i;
  2039. }
  2040. if (!mask) {
  2041. dev_err(gdev, "no available native port\n");
  2042. return -ENODEV;
  2043. }
  2044. return 0;
  2045. }
  2046. EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
  2047. /**
  2048. * ata_pci_sff_prepare_host - helper to prepare PCI PIO-only SFF ATA host
  2049. * @pdev: target PCI device
  2050. * @ppi: array of port_info, must be enough for two ports
  2051. * @r_host: out argument for the initialized ATA host
  2052. *
  2053. * Helper to allocate PIO-only SFF ATA host for @pdev, acquire
  2054. * all PCI resources and initialize it accordingly in one go.
  2055. *
  2056. * LOCKING:
  2057. * Inherited from calling layer (may sleep).
  2058. *
  2059. * RETURNS:
  2060. * 0 on success, -errno otherwise.
  2061. */
  2062. int ata_pci_sff_prepare_host(struct pci_dev *pdev,
  2063. const struct ata_port_info * const *ppi,
  2064. struct ata_host **r_host)
  2065. {
  2066. struct ata_host *host;
  2067. int rc;
  2068. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  2069. return -ENOMEM;
  2070. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  2071. if (!host) {
  2072. dev_err(&pdev->dev, "failed to allocate ATA host\n");
  2073. rc = -ENOMEM;
  2074. goto err_out;
  2075. }
  2076. rc = ata_pci_sff_init_host(host);
  2077. if (rc)
  2078. goto err_out;
  2079. devres_remove_group(&pdev->dev, NULL);
  2080. *r_host = host;
  2081. return 0;
  2082. err_out:
  2083. devres_release_group(&pdev->dev, NULL);
  2084. return rc;
  2085. }
  2086. EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
  2087. /**
  2088. * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
  2089. * @host: target SFF ATA host
  2090. * @irq_handler: irq_handler used when requesting IRQ(s)
  2091. * @sht: scsi_host_template to use when registering the host
  2092. *
  2093. * This is the counterpart of ata_host_activate() for SFF ATA
  2094. * hosts. This separate helper is necessary because SFF hosts
  2095. * use two separate interrupts in legacy mode.
  2096. *
  2097. * LOCKING:
  2098. * Inherited from calling layer (may sleep).
  2099. *
  2100. * RETURNS:
  2101. * 0 on success, -errno otherwise.
  2102. */
  2103. int ata_pci_sff_activate_host(struct ata_host *host,
  2104. irq_handler_t irq_handler,
  2105. struct scsi_host_template *sht)
  2106. {
  2107. struct device *dev = host->dev;
  2108. struct pci_dev *pdev = to_pci_dev(dev);
  2109. const char *drv_name = dev_driver_string(host->dev);
  2110. int legacy_mode = 0, rc;
  2111. rc = ata_host_start(host);
  2112. if (rc)
  2113. return rc;
  2114. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  2115. u8 tmp8, mask;
  2116. /* TODO: What if one channel is in native mode ... */
  2117. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  2118. mask = (1 << 2) | (1 << 0);
  2119. if ((tmp8 & mask) != mask)
  2120. legacy_mode = 1;
  2121. }
  2122. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2123. return -ENOMEM;
  2124. if (!legacy_mode && pdev->irq) {
  2125. int i;
  2126. rc = devm_request_irq(dev, pdev->irq, irq_handler,
  2127. IRQF_SHARED, drv_name, host);
  2128. if (rc)
  2129. goto out;
  2130. for (i = 0; i < 2; i++) {
  2131. if (ata_port_is_dummy(host->ports[i]))
  2132. continue;
  2133. ata_port_desc(host->ports[i], "irq %d", pdev->irq);
  2134. }
  2135. } else if (legacy_mode) {
  2136. if (!ata_port_is_dummy(host->ports[0])) {
  2137. rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
  2138. irq_handler, IRQF_SHARED,
  2139. drv_name, host);
  2140. if (rc)
  2141. goto out;
  2142. ata_port_desc(host->ports[0], "irq %d",
  2143. ATA_PRIMARY_IRQ(pdev));
  2144. }
  2145. if (!ata_port_is_dummy(host->ports[1])) {
  2146. rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
  2147. irq_handler, IRQF_SHARED,
  2148. drv_name, host);
  2149. if (rc)
  2150. goto out;
  2151. ata_port_desc(host->ports[1], "irq %d",
  2152. ATA_SECONDARY_IRQ(pdev));
  2153. }
  2154. }
  2155. rc = ata_host_register(host, sht);
  2156. out:
  2157. if (rc == 0)
  2158. devres_remove_group(dev, NULL);
  2159. else
  2160. devres_release_group(dev, NULL);
  2161. return rc;
  2162. }
  2163. EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
  2164. static const struct ata_port_info *ata_sff_find_valid_pi(
  2165. const struct ata_port_info * const *ppi)
  2166. {
  2167. int i;
  2168. /* look up the first valid port_info */
  2169. for (i = 0; i < 2 && ppi[i]; i++)
  2170. if (ppi[i]->port_ops != &ata_dummy_port_ops)
  2171. return ppi[i];
  2172. return NULL;
  2173. }
  2174. static int ata_pci_init_one(struct pci_dev *pdev,
  2175. const struct ata_port_info * const *ppi,
  2176. struct scsi_host_template *sht, void *host_priv,
  2177. int hflags, bool bmdma)
  2178. {
  2179. struct device *dev = &pdev->dev;
  2180. const struct ata_port_info *pi;
  2181. struct ata_host *host = NULL;
  2182. int rc;
  2183. DPRINTK("ENTER\n");
  2184. pi = ata_sff_find_valid_pi(ppi);
  2185. if (!pi) {
  2186. dev_err(&pdev->dev, "no valid port_info specified\n");
  2187. return -EINVAL;
  2188. }
  2189. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2190. return -ENOMEM;
  2191. rc = pcim_enable_device(pdev);
  2192. if (rc)
  2193. goto out;
  2194. #ifdef CONFIG_ATA_BMDMA
  2195. if (bmdma)
  2196. /* prepare and activate BMDMA host */
  2197. rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
  2198. else
  2199. #endif
  2200. /* prepare and activate SFF host */
  2201. rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
  2202. if (rc)
  2203. goto out;
  2204. host->private_data = host_priv;
  2205. host->flags |= hflags;
  2206. #ifdef CONFIG_ATA_BMDMA
  2207. if (bmdma) {
  2208. pci_set_master(pdev);
  2209. rc = ata_pci_sff_activate_host(host, ata_bmdma_interrupt, sht);
  2210. } else
  2211. #endif
  2212. rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
  2213. out:
  2214. if (rc == 0)
  2215. devres_remove_group(&pdev->dev, NULL);
  2216. else
  2217. devres_release_group(&pdev->dev, NULL);
  2218. return rc;
  2219. }
  2220. /**
  2221. * ata_pci_sff_init_one - Initialize/register PIO-only PCI IDE controller
  2222. * @pdev: Controller to be initialized
  2223. * @ppi: array of port_info, must be enough for two ports
  2224. * @sht: scsi_host_template to use when registering the host
  2225. * @host_priv: host private_data
  2226. * @hflag: host flags
  2227. *
  2228. * This is a helper function which can be called from a driver's
  2229. * xxx_init_one() probe function if the hardware uses traditional
  2230. * IDE taskfile registers and is PIO only.
  2231. *
  2232. * ASSUMPTION:
  2233. * Nobody makes a single channel controller that appears solely as
  2234. * the secondary legacy port on PCI.
  2235. *
  2236. * LOCKING:
  2237. * Inherited from PCI layer (may sleep).
  2238. *
  2239. * RETURNS:
  2240. * Zero on success, negative on errno-based value on error.
  2241. */
  2242. int ata_pci_sff_init_one(struct pci_dev *pdev,
  2243. const struct ata_port_info * const *ppi,
  2244. struct scsi_host_template *sht, void *host_priv, int hflag)
  2245. {
  2246. return ata_pci_init_one(pdev, ppi, sht, host_priv, hflag, 0);
  2247. }
  2248. EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
  2249. #endif /* CONFIG_PCI */
  2250. /*
  2251. * BMDMA support
  2252. */
  2253. #ifdef CONFIG_ATA_BMDMA
  2254. const struct ata_port_operations ata_bmdma_port_ops = {
  2255. .inherits = &ata_sff_port_ops,
  2256. .error_handler = ata_bmdma_error_handler,
  2257. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  2258. .qc_prep = ata_bmdma_qc_prep,
  2259. .qc_issue = ata_bmdma_qc_issue,
  2260. .sff_irq_clear = ata_bmdma_irq_clear,
  2261. .bmdma_setup = ata_bmdma_setup,
  2262. .bmdma_start = ata_bmdma_start,
  2263. .bmdma_stop = ata_bmdma_stop,
  2264. .bmdma_status = ata_bmdma_status,
  2265. .port_start = ata_bmdma_port_start,
  2266. };
  2267. EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
  2268. const struct ata_port_operations ata_bmdma32_port_ops = {
  2269. .inherits = &ata_bmdma_port_ops,
  2270. .sff_data_xfer = ata_sff_data_xfer32,
  2271. .port_start = ata_bmdma_port_start32,
  2272. };
  2273. EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
  2274. /**
  2275. * ata_bmdma_fill_sg - Fill PCI IDE PRD table
  2276. * @qc: Metadata associated with taskfile to be transferred
  2277. *
  2278. * Fill PCI IDE PRD (scatter-gather) table with segments
  2279. * associated with the current disk command.
  2280. *
  2281. * LOCKING:
  2282. * spin_lock_irqsave(host lock)
  2283. *
  2284. */
  2285. static void ata_bmdma_fill_sg(struct ata_queued_cmd *qc)
  2286. {
  2287. struct ata_port *ap = qc->ap;
  2288. struct ata_bmdma_prd *prd = ap->bmdma_prd;
  2289. struct scatterlist *sg;
  2290. unsigned int si, pi;
  2291. pi = 0;
  2292. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  2293. u32 addr, offset;
  2294. u32 sg_len, len;
  2295. /* determine if physical DMA addr spans 64K boundary.
  2296. * Note h/w doesn't support 64-bit, so we unconditionally
  2297. * truncate dma_addr_t to u32.
  2298. */
  2299. addr = (u32) sg_dma_address(sg);
  2300. sg_len = sg_dma_len(sg);
  2301. while (sg_len) {
  2302. offset = addr & 0xffff;
  2303. len = sg_len;
  2304. if ((offset + sg_len) > 0x10000)
  2305. len = 0x10000 - offset;
  2306. prd[pi].addr = cpu_to_le32(addr);
  2307. prd[pi].flags_len = cpu_to_le32(len & 0xffff);
  2308. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  2309. pi++;
  2310. sg_len -= len;
  2311. addr += len;
  2312. }
  2313. }
  2314. prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2315. }
  2316. /**
  2317. * ata_bmdma_fill_sg_dumb - Fill PCI IDE PRD table
  2318. * @qc: Metadata associated with taskfile to be transferred
  2319. *
  2320. * Fill PCI IDE PRD (scatter-gather) table with segments
  2321. * associated with the current disk command. Perform the fill
  2322. * so that we avoid writing any length 64K records for
  2323. * controllers that don't follow the spec.
  2324. *
  2325. * LOCKING:
  2326. * spin_lock_irqsave(host lock)
  2327. *
  2328. */
  2329. static void ata_bmdma_fill_sg_dumb(struct ata_queued_cmd *qc)
  2330. {
  2331. struct ata_port *ap = qc->ap;
  2332. struct ata_bmdma_prd *prd = ap->bmdma_prd;
  2333. struct scatterlist *sg;
  2334. unsigned int si, pi;
  2335. pi = 0;
  2336. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  2337. u32 addr, offset;
  2338. u32 sg_len, len, blen;
  2339. /* determine if physical DMA addr spans 64K boundary.
  2340. * Note h/w doesn't support 64-bit, so we unconditionally
  2341. * truncate dma_addr_t to u32.
  2342. */
  2343. addr = (u32) sg_dma_address(sg);
  2344. sg_len = sg_dma_len(sg);
  2345. while (sg_len) {
  2346. offset = addr & 0xffff;
  2347. len = sg_len;
  2348. if ((offset + sg_len) > 0x10000)
  2349. len = 0x10000 - offset;
  2350. blen = len & 0xffff;
  2351. prd[pi].addr = cpu_to_le32(addr);
  2352. if (blen == 0) {
  2353. /* Some PATA chipsets like the CS5530 can't
  2354. cope with 0x0000 meaning 64K as the spec
  2355. says */
  2356. prd[pi].flags_len = cpu_to_le32(0x8000);
  2357. blen = 0x8000;
  2358. prd[++pi].addr = cpu_to_le32(addr + 0x8000);
  2359. }
  2360. prd[pi].flags_len = cpu_to_le32(blen);
  2361. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  2362. pi++;
  2363. sg_len -= len;
  2364. addr += len;
  2365. }
  2366. }
  2367. prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2368. }
  2369. /**
  2370. * ata_bmdma_qc_prep - Prepare taskfile for submission
  2371. * @qc: Metadata associated with taskfile to be prepared
  2372. *
  2373. * Prepare ATA taskfile for submission.
  2374. *
  2375. * LOCKING:
  2376. * spin_lock_irqsave(host lock)
  2377. */
  2378. void ata_bmdma_qc_prep(struct ata_queued_cmd *qc)
  2379. {
  2380. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2381. return;
  2382. ata_bmdma_fill_sg(qc);
  2383. }
  2384. EXPORT_SYMBOL_GPL(ata_bmdma_qc_prep);
  2385. /**
  2386. * ata_bmdma_dumb_qc_prep - Prepare taskfile for submission
  2387. * @qc: Metadata associated with taskfile to be prepared
  2388. *
  2389. * Prepare ATA taskfile for submission.
  2390. *
  2391. * LOCKING:
  2392. * spin_lock_irqsave(host lock)
  2393. */
  2394. void ata_bmdma_dumb_qc_prep(struct ata_queued_cmd *qc)
  2395. {
  2396. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2397. return;
  2398. ata_bmdma_fill_sg_dumb(qc);
  2399. }
  2400. EXPORT_SYMBOL_GPL(ata_bmdma_dumb_qc_prep);
  2401. /**
  2402. * ata_bmdma_qc_issue - issue taskfile to a BMDMA controller
  2403. * @qc: command to issue to device
  2404. *
  2405. * This function issues a PIO, NODATA or DMA command to a
  2406. * SFF/BMDMA controller. PIO and NODATA are handled by
  2407. * ata_sff_qc_issue().
  2408. *
  2409. * LOCKING:
  2410. * spin_lock_irqsave(host lock)
  2411. *
  2412. * RETURNS:
  2413. * Zero on success, AC_ERR_* mask on failure
  2414. */
  2415. unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd *qc)
  2416. {
  2417. struct ata_port *ap = qc->ap;
  2418. struct ata_link *link = qc->dev->link;
  2419. /* defer PIO handling to sff_qc_issue */
  2420. if (!ata_is_dma(qc->tf.protocol))
  2421. return ata_sff_qc_issue(qc);
  2422. /* select the device */
  2423. ata_dev_select(ap, qc->dev->devno, 1, 0);
  2424. /* start the command */
  2425. switch (qc->tf.protocol) {
  2426. case ATA_PROT_DMA:
  2427. WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
  2428. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  2429. ap->ops->bmdma_setup(qc); /* set up bmdma */
  2430. ap->ops->bmdma_start(qc); /* initiate bmdma */
  2431. ap->hsm_task_state = HSM_ST_LAST;
  2432. break;
  2433. case ATAPI_PROT_DMA:
  2434. WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
  2435. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  2436. ap->ops->bmdma_setup(qc); /* set up bmdma */
  2437. ap->hsm_task_state = HSM_ST_FIRST;
  2438. /* send cdb by polling if no cdb interrupt */
  2439. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  2440. ata_sff_queue_pio_task(link, 0);
  2441. break;
  2442. default:
  2443. WARN_ON(1);
  2444. return AC_ERR_SYSTEM;
  2445. }
  2446. return 0;
  2447. }
  2448. EXPORT_SYMBOL_GPL(ata_bmdma_qc_issue);
  2449. /**
  2450. * ata_bmdma_port_intr - Handle BMDMA port interrupt
  2451. * @ap: Port on which interrupt arrived (possibly...)
  2452. * @qc: Taskfile currently active in engine
  2453. *
  2454. * Handle port interrupt for given queued command.
  2455. *
  2456. * LOCKING:
  2457. * spin_lock_irqsave(host lock)
  2458. *
  2459. * RETURNS:
  2460. * One if interrupt was handled, zero if not (shared irq).
  2461. */
  2462. unsigned int ata_bmdma_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
  2463. {
  2464. struct ata_eh_info *ehi = &ap->link.eh_info;
  2465. u8 host_stat = 0;
  2466. bool bmdma_stopped = false;
  2467. unsigned int handled;
  2468. if (ap->hsm_task_state == HSM_ST_LAST && ata_is_dma(qc->tf.protocol)) {
  2469. /* check status of DMA engine */
  2470. host_stat = ap->ops->bmdma_status(ap);
  2471. VPRINTK("ata%u: host_stat 0x%X\n", ap->print_id, host_stat);
  2472. /* if it's not our irq... */
  2473. if (!(host_stat & ATA_DMA_INTR))
  2474. return ata_sff_idle_irq(ap);
  2475. /* before we do anything else, clear DMA-Start bit */
  2476. ap->ops->bmdma_stop(qc);
  2477. bmdma_stopped = true;
  2478. if (unlikely(host_stat & ATA_DMA_ERR)) {
  2479. /* error when transferring data to/from memory */
  2480. qc->err_mask |= AC_ERR_HOST_BUS;
  2481. ap->hsm_task_state = HSM_ST_ERR;
  2482. }
  2483. }
  2484. handled = __ata_sff_port_intr(ap, qc, bmdma_stopped);
  2485. if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
  2486. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  2487. return handled;
  2488. }
  2489. EXPORT_SYMBOL_GPL(ata_bmdma_port_intr);
  2490. /**
  2491. * ata_bmdma_interrupt - Default BMDMA ATA host interrupt handler
  2492. * @irq: irq line (unused)
  2493. * @dev_instance: pointer to our ata_host information structure
  2494. *
  2495. * Default interrupt handler for PCI IDE devices. Calls
  2496. * ata_bmdma_port_intr() for each port that is not disabled.
  2497. *
  2498. * LOCKING:
  2499. * Obtains host lock during operation.
  2500. *
  2501. * RETURNS:
  2502. * IRQ_NONE or IRQ_HANDLED.
  2503. */
  2504. irqreturn_t ata_bmdma_interrupt(int irq, void *dev_instance)
  2505. {
  2506. return __ata_sff_interrupt(irq, dev_instance, ata_bmdma_port_intr);
  2507. }
  2508. EXPORT_SYMBOL_GPL(ata_bmdma_interrupt);
  2509. /**
  2510. * ata_bmdma_error_handler - Stock error handler for BMDMA controller
  2511. * @ap: port to handle error for
  2512. *
  2513. * Stock error handler for BMDMA controller. It can handle both
  2514. * PATA and SATA controllers. Most BMDMA controllers should be
  2515. * able to use this EH as-is or with some added handling before
  2516. * and after.
  2517. *
  2518. * LOCKING:
  2519. * Kernel thread context (may sleep)
  2520. */
  2521. void ata_bmdma_error_handler(struct ata_port *ap)
  2522. {
  2523. struct ata_queued_cmd *qc;
  2524. unsigned long flags;
  2525. bool thaw = false;
  2526. qc = __ata_qc_from_tag(ap, ap->link.active_tag);
  2527. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  2528. qc = NULL;
  2529. /* reset PIO HSM and stop DMA engine */
  2530. spin_lock_irqsave(ap->lock, flags);
  2531. if (qc && ata_is_dma(qc->tf.protocol)) {
  2532. u8 host_stat;
  2533. host_stat = ap->ops->bmdma_status(ap);
  2534. /* BMDMA controllers indicate host bus error by
  2535. * setting DMA_ERR bit and timing out. As it wasn't
  2536. * really a timeout event, adjust error mask and
  2537. * cancel frozen state.
  2538. */
  2539. if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
  2540. qc->err_mask = AC_ERR_HOST_BUS;
  2541. thaw = true;
  2542. }
  2543. ap->ops->bmdma_stop(qc);
  2544. /* if we're gonna thaw, make sure IRQ is clear */
  2545. if (thaw) {
  2546. ap->ops->sff_check_status(ap);
  2547. if (ap->ops->sff_irq_clear)
  2548. ap->ops->sff_irq_clear(ap);
  2549. }
  2550. }
  2551. spin_unlock_irqrestore(ap->lock, flags);
  2552. if (thaw)
  2553. ata_eh_thaw_port(ap);
  2554. ata_sff_error_handler(ap);
  2555. }
  2556. EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
  2557. /**
  2558. * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for BMDMA
  2559. * @qc: internal command to clean up
  2560. *
  2561. * LOCKING:
  2562. * Kernel thread context (may sleep)
  2563. */
  2564. void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
  2565. {
  2566. struct ata_port *ap = qc->ap;
  2567. unsigned long flags;
  2568. if (ata_is_dma(qc->tf.protocol)) {
  2569. spin_lock_irqsave(ap->lock, flags);
  2570. ap->ops->bmdma_stop(qc);
  2571. spin_unlock_irqrestore(ap->lock, flags);
  2572. }
  2573. }
  2574. EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
  2575. /**
  2576. * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  2577. * @ap: Port associated with this ATA transaction.
  2578. *
  2579. * Clear interrupt and error flags in DMA status register.
  2580. *
  2581. * May be used as the irq_clear() entry in ata_port_operations.
  2582. *
  2583. * LOCKING:
  2584. * spin_lock_irqsave(host lock)
  2585. */
  2586. void ata_bmdma_irq_clear(struct ata_port *ap)
  2587. {
  2588. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  2589. if (!mmio)
  2590. return;
  2591. iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
  2592. }
  2593. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  2594. /**
  2595. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  2596. * @qc: Info associated with this ATA transaction.
  2597. *
  2598. * LOCKING:
  2599. * spin_lock_irqsave(host lock)
  2600. */
  2601. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  2602. {
  2603. struct ata_port *ap = qc->ap;
  2604. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  2605. u8 dmactl;
  2606. /* load PRD table addr. */
  2607. mb(); /* make sure PRD table writes are visible to controller */
  2608. iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  2609. /* specify data direction, triple-check start bit is clear */
  2610. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2611. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  2612. if (!rw)
  2613. dmactl |= ATA_DMA_WR;
  2614. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2615. /* issue r/w command */
  2616. ap->ops->sff_exec_command(ap, &qc->tf);
  2617. }
  2618. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  2619. /**
  2620. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  2621. * @qc: Info associated with this ATA transaction.
  2622. *
  2623. * LOCKING:
  2624. * spin_lock_irqsave(host lock)
  2625. */
  2626. void ata_bmdma_start(struct ata_queued_cmd *qc)
  2627. {
  2628. struct ata_port *ap = qc->ap;
  2629. u8 dmactl;
  2630. /* start host DMA transaction */
  2631. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2632. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  2633. /* Strictly, one may wish to issue an ioread8() here, to
  2634. * flush the mmio write. However, control also passes
  2635. * to the hardware at this point, and it will interrupt
  2636. * us when we are to resume control. So, in effect,
  2637. * we don't care when the mmio write flushes.
  2638. * Further, a read of the DMA status register _immediately_
  2639. * following the write may not be what certain flaky hardware
  2640. * is expected, so I think it is best to not add a readb()
  2641. * without first all the MMIO ATA cards/mobos.
  2642. * Or maybe I'm just being paranoid.
  2643. *
  2644. * FIXME: The posting of this write means I/O starts are
  2645. * unnecessarily delayed for MMIO
  2646. */
  2647. }
  2648. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  2649. /**
  2650. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  2651. * @qc: Command we are ending DMA for
  2652. *
  2653. * Clears the ATA_DMA_START flag in the dma control register
  2654. *
  2655. * May be used as the bmdma_stop() entry in ata_port_operations.
  2656. *
  2657. * LOCKING:
  2658. * spin_lock_irqsave(host lock)
  2659. */
  2660. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  2661. {
  2662. struct ata_port *ap = qc->ap;
  2663. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  2664. /* clear start/stop bit */
  2665. iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  2666. mmio + ATA_DMA_CMD);
  2667. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  2668. ata_sff_dma_pause(ap);
  2669. }
  2670. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  2671. /**
  2672. * ata_bmdma_status - Read PCI IDE BMDMA status
  2673. * @ap: Port associated with this ATA transaction.
  2674. *
  2675. * Read and return BMDMA status register.
  2676. *
  2677. * May be used as the bmdma_status() entry in ata_port_operations.
  2678. *
  2679. * LOCKING:
  2680. * spin_lock_irqsave(host lock)
  2681. */
  2682. u8 ata_bmdma_status(struct ata_port *ap)
  2683. {
  2684. return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  2685. }
  2686. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  2687. /**
  2688. * ata_bmdma_port_start - Set port up for bmdma.
  2689. * @ap: Port to initialize
  2690. *
  2691. * Called just after data structures for each port are
  2692. * initialized. Allocates space for PRD table.
  2693. *
  2694. * May be used as the port_start() entry in ata_port_operations.
  2695. *
  2696. * LOCKING:
  2697. * Inherited from caller.
  2698. */
  2699. int ata_bmdma_port_start(struct ata_port *ap)
  2700. {
  2701. if (ap->mwdma_mask || ap->udma_mask) {
  2702. ap->bmdma_prd =
  2703. dmam_alloc_coherent(ap->host->dev, ATA_PRD_TBL_SZ,
  2704. &ap->bmdma_prd_dma, GFP_KERNEL);
  2705. if (!ap->bmdma_prd)
  2706. return -ENOMEM;
  2707. }
  2708. return 0;
  2709. }
  2710. EXPORT_SYMBOL_GPL(ata_bmdma_port_start);
  2711. /**
  2712. * ata_bmdma_port_start32 - Set port up for dma.
  2713. * @ap: Port to initialize
  2714. *
  2715. * Called just after data structures for each port are
  2716. * initialized. Enables 32bit PIO and allocates space for PRD
  2717. * table.
  2718. *
  2719. * May be used as the port_start() entry in ata_port_operations for
  2720. * devices that are capable of 32bit PIO.
  2721. *
  2722. * LOCKING:
  2723. * Inherited from caller.
  2724. */
  2725. int ata_bmdma_port_start32(struct ata_port *ap)
  2726. {
  2727. ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
  2728. return ata_bmdma_port_start(ap);
  2729. }
  2730. EXPORT_SYMBOL_GPL(ata_bmdma_port_start32);
  2731. #ifdef CONFIG_PCI
  2732. /**
  2733. * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
  2734. * @pdev: PCI device
  2735. *
  2736. * Some PCI ATA devices report simplex mode but in fact can be told to
  2737. * enter non simplex mode. This implements the necessary logic to
  2738. * perform the task on such devices. Calling it on other devices will
  2739. * have -undefined- behaviour.
  2740. */
  2741. int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
  2742. {
  2743. unsigned long bmdma = pci_resource_start(pdev, 4);
  2744. u8 simplex;
  2745. if (bmdma == 0)
  2746. return -ENOENT;
  2747. simplex = inb(bmdma + 0x02);
  2748. outb(simplex & 0x60, bmdma + 0x02);
  2749. simplex = inb(bmdma + 0x02);
  2750. if (simplex & 0x80)
  2751. return -EOPNOTSUPP;
  2752. return 0;
  2753. }
  2754. EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
  2755. static void ata_bmdma_nodma(struct ata_host *host, const char *reason)
  2756. {
  2757. int i;
  2758. dev_err(host->dev, "BMDMA: %s, falling back to PIO\n", reason);
  2759. for (i = 0; i < 2; i++) {
  2760. host->ports[i]->mwdma_mask = 0;
  2761. host->ports[i]->udma_mask = 0;
  2762. }
  2763. }
  2764. /**
  2765. * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
  2766. * @host: target ATA host
  2767. *
  2768. * Acquire PCI BMDMA resources and initialize @host accordingly.
  2769. *
  2770. * LOCKING:
  2771. * Inherited from calling layer (may sleep).
  2772. */
  2773. void ata_pci_bmdma_init(struct ata_host *host)
  2774. {
  2775. struct device *gdev = host->dev;
  2776. struct pci_dev *pdev = to_pci_dev(gdev);
  2777. int i, rc;
  2778. /* No BAR4 allocation: No DMA */
  2779. if (pci_resource_start(pdev, 4) == 0) {
  2780. ata_bmdma_nodma(host, "BAR4 is zero");
  2781. return;
  2782. }
  2783. /*
  2784. * Some controllers require BMDMA region to be initialized
  2785. * even if DMA is not in use to clear IRQ status via
  2786. * ->sff_irq_clear method. Try to initialize bmdma_addr
  2787. * regardless of dma masks.
  2788. */
  2789. rc = dma_set_mask(&pdev->dev, ATA_DMA_MASK);
  2790. if (rc)
  2791. ata_bmdma_nodma(host, "failed to set dma mask");
  2792. if (!rc) {
  2793. rc = dma_set_coherent_mask(&pdev->dev, ATA_DMA_MASK);
  2794. if (rc)
  2795. ata_bmdma_nodma(host,
  2796. "failed to set consistent dma mask");
  2797. }
  2798. /* request and iomap DMA region */
  2799. rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
  2800. if (rc) {
  2801. ata_bmdma_nodma(host, "failed to request/iomap BAR4");
  2802. return;
  2803. }
  2804. host->iomap = pcim_iomap_table(pdev);
  2805. for (i = 0; i < 2; i++) {
  2806. struct ata_port *ap = host->ports[i];
  2807. void __iomem *bmdma = host->iomap[4] + 8 * i;
  2808. if (ata_port_is_dummy(ap))
  2809. continue;
  2810. ap->ioaddr.bmdma_addr = bmdma;
  2811. if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  2812. (ioread8(bmdma + 2) & 0x80))
  2813. host->flags |= ATA_HOST_SIMPLEX;
  2814. ata_port_desc(ap, "bmdma 0x%llx",
  2815. (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
  2816. }
  2817. }
  2818. EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
  2819. /**
  2820. * ata_pci_bmdma_prepare_host - helper to prepare PCI BMDMA ATA host
  2821. * @pdev: target PCI device
  2822. * @ppi: array of port_info, must be enough for two ports
  2823. * @r_host: out argument for the initialized ATA host
  2824. *
  2825. * Helper to allocate BMDMA ATA host for @pdev, acquire all PCI
  2826. * resources and initialize it accordingly in one go.
  2827. *
  2828. * LOCKING:
  2829. * Inherited from calling layer (may sleep).
  2830. *
  2831. * RETURNS:
  2832. * 0 on success, -errno otherwise.
  2833. */
  2834. int ata_pci_bmdma_prepare_host(struct pci_dev *pdev,
  2835. const struct ata_port_info * const * ppi,
  2836. struct ata_host **r_host)
  2837. {
  2838. int rc;
  2839. rc = ata_pci_sff_prepare_host(pdev, ppi, r_host);
  2840. if (rc)
  2841. return rc;
  2842. ata_pci_bmdma_init(*r_host);
  2843. return 0;
  2844. }
  2845. EXPORT_SYMBOL_GPL(ata_pci_bmdma_prepare_host);
  2846. /**
  2847. * ata_pci_bmdma_init_one - Initialize/register BMDMA PCI IDE controller
  2848. * @pdev: Controller to be initialized
  2849. * @ppi: array of port_info, must be enough for two ports
  2850. * @sht: scsi_host_template to use when registering the host
  2851. * @host_priv: host private_data
  2852. * @hflags: host flags
  2853. *
  2854. * This function is similar to ata_pci_sff_init_one() but also
  2855. * takes care of BMDMA initialization.
  2856. *
  2857. * LOCKING:
  2858. * Inherited from PCI layer (may sleep).
  2859. *
  2860. * RETURNS:
  2861. * Zero on success, negative on errno-based value on error.
  2862. */
  2863. int ata_pci_bmdma_init_one(struct pci_dev *pdev,
  2864. const struct ata_port_info * const * ppi,
  2865. struct scsi_host_template *sht, void *host_priv,
  2866. int hflags)
  2867. {
  2868. return ata_pci_init_one(pdev, ppi, sht, host_priv, hflags, 1);
  2869. }
  2870. EXPORT_SYMBOL_GPL(ata_pci_bmdma_init_one);
  2871. #endif /* CONFIG_PCI */
  2872. #endif /* CONFIG_ATA_BMDMA */
  2873. /**
  2874. * ata_sff_port_init - Initialize SFF/BMDMA ATA port
  2875. * @ap: Port to initialize
  2876. *
  2877. * Called on port allocation to initialize SFF/BMDMA specific
  2878. * fields.
  2879. *
  2880. * LOCKING:
  2881. * None.
  2882. */
  2883. void ata_sff_port_init(struct ata_port *ap)
  2884. {
  2885. INIT_DELAYED_WORK(&ap->sff_pio_task, ata_sff_pio_task);
  2886. ap->ctl = ATA_DEVCTL_OBS;
  2887. ap->last_ctl = 0xFF;
  2888. }
  2889. int __init ata_sff_init(void)
  2890. {
  2891. ata_sff_wq = alloc_workqueue("ata_sff", WQ_MEM_RECLAIM, WQ_MAX_ACTIVE);
  2892. if (!ata_sff_wq)
  2893. return -ENOMEM;
  2894. return 0;
  2895. }
  2896. void ata_sff_exit(void)
  2897. {
  2898. destroy_workqueue(ata_sff_wq);
  2899. }