sdata.c 44 KB

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