sdata.c 43 KB

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