sdata.c 49 KB

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