sdata.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  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 = 0x0001, /* detected drive config info */
  13. DbgIDENTIFY = 0x0002, /* detected drive identify info */
  14. DbgSTATE = 0x0004, /* dump state on panic */
  15. DbgPROBE = 0x0008, /* trace device probing */
  16. DbgDEBUG = 0x0080, /* the current problem... */
  17. DbgINL = 0x0100, /* That Inil20+ message we hate */
  18. Dbg48BIT = 0x0200, /* 48-bit LBA */
  19. DbgBsy = 0x0400, /* interrupt but Bsy (shared IRQ) */
  20. };
  21. #define DEBUG (DbgDEBUG|DbgSTATE)
  22. enum { /* I/O ports */
  23. Data = 0,
  24. Error = 1, /* (read) */
  25. Features = 1, /* (write) */
  26. Count = 2, /* sector count<7-0>, sector count<15-8> */
  27. Ir = 2, /* interrupt reason (PACKET) */
  28. Sector = 3, /* sector number */
  29. Lbalo = 3, /* LBA<7-0>, LBA<31-24> */
  30. Cyllo = 4, /* cylinder low */
  31. Bytelo = 4, /* byte count low (PACKET) */
  32. Lbamid = 4, /* LBA<15-8>, LBA<39-32> */
  33. Cylhi = 5, /* cylinder high */
  34. Bytehi = 5, /* byte count hi (PACKET) */
  35. Lbahi = 5, /* LBA<23-16>, LBA<47-40> */
  36. Dh = 6, /* Device/Head, LBA<32-14> */
  37. Status = 7, /* (read) */
  38. Command = 7, /* (write) */
  39. As = 2, /* Alternate Status (read) */
  40. Dc = 2, /* Device Control (write) */
  41. };
  42. enum { /* Error */
  43. Med = 0x01, /* Media error */
  44. Ili = 0x01, /* command set specific (PACKET) */
  45. Nm = 0x02, /* No Media */
  46. Eom = 0x02, /* command set specific (PACKET) */
  47. Abrt = 0x04, /* Aborted command */
  48. Mcr = 0x08, /* Media Change Request */
  49. Idnf = 0x10, /* no user-accessible address */
  50. Mc = 0x20, /* Media Change */
  51. Unc = 0x40, /* Uncorrectable data error */
  52. Wp = 0x40, /* Write Protect */
  53. Icrc = 0x80, /* Interface CRC error */
  54. };
  55. enum { /* Features */
  56. Dma = 0x01, /* data transfer via DMA (PACKET) */
  57. Ovl = 0x02, /* command overlapped (PACKET) */
  58. };
  59. enum { /* Interrupt Reason */
  60. Cd = 0x01, /* Command/Data */
  61. Io = 0x02, /* I/O direction */
  62. Rel = 0x04, /* Bus Release */
  63. };
  64. enum { /* Device/Head */
  65. Dev0 = 0xA0, /* Master */
  66. Dev1 = 0xB0, /* Slave */
  67. Lba = 0x40, /* LBA mode */
  68. };
  69. enum { /* internal flags */
  70. Lba48 = 0x1, /* LBA48 mode */
  71. Lba48always = 0x2, /* ... */
  72. };
  73. enum { /* Status, Alternate Status */
  74. Err = 0x01, /* Error */
  75. Chk = 0x01, /* Check error (PACKET) */
  76. Drq = 0x08, /* Data Request */
  77. Dsc = 0x10, /* Device Seek Complete */
  78. Serv = 0x10, /* Service */
  79. Df = 0x20, /* Device Fault */
  80. Dmrd = 0x20, /* DMA ready (PACKET) */
  81. Drdy = 0x40, /* Device Ready */
  82. Bsy = 0x80, /* Busy */
  83. };
  84. enum { /* Command */
  85. Cnop = 0x00, /* NOP */
  86. Cdr = 0x08, /* Device Reset */
  87. Crs = 0x20, /* Read Sectors */
  88. Crs48 = 0x24, /* Read Sectors Ext */
  89. Crd48 = 0x25, /* Read w/ DMA Ext */
  90. Crdq48 = 0x26, /* Read w/ DMA Queued Ext */
  91. Crsm48 = 0x29, /* Read Multiple Ext */
  92. Cws = 0x30, /* Write Sectors */
  93. Cws48 = 0x34, /* Write Sectors Ext */
  94. Cwd48 = 0x35, /* Write w/ DMA Ext */
  95. Cwdq48 = 0x36, /* Write w/ DMA Queued Ext */
  96. Cwsm48 = 0x39, /* Write Multiple Ext */
  97. Cedd = 0x90, /* Execute Device Diagnostics */
  98. Cpkt = 0xA0, /* Packet */
  99. Cidpkt = 0xA1, /* Identify Packet Device */
  100. Crsm = 0xC4, /* Read Multiple */
  101. Cwsm = 0xC5, /* Write Multiple */
  102. Csm = 0xC6, /* Set Multiple */
  103. Crdq = 0xC7, /* Read DMA queued */
  104. Crd = 0xC8, /* Read DMA */
  105. Cwd = 0xCA, /* Write DMA */
  106. Cwdq = 0xCC, /* Write DMA queued */
  107. Cstandby = 0xE2, /* Standby */
  108. Cid = 0xEC, /* Identify Device */
  109. Csf = 0xEF, /* Set Features */
  110. };
  111. enum { /* Device Control */
  112. Nien = 0x02, /* (not) Interrupt Enable */
  113. Srst = 0x04, /* Software Reset */
  114. Hob = 0x80, /* High Order Bit [sic] */
  115. };
  116. enum { /* PCI Configuration Registers */
  117. Bmiba = 0x20, /* Bus Master Interface Base Address */
  118. Idetim = 0x40, /* IE Timing */
  119. Sidetim = 0x44, /* Slave IE Timing */
  120. Udmactl = 0x48, /* Ultra DMA/33 Control */
  121. Udmatim = 0x4A, /* Ultra DMA/33 Timing */
  122. };
  123. enum { /* Bus Master IDE I/O Ports */
  124. Bmicx = 0, /* Command */
  125. Bmisx = 2, /* Status */
  126. Bmidtpx = 4, /* Descriptor Table Pointer */
  127. };
  128. enum { /* Bmicx */
  129. Ssbm = 0x01, /* Start/Stop Bus Master */
  130. Rwcon = 0x08, /* Read/Write Control */
  131. };
  132. enum { /* Bmisx */
  133. Bmidea = 0x01, /* Bus Master IDE Active */
  134. Idedmae = 0x02, /* IDE DMA Error (R/WC) */
  135. Ideints = 0x04, /* IDE Interrupt Status (R/WC) */
  136. Dma0cap = 0x20, /* Drive 0 DMA Capable */
  137. Dma1cap = 0x40, /* Drive 0 DMA Capable */
  138. };
  139. enum { /* Physical Region Descriptor */
  140. PrdEOT = 0x80000000, /* Bus Master IDE Active */
  141. };
  142. enum { /* offsets into the identify info. */
  143. Iconfig = 0, /* general configuration */
  144. Ilcyl = 1, /* logical cylinders */
  145. Ilhead = 3, /* logical heads */
  146. Ilsec = 6, /* logical sectors per logical track */
  147. Iserial = 10, /* serial number */
  148. Ifirmware = 23, /* firmware revision */
  149. Imodel = 27, /* model number */
  150. Imaxrwm = 47, /* max. read/write multiple sectors */
  151. Icapabilities = 49, /* capabilities */
  152. Istandby = 50, /* device specific standby timer */
  153. Ipiomode = 51, /* PIO data transfer mode number */
  154. Ivalid = 53,
  155. Iccyl = 54, /* cylinders if (valid&0x01) */
  156. Ichead = 55, /* heads if (valid&0x01) */
  157. Icsec = 56, /* sectors if (valid&0x01) */
  158. Iccap = 57, /* capacity if (valid&0x01) */
  159. Irwm = 59, /* read/write multiple */
  160. Ilba = 60, /* LBA size */
  161. Imwdma = 63, /* multiword DMA mode */
  162. Iapiomode = 64, /* advanced PIO modes supported */
  163. Iminmwdma = 65, /* min. multiword DMA cycle time */
  164. Irecmwdma = 66, /* rec. multiword DMA cycle time */
  165. Iminpio = 67, /* min. PIO cycle w/o flow control */
  166. Iminiordy = 68, /* min. PIO cycle with IORDY */
  167. Ipcktbr = 71, /* time from PACKET to bus release */
  168. Iserbsy = 72, /* time from SERVICE to !Bsy */
  169. Iqdepth = 75, /* max. queue depth */
  170. Imajor = 80, /* major version number */
  171. Iminor = 81, /* minor version number */
  172. Icsfs = 82, /* command set/feature supported */
  173. Icsfe = 85, /* command set/feature enabled */
  174. Iudma = 88, /* ultra DMA mode */
  175. Ierase = 89, /* time for security erase */
  176. Ieerase = 90, /* time for enhanced security erase */
  177. Ipower = 91, /* current advanced power management */
  178. Ilba48 = 100, /* 48-bit LBA size (64 bits in 100-103) */
  179. Irmsn = 127, /* removable status notification */
  180. Isecstat = 128, /* security status */
  181. Icfapwr = 160, /* CFA power mode */
  182. Imediaserial = 176, /* current media serial number */
  183. Icksum = 255, /* checksum */
  184. };
  185. enum { /* bit masks for config identify info */
  186. Mpktsz = 0x0003, /* packet command size */
  187. Mincomplete = 0x0004, /* incomplete information */
  188. Mdrq = 0x0060, /* DRQ type */
  189. Mrmdev = 0x0080, /* device is removable */
  190. Mtype = 0x1F00, /* device type */
  191. Mproto = 0x8000, /* command protocol */
  192. };
  193. enum { /* bit masks for capabilities identify info */
  194. Mdma = 0x0100, /* DMA supported */
  195. Mlba = 0x0200, /* LBA supported */
  196. Mnoiordy = 0x0400, /* IORDY may be disabled */
  197. Miordy = 0x0800, /* IORDY supported */
  198. Msoftrst = 0x1000, /* needs soft reset when Bsy */
  199. Mstdby = 0x2000, /* standby supported */
  200. Mqueueing = 0x4000, /* queueing overlap supported */
  201. Midma = 0x8000, /* interleaved DMA supported */
  202. };
  203. enum { /* bit masks for supported/enabled features */
  204. Msmart = 0x0001,
  205. Msecurity = 0x0002,
  206. Mrmmedia = 0x0004,
  207. Mpwrmgmt = 0x0008,
  208. Mpkt = 0x0010,
  209. Mwcache = 0x0020,
  210. Mlookahead = 0x0040,
  211. Mrelirq = 0x0080,
  212. Msvcirq = 0x0100,
  213. Mreset = 0x0200,
  214. Mprotected = 0x0400,
  215. Mwbuf = 0x1000,
  216. Mrbuf = 0x2000,
  217. Mnop = 0x4000,
  218. Mmicrocode = 0x0001,
  219. Mqueued = 0x0002,
  220. Mcfa = 0x0004,
  221. Mapm = 0x0008,
  222. Mnotify = 0x0010,
  223. Mstandby = 0x0020,
  224. Mspinup = 0x0040,
  225. Mmaxsec = 0x0100,
  226. Mautoacoustic = 0x0200,
  227. Maddr48 = 0x0400,
  228. Mdevconfov = 0x0800,
  229. Mflush = 0x1000,
  230. Mflush48 = 0x2000,
  231. Msmarterror = 0x0001,
  232. Msmartselftest = 0x0002,
  233. Mmserial = 0x0004,
  234. Mmpassthru = 0x0008,
  235. Mlogging = 0x0020,
  236. };
  237. typedef struct Ctlr Ctlr;
  238. typedef struct Drive Drive;
  239. typedef struct Prd {
  240. ulong pa; /* Physical Base Address */
  241. int count;
  242. } Prd;
  243. enum {
  244. Nprd = SDmaxio/(64*1024)+2,
  245. };
  246. typedef struct Ctlr {
  247. int cmdport;
  248. int ctlport;
  249. int irq;
  250. int tbdf;
  251. int bmiba; /* bus master interface base address */
  252. Pcidev* pcidev;
  253. void (*ienable)(Ctlr*);
  254. void (*idisable)(Ctlr*);
  255. SDev* sdev;
  256. Drive* drive[2];
  257. Prd* prdt; /* physical region descriptor table */
  258. void* prdtbase;
  259. QLock; /* current command */
  260. Drive* curdrive;
  261. int command; /* last command issued (debugging) */
  262. Rendez;
  263. int done;
  264. Lock; /* register access */
  265. } Ctlr;
  266. typedef struct Drive {
  267. Ctlr* ctlr;
  268. int dev;
  269. ushort info[256];
  270. int c; /* cylinder */
  271. int h; /* head */
  272. int s; /* sector */
  273. vlong sectors; /* total */
  274. int secsize; /* sector size */
  275. int dma; /* DMA R/W possible */
  276. int dmactl;
  277. int rwm; /* read/write multiple possible */
  278. int rwmctl;
  279. int pkt; /* PACKET device, length of pktcmd */
  280. uchar pktcmd[16];
  281. int pktdma; /* this PACKET command using dma */
  282. uchar sense[18];
  283. uchar inquiry[48];
  284. QLock; /* drive access */
  285. int command; /* current command */
  286. int write;
  287. uchar* data;
  288. int dlen;
  289. uchar* limit;
  290. int count; /* sectors */
  291. int block; /* R/W bytes per block */
  292. int status;
  293. int error;
  294. int flags; /* internal flags */
  295. } Drive;
  296. static void
  297. pc87415ienable(Ctlr* ctlr)
  298. {
  299. Pcidev *p;
  300. int x;
  301. p = ctlr->pcidev;
  302. if(p == nil)
  303. return;
  304. x = pcicfgr32(p, 0x40);
  305. if(ctlr->cmdport == p->mem[0].bar)
  306. x &= ~0x00000100;
  307. else
  308. x &= ~0x00000200;
  309. pcicfgw32(p, 0x40, x);
  310. }
  311. static void
  312. atadumpstate(Drive* drive, uchar* cmd, vlong lba, int count)
  313. {
  314. Prd *prd;
  315. Pcidev *p;
  316. Ctlr *ctlr;
  317. int i, bmiba;
  318. if(!(DEBUG & DbgSTATE)){
  319. USED(drive, cmd, lba, count);
  320. return;
  321. }
  322. ctlr = drive->ctlr;
  323. print("command %2.2uX\n", ctlr->command);
  324. print("data %8.8p limit %8.8p dlen %d status %uX error %uX\n",
  325. drive->data, drive->limit, drive->dlen,
  326. drive->status, drive->error);
  327. if(cmd != nil){
  328. print("lba %d -> %lld, count %d -> %d (%d)\n",
  329. (cmd[2]<<24)|(cmd[3]<<16)|(cmd[4]<<8)|cmd[5], lba,
  330. (cmd[7]<<8)|cmd[8], count, drive->count);
  331. }
  332. if(!(inb(ctlr->ctlport+As) & Bsy)){
  333. for(i = 1; i < 7; i++)
  334. print(" 0x%2.2uX", inb(ctlr->cmdport+i));
  335. print(" 0x%2.2uX\n", inb(ctlr->ctlport+As));
  336. }
  337. if(drive->command == Cwd || drive->command == Crd){
  338. bmiba = ctlr->bmiba;
  339. prd = ctlr->prdt;
  340. print("bmicx %2.2uX bmisx %2.2uX prdt %8.8p\n",
  341. inb(bmiba+Bmicx), inb(bmiba+Bmisx), prd);
  342. for(;;){
  343. print("pa 0x%8.8luX count %8.8uX\n",
  344. prd->pa, prd->count);
  345. if(prd->count & PrdEOT)
  346. break;
  347. prd++;
  348. }
  349. }
  350. if(ctlr->pcidev && ctlr->pcidev->vid == 0x8086){
  351. p = ctlr->pcidev;
  352. print("0x40: %4.4uX 0x42: %4.4uX",
  353. pcicfgr16(p, 0x40), pcicfgr16(p, 0x42));
  354. print("0x48: %2.2uX\n", pcicfgr8(p, 0x48));
  355. print("0x4A: %4.4uX\n", pcicfgr16(p, 0x4A));
  356. }
  357. }
  358. static int
  359. atadebug(int cmdport, int ctlport, char* fmt, ...)
  360. {
  361. int i, n;
  362. va_list arg;
  363. char buf[PRINTSIZE];
  364. if(!(DEBUG & DbgPROBE)){
  365. USED(cmdport, ctlport, fmt);
  366. return 0;
  367. }
  368. va_start(arg, fmt);
  369. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  370. va_end(arg);
  371. if(cmdport){
  372. if(buf[n-1] == '\n')
  373. n--;
  374. n += snprint(buf+n, PRINTSIZE-n, " ataregs 0x%uX:",
  375. cmdport);
  376. for(i = Features; i < Command; i++)
  377. n += snprint(buf+n, PRINTSIZE-n, " 0x%2.2uX",
  378. inb(cmdport+i));
  379. if(ctlport)
  380. n += snprint(buf+n, PRINTSIZE-n, " 0x%2.2uX",
  381. inb(ctlport+As));
  382. n += snprint(buf+n, PRINTSIZE-n, "\n");
  383. }
  384. putstrn(buf, n);
  385. return n;
  386. }
  387. static int
  388. ataready(int cmdport, int ctlport, int dev, int reset, int ready, int micro)
  389. {
  390. int as;
  391. atadebug(cmdport, ctlport, "ataready: dev %uX reset %uX ready %uX",
  392. dev, reset, ready);
  393. for(;;){
  394. /*
  395. * Wait for the controller to become not busy and
  396. * possibly for a status bit to become true (usually
  397. * Drdy). Must change to the appropriate device
  398. * register set if necessary before testing for ready.
  399. * Always run through the loop at least once so it
  400. * can be used as a test for !Bsy.
  401. */
  402. as = inb(ctlport+As);
  403. if(as & reset){
  404. /* nothing to do */
  405. }
  406. else if(dev){
  407. outb(cmdport+Dh, dev);
  408. dev = 0;
  409. }
  410. else if(ready == 0 || (as & ready)){
  411. atadebug(0, 0, "ataready: %d 0x%2.2uX\n", micro, as);
  412. return as;
  413. }
  414. if(micro-- <= 0){
  415. atadebug(0, 0, "ataready: %d 0x%2.2uX\n", micro, as);
  416. break;
  417. }
  418. microdelay(1);
  419. }
  420. atadebug(cmdport, ctlport, "ataready: timeout");
  421. return -1;
  422. }
  423. /*
  424. static int
  425. atacsf(Drive* drive, vlong csf, int supported)
  426. {
  427. ushort *info;
  428. int cmdset, i, x;
  429. if(supported)
  430. info = &drive->info[Icsfs];
  431. else
  432. info = &drive->info[Icsfe];
  433. for(i = 0; i < 3; i++){
  434. x = (csf>>(16*i)) & 0xFFFF;
  435. if(x == 0)
  436. continue;
  437. cmdset = info[i];
  438. if(cmdset == 0 || cmdset == 0xFFFF)
  439. return 0;
  440. return cmdset & x;
  441. }
  442. return 0;
  443. }
  444. */
  445. static int
  446. atadone(void* arg)
  447. {
  448. return ((Ctlr*)arg)->done;
  449. }
  450. static int
  451. atarwmmode(Drive* drive, int cmdport, int ctlport, int dev)
  452. {
  453. int as, maxrwm, rwm;
  454. maxrwm = (drive->info[Imaxrwm] & 0xFF);
  455. if(maxrwm == 0)
  456. return 0;
  457. /*
  458. * Sometimes drives come up with the current count set
  459. * to 0; if so, set a suitable value, otherwise believe
  460. * the value in Irwm if the 0x100 bit is set.
  461. */
  462. if(drive->info[Irwm] & 0x100)
  463. rwm = (drive->info[Irwm] & 0xFF);
  464. else
  465. rwm = 0;
  466. if(rwm == 0)
  467. rwm = maxrwm;
  468. if(rwm > 16)
  469. rwm = 16;
  470. if(ataready(cmdport, ctlport, dev, Bsy|Drq, Drdy, 102*1000) < 0)
  471. return 0;
  472. outb(cmdport+Count, rwm);
  473. outb(cmdport+Command, Csm);
  474. microdelay(1);
  475. as = ataready(cmdport, ctlport, 0, Bsy, Drdy|Df|Err, 1000);
  476. inb(cmdport+Status);
  477. if(as < 0 || (as & (Df|Err)))
  478. return 0;
  479. drive->rwm = rwm;
  480. return rwm;
  481. }
  482. static int
  483. atadmamode(Drive* drive)
  484. {
  485. int dma;
  486. /*
  487. * Check if any DMA mode enabled.
  488. * Assumes the BIOS has picked and enabled the best.
  489. * This is completely passive at the moment, no attempt is
  490. * made to ensure the hardware is correctly set up.
  491. */
  492. dma = drive->info[Imwdma] & 0x0707;
  493. drive->dma = (dma>>8) & dma;
  494. if(drive->dma == 0 && (drive->info[Ivalid] & 0x04)){
  495. dma = drive->info[Iudma] & 0x7F7F;
  496. drive->dma = (dma>>8) & dma;
  497. if(drive->dma)
  498. drive->dma |= 'U'<<16;
  499. }
  500. return dma;
  501. }
  502. static int
  503. ataidentify(int cmdport, int ctlport, int dev, int pkt, void* info)
  504. {
  505. int as, command, drdy;
  506. if(pkt){
  507. command = Cidpkt;
  508. drdy = 0;
  509. }
  510. else{
  511. command = Cid;
  512. drdy = Drdy;
  513. }
  514. as = ataready(cmdport, ctlport, dev, Bsy|Drq, drdy, 103*1000);
  515. if(as < 0)
  516. return as;
  517. outb(cmdport+Command, command);
  518. microdelay(1);
  519. as = ataready(cmdport, ctlport, 0, Bsy, Drq|Err, 400*1000);
  520. if(as < 0)
  521. return -1;
  522. if(as & Err)
  523. return as;
  524. memset(info, 0, 512);
  525. inss(cmdport+Data, info, 256);
  526. inb(cmdport+Status);
  527. if(DEBUG & DbgIDENTIFY){
  528. int i;
  529. ushort *sp;
  530. sp = (ushort*)info;
  531. for(i = 0; i < 256; i++){
  532. if(i && (i%16) == 0)
  533. print("\n");
  534. print(" %4.4uX", *sp);
  535. sp++;
  536. }
  537. print("\n");
  538. }
  539. return 0;
  540. }
  541. static Drive*
  542. atadrive(int cmdport, int ctlport, int dev)
  543. {
  544. Drive *drive;
  545. int as, i, pkt;
  546. uchar buf[512], *p;
  547. ushort iconfig, *sp;
  548. atadebug(0, 0, "identify: port 0x%uX dev 0x%2.2uX\n", cmdport, dev);
  549. pkt = 1;
  550. retry:
  551. as = ataidentify(cmdport, ctlport, dev, pkt, buf);
  552. if(as < 0)
  553. return nil;
  554. if(as & Err){
  555. if(pkt == 0)
  556. return nil;
  557. pkt = 0;
  558. goto retry;
  559. }
  560. if((drive = malloc(sizeof(Drive))) == nil)
  561. return nil;
  562. drive->dev = dev;
  563. memmove(drive->info, buf, sizeof(drive->info));
  564. drive->sense[0] = 0x70;
  565. drive->sense[7] = sizeof(drive->sense)-7;
  566. drive->inquiry[2] = 2;
  567. drive->inquiry[3] = 2;
  568. drive->inquiry[4] = sizeof(drive->inquiry)-4;
  569. p = &drive->inquiry[8];
  570. sp = &drive->info[Imodel];
  571. for(i = 0; i < 20; i++){
  572. *p++ = *sp>>8;
  573. *p++ = *sp++;
  574. }
  575. drive->secsize = 512;
  576. /*
  577. * Beware the CompactFlash Association feature set.
  578. * Now, why this value in Iconfig just walks all over the bit
  579. * definitions used in the other parts of the ATA/ATAPI standards
  580. * is a mystery and a sign of true stupidity on someone's part.
  581. * Anyway, the standard says if this value is 0x848A then it's
  582. * CompactFlash and it's NOT a packet device.
  583. */
  584. iconfig = drive->info[Iconfig];
  585. if(iconfig != 0x848A && (iconfig & 0xC000) == 0x8000){
  586. if(iconfig & 0x01)
  587. drive->pkt = 16;
  588. else
  589. drive->pkt = 12;
  590. }
  591. else{
  592. if(drive->info[Ivalid] & 0x0001){
  593. drive->c = drive->info[Iccyl];
  594. drive->h = drive->info[Ichead];
  595. drive->s = drive->info[Icsec];
  596. }else{
  597. drive->c = drive->info[Ilcyl];
  598. drive->h = drive->info[Ilhead];
  599. drive->s = drive->info[Ilsec];
  600. }
  601. if(drive->info[Icapabilities] & Mlba){
  602. if(drive->info[Icsfs+1] & Maddr48){
  603. drive->sectors = drive->info[Ilba48]
  604. | (drive->info[Ilba48+1]<<16)
  605. | ((vlong)drive->info[Ilba48+2]<<32);
  606. drive->flags |= Lba48;
  607. }else{
  608. drive->sectors = (drive->info[Ilba+1]<<16)
  609. |drive->info[Ilba];
  610. }
  611. drive->dev |= Lba;
  612. }else
  613. drive->sectors = drive->c*drive->h*drive->s;
  614. atarwmmode(drive, cmdport, ctlport, dev);
  615. }
  616. atadmamode(drive);
  617. if(DEBUG & DbgCONFIG){
  618. print("dev %2.2uX port %uX config %4.4uX capabilities %4.4uX",
  619. dev, cmdport, 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 & DbgBsy)
  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. * Sub-class code 0x04 is 'RAID controller', e.g. VIA VT8237.
  1631. */
  1632. if(p->ccrb != 0x01)
  1633. continue;
  1634. if(p->ccru != 0x01 && p->ccru != 0x04 && p->ccru != 0x80)
  1635. continue;
  1636. pi = p->ccrp;
  1637. ispc87415 = 0;
  1638. switch((p->did<<16)|p->vid){
  1639. default:
  1640. continue;
  1641. case (0x0002<<16)|0x100B: /* NS PC87415 */
  1642. /*
  1643. * Disable interrupts on both channels until
  1644. * after they are probed for drives.
  1645. * This must be called before interrupts are
  1646. * enabled because the IRQ may be shared.
  1647. */
  1648. ispc87415 = 1;
  1649. pcicfgw32(p, 0x40, 0x00000300);
  1650. break;
  1651. case (0x1000<<16)|0x1042: /* PC-Tech RZ1000 */
  1652. /*
  1653. * Turn off prefetch. Overkill, but cheap.
  1654. */
  1655. r = pcicfgr32(p, 0x40);
  1656. r &= ~0x2000;
  1657. pcicfgw32(p, 0x40, r);
  1658. break;
  1659. case (0x4D38<<16)|0x105A: /* Promise PDC20262 */
  1660. case (0x4D30<<16)|0x105A: /* Promise PDC202xx */
  1661. case (0x4D68<<16)|0x105A: /* Promise PDC20268 */
  1662. case (0x3373<<16)|0x105A: /* Promise 20378 RAID */
  1663. case (0x3149<<16)|0x1106: /* VIA VT8237 SATA/RAID */
  1664. pi = 0x85;
  1665. break;
  1666. case (0x0004<<16)|0x1103: /* HighPoint HPT-370 */
  1667. pi = 0x85;
  1668. /*
  1669. * Turn off fast interrupt prediction.
  1670. */
  1671. if((r = pcicfgr8(p, 0x51)) & 0x80)
  1672. pcicfgw8(p, 0x51, r & ~0x80);
  1673. if((r = pcicfgr8(p, 0x55)) & 0x80)
  1674. pcicfgw8(p, 0x55, r & ~0x80);
  1675. break;
  1676. case (0x0640<<16)|0x1095: /* CMD 640B */
  1677. /*
  1678. * Bugfix code here...
  1679. */
  1680. break;
  1681. case (0x7441<<16)|0x1022: /* AMD 768 */
  1682. /*
  1683. * Set:
  1684. * 0x41 prefetch, postwrite;
  1685. * 0x43 FIFO configuration 1/2 and 1/2;
  1686. * 0x44 status register read retry;
  1687. * 0x46 DMA read and end of sector flush.
  1688. */
  1689. r = pcicfgr8(p, 0x41);
  1690. pcicfgw8(p, 0x41, r|0xF0);
  1691. r = pcicfgr8(p, 0x43);
  1692. pcicfgw8(p, 0x43, (r & 0x90)|0x2A);
  1693. r = pcicfgr8(p, 0x44);
  1694. pcicfgw8(p, 0x44, r|0x08);
  1695. r = pcicfgr8(p, 0x46);
  1696. pcicfgw8(p, 0x46, (r & 0x0C)|0xF0);
  1697. break;
  1698. case (0x0646<<16)|0x1095: /* CMD 646 */
  1699. case (0x0571<<16)|0x1106: /* VIA 82C686 */
  1700. case (0x0211<<16)|0x1166: /* ServerWorks IB6566 */
  1701. case (0x1230<<16)|0x8086: /* 82371FB (PIIX) */
  1702. case (0x7010<<16)|0x8086: /* 82371SB (PIIX3) */
  1703. case (0x7111<<16)|0x8086: /* 82371[AE]B (PIIX4[E]) */
  1704. case (0x2411<<16)|0x8086: /* 82801AA (ICH) */
  1705. case (0x2421<<16)|0x8086: /* 82801AB (ICH0) */
  1706. case (0x244A<<16)|0x8086: /* 82801BA (ICH2, Mobile) */
  1707. case (0x244B<<16)|0x8086: /* 82801BA (ICH2, High-End) */
  1708. case (0x248A<<16)|0x8086: /* 82801CA (ICH3, Mobile) */
  1709. case (0x248B<<16)|0x8086: /* 82801CA (ICH3, High-End) */
  1710. case (0x24CA<<16)|0x8086: /* 82801DBM (ICH4, Mobile) */
  1711. case (0x24CB<<16)|0x8086: /* 82801DB (ICH4, High-End) */
  1712. case (0x24DB<<16)|0x8086: /* 82801EB (ICH5) */
  1713. break;
  1714. }
  1715. for(channel = 0; channel < 2; channel++){
  1716. if(pi & (1<<(2*channel))){
  1717. sdev = ataprobe(p->mem[0+2*channel].bar & ~0x01,
  1718. p->mem[1+2*channel].bar & ~0x01,
  1719. p->intl);
  1720. if(sdev == nil)
  1721. continue;
  1722. ctlr = sdev->ctlr;
  1723. if(ispc87415) {
  1724. ctlr->ienable = pc87415ienable;
  1725. print("pc87415disable: not yet implemented\n");
  1726. }
  1727. if(head != nil)
  1728. tail->next = sdev;
  1729. else
  1730. head = sdev;
  1731. tail = sdev;
  1732. ctlr->tbdf = p->tbdf;
  1733. }
  1734. else if((sdev = legacy[channel]) == nil)
  1735. continue;
  1736. else
  1737. ctlr = sdev->ctlr;
  1738. ctlr->pcidev = p;
  1739. if(!(pi & 0x80))
  1740. continue;
  1741. ctlr->bmiba = (p->mem[4].bar & ~0x01) + channel*8;
  1742. }
  1743. }
  1744. if(0){
  1745. int port;
  1746. ISAConf isa;
  1747. /*
  1748. * Hack for PCMCIA drives.
  1749. * This will be tidied once we figure out how the whole
  1750. * removeable device thing is going to work.
  1751. */
  1752. memset(&isa, 0, sizeof(isa));
  1753. isa.port = 0x180; /* change this for your machine */
  1754. isa.irq = 11; /* change this for your machine */
  1755. port = isa.port+0x0C;
  1756. channel = pcmspecial("MK2001MPL", &isa);
  1757. if(channel == -1)
  1758. channel = pcmspecial("SunDisk", &isa);
  1759. if(channel == -1){
  1760. isa.irq = 10;
  1761. channel = pcmspecial("CF", &isa);
  1762. }
  1763. if(channel == -1){
  1764. isa.irq = 10;
  1765. channel = pcmspecial("OLYMPUS", &isa);
  1766. }
  1767. if(channel == -1){
  1768. port = isa.port+0x204;
  1769. channel = pcmspecial("ATA/ATAPI", &isa);
  1770. }
  1771. if(channel >= 0 && (sdev = ataprobe(isa.port, port, isa.irq)) != nil){
  1772. if(head != nil)
  1773. tail->next = sdev;
  1774. else
  1775. head = sdev;
  1776. }
  1777. }
  1778. return head;
  1779. }
  1780. static SDev*
  1781. atalegacy(int port, int irq)
  1782. {
  1783. return ataprobe(port, port+0x204, irq);
  1784. }
  1785. static SDev*
  1786. ataid(SDev* sdev)
  1787. {
  1788. int i;
  1789. Ctlr *ctlr;
  1790. char name[32];
  1791. /*
  1792. * Legacy controllers are always 'C' and 'D' and if
  1793. * they exist and have drives will be first in the list.
  1794. * If there are no active legacy controllers, native
  1795. * controllers start at 'C'.
  1796. */
  1797. if(sdev == nil)
  1798. return nil;
  1799. ctlr = sdev->ctlr;
  1800. if(ctlr->cmdport == 0x1F0 || ctlr->cmdport == 0x170)
  1801. i = 2;
  1802. else
  1803. i = 0;
  1804. while(sdev){
  1805. if(sdev->ifc == &sdataifc){
  1806. ctlr = sdev->ctlr;
  1807. if(ctlr->cmdport == 0x1F0)
  1808. sdev->idno = 'C';
  1809. else if(ctlr->cmdport == 0x170)
  1810. sdev->idno = 'D';
  1811. else{
  1812. sdev->idno = 'C'+i;
  1813. i++;
  1814. }
  1815. snprint(name, sizeof(name), "sd%c", sdev->idno);
  1816. kstrdup(&sdev->name, name);
  1817. }
  1818. sdev = sdev->next;
  1819. }
  1820. return nil;
  1821. }
  1822. static int
  1823. ataenable(SDev* sdev)
  1824. {
  1825. Ctlr *ctlr;
  1826. char name[32];
  1827. ctlr = sdev->ctlr;
  1828. if(ctlr->bmiba){
  1829. #define ALIGN (4 * 1024)
  1830. if(ctlr->pcidev != nil)
  1831. pcisetbme(ctlr->pcidev);
  1832. // ctlr->prdt = xspanalloc(Nprd*sizeof(Prd), 4, 4*1024);
  1833. ctlr->prdtbase = xalloc(Nprd * sizeof(Prd) + ALIGN);
  1834. ctlr->prdt = (Prd *)(((ulong)ctlr->prdtbase + ALIGN) & ~(ALIGN - 1));
  1835. }
  1836. snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
  1837. intrenable(ctlr->irq, atainterrupt, ctlr, ctlr->tbdf, name);
  1838. outb(ctlr->ctlport+Dc, 0);
  1839. if(ctlr->ienable)
  1840. ctlr->ienable(ctlr);
  1841. return 1;
  1842. }
  1843. static int
  1844. atadisable(SDev *sdev)
  1845. {
  1846. Ctlr *ctlr;
  1847. char name[32];
  1848. ctlr = sdev->ctlr;
  1849. outb(ctlr->ctlport+Dc, Nien); /* disable interrupts */
  1850. if (ctlr->idisable)
  1851. ctlr->idisable(ctlr);
  1852. snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
  1853. intrdisable(ctlr->irq, atainterrupt, ctlr, ctlr->tbdf, name);
  1854. if (ctlr->bmiba) {
  1855. if (ctlr->pcidev)
  1856. pciclrbme(ctlr->pcidev);
  1857. xfree(ctlr->prdtbase);
  1858. }
  1859. return 0;
  1860. }
  1861. static int
  1862. atarctl(SDunit* unit, char* p, int l)
  1863. {
  1864. int n;
  1865. Ctlr *ctlr;
  1866. Drive *drive;
  1867. if((ctlr = unit->dev->ctlr) == nil || ctlr->drive[unit->subno] == nil)
  1868. return 0;
  1869. drive = ctlr->drive[unit->subno];
  1870. qlock(drive);
  1871. n = snprint(p, l, "config %4.4uX capabilities %4.4uX",
  1872. drive->info[Iconfig], drive->info[Icapabilities]);
  1873. if(drive->dma)
  1874. n += snprint(p+n, l-n, " dma %8.8uX dmactl %8.8uX",
  1875. drive->dma, drive->dmactl);
  1876. if(drive->rwm)
  1877. n += snprint(p+n, l-n, " rwm %ud rwmctl %ud",
  1878. drive->rwm, drive->rwmctl);
  1879. if(drive->flags&Lba48)
  1880. n += snprint(p+n, l-n, " lba48always %s",
  1881. (drive->flags&Lba48always) ? "on" : "off");
  1882. n += snprint(p+n, l-n, "\n");
  1883. if(drive->sectors){
  1884. n += snprint(p+n, l-n, "geometry %lld %d",
  1885. drive->sectors, drive->secsize);
  1886. if(drive->pkt == 0)
  1887. n += snprint(p+n, l-n, " %d %d %d",
  1888. drive->c, drive->h, drive->s);
  1889. n += snprint(p+n, l-n, "\n");
  1890. }
  1891. qunlock(drive);
  1892. return n;
  1893. }
  1894. static int
  1895. atawctl(SDunit* unit, Cmdbuf* cb)
  1896. {
  1897. int period;
  1898. Ctlr *ctlr;
  1899. Drive *drive;
  1900. if((ctlr = unit->dev->ctlr) == nil || ctlr->drive[unit->subno] == nil)
  1901. return 0;
  1902. drive = ctlr->drive[unit->subno];
  1903. qlock(drive);
  1904. if(waserror()){
  1905. qunlock(drive);
  1906. nexterror();
  1907. }
  1908. /*
  1909. * Dma and rwm control is passive at the moment,
  1910. * i.e. it is assumed that the hardware is set up
  1911. * correctly already either by the BIOS or when
  1912. * the drive was initially identified.
  1913. */
  1914. if(strcmp(cb->f[0], "dma") == 0){
  1915. if(cb->nf != 2 || drive->dma == 0)
  1916. error(Ebadctl);
  1917. if(strcmp(cb->f[1], "on") == 0)
  1918. drive->dmactl = drive->dma;
  1919. else if(strcmp(cb->f[1], "off") == 0)
  1920. drive->dmactl = 0;
  1921. else
  1922. error(Ebadctl);
  1923. }
  1924. else if(strcmp(cb->f[0], "rwm") == 0){
  1925. if(cb->nf != 2 || drive->rwm == 0)
  1926. error(Ebadctl);
  1927. if(strcmp(cb->f[1], "on") == 0)
  1928. drive->rwmctl = drive->rwm;
  1929. else if(strcmp(cb->f[1], "off") == 0)
  1930. drive->rwmctl = 0;
  1931. else
  1932. error(Ebadctl);
  1933. }
  1934. else if(strcmp(cb->f[0], "standby") == 0){
  1935. switch(cb->nf){
  1936. default:
  1937. error(Ebadctl);
  1938. case 2:
  1939. period = strtol(cb->f[1], 0, 0);
  1940. if(period && (period < 30 || period > 240*5))
  1941. error(Ebadctl);
  1942. period /= 5;
  1943. break;
  1944. }
  1945. if(atastandby(drive, period) != SDok)
  1946. error(Ebadctl);
  1947. }
  1948. else if(strcmp(cb->f[0], "lba48always") == 0){
  1949. if(cb->nf != 2 || !(drive->flags&Lba48))
  1950. error(Ebadctl);
  1951. if(strcmp(cb->f[1], "on") == 0)
  1952. drive->flags |= Lba48always;
  1953. else if(strcmp(cb->f[1], "off") == 0)
  1954. drive->flags &= ~Lba48always;
  1955. else
  1956. error(Ebadctl);
  1957. }
  1958. else
  1959. error(Ebadctl);
  1960. qunlock(drive);
  1961. poperror();
  1962. return 0;
  1963. }
  1964. SDifc sdataifc = {
  1965. "ata", /* name */
  1966. atapnp, /* pnp */
  1967. atalegacy, /* legacy */
  1968. ataid, /* id */
  1969. ataenable, /* enable */
  1970. atadisable, /* disable */
  1971. scsiverify, /* verify */
  1972. scsionline, /* online */
  1973. atario, /* rio */
  1974. atarctl, /* rctl */
  1975. atawctl, /* wctl */
  1976. scsibio, /* bio */
  1977. ataprobew, /* probe */
  1978. ataclear, /* clear */
  1979. atastat, /* stat */
  1980. };