libata-sff.c 70 KB

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