dp8393x.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * QEMU NS SONIC DP8393x netcard
  3. *
  4. * Copyright (c) 2008-2009 Herve Poussineau
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/irq.h"
  21. #include "hw/qdev-properties.h"
  22. #include "hw/sysbus.h"
  23. #include "migration/vmstate.h"
  24. #include "net/net.h"
  25. #include "qapi/error.h"
  26. #include "qemu/module.h"
  27. #include "qemu/timer.h"
  28. #include <zlib.h>
  29. #include "qom/object.h"
  30. //#define DEBUG_SONIC
  31. #define SONIC_PROM_SIZE 0x1000
  32. #ifdef DEBUG_SONIC
  33. #define DPRINTF(fmt, ...) \
  34. do { printf("sonic: " fmt , ## __VA_ARGS__); } while (0)
  35. static const char* reg_names[] = {
  36. "CR", "DCR", "RCR", "TCR", "IMR", "ISR", "UTDA", "CTDA",
  37. "TPS", "TFC", "TSA0", "TSA1", "TFS", "URDA", "CRDA", "CRBA0",
  38. "CRBA1", "RBWC0", "RBWC1", "EOBC", "URRA", "RSA", "REA", "RRP",
  39. "RWP", "TRBA0", "TRBA1", "0x1b", "0x1c", "0x1d", "0x1e", "LLFA",
  40. "TTDA", "CEP", "CAP2", "CAP1", "CAP0", "CE", "CDP", "CDC",
  41. "SR", "WT0", "WT1", "RSC", "CRCT", "FAET", "MPT", "MDT",
  42. "0x30", "0x31", "0x32", "0x33", "0x34", "0x35", "0x36", "0x37",
  43. "0x38", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "DCR2" };
  44. #else
  45. #define DPRINTF(fmt, ...) do {} while (0)
  46. #endif
  47. #define SONIC_ERROR(fmt, ...) \
  48. do { printf("sonic ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
  49. #define SONIC_CR 0x00
  50. #define SONIC_DCR 0x01
  51. #define SONIC_RCR 0x02
  52. #define SONIC_TCR 0x03
  53. #define SONIC_IMR 0x04
  54. #define SONIC_ISR 0x05
  55. #define SONIC_UTDA 0x06
  56. #define SONIC_CTDA 0x07
  57. #define SONIC_TPS 0x08
  58. #define SONIC_TFC 0x09
  59. #define SONIC_TSA0 0x0a
  60. #define SONIC_TSA1 0x0b
  61. #define SONIC_TFS 0x0c
  62. #define SONIC_URDA 0x0d
  63. #define SONIC_CRDA 0x0e
  64. #define SONIC_CRBA0 0x0f
  65. #define SONIC_CRBA1 0x10
  66. #define SONIC_RBWC0 0x11
  67. #define SONIC_RBWC1 0x12
  68. #define SONIC_EOBC 0x13
  69. #define SONIC_URRA 0x14
  70. #define SONIC_RSA 0x15
  71. #define SONIC_REA 0x16
  72. #define SONIC_RRP 0x17
  73. #define SONIC_RWP 0x18
  74. #define SONIC_TRBA0 0x19
  75. #define SONIC_TRBA1 0x1a
  76. #define SONIC_LLFA 0x1f
  77. #define SONIC_TTDA 0x20
  78. #define SONIC_CEP 0x21
  79. #define SONIC_CAP2 0x22
  80. #define SONIC_CAP1 0x23
  81. #define SONIC_CAP0 0x24
  82. #define SONIC_CE 0x25
  83. #define SONIC_CDP 0x26
  84. #define SONIC_CDC 0x27
  85. #define SONIC_SR 0x28
  86. #define SONIC_WT0 0x29
  87. #define SONIC_WT1 0x2a
  88. #define SONIC_RSC 0x2b
  89. #define SONIC_CRCT 0x2c
  90. #define SONIC_FAET 0x2d
  91. #define SONIC_MPT 0x2e
  92. #define SONIC_MDT 0x2f
  93. #define SONIC_DCR2 0x3f
  94. #define SONIC_CR_HTX 0x0001
  95. #define SONIC_CR_TXP 0x0002
  96. #define SONIC_CR_RXDIS 0x0004
  97. #define SONIC_CR_RXEN 0x0008
  98. #define SONIC_CR_STP 0x0010
  99. #define SONIC_CR_ST 0x0020
  100. #define SONIC_CR_RST 0x0080
  101. #define SONIC_CR_RRRA 0x0100
  102. #define SONIC_CR_LCAM 0x0200
  103. #define SONIC_CR_MASK 0x03bf
  104. #define SONIC_DCR_DW 0x0020
  105. #define SONIC_DCR_LBR 0x2000
  106. #define SONIC_DCR_EXBUS 0x8000
  107. #define SONIC_RCR_PRX 0x0001
  108. #define SONIC_RCR_LBK 0x0002
  109. #define SONIC_RCR_FAER 0x0004
  110. #define SONIC_RCR_CRCR 0x0008
  111. #define SONIC_RCR_CRS 0x0020
  112. #define SONIC_RCR_LPKT 0x0040
  113. #define SONIC_RCR_BC 0x0080
  114. #define SONIC_RCR_MC 0x0100
  115. #define SONIC_RCR_LB0 0x0200
  116. #define SONIC_RCR_LB1 0x0400
  117. #define SONIC_RCR_AMC 0x0800
  118. #define SONIC_RCR_PRO 0x1000
  119. #define SONIC_RCR_BRD 0x2000
  120. #define SONIC_RCR_RNT 0x4000
  121. #define SONIC_TCR_PTX 0x0001
  122. #define SONIC_TCR_BCM 0x0002
  123. #define SONIC_TCR_FU 0x0004
  124. #define SONIC_TCR_EXC 0x0040
  125. #define SONIC_TCR_CRSL 0x0080
  126. #define SONIC_TCR_NCRS 0x0100
  127. #define SONIC_TCR_EXD 0x0400
  128. #define SONIC_TCR_CRCI 0x2000
  129. #define SONIC_TCR_PINT 0x8000
  130. #define SONIC_ISR_RBAE 0x0010
  131. #define SONIC_ISR_RBE 0x0020
  132. #define SONIC_ISR_RDE 0x0040
  133. #define SONIC_ISR_TC 0x0080
  134. #define SONIC_ISR_TXDN 0x0200
  135. #define SONIC_ISR_PKTRX 0x0400
  136. #define SONIC_ISR_PINT 0x0800
  137. #define SONIC_ISR_LCD 0x1000
  138. #define SONIC_DESC_EOL 0x0001
  139. #define SONIC_DESC_ADDR 0xFFFE
  140. #define TYPE_DP8393X "dp8393x"
  141. OBJECT_DECLARE_SIMPLE_TYPE(dp8393xState, DP8393X)
  142. struct dp8393xState {
  143. SysBusDevice parent_obj;
  144. /* Hardware */
  145. uint8_t it_shift;
  146. bool big_endian;
  147. bool last_rba_is_full;
  148. qemu_irq irq;
  149. #ifdef DEBUG_SONIC
  150. int irq_level;
  151. #endif
  152. QEMUTimer *watchdog;
  153. int64_t wt_last_update;
  154. NICConf conf;
  155. NICState *nic;
  156. MemoryRegion mmio;
  157. MemoryRegion prom;
  158. /* Registers */
  159. uint8_t cam[16][6];
  160. uint16_t regs[0x40];
  161. /* Temporaries */
  162. uint8_t tx_buffer[0x10000];
  163. uint16_t data[12];
  164. int loopback_packet;
  165. /* Memory access */
  166. MemoryRegion *dma_mr;
  167. AddressSpace as;
  168. };
  169. /* Accessor functions for values which are formed by
  170. * concatenating two 16 bit device registers. By putting these
  171. * in their own functions with a uint32_t return type we avoid the
  172. * pitfall of implicit sign extension where ((x << 16) | y) is a
  173. * signed 32 bit integer that might get sign-extended to a 64 bit integer.
  174. */
  175. static uint32_t dp8393x_cdp(dp8393xState *s)
  176. {
  177. return (s->regs[SONIC_URRA] << 16) | s->regs[SONIC_CDP];
  178. }
  179. static uint32_t dp8393x_crba(dp8393xState *s)
  180. {
  181. return (s->regs[SONIC_CRBA1] << 16) | s->regs[SONIC_CRBA0];
  182. }
  183. static uint32_t dp8393x_crda(dp8393xState *s)
  184. {
  185. return (s->regs[SONIC_URDA] << 16) |
  186. (s->regs[SONIC_CRDA] & SONIC_DESC_ADDR);
  187. }
  188. static uint32_t dp8393x_rbwc(dp8393xState *s)
  189. {
  190. return (s->regs[SONIC_RBWC1] << 16) | s->regs[SONIC_RBWC0];
  191. }
  192. static uint32_t dp8393x_rrp(dp8393xState *s)
  193. {
  194. return (s->regs[SONIC_URRA] << 16) | s->regs[SONIC_RRP];
  195. }
  196. static uint32_t dp8393x_tsa(dp8393xState *s)
  197. {
  198. return (s->regs[SONIC_TSA1] << 16) | s->regs[SONIC_TSA0];
  199. }
  200. static uint32_t dp8393x_ttda(dp8393xState *s)
  201. {
  202. return (s->regs[SONIC_UTDA] << 16) |
  203. (s->regs[SONIC_TTDA] & SONIC_DESC_ADDR);
  204. }
  205. static uint32_t dp8393x_wt(dp8393xState *s)
  206. {
  207. return s->regs[SONIC_WT1] << 16 | s->regs[SONIC_WT0];
  208. }
  209. static uint16_t dp8393x_get(dp8393xState *s, int width, int offset)
  210. {
  211. uint16_t val;
  212. if (s->big_endian) {
  213. val = be16_to_cpu(s->data[offset * width + width - 1]);
  214. } else {
  215. val = le16_to_cpu(s->data[offset * width]);
  216. }
  217. return val;
  218. }
  219. static void dp8393x_put(dp8393xState *s, int width, int offset,
  220. uint16_t val)
  221. {
  222. if (s->big_endian) {
  223. if (width == 2) {
  224. s->data[offset * 2] = 0;
  225. s->data[offset * 2 + 1] = cpu_to_be16(val);
  226. } else {
  227. s->data[offset] = cpu_to_be16(val);
  228. }
  229. } else {
  230. if (width == 2) {
  231. s->data[offset * 2] = cpu_to_le16(val);
  232. s->data[offset * 2 + 1] = 0;
  233. } else {
  234. s->data[offset] = cpu_to_le16(val);
  235. }
  236. }
  237. }
  238. static void dp8393x_update_irq(dp8393xState *s)
  239. {
  240. int level = (s->regs[SONIC_IMR] & s->regs[SONIC_ISR]) ? 1 : 0;
  241. #ifdef DEBUG_SONIC
  242. if (level != s->irq_level) {
  243. s->irq_level = level;
  244. if (level) {
  245. DPRINTF("raise irq, isr is 0x%04x\n", s->regs[SONIC_ISR]);
  246. } else {
  247. DPRINTF("lower irq\n");
  248. }
  249. }
  250. #endif
  251. qemu_set_irq(s->irq, level);
  252. }
  253. static void dp8393x_do_load_cam(dp8393xState *s)
  254. {
  255. int width, size;
  256. uint16_t index = 0;
  257. width = (s->regs[SONIC_DCR] & SONIC_DCR_DW) ? 2 : 1;
  258. size = sizeof(uint16_t) * 4 * width;
  259. while (s->regs[SONIC_CDC] & 0x1f) {
  260. /* Fill current entry */
  261. address_space_read(&s->as, dp8393x_cdp(s),
  262. MEMTXATTRS_UNSPECIFIED, s->data, size);
  263. s->cam[index][0] = dp8393x_get(s, width, 1) & 0xff;
  264. s->cam[index][1] = dp8393x_get(s, width, 1) >> 8;
  265. s->cam[index][2] = dp8393x_get(s, width, 2) & 0xff;
  266. s->cam[index][3] = dp8393x_get(s, width, 2) >> 8;
  267. s->cam[index][4] = dp8393x_get(s, width, 3) & 0xff;
  268. s->cam[index][5] = dp8393x_get(s, width, 3) >> 8;
  269. DPRINTF("load cam[%d] with %02x%02x%02x%02x%02x%02x\n", index,
  270. s->cam[index][0], s->cam[index][1], s->cam[index][2],
  271. s->cam[index][3], s->cam[index][4], s->cam[index][5]);
  272. /* Move to next entry */
  273. s->regs[SONIC_CDC]--;
  274. s->regs[SONIC_CDP] += size;
  275. index++;
  276. }
  277. /* Read CAM enable */
  278. address_space_read(&s->as, dp8393x_cdp(s),
  279. MEMTXATTRS_UNSPECIFIED, s->data, size);
  280. s->regs[SONIC_CE] = dp8393x_get(s, width, 0);
  281. DPRINTF("load cam done. cam enable mask 0x%04x\n", s->regs[SONIC_CE]);
  282. /* Done */
  283. s->regs[SONIC_CR] &= ~SONIC_CR_LCAM;
  284. s->regs[SONIC_ISR] |= SONIC_ISR_LCD;
  285. dp8393x_update_irq(s);
  286. }
  287. static void dp8393x_do_read_rra(dp8393xState *s)
  288. {
  289. int width, size;
  290. /* Read memory */
  291. width = (s->regs[SONIC_DCR] & SONIC_DCR_DW) ? 2 : 1;
  292. size = sizeof(uint16_t) * 4 * width;
  293. address_space_read(&s->as, dp8393x_rrp(s),
  294. MEMTXATTRS_UNSPECIFIED, s->data, size);
  295. /* Update SONIC registers */
  296. s->regs[SONIC_CRBA0] = dp8393x_get(s, width, 0);
  297. s->regs[SONIC_CRBA1] = dp8393x_get(s, width, 1);
  298. s->regs[SONIC_RBWC0] = dp8393x_get(s, width, 2);
  299. s->regs[SONIC_RBWC1] = dp8393x_get(s, width, 3);
  300. DPRINTF("CRBA0/1: 0x%04x/0x%04x, RBWC0/1: 0x%04x/0x%04x\n",
  301. s->regs[SONIC_CRBA0], s->regs[SONIC_CRBA1],
  302. s->regs[SONIC_RBWC0], s->regs[SONIC_RBWC1]);
  303. /* Go to next entry */
  304. s->regs[SONIC_RRP] += size;
  305. /* Handle wrap */
  306. if (s->regs[SONIC_RRP] == s->regs[SONIC_REA]) {
  307. s->regs[SONIC_RRP] = s->regs[SONIC_RSA];
  308. }
  309. /* Warn the host if CRBA now has the last available resource */
  310. if (s->regs[SONIC_RRP] == s->regs[SONIC_RWP])
  311. {
  312. s->regs[SONIC_ISR] |= SONIC_ISR_RBE;
  313. dp8393x_update_irq(s);
  314. }
  315. /* Allow packet reception */
  316. s->last_rba_is_full = false;
  317. }
  318. static void dp8393x_do_software_reset(dp8393xState *s)
  319. {
  320. timer_del(s->watchdog);
  321. s->regs[SONIC_CR] &= ~(SONIC_CR_LCAM | SONIC_CR_RRRA | SONIC_CR_TXP | SONIC_CR_HTX);
  322. s->regs[SONIC_CR] |= SONIC_CR_RST | SONIC_CR_RXDIS;
  323. }
  324. static void dp8393x_set_next_tick(dp8393xState *s)
  325. {
  326. uint32_t ticks;
  327. int64_t delay;
  328. if (s->regs[SONIC_CR] & SONIC_CR_STP) {
  329. timer_del(s->watchdog);
  330. return;
  331. }
  332. ticks = dp8393x_wt(s);
  333. s->wt_last_update = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  334. delay = NANOSECONDS_PER_SECOND * ticks / 5000000;
  335. timer_mod(s->watchdog, s->wt_last_update + delay);
  336. }
  337. static void dp8393x_update_wt_regs(dp8393xState *s)
  338. {
  339. int64_t elapsed;
  340. uint32_t val;
  341. if (s->regs[SONIC_CR] & SONIC_CR_STP) {
  342. timer_del(s->watchdog);
  343. return;
  344. }
  345. elapsed = s->wt_last_update - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  346. val = dp8393x_wt(s);
  347. val -= elapsed / 5000000;
  348. s->regs[SONIC_WT1] = (val >> 16) & 0xffff;
  349. s->regs[SONIC_WT0] = (val >> 0) & 0xffff;
  350. dp8393x_set_next_tick(s);
  351. }
  352. static void dp8393x_do_start_timer(dp8393xState *s)
  353. {
  354. s->regs[SONIC_CR] &= ~SONIC_CR_STP;
  355. dp8393x_set_next_tick(s);
  356. }
  357. static void dp8393x_do_stop_timer(dp8393xState *s)
  358. {
  359. s->regs[SONIC_CR] &= ~SONIC_CR_ST;
  360. dp8393x_update_wt_regs(s);
  361. }
  362. static bool dp8393x_can_receive(NetClientState *nc);
  363. static void dp8393x_do_receiver_enable(dp8393xState *s)
  364. {
  365. s->regs[SONIC_CR] &= ~SONIC_CR_RXDIS;
  366. if (dp8393x_can_receive(s->nic->ncs)) {
  367. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  368. }
  369. }
  370. static void dp8393x_do_receiver_disable(dp8393xState *s)
  371. {
  372. s->regs[SONIC_CR] &= ~SONIC_CR_RXEN;
  373. }
  374. static void dp8393x_do_transmit_packets(dp8393xState *s)
  375. {
  376. NetClientState *nc = qemu_get_queue(s->nic);
  377. int width, size;
  378. int tx_len, len;
  379. uint16_t i;
  380. width = (s->regs[SONIC_DCR] & SONIC_DCR_DW) ? 2 : 1;
  381. while (1) {
  382. /* Read memory */
  383. size = sizeof(uint16_t) * 6 * width;
  384. s->regs[SONIC_TTDA] = s->regs[SONIC_CTDA];
  385. DPRINTF("Transmit packet at %08x\n", dp8393x_ttda(s));
  386. address_space_read(&s->as, dp8393x_ttda(s) + sizeof(uint16_t) * width,
  387. MEMTXATTRS_UNSPECIFIED, s->data, size);
  388. tx_len = 0;
  389. /* Update registers */
  390. s->regs[SONIC_TCR] = dp8393x_get(s, width, 0) & 0xf000;
  391. s->regs[SONIC_TPS] = dp8393x_get(s, width, 1);
  392. s->regs[SONIC_TFC] = dp8393x_get(s, width, 2);
  393. s->regs[SONIC_TSA0] = dp8393x_get(s, width, 3);
  394. s->regs[SONIC_TSA1] = dp8393x_get(s, width, 4);
  395. s->regs[SONIC_TFS] = dp8393x_get(s, width, 5);
  396. /* Handle programmable interrupt */
  397. if (s->regs[SONIC_TCR] & SONIC_TCR_PINT) {
  398. s->regs[SONIC_ISR] |= SONIC_ISR_PINT;
  399. } else {
  400. s->regs[SONIC_ISR] &= ~SONIC_ISR_PINT;
  401. }
  402. for (i = 0; i < s->regs[SONIC_TFC]; ) {
  403. /* Append fragment */
  404. len = s->regs[SONIC_TFS];
  405. if (tx_len + len > sizeof(s->tx_buffer)) {
  406. len = sizeof(s->tx_buffer) - tx_len;
  407. }
  408. address_space_read(&s->as, dp8393x_tsa(s), MEMTXATTRS_UNSPECIFIED,
  409. &s->tx_buffer[tx_len], len);
  410. tx_len += len;
  411. i++;
  412. if (i != s->regs[SONIC_TFC]) {
  413. /* Read next fragment details */
  414. size = sizeof(uint16_t) * 3 * width;
  415. address_space_read(&s->as,
  416. dp8393x_ttda(s)
  417. + sizeof(uint16_t) * width * (4 + 3 * i),
  418. MEMTXATTRS_UNSPECIFIED, s->data,
  419. size);
  420. s->regs[SONIC_TSA0] = dp8393x_get(s, width, 0);
  421. s->regs[SONIC_TSA1] = dp8393x_get(s, width, 1);
  422. s->regs[SONIC_TFS] = dp8393x_get(s, width, 2);
  423. }
  424. }
  425. /* Handle Ethernet checksum */
  426. if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
  427. /* Don't append FCS there, to look like slirp packets
  428. * which don't have one */
  429. } else {
  430. /* Remove existing FCS */
  431. tx_len -= 4;
  432. }
  433. if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {
  434. /* Loopback */
  435. s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
  436. if (nc->info->can_receive(nc)) {
  437. s->loopback_packet = 1;
  438. nc->info->receive(nc, s->tx_buffer, tx_len);
  439. }
  440. } else {
  441. /* Transmit packet */
  442. qemu_send_packet(nc, s->tx_buffer, tx_len);
  443. }
  444. s->regs[SONIC_TCR] |= SONIC_TCR_PTX;
  445. /* Write status */
  446. dp8393x_put(s, width, 0,
  447. s->regs[SONIC_TCR] & 0x0fff); /* status */
  448. size = sizeof(uint16_t) * width;
  449. address_space_write(&s->as, dp8393x_ttda(s),
  450. MEMTXATTRS_UNSPECIFIED, s->data, size);
  451. if (!(s->regs[SONIC_CR] & SONIC_CR_HTX)) {
  452. /* Read footer of packet */
  453. size = sizeof(uint16_t) * width;
  454. address_space_read(&s->as,
  455. dp8393x_ttda(s)
  456. + sizeof(uint16_t) * width
  457. * (4 + 3 * s->regs[SONIC_TFC]),
  458. MEMTXATTRS_UNSPECIFIED, s->data,
  459. size);
  460. s->regs[SONIC_CTDA] = dp8393x_get(s, width, 0);
  461. if (s->regs[SONIC_CTDA] & SONIC_DESC_EOL) {
  462. /* EOL detected */
  463. break;
  464. }
  465. }
  466. }
  467. /* Done */
  468. s->regs[SONIC_CR] &= ~SONIC_CR_TXP;
  469. s->regs[SONIC_ISR] |= SONIC_ISR_TXDN;
  470. dp8393x_update_irq(s);
  471. }
  472. static void dp8393x_do_halt_transmission(dp8393xState *s)
  473. {
  474. /* Nothing to do */
  475. }
  476. static void dp8393x_do_command(dp8393xState *s, uint16_t command)
  477. {
  478. if ((s->regs[SONIC_CR] & SONIC_CR_RST) && !(command & SONIC_CR_RST)) {
  479. s->regs[SONIC_CR] &= ~SONIC_CR_RST;
  480. return;
  481. }
  482. s->regs[SONIC_CR] |= (command & SONIC_CR_MASK);
  483. if (command & SONIC_CR_HTX)
  484. dp8393x_do_halt_transmission(s);
  485. if (command & SONIC_CR_TXP)
  486. dp8393x_do_transmit_packets(s);
  487. if (command & SONIC_CR_RXDIS)
  488. dp8393x_do_receiver_disable(s);
  489. if (command & SONIC_CR_RXEN)
  490. dp8393x_do_receiver_enable(s);
  491. if (command & SONIC_CR_STP)
  492. dp8393x_do_stop_timer(s);
  493. if (command & SONIC_CR_ST)
  494. dp8393x_do_start_timer(s);
  495. if (command & SONIC_CR_RST)
  496. dp8393x_do_software_reset(s);
  497. if (command & SONIC_CR_RRRA) {
  498. dp8393x_do_read_rra(s);
  499. s->regs[SONIC_CR] &= ~SONIC_CR_RRRA;
  500. }
  501. if (command & SONIC_CR_LCAM)
  502. dp8393x_do_load_cam(s);
  503. }
  504. static uint64_t dp8393x_read(void *opaque, hwaddr addr, unsigned int size)
  505. {
  506. dp8393xState *s = opaque;
  507. int reg = addr >> s->it_shift;
  508. uint16_t val = 0;
  509. switch (reg) {
  510. /* Update data before reading it */
  511. case SONIC_WT0:
  512. case SONIC_WT1:
  513. dp8393x_update_wt_regs(s);
  514. val = s->regs[reg];
  515. break;
  516. /* Accept read to some registers only when in reset mode */
  517. case SONIC_CAP2:
  518. case SONIC_CAP1:
  519. case SONIC_CAP0:
  520. if (s->regs[SONIC_CR] & SONIC_CR_RST) {
  521. val = s->cam[s->regs[SONIC_CEP] & 0xf][2* (SONIC_CAP0 - reg) + 1] << 8;
  522. val |= s->cam[s->regs[SONIC_CEP] & 0xf][2* (SONIC_CAP0 - reg)];
  523. }
  524. break;
  525. /* All other registers have no special contrainst */
  526. default:
  527. val = s->regs[reg];
  528. }
  529. DPRINTF("read 0x%04x from reg %s\n", val, reg_names[reg]);
  530. return s->big_endian ? val << 16 : val;
  531. }
  532. static void dp8393x_write(void *opaque, hwaddr addr, uint64_t data,
  533. unsigned int size)
  534. {
  535. dp8393xState *s = opaque;
  536. int reg = addr >> s->it_shift;
  537. uint32_t val = s->big_endian ? data >> 16 : data;
  538. DPRINTF("write 0x%04x to reg %s\n", (uint16_t)val, reg_names[reg]);
  539. switch (reg) {
  540. /* Command register */
  541. case SONIC_CR:
  542. dp8393x_do_command(s, val);
  543. break;
  544. /* Prevent write to read-only registers */
  545. case SONIC_CAP2:
  546. case SONIC_CAP1:
  547. case SONIC_CAP0:
  548. case SONIC_SR:
  549. case SONIC_MDT:
  550. DPRINTF("writing to reg %d invalid\n", reg);
  551. break;
  552. /* Accept write to some registers only when in reset mode */
  553. case SONIC_DCR:
  554. if (s->regs[SONIC_CR] & SONIC_CR_RST) {
  555. s->regs[reg] = val & 0xbfff;
  556. } else {
  557. DPRINTF("writing to DCR invalid\n");
  558. }
  559. break;
  560. case SONIC_DCR2:
  561. if (s->regs[SONIC_CR] & SONIC_CR_RST) {
  562. s->regs[reg] = val & 0xf017;
  563. } else {
  564. DPRINTF("writing to DCR2 invalid\n");
  565. }
  566. break;
  567. /* 12 lower bytes are Read Only */
  568. case SONIC_TCR:
  569. s->regs[reg] = val & 0xf000;
  570. break;
  571. /* 9 lower bytes are Read Only */
  572. case SONIC_RCR:
  573. s->regs[reg] = val & 0xffe0;
  574. break;
  575. /* Ignore most significant bit */
  576. case SONIC_IMR:
  577. s->regs[reg] = val & 0x7fff;
  578. dp8393x_update_irq(s);
  579. break;
  580. /* Clear bits by writing 1 to them */
  581. case SONIC_ISR:
  582. val &= s->regs[reg];
  583. s->regs[reg] &= ~val;
  584. if (val & SONIC_ISR_RBE) {
  585. dp8393x_do_read_rra(s);
  586. }
  587. dp8393x_update_irq(s);
  588. break;
  589. /* The guest is required to store aligned pointers here */
  590. case SONIC_RSA:
  591. case SONIC_REA:
  592. case SONIC_RRP:
  593. case SONIC_RWP:
  594. if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
  595. s->regs[reg] = val & 0xfffc;
  596. } else {
  597. s->regs[reg] = val & 0xfffe;
  598. }
  599. break;
  600. /* Invert written value for some registers */
  601. case SONIC_CRCT:
  602. case SONIC_FAET:
  603. case SONIC_MPT:
  604. s->regs[reg] = val ^ 0xffff;
  605. break;
  606. /* All other registers have no special contrainst */
  607. default:
  608. s->regs[reg] = val;
  609. }
  610. if (reg == SONIC_WT0 || reg == SONIC_WT1) {
  611. dp8393x_set_next_tick(s);
  612. }
  613. }
  614. static const MemoryRegionOps dp8393x_ops = {
  615. .read = dp8393x_read,
  616. .write = dp8393x_write,
  617. .impl.min_access_size = 4,
  618. .impl.max_access_size = 4,
  619. .endianness = DEVICE_NATIVE_ENDIAN,
  620. };
  621. static void dp8393x_watchdog(void *opaque)
  622. {
  623. dp8393xState *s = opaque;
  624. if (s->regs[SONIC_CR] & SONIC_CR_STP) {
  625. return;
  626. }
  627. s->regs[SONIC_WT1] = 0xffff;
  628. s->regs[SONIC_WT0] = 0xffff;
  629. dp8393x_set_next_tick(s);
  630. /* Signal underflow */
  631. s->regs[SONIC_ISR] |= SONIC_ISR_TC;
  632. dp8393x_update_irq(s);
  633. }
  634. static bool dp8393x_can_receive(NetClientState *nc)
  635. {
  636. dp8393xState *s = qemu_get_nic_opaque(nc);
  637. return !!(s->regs[SONIC_CR] & SONIC_CR_RXEN);
  638. }
  639. static int dp8393x_receive_filter(dp8393xState *s, const uint8_t * buf,
  640. int size)
  641. {
  642. static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  643. int i;
  644. /* Check promiscuous mode */
  645. if ((s->regs[SONIC_RCR] & SONIC_RCR_PRO) && (buf[0] & 1) == 0) {
  646. return 0;
  647. }
  648. /* Check multicast packets */
  649. if ((s->regs[SONIC_RCR] & SONIC_RCR_AMC) && (buf[0] & 1) == 1) {
  650. return SONIC_RCR_MC;
  651. }
  652. /* Check broadcast */
  653. if ((s->regs[SONIC_RCR] & SONIC_RCR_BRD) && !memcmp(buf, bcast, sizeof(bcast))) {
  654. return SONIC_RCR_BC;
  655. }
  656. /* Check CAM */
  657. for (i = 0; i < 16; i++) {
  658. if (s->regs[SONIC_CE] & (1 << i)) {
  659. /* Entry enabled */
  660. if (!memcmp(buf, s->cam[i], sizeof(s->cam[i]))) {
  661. return 0;
  662. }
  663. }
  664. }
  665. return -1;
  666. }
  667. static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
  668. size_t pkt_size)
  669. {
  670. dp8393xState *s = qemu_get_nic_opaque(nc);
  671. int packet_type;
  672. uint32_t available, address;
  673. int width, rx_len, padded_len;
  674. uint32_t checksum;
  675. int size;
  676. s->regs[SONIC_RCR] &= ~(SONIC_RCR_PRX | SONIC_RCR_LBK | SONIC_RCR_FAER |
  677. SONIC_RCR_CRCR | SONIC_RCR_LPKT | SONIC_RCR_BC | SONIC_RCR_MC);
  678. if (s->last_rba_is_full) {
  679. return pkt_size;
  680. }
  681. rx_len = pkt_size + sizeof(checksum);
  682. if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
  683. width = 2;
  684. padded_len = ((rx_len - 1) | 3) + 1;
  685. } else {
  686. width = 1;
  687. padded_len = ((rx_len - 1) | 1) + 1;
  688. }
  689. if (padded_len > dp8393x_rbwc(s) * 2) {
  690. DPRINTF("oversize packet, pkt_size is %d\n", pkt_size);
  691. s->regs[SONIC_ISR] |= SONIC_ISR_RBAE;
  692. dp8393x_update_irq(s);
  693. s->regs[SONIC_RCR] |= SONIC_RCR_LPKT;
  694. goto done;
  695. }
  696. packet_type = dp8393x_receive_filter(s, buf, pkt_size);
  697. if (packet_type < 0) {
  698. DPRINTF("packet not for netcard\n");
  699. return -1;
  700. }
  701. /* Check for EOL */
  702. if (s->regs[SONIC_LLFA] & SONIC_DESC_EOL) {
  703. /* Are we still in resource exhaustion? */
  704. size = sizeof(uint16_t) * 1 * width;
  705. address = dp8393x_crda(s) + sizeof(uint16_t) * 5 * width;
  706. address_space_read(&s->as, address, MEMTXATTRS_UNSPECIFIED,
  707. s->data, size);
  708. s->regs[SONIC_LLFA] = dp8393x_get(s, width, 0);
  709. if (s->regs[SONIC_LLFA] & SONIC_DESC_EOL) {
  710. /* Still EOL ; stop reception */
  711. return -1;
  712. }
  713. /* Link has been updated by host */
  714. /* Clear in_use */
  715. size = sizeof(uint16_t) * width;
  716. address = dp8393x_crda(s) + sizeof(uint16_t) * 6 * width;
  717. dp8393x_put(s, width, 0, 0);
  718. address_space_rw(&s->as, address, MEMTXATTRS_UNSPECIFIED,
  719. (uint8_t *)s->data, size, 1);
  720. /* Move to next descriptor */
  721. s->regs[SONIC_CRDA] = s->regs[SONIC_LLFA];
  722. s->regs[SONIC_ISR] |= SONIC_ISR_PKTRX;
  723. }
  724. /* Save current position */
  725. s->regs[SONIC_TRBA1] = s->regs[SONIC_CRBA1];
  726. s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
  727. /* Calculate the ethernet checksum */
  728. checksum = cpu_to_le32(crc32(0, buf, pkt_size));
  729. /* Put packet into RBA */
  730. DPRINTF("Receive packet at %08x\n", dp8393x_crba(s));
  731. address = dp8393x_crba(s);
  732. address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
  733. buf, pkt_size);
  734. address += pkt_size;
  735. /* Put frame checksum into RBA */
  736. address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
  737. &checksum, sizeof(checksum));
  738. address += sizeof(checksum);
  739. /* Pad short packets to keep pointers aligned */
  740. if (rx_len < padded_len) {
  741. size = padded_len - rx_len;
  742. address_space_rw(&s->as, address, MEMTXATTRS_UNSPECIFIED,
  743. (uint8_t *)"\xFF\xFF\xFF", size, 1);
  744. address += size;
  745. }
  746. s->regs[SONIC_CRBA1] = address >> 16;
  747. s->regs[SONIC_CRBA0] = address & 0xffff;
  748. available = dp8393x_rbwc(s);
  749. available -= padded_len >> 1;
  750. s->regs[SONIC_RBWC1] = available >> 16;
  751. s->regs[SONIC_RBWC0] = available & 0xffff;
  752. /* Update status */
  753. if (dp8393x_rbwc(s) < s->regs[SONIC_EOBC]) {
  754. s->regs[SONIC_RCR] |= SONIC_RCR_LPKT;
  755. }
  756. s->regs[SONIC_RCR] |= packet_type;
  757. s->regs[SONIC_RCR] |= SONIC_RCR_PRX;
  758. if (s->loopback_packet) {
  759. s->regs[SONIC_RCR] |= SONIC_RCR_LBK;
  760. s->loopback_packet = 0;
  761. }
  762. /* Write status to memory */
  763. DPRINTF("Write status at %08x\n", dp8393x_crda(s));
  764. dp8393x_put(s, width, 0, s->regs[SONIC_RCR]); /* status */
  765. dp8393x_put(s, width, 1, rx_len); /* byte count */
  766. dp8393x_put(s, width, 2, s->regs[SONIC_TRBA0]); /* pkt_ptr0 */
  767. dp8393x_put(s, width, 3, s->regs[SONIC_TRBA1]); /* pkt_ptr1 */
  768. dp8393x_put(s, width, 4, s->regs[SONIC_RSC]); /* seq_no */
  769. size = sizeof(uint16_t) * 5 * width;
  770. address_space_write(&s->as, dp8393x_crda(s),
  771. MEMTXATTRS_UNSPECIFIED,
  772. s->data, size);
  773. /* Check link field */
  774. size = sizeof(uint16_t) * width;
  775. address_space_read(&s->as,
  776. dp8393x_crda(s) + sizeof(uint16_t) * 5 * width,
  777. MEMTXATTRS_UNSPECIFIED, s->data, size);
  778. s->regs[SONIC_LLFA] = dp8393x_get(s, width, 0);
  779. if (s->regs[SONIC_LLFA] & SONIC_DESC_EOL) {
  780. /* EOL detected */
  781. s->regs[SONIC_ISR] |= SONIC_ISR_RDE;
  782. } else {
  783. /* Clear in_use */
  784. size = sizeof(uint16_t) * width;
  785. address = dp8393x_crda(s) + sizeof(uint16_t) * 6 * width;
  786. dp8393x_put(s, width, 0, 0);
  787. address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
  788. s->data, size);
  789. /* Move to next descriptor */
  790. s->regs[SONIC_CRDA] = s->regs[SONIC_LLFA];
  791. s->regs[SONIC_ISR] |= SONIC_ISR_PKTRX;
  792. }
  793. dp8393x_update_irq(s);
  794. s->regs[SONIC_RSC] = (s->regs[SONIC_RSC] & 0xff00) |
  795. ((s->regs[SONIC_RSC] + 1) & 0x00ff);
  796. done:
  797. if (s->regs[SONIC_RCR] & SONIC_RCR_LPKT) {
  798. if (s->regs[SONIC_RRP] == s->regs[SONIC_RWP]) {
  799. /* Stop packet reception */
  800. s->last_rba_is_full = true;
  801. } else {
  802. /* Read next resource */
  803. dp8393x_do_read_rra(s);
  804. }
  805. }
  806. return pkt_size;
  807. }
  808. static void dp8393x_reset(DeviceState *dev)
  809. {
  810. dp8393xState *s = DP8393X(dev);
  811. timer_del(s->watchdog);
  812. memset(s->regs, 0, sizeof(s->regs));
  813. s->regs[SONIC_SR] = 0x0004; /* only revision recognized by Linux/mips */
  814. s->regs[SONIC_CR] = SONIC_CR_RST | SONIC_CR_STP | SONIC_CR_RXDIS;
  815. s->regs[SONIC_DCR] &= ~(SONIC_DCR_EXBUS | SONIC_DCR_LBR);
  816. s->regs[SONIC_RCR] &= ~(SONIC_RCR_LB0 | SONIC_RCR_LB1 | SONIC_RCR_BRD | SONIC_RCR_RNT);
  817. s->regs[SONIC_TCR] |= SONIC_TCR_NCRS | SONIC_TCR_PTX;
  818. s->regs[SONIC_TCR] &= ~SONIC_TCR_BCM;
  819. s->regs[SONIC_IMR] = 0;
  820. s->regs[SONIC_ISR] = 0;
  821. s->regs[SONIC_DCR2] = 0;
  822. s->regs[SONIC_EOBC] = 0x02F8;
  823. s->regs[SONIC_RSC] = 0;
  824. s->regs[SONIC_CE] = 0;
  825. s->regs[SONIC_RSC] = 0;
  826. /* Network cable is connected */
  827. s->regs[SONIC_RCR] |= SONIC_RCR_CRS;
  828. dp8393x_update_irq(s);
  829. }
  830. static NetClientInfo net_dp83932_info = {
  831. .type = NET_CLIENT_DRIVER_NIC,
  832. .size = sizeof(NICState),
  833. .can_receive = dp8393x_can_receive,
  834. .receive = dp8393x_receive,
  835. };
  836. static void dp8393x_instance_init(Object *obj)
  837. {
  838. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  839. dp8393xState *s = DP8393X(obj);
  840. sysbus_init_mmio(sbd, &s->mmio);
  841. sysbus_init_mmio(sbd, &s->prom);
  842. sysbus_init_irq(sbd, &s->irq);
  843. }
  844. static void dp8393x_realize(DeviceState *dev, Error **errp)
  845. {
  846. dp8393xState *s = DP8393X(dev);
  847. int i, checksum;
  848. uint8_t *prom;
  849. Error *local_err = NULL;
  850. address_space_init(&s->as, s->dma_mr, "dp8393x");
  851. memory_region_init_io(&s->mmio, OBJECT(dev), &dp8393x_ops, s,
  852. "dp8393x-regs", 0x40 << s->it_shift);
  853. s->nic = qemu_new_nic(&net_dp83932_info, &s->conf,
  854. object_get_typename(OBJECT(dev)), dev->id, s);
  855. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
  856. s->watchdog = timer_new_ns(QEMU_CLOCK_VIRTUAL, dp8393x_watchdog, s);
  857. memory_region_init_rom(&s->prom, OBJECT(dev), "dp8393x-prom",
  858. SONIC_PROM_SIZE, &local_err);
  859. if (local_err) {
  860. error_propagate(errp, local_err);
  861. return;
  862. }
  863. prom = memory_region_get_ram_ptr(&s->prom);
  864. checksum = 0;
  865. for (i = 0; i < 6; i++) {
  866. prom[i] = s->conf.macaddr.a[i];
  867. checksum += prom[i];
  868. if (checksum > 0xff) {
  869. checksum = (checksum + 1) & 0xff;
  870. }
  871. }
  872. prom[7] = 0xff - checksum;
  873. }
  874. static const VMStateDescription vmstate_dp8393x = {
  875. .name = "dp8393x",
  876. .version_id = 0,
  877. .minimum_version_id = 0,
  878. .fields = (VMStateField []) {
  879. VMSTATE_BUFFER_UNSAFE(cam, dp8393xState, 0, 16 * 6),
  880. VMSTATE_UINT16_ARRAY(regs, dp8393xState, 0x40),
  881. VMSTATE_END_OF_LIST()
  882. }
  883. };
  884. static Property dp8393x_properties[] = {
  885. DEFINE_NIC_PROPERTIES(dp8393xState, conf),
  886. DEFINE_PROP_LINK("dma_mr", dp8393xState, dma_mr,
  887. TYPE_MEMORY_REGION, MemoryRegion *),
  888. DEFINE_PROP_UINT8("it_shift", dp8393xState, it_shift, 0),
  889. DEFINE_PROP_BOOL("big_endian", dp8393xState, big_endian, false),
  890. DEFINE_PROP_END_OF_LIST(),
  891. };
  892. static void dp8393x_class_init(ObjectClass *klass, void *data)
  893. {
  894. DeviceClass *dc = DEVICE_CLASS(klass);
  895. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  896. dc->realize = dp8393x_realize;
  897. dc->reset = dp8393x_reset;
  898. dc->vmsd = &vmstate_dp8393x;
  899. device_class_set_props(dc, dp8393x_properties);
  900. }
  901. static const TypeInfo dp8393x_info = {
  902. .name = TYPE_DP8393X,
  903. .parent = TYPE_SYS_BUS_DEVICE,
  904. .instance_size = sizeof(dp8393xState),
  905. .instance_init = dp8393x_instance_init,
  906. .class_init = dp8393x_class_init,
  907. };
  908. static void dp8393x_register_types(void)
  909. {
  910. type_register_static(&dp8393x_info);
  911. }
  912. type_init(dp8393x_register_types)