sdata.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  1. /*
  2. * (S)ATA(PI)/(E)IDE disk driver for file server.
  3. * derived from /sys/src/boot/pc/sdata.c and /sys/src/9/pc/sdata.c
  4. *
  5. * we can't write message into a ctl file on the file server, so
  6. * enable dma and rwm as advertised by the drive & controller.
  7. * if that doesn't work, fix the hardware or turn it off in the source
  8. * (set conf.idedma = 0).
  9. *
  10. * entry points:
  11. ../fs64/9fsfs64.c:38: { "hd", ataread, ataseek, atawrite, setatapart, },
  12. ../fs64/9fsfs64.c:58: nhd = atainit();
  13. ../port/sub.c:1065: return ideread(d, b, c);
  14. ../port/sub.c:1129: return idewrite(d, b, c);
  15. ../port/sub.c:1182: return idesize(d);
  16. ../port/sub.c:1362: ideinit(d);
  17. */
  18. #include "all.h"
  19. #include "io.h"
  20. #include "mem.h"
  21. #include "sd.h"
  22. #include "compat.h"
  23. #undef error
  24. #define HOWMANY(x, y) (((x)+((y)-1))/(y))
  25. #define ROUNDUP(x, y) (HOWMANY((x), (y))*(y))
  26. enum {
  27. IDEBUG = 0,
  28. /* old stuff carried forward */
  29. NCtlr= 8,
  30. NCtlrdrv= 2, /* fixed by hardware for pata */
  31. NDrive= NCtlr*NCtlrdrv,
  32. Maxxfer= 16*1024, /* maximum transfer size/cmd */
  33. Read = 0,
  34. Write,
  35. /* I/O ports */
  36. Ctlr0cmd = 0x1f0,
  37. Ctlr0ctl = 0x3f4,
  38. Ctlr1cmd = 0x170,
  39. Ctlr1ctl = 0x374,
  40. Ctl2cmd = Ctlr0ctl - Ctlr0cmd,
  41. };
  42. #define IDPRINT if(IDEBUG)print
  43. extern SDifc sdataifc;
  44. enum {
  45. DbgCONFIG = 0x0001, /* detected drive config info */
  46. DbgIDENTIFY = 0x0002, /* detected drive identify info */
  47. DbgSTATE = 0x0004, /* dump state on panic */
  48. DbgPROBE = 0x0008, /* trace device probing */
  49. DbgDEBUG = 0x0080, /* the current problem... */
  50. DbgINL = 0x0100, /* That Inil20+ message we hate */
  51. Dbg48BIT = 0x0200, /* 48-bit LBA */
  52. DbgBsy = 0x0400, /* interrupt but Bsy (shared IRQ) */
  53. };
  54. /* adjust to taste */
  55. #define DEBUG (DbgDEBUG|DbgCONFIG)
  56. enum { /* I/O ports */
  57. Data = 0,
  58. Error = 1, /* (read) */
  59. Features = 1, /* (write) */
  60. Count = 2, /* sector count<7-0>, sector count<15-8> */
  61. Ir = 2, /* interrupt reason (PACKET) */
  62. Sector = 3, /* sector number */
  63. Lbalo = 3, /* LBA<7-0>, LBA<31-24> */
  64. Cyllo = 4, /* cylinder low */
  65. Bytelo = 4, /* byte count low (PACKET) */
  66. Lbamid = 4, /* LBA<15-8>, LBA<39-32> */
  67. Cylhi = 5, /* cylinder high */
  68. Bytehi = 5, /* byte count hi (PACKET) */
  69. Lbahi = 5, /* LBA<23-16>, LBA<47-40> */
  70. Dh = 6, /* Device/Head, LBA<32-14> */
  71. Status = 7, /* (read) */
  72. Cmd = 7, /* (write) */
  73. As = 2, /* Alternate Status (read) */
  74. Dc = 2, /* Device Control (write) */
  75. };
  76. enum { /* Error */
  77. Med = 0x01, /* Media error */
  78. Ili = 0x01, /* command set specific (PACKET) */
  79. Nm = 0x02, /* No Media */
  80. Eom = 0x02, /* command set specific (PACKET) */
  81. Abrt = 0x04, /* Aborted command */
  82. Mcr = 0x08, /* Media Change Request */
  83. Idnf = 0x10, /* no user-accessible address */
  84. Mc = 0x20, /* Media Change */
  85. Unc = 0x40, /* Uncorrectable data error */
  86. Wp = 0x40, /* Write Protect */
  87. Icrc = 0x80, /* Interface CRC error */
  88. };
  89. enum { /* Features */
  90. Dma = 0x01, /* data transfer via DMA (PACKET) */
  91. Ovl = 0x02, /* command overlapped (PACKET) */
  92. };
  93. enum { /* Interrupt Reason */
  94. Cd = 0x01, /* Cmd/Data */
  95. Io = 0x02, /* I/O direction */
  96. Rel = 0x04, /* Bus Release */
  97. };
  98. enum { /* Device/Head */
  99. Dev0 = 0xA0, /* Master */
  100. Dev1 = 0xB0, /* Slave */
  101. Lba = 0x40, /* LBA mode */
  102. };
  103. enum { /* internal flags */
  104. Lba48 = 0x1, /* LBA48 mode */
  105. Lba48always = 0x2, /* ... */
  106. };
  107. enum { /* Status, Alternate Status */
  108. Err = 0x01, /* Error */
  109. Chk = 0x01, /* Check error (PACKET) */
  110. Drq = 0x08, /* Data Request */
  111. Dsc = 0x10, /* Device Seek Complete */
  112. Serv = 0x10, /* Service */
  113. Df = 0x20, /* Device Fault */
  114. Dmrd = 0x20, /* DMA ready (PACKET) */
  115. Drdy = 0x40, /* Device Ready */
  116. Bsy = 0x80, /* Busy */
  117. };
  118. enum { /* Cmd */
  119. Cnop = 0x00, /* NOP */
  120. Cdr = 0x08, /* Device Reset */
  121. Crs = 0x20, /* Read Sectors */
  122. Crs48 = 0x24, /* Read Sectors Ext */
  123. Crd48 = 0x25, /* Read w/ DMA Ext */
  124. Crdq48 = 0x26, /* Read w/ DMA Queued Ext */
  125. Crsm48 = 0x29, /* Read Multiple Ext */
  126. Cws = 0x30, /* Write Sectors */
  127. Cws48 = 0x34, /* Write Sectors Ext */
  128. Cwd48 = 0x35, /* Write w/ DMA Ext */
  129. Cwdq48 = 0x36, /* Write w/ DMA Queued Ext */
  130. Cwsm48 = 0x39, /* Write Multiple Ext */
  131. Cedd = 0x90, /* Execute Device Diagnostics */
  132. Cpkt = 0xA0, /* Packet */
  133. Cidpkt = 0xA1, /* Identify Packet Device */
  134. Crsm = 0xC4, /* Read Multiple */
  135. Cwsm = 0xC5, /* Write Multiple */
  136. Csm = 0xC6, /* Set Multiple */
  137. Crdq = 0xC7, /* Read DMA queued */
  138. Crd = 0xC8, /* Read DMA */
  139. Cwd = 0xCA, /* Write DMA */
  140. Cwdq = 0xCC, /* Write DMA queued */
  141. Cstandby = 0xE2, /* Standby */
  142. Cid = 0xEC, /* Identify Device */
  143. Csf = 0xEF, /* Set Features */
  144. };
  145. enum { /* Device Control */
  146. Nien = 0x02, /* (not) Interrupt Enable */
  147. Srst = 0x04, /* Software Reset */
  148. Hob = 0x80, /* High Order Bit [sic] */
  149. };
  150. enum { /* PCI Configuration Registers */
  151. Bmiba = 0x20, /* Bus Master Interface Base Address */
  152. Idetim = 0x40, /* IE Timing */
  153. Sidetim = 0x44, /* Slave IE Timing */
  154. Udmactl = 0x48, /* Ultra DMA/33 Control */
  155. Udmatim = 0x4A, /* Ultra DMA/33 Timing */
  156. };
  157. enum { /* Bus Master IDE I/O Ports */
  158. Bmicx = 0, /* Cmd */
  159. Bmisx = 2, /* Status */
  160. Bmidtpx = 4, /* Descriptor Table Pointer */
  161. };
  162. enum { /* Bmicx */
  163. Ssbm = 0x01, /* Start/Stop Bus Master */
  164. Rwcon = 0x08, /* Read/Write Control */
  165. };
  166. enum { /* Bmisx */
  167. Bmidea = 0x01, /* Bus Master IDE Active */
  168. Idedmae = 0x02, /* IDE DMA Error (R/WC) */
  169. Ideints = 0x04, /* IDE Interrupt Status (R/WC) */
  170. Dma0cap = 0x20, /* Drive 0 DMA Capable */
  171. Dma1cap = 0x40, /* Drive 0 DMA Capable */
  172. };
  173. enum { /* Physical Region Descriptor */
  174. PrdEOT = 0x80000000, /* Bus Master IDE Active */
  175. };
  176. enum { /* offsets into the identify info. */
  177. Iconfig = 0, /* general configuration */
  178. Ilcyl = 1, /* logical cylinders */
  179. Ilhead = 3, /* logical heads */
  180. Ilsec = 6, /* logical sectors per logical track */
  181. Iserial = 10, /* serial number */
  182. Ifirmware = 23, /* firmware revision */
  183. Imodel = 27, /* model number */
  184. Imaxrwm = 47, /* max. read/write multiple sectors */
  185. Icapabilities = 49, /* capabilities */
  186. Istandby = 50, /* device specific standby timer */
  187. Ipiomode = 51, /* PIO data transfer mode number */
  188. Ivalid = 53,
  189. Iccyl = 54, /* cylinders if (valid&0x01) */
  190. Ichead = 55, /* heads if (valid&0x01) */
  191. Icsec = 56, /* sectors if (valid&0x01) */
  192. Iccap = 57, /* capacity if (valid&0x01) */
  193. Irwm = 59, /* read/write multiple */
  194. Ilba = 60, /* LBA size */
  195. Imwdma = 63, /* multiword DMA mode */
  196. Iapiomode = 64, /* advanced PIO modes supported */
  197. Iminmwdma = 65, /* min. multiword DMA cycle time */
  198. Irecmwdma = 66, /* rec. multiword DMA cycle time */
  199. Iminpio = 67, /* min. PIO cycle w/o flow control */
  200. Iminiordy = 68, /* min. PIO cycle with IORDY */
  201. Ipcktbr = 71, /* time from PACKET to bus release */
  202. Iserbsy = 72, /* time from SERVICE to !Bsy */
  203. Iqdepth = 75, /* max. queue depth */
  204. Imajor = 80, /* major version number */
  205. Iminor = 81, /* minor version number */
  206. Icsfs = 82, /* command set/feature supported */
  207. Icsfe = 85, /* command set/feature enabled */
  208. Iudma = 88, /* ultra DMA mode */
  209. Ierase = 89, /* time for security erase */
  210. Ieerase = 90, /* time for enhanced security erase */
  211. Ipower = 91, /* current advanced power management */
  212. Ilba48 = 100, /* 48-bit LBA size (64 bits in 100-103) */
  213. Irmsn = 127, /* removable status notification */
  214. Isecstat = 128, /* security status */
  215. Icfapwr = 160, /* CFA power mode */
  216. Imediaserial = 176, /* current media serial number */
  217. Icksum = 255, /* checksum */
  218. };
  219. enum { /* bit masks for config identify info */
  220. Mpktsz = 0x0003, /* packet command size */
  221. Mincomplete = 0x0004, /* incomplete information */
  222. Mdrq = 0x0060, /* DRQ type */
  223. Mrmdev = 0x0080, /* device is removable */
  224. Mtype = 0x1F00, /* device type */
  225. Mproto = 0x8000, /* command protocol */
  226. };
  227. enum { /* bit masks for capabilities identify info */
  228. Mdma = 0x0100, /* DMA supported */
  229. Mlba = 0x0200, /* LBA supported */
  230. Mnoiordy = 0x0400, /* IORDY may be disabled */
  231. Miordy = 0x0800, /* IORDY supported */
  232. Msoftrst = 0x1000, /* needs soft reset when Bsy */
  233. Mstdby = 0x2000, /* standby supported */
  234. Mqueueing = 0x4000, /* queueing overlap supported */
  235. Midma = 0x8000, /* interleaved DMA supported */
  236. };
  237. enum { /* bit masks for supported/enabled features */
  238. Msmart = 0x0001,
  239. Msecurity = 0x0002,
  240. Mrmmedia = 0x0004,
  241. Mpwrmgmt = 0x0008,
  242. Mpkt = 0x0010,
  243. Mwcache = 0x0020,
  244. Mlookahead = 0x0040,
  245. Mrelirq = 0x0080,
  246. Msvcirq = 0x0100,
  247. Mreset = 0x0200,
  248. Mprotected = 0x0400,
  249. Mwbuf = 0x1000,
  250. Mrbuf = 0x2000,
  251. Mnop = 0x4000,
  252. Mmicrocode = 0x0001,
  253. Mqueued = 0x0002,
  254. Mcfa = 0x0004,
  255. Mapm = 0x0008,
  256. Mnotify = 0x0010,
  257. Mstandby = 0x0020,
  258. Mspinup = 0x0040,
  259. Mmaxsec = 0x0100,
  260. Mautoacoustic = 0x0200,
  261. Maddr48 = 0x0400,
  262. Mdevconfov = 0x0800,
  263. Mflush = 0x1000,
  264. Mflush48 = 0x2000,
  265. Msmarterror = 0x0001,
  266. Msmartselftest = 0x0002,
  267. Mmserial = 0x0004,
  268. Mmpassthru = 0x0008,
  269. Mlogging = 0x0020,
  270. };
  271. typedef struct Ctlr Ctlr;
  272. typedef struct Drive Drive;
  273. typedef struct Prd {
  274. ulong pa; /* Physical Base Address */
  275. int count;
  276. } Prd;
  277. enum {
  278. BMspan = 64*1024, /* must be power of 2 <= 64*1024 */
  279. Nprd = SDmaxio/BMspan+2,
  280. };
  281. typedef struct Ctlr {
  282. int cmdport;
  283. int ctlport;
  284. int irq;
  285. int tbdf;
  286. int bmiba; /* bus master interface base address */
  287. int maxio; /* sector count transfer maximum */
  288. int span; /* don't span this boundary with dma */
  289. Pcidev* pcidev;
  290. void (*ienable)(Ctlr*);
  291. void (*idisable)(Ctlr*);
  292. SDev* sdev;
  293. Drive* drive[NCtlrdrv];
  294. Target target[NTarget]; /* contains filters for stats */
  295. Prd* prdt; /* physical region descriptor table */
  296. void* prdtbase;
  297. QLock; /* current command */
  298. Drive* curdrive;
  299. int command; /* last command issued (debugging) */
  300. Rendez;
  301. int done;
  302. Lock; /* register access */
  303. /* old stuff carried forward */
  304. QLock idelock; /* make seek & i/o atomic in ide* routines */
  305. } Ctlr;
  306. typedef struct Drive {
  307. Ctlr* ctlr;
  308. int dev;
  309. ushort info[256];
  310. int c; /* cylinder */
  311. int h; /* head */
  312. int s; /* sector */
  313. Devsize sectors; /* total sectors */
  314. int secsize; /* sector size */
  315. int dma; /* DMA R/W possible */
  316. int dmactl;
  317. int rwm; /* read/write multiple possible */
  318. int rwmctl;
  319. int pkt; /* PACKET device, length of pktcmd */
  320. uchar pktcmd[16];
  321. int pktdma; /* this PACKET command using dma */
  322. uchar sense[18];
  323. uchar inquiry[48];
  324. QLock; /* drive access */
  325. int command; /* current command */
  326. int write;
  327. uchar* data;
  328. int dlen;
  329. uchar* limit;
  330. int count; /* sectors */
  331. int block; /* R/W bytes per block */
  332. int status;
  333. int error;
  334. int flags; /* internal flags */
  335. /* for ata* routines */
  336. int online;
  337. Devsize offset;
  338. int driveno; /* ctlr*NCtlrdrv + unit */
  339. char lba; /* true if drive has logical block addressing */
  340. char multi; /* non-0 if drive does multiple block xfers */
  341. /*
  342. * old stuff carried forward. it's in Drive not Ctlr to maximise
  343. * possible concurrency.
  344. */
  345. uchar buf[RBUFSIZE];
  346. } Drive;
  347. /* file-server-specific data */
  348. static Ctlr *atactlr[NCtlr];
  349. static SDev *sdevs[NCtlr];
  350. static Drive *atadrive[NDrive];
  351. // static SDunit *sdunits[NDrive];
  352. SDunit* sdgetunit(SDev* sdev, int subno);
  353. static Drive *atadriveprobe(int driveno);
  354. void
  355. presleep(Rendez *r, int (*fn)(void*), void *v)
  356. {
  357. int x;
  358. if (u != nil) {
  359. sleep(r, fn, v);
  360. return;
  361. }
  362. /* else we're in predawn with no u */
  363. x = spllo();
  364. while (!fn(v))
  365. continue;
  366. splx(x);
  367. }
  368. void
  369. pretsleep(Rendez *r, int (*fn)(void*), void *v, int msec)
  370. {
  371. int x;
  372. ulong start;
  373. if (u != nil) {
  374. tsleep(r, fn, v, msec);
  375. return;
  376. }
  377. /* else we're in predawn with no u */
  378. x = spllo();
  379. for (start = m->ticks; TK2MS(m->ticks - start) < msec &&
  380. !fn(v); )
  381. continue;
  382. splx(x);
  383. }
  384. #define sleep presleep
  385. #define tsleep pretsleep
  386. static void
  387. pc87415ienable(Ctlr* ctlr)
  388. {
  389. Pcidev *p;
  390. int x;
  391. p = ctlr->pcidev;
  392. if(p == nil)
  393. return;
  394. x = pcicfgr32(p, 0x40);
  395. if(ctlr->cmdport == p->mem[0].bar)
  396. x &= ~0x00000100;
  397. else
  398. x &= ~0x00000200;
  399. pcicfgw32(p, 0x40, x);
  400. }
  401. static void
  402. atadumpstate(Drive* drive, uchar* cmd, Devsize lba, int count)
  403. {
  404. Prd *prd;
  405. Pcidev *p;
  406. Ctlr *ctlr;
  407. int i, bmiba;
  408. if(!(DEBUG & DbgSTATE)){
  409. USED(drive, cmd, lba, count);
  410. return;
  411. }
  412. ctlr = drive->ctlr;
  413. print("command %2.2uX\n", ctlr->command);
  414. print("data %8.8p limit %8.8p dlen %d status %uX error %uX\n",
  415. drive->data, drive->limit, drive->dlen,
  416. drive->status, drive->error);
  417. if(cmd != nil){
  418. print("lba %d -> %lld, count %d -> %d (%d)\n",
  419. (cmd[2]<<24)|(cmd[3]<<16)|(cmd[4]<<8)|cmd[5],
  420. (Wideoff)lba,
  421. (cmd[7]<<8)|cmd[8], count, drive->count);
  422. }
  423. if(!(inb(ctlr->ctlport+As) & Bsy)){
  424. for(i = 1; i < 7; i++)
  425. print(" 0x%2.2uX", inb(ctlr->cmdport+i));
  426. print(" 0x%2.2uX\n", inb(ctlr->ctlport+As));
  427. }
  428. if(drive->command == Cwd || drive->command == Crd){
  429. bmiba = ctlr->bmiba;
  430. prd = ctlr->prdt;
  431. print("bmicx %2.2uX bmisx %2.2uX prdt %8.8p\n",
  432. inb(bmiba+Bmicx), inb(bmiba+Bmisx), prd);
  433. for(;;){
  434. print("pa 0x%8.8luX count %8.8uX\n",
  435. prd->pa, prd->count);
  436. if(prd->count & PrdEOT)
  437. break;
  438. prd++;
  439. }
  440. }
  441. if(ctlr->pcidev && ctlr->pcidev->vid == 0x8086){
  442. p = ctlr->pcidev;
  443. print("0x40: %4.4uX 0x42: %4.4uX",
  444. pcicfgr16(p, 0x40), pcicfgr16(p, 0x42));
  445. print("0x48: %2.2uX\n", pcicfgr8(p, 0x48));
  446. print("0x4A: %4.4uX\n", pcicfgr16(p, 0x4A));
  447. }
  448. }
  449. static int
  450. atadebug(int cmdport, int ctlport, char* fmt, ...)
  451. {
  452. int i, n;
  453. va_list arg;
  454. char buf[PRINTSIZE];
  455. if(!(DEBUG & DbgPROBE)){
  456. USED(cmdport, ctlport, fmt);
  457. return 0;
  458. }
  459. va_start(arg, fmt);
  460. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  461. va_end(arg);
  462. if(cmdport){
  463. if(buf[n-1] == '\n')
  464. n--;
  465. n += snprint(buf+n, PRINTSIZE-n, " ataregs 0x%uX:",
  466. cmdport);
  467. for(i = Features; i < Cmd; i++)
  468. n += snprint(buf+n, PRINTSIZE-n, " 0x%2.2uX",
  469. inb(cmdport+i));
  470. if(ctlport)
  471. n += snprint(buf+n, PRINTSIZE-n, " 0x%2.2uX",
  472. inb(ctlport+As));
  473. n += snprint(buf+n, PRINTSIZE-n, "\n");
  474. }
  475. putstrn(buf, n);
  476. return n;
  477. }
  478. static int
  479. ataready(int cmdport, int ctlport, int dev, int reset, int ready, int micro)
  480. {
  481. int as;
  482. atadebug(cmdport, ctlport, "ataready: dev %uX reset %uX ready %uX",
  483. dev, reset, ready);
  484. for(;;){
  485. /*
  486. * Wait for the controller to become not busy and
  487. * possibly for a status bit to become true (usually
  488. * Drdy). Must change to the appropriate device
  489. * register set if necessary before testing for ready.
  490. * Always run through the loop at least once so it
  491. * can be used as a test for !Bsy.
  492. */
  493. as = inb(ctlport+As);
  494. if(as & reset){
  495. /* nothing to do */
  496. }
  497. else if(dev){
  498. outb(cmdport+Dh, dev);
  499. dev = 0;
  500. }
  501. else if(ready == 0 || (as & ready)){
  502. atadebug(0, 0, "ataready: %d 0x%2.2uX\n", micro, as);
  503. return as;
  504. }
  505. if(micro-- <= 0){
  506. atadebug(0, 0, "ataready: %d 0x%2.2uX\n", micro, as);
  507. break;
  508. }
  509. microdelay(1);
  510. }
  511. atadebug(cmdport, ctlport, "ataready: timeout");
  512. return -1;
  513. }
  514. /*
  515. static int
  516. atacsf(Drive* drive, vlong csf, int supported)
  517. {
  518. ushort *info;
  519. int cmdset, i, x;
  520. if(supported)
  521. info = &drive->info[Icsfs];
  522. else
  523. info = &drive->info[Icsfe];
  524. for(i = 0; i < 3; i++){
  525. x = (csf>>(16*i)) & 0xFFFF;
  526. if(x == 0)
  527. continue;
  528. cmdset = info[i];
  529. if(cmdset == 0 || cmdset == 0xFFFF)
  530. return 0;
  531. return cmdset & x;
  532. }
  533. return 0;
  534. }
  535. */
  536. static int
  537. atadone(void* arg)
  538. {
  539. return ((Ctlr*)arg)->done;
  540. }
  541. static int
  542. atarwmmode(Drive* drive, int cmdport, int ctlport, int dev)
  543. {
  544. int as, maxrwm, rwm;
  545. maxrwm = (drive->info[Imaxrwm] & 0xFF);
  546. if(maxrwm == 0)
  547. return 0;
  548. /*
  549. * Sometimes drives come up with the current count set
  550. * to 0; if so, set a suitable value, otherwise believe
  551. * the value in Irwm if the 0x100 bit is set.
  552. */
  553. if(drive->info[Irwm] & 0x100)
  554. rwm = (drive->info[Irwm] & 0xFF);
  555. else
  556. rwm = 0;
  557. if(rwm == 0)
  558. rwm = maxrwm;
  559. if(rwm > 16)
  560. rwm = 16;
  561. if(ataready(cmdport, ctlport, dev, Bsy|Drq, Drdy, 102*1000) < 0)
  562. return 0;
  563. outb(cmdport+Count, rwm);
  564. outb(cmdport+Cmd, Csm);
  565. microdelay(1);
  566. as = ataready(cmdport, ctlport, 0, Bsy, Drdy|Df|Err, 1000);
  567. inb(cmdport+Status);
  568. if(as < 0 || (as & (Df|Err)))
  569. return 0;
  570. drive->rwm = rwm;
  571. if (conf.idedma)
  572. drive->rwmctl = drive->rwm; /* FS special */
  573. else
  574. drive->rwm = 0;
  575. return rwm;
  576. }
  577. static int
  578. atadmamode(Drive* drive)
  579. {
  580. int dma;
  581. /*
  582. * Check if any DMA mode enabled.
  583. * Assumes the BIOS has picked and enabled the best.
  584. * This is completely passive at the moment, no attempt is
  585. * made to ensure the hardware is correctly set up.
  586. */
  587. dma = drive->info[Imwdma] & 0x0707;
  588. drive->dma = (dma>>8) & dma;
  589. if(drive->dma == 0 && (drive->info[Ivalid] & 0x04)){
  590. dma = drive->info[Iudma] & 0x7F7F;
  591. drive->dma = (dma>>8) & dma;
  592. if(drive->dma)
  593. drive->dma |= 'U'<<16;
  594. }
  595. if (conf.idedma)
  596. drive->dmactl = drive->dma; /* FS special */
  597. else
  598. drive->dma = 0;
  599. return dma;
  600. }
  601. static int
  602. ataidentify(int cmdport, int ctlport, int dev, int pkt, void* info)
  603. {
  604. int as, command, drdy;
  605. if(pkt){
  606. command = Cidpkt;
  607. drdy = 0;
  608. }
  609. else{
  610. command = Cid;
  611. drdy = Drdy;
  612. }
  613. as = ataready(cmdport, ctlport, dev, Bsy|Drq, drdy, 103*1000);
  614. if(as < 0)
  615. return as;
  616. outb(cmdport+Cmd, command);
  617. microdelay(1);
  618. as = ataready(cmdport, ctlport, 0, Bsy, Drq|Err, 400*1000);
  619. if(as < 0)
  620. return -1;
  621. if(as & Err)
  622. return as;
  623. memset(info, 0, 512);
  624. inss(cmdport+Data, info, 256);
  625. inb(cmdport+Status);
  626. if(DEBUG & DbgIDENTIFY){
  627. int i;
  628. ushort *sp;
  629. sp = (ushort*)info;
  630. for(i = 0; i < 256; i++){
  631. if(i && (i%16) == 0)
  632. print("\n");
  633. print(" %4.4uX", *sp);
  634. sp++;
  635. }
  636. print("\n");
  637. }
  638. return 0;
  639. }
  640. /*
  641. * DEBUGGING only.
  642. * write, read and verify block 1 (never used in an fs otherwise)
  643. * to see if dma and rwm actually work.
  644. * if not, turn them off, though the kernel could be corrupt by then.
  645. */
  646. static void
  647. ataverify(Drive *dp)
  648. {
  649. int n, nb, dev = dp->driveno;
  650. uchar *buf = dp->buf;
  651. if (dp->ctlr == nil)
  652. panic("ataverify: nil ctlr for drive");
  653. atadriveprobe(dev);
  654. print("ataverify h%d...", dev);
  655. for (n = 0; n < RBUFSIZE; n++)
  656. buf[n] = n;
  657. if (ataseek(dev, RBUFSIZE) < 0)
  658. panic("ataverify: seek 1");
  659. nb = atawrite(dev, buf, RBUFSIZE);
  660. if (nb != RBUFSIZE)
  661. print("short write of %d bytes to block 1\n", nb);
  662. else {
  663. for (n = 0; n < RBUFSIZE; n++)
  664. buf[n] = ~n;
  665. if (ataseek(dev, RBUFSIZE) < 0)
  666. panic("ataverify: seek 1");
  667. nb = ataread(dev, buf, RBUFSIZE);
  668. if (nb != RBUFSIZE)
  669. print("short read of %d bytes to block 1\n", nb);
  670. else {
  671. for (n = 0; n < RBUFSIZE; n++)
  672. if ((uchar)buf[n] != (uchar)n)
  673. break;
  674. if (n == RBUFSIZE) {
  675. print("OK\n");
  676. return; /* verified OK */
  677. }
  678. print("byte comparison failed\n");
  679. }
  680. }
  681. print("ataverify: disabling dma and rwm\n");
  682. dp->dmactl = dp->rwmctl = 0;
  683. }
  684. static Drive*
  685. atagetdrive(int cmdport, int ctlport, int dev)
  686. {
  687. Drive *drive;
  688. int as, i, pkt, driveno;
  689. uchar buf[512], *p;
  690. ushort iconfig, *sp;
  691. driveno = (cmdport == Ctlr0cmd? 0:
  692. cmdport == Ctlr1cmd? NCtlrdrv: 2*NCtlrdrv);
  693. if (dev == Dev1)
  694. driveno++;
  695. atadebug(0, 0, "identify: port 0x%uX dev 0x%2.2uX\n", cmdport, dev);
  696. pkt = 1;
  697. retry:
  698. as = ataidentify(cmdport, ctlport, dev, pkt, buf);
  699. if(as < 0)
  700. return nil;
  701. if(as & Err){
  702. if(pkt == 0)
  703. return nil;
  704. pkt = 0;
  705. goto retry;
  706. }
  707. if((drive = malloc(sizeof(Drive))) == nil)
  708. return nil;
  709. drive->dev = dev;
  710. drive->driveno = -1; /* unset */
  711. memmove(drive->info, buf, sizeof(drive->info));
  712. drive->sense[0] = 0x70;
  713. drive->sense[7] = sizeof(drive->sense)-7;
  714. drive->inquiry[2] = 2;
  715. drive->inquiry[3] = 2;
  716. drive->inquiry[4] = sizeof(drive->inquiry)-4;
  717. p = &drive->inquiry[8];
  718. sp = &drive->info[Imodel];
  719. for(i = 0; i < 20; i++){
  720. *p++ = *sp>>8;
  721. *p++ = *sp++;
  722. }
  723. drive->secsize = 512;
  724. /*
  725. * Beware the CompactFlash Association feature set.
  726. * Now, why this value in Iconfig just walks all over the bit
  727. * definitions used in the other parts of the ATA/ATAPI standards
  728. * is a mystery and a sign of true stupidity on someone's part.
  729. * Anyway, the standard says if this value is 0x848A then it's
  730. * CompactFlash and it's NOT a packet device.
  731. */
  732. iconfig = drive->info[Iconfig];
  733. if(iconfig != 0x848A && (iconfig & 0xC000) == 0x8000){
  734. print("atagetdrive: port 0x%uX dev 0x%2.2uX: packet device\n",
  735. cmdport, dev);
  736. if(iconfig & 0x01)
  737. drive->pkt = 16;
  738. else
  739. drive->pkt = 12;
  740. }
  741. else{
  742. if (iconfig == 0x848A)
  743. print("atagetdrive: port 0x%uX dev 0x%2.2uX: non-packet CF device\n",
  744. cmdport, dev);
  745. if(drive->info[Ivalid] & 0x0001){
  746. drive->c = drive->info[Iccyl];
  747. drive->h = drive->info[Ichead];
  748. drive->s = drive->info[Icsec];
  749. }else{
  750. drive->c = drive->info[Ilcyl];
  751. drive->h = drive->info[Ilhead];
  752. drive->s = drive->info[Ilsec];
  753. }
  754. if(drive->info[Icapabilities] & Mlba){
  755. if(drive->info[Icsfs+1] & Maddr48){
  756. drive->sectors = drive->info[Ilba48]
  757. | (drive->info[Ilba48+1]<<16)
  758. | ((Devsize)drive->info[Ilba48+2]<<32);
  759. drive->flags |= Lba48;
  760. }else
  761. drive->sectors = (drive->info[Ilba+1]<<16)
  762. |drive->info[Ilba];
  763. drive->dev |= Lba;
  764. drive->lba = 1;
  765. }else
  766. drive->sectors = drive->c * drive->h * drive->s;
  767. atarwmmode(drive, cmdport, ctlport, dev);
  768. }
  769. atadmamode(drive);
  770. if(DEBUG & DbgCONFIG){
  771. print("ata h%d: dev %2.2uX port %uX config %4.4uX capabilities %4.4uX",
  772. driveno, dev, cmdport, iconfig,
  773. drive->info[Icapabilities]);
  774. print(" mwdma %4.4uX", drive->info[Imwdma]);
  775. if(drive->info[Ivalid] & 0x04)
  776. print(" udma %4.4uX", drive->info[Iudma]);
  777. print(" dma %8.8uX rwm %ud\n", drive->dma, drive->rwm);
  778. if(drive->flags&Lba48)
  779. print("\tLLBA sectors %lld\n", (Wideoff)drive->sectors);
  780. }
  781. return drive;
  782. }
  783. static void
  784. atasrst(int ctlport)
  785. {
  786. /*
  787. * Srst is a big stick and may cause problems if further
  788. * commands are tried before the drives become ready again.
  789. * Also, there will be problems here if overlapped commands
  790. * are ever supported.
  791. */
  792. microdelay(5);
  793. outb(ctlport+Dc, Srst);
  794. microdelay(5);
  795. outb(ctlport+Dc, 0);
  796. microdelay(2*1000);
  797. }
  798. static int drivenum = 0; /* hope that we probe in order */
  799. static void
  800. updprobe(int cmdport)
  801. {
  802. if(cmdport == Ctlr0cmd)
  803. drivenum = NCtlrdrv;
  804. else if (cmdport == Ctlr1cmd)
  805. drivenum = 2*NCtlrdrv;
  806. }
  807. static SDev*
  808. ataprobe(int cmdport, int ctlport, int irq)
  809. {
  810. Ctlr* ctlr;
  811. SDev *sdev;
  812. Drive *drive;
  813. int i, dev, error, rhi, rlo;
  814. if(cmdport == Ctlr0cmd)
  815. drivenum = 0;
  816. else if (cmdport == Ctlr1cmd)
  817. drivenum = NCtlrdrv;
  818. if(ioalloc(cmdport, 8, 0, "atacmd") < 0) {
  819. print("ataprobe: Cannot allocate %X\n", cmdport);
  820. updprobe(cmdport);
  821. return nil;
  822. }
  823. if(ioalloc(ctlport+As, 1, 0, "atactl") < 0){
  824. print("ataprobe: Cannot allocate %X\n", ctlport + As);
  825. iofree(cmdport);
  826. updprobe(cmdport);
  827. return nil;
  828. }
  829. /*
  830. * Try to detect a floating bus.
  831. * Bsy should be cleared. If not, see if the cylinder registers
  832. * are read/write capable.
  833. * If the master fails, try the slave to catch slave-only
  834. * configurations.
  835. * There's no need to restore the tested registers as they will
  836. * be reset on any detected drives by the Cedd command.
  837. * All this indicates is that there is at least one drive on the
  838. * controller; when the non-existent drive is selected in a
  839. * single-drive configuration the registers of the existing drive
  840. * are often seen, only command execution fails.
  841. */
  842. dev = Dev0;
  843. if(inb(ctlport+As) & Bsy){
  844. outb(cmdport+Dh, dev);
  845. microdelay(1);
  846. trydev1:
  847. atadebug(cmdport, ctlport, "ataprobe bsy");
  848. outb(cmdport+Cyllo, 0xAA);
  849. outb(cmdport+Cylhi, 0x55);
  850. outb(cmdport+Sector, 0xFF);
  851. rlo = inb(cmdport+Cyllo);
  852. rhi = inb(cmdport+Cylhi);
  853. if(rlo != 0xAA && (rlo == 0xFF || rhi != 0x55)){
  854. if(dev == Dev1){
  855. release:
  856. iofree(cmdport);
  857. iofree(ctlport+As);
  858. updprobe(cmdport);
  859. return nil;
  860. }
  861. dev = Dev1;
  862. if(ataready(cmdport, ctlport, dev, Bsy, 0, 20*1000) < 0)
  863. goto trydev1;
  864. }
  865. }
  866. /*
  867. * Disable interrupts on any detected controllers.
  868. */
  869. outb(ctlport+Dc, Nien);
  870. tryedd1:
  871. if(ataready(cmdport, ctlport, dev, Bsy|Drq, 0, 105*1000) < 0){
  872. /*
  873. * There's something there, but it didn't come up clean,
  874. * so try hitting it with a big stick. The timing here is
  875. * wrong but this is a last-ditch effort and it sometimes
  876. * gets some marginal hardware back online.
  877. */
  878. atasrst(ctlport);
  879. if(ataready(cmdport, ctlport, dev, Bsy|Drq, 0, 106*1000) < 0)
  880. goto release;
  881. }
  882. /*
  883. * Can only get here if controller is not busy.
  884. * If there are drives Bsy will be set within 400nS,
  885. * must wait 2mS before testing Status.
  886. * Wait for the command to complete (6 seconds max).
  887. */
  888. outb(cmdport+Cmd, Cedd);
  889. delay(2);
  890. if(ataready(cmdport, ctlport, dev, Bsy|Drq, 0, 6*1000*1000) < 0)
  891. goto release;
  892. /*
  893. * If bit 0 of the error register is set then the selected drive
  894. * exists. This is enough to detect single-drive configurations.
  895. * However, if the master exists there is no way short of executing
  896. * a command to determine if a slave is present.
  897. * It appears possible to get here testing Dev0 although it doesn't
  898. * exist and the EDD won't take, so try again with Dev1.
  899. */
  900. error = inb(cmdport+Error);
  901. atadebug(cmdport, ctlport, "ataprobe: dev %uX", dev);
  902. if((error & ~0x80) != 0x01){
  903. if(dev == Dev1)
  904. goto release;
  905. dev = Dev1;
  906. goto tryedd1;
  907. }
  908. /*
  909. * At least one drive is known to exist, try to
  910. * identify it. If that fails, don't bother checking
  911. * any further.
  912. * If the one drive found is Dev0 and the EDD command
  913. * didn't indicate Dev1 doesn't exist, check for it.
  914. */
  915. if((drive = atagetdrive(cmdport, ctlport, dev)) == nil)
  916. goto release;
  917. if((ctlr = malloc(sizeof(Ctlr))) == nil){
  918. free(drive);
  919. goto release;
  920. }
  921. memset(ctlr, 0, sizeof(Ctlr));
  922. if((sdev = malloc(sizeof(SDev))) == nil){
  923. free(ctlr);
  924. free(drive);
  925. goto release;
  926. }
  927. memset(sdev, 0, sizeof(SDev));
  928. drive->ctlr = ctlr;
  929. atactlr[drivenum/NCtlrdrv] = ctlr;
  930. atadrive[drivenum++] = drive;
  931. sdevs[drivenum/NCtlrdrv] = sdev;
  932. if(dev == Dev0){
  933. ctlr->drive[0] = drive;
  934. if(!(error & 0x80)){
  935. /*
  936. * Always leave Dh pointing to a valid drive,
  937. * otherwise a subsequent call to ataready on
  938. * this controller may try to test a bogus Status.
  939. * Ataprobe is the only place possibly invalid
  940. * drives should be selected.
  941. */
  942. drive = atagetdrive(cmdport, ctlport, Dev1);
  943. if(drive != nil){
  944. drive->ctlr = ctlr;
  945. ctlr->drive[1] = drive;
  946. }
  947. else{
  948. outb(cmdport+Dh, Dev0);
  949. microdelay(1);
  950. }
  951. atadrive[drivenum] = drive;
  952. }
  953. }
  954. else
  955. ctlr->drive[1] = drive;
  956. drivenum++;
  957. print("ata%d: cmd 0x%ux ctl 0x%ux irq %d\n",
  958. (drivenum-1)/NCtlrdrv, cmdport, ctlport, irq);
  959. ctlr->cmdport = cmdport;
  960. ctlr->ctlport = ctlport;
  961. ctlr->irq = irq;
  962. ctlr->tbdf = BUSUNKNOWN;
  963. ctlr->command = Cedd; /* debugging */
  964. sdev->ifc = &sdataifc;
  965. sdev->ctlr = ctlr;
  966. sdev->nunit = NCtlrdrv;
  967. ctlr->sdev = sdev;
  968. if (0)
  969. for (i = drivenum - 2; i < drivenum; i++)
  970. if (atadrive[i])
  971. ataverify(atadrive[i]);
  972. updprobe(cmdport);
  973. return sdev;
  974. }
  975. static void
  976. ataclear(SDev *sdev)
  977. {
  978. Ctlr* ctlr;
  979. ctlr = sdev->ctlr;
  980. iofree(ctlr->cmdport);
  981. iofree(ctlr->ctlport + As);
  982. if (ctlr->drive[0])
  983. free(ctlr->drive[0]);
  984. if (ctlr->drive[1])
  985. free(ctlr->drive[1]);
  986. /* TODO: clear entries in atadrive[] too */
  987. if (sdev->name)
  988. free(sdev->name);
  989. free(ctlr);
  990. free(sdev);
  991. }
  992. static char *
  993. atastat(SDev *sdev, char *p, char *e)
  994. {
  995. Ctlr *ctlr = sdev->ctlr;
  996. return seprint(p, e, "%s ata port %X ctl %X irq %d\n",
  997. sdev->name, ctlr->cmdport, ctlr->ctlport, ctlr->irq);
  998. }
  999. #ifndef FS
  1000. static SDev*
  1001. ataprobew(DevConf *cf)
  1002. {
  1003. if (cf->nports != 2)
  1004. error(Ebadarg);
  1005. return ataprobe(cf->ports[0].port, cf->ports[1].port, cf->intnum);
  1006. }
  1007. #endif
  1008. static int
  1009. atasetsense(Drive* drive, int status, int key, int asc, int ascq)
  1010. {
  1011. drive->sense[2] = key;
  1012. drive->sense[12] = asc;
  1013. drive->sense[13] = ascq;
  1014. return status;
  1015. }
  1016. static int
  1017. atastandby(Drive* drive, int period)
  1018. {
  1019. Ctlr* ctlr;
  1020. int cmdport, done;
  1021. ctlr = drive->ctlr;
  1022. drive->command = Cstandby;
  1023. qlock(ctlr);
  1024. cmdport = ctlr->cmdport;
  1025. ilock(ctlr);
  1026. outb(cmdport+Count, period);
  1027. outb(cmdport+Dh, drive->dev);
  1028. ctlr->done = 0;
  1029. ctlr->curdrive = drive;
  1030. ctlr->command = Cstandby; /* debugging */
  1031. outb(cmdport+Cmd, Cstandby);
  1032. iunlock(ctlr);
  1033. while(waserror())
  1034. ;
  1035. tsleep(ctlr, atadone, ctlr, 30*1000);
  1036. poperror();
  1037. done = ctlr->done;
  1038. qunlock(ctlr);
  1039. if(!done || (drive->status & Err))
  1040. return atasetsense(drive, SDcheck, 4, 8, drive->error);
  1041. return SDok;
  1042. }
  1043. static int
  1044. atamodesense(Drive* drive, uchar* cmd)
  1045. {
  1046. int len;
  1047. /*
  1048. * Fake a vendor-specific request with page code 0,
  1049. * return the drive info.
  1050. */
  1051. if((cmd[2] & 0x3F) != 0 && (cmd[2] & 0x3F) != 0x3F)
  1052. return atasetsense(drive, SDcheck, 0x05, 0x24, 0);
  1053. len = (cmd[7]<<8)|cmd[8];
  1054. if(len == 0)
  1055. return SDok;
  1056. if(len < 8+sizeof(drive->info))
  1057. return atasetsense(drive, SDcheck, 0x05, 0x1A, 0);
  1058. if(drive->data == nil || drive->dlen < len)
  1059. return atasetsense(drive, SDcheck, 0x05, 0x20, 1);
  1060. memset(drive->data, 0, 8);
  1061. drive->data[0] = sizeof(drive->info)>>8;
  1062. drive->data[1] = sizeof(drive->info);
  1063. memmove(drive->data+8, drive->info, sizeof(drive->info));
  1064. drive->data += 8+sizeof(drive->info);
  1065. return SDok;
  1066. }
  1067. static void
  1068. atanop(Drive* drive, int subcommand)
  1069. {
  1070. Ctlr* ctlr;
  1071. int as, cmdport, ctlport, timeo;
  1072. /*
  1073. * Attempt to abort a command by using NOP.
  1074. * In response, the drive is supposed to set Abrt
  1075. * in the Error register, set (Drdy|Err) in Status
  1076. * and clear Bsy when done. However, some drives
  1077. * (e.g. ATAPI Zip) just go Bsy then clear Status
  1078. * when done, hence the timeout loop only on Bsy
  1079. * and the forced setting of drive->error.
  1080. */
  1081. ctlr = drive->ctlr;
  1082. cmdport = ctlr->cmdport;
  1083. outb(cmdport+Features, subcommand);
  1084. outb(cmdport+Dh, drive->dev);
  1085. ctlr->command = Cnop; /* debugging */
  1086. outb(cmdport+Cmd, Cnop);
  1087. microdelay(1);
  1088. ctlport = ctlr->ctlport;
  1089. for(timeo = 0; timeo < 1000; timeo++){
  1090. as = inb(ctlport+As);
  1091. if(!(as & Bsy))
  1092. break;
  1093. microdelay(1);
  1094. }
  1095. drive->error |= Abrt;
  1096. }
  1097. static void
  1098. ataabort(Drive* drive, int dolock)
  1099. {
  1100. /*
  1101. * If NOP is available (packet commands) use it otherwise
  1102. * must try a software reset.
  1103. */
  1104. if(dolock)
  1105. ilock(drive->ctlr);
  1106. if(drive->info[Icsfs] & Mnop)
  1107. atanop(drive, 0);
  1108. else{
  1109. atasrst(drive->ctlr->ctlport);
  1110. drive->error |= Abrt;
  1111. }
  1112. if(dolock)
  1113. iunlock(drive->ctlr);
  1114. }
  1115. static int
  1116. atadmasetup(Drive* drive, int len)
  1117. {
  1118. Prd *prd;
  1119. ulong pa;
  1120. Ctlr *ctlr;
  1121. int bmiba, bmisx, count, i, span;
  1122. ctlr = drive->ctlr;
  1123. pa = PCIWADDR(drive->data);
  1124. if(pa & 0x03)
  1125. return -1;
  1126. /*
  1127. * Sometimes drives identify themselves as being DMA capable
  1128. * although they are not on a busmastering controller.
  1129. */
  1130. prd = ctlr->prdt;
  1131. if(prd == nil){
  1132. drive->dmactl = 0;
  1133. print("h%d: disabling dma: not on a busmastering controller\n",
  1134. drive->driveno);
  1135. return -1;
  1136. }
  1137. for(i = 0; len && i < Nprd; i++){
  1138. prd->pa = pa;
  1139. span = ROUNDUP(pa, ctlr->span);
  1140. if(span == pa)
  1141. span += ctlr->span;
  1142. count = span - pa;
  1143. if(count >= len){
  1144. prd->count = PrdEOT|len;
  1145. break;
  1146. }
  1147. prd->count = count;
  1148. len -= count;
  1149. pa += count;
  1150. prd++;
  1151. }
  1152. if(i == Nprd)
  1153. (prd-1)->count |= PrdEOT;
  1154. bmiba = ctlr->bmiba;
  1155. outl(bmiba+Bmidtpx, PCIWADDR(ctlr->prdt));
  1156. if(drive->write)
  1157. outb(ctlr->bmiba+Bmicx, 0);
  1158. else
  1159. outb(ctlr->bmiba+Bmicx, Rwcon);
  1160. bmisx = inb(bmiba+Bmisx);
  1161. outb(bmiba+Bmisx, bmisx|Ideints|Idedmae);
  1162. return 0;
  1163. }
  1164. static void
  1165. atadmastart(Ctlr* ctlr, int write)
  1166. {
  1167. if(write)
  1168. outb(ctlr->bmiba+Bmicx, Ssbm);
  1169. else
  1170. outb(ctlr->bmiba+Bmicx, Rwcon|Ssbm);
  1171. }
  1172. static int
  1173. atadmastop(Ctlr* ctlr)
  1174. {
  1175. int bmiba;
  1176. bmiba = ctlr->bmiba;
  1177. outb(bmiba+Bmicx, inb(bmiba+Bmicx) & ~Ssbm);
  1178. return inb(bmiba+Bmisx);
  1179. }
  1180. static void
  1181. atadmainterrupt(Drive* drive, int count)
  1182. {
  1183. Ctlr* ctlr;
  1184. int bmiba, bmisx;
  1185. ctlr = drive->ctlr;
  1186. bmiba = ctlr->bmiba;
  1187. bmisx = inb(bmiba+Bmisx);
  1188. switch(bmisx & (Ideints|Idedmae|Bmidea)){
  1189. case Bmidea:
  1190. /*
  1191. * Data transfer still in progress, nothing to do
  1192. * (this should never happen).
  1193. */
  1194. return;
  1195. case Ideints:
  1196. case Ideints|Bmidea:
  1197. /*
  1198. * Normal termination, tidy up.
  1199. */
  1200. drive->data += count;
  1201. break;
  1202. default:
  1203. /*
  1204. * What's left are error conditions (memory transfer
  1205. * problem) and the device is not done but the PRD is
  1206. * exhausted. For both cases must somehow tell the
  1207. * drive to abort.
  1208. */
  1209. ataabort(drive, 0);
  1210. break;
  1211. }
  1212. atadmastop(ctlr);
  1213. ctlr->done = 1;
  1214. }
  1215. static void
  1216. atapktinterrupt(Drive* drive)
  1217. {
  1218. Ctlr* ctlr;
  1219. int cmdport, len;
  1220. ctlr = drive->ctlr;
  1221. cmdport = ctlr->cmdport;
  1222. switch(inb(cmdport+Ir) & (/*Rel|*/Io|Cd)){
  1223. case Cd:
  1224. outss(cmdport+Data, drive->pktcmd, drive->pkt/2);
  1225. break;
  1226. case 0:
  1227. len = (inb(cmdport+Bytehi)<<8)|inb(cmdport+Bytelo);
  1228. if(drive->data+len > drive->limit){
  1229. atanop(drive, 0);
  1230. break;
  1231. }
  1232. outss(cmdport+Data, drive->data, len/2);
  1233. drive->data += len;
  1234. break;
  1235. case Io:
  1236. len = (inb(cmdport+Bytehi)<<8)|inb(cmdport+Bytelo);
  1237. if(drive->data+len > drive->limit){
  1238. atanop(drive, 0);
  1239. break;
  1240. }
  1241. inss(cmdport+Data, drive->data, len/2);
  1242. drive->data += len;
  1243. break;
  1244. case Io|Cd:
  1245. if(drive->pktdma)
  1246. atadmainterrupt(drive, drive->dlen);
  1247. else
  1248. ctlr->done = 1;
  1249. break;
  1250. }
  1251. }
  1252. static int
  1253. atapktio(Drive* drive, uchar* cmd, int clen)
  1254. {
  1255. Ctlr *ctlr;
  1256. int as, cmdport, ctlport, len, r, timeo;
  1257. if(cmd[0] == 0x5A && (cmd[2] & 0x3F) == 0)
  1258. return atamodesense(drive, cmd);
  1259. r = SDok;
  1260. drive->command = Cpkt;
  1261. memmove(drive->pktcmd, cmd, clen);
  1262. memset(drive->pktcmd+clen, 0, drive->pkt-clen);
  1263. drive->limit = drive->data+drive->dlen;
  1264. ctlr = drive->ctlr;
  1265. cmdport = ctlr->cmdport;
  1266. ctlport = ctlr->ctlport;
  1267. qlock(ctlr);
  1268. if(ataready(cmdport, ctlport, drive->dev, Bsy|Drq, 0, 107*1000) < 0){
  1269. qunlock(ctlr);
  1270. return -1;
  1271. }
  1272. ilock(ctlr);
  1273. if(drive->dlen && drive->dmactl && !atadmasetup(drive, drive->dlen))
  1274. drive->pktdma = Dma;
  1275. else
  1276. drive->pktdma = 0;
  1277. outb(cmdport+Features, drive->pktdma);
  1278. outb(cmdport+Count, 0);
  1279. outb(cmdport+Sector, 0);
  1280. len = 16*drive->secsize;
  1281. outb(cmdport+Bytelo, len);
  1282. outb(cmdport+Bytehi, len>>8);
  1283. outb(cmdport+Dh, drive->dev);
  1284. ctlr->done = 0;
  1285. ctlr->curdrive = drive;
  1286. ctlr->command = Cpkt; /* debugging */
  1287. if(drive->pktdma)
  1288. atadmastart(ctlr, drive->write);
  1289. outb(cmdport+Cmd, Cpkt);
  1290. if((drive->info[Iconfig] & Mdrq) != 0x0020){
  1291. microdelay(1);
  1292. as = ataready(cmdport, ctlport, 0, Bsy, Drq|Chk, 4*1000);
  1293. if(as < 0)
  1294. r = SDtimeout;
  1295. else if(as & Chk)
  1296. r = SDcheck;
  1297. else
  1298. atapktinterrupt(drive);
  1299. }
  1300. iunlock(ctlr);
  1301. while(waserror())
  1302. ;
  1303. if(!drive->pktdma)
  1304. sleep(ctlr, atadone, ctlr);
  1305. else for(timeo = 0; !ctlr->done; timeo++){
  1306. tsleep(ctlr, atadone, ctlr, 1000);
  1307. if(ctlr->done)
  1308. break;
  1309. ilock(ctlr);
  1310. atadmainterrupt(drive, 0);
  1311. if(!drive->error && timeo > 10){
  1312. ataabort(drive, 0);
  1313. atadmastop(ctlr);
  1314. drive->dmactl = 0;
  1315. drive->error |= Abrt;
  1316. }
  1317. if(drive->error){
  1318. drive->status |= Chk;
  1319. ctlr->curdrive = nil;
  1320. }
  1321. iunlock(ctlr);
  1322. }
  1323. poperror();
  1324. qunlock(ctlr);
  1325. if(drive->status & Chk)
  1326. r = SDcheck;
  1327. return r;
  1328. }
  1329. static uchar cmd48[256] = {
  1330. [Crs] Crs48,
  1331. [Crd] Crd48,
  1332. [Crdq] Crdq48,
  1333. [Crsm] Crsm48,
  1334. [Cws] Cws48,
  1335. [Cwd] Cwd48,
  1336. [Cwdq] Cwdq48,
  1337. [Cwsm] Cwsm48,
  1338. };
  1339. static int
  1340. atageniostart(Drive* drive, Devsize lba)
  1341. {
  1342. Ctlr *ctlr;
  1343. uchar cmd;
  1344. int as, c, cmdport, ctlport, h, len, s, use48;
  1345. use48 = 0;
  1346. if((drive->flags&Lba48always) || (lba>>28) || drive->count > 256){
  1347. if(!(drive->flags & Lba48))
  1348. return -1;
  1349. use48 = 1;
  1350. c = h = s = 0;
  1351. }else if(drive->dev & Lba){
  1352. c = (lba>>8) & 0xFFFF;
  1353. h = (lba>>24) & 0x0F;
  1354. s = lba & 0xFF;
  1355. }else{
  1356. c = lba/(drive->s*drive->h);
  1357. h = ((lba/drive->s) % drive->h);
  1358. s = (lba % drive->s) + 1;
  1359. }
  1360. ctlr = drive->ctlr;
  1361. cmdport = ctlr->cmdport;
  1362. ctlport = ctlr->ctlport;
  1363. if(ataready(cmdport, ctlport, drive->dev, Bsy|Drq, 0, 101*1000) < 0)
  1364. return -1;
  1365. ilock(ctlr);
  1366. if(drive->dmactl && !atadmasetup(drive, drive->count*drive->secsize)){
  1367. if(drive->write)
  1368. drive->command = Cwd;
  1369. else
  1370. drive->command = Crd;
  1371. }
  1372. else if(drive->rwmctl){
  1373. drive->block = drive->rwm*drive->secsize;
  1374. if(drive->write)
  1375. drive->command = Cwsm;
  1376. else
  1377. drive->command = Crsm;
  1378. }
  1379. else{
  1380. drive->block = drive->secsize;
  1381. if(drive->write)
  1382. drive->command = Cws;
  1383. else
  1384. drive->command = Crs;
  1385. }
  1386. drive->limit = drive->data + drive->count*drive->secsize;
  1387. cmd = drive->command;
  1388. if(use48){
  1389. outb(cmdport+Count, (drive->count>>8) & 0xFF);
  1390. outb(cmdport+Count, drive->count & 0XFF);
  1391. outb(cmdport+Lbalo, (lba>>24) & 0xFF);
  1392. outb(cmdport+Lbalo, lba & 0xFF);
  1393. outb(cmdport+Lbamid, (lba>>32) & 0xFF);
  1394. outb(cmdport+Lbamid, (lba>>8) & 0xFF);
  1395. outb(cmdport+Lbahi, (lba>>40) & 0xFF);
  1396. outb(cmdport+Lbahi, (lba>>16) & 0xFF);
  1397. outb(cmdport+Dh, drive->dev|Lba);
  1398. cmd = cmd48[cmd];
  1399. if(DEBUG & Dbg48BIT)
  1400. print("using 48-bit commands\n");
  1401. }else{
  1402. outb(cmdport+Count, drive->count);
  1403. outb(cmdport+Sector, s);
  1404. outb(cmdport+Cyllo, c);
  1405. outb(cmdport+Cylhi, c>>8);
  1406. outb(cmdport+Dh, drive->dev|h);
  1407. }
  1408. ctlr->done = 0;
  1409. ctlr->curdrive = drive;
  1410. ctlr->command = drive->command; /* debugging */
  1411. outb(cmdport+Cmd, cmd);
  1412. switch(drive->command){
  1413. case Cws:
  1414. case Cwsm:
  1415. microdelay(1);
  1416. as = ataready(cmdport, ctlport, 0, Bsy, Drq|Err, 1000);
  1417. if(as < 0 || (as & Err)){
  1418. iunlock(ctlr);
  1419. return -1;
  1420. }
  1421. len = drive->block;
  1422. if(drive->data+len > drive->limit)
  1423. len = drive->limit-drive->data;
  1424. outss(cmdport+Data, drive->data, len/2);
  1425. break;
  1426. case Crd:
  1427. case Cwd:
  1428. atadmastart(ctlr, drive->write);
  1429. break;
  1430. }
  1431. iunlock(ctlr);
  1432. return 0;
  1433. }
  1434. static int
  1435. atagenioretry(Drive* drive)
  1436. {
  1437. if(drive->dmactl){
  1438. drive->dmactl = 0;
  1439. print("atagenioretry: disabling dma\n");
  1440. }
  1441. else if(drive->rwmctl)
  1442. drive->rwmctl = 0;
  1443. else
  1444. return atasetsense(drive, SDcheck, 4, 8, drive->error);
  1445. return SDretry;
  1446. }
  1447. static int
  1448. atagenio(Drive* drive, uchar* cmd, int)
  1449. {
  1450. uchar *p;
  1451. Ctlr *ctlr;
  1452. Devsize lba, len;
  1453. int count, maxio;
  1454. /*
  1455. * Map SCSI commands into ATA commands for discs.
  1456. * Fail any command with a LUN except INQUIRY which
  1457. * will return 'logical unit not supported'.
  1458. */
  1459. if((cmd[1]>>5) && cmd[0] != 0x12)
  1460. return atasetsense(drive, SDcheck, 0x05, 0x25, 0);
  1461. switch(cmd[0]){
  1462. default:
  1463. return atasetsense(drive, SDcheck, 0x05, 0x20, 0);
  1464. case 0x00: /* test unit ready */
  1465. return SDok;
  1466. case 0x03: /* request sense */
  1467. if(cmd[4] < sizeof(drive->sense))
  1468. len = cmd[4];
  1469. else
  1470. len = sizeof(drive->sense);
  1471. if(drive->data && drive->dlen >= len){
  1472. memmove(drive->data, drive->sense, len);
  1473. drive->data += len;
  1474. }
  1475. return SDok;
  1476. case 0x12: /* inquiry */
  1477. if(cmd[4] < sizeof(drive->inquiry))
  1478. len = cmd[4];
  1479. else
  1480. len = sizeof(drive->inquiry);
  1481. if(drive->data && drive->dlen >= len){
  1482. memmove(drive->data, drive->inquiry, len);
  1483. drive->data += len;
  1484. }
  1485. return SDok;
  1486. case 0x1B: /* start/stop unit */
  1487. /*
  1488. * NOP for now, can use the power management feature
  1489. * set later.
  1490. */
  1491. return SDok;
  1492. case 0x25: /* read capacity */
  1493. if((cmd[1] & 0x01) || cmd[2] || cmd[3])
  1494. return atasetsense(drive, SDcheck, 0x05, 0x24, 0);
  1495. if(drive->data == nil || drive->dlen < 8)
  1496. return atasetsense(drive, SDcheck, 0x05, 0x20, 1);
  1497. /*
  1498. * Read capacity returns the LBA of the last sector.
  1499. */
  1500. len = drive->sectors-1;
  1501. p = drive->data;
  1502. *p++ = len>>24;
  1503. *p++ = len>>16;
  1504. *p++ = len>>8;
  1505. *p++ = len;
  1506. len = drive->secsize;
  1507. *p++ = len>>24;
  1508. *p++ = len>>16;
  1509. *p++ = len>>8;
  1510. *p = len;
  1511. drive->data += 8;
  1512. return SDok;
  1513. case 0x9E: /* long read capacity */
  1514. if((cmd[1] & 0x01) || cmd[2] || cmd[3])
  1515. return atasetsense(drive, SDcheck, 0x05, 0x24, 0);
  1516. if(drive->data == nil || drive->dlen < 8)
  1517. return atasetsense(drive, SDcheck, 0x05, 0x20, 1);
  1518. /*
  1519. * Read capacity returns the LBA of the last sector.
  1520. */
  1521. len = drive->sectors-1;
  1522. p = drive->data;
  1523. *p++ = len>>56;
  1524. *p++ = len>>48;
  1525. *p++ = len>>40;
  1526. *p++ = len>>32;
  1527. *p++ = len>>24;
  1528. *p++ = len>>16;
  1529. *p++ = len>>8;
  1530. *p++ = len;
  1531. len = drive->secsize;
  1532. *p++ = len>>24;
  1533. *p++ = len>>16;
  1534. *p++ = len>>8;
  1535. *p = len;
  1536. drive->data += 8;
  1537. return SDok;
  1538. case 0x28: /* read */
  1539. case 0x2A: /* write */
  1540. break;
  1541. case 0x5A:
  1542. return atamodesense(drive, cmd);
  1543. }
  1544. ctlr = drive->ctlr;
  1545. lba = (cmd[2]<<24)|(cmd[3]<<16)|(cmd[4]<<8)|cmd[5];
  1546. count = (cmd[7]<<8)|cmd[8];
  1547. if(drive->data == nil)
  1548. return SDok;
  1549. if(drive->dlen < count*drive->secsize)
  1550. count = drive->dlen/drive->secsize;
  1551. qlock(ctlr);
  1552. if(ctlr->maxio)
  1553. maxio = ctlr->maxio;
  1554. else if(drive->flags & Lba48)
  1555. maxio = 65536;
  1556. else
  1557. maxio = 256;
  1558. while(count){
  1559. if(count > maxio)
  1560. drive->count = maxio;
  1561. else
  1562. drive->count = count;
  1563. if(atageniostart(drive, lba)){
  1564. ilock(ctlr);
  1565. atanop(drive, 0);
  1566. iunlock(ctlr);
  1567. qunlock(ctlr);
  1568. return atagenioretry(drive);
  1569. }
  1570. while(waserror())
  1571. ;
  1572. tsleep(ctlr, atadone, ctlr, 60*1000);
  1573. poperror();
  1574. if(!ctlr->done){
  1575. /*
  1576. * What should the above timeout be? In
  1577. * standby and sleep modes it could take as
  1578. * long as 30 seconds for a drive to respond.
  1579. * Very hard to get out of this cleanly.
  1580. */
  1581. atadumpstate(drive, cmd, lba, count);
  1582. ataabort(drive, 1);
  1583. qunlock(ctlr);
  1584. return atagenioretry(drive);
  1585. }
  1586. if(drive->status & Err){
  1587. qunlock(ctlr);
  1588. return atasetsense(drive, SDcheck, 4, 8, drive->error);
  1589. }
  1590. count -= drive->count;
  1591. lba += drive->count;
  1592. }
  1593. qunlock(ctlr);
  1594. return SDok;
  1595. }
  1596. static int
  1597. atario(SDreq* r)
  1598. {
  1599. Ctlr *ctlr;
  1600. Drive *drive;
  1601. SDunit *unit;
  1602. uchar cmd10[10], *cmdp, *p;
  1603. int clen, reqstatus, status;
  1604. unit = r->unit;
  1605. if((ctlr = unit->dev->ctlr) == nil || ctlr->drive[unit->subno] == nil){
  1606. r->status = SDtimeout;
  1607. return SDtimeout;
  1608. }
  1609. drive = ctlr->drive[unit->subno];
  1610. /*
  1611. * Most SCSI commands can be passed unchanged except for
  1612. * the padding on the end. The few which require munging
  1613. * are not used internally. Mode select/sense(6) could be
  1614. * converted to the 10-byte form but it's not worth the
  1615. * effort. Read/write(6) are easy.
  1616. */
  1617. switch(r->cmd[0]){
  1618. case 0x08: /* read */
  1619. case 0x0A: /* write */
  1620. cmdp = cmd10;
  1621. memset(cmdp, 0, sizeof(cmd10));
  1622. cmdp[0] = r->cmd[0]|0x20;
  1623. cmdp[1] = r->cmd[1] & 0xE0;
  1624. cmdp[5] = r->cmd[3];
  1625. cmdp[4] = r->cmd[2];
  1626. cmdp[3] = r->cmd[1] & 0x0F;
  1627. cmdp[8] = r->cmd[4];
  1628. clen = sizeof(cmd10);
  1629. break;
  1630. default:
  1631. cmdp = r->cmd;
  1632. clen = r->clen;
  1633. break;
  1634. }
  1635. qlock(drive);
  1636. retry:
  1637. drive->write = r->write;
  1638. drive->data = r->data;
  1639. drive->dlen = r->dlen;
  1640. drive->status = 0;
  1641. drive->error = 0;
  1642. if(drive->pkt)
  1643. status = atapktio(drive, cmdp, clen);
  1644. else
  1645. status = atagenio(drive, cmdp, clen);
  1646. if(status == SDretry){
  1647. if(DEBUG & DbgDEBUG)
  1648. print("%s: retry: dma %8.8uX rwm %4.4uX\n",
  1649. unit->name, drive->dmactl, drive->rwmctl);
  1650. goto retry;
  1651. }
  1652. if(status == SDok){
  1653. atasetsense(drive, SDok, 0, 0, 0);
  1654. if(drive->data){
  1655. p = r->data;
  1656. r->rlen = drive->data - p;
  1657. }
  1658. else
  1659. r->rlen = 0;
  1660. }
  1661. else if(status == SDcheck && !(r->flags & SDnosense)){
  1662. drive->write = 0;
  1663. memset(cmd10, 0, sizeof(cmd10));
  1664. cmd10[0] = 0x03;
  1665. cmd10[1] = r->lun<<5;
  1666. cmd10[4] = sizeof(r->sense)-1;
  1667. drive->data = r->sense;
  1668. drive->dlen = sizeof(r->sense)-1;
  1669. drive->status = 0;
  1670. drive->error = 0;
  1671. if(drive->pkt)
  1672. reqstatus = atapktio(drive, cmd10, 6);
  1673. else
  1674. reqstatus = atagenio(drive, cmd10, 6);
  1675. if(reqstatus == SDok){
  1676. r->flags |= SDvalidsense;
  1677. atasetsense(drive, SDok, 0, 0, 0);
  1678. }
  1679. }
  1680. qunlock(drive);
  1681. r->status = status;
  1682. if(status != SDok)
  1683. return status;
  1684. /*
  1685. * Fix up any results.
  1686. * Many ATAPI CD-ROMs ignore the LUN field completely and
  1687. * return valid INQUIRY data. Patch the response to indicate
  1688. * 'logical unit not supported' if the LUN is non-zero.
  1689. */
  1690. switch(cmdp[0]){
  1691. case 0x12: /* inquiry */
  1692. if((p = r->data) == nil)
  1693. break;
  1694. if((cmdp[1]>>5) && (!drive->pkt || (p[0] & 0x1F) == 0x05))
  1695. p[0] = 0x7F;
  1696. /*FALLTHROUGH*/
  1697. default:
  1698. break;
  1699. }
  1700. return SDok;
  1701. }
  1702. static void
  1703. atainterrupt(Ureg*, void* arg)
  1704. {
  1705. Ctlr *ctlr;
  1706. Drive *drive;
  1707. int cmdport, len, status;
  1708. ctlr = arg;
  1709. ilock(ctlr);
  1710. if(inb(ctlr->ctlport+As) & Bsy){
  1711. iunlock(ctlr);
  1712. if(DEBUG & DbgBsy)
  1713. print("IBsy+");
  1714. return;
  1715. }
  1716. cmdport = ctlr->cmdport;
  1717. status = inb(cmdport+Status);
  1718. if((drive = ctlr->curdrive) == nil){
  1719. iunlock(ctlr);
  1720. if((DEBUG & DbgINL) && ctlr->command != Cedd)
  1721. print("Inil%2.2uX+", ctlr->command);
  1722. return;
  1723. }
  1724. if(status & Err)
  1725. drive->error = inb(cmdport+Error);
  1726. else switch(drive->command){
  1727. default:
  1728. drive->error = Abrt;
  1729. break;
  1730. case Crs:
  1731. case Crsm:
  1732. if(!(status & Drq)){
  1733. drive->error = Abrt;
  1734. break;
  1735. }
  1736. len = drive->block;
  1737. if(drive->data+len > drive->limit)
  1738. len = drive->limit-drive->data;
  1739. inss(cmdport+Data, drive->data, len/2);
  1740. drive->data += len;
  1741. if(drive->data >= drive->limit)
  1742. ctlr->done = 1;
  1743. break;
  1744. case Cws:
  1745. case Cwsm:
  1746. len = drive->block;
  1747. if(drive->data+len > drive->limit)
  1748. len = drive->limit-drive->data;
  1749. drive->data += len;
  1750. if(drive->data >= drive->limit){
  1751. ctlr->done = 1;
  1752. break;
  1753. }
  1754. if(!(status & Drq)){
  1755. drive->error = Abrt;
  1756. break;
  1757. }
  1758. len = drive->block;
  1759. if(drive->data+len > drive->limit)
  1760. len = drive->limit-drive->data;
  1761. outss(cmdport+Data, drive->data, len/2);
  1762. break;
  1763. case Cpkt:
  1764. atapktinterrupt(drive);
  1765. break;
  1766. case Crd:
  1767. case Cwd:
  1768. atadmainterrupt(drive, drive->count*drive->secsize);
  1769. break;
  1770. case Cstandby:
  1771. ctlr->done = 1;
  1772. break;
  1773. }
  1774. iunlock(ctlr);
  1775. if(drive->error){
  1776. status |= Err;
  1777. ctlr->done = 1;
  1778. }
  1779. if(ctlr->done){
  1780. ctlr->curdrive = nil;
  1781. drive->status = status;
  1782. wakeup(ctlr);
  1783. }
  1784. }
  1785. static SDev*
  1786. atapnp(void)
  1787. {
  1788. Ctlr *ctlr;
  1789. Pcidev *p;
  1790. SDev *legacy[2], *sdev, *head, *tail;
  1791. int channel, ispc87415, maxio, pi, r, span;
  1792. static int done;
  1793. if (done)
  1794. return nil;
  1795. done = 1;
  1796. legacy[0] = legacy[1] = head = tail = nil;
  1797. if(sdev = ataprobe(Ctlr0cmd, Ctlr0ctl, IrqATA0)){
  1798. head = tail = sdev;
  1799. legacy[0] = sdev;
  1800. }
  1801. if(sdev = ataprobe(Ctlr1cmd, Ctlr1ctl, IrqATA1)){
  1802. if(head != nil)
  1803. tail->next = sdev;
  1804. else
  1805. head = sdev;
  1806. tail = sdev;
  1807. legacy[1] = sdev;
  1808. }
  1809. p = nil;
  1810. while(p = pcimatch(p, 0, 0)){
  1811. /*
  1812. * Look for devices with the correct class and sub-class
  1813. * code and known device and vendor ID; add native-mode
  1814. * channels to the list to be probed, save info for the
  1815. * compatibility mode channels.
  1816. * Note that the legacy devices should not be considered
  1817. * PCI devices by the interrupt controller.
  1818. * For both native and legacy, save info for busmastering
  1819. * if capable.
  1820. * Promise Ultra ATA/66 (PDC20262) appears to
  1821. * 1) give a sub-class of 'other mass storage controller'
  1822. * instead of 'IDE controller', regardless of whether it's
  1823. * the only controller or not;
  1824. * 2) put 0 in the programming interface byte (probably
  1825. * as a consequence of 1) above).
  1826. * Sub-class code 0x04 is 'RAID controller', e.g. VIA VT8237.
  1827. */
  1828. if(p->ccrb != 0x01)
  1829. continue;
  1830. /*
  1831. * file server special: ccru is a short in the FS kernel,
  1832. * thus the cast to uchar.
  1833. */
  1834. switch ((uchar)p->ccru) {
  1835. case 1:
  1836. case 4:
  1837. case 0x80:
  1838. break;
  1839. default:
  1840. continue;
  1841. }
  1842. pi = p->ccrp;
  1843. ispc87415 = 0;
  1844. maxio = 0;
  1845. span = BMspan;
  1846. switch((p->did<<16)|p->vid){
  1847. default:
  1848. continue;
  1849. case (0x0002<<16)|0x100B: /* NS PC87415 */
  1850. /*
  1851. * Disable interrupts on both channels until
  1852. * after they are probed for drives.
  1853. * This must be called before interrupts are
  1854. * enabled because the IRQ may be shared.
  1855. */
  1856. ispc87415 = 1;
  1857. pcicfgw32(p, 0x40, 0x00000300);
  1858. break;
  1859. case (0x1000<<16)|0x1042: /* PC-Tech RZ1000 */
  1860. /*
  1861. * Turn off prefetch. Overkill, but cheap.
  1862. */
  1863. r = pcicfgr32(p, 0x40);
  1864. r &= ~0x2000;
  1865. pcicfgw32(p, 0x40, r);
  1866. break;
  1867. case (0x4D38<<16)|0x105A: /* Promise PDC20262 */
  1868. case (0x4D30<<16)|0x105A: /* Promise PDC202xx */
  1869. case (0x4D68<<16)|0x105A: /* Promise PDC20268 */
  1870. case (0x4D69<<16)|0x105A: /* Promise Ultra/133 TX2 */
  1871. case (0x3373<<16)|0x105A: /* Promise 20378 RAID */
  1872. case (0x3149<<16)|0x1106: /* VIA VT8237 SATA/RAID */
  1873. case (0x3112<<16)|0x1095: /* SiI 3112 SATA/RAID */
  1874. maxio = 15;
  1875. span = 8*1024;
  1876. /*FALLTHROUGH*/
  1877. case (0x3114<<16)|0x1095: /* SiI 3114 SATA/RAID */
  1878. pi = 0x85;
  1879. break;
  1880. case (0x0004<<16)|0x1103: /* HighPoint HPT366 */
  1881. pi = 0x85;
  1882. /*
  1883. * Turn off fast interrupt prediction.
  1884. */
  1885. if((r = pcicfgr8(p, 0x51)) & 0x80)
  1886. pcicfgw8(p, 0x51, r & ~0x80);
  1887. if((r = pcicfgr8(p, 0x55)) & 0x80)
  1888. pcicfgw8(p, 0x55, r & ~0x80);
  1889. break;
  1890. case (0x0640<<16)|0x1095: /* CMD 640B */
  1891. /*
  1892. * Bugfix code here...
  1893. */
  1894. break;
  1895. case (0x7441<<16)|0x1022: /* AMD 768 */
  1896. /*
  1897. * Set:
  1898. * 0x41 prefetch, postwrite;
  1899. * 0x43 FIFO configuration 1/2 and 1/2;
  1900. * 0x44 status register read retry;
  1901. * 0x46 DMA read and end of sector flush.
  1902. */
  1903. r = pcicfgr8(p, 0x41);
  1904. pcicfgw8(p, 0x41, r|0xF0);
  1905. r = pcicfgr8(p, 0x43);
  1906. pcicfgw8(p, 0x43, (r & 0x90)|0x2A);
  1907. r = pcicfgr8(p, 0x44);
  1908. pcicfgw8(p, 0x44, r|0x08);
  1909. r = pcicfgr8(p, 0x46);
  1910. pcicfgw8(p, 0x46, (r & 0x0C)|0xF0);
  1911. case (0x7469<<16)|0x1022: /* AMD 3111 */
  1912. /*
  1913. * This can probably be lumped in with the 768 above.
  1914. */
  1915. /*FALLTHROUGH*/
  1916. case (0x01BC<<16)|0x10DE: /* nVidia nForce1 */
  1917. case (0x0065<<16)|0x10DE: /* nVidia nForce2 */
  1918. case (0x0085<<16)|0x10DE: /* nVidia nForce2 MCP */
  1919. case (0x00D5<<16)|0x10DE: /* nVidia nForce3 */
  1920. case (0x00E5<<16)|0x10DE: /* nVidia nForce3 Pro */
  1921. case (0x0035<<16)|0x10DE: /* nVidia nForce3 MCP */
  1922. case (0x0053<<16)|0x10DE: /* nVidia nForce4 */
  1923. /*
  1924. * Ditto, although it may have a different base
  1925. * address for the registers (0x50?).
  1926. */
  1927. /*FALLTHROUGH*/
  1928. case (0x4376<<16)|0x1002: /* ATI Radeon Xpress 200M */
  1929. break;
  1930. case (0x0211<<16)|0x1166: /* ServerWorks IB6566 */
  1931. {
  1932. Pcidev *sb;
  1933. sb = pcimatch(nil, 0x1166, 0x0200);
  1934. if(sb == nil)
  1935. break;
  1936. r = pcicfgr32(sb, 0x64);
  1937. r &= ~0x2000;
  1938. pcicfgw32(sb, 0x64, r);
  1939. }
  1940. span = 32*1024;
  1941. break;
  1942. case (0x5513<<16)|0x1039: /* SiS 962 */
  1943. case (0x0646<<16)|0x1095: /* CMD 646 */
  1944. case (0x0571<<16)|0x1106: /* VIA 82C686 */
  1945. case (0x1230<<16)|0x8086: /* 82371FB (PIIX) */
  1946. case (0x7010<<16)|0x8086: /* 82371SB (PIIX3) */
  1947. case (0x7111<<16)|0x8086: /* 82371[AE]B (PIIX4[E]) */
  1948. case (0x2411<<16)|0x8086: /* 82801AA (ICH) */
  1949. case (0x2421<<16)|0x8086: /* 82801AB (ICH0) */
  1950. case (0x244A<<16)|0x8086: /* 82801BA (ICH2, Mobile) */
  1951. case (0x244B<<16)|0x8086: /* 82801BA (ICH2, High-End) */
  1952. case (0x248A<<16)|0x8086: /* 82801CA (ICH3, Mobile) */
  1953. case (0x248B<<16)|0x8086: /* 82801CA (ICH3, High-End) */
  1954. case (0x24CA<<16)|0x8086: /* 82801DBM (ICH4, Mobile) */
  1955. case (0x24CB<<16)|0x8086: /* 82801DB (ICH4, High-End) */
  1956. case (0x24DB<<16)|0x8086: /* 82801EB (ICH5) */
  1957. case (0x266F<<16)|0x8086: /* 82801FB (ICH6) */
  1958. break;
  1959. }
  1960. for(channel = 0; channel < 2; channel++){
  1961. if(pi & (1<<(2*channel))){
  1962. sdev = ataprobe(p->mem[0+2*channel].bar & ~0x01,
  1963. p->mem[1+2*channel].bar & ~0x01,
  1964. p->intl);
  1965. if(sdev == nil)
  1966. continue;
  1967. ctlr = sdev->ctlr;
  1968. if(ispc87415) {
  1969. ctlr->ienable = pc87415ienable;
  1970. print("pc87415disable: not yet implemented\n");
  1971. }
  1972. if(head != nil)
  1973. tail->next = sdev;
  1974. else
  1975. head = sdev;
  1976. tail = sdev;
  1977. ctlr->tbdf = p->tbdf;
  1978. }
  1979. else if((sdev = legacy[channel]) == nil)
  1980. continue;
  1981. else
  1982. ctlr = sdev->ctlr;
  1983. ctlr->pcidev = p;
  1984. ctlr->maxio = maxio;
  1985. ctlr->span = span;
  1986. if(!(pi & 0x80))
  1987. continue;
  1988. ctlr->bmiba = (p->mem[4].bar & ~0x01) + channel*8;
  1989. }
  1990. }
  1991. return head;
  1992. }
  1993. static SDev*
  1994. atalegacy(int port, int irq)
  1995. {
  1996. return ataprobe(port, port+Ctl2cmd, irq);
  1997. }
  1998. static SDev*
  1999. ataid(SDev* sdev)
  2000. {
  2001. int i;
  2002. Ctlr *ctlr;
  2003. char name[32];
  2004. /*
  2005. * Legacy controllers are always 'C' and 'D' and if
  2006. * they exist and have drives will be first in the list.
  2007. * If there are no active legacy controllers, native
  2008. * controllers start at 'C'.
  2009. */
  2010. if(sdev == nil)
  2011. return nil;
  2012. ctlr = sdev->ctlr;
  2013. if(ctlr->cmdport == Ctlr0cmd || ctlr->cmdport == Ctlr1cmd)
  2014. i = 2;
  2015. else
  2016. i = 0;
  2017. while(sdev){
  2018. if(sdev->ifc == &sdataifc){
  2019. ctlr = sdev->ctlr;
  2020. if(ctlr->cmdport == Ctlr0cmd)
  2021. sdev->idno = 'C';
  2022. else if(ctlr->cmdport == Ctlr1cmd)
  2023. sdev->idno = 'D';
  2024. else{
  2025. sdev->idno = 'C'+i;
  2026. i++;
  2027. }
  2028. snprint(name, sizeof(name), "sd%c", sdev->idno);
  2029. kstrdup(&sdev->name, name);
  2030. }
  2031. sdev = sdev->next;
  2032. }
  2033. return nil;
  2034. }
  2035. static int
  2036. ataenable(SDev* sdev)
  2037. {
  2038. Ctlr *ctlr;
  2039. char name[32];
  2040. ctlr = sdev->ctlr;
  2041. if(ctlr->bmiba){
  2042. #define ALIGN (4 * 1024)
  2043. if(ctlr->pcidev != nil)
  2044. pcisetbme(ctlr->pcidev);
  2045. // ctlr->prdt = xspanalloc(Nprd*sizeof(Prd), 4, 4*1024);
  2046. ctlr->prdtbase = xalloc(Nprd * sizeof(Prd) + ALIGN);
  2047. ctlr->prdt = (Prd *)(((ulong)ctlr->prdtbase + ALIGN) & ~(ALIGN - 1));
  2048. }
  2049. snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
  2050. intrenable(ctlr->irq, atainterrupt, ctlr, ctlr->tbdf, name);
  2051. outb(ctlr->ctlport+Dc, 0);
  2052. if(ctlr->ienable)
  2053. ctlr->ienable(ctlr);
  2054. return 1;
  2055. }
  2056. static int
  2057. atadisable(SDev *sdev)
  2058. {
  2059. Ctlr *ctlr;
  2060. char name[32];
  2061. ctlr = sdev->ctlr;
  2062. outb(ctlr->ctlport+Dc, Nien); /* disable interrupts */
  2063. if (ctlr->idisable)
  2064. ctlr->idisable(ctlr);
  2065. snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
  2066. intrdisable(ctlr->irq, atainterrupt, ctlr, ctlr->tbdf, name);
  2067. if (ctlr->bmiba) {
  2068. if (ctlr->pcidev)
  2069. pciclrbme(ctlr->pcidev);
  2070. xfree(ctlr->prdtbase);
  2071. }
  2072. return 0;
  2073. }
  2074. #ifndef FS
  2075. static int
  2076. atarctl(SDunit* unit, char* p, int l)
  2077. {
  2078. int n;
  2079. Ctlr *ctlr;
  2080. Drive *drive;
  2081. if((ctlr = unit->dev->ctlr) == nil || ctlr->drive[unit->subno] == nil)
  2082. return 0;
  2083. drive = ctlr->drive[unit->subno];
  2084. qlock(drive);
  2085. n = snprint(p, l, "config %4.4uX capabilities %4.4uX",
  2086. drive->info[Iconfig], drive->info[Icapabilities]);
  2087. if(drive->dma)
  2088. n += snprint(p+n, l-n, " dma %8.8uX dmactl %8.8uX",
  2089. drive->dma, drive->dmactl);
  2090. if(drive->rwm)
  2091. n += snprint(p+n, l-n, " rwm %ud rwmctl %ud",
  2092. drive->rwm, drive->rwmctl);
  2093. if(drive->flags&Lba48)
  2094. n += snprint(p+n, l-n, " lba48always %s",
  2095. (drive->flags&Lba48always) ? "on" : "off");
  2096. n += snprint(p+n, l-n, "\n");
  2097. if(drive->sectors){
  2098. n += snprint(p+n, l-n, "geometry %lld %d",
  2099. (Wideoff)drive->sectors, drive->secsize);
  2100. if(drive->pkt == 0)
  2101. n += snprint(p+n, l-n, " %d %d %d",
  2102. drive->c, drive->h, drive->s);
  2103. n += snprint(p+n, l-n, "\n");
  2104. }
  2105. qunlock(drive);
  2106. return n;
  2107. }
  2108. static int
  2109. atawctl(SDunit* unit, Cmdbuf* cb)
  2110. {
  2111. int period;
  2112. Ctlr *ctlr;
  2113. Drive *drive;
  2114. if((ctlr = unit->dev->ctlr) == nil || ctlr->drive[unit->subno] == nil)
  2115. return 0;
  2116. drive = ctlr->drive[unit->subno];
  2117. qlock(drive);
  2118. if(waserror()){
  2119. qunlock(drive);
  2120. nexterror();
  2121. }
  2122. /*
  2123. * Dma and rwm control is passive at the moment,
  2124. * i.e. it is assumed that the hardware is set up
  2125. * correctly already either by the BIOS or when
  2126. * the drive was initially identified.
  2127. */
  2128. if(strcmp(cb->f[0], "dma") == 0){
  2129. if(cb->nf != 2 || drive->dma == 0)
  2130. error(Ebadctl);
  2131. if(strcmp(cb->f[1], "on") == 0)
  2132. drive->dmactl = drive->dma;
  2133. else if(strcmp(cb->f[1], "off") == 0)
  2134. drive->dmactl = 0;
  2135. else
  2136. error(Ebadctl);
  2137. }
  2138. else if(strcmp(cb->f[0], "rwm") == 0){
  2139. if(cb->nf != 2 || drive->rwm == 0)
  2140. error(Ebadctl);
  2141. if(strcmp(cb->f[1], "on") == 0)
  2142. drive->rwmctl = drive->rwm;
  2143. else if(strcmp(cb->f[1], "off") == 0)
  2144. drive->rwmctl = 0;
  2145. else
  2146. error(Ebadctl);
  2147. }
  2148. else if(strcmp(cb->f[0], "standby") == 0){
  2149. switch(cb->nf){
  2150. default:
  2151. error(Ebadctl);
  2152. case 2:
  2153. period = strtol(cb->f[1], 0, 0);
  2154. if(period && (period < 30 || period > 240*5))
  2155. error(Ebadctl);
  2156. period /= 5;
  2157. break;
  2158. }
  2159. if(atastandby(drive, period) != SDok)
  2160. error(Ebadctl);
  2161. }
  2162. else if(strcmp(cb->f[0], "lba48always") == 0){
  2163. if(cb->nf != 2 || !(drive->flags&Lba48))
  2164. error(Ebadctl);
  2165. if(strcmp(cb->f[1], "on") == 0)
  2166. drive->flags |= Lba48always;
  2167. else if(strcmp(cb->f[1], "off") == 0)
  2168. drive->flags &= ~Lba48always;
  2169. else
  2170. error(Ebadctl);
  2171. }
  2172. else
  2173. error(Ebadctl);
  2174. qunlock(drive);
  2175. poperror();
  2176. return 0;
  2177. }
  2178. #endif
  2179. SDifc sdataifc = {
  2180. "ata", /* name */
  2181. atapnp, /* pnp */
  2182. atalegacy, /* legacy */
  2183. ataid, /* id */
  2184. ataenable, /* enable */
  2185. atadisable, /* disable */
  2186. scsiverify, /* verify */
  2187. scsionline, /* online */
  2188. atario, /* rio */
  2189. nil, //atarctl, /* rctl */
  2190. nil, //atawctl, /* wctl */
  2191. scsibio, /* bio */
  2192. #ifndef FS
  2193. nil, //ataprobew, /* probe */
  2194. ataclear, /* clear */
  2195. atastat, /* stat */
  2196. #endif
  2197. };
  2198. /*
  2199. * file-server-specific routines
  2200. *
  2201. * ata* routines below this point are used to access nvram file,
  2202. * ide* routines implement the `h' device and call the ata* routines.
  2203. */
  2204. static Drive*
  2205. atapart(Drive *dp)
  2206. {
  2207. return dp;
  2208. }
  2209. static Drive*
  2210. atadriveprobe(int driveno)
  2211. {
  2212. Drive *drive;
  2213. drive = atadrive[driveno];
  2214. if (drive == nil)
  2215. return nil;
  2216. drive->driveno = driveno;
  2217. if(drive->online == 0){
  2218. if(drive->lba)
  2219. print("h%d: LBA %llud sectors\n",
  2220. drive->driveno, (Wideoff)drive->sectors);
  2221. else
  2222. print("h%d: CHS %d/%d/%d %llud sectors\n",
  2223. drive->driveno, drive->c, drive->h, drive->s,
  2224. (Wideoff)drive->sectors);
  2225. drive->online = 1;
  2226. }
  2227. return atapart(drive);
  2228. }
  2229. static void
  2230. cmd_stat(int, char*[])
  2231. {
  2232. Ctlr *ctlr;
  2233. int ctlrno, targetno;
  2234. Target *tp;
  2235. for(ctlrno = 0; ctlrno < nelem(atactlr); ctlrno++){
  2236. ctlr = atactlr[ctlrno];
  2237. if(ctlr == nil || ctlr->sdev == nil)
  2238. continue;
  2239. for(targetno = 0; targetno < NTarget; targetno++){
  2240. tp = &ctlr->target[targetno];
  2241. if(tp->fflag == 0)
  2242. continue;
  2243. print("\t%d.%d work =%7W%7W%7W xfrs\n",
  2244. ctlrno, targetno,
  2245. tp->work+0, tp->work+1, tp->work+2);
  2246. print("\t rate =%7W%7W%7W tBps\n",
  2247. tp->rate+0, tp->rate+1, tp->rate+2);
  2248. }
  2249. }
  2250. }
  2251. /* find all the controllers, enable interrupts, set up SDevs & SDunits */
  2252. int
  2253. atainit(void)
  2254. {
  2255. unsigned i;
  2256. SDev *sdp;
  2257. SDev **sdpp;
  2258. static int first = 1;
  2259. if (first)
  2260. first = 0;
  2261. else
  2262. return 0xFF;
  2263. atapnp();
  2264. for (sdpp = sdevs; sdpp < sdevs + nelem(sdevs); sdpp++) {
  2265. sdp = *sdpp;
  2266. if (sdp == nil)
  2267. continue;
  2268. i = sdpp - sdevs;
  2269. sdp->ifc = &sdataifc;
  2270. sdp->nunit = NCtlrdrv;
  2271. sdp->index = i;
  2272. sdp->idno = 'C' + i;
  2273. sdp->ctlr = atactlr[i];
  2274. if (sdp->ctlr != nil)
  2275. ataenable(sdp);
  2276. }
  2277. cmd_install("stati", "-- ide/ata stats", cmd_stat);
  2278. return 0xFF;
  2279. }
  2280. Devsize
  2281. ataseek(int driveno, Devsize offset)
  2282. {
  2283. Drive *drive = atadrive[driveno];
  2284. if (drive == nil || !drive->online)
  2285. return -1;
  2286. drive->offset = offset;
  2287. return offset;
  2288. }
  2289. /* zero indicates failure; only otherinit() cares */
  2290. int
  2291. setatapart(int driveno, char *)
  2292. {
  2293. /* atadriveprobe() sets drive->online */
  2294. if(atadriveprobe(driveno) == nil)
  2295. return 0;
  2296. return 1;
  2297. }
  2298. static void
  2299. keepstats(Drive *dp, int dbytes)
  2300. {
  2301. Target *tp = &dp->ctlr->target[dp->driveno%NCtlrdrv];
  2302. qlock(tp);
  2303. // if(tp->ok == 0)
  2304. // scsiprobe(d);
  2305. if(tp->fflag == 0) {
  2306. dofilter(tp->work+0, C0a, C0b, 1); /* was , 1000); */
  2307. dofilter(tp->work+1, C1a, C1b, 1); /* was , 1000); */
  2308. dofilter(tp->work+2, C2a, C2b, 1); /* was , 1000); */
  2309. dofilter(tp->rate+0, C0a, C0b, 1);
  2310. dofilter(tp->rate+1, C1a, C1b, 1);
  2311. dofilter(tp->rate+2, C2a, C2b, 1);
  2312. tp->fflag = 1;
  2313. }
  2314. tp->work[0].count++;
  2315. tp->work[1].count++;
  2316. tp->work[2].count++;
  2317. tp->rate[0].count += dbytes;
  2318. tp->rate[1].count += dbytes;
  2319. tp->rate[2].count += dbytes;
  2320. qunlock(tp);
  2321. }
  2322. static long
  2323. ataxfer(Drive *dp, int inout, Devsize start, long bytes)
  2324. {
  2325. unsigned driveno = dp->driveno;
  2326. ulong secsize = dp->secsize, sects;
  2327. SDunit *unit = sdgetunit(sdevs[driveno/NCtlrdrv], driveno%NCtlrdrv);
  2328. if (unit == nil) {
  2329. print("mvsataxfer: nil unit\n");
  2330. return -1;
  2331. }
  2332. if (dp->driveno == -1)
  2333. panic("ataxfer: dp->driveno unset");
  2334. /*
  2335. * unit->dev will be nil if the controller is missing (e.g., h0 on a
  2336. * machine with only sdD, not sdC), so make this a non-fatal error.
  2337. */
  2338. if (unit->dev == nil) {
  2339. print("ataxfer: missing controller for h%d\n", driveno);
  2340. return -1;
  2341. }
  2342. if (unit->dev != sdevs[driveno/NCtlrdrv])
  2343. panic("ataxfer: sdunits[%d].dev=%#p is wrong controller (want %#p)",
  2344. driveno, unit->dev, sdevs[driveno/NCtlrdrv]);
  2345. if (unit->subno != driveno%NCtlrdrv)
  2346. panic("ataxfer: sdunits[%d].subno is %d, not %d",
  2347. driveno, unit->subno, driveno%NCtlrdrv);
  2348. keepstats(dp, bytes);
  2349. if (unit->sectors == 0) {
  2350. unit->sectors = dp->sectors;
  2351. unit->secsize = secsize;
  2352. }
  2353. sects = (bytes + secsize - 1) / secsize; /* round up */
  2354. if (start%secsize != 0)
  2355. print("ataxfer: start offset not on sector boundary\n");
  2356. return scsibio(unit, 0, inout, dp->buf, sects, start/secsize);
  2357. }
  2358. /*
  2359. * ataread & atawrite do the real work; ideread & idewrite just call them.
  2360. * ataread & atawrite are called by the nvram routines.
  2361. * ideread & idewrite are called for normal file server I/O.
  2362. */
  2363. Off
  2364. ataread(int driveno, void *a, long n)
  2365. {
  2366. int skip;
  2367. Off rv, i;
  2368. uchar *aa = a;
  2369. // Ctlr *cp;
  2370. Drive *dp;
  2371. dp = atadrive[driveno];
  2372. if(dp == nil || !dp->online)
  2373. return 0;
  2374. // cp = dp->ctlr;
  2375. if (dp->secsize == 0)
  2376. panic("ataread: sector size of zero");
  2377. skip = dp->offset % dp->secsize;
  2378. for(rv = 0; rv < n; rv += i){
  2379. i = ataxfer(dp, Read, dp->offset+rv-skip, n-rv+skip);
  2380. if(i == 0)
  2381. break;
  2382. if(i < 0)
  2383. return -1;
  2384. i -= skip;
  2385. if(i > n - rv)
  2386. i = n - rv;
  2387. memmove(aa+rv, dp->buf + skip, i);
  2388. skip = 0;
  2389. }
  2390. dp->offset += rv;
  2391. return rv;
  2392. }
  2393. Off
  2394. atawrite(int driveno, void *a, long n)
  2395. {
  2396. Off rv, i, partial;
  2397. uchar *aa = a;
  2398. // Ctlr *cp;
  2399. Drive *dp;
  2400. dp = atadrive[driveno];
  2401. if(dp == nil || !dp->online)
  2402. return 0;
  2403. // cp = dp->ctlr;
  2404. /*
  2405. * if not starting on a sector boundary,
  2406. * read in the first sector before writing it out.
  2407. */
  2408. if (dp->secsize == 0)
  2409. panic("atawrite: sector size of zero");
  2410. partial = dp->offset % dp->secsize;
  2411. if(partial){
  2412. if (ataxfer(dp, Read, dp->offset-partial, dp->secsize) < 0)
  2413. return -1;
  2414. if(partial+n > dp->secsize)
  2415. rv = dp->secsize - partial;
  2416. else
  2417. rv = n;
  2418. memmove(dp->buf+partial, aa, rv);
  2419. if(ataxfer(dp, Write, dp->offset-partial, dp->secsize) < 0)
  2420. return -1;
  2421. } else
  2422. rv = 0;
  2423. /*
  2424. * write out the full sectors (common case)
  2425. */
  2426. partial = (n - rv) % dp->secsize;
  2427. n -= partial;
  2428. for(; rv < n; rv += i){
  2429. i = n - rv;
  2430. if(i > Maxxfer)
  2431. i = Maxxfer;
  2432. memmove(dp->buf, aa+rv, i);
  2433. i = ataxfer(dp, Write, dp->offset+rv, i);
  2434. if(i == 0)
  2435. break;
  2436. if(i < 0)
  2437. return -1;
  2438. }
  2439. /*
  2440. * if not ending on a sector boundary,
  2441. * read in the last sector before writing it out.
  2442. */
  2443. if(partial){
  2444. if(ataxfer(dp, Read, dp->offset+rv, dp->secsize) < 0)
  2445. return -1;
  2446. memmove(dp->buf, aa+rv, partial);
  2447. if(ataxfer(dp, Write, dp->offset+rv, dp->secsize) < 0)
  2448. return -1;
  2449. rv += partial;
  2450. }
  2451. dp->offset += rv;
  2452. return rv;
  2453. }
  2454. /*
  2455. * normal I/O interface
  2456. */
  2457. /* result is size of d in blocks of RBUFSIZE bytes */
  2458. Devsize
  2459. idesize(Device *d)
  2460. {
  2461. Drive *dp = d->private;
  2462. if (dp == nil)
  2463. return 0;
  2464. /*
  2465. * dividing first is sloppy but reduces the range of intermediate
  2466. * values, avoiding possible overflow.
  2467. */
  2468. return (dp->sectors / RBUFSIZE) * dp->secsize;
  2469. }
  2470. void
  2471. ideinit(Device *d)
  2472. {
  2473. int driveno;
  2474. Drive *dp;
  2475. atainit();
  2476. if (d->private)
  2477. return;
  2478. /* call setatapart() first in case we didn't boot off this drive */
  2479. driveno = d->wren.ctrl*NCtlrdrv + d->wren.targ;
  2480. setatapart(driveno, "disk");
  2481. dp = atadriveprobe(driveno);
  2482. if (dp) {
  2483. print("ideinit(ctrl %d targ %d) driveno %d\n",
  2484. d->wren.ctrl, d->wren.targ, dp->driveno);
  2485. if (dp->driveno != driveno)
  2486. panic("ideinit: dp->dev != driveno");
  2487. d->private = dp;
  2488. /* print the sizes now, not later */
  2489. print(
  2490. " idesize(driveno %d): %llud %d-byte sectors -> %llud blocks\n",
  2491. dp->driveno, (Wideoff)dp->sectors, dp->secsize,
  2492. (Wideoff)idesize(d));
  2493. }
  2494. }
  2495. int
  2496. ideread(Device *d, Devsize b, void *c)
  2497. {
  2498. int x, driveno;
  2499. Drive *dp;
  2500. Ctlr *cp;
  2501. if (d == nil || d->private == nil)
  2502. return 1;
  2503. dp = d->private;
  2504. cp = dp->ctlr;
  2505. if (cp == nil)
  2506. panic("ideread: no controller for drive");
  2507. qlock(&cp->idelock);
  2508. cp->idelock.name = "ideio";
  2509. driveno = dp->driveno;
  2510. if (driveno == -1)
  2511. panic("ideread: dp->driveno unset");
  2512. IDPRINT("ideread(dev %p, %lld, %p, %d): %p\n", d, (Wideoff)b, c,
  2513. driveno, dp);
  2514. ataseek(driveno, b * RBUFSIZE);
  2515. x = ataread(driveno, c, RBUFSIZE) != RBUFSIZE;
  2516. qunlock(&cp->idelock);
  2517. return x;
  2518. }
  2519. int
  2520. idewrite(Device *d, Devsize b, void *c)
  2521. {
  2522. int x, driveno;
  2523. Drive *dp;
  2524. Ctlr *cp;
  2525. if (d == nil || d->private == nil)
  2526. return 1;
  2527. dp = d->private;
  2528. cp = dp->ctlr;
  2529. if (cp == nil)
  2530. panic("idewrite: no controller for drive");
  2531. qlock(&cp->idelock);
  2532. cp->idelock.name = "ideio";
  2533. driveno = dp->driveno;
  2534. if (driveno == -1)
  2535. panic("idewrite: dp->driveno unset");
  2536. IDPRINT("idewrite(%p, %lld, %p): driveno %d\n", d, (Wideoff)b, c,
  2537. driveno);
  2538. ataseek(driveno, b * RBUFSIZE);
  2539. x = atawrite(driveno, c, RBUFSIZE) != RBUFSIZE;
  2540. qunlock(&cp->idelock);
  2541. return x;
  2542. }