sdata.c 62 KB

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