libata-sff.c 70 KB

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