libata-sff.c 84 KB

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