libata-sff.c 83 KB

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