libata-sff.c 71 KB

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