libata-sff.c 84 KB

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