sdata.c 60 KB

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