libata-sff.c 80 KB

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