libata-sff.c 70 KB

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