lsi53c895a.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. /*
  2. * QEMU LSI53C895A SCSI Host Bus Adapter emulation
  3. *
  4. * Copyright (c) 2006 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the LGPL.
  8. */
  9. /* Note:
  10. * LSI53C810 emulation is incorrect, in the sense that it supports
  11. * features added in later evolutions. This should not be a problem,
  12. * as well-behaved operating systems will not try to use them.
  13. */
  14. #include "qemu/osdep.h"
  15. #include "hw/irq.h"
  16. #include "hw/pci/pci_device.h"
  17. #include "hw/scsi/scsi.h"
  18. #include "migration/vmstate.h"
  19. #include "sysemu/dma.h"
  20. #include "qemu/log.h"
  21. #include "qemu/module.h"
  22. #include "trace.h"
  23. #include "qom/object.h"
  24. static const char *names[] = {
  25. "SCNTL0", "SCNTL1", "SCNTL2", "SCNTL3", "SCID", "SXFER", "SDID", "GPREG",
  26. "SFBR", "SOCL", "SSID", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
  27. "DSA0", "DSA1", "DSA2", "DSA3", "ISTAT", "0x15", "0x16", "0x17",
  28. "CTEST0", "CTEST1", "CTEST2", "CTEST3", "TEMP0", "TEMP1", "TEMP2", "TEMP3",
  29. "DFIFO", "CTEST4", "CTEST5", "CTEST6", "DBC0", "DBC1", "DBC2", "DCMD",
  30. "DNAD0", "DNAD1", "DNAD2", "DNAD3", "DSP0", "DSP1", "DSP2", "DSP3",
  31. "DSPS0", "DSPS1", "DSPS2", "DSPS3", "SCRATCHA0", "SCRATCHA1", "SCRATCHA2", "SCRATCHA3",
  32. "DMODE", "DIEN", "SBR", "DCNTL", "ADDER0", "ADDER1", "ADDER2", "ADDER3",
  33. "SIEN0", "SIEN1", "SIST0", "SIST1", "SLPAR", "0x45", "MACNTL", "GPCNTL",
  34. "STIME0", "STIME1", "RESPID", "0x4b", "STEST0", "STEST1", "STEST2", "STEST3",
  35. "SIDL", "0x51", "0x52", "0x53", "SODL", "0x55", "0x56", "0x57",
  36. "SBDL", "0x59", "0x5a", "0x5b", "SCRATCHB0", "SCRATCHB1", "SCRATCHB2", "SCRATCHB3",
  37. };
  38. #define LSI_MAX_DEVS 7
  39. #define LSI_SCNTL0_TRG 0x01
  40. #define LSI_SCNTL0_AAP 0x02
  41. #define LSI_SCNTL0_EPC 0x08
  42. #define LSI_SCNTL0_WATN 0x10
  43. #define LSI_SCNTL0_START 0x20
  44. #define LSI_SCNTL1_SST 0x01
  45. #define LSI_SCNTL1_IARB 0x02
  46. #define LSI_SCNTL1_AESP 0x04
  47. #define LSI_SCNTL1_RST 0x08
  48. #define LSI_SCNTL1_CON 0x10
  49. #define LSI_SCNTL1_DHP 0x20
  50. #define LSI_SCNTL1_ADB 0x40
  51. #define LSI_SCNTL1_EXC 0x80
  52. #define LSI_SCNTL2_WSR 0x01
  53. #define LSI_SCNTL2_VUE0 0x02
  54. #define LSI_SCNTL2_VUE1 0x04
  55. #define LSI_SCNTL2_WSS 0x08
  56. #define LSI_SCNTL2_SLPHBEN 0x10
  57. #define LSI_SCNTL2_SLPMD 0x20
  58. #define LSI_SCNTL2_CHM 0x40
  59. #define LSI_SCNTL2_SDU 0x80
  60. #define LSI_ISTAT0_DIP 0x01
  61. #define LSI_ISTAT0_SIP 0x02
  62. #define LSI_ISTAT0_INTF 0x04
  63. #define LSI_ISTAT0_CON 0x08
  64. #define LSI_ISTAT0_SEM 0x10
  65. #define LSI_ISTAT0_SIGP 0x20
  66. #define LSI_ISTAT0_SRST 0x40
  67. #define LSI_ISTAT0_ABRT 0x80
  68. #define LSI_ISTAT1_SI 0x01
  69. #define LSI_ISTAT1_SRUN 0x02
  70. #define LSI_ISTAT1_FLSH 0x04
  71. #define LSI_SSTAT0_SDP0 0x01
  72. #define LSI_SSTAT0_RST 0x02
  73. #define LSI_SSTAT0_WOA 0x04
  74. #define LSI_SSTAT0_LOA 0x08
  75. #define LSI_SSTAT0_AIP 0x10
  76. #define LSI_SSTAT0_OLF 0x20
  77. #define LSI_SSTAT0_ORF 0x40
  78. #define LSI_SSTAT0_ILF 0x80
  79. #define LSI_SIST0_PAR 0x01
  80. #define LSI_SIST0_RST 0x02
  81. #define LSI_SIST0_UDC 0x04
  82. #define LSI_SIST0_SGE 0x08
  83. #define LSI_SIST0_RSL 0x10
  84. #define LSI_SIST0_SEL 0x20
  85. #define LSI_SIST0_CMP 0x40
  86. #define LSI_SIST0_MA 0x80
  87. #define LSI_SIST1_HTH 0x01
  88. #define LSI_SIST1_GEN 0x02
  89. #define LSI_SIST1_STO 0x04
  90. #define LSI_SIST1_SBMC 0x10
  91. #define LSI_SOCL_IO 0x01
  92. #define LSI_SOCL_CD 0x02
  93. #define LSI_SOCL_MSG 0x04
  94. #define LSI_SOCL_ATN 0x08
  95. #define LSI_SOCL_SEL 0x10
  96. #define LSI_SOCL_BSY 0x20
  97. #define LSI_SOCL_ACK 0x40
  98. #define LSI_SOCL_REQ 0x80
  99. #define LSI_DSTAT_IID 0x01
  100. #define LSI_DSTAT_SIR 0x04
  101. #define LSI_DSTAT_SSI 0x08
  102. #define LSI_DSTAT_ABRT 0x10
  103. #define LSI_DSTAT_BF 0x20
  104. #define LSI_DSTAT_MDPE 0x40
  105. #define LSI_DSTAT_DFE 0x80
  106. #define LSI_DCNTL_COM 0x01
  107. #define LSI_DCNTL_IRQD 0x02
  108. #define LSI_DCNTL_STD 0x04
  109. #define LSI_DCNTL_IRQM 0x08
  110. #define LSI_DCNTL_SSM 0x10
  111. #define LSI_DCNTL_PFEN 0x20
  112. #define LSI_DCNTL_PFF 0x40
  113. #define LSI_DCNTL_CLSE 0x80
  114. #define LSI_DMODE_MAN 0x01
  115. #define LSI_DMODE_BOF 0x02
  116. #define LSI_DMODE_ERMP 0x04
  117. #define LSI_DMODE_ERL 0x08
  118. #define LSI_DMODE_DIOM 0x10
  119. #define LSI_DMODE_SIOM 0x20
  120. #define LSI_CTEST2_DACK 0x01
  121. #define LSI_CTEST2_DREQ 0x02
  122. #define LSI_CTEST2_TEOP 0x04
  123. #define LSI_CTEST2_PCICIE 0x08
  124. #define LSI_CTEST2_CM 0x10
  125. #define LSI_CTEST2_CIO 0x20
  126. #define LSI_CTEST2_SIGP 0x40
  127. #define LSI_CTEST2_DDIR 0x80
  128. #define LSI_CTEST5_BL2 0x04
  129. #define LSI_CTEST5_DDIR 0x08
  130. #define LSI_CTEST5_MASR 0x10
  131. #define LSI_CTEST5_DFSN 0x20
  132. #define LSI_CTEST5_BBCK 0x40
  133. #define LSI_CTEST5_ADCK 0x80
  134. #define LSI_CCNTL0_DILS 0x01
  135. #define LSI_CCNTL0_DISFC 0x10
  136. #define LSI_CCNTL0_ENNDJ 0x20
  137. #define LSI_CCNTL0_PMJCTL 0x40
  138. #define LSI_CCNTL0_ENPMJ 0x80
  139. #define LSI_CCNTL1_EN64DBMV 0x01
  140. #define LSI_CCNTL1_EN64TIBMV 0x02
  141. #define LSI_CCNTL1_64TIMOD 0x04
  142. #define LSI_CCNTL1_DDAC 0x08
  143. #define LSI_CCNTL1_ZMOD 0x80
  144. #define LSI_SBCL_ATN 0x08
  145. #define LSI_SBCL_BSY 0x20
  146. #define LSI_SBCL_ACK 0x40
  147. #define LSI_SBCL_REQ 0x80
  148. /* Enable Response to Reselection */
  149. #define LSI_SCID_RRE 0x60
  150. #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
  151. #define PHASE_DO 0
  152. #define PHASE_DI 1
  153. #define PHASE_CMD 2
  154. #define PHASE_ST 3
  155. #define PHASE_MO 6
  156. #define PHASE_MI 7
  157. #define PHASE_MASK 7
  158. /* Maximum length of MSG IN data. */
  159. #define LSI_MAX_MSGIN_LEN 8
  160. /* Flag set if this is a tagged command. */
  161. #define LSI_TAG_VALID (1 << 16)
  162. /* Maximum instructions to process. */
  163. #define LSI_MAX_INSN 10000
  164. typedef struct lsi_request {
  165. SCSIRequest *req;
  166. uint32_t tag;
  167. uint32_t dma_len;
  168. uint8_t *dma_buf;
  169. uint32_t pending;
  170. int out;
  171. QTAILQ_ENTRY(lsi_request) next;
  172. } lsi_request;
  173. enum {
  174. LSI_NOWAIT, /* SCRIPTS are running or stopped */
  175. LSI_WAIT_RESELECT, /* Wait Reselect instruction has been issued */
  176. LSI_DMA_SCRIPTS, /* processing DMA from lsi_execute_script */
  177. LSI_DMA_IN_PROGRESS, /* DMA operation is in progress */
  178. };
  179. enum {
  180. LSI_MSG_ACTION_COMMAND = 0,
  181. LSI_MSG_ACTION_DISCONNECT = 1,
  182. LSI_MSG_ACTION_DOUT = 2,
  183. LSI_MSG_ACTION_DIN = 3,
  184. };
  185. struct LSIState {
  186. /*< private >*/
  187. PCIDevice parent_obj;
  188. /*< public >*/
  189. qemu_irq ext_irq;
  190. MemoryRegion mmio_io;
  191. MemoryRegion ram_io;
  192. MemoryRegion io_io;
  193. AddressSpace pci_io_as;
  194. int carry; /* ??? Should this be an a visible register somewhere? */
  195. int status;
  196. int msg_action;
  197. int msg_len;
  198. uint8_t msg[LSI_MAX_MSGIN_LEN];
  199. int waiting;
  200. SCSIBus bus;
  201. int current_lun;
  202. /* The tag is a combination of the device ID and the SCSI tag. */
  203. uint32_t select_tag;
  204. int command_complete;
  205. QTAILQ_HEAD(, lsi_request) queue;
  206. lsi_request *current;
  207. uint32_t dsa;
  208. uint32_t temp;
  209. uint32_t dnad;
  210. uint32_t dbc;
  211. uint8_t istat0;
  212. uint8_t istat1;
  213. uint8_t dcmd;
  214. uint8_t dstat;
  215. uint8_t dien;
  216. uint8_t sist0;
  217. uint8_t sist1;
  218. uint8_t sien0;
  219. uint8_t sien1;
  220. uint8_t mbox0;
  221. uint8_t mbox1;
  222. uint8_t dfifo;
  223. uint8_t ctest2;
  224. uint8_t ctest3;
  225. uint8_t ctest4;
  226. uint8_t ctest5;
  227. uint8_t ccntl0;
  228. uint8_t ccntl1;
  229. uint32_t dsp;
  230. uint32_t dsps;
  231. uint8_t dmode;
  232. uint8_t dcntl;
  233. uint8_t scntl0;
  234. uint8_t scntl1;
  235. uint8_t scntl2;
  236. uint8_t scntl3;
  237. uint8_t sstat0;
  238. uint8_t sstat1;
  239. uint8_t scid;
  240. uint8_t sxfer;
  241. uint8_t socl;
  242. uint8_t sdid;
  243. uint8_t ssid;
  244. uint8_t sfbr;
  245. uint8_t sbcl;
  246. uint8_t stest1;
  247. uint8_t stest2;
  248. uint8_t stest3;
  249. uint8_t sidl;
  250. uint8_t stime0;
  251. uint8_t respid0;
  252. uint8_t respid1;
  253. uint32_t mmrs;
  254. uint32_t mmws;
  255. uint32_t sfs;
  256. uint32_t drs;
  257. uint32_t sbms;
  258. uint32_t dbms;
  259. uint32_t dnad64;
  260. uint32_t pmjad1;
  261. uint32_t pmjad2;
  262. uint32_t rbc;
  263. uint32_t ua;
  264. uint32_t ia;
  265. uint32_t sbc;
  266. uint32_t csbc;
  267. uint32_t scratch[18]; /* SCRATCHA-SCRATCHR */
  268. uint8_t sbr;
  269. uint32_t adder;
  270. uint8_t script_ram[2048 * sizeof(uint32_t)];
  271. };
  272. #define TYPE_LSI53C810 "lsi53c810"
  273. #define TYPE_LSI53C895A "lsi53c895a"
  274. OBJECT_DECLARE_SIMPLE_TYPE(LSIState, LSI53C895A)
  275. static const char *scsi_phases[] = {
  276. "DOUT",
  277. "DIN",
  278. "CMD",
  279. "STATUS",
  280. "RSVOUT",
  281. "RSVIN",
  282. "MSGOUT",
  283. "MSGIN"
  284. };
  285. static const char *scsi_phase_name(int phase)
  286. {
  287. return scsi_phases[phase & PHASE_MASK];
  288. }
  289. static inline int lsi_irq_on_rsl(LSIState *s)
  290. {
  291. return (s->sien0 & LSI_SIST0_RSL) && (s->scid & LSI_SCID_RRE);
  292. }
  293. static lsi_request *get_pending_req(LSIState *s)
  294. {
  295. lsi_request *p;
  296. QTAILQ_FOREACH(p, &s->queue, next) {
  297. if (p->pending) {
  298. return p;
  299. }
  300. }
  301. return NULL;
  302. }
  303. static void lsi_soft_reset(LSIState *s)
  304. {
  305. trace_lsi_reset();
  306. s->carry = 0;
  307. s->msg_action = LSI_MSG_ACTION_COMMAND;
  308. s->msg_len = 0;
  309. s->waiting = LSI_NOWAIT;
  310. s->dsa = 0;
  311. s->dnad = 0;
  312. s->dbc = 0;
  313. s->temp = 0;
  314. memset(s->scratch, 0, sizeof(s->scratch));
  315. s->istat0 = 0;
  316. s->istat1 = 0;
  317. s->dcmd = 0x40;
  318. s->dstat = 0;
  319. s->dien = 0;
  320. s->sist0 = 0;
  321. s->sist1 = 0;
  322. s->sien0 = 0;
  323. s->sien1 = 0;
  324. s->mbox0 = 0;
  325. s->mbox1 = 0;
  326. s->dfifo = 0;
  327. s->ctest2 = LSI_CTEST2_DACK;
  328. s->ctest3 = 0;
  329. s->ctest4 = 0;
  330. s->ctest5 = 0;
  331. s->ccntl0 = 0;
  332. s->ccntl1 = 0;
  333. s->dsp = 0;
  334. s->dsps = 0;
  335. s->dmode = 0;
  336. s->dcntl = 0;
  337. s->scntl0 = 0xc0;
  338. s->scntl1 = 0;
  339. s->scntl2 = 0;
  340. s->scntl3 = 0;
  341. s->sstat0 = 0;
  342. s->sstat1 = 0;
  343. s->scid = 7;
  344. s->sxfer = 0;
  345. s->socl = 0;
  346. s->sdid = 0;
  347. s->ssid = 0;
  348. s->sbcl = 0;
  349. s->stest1 = 0;
  350. s->stest2 = 0;
  351. s->stest3 = 0;
  352. s->sidl = 0;
  353. s->stime0 = 0;
  354. s->respid0 = 0x80;
  355. s->respid1 = 0;
  356. s->mmrs = 0;
  357. s->mmws = 0;
  358. s->sfs = 0;
  359. s->drs = 0;
  360. s->sbms = 0;
  361. s->dbms = 0;
  362. s->dnad64 = 0;
  363. s->pmjad1 = 0;
  364. s->pmjad2 = 0;
  365. s->rbc = 0;
  366. s->ua = 0;
  367. s->ia = 0;
  368. s->sbc = 0;
  369. s->csbc = 0;
  370. s->sbr = 0;
  371. assert(QTAILQ_EMPTY(&s->queue));
  372. assert(!s->current);
  373. }
  374. static int lsi_dma_40bit(LSIState *s)
  375. {
  376. if ((s->ccntl1 & LSI_CCNTL1_40BIT) == LSI_CCNTL1_40BIT)
  377. return 1;
  378. return 0;
  379. }
  380. static int lsi_dma_ti64bit(LSIState *s)
  381. {
  382. if ((s->ccntl1 & LSI_CCNTL1_EN64TIBMV) == LSI_CCNTL1_EN64TIBMV)
  383. return 1;
  384. return 0;
  385. }
  386. static int lsi_dma_64bit(LSIState *s)
  387. {
  388. if ((s->ccntl1 & LSI_CCNTL1_EN64DBMV) == LSI_CCNTL1_EN64DBMV)
  389. return 1;
  390. return 0;
  391. }
  392. static uint8_t lsi_reg_readb(LSIState *s, int offset);
  393. static void lsi_reg_writeb(LSIState *s, int offset, uint8_t val);
  394. static void lsi_execute_script(LSIState *s);
  395. static void lsi_reselect(LSIState *s, lsi_request *p);
  396. static inline void lsi_mem_read(LSIState *s, dma_addr_t addr,
  397. void *buf, dma_addr_t len)
  398. {
  399. if (s->dmode & LSI_DMODE_SIOM) {
  400. address_space_read(&s->pci_io_as, addr, MEMTXATTRS_UNSPECIFIED,
  401. buf, len);
  402. } else {
  403. pci_dma_read(PCI_DEVICE(s), addr, buf, len);
  404. }
  405. }
  406. static inline void lsi_mem_write(LSIState *s, dma_addr_t addr,
  407. const void *buf, dma_addr_t len)
  408. {
  409. if (s->dmode & LSI_DMODE_DIOM) {
  410. address_space_write(&s->pci_io_as, addr, MEMTXATTRS_UNSPECIFIED,
  411. buf, len);
  412. } else {
  413. pci_dma_write(PCI_DEVICE(s), addr, buf, len);
  414. }
  415. }
  416. static inline uint32_t read_dword(LSIState *s, uint32_t addr)
  417. {
  418. uint32_t buf;
  419. pci_dma_read(PCI_DEVICE(s), addr, &buf, 4);
  420. return cpu_to_le32(buf);
  421. }
  422. static void lsi_stop_script(LSIState *s)
  423. {
  424. s->istat1 &= ~LSI_ISTAT1_SRUN;
  425. }
  426. static void lsi_set_irq(LSIState *s, int level)
  427. {
  428. PCIDevice *d = PCI_DEVICE(s);
  429. if (s->ext_irq) {
  430. qemu_set_irq(s->ext_irq, level);
  431. } else {
  432. pci_set_irq(d, level);
  433. }
  434. }
  435. static void lsi_update_irq(LSIState *s)
  436. {
  437. int level;
  438. static int last_level;
  439. /* It's unclear whether the DIP/SIP bits should be cleared when the
  440. Interrupt Status Registers are cleared or when istat0 is read.
  441. We currently do the formwer, which seems to work. */
  442. level = 0;
  443. if (s->dstat) {
  444. if (s->dstat & s->dien)
  445. level = 1;
  446. s->istat0 |= LSI_ISTAT0_DIP;
  447. } else {
  448. s->istat0 &= ~LSI_ISTAT0_DIP;
  449. }
  450. if (s->sist0 || s->sist1) {
  451. if ((s->sist0 & s->sien0) || (s->sist1 & s->sien1))
  452. level = 1;
  453. s->istat0 |= LSI_ISTAT0_SIP;
  454. } else {
  455. s->istat0 &= ~LSI_ISTAT0_SIP;
  456. }
  457. if (s->istat0 & LSI_ISTAT0_INTF)
  458. level = 1;
  459. if (level != last_level) {
  460. trace_lsi_update_irq(level, s->dstat, s->sist1, s->sist0);
  461. last_level = level;
  462. }
  463. lsi_set_irq(s, level);
  464. if (!s->current && !level && lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON)) {
  465. lsi_request *p;
  466. trace_lsi_update_irq_disconnected();
  467. p = get_pending_req(s);
  468. if (p) {
  469. lsi_reselect(s, p);
  470. }
  471. }
  472. }
  473. /* Stop SCRIPTS execution and raise a SCSI interrupt. */
  474. static void lsi_script_scsi_interrupt(LSIState *s, int stat0, int stat1)
  475. {
  476. uint32_t mask0;
  477. uint32_t mask1;
  478. trace_lsi_script_scsi_interrupt(stat1, stat0, s->sist1, s->sist0);
  479. s->sist0 |= stat0;
  480. s->sist1 |= stat1;
  481. /* Stop processor on fatal or unmasked interrupt. As a special hack
  482. we don't stop processing when raising STO. Instead continue
  483. execution and stop at the next insn that accesses the SCSI bus. */
  484. mask0 = s->sien0 | ~(LSI_SIST0_CMP | LSI_SIST0_SEL | LSI_SIST0_RSL);
  485. mask1 = s->sien1 | ~(LSI_SIST1_GEN | LSI_SIST1_HTH);
  486. mask1 &= ~LSI_SIST1_STO;
  487. if (s->sist0 & mask0 || s->sist1 & mask1) {
  488. lsi_stop_script(s);
  489. }
  490. lsi_update_irq(s);
  491. }
  492. /* Stop SCRIPTS execution and raise a DMA interrupt. */
  493. static void lsi_script_dma_interrupt(LSIState *s, int stat)
  494. {
  495. trace_lsi_script_dma_interrupt(stat, s->dstat);
  496. s->dstat |= stat;
  497. lsi_update_irq(s);
  498. lsi_stop_script(s);
  499. }
  500. static inline void lsi_set_phase(LSIState *s, int phase)
  501. {
  502. s->sbcl &= ~PHASE_MASK;
  503. s->sbcl |= phase | LSI_SBCL_REQ;
  504. s->sstat1 = (s->sstat1 & ~PHASE_MASK) | phase;
  505. }
  506. static void lsi_bad_phase(LSIState *s, int out, int new_phase)
  507. {
  508. /* Trigger a phase mismatch. */
  509. if (s->ccntl0 & LSI_CCNTL0_ENPMJ) {
  510. if ((s->ccntl0 & LSI_CCNTL0_PMJCTL)) {
  511. s->dsp = out ? s->pmjad1 : s->pmjad2;
  512. } else {
  513. s->dsp = (s->scntl2 & LSI_SCNTL2_WSR ? s->pmjad2 : s->pmjad1);
  514. }
  515. trace_lsi_bad_phase_jump(s->dsp);
  516. } else {
  517. trace_lsi_bad_phase_interrupt();
  518. lsi_script_scsi_interrupt(s, LSI_SIST0_MA, 0);
  519. lsi_stop_script(s);
  520. }
  521. lsi_set_phase(s, new_phase);
  522. }
  523. /* Resume SCRIPTS execution after a DMA operation. */
  524. static void lsi_resume_script(LSIState *s)
  525. {
  526. if (s->waiting != 2) {
  527. s->waiting = LSI_NOWAIT;
  528. lsi_execute_script(s);
  529. } else {
  530. s->waiting = LSI_NOWAIT;
  531. }
  532. }
  533. static void lsi_disconnect(LSIState *s)
  534. {
  535. s->scntl1 &= ~LSI_SCNTL1_CON;
  536. s->sstat1 &= ~PHASE_MASK;
  537. s->sbcl = 0;
  538. }
  539. static void lsi_bad_selection(LSIState *s, uint32_t id)
  540. {
  541. trace_lsi_bad_selection(id);
  542. lsi_script_scsi_interrupt(s, 0, LSI_SIST1_STO);
  543. lsi_disconnect(s);
  544. }
  545. /* Initiate a SCSI layer data transfer. */
  546. static void lsi_do_dma(LSIState *s, int out)
  547. {
  548. uint32_t count;
  549. dma_addr_t addr;
  550. SCSIDevice *dev;
  551. if (!s->current || !s->current->dma_len) {
  552. /* Wait until data is available. */
  553. trace_lsi_do_dma_unavailable();
  554. return;
  555. }
  556. dev = s->current->req->dev;
  557. assert(dev);
  558. count = s->dbc;
  559. if (count > s->current->dma_len)
  560. count = s->current->dma_len;
  561. addr = s->dnad;
  562. /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
  563. if (lsi_dma_40bit(s) || lsi_dma_ti64bit(s))
  564. addr |= ((uint64_t)s->dnad64 << 32);
  565. else if (s->dbms)
  566. addr |= ((uint64_t)s->dbms << 32);
  567. else if (s->sbms)
  568. addr |= ((uint64_t)s->sbms << 32);
  569. trace_lsi_do_dma(addr, count);
  570. s->csbc += count;
  571. s->dnad += count;
  572. s->dbc -= count;
  573. if (s->current->dma_buf == NULL) {
  574. s->current->dma_buf = scsi_req_get_buf(s->current->req);
  575. }
  576. /* ??? Set SFBR to first data byte. */
  577. if (out) {
  578. lsi_mem_read(s, addr, s->current->dma_buf, count);
  579. } else {
  580. lsi_mem_write(s, addr, s->current->dma_buf, count);
  581. }
  582. s->current->dma_len -= count;
  583. if (s->current->dma_len == 0) {
  584. s->current->dma_buf = NULL;
  585. scsi_req_continue(s->current->req);
  586. } else {
  587. s->current->dma_buf += count;
  588. lsi_resume_script(s);
  589. }
  590. }
  591. /* Add a command to the queue. */
  592. static void lsi_queue_command(LSIState *s)
  593. {
  594. lsi_request *p = s->current;
  595. trace_lsi_queue_command(p->tag);
  596. assert(s->current != NULL);
  597. assert(s->current->dma_len == 0);
  598. QTAILQ_INSERT_TAIL(&s->queue, s->current, next);
  599. s->current = NULL;
  600. p->pending = 0;
  601. p->out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
  602. }
  603. /* Queue a byte for a MSG IN phase. */
  604. static void lsi_add_msg_byte(LSIState *s, uint8_t data)
  605. {
  606. if (s->msg_len >= LSI_MAX_MSGIN_LEN) {
  607. trace_lsi_add_msg_byte_error();
  608. } else {
  609. trace_lsi_add_msg_byte(data);
  610. s->msg[s->msg_len++] = data;
  611. }
  612. }
  613. /* Perform reselection to continue a command. */
  614. static void lsi_reselect(LSIState *s, lsi_request *p)
  615. {
  616. int id;
  617. assert(s->current == NULL);
  618. QTAILQ_REMOVE(&s->queue, p, next);
  619. s->current = p;
  620. id = (p->tag >> 8) & 0xf;
  621. s->ssid = id | 0x80;
  622. /* LSI53C700 Family Compatibility, see LSI53C895A 4-73 */
  623. if (!(s->dcntl & LSI_DCNTL_COM)) {
  624. s->sfbr = 1 << (id & 0x7);
  625. }
  626. trace_lsi_reselect(id);
  627. s->scntl1 |= LSI_SCNTL1_CON;
  628. lsi_set_phase(s, PHASE_MI);
  629. s->msg_action = p->out ? LSI_MSG_ACTION_DOUT : LSI_MSG_ACTION_DIN;
  630. s->current->dma_len = p->pending;
  631. lsi_add_msg_byte(s, 0x80);
  632. if (s->current->tag & LSI_TAG_VALID) {
  633. lsi_add_msg_byte(s, 0x20);
  634. lsi_add_msg_byte(s, p->tag & 0xff);
  635. }
  636. if (lsi_irq_on_rsl(s)) {
  637. lsi_script_scsi_interrupt(s, LSI_SIST0_RSL, 0);
  638. }
  639. }
  640. static lsi_request *lsi_find_by_tag(LSIState *s, uint32_t tag)
  641. {
  642. lsi_request *p;
  643. QTAILQ_FOREACH(p, &s->queue, next) {
  644. if (p->tag == tag) {
  645. return p;
  646. }
  647. }
  648. return NULL;
  649. }
  650. static void lsi_request_free(LSIState *s, lsi_request *p)
  651. {
  652. if (p == s->current) {
  653. s->current = NULL;
  654. } else {
  655. QTAILQ_REMOVE(&s->queue, p, next);
  656. }
  657. g_free(p);
  658. }
  659. static void lsi_request_cancelled(SCSIRequest *req)
  660. {
  661. LSIState *s = LSI53C895A(req->bus->qbus.parent);
  662. lsi_request *p = req->hba_private;
  663. req->hba_private = NULL;
  664. lsi_request_free(s, p);
  665. scsi_req_unref(req);
  666. }
  667. /* Record that data is available for a queued command. Returns zero if
  668. the device was reselected, nonzero if the IO is deferred. */
  669. static int lsi_queue_req(LSIState *s, SCSIRequest *req, uint32_t len)
  670. {
  671. lsi_request *p = req->hba_private;
  672. if (p->pending) {
  673. trace_lsi_queue_req_error(p);
  674. }
  675. p->pending = len;
  676. /* Reselect if waiting for it, or if reselection triggers an IRQ
  677. and the bus is free.
  678. Since no interrupt stacking is implemented in the emulation, it
  679. is also required that there are no pending interrupts waiting
  680. for service from the device driver. */
  681. if (s->waiting == LSI_WAIT_RESELECT ||
  682. (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON) &&
  683. !(s->istat0 & (LSI_ISTAT0_SIP | LSI_ISTAT0_DIP)))) {
  684. /* Reselect device. */
  685. lsi_reselect(s, p);
  686. return 0;
  687. } else {
  688. trace_lsi_queue_req(p->tag);
  689. p->pending = len;
  690. return 1;
  691. }
  692. }
  693. /* Callback to indicate that the SCSI layer has completed a command. */
  694. static void lsi_command_complete(SCSIRequest *req, size_t resid)
  695. {
  696. LSIState *s = LSI53C895A(req->bus->qbus.parent);
  697. int out;
  698. out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
  699. trace_lsi_command_complete(req->status);
  700. s->status = req->status;
  701. s->command_complete = 2;
  702. if (s->waiting && s->dbc != 0) {
  703. /* Raise phase mismatch for short transfers. */
  704. lsi_bad_phase(s, out, PHASE_ST);
  705. } else {
  706. lsi_set_phase(s, PHASE_ST);
  707. }
  708. if (req->hba_private == s->current) {
  709. req->hba_private = NULL;
  710. lsi_request_free(s, s->current);
  711. scsi_req_unref(req);
  712. }
  713. lsi_resume_script(s);
  714. }
  715. /* Callback to indicate that the SCSI layer has completed a transfer. */
  716. static void lsi_transfer_data(SCSIRequest *req, uint32_t len)
  717. {
  718. LSIState *s = LSI53C895A(req->bus->qbus.parent);
  719. int out;
  720. assert(req->hba_private);
  721. if (s->waiting == LSI_WAIT_RESELECT || req->hba_private != s->current ||
  722. (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON))) {
  723. if (lsi_queue_req(s, req, len)) {
  724. return;
  725. }
  726. }
  727. out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
  728. /* host adapter (re)connected */
  729. trace_lsi_transfer_data(req->tag, len);
  730. s->current->dma_len = len;
  731. s->command_complete = 1;
  732. if (s->waiting) {
  733. if (s->waiting == LSI_WAIT_RESELECT || s->dbc == 0) {
  734. lsi_resume_script(s);
  735. } else {
  736. lsi_do_dma(s, out);
  737. }
  738. }
  739. }
  740. static void lsi_do_command(LSIState *s)
  741. {
  742. SCSIDevice *dev;
  743. uint8_t buf[16];
  744. uint32_t id;
  745. int n;
  746. trace_lsi_do_command(s->dbc);
  747. if (s->dbc > 16)
  748. s->dbc = 16;
  749. pci_dma_read(PCI_DEVICE(s), s->dnad, buf, s->dbc);
  750. s->sfbr = buf[0];
  751. s->command_complete = 0;
  752. id = (s->select_tag >> 8) & 0xf;
  753. dev = scsi_device_find(&s->bus, 0, id, s->current_lun);
  754. if (!dev) {
  755. lsi_bad_selection(s, id);
  756. return;
  757. }
  758. assert(s->current == NULL);
  759. s->current = g_new0(lsi_request, 1);
  760. s->current->tag = s->select_tag;
  761. s->current->req = scsi_req_new(dev, s->current->tag, s->current_lun, buf,
  762. s->dbc, s->current);
  763. n = scsi_req_enqueue(s->current->req);
  764. if (n) {
  765. if (n > 0) {
  766. lsi_set_phase(s, PHASE_DI);
  767. } else if (n < 0) {
  768. lsi_set_phase(s, PHASE_DO);
  769. }
  770. scsi_req_continue(s->current->req);
  771. }
  772. if (!s->command_complete) {
  773. if (n) {
  774. /* Command did not complete immediately so disconnect. */
  775. lsi_add_msg_byte(s, 2); /* SAVE DATA POINTER */
  776. lsi_add_msg_byte(s, 4); /* DISCONNECT */
  777. /* wait data */
  778. lsi_set_phase(s, PHASE_MI);
  779. s->msg_action = LSI_MSG_ACTION_DISCONNECT;
  780. lsi_queue_command(s);
  781. } else {
  782. /* wait command complete */
  783. lsi_set_phase(s, PHASE_DI);
  784. }
  785. }
  786. }
  787. static void lsi_do_status(LSIState *s)
  788. {
  789. uint8_t status;
  790. trace_lsi_do_status(s->dbc, s->status);
  791. if (s->dbc != 1) {
  792. trace_lsi_do_status_error();
  793. }
  794. s->dbc = 1;
  795. status = s->status;
  796. s->sfbr = status;
  797. pci_dma_write(PCI_DEVICE(s), s->dnad, &status, 1);
  798. lsi_set_phase(s, PHASE_MI);
  799. s->msg_action = LSI_MSG_ACTION_DISCONNECT;
  800. lsi_add_msg_byte(s, 0); /* COMMAND COMPLETE */
  801. }
  802. static void lsi_do_msgin(LSIState *s)
  803. {
  804. uint8_t len;
  805. trace_lsi_do_msgin(s->dbc, s->msg_len);
  806. s->sfbr = s->msg[0];
  807. len = s->msg_len;
  808. assert(len > 0 && len <= LSI_MAX_MSGIN_LEN);
  809. if (len > s->dbc)
  810. len = s->dbc;
  811. pci_dma_write(PCI_DEVICE(s), s->dnad, s->msg, len);
  812. /* Linux drivers rely on the last byte being in the SIDL. */
  813. s->sidl = s->msg[len - 1];
  814. s->msg_len -= len;
  815. if (s->msg_len) {
  816. memmove(s->msg, s->msg + len, s->msg_len);
  817. } else {
  818. /* ??? Check if ATN (not yet implemented) is asserted and maybe
  819. switch to PHASE_MO. */
  820. switch (s->msg_action) {
  821. case LSI_MSG_ACTION_COMMAND:
  822. lsi_set_phase(s, PHASE_CMD);
  823. break;
  824. case LSI_MSG_ACTION_DISCONNECT:
  825. lsi_disconnect(s);
  826. break;
  827. case LSI_MSG_ACTION_DOUT:
  828. lsi_set_phase(s, PHASE_DO);
  829. break;
  830. case LSI_MSG_ACTION_DIN:
  831. lsi_set_phase(s, PHASE_DI);
  832. break;
  833. default:
  834. abort();
  835. }
  836. }
  837. }
  838. /* Read the next byte during a MSGOUT phase. */
  839. static uint8_t lsi_get_msgbyte(LSIState *s)
  840. {
  841. uint8_t data;
  842. pci_dma_read(PCI_DEVICE(s), s->dnad, &data, 1);
  843. s->dnad++;
  844. s->dbc--;
  845. return data;
  846. }
  847. /* Skip the next n bytes during a MSGOUT phase. */
  848. static void lsi_skip_msgbytes(LSIState *s, unsigned int n)
  849. {
  850. s->dnad += n;
  851. s->dbc -= n;
  852. }
  853. static void lsi_do_msgout(LSIState *s)
  854. {
  855. uint8_t msg;
  856. int len;
  857. uint32_t current_tag;
  858. lsi_request *current_req, *p, *p_next;
  859. if (s->current) {
  860. current_tag = s->current->tag;
  861. current_req = s->current;
  862. } else {
  863. current_tag = s->select_tag;
  864. current_req = lsi_find_by_tag(s, current_tag);
  865. }
  866. trace_lsi_do_msgout(s->dbc);
  867. while (s->dbc) {
  868. msg = lsi_get_msgbyte(s);
  869. s->sfbr = msg;
  870. switch (msg) {
  871. case 0x04:
  872. trace_lsi_do_msgout_disconnect();
  873. lsi_disconnect(s);
  874. break;
  875. case 0x08:
  876. trace_lsi_do_msgout_noop();
  877. lsi_set_phase(s, PHASE_CMD);
  878. break;
  879. case 0x01:
  880. len = lsi_get_msgbyte(s);
  881. msg = lsi_get_msgbyte(s);
  882. (void)len; /* avoid a warning about unused variable*/
  883. trace_lsi_do_msgout_extended(msg, len);
  884. switch (msg) {
  885. case 1:
  886. trace_lsi_do_msgout_ignored("SDTR");
  887. lsi_skip_msgbytes(s, 2);
  888. break;
  889. case 3:
  890. trace_lsi_do_msgout_ignored("WDTR");
  891. lsi_skip_msgbytes(s, 1);
  892. break;
  893. case 4:
  894. trace_lsi_do_msgout_ignored("PPR");
  895. lsi_skip_msgbytes(s, 5);
  896. break;
  897. default:
  898. goto bad;
  899. }
  900. break;
  901. case 0x20: /* SIMPLE queue */
  902. s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
  903. trace_lsi_do_msgout_simplequeue(s->select_tag & 0xff);
  904. break;
  905. case 0x21: /* HEAD of queue */
  906. qemu_log_mask(LOG_UNIMP, "lsi_scsi: HEAD queue not implemented\n");
  907. s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
  908. break;
  909. case 0x22: /* ORDERED queue */
  910. qemu_log_mask(LOG_UNIMP,
  911. "lsi_scsi: ORDERED queue not implemented\n");
  912. s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
  913. break;
  914. case 0x0d:
  915. /* The ABORT TAG message clears the current I/O process only. */
  916. trace_lsi_do_msgout_abort(current_tag);
  917. if (current_req && current_req->req) {
  918. scsi_req_cancel(current_req->req);
  919. current_req = NULL;
  920. }
  921. lsi_disconnect(s);
  922. break;
  923. case 0x06:
  924. case 0x0e:
  925. case 0x0c:
  926. /* The ABORT message clears all I/O processes for the selecting
  927. initiator on the specified logical unit of the target. */
  928. if (msg == 0x06) {
  929. trace_lsi_do_msgout_abort(current_tag);
  930. }
  931. /* The CLEAR QUEUE message clears all I/O processes for all
  932. initiators on the specified logical unit of the target. */
  933. if (msg == 0x0e) {
  934. trace_lsi_do_msgout_clearqueue(current_tag);
  935. }
  936. /* The BUS DEVICE RESET message clears all I/O processes for all
  937. initiators on all logical units of the target. */
  938. if (msg == 0x0c) {
  939. trace_lsi_do_msgout_busdevicereset(current_tag);
  940. }
  941. /* clear the current I/O process */
  942. if (s->current) {
  943. scsi_req_cancel(s->current->req);
  944. current_req = NULL;
  945. }
  946. /* As the current implemented devices scsi_disk and scsi_generic
  947. only support one LUN, we don't need to keep track of LUNs.
  948. Clearing I/O processes for other initiators could be possible
  949. for scsi_generic by sending a SG_SCSI_RESET to the /dev/sgX
  950. device, but this is currently not implemented (and seems not
  951. to be really necessary). So let's simply clear all queued
  952. commands for the current device: */
  953. QTAILQ_FOREACH_SAFE(p, &s->queue, next, p_next) {
  954. if ((p->tag & 0x0000ff00) == (current_tag & 0x0000ff00)) {
  955. scsi_req_cancel(p->req);
  956. }
  957. }
  958. lsi_disconnect(s);
  959. break;
  960. default:
  961. if ((msg & 0x80) == 0) {
  962. goto bad;
  963. }
  964. s->current_lun = msg & 7;
  965. trace_lsi_do_msgout_select(s->current_lun);
  966. lsi_set_phase(s, PHASE_CMD);
  967. break;
  968. }
  969. }
  970. return;
  971. bad:
  972. qemu_log_mask(LOG_UNIMP, "Unimplemented message 0x%02x\n", msg);
  973. lsi_set_phase(s, PHASE_MI);
  974. lsi_add_msg_byte(s, 7); /* MESSAGE REJECT */
  975. s->msg_action = LSI_MSG_ACTION_COMMAND;
  976. }
  977. #define LSI_BUF_SIZE 4096
  978. static void lsi_memcpy(LSIState *s, uint32_t dest, uint32_t src, int count)
  979. {
  980. int n;
  981. uint8_t buf[LSI_BUF_SIZE];
  982. trace_lsi_memcpy(dest, src, count);
  983. while (count) {
  984. n = (count > LSI_BUF_SIZE) ? LSI_BUF_SIZE : count;
  985. lsi_mem_read(s, src, buf, n);
  986. lsi_mem_write(s, dest, buf, n);
  987. src += n;
  988. dest += n;
  989. count -= n;
  990. }
  991. }
  992. static void lsi_wait_reselect(LSIState *s)
  993. {
  994. lsi_request *p;
  995. trace_lsi_wait_reselect();
  996. if (s->current) {
  997. return;
  998. }
  999. p = get_pending_req(s);
  1000. if (p) {
  1001. lsi_reselect(s, p);
  1002. }
  1003. if (s->current == NULL) {
  1004. s->waiting = LSI_WAIT_RESELECT;
  1005. }
  1006. }
  1007. static void lsi_execute_script(LSIState *s)
  1008. {
  1009. PCIDevice *pci_dev = PCI_DEVICE(s);
  1010. uint32_t insn;
  1011. uint32_t addr, addr_high;
  1012. int opcode;
  1013. int insn_processed = 0;
  1014. s->istat1 |= LSI_ISTAT1_SRUN;
  1015. again:
  1016. if (++insn_processed > LSI_MAX_INSN) {
  1017. /* Some windows drivers make the device spin waiting for a memory
  1018. location to change. If we have been executed a lot of code then
  1019. assume this is the case and force an unexpected device disconnect.
  1020. This is apparently sufficient to beat the drivers into submission.
  1021. */
  1022. if (!(s->sien0 & LSI_SIST0_UDC)) {
  1023. qemu_log_mask(LOG_GUEST_ERROR,
  1024. "lsi_scsi: inf. loop with UDC masked");
  1025. }
  1026. lsi_script_scsi_interrupt(s, LSI_SIST0_UDC, 0);
  1027. lsi_disconnect(s);
  1028. trace_lsi_execute_script_stop();
  1029. return;
  1030. }
  1031. insn = read_dword(s, s->dsp);
  1032. if (!insn) {
  1033. /* If we receive an empty opcode increment the DSP by 4 bytes
  1034. instead of 8 and execute the next opcode at that location */
  1035. s->dsp += 4;
  1036. goto again;
  1037. }
  1038. addr = read_dword(s, s->dsp + 4);
  1039. addr_high = 0;
  1040. trace_lsi_execute_script(s->dsp, insn, addr);
  1041. s->dsps = addr;
  1042. s->dcmd = insn >> 24;
  1043. s->dsp += 8;
  1044. switch (insn >> 30) {
  1045. case 0: /* Block move. */
  1046. if (s->sist1 & LSI_SIST1_STO) {
  1047. trace_lsi_execute_script_blockmove_delayed();
  1048. lsi_stop_script(s);
  1049. break;
  1050. }
  1051. s->dbc = insn & 0xffffff;
  1052. s->rbc = s->dbc;
  1053. /* ??? Set ESA. */
  1054. s->ia = s->dsp - 8;
  1055. if (insn & (1 << 29)) {
  1056. /* Indirect addressing. */
  1057. addr = read_dword(s, addr);
  1058. } else if (insn & (1 << 28)) {
  1059. uint32_t buf[2];
  1060. int32_t offset;
  1061. /* Table indirect addressing. */
  1062. /* 32-bit Table indirect */
  1063. offset = sextract32(addr, 0, 24);
  1064. pci_dma_read(pci_dev, s->dsa + offset, buf, 8);
  1065. /* byte count is stored in bits 0:23 only */
  1066. s->dbc = cpu_to_le32(buf[0]) & 0xffffff;
  1067. s->rbc = s->dbc;
  1068. addr = cpu_to_le32(buf[1]);
  1069. /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
  1070. * table, bits [31:24] */
  1071. if (lsi_dma_40bit(s))
  1072. addr_high = cpu_to_le32(buf[0]) >> 24;
  1073. else if (lsi_dma_ti64bit(s)) {
  1074. int selector = (cpu_to_le32(buf[0]) >> 24) & 0x1f;
  1075. switch (selector) {
  1076. case 0 ... 0x0f:
  1077. /* offset index into scratch registers since
  1078. * TI64 mode can use registers C to R */
  1079. addr_high = s->scratch[2 + selector];
  1080. break;
  1081. case 0x10:
  1082. addr_high = s->mmrs;
  1083. break;
  1084. case 0x11:
  1085. addr_high = s->mmws;
  1086. break;
  1087. case 0x12:
  1088. addr_high = s->sfs;
  1089. break;
  1090. case 0x13:
  1091. addr_high = s->drs;
  1092. break;
  1093. case 0x14:
  1094. addr_high = s->sbms;
  1095. break;
  1096. case 0x15:
  1097. addr_high = s->dbms;
  1098. break;
  1099. default:
  1100. qemu_log_mask(LOG_GUEST_ERROR,
  1101. "lsi_scsi: Illegal selector specified (0x%x > 0x15) "
  1102. "for 64-bit DMA block move", selector);
  1103. break;
  1104. }
  1105. }
  1106. } else if (lsi_dma_64bit(s)) {
  1107. /* fetch a 3rd dword if 64-bit direct move is enabled and
  1108. only if we're not doing table indirect or indirect addressing */
  1109. s->dbms = read_dword(s, s->dsp);
  1110. s->dsp += 4;
  1111. s->ia = s->dsp - 12;
  1112. }
  1113. if ((s->sstat1 & PHASE_MASK) != ((insn >> 24) & 7)) {
  1114. trace_lsi_execute_script_blockmove_badphase(
  1115. scsi_phase_name(s->sstat1),
  1116. scsi_phase_name(insn >> 24));
  1117. lsi_script_scsi_interrupt(s, LSI_SIST0_MA, 0);
  1118. break;
  1119. }
  1120. s->dnad = addr;
  1121. s->dnad64 = addr_high;
  1122. switch (s->sstat1 & 0x7) {
  1123. case PHASE_DO:
  1124. s->waiting = LSI_DMA_SCRIPTS;
  1125. lsi_do_dma(s, 1);
  1126. if (s->waiting)
  1127. s->waiting = LSI_DMA_IN_PROGRESS;
  1128. break;
  1129. case PHASE_DI:
  1130. s->waiting = LSI_DMA_SCRIPTS;
  1131. lsi_do_dma(s, 0);
  1132. if (s->waiting)
  1133. s->waiting = LSI_DMA_IN_PROGRESS;
  1134. break;
  1135. case PHASE_CMD:
  1136. lsi_do_command(s);
  1137. break;
  1138. case PHASE_ST:
  1139. lsi_do_status(s);
  1140. break;
  1141. case PHASE_MO:
  1142. lsi_do_msgout(s);
  1143. break;
  1144. case PHASE_MI:
  1145. lsi_do_msgin(s);
  1146. break;
  1147. default:
  1148. qemu_log_mask(LOG_UNIMP, "lsi_scsi: Unimplemented phase %s\n",
  1149. scsi_phase_name(s->sstat1));
  1150. }
  1151. s->dfifo = s->dbc & 0xff;
  1152. s->ctest5 = (s->ctest5 & 0xfc) | ((s->dbc >> 8) & 3);
  1153. s->sbc = s->dbc;
  1154. s->rbc -= s->dbc;
  1155. s->ua = addr + s->dbc;
  1156. break;
  1157. case 1: /* IO or Read/Write instruction. */
  1158. opcode = (insn >> 27) & 7;
  1159. if (opcode < 5) {
  1160. uint32_t id;
  1161. if (insn & (1 << 25)) {
  1162. id = read_dword(s, s->dsa + sextract32(insn, 0, 24));
  1163. } else {
  1164. id = insn;
  1165. }
  1166. id = (id >> 16) & 0xf;
  1167. if (insn & (1 << 26)) {
  1168. addr = s->dsp + sextract32(addr, 0, 24);
  1169. }
  1170. s->dnad = addr;
  1171. switch (opcode) {
  1172. case 0: /* Select */
  1173. s->sdid = id;
  1174. if (s->scntl1 & LSI_SCNTL1_CON) {
  1175. trace_lsi_execute_script_io_alreadyreselected();
  1176. s->dsp = s->dnad;
  1177. break;
  1178. }
  1179. s->sstat0 |= LSI_SSTAT0_WOA;
  1180. s->scntl1 &= ~LSI_SCNTL1_IARB;
  1181. if (!scsi_device_find(&s->bus, 0, id, 0)) {
  1182. lsi_bad_selection(s, id);
  1183. break;
  1184. }
  1185. trace_lsi_execute_script_io_selected(id,
  1186. insn & (1 << 3) ? " ATN" : "");
  1187. /* ??? Linux drivers compain when this is set. Maybe
  1188. it only applies in low-level mode (unimplemented).
  1189. lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
  1190. s->select_tag = id << 8;
  1191. s->scntl1 |= LSI_SCNTL1_CON;
  1192. if (insn & (1 << 3)) {
  1193. s->socl |= LSI_SOCL_ATN;
  1194. s->sbcl |= LSI_SBCL_ATN;
  1195. }
  1196. s->sbcl |= LSI_SBCL_BSY;
  1197. lsi_set_phase(s, PHASE_MO);
  1198. s->waiting = LSI_NOWAIT;
  1199. break;
  1200. case 1: /* Disconnect */
  1201. trace_lsi_execute_script_io_disconnect();
  1202. s->scntl1 &= ~LSI_SCNTL1_CON;
  1203. /* FIXME: this is not entirely correct; the target need not ask
  1204. * for reselection until it has to send data, while here we force a
  1205. * reselection as soon as the bus is free. The correct flow would
  1206. * reselect before lsi_transfer_data and disconnect as soon as
  1207. * DMA ends.
  1208. */
  1209. if (!s->current) {
  1210. lsi_request *p = get_pending_req(s);
  1211. if (p) {
  1212. lsi_reselect(s, p);
  1213. }
  1214. }
  1215. break;
  1216. case 2: /* Wait Reselect */
  1217. if (s->istat0 & LSI_ISTAT0_SIGP) {
  1218. s->dsp = s->dnad;
  1219. } else if (!lsi_irq_on_rsl(s)) {
  1220. lsi_wait_reselect(s);
  1221. }
  1222. break;
  1223. case 3: /* Set */
  1224. trace_lsi_execute_script_io_set(
  1225. insn & (1 << 3) ? " ATN" : "",
  1226. insn & (1 << 6) ? " ACK" : "",
  1227. insn & (1 << 9) ? " TM" : "",
  1228. insn & (1 << 10) ? " CC" : "");
  1229. if (insn & (1 << 3)) {
  1230. s->socl |= LSI_SOCL_ATN;
  1231. s->sbcl |= LSI_SBCL_ATN;
  1232. lsi_set_phase(s, PHASE_MO);
  1233. }
  1234. if (insn & (1 << 6)) {
  1235. s->sbcl |= LSI_SBCL_ACK;
  1236. }
  1237. if (insn & (1 << 9)) {
  1238. qemu_log_mask(LOG_UNIMP,
  1239. "lsi_scsi: Target mode not implemented\n");
  1240. }
  1241. if (insn & (1 << 10))
  1242. s->carry = 1;
  1243. break;
  1244. case 4: /* Clear */
  1245. trace_lsi_execute_script_io_clear(
  1246. insn & (1 << 3) ? " ATN" : "",
  1247. insn & (1 << 6) ? " ACK" : "",
  1248. insn & (1 << 9) ? " TM" : "",
  1249. insn & (1 << 10) ? " CC" : "");
  1250. if (insn & (1 << 3)) {
  1251. s->socl &= ~LSI_SOCL_ATN;
  1252. s->sbcl &= ~LSI_SBCL_ATN;
  1253. }
  1254. if (insn & (1 << 6)) {
  1255. s->sbcl &= ~LSI_SBCL_ACK;
  1256. }
  1257. if (insn & (1 << 10))
  1258. s->carry = 0;
  1259. break;
  1260. }
  1261. } else {
  1262. uint8_t op0;
  1263. uint8_t op1;
  1264. uint8_t data8;
  1265. int reg;
  1266. int operator;
  1267. static const char *opcode_names[3] =
  1268. {"Write", "Read", "Read-Modify-Write"};
  1269. static const char *operator_names[8] =
  1270. {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
  1271. reg = ((insn >> 16) & 0x7f) | (insn & 0x80);
  1272. data8 = (insn >> 8) & 0xff;
  1273. opcode = (insn >> 27) & 7;
  1274. operator = (insn >> 24) & 7;
  1275. trace_lsi_execute_script_io_opcode(
  1276. opcode_names[opcode - 5], reg,
  1277. operator_names[operator], data8, s->sfbr,
  1278. (insn & (1 << 23)) ? " SFBR" : "");
  1279. op0 = op1 = 0;
  1280. switch (opcode) {
  1281. case 5: /* From SFBR */
  1282. op0 = s->sfbr;
  1283. op1 = data8;
  1284. break;
  1285. case 6: /* To SFBR */
  1286. if (operator)
  1287. op0 = lsi_reg_readb(s, reg);
  1288. op1 = data8;
  1289. break;
  1290. case 7: /* Read-modify-write */
  1291. if (operator)
  1292. op0 = lsi_reg_readb(s, reg);
  1293. if (insn & (1 << 23)) {
  1294. op1 = s->sfbr;
  1295. } else {
  1296. op1 = data8;
  1297. }
  1298. break;
  1299. }
  1300. switch (operator) {
  1301. case 0: /* move */
  1302. op0 = op1;
  1303. break;
  1304. case 1: /* Shift left */
  1305. op1 = op0 >> 7;
  1306. op0 = (op0 << 1) | s->carry;
  1307. s->carry = op1;
  1308. break;
  1309. case 2: /* OR */
  1310. op0 |= op1;
  1311. break;
  1312. case 3: /* XOR */
  1313. op0 ^= op1;
  1314. break;
  1315. case 4: /* AND */
  1316. op0 &= op1;
  1317. break;
  1318. case 5: /* SHR */
  1319. op1 = op0 & 1;
  1320. op0 = (op0 >> 1) | (s->carry << 7);
  1321. s->carry = op1;
  1322. break;
  1323. case 6: /* ADD */
  1324. op0 += op1;
  1325. s->carry = op0 < op1;
  1326. break;
  1327. case 7: /* ADC */
  1328. op0 += op1 + s->carry;
  1329. if (s->carry)
  1330. s->carry = op0 <= op1;
  1331. else
  1332. s->carry = op0 < op1;
  1333. break;
  1334. }
  1335. switch (opcode) {
  1336. case 5: /* From SFBR */
  1337. case 7: /* Read-modify-write */
  1338. lsi_reg_writeb(s, reg, op0);
  1339. break;
  1340. case 6: /* To SFBR */
  1341. s->sfbr = op0;
  1342. break;
  1343. }
  1344. }
  1345. break;
  1346. case 2: /* Transfer Control. */
  1347. {
  1348. int cond;
  1349. int jmp;
  1350. if ((insn & 0x002e0000) == 0) {
  1351. trace_lsi_execute_script_tc_nop();
  1352. break;
  1353. }
  1354. if (s->sist1 & LSI_SIST1_STO) {
  1355. trace_lsi_execute_script_tc_delayedselect_timeout();
  1356. lsi_stop_script(s);
  1357. break;
  1358. }
  1359. cond = jmp = (insn & (1 << 19)) != 0;
  1360. if (cond == jmp && (insn & (1 << 21))) {
  1361. trace_lsi_execute_script_tc_compc(s->carry == jmp);
  1362. cond = s->carry != 0;
  1363. }
  1364. if (cond == jmp && (insn & (1 << 17))) {
  1365. trace_lsi_execute_script_tc_compp(scsi_phase_name(s->sstat1),
  1366. jmp ? '=' : '!', scsi_phase_name(insn >> 24));
  1367. cond = (s->sstat1 & PHASE_MASK) == ((insn >> 24) & 7);
  1368. }
  1369. if (cond == jmp && (insn & (1 << 18))) {
  1370. uint8_t mask;
  1371. mask = (~insn >> 8) & 0xff;
  1372. trace_lsi_execute_script_tc_compd(
  1373. s->sfbr, mask, jmp ? '=' : '!', insn & mask);
  1374. cond = (s->sfbr & mask) == (insn & mask);
  1375. }
  1376. if (cond == jmp) {
  1377. if (insn & (1 << 23)) {
  1378. /* Relative address. */
  1379. addr = s->dsp + sextract32(addr, 0, 24);
  1380. }
  1381. switch ((insn >> 27) & 7) {
  1382. case 0: /* Jump */
  1383. trace_lsi_execute_script_tc_jump(addr);
  1384. s->adder = addr;
  1385. s->dsp = addr;
  1386. break;
  1387. case 1: /* Call */
  1388. trace_lsi_execute_script_tc_call(addr);
  1389. s->temp = s->dsp;
  1390. s->dsp = addr;
  1391. break;
  1392. case 2: /* Return */
  1393. trace_lsi_execute_script_tc_return(s->temp);
  1394. s->dsp = s->temp;
  1395. break;
  1396. case 3: /* Interrupt */
  1397. trace_lsi_execute_script_tc_interrupt(s->dsps);
  1398. if ((insn & (1 << 20)) != 0) {
  1399. s->istat0 |= LSI_ISTAT0_INTF;
  1400. lsi_update_irq(s);
  1401. } else {
  1402. lsi_script_dma_interrupt(s, LSI_DSTAT_SIR);
  1403. }
  1404. break;
  1405. default:
  1406. trace_lsi_execute_script_tc_illegal();
  1407. lsi_script_dma_interrupt(s, LSI_DSTAT_IID);
  1408. break;
  1409. }
  1410. } else {
  1411. trace_lsi_execute_script_tc_cc_failed();
  1412. }
  1413. }
  1414. break;
  1415. case 3:
  1416. if ((insn & (1 << 29)) == 0) {
  1417. /* Memory move. */
  1418. uint32_t dest;
  1419. /* ??? The docs imply the destination address is loaded into
  1420. the TEMP register. However the Linux drivers rely on
  1421. the value being presrved. */
  1422. dest = read_dword(s, s->dsp);
  1423. s->dsp += 4;
  1424. lsi_memcpy(s, dest, addr, insn & 0xffffff);
  1425. } else {
  1426. uint8_t data[7];
  1427. int reg;
  1428. int n;
  1429. int i;
  1430. if (insn & (1 << 28)) {
  1431. addr = s->dsa + sextract32(addr, 0, 24);
  1432. }
  1433. n = (insn & 7);
  1434. reg = (insn >> 16) & 0xff;
  1435. if (insn & (1 << 24)) {
  1436. pci_dma_read(pci_dev, addr, data, n);
  1437. trace_lsi_execute_script_mm_load(reg, n, addr, *(int *)data);
  1438. for (i = 0; i < n; i++) {
  1439. lsi_reg_writeb(s, reg + i, data[i]);
  1440. }
  1441. } else {
  1442. trace_lsi_execute_script_mm_store(reg, n, addr);
  1443. for (i = 0; i < n; i++) {
  1444. data[i] = lsi_reg_readb(s, reg + i);
  1445. }
  1446. pci_dma_write(pci_dev, addr, data, n);
  1447. }
  1448. }
  1449. }
  1450. if (s->istat1 & LSI_ISTAT1_SRUN && s->waiting == LSI_NOWAIT) {
  1451. if (s->dcntl & LSI_DCNTL_SSM) {
  1452. lsi_script_dma_interrupt(s, LSI_DSTAT_SSI);
  1453. } else {
  1454. goto again;
  1455. }
  1456. }
  1457. trace_lsi_execute_script_stop();
  1458. }
  1459. static uint8_t lsi_reg_readb(LSIState *s, int offset)
  1460. {
  1461. uint8_t ret;
  1462. #define CASE_GET_REG24(name, addr) \
  1463. case addr: ret = s->name & 0xff; break; \
  1464. case addr + 1: ret = (s->name >> 8) & 0xff; break; \
  1465. case addr + 2: ret = (s->name >> 16) & 0xff; break;
  1466. #define CASE_GET_REG32(name, addr) \
  1467. case addr: ret = s->name & 0xff; break; \
  1468. case addr + 1: ret = (s->name >> 8) & 0xff; break; \
  1469. case addr + 2: ret = (s->name >> 16) & 0xff; break; \
  1470. case addr + 3: ret = (s->name >> 24) & 0xff; break;
  1471. switch (offset) {
  1472. case 0x00: /* SCNTL0 */
  1473. ret = s->scntl0;
  1474. break;
  1475. case 0x01: /* SCNTL1 */
  1476. ret = s->scntl1;
  1477. break;
  1478. case 0x02: /* SCNTL2 */
  1479. ret = s->scntl2;
  1480. break;
  1481. case 0x03: /* SCNTL3 */
  1482. ret = s->scntl3;
  1483. break;
  1484. case 0x04: /* SCID */
  1485. ret = s->scid;
  1486. break;
  1487. case 0x05: /* SXFER */
  1488. ret = s->sxfer;
  1489. break;
  1490. case 0x06: /* SDID */
  1491. ret = s->sdid;
  1492. break;
  1493. case 0x07: /* GPREG0 */
  1494. ret = 0x7f;
  1495. break;
  1496. case 0x08: /* Revision ID */
  1497. ret = 0x00;
  1498. break;
  1499. case 0x09: /* SOCL */
  1500. ret = s->socl;
  1501. break;
  1502. case 0xa: /* SSID */
  1503. ret = s->ssid;
  1504. break;
  1505. case 0xb: /* SBCL */
  1506. ret = s->sbcl;
  1507. break;
  1508. case 0xc: /* DSTAT */
  1509. ret = s->dstat | LSI_DSTAT_DFE;
  1510. if ((s->istat0 & LSI_ISTAT0_INTF) == 0)
  1511. s->dstat = 0;
  1512. lsi_update_irq(s);
  1513. break;
  1514. case 0x0d: /* SSTAT0 */
  1515. ret = s->sstat0;
  1516. break;
  1517. case 0x0e: /* SSTAT1 */
  1518. ret = s->sstat1;
  1519. break;
  1520. case 0x0f: /* SSTAT2 */
  1521. ret = s->scntl1 & LSI_SCNTL1_CON ? 0 : 2;
  1522. break;
  1523. CASE_GET_REG32(dsa, 0x10)
  1524. case 0x14: /* ISTAT0 */
  1525. ret = s->istat0;
  1526. break;
  1527. case 0x15: /* ISTAT1 */
  1528. ret = s->istat1;
  1529. break;
  1530. case 0x16: /* MBOX0 */
  1531. ret = s->mbox0;
  1532. break;
  1533. case 0x17: /* MBOX1 */
  1534. ret = s->mbox1;
  1535. break;
  1536. case 0x18: /* CTEST0 */
  1537. ret = 0xff;
  1538. break;
  1539. case 0x19: /* CTEST1 */
  1540. ret = 0;
  1541. break;
  1542. case 0x1a: /* CTEST2 */
  1543. ret = s->ctest2 | LSI_CTEST2_DACK | LSI_CTEST2_CM;
  1544. if (s->istat0 & LSI_ISTAT0_SIGP) {
  1545. s->istat0 &= ~LSI_ISTAT0_SIGP;
  1546. ret |= LSI_CTEST2_SIGP;
  1547. }
  1548. break;
  1549. case 0x1b: /* CTEST3 */
  1550. ret = s->ctest3;
  1551. break;
  1552. CASE_GET_REG32(temp, 0x1c)
  1553. case 0x20: /* DFIFO */
  1554. ret = s->dfifo;
  1555. break;
  1556. case 0x21: /* CTEST4 */
  1557. ret = s->ctest4;
  1558. break;
  1559. case 0x22: /* CTEST5 */
  1560. ret = s->ctest5;
  1561. break;
  1562. case 0x23: /* CTEST6 */
  1563. ret = 0;
  1564. break;
  1565. CASE_GET_REG24(dbc, 0x24)
  1566. case 0x27: /* DCMD */
  1567. ret = s->dcmd;
  1568. break;
  1569. CASE_GET_REG32(dnad, 0x28)
  1570. CASE_GET_REG32(dsp, 0x2c)
  1571. CASE_GET_REG32(dsps, 0x30)
  1572. CASE_GET_REG32(scratch[0], 0x34)
  1573. case 0x38: /* DMODE */
  1574. ret = s->dmode;
  1575. break;
  1576. case 0x39: /* DIEN */
  1577. ret = s->dien;
  1578. break;
  1579. case 0x3a: /* SBR */
  1580. ret = s->sbr;
  1581. break;
  1582. case 0x3b: /* DCNTL */
  1583. ret = s->dcntl;
  1584. break;
  1585. /* ADDER Output (Debug of relative jump address) */
  1586. CASE_GET_REG32(adder, 0x3c)
  1587. case 0x40: /* SIEN0 */
  1588. ret = s->sien0;
  1589. break;
  1590. case 0x41: /* SIEN1 */
  1591. ret = s->sien1;
  1592. break;
  1593. case 0x42: /* SIST0 */
  1594. ret = s->sist0;
  1595. s->sist0 = 0;
  1596. lsi_update_irq(s);
  1597. break;
  1598. case 0x43: /* SIST1 */
  1599. ret = s->sist1;
  1600. s->sist1 = 0;
  1601. lsi_update_irq(s);
  1602. break;
  1603. case 0x46: /* MACNTL */
  1604. ret = 0x0f;
  1605. break;
  1606. case 0x47: /* GPCNTL0 */
  1607. ret = 0x0f;
  1608. break;
  1609. case 0x48: /* STIME0 */
  1610. ret = s->stime0;
  1611. break;
  1612. case 0x4a: /* RESPID0 */
  1613. ret = s->respid0;
  1614. break;
  1615. case 0x4b: /* RESPID1 */
  1616. ret = s->respid1;
  1617. break;
  1618. case 0x4d: /* STEST1 */
  1619. ret = s->stest1;
  1620. break;
  1621. case 0x4e: /* STEST2 */
  1622. ret = s->stest2;
  1623. break;
  1624. case 0x4f: /* STEST3 */
  1625. ret = s->stest3;
  1626. break;
  1627. case 0x50: /* SIDL */
  1628. /* This is needed by the linux drivers. We currently only update it
  1629. during the MSG IN phase. */
  1630. ret = s->sidl;
  1631. break;
  1632. case 0x52: /* STEST4 */
  1633. ret = 0xe0;
  1634. break;
  1635. case 0x56: /* CCNTL0 */
  1636. ret = s->ccntl0;
  1637. break;
  1638. case 0x57: /* CCNTL1 */
  1639. ret = s->ccntl1;
  1640. break;
  1641. case 0x58: /* SBDL */
  1642. /* Some drivers peek at the data bus during the MSG IN phase. */
  1643. if ((s->sstat1 & PHASE_MASK) == PHASE_MI) {
  1644. assert(s->msg_len > 0);
  1645. return s->msg[0];
  1646. }
  1647. ret = 0;
  1648. break;
  1649. case 0x59: /* SBDL high */
  1650. ret = 0;
  1651. break;
  1652. CASE_GET_REG32(mmrs, 0xa0)
  1653. CASE_GET_REG32(mmws, 0xa4)
  1654. CASE_GET_REG32(sfs, 0xa8)
  1655. CASE_GET_REG32(drs, 0xac)
  1656. CASE_GET_REG32(sbms, 0xb0)
  1657. CASE_GET_REG32(dbms, 0xb4)
  1658. CASE_GET_REG32(dnad64, 0xb8)
  1659. CASE_GET_REG32(pmjad1, 0xc0)
  1660. CASE_GET_REG32(pmjad2, 0xc4)
  1661. CASE_GET_REG32(rbc, 0xc8)
  1662. CASE_GET_REG32(ua, 0xcc)
  1663. CASE_GET_REG32(ia, 0xd4)
  1664. CASE_GET_REG32(sbc, 0xd8)
  1665. CASE_GET_REG32(csbc, 0xdc)
  1666. case 0x5c ... 0x9f:
  1667. {
  1668. int n;
  1669. int shift;
  1670. n = (offset - 0x58) >> 2;
  1671. shift = (offset & 3) * 8;
  1672. ret = (s->scratch[n] >> shift) & 0xff;
  1673. break;
  1674. }
  1675. default:
  1676. {
  1677. qemu_log_mask(LOG_GUEST_ERROR,
  1678. "lsi_scsi: invalid read from reg %s %x\n",
  1679. offset < ARRAY_SIZE(names) ? names[offset] : "???",
  1680. offset);
  1681. ret = 0xff;
  1682. break;
  1683. }
  1684. }
  1685. #undef CASE_GET_REG24
  1686. #undef CASE_GET_REG32
  1687. trace_lsi_reg_read(offset < ARRAY_SIZE(names) ? names[offset] : "???",
  1688. offset, ret);
  1689. return ret;
  1690. }
  1691. static void lsi_reg_writeb(LSIState *s, int offset, uint8_t val)
  1692. {
  1693. #define CASE_SET_REG24(name, addr) \
  1694. case addr : s->name &= 0xffffff00; s->name |= val; break; \
  1695. case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
  1696. case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
  1697. #define CASE_SET_REG32(name, addr) \
  1698. case addr : s->name &= 0xffffff00; s->name |= val; break; \
  1699. case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
  1700. case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
  1701. case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
  1702. trace_lsi_reg_write(offset < ARRAY_SIZE(names) ? names[offset] : "???",
  1703. offset, val);
  1704. switch (offset) {
  1705. case 0x00: /* SCNTL0 */
  1706. s->scntl0 = val;
  1707. if (val & LSI_SCNTL0_START) {
  1708. qemu_log_mask(LOG_UNIMP,
  1709. "lsi_scsi: Start sequence not implemented\n");
  1710. }
  1711. break;
  1712. case 0x01: /* SCNTL1 */
  1713. s->scntl1 = val & ~LSI_SCNTL1_SST;
  1714. if (val & LSI_SCNTL1_IARB) {
  1715. qemu_log_mask(LOG_UNIMP,
  1716. "lsi_scsi: Immediate Arbritration not implemented\n");
  1717. }
  1718. if (val & LSI_SCNTL1_RST) {
  1719. if (!(s->sstat0 & LSI_SSTAT0_RST)) {
  1720. bus_cold_reset(BUS(&s->bus));
  1721. s->sstat0 |= LSI_SSTAT0_RST;
  1722. lsi_script_scsi_interrupt(s, LSI_SIST0_RST, 0);
  1723. }
  1724. } else {
  1725. s->sstat0 &= ~LSI_SSTAT0_RST;
  1726. }
  1727. break;
  1728. case 0x02: /* SCNTL2 */
  1729. val &= ~(LSI_SCNTL2_WSR | LSI_SCNTL2_WSS);
  1730. s->scntl2 = val;
  1731. break;
  1732. case 0x03: /* SCNTL3 */
  1733. s->scntl3 = val;
  1734. break;
  1735. case 0x04: /* SCID */
  1736. s->scid = val;
  1737. break;
  1738. case 0x05: /* SXFER */
  1739. s->sxfer = val;
  1740. break;
  1741. case 0x06: /* SDID */
  1742. if ((s->ssid & 0x80) && (val & 0xf) != (s->ssid & 0xf)) {
  1743. qemu_log_mask(LOG_GUEST_ERROR,
  1744. "lsi_scsi: Destination ID does not match SSID\n");
  1745. }
  1746. s->sdid = val & 0xf;
  1747. break;
  1748. case 0x07: /* GPREG0 */
  1749. break;
  1750. case 0x08: /* SFBR */
  1751. /* The CPU is not allowed to write to this register. However the
  1752. SCRIPTS register move instructions are. */
  1753. s->sfbr = val;
  1754. break;
  1755. case 0x0a: case 0x0b:
  1756. /* Openserver writes to these readonly registers on startup */
  1757. return;
  1758. case 0x0c: case 0x0d: case 0x0e: case 0x0f:
  1759. /* Linux writes to these readonly registers on startup. */
  1760. return;
  1761. CASE_SET_REG32(dsa, 0x10)
  1762. case 0x14: /* ISTAT0 */
  1763. s->istat0 = (s->istat0 & 0x0f) | (val & 0xf0);
  1764. if (val & LSI_ISTAT0_ABRT) {
  1765. lsi_script_dma_interrupt(s, LSI_DSTAT_ABRT);
  1766. }
  1767. if (val & LSI_ISTAT0_INTF) {
  1768. s->istat0 &= ~LSI_ISTAT0_INTF;
  1769. lsi_update_irq(s);
  1770. }
  1771. if (s->waiting == LSI_WAIT_RESELECT && val & LSI_ISTAT0_SIGP) {
  1772. trace_lsi_awoken();
  1773. s->waiting = LSI_NOWAIT;
  1774. s->dsp = s->dnad;
  1775. lsi_execute_script(s);
  1776. }
  1777. if (val & LSI_ISTAT0_SRST) {
  1778. device_cold_reset(DEVICE(s));
  1779. }
  1780. break;
  1781. case 0x16: /* MBOX0 */
  1782. s->mbox0 = val;
  1783. break;
  1784. case 0x17: /* MBOX1 */
  1785. s->mbox1 = val;
  1786. break;
  1787. case 0x18: /* CTEST0 */
  1788. /* nothing to do */
  1789. break;
  1790. case 0x1a: /* CTEST2 */
  1791. s->ctest2 = val & LSI_CTEST2_PCICIE;
  1792. break;
  1793. case 0x1b: /* CTEST3 */
  1794. s->ctest3 = val & 0x0f;
  1795. break;
  1796. CASE_SET_REG32(temp, 0x1c)
  1797. case 0x21: /* CTEST4 */
  1798. if (val & 7) {
  1799. qemu_log_mask(LOG_UNIMP,
  1800. "lsi_scsi: Unimplemented CTEST4-FBL 0x%x\n", val);
  1801. }
  1802. s->ctest4 = val;
  1803. break;
  1804. case 0x22: /* CTEST5 */
  1805. if (val & (LSI_CTEST5_ADCK | LSI_CTEST5_BBCK)) {
  1806. qemu_log_mask(LOG_UNIMP,
  1807. "lsi_scsi: CTEST5 DMA increment not implemented\n");
  1808. }
  1809. s->ctest5 = val;
  1810. break;
  1811. CASE_SET_REG24(dbc, 0x24)
  1812. CASE_SET_REG32(dnad, 0x28)
  1813. case 0x2c: /* DSP[0:7] */
  1814. s->dsp &= 0xffffff00;
  1815. s->dsp |= val;
  1816. break;
  1817. case 0x2d: /* DSP[8:15] */
  1818. s->dsp &= 0xffff00ff;
  1819. s->dsp |= val << 8;
  1820. break;
  1821. case 0x2e: /* DSP[16:23] */
  1822. s->dsp &= 0xff00ffff;
  1823. s->dsp |= val << 16;
  1824. break;
  1825. case 0x2f: /* DSP[24:31] */
  1826. s->dsp &= 0x00ffffff;
  1827. s->dsp |= val << 24;
  1828. /*
  1829. * FIXME: if s->waiting != LSI_NOWAIT, this will only execute one
  1830. * instruction. Is this correct?
  1831. */
  1832. if ((s->dmode & LSI_DMODE_MAN) == 0
  1833. && (s->istat1 & LSI_ISTAT1_SRUN) == 0)
  1834. lsi_execute_script(s);
  1835. break;
  1836. CASE_SET_REG32(dsps, 0x30)
  1837. CASE_SET_REG32(scratch[0], 0x34)
  1838. case 0x38: /* DMODE */
  1839. s->dmode = val;
  1840. break;
  1841. case 0x39: /* DIEN */
  1842. s->dien = val;
  1843. lsi_update_irq(s);
  1844. break;
  1845. case 0x3a: /* SBR */
  1846. s->sbr = val;
  1847. break;
  1848. case 0x3b: /* DCNTL */
  1849. s->dcntl = val & ~(LSI_DCNTL_PFF | LSI_DCNTL_STD);
  1850. /*
  1851. * FIXME: if s->waiting != LSI_NOWAIT, this will only execute one
  1852. * instruction. Is this correct?
  1853. */
  1854. if ((val & LSI_DCNTL_STD) && (s->istat1 & LSI_ISTAT1_SRUN) == 0)
  1855. lsi_execute_script(s);
  1856. break;
  1857. case 0x40: /* SIEN0 */
  1858. s->sien0 = val;
  1859. lsi_update_irq(s);
  1860. break;
  1861. case 0x41: /* SIEN1 */
  1862. s->sien1 = val;
  1863. lsi_update_irq(s);
  1864. break;
  1865. case 0x47: /* GPCNTL0 */
  1866. break;
  1867. case 0x48: /* STIME0 */
  1868. s->stime0 = val;
  1869. break;
  1870. case 0x49: /* STIME1 */
  1871. if (val & 0xf) {
  1872. qemu_log_mask(LOG_UNIMP,
  1873. "lsi_scsi: General purpose timer not implemented\n");
  1874. /* ??? Raising the interrupt immediately seems to be sufficient
  1875. to keep the FreeBSD driver happy. */
  1876. lsi_script_scsi_interrupt(s, 0, LSI_SIST1_GEN);
  1877. }
  1878. break;
  1879. case 0x4a: /* RESPID0 */
  1880. s->respid0 = val;
  1881. break;
  1882. case 0x4b: /* RESPID1 */
  1883. s->respid1 = val;
  1884. break;
  1885. case 0x4d: /* STEST1 */
  1886. s->stest1 = val;
  1887. break;
  1888. case 0x4e: /* STEST2 */
  1889. if (val & 1) {
  1890. qemu_log_mask(LOG_UNIMP,
  1891. "lsi_scsi: Low level mode not implemented\n");
  1892. }
  1893. s->stest2 = val;
  1894. break;
  1895. case 0x4f: /* STEST3 */
  1896. if (val & 0x41) {
  1897. qemu_log_mask(LOG_UNIMP,
  1898. "lsi_scsi: SCSI FIFO test mode not implemented\n");
  1899. }
  1900. s->stest3 = val;
  1901. break;
  1902. case 0x56: /* CCNTL0 */
  1903. s->ccntl0 = val;
  1904. break;
  1905. case 0x57: /* CCNTL1 */
  1906. s->ccntl1 = val;
  1907. break;
  1908. CASE_SET_REG32(mmrs, 0xa0)
  1909. CASE_SET_REG32(mmws, 0xa4)
  1910. CASE_SET_REG32(sfs, 0xa8)
  1911. CASE_SET_REG32(drs, 0xac)
  1912. CASE_SET_REG32(sbms, 0xb0)
  1913. CASE_SET_REG32(dbms, 0xb4)
  1914. CASE_SET_REG32(dnad64, 0xb8)
  1915. CASE_SET_REG32(pmjad1, 0xc0)
  1916. CASE_SET_REG32(pmjad2, 0xc4)
  1917. CASE_SET_REG32(rbc, 0xc8)
  1918. CASE_SET_REG32(ua, 0xcc)
  1919. CASE_SET_REG32(ia, 0xd4)
  1920. CASE_SET_REG32(sbc, 0xd8)
  1921. CASE_SET_REG32(csbc, 0xdc)
  1922. default:
  1923. if (offset >= 0x5c && offset < 0xa0) {
  1924. int n;
  1925. int shift;
  1926. n = (offset - 0x58) >> 2;
  1927. shift = (offset & 3) * 8;
  1928. s->scratch[n] = deposit32(s->scratch[n], shift, 8, val);
  1929. } else {
  1930. qemu_log_mask(LOG_GUEST_ERROR,
  1931. "lsi_scsi: invalid write to reg %s %x (0x%02x)\n",
  1932. offset < ARRAY_SIZE(names) ? names[offset] : "???",
  1933. offset, val);
  1934. }
  1935. }
  1936. #undef CASE_SET_REG24
  1937. #undef CASE_SET_REG32
  1938. }
  1939. static void lsi_mmio_write(void *opaque, hwaddr addr,
  1940. uint64_t val, unsigned size)
  1941. {
  1942. LSIState *s = opaque;
  1943. lsi_reg_writeb(s, addr & 0xff, val);
  1944. }
  1945. static uint64_t lsi_mmio_read(void *opaque, hwaddr addr,
  1946. unsigned size)
  1947. {
  1948. LSIState *s = opaque;
  1949. return lsi_reg_readb(s, addr & 0xff);
  1950. }
  1951. static const MemoryRegionOps lsi_mmio_ops = {
  1952. .read = lsi_mmio_read,
  1953. .write = lsi_mmio_write,
  1954. .endianness = DEVICE_LITTLE_ENDIAN,
  1955. .impl = {
  1956. .min_access_size = 1,
  1957. .max_access_size = 1,
  1958. },
  1959. };
  1960. static void lsi_ram_write(void *opaque, hwaddr addr,
  1961. uint64_t val, unsigned size)
  1962. {
  1963. LSIState *s = opaque;
  1964. stn_le_p(s->script_ram + addr, size, val);
  1965. }
  1966. static uint64_t lsi_ram_read(void *opaque, hwaddr addr,
  1967. unsigned size)
  1968. {
  1969. LSIState *s = opaque;
  1970. return ldn_le_p(s->script_ram + addr, size);
  1971. }
  1972. static const MemoryRegionOps lsi_ram_ops = {
  1973. .read = lsi_ram_read,
  1974. .write = lsi_ram_write,
  1975. .endianness = DEVICE_LITTLE_ENDIAN,
  1976. };
  1977. static uint64_t lsi_io_read(void *opaque, hwaddr addr,
  1978. unsigned size)
  1979. {
  1980. LSIState *s = opaque;
  1981. return lsi_reg_readb(s, addr & 0xff);
  1982. }
  1983. static void lsi_io_write(void *opaque, hwaddr addr,
  1984. uint64_t val, unsigned size)
  1985. {
  1986. LSIState *s = opaque;
  1987. lsi_reg_writeb(s, addr & 0xff, val);
  1988. }
  1989. static const MemoryRegionOps lsi_io_ops = {
  1990. .read = lsi_io_read,
  1991. .write = lsi_io_write,
  1992. .endianness = DEVICE_LITTLE_ENDIAN,
  1993. .impl = {
  1994. .min_access_size = 1,
  1995. .max_access_size = 1,
  1996. },
  1997. };
  1998. static void lsi_scsi_reset(DeviceState *dev)
  1999. {
  2000. LSIState *s = LSI53C895A(dev);
  2001. lsi_soft_reset(s);
  2002. }
  2003. static int lsi_pre_save(void *opaque)
  2004. {
  2005. LSIState *s = opaque;
  2006. if (s->current) {
  2007. assert(s->current->dma_buf == NULL);
  2008. assert(s->current->dma_len == 0);
  2009. }
  2010. assert(QTAILQ_EMPTY(&s->queue));
  2011. return 0;
  2012. }
  2013. static int lsi_post_load(void *opaque, int version_id)
  2014. {
  2015. LSIState *s = opaque;
  2016. if (s->msg_len < 0 || s->msg_len > LSI_MAX_MSGIN_LEN) {
  2017. return -EINVAL;
  2018. }
  2019. return 0;
  2020. }
  2021. static const VMStateDescription vmstate_lsi_scsi = {
  2022. .name = "lsiscsi",
  2023. .version_id = 1,
  2024. .minimum_version_id = 0,
  2025. .pre_save = lsi_pre_save,
  2026. .post_load = lsi_post_load,
  2027. .fields = (VMStateField[]) {
  2028. VMSTATE_PCI_DEVICE(parent_obj, LSIState),
  2029. VMSTATE_INT32(carry, LSIState),
  2030. VMSTATE_INT32(status, LSIState),
  2031. VMSTATE_INT32(msg_action, LSIState),
  2032. VMSTATE_INT32(msg_len, LSIState),
  2033. VMSTATE_BUFFER(msg, LSIState),
  2034. VMSTATE_INT32(waiting, LSIState),
  2035. VMSTATE_UINT32(dsa, LSIState),
  2036. VMSTATE_UINT32(temp, LSIState),
  2037. VMSTATE_UINT32(dnad, LSIState),
  2038. VMSTATE_UINT32(dbc, LSIState),
  2039. VMSTATE_UINT8(istat0, LSIState),
  2040. VMSTATE_UINT8(istat1, LSIState),
  2041. VMSTATE_UINT8(dcmd, LSIState),
  2042. VMSTATE_UINT8(dstat, LSIState),
  2043. VMSTATE_UINT8(dien, LSIState),
  2044. VMSTATE_UINT8(sist0, LSIState),
  2045. VMSTATE_UINT8(sist1, LSIState),
  2046. VMSTATE_UINT8(sien0, LSIState),
  2047. VMSTATE_UINT8(sien1, LSIState),
  2048. VMSTATE_UINT8(mbox0, LSIState),
  2049. VMSTATE_UINT8(mbox1, LSIState),
  2050. VMSTATE_UINT8(dfifo, LSIState),
  2051. VMSTATE_UINT8(ctest2, LSIState),
  2052. VMSTATE_UINT8(ctest3, LSIState),
  2053. VMSTATE_UINT8(ctest4, LSIState),
  2054. VMSTATE_UINT8(ctest5, LSIState),
  2055. VMSTATE_UINT8(ccntl0, LSIState),
  2056. VMSTATE_UINT8(ccntl1, LSIState),
  2057. VMSTATE_UINT32(dsp, LSIState),
  2058. VMSTATE_UINT32(dsps, LSIState),
  2059. VMSTATE_UINT8(dmode, LSIState),
  2060. VMSTATE_UINT8(dcntl, LSIState),
  2061. VMSTATE_UINT8(scntl0, LSIState),
  2062. VMSTATE_UINT8(scntl1, LSIState),
  2063. VMSTATE_UINT8(scntl2, LSIState),
  2064. VMSTATE_UINT8(scntl3, LSIState),
  2065. VMSTATE_UINT8(sstat0, LSIState),
  2066. VMSTATE_UINT8(sstat1, LSIState),
  2067. VMSTATE_UINT8(scid, LSIState),
  2068. VMSTATE_UINT8(sxfer, LSIState),
  2069. VMSTATE_UINT8(socl, LSIState),
  2070. VMSTATE_UINT8(sdid, LSIState),
  2071. VMSTATE_UINT8(ssid, LSIState),
  2072. VMSTATE_UINT8(sfbr, LSIState),
  2073. VMSTATE_UINT8(stest1, LSIState),
  2074. VMSTATE_UINT8(stest2, LSIState),
  2075. VMSTATE_UINT8(stest3, LSIState),
  2076. VMSTATE_UINT8(sidl, LSIState),
  2077. VMSTATE_UINT8(stime0, LSIState),
  2078. VMSTATE_UINT8(respid0, LSIState),
  2079. VMSTATE_UINT8(respid1, LSIState),
  2080. VMSTATE_UINT8_V(sbcl, LSIState, 1),
  2081. VMSTATE_UINT32(mmrs, LSIState),
  2082. VMSTATE_UINT32(mmws, LSIState),
  2083. VMSTATE_UINT32(sfs, LSIState),
  2084. VMSTATE_UINT32(drs, LSIState),
  2085. VMSTATE_UINT32(sbms, LSIState),
  2086. VMSTATE_UINT32(dbms, LSIState),
  2087. VMSTATE_UINT32(dnad64, LSIState),
  2088. VMSTATE_UINT32(pmjad1, LSIState),
  2089. VMSTATE_UINT32(pmjad2, LSIState),
  2090. VMSTATE_UINT32(rbc, LSIState),
  2091. VMSTATE_UINT32(ua, LSIState),
  2092. VMSTATE_UINT32(ia, LSIState),
  2093. VMSTATE_UINT32(sbc, LSIState),
  2094. VMSTATE_UINT32(csbc, LSIState),
  2095. VMSTATE_BUFFER_UNSAFE(scratch, LSIState, 0, 18 * sizeof(uint32_t)),
  2096. VMSTATE_UINT8(sbr, LSIState),
  2097. VMSTATE_BUFFER_UNSAFE(script_ram, LSIState, 0, 8192),
  2098. VMSTATE_END_OF_LIST()
  2099. }
  2100. };
  2101. static const struct SCSIBusInfo lsi_scsi_info = {
  2102. .tcq = true,
  2103. .max_target = LSI_MAX_DEVS,
  2104. .max_lun = 0, /* LUN support is buggy */
  2105. .transfer_data = lsi_transfer_data,
  2106. .complete = lsi_command_complete,
  2107. .cancel = lsi_request_cancelled
  2108. };
  2109. static void lsi_scsi_realize(PCIDevice *dev, Error **errp)
  2110. {
  2111. LSIState *s = LSI53C895A(dev);
  2112. DeviceState *d = DEVICE(dev);
  2113. uint8_t *pci_conf;
  2114. pci_conf = dev->config;
  2115. /* PCI latency timer = 255 */
  2116. pci_conf[PCI_LATENCY_TIMER] = 0xff;
  2117. /* Interrupt pin A */
  2118. pci_conf[PCI_INTERRUPT_PIN] = 0x01;
  2119. memory_region_init_io(&s->mmio_io, OBJECT(s), &lsi_mmio_ops, s,
  2120. "lsi-mmio", 0x400);
  2121. memory_region_init_io(&s->ram_io, OBJECT(s), &lsi_ram_ops, s,
  2122. "lsi-ram", 0x2000);
  2123. memory_region_init_io(&s->io_io, OBJECT(s), &lsi_io_ops, s,
  2124. "lsi-io", 256);
  2125. address_space_init(&s->pci_io_as, pci_address_space_io(dev), "lsi-pci-io");
  2126. qdev_init_gpio_out(d, &s->ext_irq, 1);
  2127. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_io);
  2128. pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mmio_io);
  2129. pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->ram_io);
  2130. QTAILQ_INIT(&s->queue);
  2131. scsi_bus_init(&s->bus, sizeof(s->bus), d, &lsi_scsi_info);
  2132. }
  2133. static void lsi_scsi_exit(PCIDevice *dev)
  2134. {
  2135. LSIState *s = LSI53C895A(dev);
  2136. address_space_destroy(&s->pci_io_as);
  2137. }
  2138. static void lsi_class_init(ObjectClass *klass, void *data)
  2139. {
  2140. DeviceClass *dc = DEVICE_CLASS(klass);
  2141. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2142. k->realize = lsi_scsi_realize;
  2143. k->exit = lsi_scsi_exit;
  2144. k->vendor_id = PCI_VENDOR_ID_LSI_LOGIC;
  2145. k->device_id = PCI_DEVICE_ID_LSI_53C895A;
  2146. k->class_id = PCI_CLASS_STORAGE_SCSI;
  2147. k->subsystem_id = 0x1000;
  2148. dc->reset = lsi_scsi_reset;
  2149. dc->vmsd = &vmstate_lsi_scsi;
  2150. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  2151. }
  2152. static const TypeInfo lsi_info = {
  2153. .name = TYPE_LSI53C895A,
  2154. .parent = TYPE_PCI_DEVICE,
  2155. .instance_size = sizeof(LSIState),
  2156. .class_init = lsi_class_init,
  2157. .interfaces = (InterfaceInfo[]) {
  2158. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  2159. { },
  2160. },
  2161. };
  2162. static void lsi53c810_class_init(ObjectClass *klass, void *data)
  2163. {
  2164. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2165. k->device_id = PCI_DEVICE_ID_LSI_53C810;
  2166. }
  2167. static const TypeInfo lsi53c810_info = {
  2168. .name = TYPE_LSI53C810,
  2169. .parent = TYPE_LSI53C895A,
  2170. .class_init = lsi53c810_class_init,
  2171. };
  2172. static void lsi53c895a_register_types(void)
  2173. {
  2174. type_register_static(&lsi_info);
  2175. type_register_static(&lsi53c810_info);
  2176. }
  2177. type_init(lsi53c895a_register_types)
  2178. void lsi53c8xx_handle_legacy_cmdline(DeviceState *lsi_dev)
  2179. {
  2180. LSIState *s = LSI53C895A(lsi_dev);
  2181. scsi_bus_legacy_handle_cmdline(&s->bus);
  2182. }