libata-sff.c 72 KB

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