libata-sff.c 79 KB

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