libata-sff.c 70 KB

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