libata-sff.c 83 KB

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