devsd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Storage Device.
  3. */
  4. #include "u.h"
  5. #include "mem.h"
  6. #include "lib.h"
  7. #include "dat.h"
  8. #include "fns.h"
  9. #include "io.h"
  10. #include "ureg.h"
  11. #include "error.h"
  12. #include "sd.h"
  13. #include "dosfs.h"
  14. #define parttrace 0
  15. extern SDifc* sdifc[];
  16. static SDev* sdlist;
  17. static SDunit** sdunit;
  18. static int sdnunit;
  19. enum {
  20. Rawcmd,
  21. Rawdata,
  22. Rawstatus,
  23. };
  24. void
  25. sdaddpart(SDunit* unit, char* name, ulong start, ulong end)
  26. {
  27. SDpart *pp;
  28. int i, partno;
  29. if(parttrace)
  30. print("add %d %s %s %ld %ld\n", unit->npart, unit->name, name, start, end);
  31. /*
  32. * Check name not already used
  33. * and look for a free slot.
  34. */
  35. if(unit->part != nil){
  36. partno = -1;
  37. for(i = 0; i < SDnpart; i++){
  38. pp = &unit->part[i];
  39. if(!pp->valid){
  40. if(partno == -1)
  41. partno = i;
  42. break;
  43. }
  44. if(strcmp(name, pp->name) == 0){
  45. if(pp->start == start && pp->end == end){
  46. if(parttrace)
  47. print("already present\n");
  48. return;
  49. }
  50. }
  51. }
  52. }else{
  53. if((unit->part = malloc(sizeof(SDpart)*SDnpart)) == nil){
  54. if(parttrace)
  55. print("malloc failed\n");
  56. return;
  57. }
  58. partno = 0;
  59. }
  60. /*
  61. * Check there is a free slot and size and extent are valid.
  62. */
  63. if(partno == -1 || start > end || end > unit->sectors){
  64. print("cannot add %s!%s [%lud,%lud) to disk [0,%lud): %s\n",
  65. unit->name, name, start, end, unit->sectors,
  66. partno==-1 ? "no free partitions" : "partition boundaries out of range");
  67. return;
  68. }
  69. pp = &unit->part[partno];
  70. pp->start = start;
  71. pp->end = end;
  72. strncpy(pp->name, name, NAMELEN);
  73. pp->valid = 1;
  74. unit->npart++;
  75. }
  76. void
  77. sddelpart(SDunit* unit, char* name)
  78. {
  79. int i;
  80. SDpart *pp;
  81. if(parttrace)
  82. print("del %d %s %s\n", unit->npart, unit->name, name);
  83. /*
  84. * Look for the partition to delete.
  85. * Can't delete if someone still has it open.
  86. * If it's the last valid partition zap the
  87. * whole table.
  88. */
  89. pp = unit->part;
  90. for(i = 0; i < SDnpart; i++){
  91. if(strncmp(name, pp->name, NAMELEN) == 0)
  92. break;
  93. pp++;
  94. }
  95. if(i >= SDnpart)
  96. return;
  97. pp->valid = 0;
  98. unit->npart--;
  99. if(unit->npart == 0){
  100. free(unit->part);
  101. unit->part = nil;
  102. }
  103. }
  104. static int
  105. sdinitpart(SDunit* unit)
  106. {
  107. unit->sectors = unit->secsize = 0;
  108. unit->npart = 0;
  109. if(unit->part){
  110. free(unit->part);
  111. unit->part = nil;
  112. }
  113. if(unit->inquiry[0] & 0xC0)
  114. return 0;
  115. switch(unit->inquiry[0] & 0x1F){
  116. case 0x00: /* DA */
  117. case 0x04: /* WORM */
  118. case 0x05: /* CD-ROM */
  119. case 0x07: /* MO */
  120. break;
  121. default:
  122. return 0;
  123. }
  124. if(unit->dev->ifc->online == nil || unit->dev->ifc->online(unit) == 0)
  125. return 0;
  126. sdaddpart(unit, "data", 0, unit->sectors);
  127. return 1;
  128. }
  129. static SDunit*
  130. sdgetunit(SDev* sdev, int subno)
  131. {
  132. int index;
  133. SDunit *unit;
  134. /*
  135. * Associate a unit with a given device and sub-unit
  136. * number on that device.
  137. * The device will be probed if it has not already been
  138. * successfully accessed.
  139. */
  140. qlock(&sdqlock);
  141. index = sdev->index+subno;
  142. unit = sdunit[index];
  143. if(unit == nil){
  144. if((unit = malloc(sizeof(SDunit))) == nil){
  145. qunlock(&sdqlock);
  146. return nil;
  147. }
  148. if(sdev->enabled == 0 && sdev->ifc->enable)
  149. sdev->ifc->enable(sdev);
  150. sdev->enabled = 1;
  151. snprint(unit->name, NAMELEN, "sd%c%d", sdev->idno, subno);
  152. unit->subno = subno;
  153. unit->dev = sdev;
  154. /*
  155. * No need to lock anything here as this is only
  156. * called before the unit is made available in the
  157. * sdunit[] array.
  158. */
  159. if(unit->dev->ifc->verify(unit) == 0){
  160. qunlock(&sdqlock);
  161. free(unit);
  162. return nil;
  163. }
  164. sdunit[index] = unit;
  165. }
  166. qunlock(&sdqlock);
  167. return unit;
  168. }
  169. static SDunit*
  170. sdindex2unit(int index)
  171. {
  172. SDev *sdev;
  173. /*
  174. * Associate a unit with a given index into the top-level
  175. * device directory.
  176. * The device will be probed if it has not already been
  177. * successfully accessed.
  178. */
  179. for(sdev = sdlist; sdev != nil; sdev = sdev->next){
  180. if(index >= sdev->index && index < sdev->index+sdev->nunit)
  181. return sdgetunit(sdev, index-sdev->index);
  182. }
  183. return nil;
  184. }
  185. static void
  186. _sddetach(void)
  187. {
  188. SDev *sdev;
  189. for(sdev = sdlist; sdev != nil; sdev = sdev->next){
  190. if(sdev->enabled == 0)
  191. continue;
  192. if(sdev->ifc->disable)
  193. sdev->ifc->disable(sdev);
  194. sdev->enabled = 0;
  195. }
  196. }
  197. int
  198. sdinit(void)
  199. {
  200. ulong m;
  201. int i;
  202. SDev *sdev, *tail;
  203. SDunit *unit;
  204. /*
  205. * Probe all configured controllers and make a list
  206. * of devices found, accumulating a possible maximum number
  207. * of units attached and marking each device with an index
  208. * into the linear top-level directory array of units.
  209. */
  210. tail = nil;
  211. for(i = 0; sdifc[i] != nil; i++){
  212. if((sdev = sdifc[i]->pnp()) == nil)
  213. continue;
  214. if(sdlist != nil)
  215. tail->next = sdev;
  216. else
  217. sdlist = sdev;
  218. for(tail = sdev; tail->next != nil; tail = tail->next){
  219. sdev->index = sdnunit;
  220. sdnunit += tail->nunit;
  221. }
  222. tail->index = sdnunit;
  223. sdnunit += tail->nunit;
  224. }
  225. /*
  226. * Legacy and option code goes here. This will be hard...
  227. */
  228. /*
  229. * The maximum number of possible units is known, allocate
  230. * placeholders for their datastructures; the units will be
  231. * probed and structures allocated when attached.
  232. * Allocate controller names for the different types.
  233. */
  234. if(sdnunit == 0)
  235. return 0;
  236. if((sdunit = malloc(sdnunit*sizeof(SDunit*))) == nil)
  237. return 0;
  238. sddetach = _sddetach;
  239. for(i = 0; sdifc[i] != nil; i++){
  240. if(sdifc[i]->id)
  241. sdifc[i]->id(sdlist);
  242. }
  243. m = 0;
  244. for(i=0; i<sdnunit && i < 32; i++) {
  245. unit = sdindex2unit(i);
  246. if(unit == nil)
  247. continue;
  248. sdinitpart(unit);
  249. partition(unit);
  250. if(unit->npart > 0) /* BUG */
  251. m |= (1<<i);
  252. }
  253. //notesdinfo();
  254. return m;
  255. }
  256. void
  257. sdinitdev(int i, char *s)
  258. {
  259. SDunit *unit;
  260. unit = sdindex2unit(i);
  261. strcpy(s, unit->name);
  262. }
  263. void
  264. sdprintdevs(int i)
  265. {
  266. char *s;
  267. SDunit *unit;
  268. unit = sdindex2unit(i);
  269. for(i=0; i<unit->npart; i++){
  270. s = unit->part[i].name;
  271. if(strncmp(s, "dos", 3) == 0 || strncmp(s, "9fat", 4) == 0)
  272. print(" %s!%s", unit->name, s);
  273. }
  274. }
  275. SDpart*
  276. sdfindpart(SDunit *unit, char *name)
  277. {
  278. int i;
  279. if(parttrace)
  280. print("findpart %d %s %s\t\n", unit->npart, unit->name, name);
  281. for(i=0; i<unit->npart; i++) {
  282. if(parttrace)
  283. print("%s...", unit->part[i].name);
  284. if(strcmp(unit->part[i].name, name) == 0){
  285. if(parttrace)
  286. print("\n");
  287. return &unit->part[i];
  288. }
  289. }
  290. if(parttrace)
  291. print("not found\n");
  292. return nil;
  293. }
  294. typedef struct Scsicrud Scsicrud;
  295. struct Scsicrud {
  296. Dos;
  297. vlong offset;
  298. SDunit *unit;
  299. SDpart *part;
  300. };
  301. long
  302. sdread(Dos *vdos, void *v, long n)
  303. {
  304. Scsicrud *dos;
  305. long x;
  306. dos = (Scsicrud*)vdos;
  307. x = sdbio(dos->unit, dos->part, v, n, dos->offset);
  308. if(x > 0)
  309. dos->offset += x;
  310. return x;
  311. }
  312. vlong
  313. sdseek(Dos *vdos, vlong seek)
  314. {
  315. ((Scsicrud*)vdos)->offset = seek;
  316. return seek;
  317. }
  318. void*
  319. sdgetdospart(int i, char *s, int chatty)
  320. {
  321. SDunit *unit;
  322. SDpart *p;
  323. Scsicrud *dos;
  324. unit = sdindex2unit(i);
  325. if((p = sdfindpart(unit, s)) == nil){
  326. if(chatty)
  327. print("unknown partition %s!%s\n", unit->name, s);
  328. return nil;
  329. }
  330. if(p->dos == nil) {
  331. dos = malloc(sizeof(Scsicrud));
  332. dos->dev = i;
  333. dos->read = sdread;
  334. dos->seek = sdseek;
  335. dos->start = 0;
  336. dos->unit = unit;
  337. dos->part = p;
  338. if(dosinit(dos) < 0 && dosinit(dos) < 0){
  339. if(chatty)
  340. print("partition %s!%s does not contain a DOS file system\n",
  341. unit->name, s);
  342. return nil;
  343. }
  344. p->dos = dos;
  345. }
  346. return p->dos;
  347. }
  348. /*
  349. * Leave partitions around for devsd to pick up.
  350. * (Needed by boot process; more extensive
  351. * partitioning is done by termrc or cpurc).
  352. */
  353. void
  354. sdaddconf(int i)
  355. {
  356. SDunit *unit;
  357. SDpart *pp;
  358. unit = sdindex2unit(i);
  359. /*
  360. * If there were no partitions (just data and partition), don't bother.
  361. */
  362. if(unit->npart<= 1 || (unit->npart==2 && strcmp(unit->part[1].name, "partition")==0))
  363. return;
  364. addconf("%spart=", unit->name);
  365. for(i=1, pp=&unit->part[i]; i<unit->npart; i++, pp++) /* skip 0, which is "data" */
  366. addconf("%s%s %ld %ld", i==1 ? "" : "/", pp->name,
  367. pp->start, pp->end);
  368. addconf("\n");
  369. }
  370. int
  371. sdboot(int dev, char *pname, Boot *b)
  372. {
  373. char *file;
  374. Dos *dos;
  375. if((file = strchr(pname, '!')) == nil) {
  376. print("syntax is sdC0!partition!file\n");
  377. return -1;
  378. }
  379. *file++ = '\0';
  380. dos = sdgetdospart(dev, pname, 1);
  381. if(dos == nil)
  382. return -1;
  383. return dosboot(dos, file, b);
  384. }
  385. long
  386. sdbio(SDunit *unit, SDpart *pp, void* va, long len, vlong off)
  387. {
  388. long l;
  389. ulong bno, max, nb, offset;
  390. static uchar *b;
  391. char *a;
  392. static ulong bsz;
  393. a = va;
  394. memset(a, 0xDA, len);
  395. qlock(&unit->ctl);
  396. if(unit->changed){
  397. qunlock(&unit->ctl);
  398. return 0;
  399. }
  400. /*
  401. * Check the request is within bounds.
  402. * Removeable drives are locked throughout the I/O
  403. * in case the media changes unexpectedly.
  404. * Non-removeable drives are not locked during the I/O
  405. * to allow the hardware to optimise if it can; this is
  406. * a little fast and loose.
  407. * It's assumed that non-removable media parameters
  408. * (sectors, secsize) can't change once the drive has
  409. * been brought online.
  410. */
  411. bno = (off/unit->secsize) + pp->start;
  412. nb = ((off+len+unit->secsize-1)/unit->secsize) + pp->start - bno;
  413. max = SDmaxio/unit->secsize;
  414. if(nb > max)
  415. nb = max;
  416. if(bno+nb > pp->end)
  417. nb = pp->end - bno;
  418. if(bno >= pp->end || nb == 0){
  419. qunlock(&unit->ctl);
  420. return 0;
  421. }
  422. if(!(unit->inquiry[1] & 0x80))
  423. qunlock(&unit->ctl);
  424. if(bsz < nb*unit->secsize){
  425. b = malloc(nb*unit->secsize);
  426. bsz = nb*unit->secsize;
  427. }
  428. // b = sdmalloc(nb*unit->secsize);
  429. // if(b == nil)
  430. // return 0;
  431. offset = off%unit->secsize;
  432. if((l = unit->dev->ifc->bio(unit, 0, 0, b, nb, bno)) < 0) {
  433. // sdfree(b);
  434. return 0;
  435. }
  436. if(l < offset)
  437. len = 0;
  438. else if(len > l - offset)
  439. len = l - offset;
  440. if(len)
  441. memmove(a, b+offset, len);
  442. // sdfree(b);
  443. if(unit->inquiry[1] & 0x80)
  444. qunlock(&unit->ctl);
  445. return len;
  446. }
  447. #ifdef DMA
  448. long
  449. sdrio(SDreq *r, void* a, long n)
  450. {
  451. if(n >= SDmaxio || n < 0)
  452. return 0;
  453. r->data = nil;
  454. if(n){
  455. if((r->data = malloc(n)) == nil)
  456. return 0;
  457. if(r->write)
  458. memmove(r->data, a, n);
  459. }
  460. r->dlen = n;
  461. if(r->unit->dev->ifc->rio(r) != SDok){
  462. // cgascreenputs("1", 1);
  463. if(r->data != nil){
  464. sdfree(r->data);
  465. r->data = nil;
  466. }
  467. return 0;
  468. }
  469. // cgascreenputs("2", 1);
  470. if(!r->write && r->rlen > 0)
  471. memmove(a, r->data, r->rlen);
  472. // cgascreenputs("3", 1);
  473. if(r->data != nil){
  474. sdfree(r->data);
  475. r->data = nil;
  476. }
  477. // cgascreenputs("4", 1);
  478. return r->rlen;
  479. }
  480. #endif /* DMA */
  481. void
  482. sleep(void*, int (*fn)(void*), void *v)
  483. {
  484. int x;
  485. x = spllo();
  486. while(!fn(v))
  487. ;
  488. splx(x);
  489. return;
  490. }
  491. void
  492. tsleep(void*, int (*fn)(void*), void *v, int msec)
  493. {
  494. int x;
  495. ulong start;
  496. x = spllo();
  497. for(start = m->ticks; TK2MS(m->ticks - start) < msec
  498. && !fn(v); )
  499. ;
  500. splx(x);
  501. return;
  502. }
  503. void*
  504. sdmalloc(void *p, ulong sz)
  505. {
  506. if(p != nil) {
  507. memset(p, 0, sz);
  508. return p;
  509. }
  510. return malloc(sz);
  511. }