libata-sff.c 70 KB

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