libata-sff.c 84 KB

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