sdata.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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 (0x24CA<<16)|0x8086: /* 82801DBM (ICH4, Mobile) */
  1706. case (0x24CB<<16)|0x8086: /* 82801DB (ICH4, High-End) */
  1707. case (0x24DB<<16)|0x8086: /* 82801EB (ICH5) */
  1708. break;
  1709. }
  1710. for(channel = 0; channel < 2; channel++){
  1711. if(pi & (1<<(2*channel))){
  1712. sdev = ataprobe(p->mem[0+2*channel].bar & ~0x01,
  1713. p->mem[1+2*channel].bar & ~0x01,
  1714. p->intl);
  1715. if(sdev == nil)
  1716. continue;
  1717. ctlr = sdev->ctlr;
  1718. if(ispc87415) {
  1719. ctlr->ienable = pc87415ienable;
  1720. print("pc87415disable: not yet implemented\n");
  1721. }
  1722. if(head != nil)
  1723. tail->next = sdev;
  1724. else
  1725. head = sdev;
  1726. tail = sdev;
  1727. ctlr->tbdf = p->tbdf;
  1728. }
  1729. else if((sdev = legacy[channel]) == nil)
  1730. continue;
  1731. else
  1732. ctlr = sdev->ctlr;
  1733. ctlr->pcidev = p;
  1734. if(!(pi & 0x80))
  1735. continue;
  1736. ctlr->bmiba = (p->mem[4].bar & ~0x01) + channel*8;
  1737. }
  1738. }
  1739. if(0){
  1740. int port;
  1741. ISAConf isa;
  1742. /*
  1743. * Hack for PCMCIA drives.
  1744. * This will be tidied once we figure out how the whole
  1745. * removeable device thing is going to work.
  1746. */
  1747. memset(&isa, 0, sizeof(isa));
  1748. isa.port = 0x180; /* change this for your machine */
  1749. isa.irq = 11; /* change this for your machine */
  1750. port = isa.port+0x0C;
  1751. channel = pcmspecial("MK2001MPL", &isa);
  1752. if(channel == -1)
  1753. channel = pcmspecial("SunDisk", &isa);
  1754. if(channel == -1){
  1755. isa.irq = 10;
  1756. channel = pcmspecial("CF", &isa);
  1757. }
  1758. if(channel == -1){
  1759. isa.irq = 10;
  1760. channel = pcmspecial("OLYMPUS", &isa);
  1761. }
  1762. if(channel == -1){
  1763. port = isa.port+0x204;
  1764. channel = pcmspecial("ATA/ATAPI", &isa);
  1765. }
  1766. if(channel >= 0 && (sdev = ataprobe(isa.port, port, isa.irq)) != nil){
  1767. if(head != nil)
  1768. tail->next = sdev;
  1769. else
  1770. head = sdev;
  1771. }
  1772. }
  1773. return head;
  1774. }
  1775. static SDev*
  1776. atalegacy(int port, int irq)
  1777. {
  1778. return ataprobe(port, port+0x204, irq);
  1779. }
  1780. static SDev*
  1781. ataid(SDev* sdev)
  1782. {
  1783. int i;
  1784. Ctlr *ctlr;
  1785. char name[32];
  1786. /*
  1787. * Legacy controllers are always 'C' and 'D' and if
  1788. * they exist and have drives will be first in the list.
  1789. * If there are no active legacy controllers, native
  1790. * controllers start at 'C'.
  1791. */
  1792. if(sdev == nil)
  1793. return nil;
  1794. ctlr = sdev->ctlr;
  1795. if(ctlr->cmdport == 0x1F0 || ctlr->cmdport == 0x170)
  1796. i = 2;
  1797. else
  1798. i = 0;
  1799. while(sdev){
  1800. if(sdev->ifc == &sdataifc){
  1801. ctlr = sdev->ctlr;
  1802. if(ctlr->cmdport == 0x1F0)
  1803. sdev->idno = 'C';
  1804. else if(ctlr->cmdport == 0x170)
  1805. sdev->idno = 'D';
  1806. else{
  1807. sdev->idno = 'C'+i;
  1808. i++;
  1809. }
  1810. snprint(name, sizeof(name), "sd%c", sdev->idno);
  1811. kstrdup(&sdev->name, name);
  1812. }
  1813. sdev = sdev->next;
  1814. }
  1815. return nil;
  1816. }
  1817. static int
  1818. ataenable(SDev* sdev)
  1819. {
  1820. Ctlr *ctlr;
  1821. char name[32];
  1822. ctlr = sdev->ctlr;
  1823. if(ctlr->bmiba){
  1824. #define ALIGN (4 * 1024)
  1825. if(ctlr->pcidev != nil)
  1826. pcisetbme(ctlr->pcidev);
  1827. // ctlr->prdt = xspanalloc(Nprd*sizeof(Prd), 4, 4*1024);
  1828. ctlr->prdtbase = xalloc(Nprd * sizeof(Prd) + ALIGN);
  1829. ctlr->prdt = (Prd *)(((ulong)ctlr->prdtbase + ALIGN) & ~(ALIGN - 1));
  1830. }
  1831. snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
  1832. intrenable(ctlr->irq, atainterrupt, ctlr, ctlr->tbdf, name);
  1833. outb(ctlr->ctlport+Dc, 0);
  1834. if(ctlr->ienable)
  1835. ctlr->ienable(ctlr);
  1836. return 1;
  1837. }
  1838. static int
  1839. atadisable(SDev *sdev)
  1840. {
  1841. Ctlr *ctlr;
  1842. char name[32];
  1843. ctlr = sdev->ctlr;
  1844. outb(ctlr->ctlport+Dc, Nien); /* disable interrupts */
  1845. if (ctlr->idisable)
  1846. ctlr->idisable(ctlr);
  1847. snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
  1848. intrdisable(ctlr->irq, atainterrupt, ctlr, ctlr->tbdf, name);
  1849. if (ctlr->bmiba) {
  1850. if (ctlr->pcidev)
  1851. pciclrbme(ctlr->pcidev);
  1852. xfree(ctlr->prdtbase);
  1853. }
  1854. return 0;
  1855. }
  1856. static int
  1857. atarctl(SDunit* unit, char* p, int l)
  1858. {
  1859. int n;
  1860. Ctlr *ctlr;
  1861. Drive *drive;
  1862. if((ctlr = unit->dev->ctlr) == nil || ctlr->drive[unit->subno] == nil)
  1863. return 0;
  1864. drive = ctlr->drive[unit->subno];
  1865. qlock(drive);
  1866. n = snprint(p, l, "config %4.4uX capabilities %4.4uX",
  1867. drive->info[Iconfig], drive->info[Icapabilities]);
  1868. if(drive->dma)
  1869. n += snprint(p+n, l-n, " dma %8.8uX dmactl %8.8uX",
  1870. drive->dma, drive->dmactl);
  1871. if(drive->rwm)
  1872. n += snprint(p+n, l-n, " rwm %ud rwmctl %ud",
  1873. drive->rwm, drive->rwmctl);
  1874. if(drive->flags&Lba48)
  1875. n += snprint(p+n, l-n, " lba48always %s",
  1876. (drive->flags&Lba48always) ? "on" : "off");
  1877. n += snprint(p+n, l-n, "\n");
  1878. if(drive->sectors){
  1879. n += snprint(p+n, l-n, "geometry %lld %d",
  1880. drive->sectors, drive->secsize);
  1881. if(drive->pkt == 0)
  1882. n += snprint(p+n, l-n, " %d %d %d",
  1883. drive->c, drive->h, drive->s);
  1884. n += snprint(p+n, l-n, "\n");
  1885. }
  1886. qunlock(drive);
  1887. return n;
  1888. }
  1889. static int
  1890. atawctl(SDunit* unit, Cmdbuf* cb)
  1891. {
  1892. int period;
  1893. Ctlr *ctlr;
  1894. Drive *drive;
  1895. if((ctlr = unit->dev->ctlr) == nil || ctlr->drive[unit->subno] == nil)
  1896. return 0;
  1897. drive = ctlr->drive[unit->subno];
  1898. qlock(drive);
  1899. if(waserror()){
  1900. qunlock(drive);
  1901. nexterror();
  1902. }
  1903. /*
  1904. * Dma and rwm control is passive at the moment,
  1905. * i.e. it is assumed that the hardware is set up
  1906. * correctly already either by the BIOS or when
  1907. * the drive was initially identified.
  1908. */
  1909. if(strcmp(cb->f[0], "dma") == 0){
  1910. if(cb->nf != 2 || drive->dma == 0)
  1911. error(Ebadctl);
  1912. if(strcmp(cb->f[1], "on") == 0)
  1913. drive->dmactl = drive->dma;
  1914. else if(strcmp(cb->f[1], "off") == 0)
  1915. drive->dmactl = 0;
  1916. else
  1917. error(Ebadctl);
  1918. }
  1919. else if(strcmp(cb->f[0], "rwm") == 0){
  1920. if(cb->nf != 2 || drive->rwm == 0)
  1921. error(Ebadctl);
  1922. if(strcmp(cb->f[1], "on") == 0)
  1923. drive->rwmctl = drive->rwm;
  1924. else if(strcmp(cb->f[1], "off") == 0)
  1925. drive->rwmctl = 0;
  1926. else
  1927. error(Ebadctl);
  1928. }
  1929. else if(strcmp(cb->f[0], "standby") == 0){
  1930. switch(cb->nf){
  1931. default:
  1932. error(Ebadctl);
  1933. case 2:
  1934. period = strtol(cb->f[1], 0, 0);
  1935. if(period && (period < 30 || period > 240*5))
  1936. error(Ebadctl);
  1937. period /= 5;
  1938. break;
  1939. }
  1940. if(atastandby(drive, period) != SDok)
  1941. error(Ebadctl);
  1942. }
  1943. else if(strcmp(cb->f[0], "lba48always") == 0){
  1944. if(cb->nf != 2 || !(drive->flags&Lba48))
  1945. error(Ebadctl);
  1946. if(strcmp(cb->f[1], "on") == 0)
  1947. drive->flags |= Lba48always;
  1948. else if(strcmp(cb->f[1], "off") == 0)
  1949. drive->flags &= ~Lba48always;
  1950. else
  1951. error(Ebadctl);
  1952. }
  1953. else
  1954. error(Ebadctl);
  1955. qunlock(drive);
  1956. poperror();
  1957. return 0;
  1958. }
  1959. SDifc sdataifc = {
  1960. "ata", /* name */
  1961. atapnp, /* pnp */
  1962. atalegacy, /* legacy */
  1963. ataid, /* id */
  1964. ataenable, /* enable */
  1965. atadisable, /* disable */
  1966. scsiverify, /* verify */
  1967. scsionline, /* online */
  1968. atario, /* rio */
  1969. atarctl, /* rctl */
  1970. atawctl, /* wctl */
  1971. scsibio, /* bio */
  1972. ataprobew, /* probe */
  1973. ataclear, /* clear */
  1974. atastat, /* stat */
  1975. };