viohs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /* viohs.c: LDOM Virtual I/O handshake helper layer.
  2. *
  3. * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/export.h>
  7. #include <linux/string.h>
  8. #include <linux/delay.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <asm/ldc.h>
  12. #include <asm/vio.h>
  13. int vio_ldc_send(struct vio_driver_state *vio, void *data, int len)
  14. {
  15. int err, limit = 1000;
  16. err = -EINVAL;
  17. while (limit-- > 0) {
  18. err = ldc_write(vio->lp, data, len);
  19. if (!err || (err != -EAGAIN))
  20. break;
  21. udelay(1);
  22. }
  23. return err;
  24. }
  25. EXPORT_SYMBOL(vio_ldc_send);
  26. static int send_ctrl(struct vio_driver_state *vio,
  27. struct vio_msg_tag *tag, int len)
  28. {
  29. tag->sid = vio_send_sid(vio);
  30. return vio_ldc_send(vio, tag, len);
  31. }
  32. static void init_tag(struct vio_msg_tag *tag, u8 type, u8 stype, u16 stype_env)
  33. {
  34. tag->type = type;
  35. tag->stype = stype;
  36. tag->stype_env = stype_env;
  37. }
  38. static int send_version(struct vio_driver_state *vio, u16 major, u16 minor)
  39. {
  40. struct vio_ver_info pkt;
  41. vio->_local_sid = (u32) sched_clock();
  42. memset(&pkt, 0, sizeof(pkt));
  43. init_tag(&pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_VER_INFO);
  44. pkt.major = major;
  45. pkt.minor = minor;
  46. pkt.dev_class = vio->dev_class;
  47. viodbg(HS, "SEND VERSION INFO maj[%u] min[%u] devclass[%u]\n",
  48. major, minor, vio->dev_class);
  49. return send_ctrl(vio, &pkt.tag, sizeof(pkt));
  50. }
  51. static int start_handshake(struct vio_driver_state *vio)
  52. {
  53. int err;
  54. viodbg(HS, "START HANDSHAKE\n");
  55. vio->hs_state = VIO_HS_INVALID;
  56. err = send_version(vio,
  57. vio->ver_table[0].major,
  58. vio->ver_table[0].minor);
  59. if (err < 0)
  60. return err;
  61. return 0;
  62. }
  63. static void flush_rx_dring(struct vio_driver_state *vio)
  64. {
  65. struct vio_dring_state *dr;
  66. u64 ident;
  67. BUG_ON(!(vio->dr_state & VIO_DR_STATE_RXREG));
  68. dr = &vio->drings[VIO_DRIVER_RX_RING];
  69. ident = dr->ident;
  70. BUG_ON(!vio->desc_buf);
  71. kfree(vio->desc_buf);
  72. vio->desc_buf = NULL;
  73. memset(dr, 0, sizeof(*dr));
  74. dr->ident = ident;
  75. }
  76. void vio_link_state_change(struct vio_driver_state *vio, int event)
  77. {
  78. if (event == LDC_EVENT_UP) {
  79. vio->hs_state = VIO_HS_INVALID;
  80. switch (vio->dev_class) {
  81. case VDEV_NETWORK:
  82. case VDEV_NETWORK_SWITCH:
  83. vio->dr_state = (VIO_DR_STATE_TXREQ |
  84. VIO_DR_STATE_RXREQ);
  85. break;
  86. case VDEV_DISK:
  87. vio->dr_state = VIO_DR_STATE_TXREQ;
  88. break;
  89. case VDEV_DISK_SERVER:
  90. vio->dr_state = VIO_DR_STATE_RXREQ;
  91. break;
  92. }
  93. start_handshake(vio);
  94. } else if (event == LDC_EVENT_RESET) {
  95. vio->hs_state = VIO_HS_INVALID;
  96. if (vio->dr_state & VIO_DR_STATE_RXREG)
  97. flush_rx_dring(vio);
  98. vio->dr_state = 0x00;
  99. memset(&vio->ver, 0, sizeof(vio->ver));
  100. ldc_disconnect(vio->lp);
  101. }
  102. }
  103. EXPORT_SYMBOL(vio_link_state_change);
  104. static int handshake_failure(struct vio_driver_state *vio)
  105. {
  106. struct vio_dring_state *dr;
  107. /* XXX Put policy here... Perhaps start a timer to fire
  108. * XXX in 100 ms, which will bring the link up and retry
  109. * XXX the handshake.
  110. */
  111. viodbg(HS, "HANDSHAKE FAILURE\n");
  112. vio->dr_state &= ~(VIO_DR_STATE_TXREG |
  113. VIO_DR_STATE_RXREG);
  114. dr = &vio->drings[VIO_DRIVER_RX_RING];
  115. memset(dr, 0, sizeof(*dr));
  116. kfree(vio->desc_buf);
  117. vio->desc_buf = NULL;
  118. vio->desc_buf_len = 0;
  119. vio->hs_state = VIO_HS_INVALID;
  120. return -ECONNRESET;
  121. }
  122. static int process_unknown(struct vio_driver_state *vio, void *arg)
  123. {
  124. struct vio_msg_tag *pkt = arg;
  125. viodbg(HS, "UNKNOWN CONTROL [%02x:%02x:%04x:%08x]\n",
  126. pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
  127. printk(KERN_ERR "vio: ID[%lu] Resetting connection.\n",
  128. vio->vdev->channel_id);
  129. ldc_disconnect(vio->lp);
  130. return -ECONNRESET;
  131. }
  132. static int send_dreg(struct vio_driver_state *vio)
  133. {
  134. struct vio_dring_state *dr = &vio->drings[VIO_DRIVER_TX_RING];
  135. union {
  136. struct vio_dring_register pkt;
  137. char all[sizeof(struct vio_dring_register) +
  138. (sizeof(struct ldc_trans_cookie) *
  139. dr->ncookies)];
  140. } u;
  141. int i;
  142. memset(&u, 0, sizeof(u));
  143. init_tag(&u.pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_DRING_REG);
  144. u.pkt.dring_ident = 0;
  145. u.pkt.num_descr = dr->num_entries;
  146. u.pkt.descr_size = dr->entry_size;
  147. u.pkt.options = VIO_TX_DRING;
  148. u.pkt.num_cookies = dr->ncookies;
  149. viodbg(HS, "SEND DRING_REG INFO ndesc[%u] dsz[%u] opt[0x%x] "
  150. "ncookies[%u]\n",
  151. u.pkt.num_descr, u.pkt.descr_size, u.pkt.options,
  152. u.pkt.num_cookies);
  153. for (i = 0; i < dr->ncookies; i++) {
  154. u.pkt.cookies[i] = dr->cookies[i];
  155. viodbg(HS, "DRING COOKIE(%d) [%016llx:%016llx]\n",
  156. i,
  157. (unsigned long long) u.pkt.cookies[i].cookie_addr,
  158. (unsigned long long) u.pkt.cookies[i].cookie_size);
  159. }
  160. return send_ctrl(vio, &u.pkt.tag, sizeof(u));
  161. }
  162. static int send_rdx(struct vio_driver_state *vio)
  163. {
  164. struct vio_rdx pkt;
  165. memset(&pkt, 0, sizeof(pkt));
  166. init_tag(&pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX);
  167. viodbg(HS, "SEND RDX INFO\n");
  168. return send_ctrl(vio, &pkt.tag, sizeof(pkt));
  169. }
  170. static int send_attr(struct vio_driver_state *vio)
  171. {
  172. return vio->ops->send_attr(vio);
  173. }
  174. static struct vio_version *find_by_major(struct vio_driver_state *vio,
  175. u16 major)
  176. {
  177. struct vio_version *ret = NULL;
  178. int i;
  179. for (i = 0; i < vio->ver_table_entries; i++) {
  180. struct vio_version *v = &vio->ver_table[i];
  181. if (v->major <= major) {
  182. ret = v;
  183. break;
  184. }
  185. }
  186. return ret;
  187. }
  188. static int process_ver_info(struct vio_driver_state *vio,
  189. struct vio_ver_info *pkt)
  190. {
  191. struct vio_version *vap;
  192. int err;
  193. viodbg(HS, "GOT VERSION INFO maj[%u] min[%u] devclass[%u]\n",
  194. pkt->major, pkt->minor, pkt->dev_class);
  195. if (vio->hs_state != VIO_HS_INVALID) {
  196. /* XXX Perhaps invoke start_handshake? XXX */
  197. memset(&vio->ver, 0, sizeof(vio->ver));
  198. vio->hs_state = VIO_HS_INVALID;
  199. }
  200. vap = find_by_major(vio, pkt->major);
  201. vio->_peer_sid = pkt->tag.sid;
  202. if (!vap) {
  203. pkt->tag.stype = VIO_SUBTYPE_NACK;
  204. pkt->major = 0;
  205. pkt->minor = 0;
  206. viodbg(HS, "SEND VERSION NACK maj[0] min[0]\n");
  207. err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
  208. } else if (vap->major != pkt->major) {
  209. pkt->tag.stype = VIO_SUBTYPE_NACK;
  210. pkt->major = vap->major;
  211. pkt->minor = vap->minor;
  212. viodbg(HS, "SEND VERSION NACK maj[%u] min[%u]\n",
  213. pkt->major, pkt->minor);
  214. err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
  215. } else {
  216. struct vio_version ver = {
  217. .major = pkt->major,
  218. .minor = pkt->minor,
  219. };
  220. if (ver.minor > vap->minor)
  221. ver.minor = vap->minor;
  222. pkt->minor = ver.minor;
  223. pkt->tag.stype = VIO_SUBTYPE_ACK;
  224. viodbg(HS, "SEND VERSION ACK maj[%u] min[%u]\n",
  225. pkt->major, pkt->minor);
  226. err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
  227. if (err > 0) {
  228. vio->ver = ver;
  229. vio->hs_state = VIO_HS_GOTVERS;
  230. }
  231. }
  232. if (err < 0)
  233. return handshake_failure(vio);
  234. return 0;
  235. }
  236. static int process_ver_ack(struct vio_driver_state *vio,
  237. struct vio_ver_info *pkt)
  238. {
  239. viodbg(HS, "GOT VERSION ACK maj[%u] min[%u] devclass[%u]\n",
  240. pkt->major, pkt->minor, pkt->dev_class);
  241. if (vio->hs_state & VIO_HS_GOTVERS) {
  242. if (vio->ver.major != pkt->major ||
  243. vio->ver.minor != pkt->minor) {
  244. pkt->tag.stype = VIO_SUBTYPE_NACK;
  245. (void) send_ctrl(vio, &pkt->tag, sizeof(*pkt));
  246. return handshake_failure(vio);
  247. }
  248. } else {
  249. vio->ver.major = pkt->major;
  250. vio->ver.minor = pkt->minor;
  251. vio->hs_state = VIO_HS_GOTVERS;
  252. }
  253. switch (vio->dev_class) {
  254. case VDEV_NETWORK:
  255. case VDEV_DISK:
  256. if (send_attr(vio) < 0)
  257. return handshake_failure(vio);
  258. break;
  259. default:
  260. break;
  261. }
  262. return 0;
  263. }
  264. static int process_ver_nack(struct vio_driver_state *vio,
  265. struct vio_ver_info *pkt)
  266. {
  267. struct vio_version *nver;
  268. viodbg(HS, "GOT VERSION NACK maj[%u] min[%u] devclass[%u]\n",
  269. pkt->major, pkt->minor, pkt->dev_class);
  270. if (pkt->major == 0 && pkt->minor == 0)
  271. return handshake_failure(vio);
  272. nver = find_by_major(vio, pkt->major);
  273. if (!nver)
  274. return handshake_failure(vio);
  275. if (send_version(vio, nver->major, nver->minor) < 0)
  276. return handshake_failure(vio);
  277. return 0;
  278. }
  279. static int process_ver(struct vio_driver_state *vio, struct vio_ver_info *pkt)
  280. {
  281. switch (pkt->tag.stype) {
  282. case VIO_SUBTYPE_INFO:
  283. return process_ver_info(vio, pkt);
  284. case VIO_SUBTYPE_ACK:
  285. return process_ver_ack(vio, pkt);
  286. case VIO_SUBTYPE_NACK:
  287. return process_ver_nack(vio, pkt);
  288. default:
  289. return handshake_failure(vio);
  290. }
  291. }
  292. static int process_attr(struct vio_driver_state *vio, void *pkt)
  293. {
  294. int err;
  295. if (!(vio->hs_state & VIO_HS_GOTVERS))
  296. return handshake_failure(vio);
  297. err = vio->ops->handle_attr(vio, pkt);
  298. if (err < 0) {
  299. return handshake_failure(vio);
  300. } else {
  301. vio->hs_state |= VIO_HS_GOT_ATTR;
  302. if ((vio->dr_state & VIO_DR_STATE_TXREQ) &&
  303. !(vio->hs_state & VIO_HS_SENT_DREG)) {
  304. if (send_dreg(vio) < 0)
  305. return handshake_failure(vio);
  306. vio->hs_state |= VIO_HS_SENT_DREG;
  307. }
  308. }
  309. return 0;
  310. }
  311. static int all_drings_registered(struct vio_driver_state *vio)
  312. {
  313. int need_rx, need_tx;
  314. need_rx = (vio->dr_state & VIO_DR_STATE_RXREQ);
  315. need_tx = (vio->dr_state & VIO_DR_STATE_TXREQ);
  316. if (need_rx &&
  317. !(vio->dr_state & VIO_DR_STATE_RXREG))
  318. return 0;
  319. if (need_tx &&
  320. !(vio->dr_state & VIO_DR_STATE_TXREG))
  321. return 0;
  322. return 1;
  323. }
  324. static int process_dreg_info(struct vio_driver_state *vio,
  325. struct vio_dring_register *pkt)
  326. {
  327. struct vio_dring_state *dr;
  328. int i, len;
  329. viodbg(HS, "GOT DRING_REG INFO ident[%llx] "
  330. "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
  331. (unsigned long long) pkt->dring_ident,
  332. pkt->num_descr, pkt->descr_size, pkt->options,
  333. pkt->num_cookies);
  334. if (!(vio->dr_state & VIO_DR_STATE_RXREQ))
  335. goto send_nack;
  336. if (vio->dr_state & VIO_DR_STATE_RXREG)
  337. goto send_nack;
  338. /* v1.6 and higher, ACK with desired, supported mode, or NACK */
  339. if (vio_version_after_eq(vio, 1, 6)) {
  340. if (!(pkt->options & VIO_TX_DRING))
  341. goto send_nack;
  342. pkt->options = VIO_TX_DRING;
  343. }
  344. BUG_ON(vio->desc_buf);
  345. vio->desc_buf = kzalloc(pkt->descr_size, GFP_ATOMIC);
  346. if (!vio->desc_buf)
  347. goto send_nack;
  348. vio->desc_buf_len = pkt->descr_size;
  349. dr = &vio->drings[VIO_DRIVER_RX_RING];
  350. dr->num_entries = pkt->num_descr;
  351. dr->entry_size = pkt->descr_size;
  352. dr->ncookies = pkt->num_cookies;
  353. for (i = 0; i < dr->ncookies; i++) {
  354. dr->cookies[i] = pkt->cookies[i];
  355. viodbg(HS, "DRING COOKIE(%d) [%016llx:%016llx]\n",
  356. i,
  357. (unsigned long long)
  358. pkt->cookies[i].cookie_addr,
  359. (unsigned long long)
  360. pkt->cookies[i].cookie_size);
  361. }
  362. pkt->tag.stype = VIO_SUBTYPE_ACK;
  363. pkt->dring_ident = ++dr->ident;
  364. viodbg(HS, "SEND DRING_REG ACK ident[%llx] "
  365. "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
  366. (unsigned long long) pkt->dring_ident,
  367. pkt->num_descr, pkt->descr_size, pkt->options,
  368. pkt->num_cookies);
  369. len = (sizeof(*pkt) +
  370. (dr->ncookies * sizeof(struct ldc_trans_cookie)));
  371. if (send_ctrl(vio, &pkt->tag, len) < 0)
  372. goto send_nack;
  373. vio->dr_state |= VIO_DR_STATE_RXREG;
  374. return 0;
  375. send_nack:
  376. pkt->tag.stype = VIO_SUBTYPE_NACK;
  377. viodbg(HS, "SEND DRING_REG NACK\n");
  378. (void) send_ctrl(vio, &pkt->tag, sizeof(*pkt));
  379. return handshake_failure(vio);
  380. }
  381. static int process_dreg_ack(struct vio_driver_state *vio,
  382. struct vio_dring_register *pkt)
  383. {
  384. struct vio_dring_state *dr;
  385. viodbg(HS, "GOT DRING_REG ACK ident[%llx] "
  386. "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
  387. (unsigned long long) pkt->dring_ident,
  388. pkt->num_descr, pkt->descr_size, pkt->options,
  389. pkt->num_cookies);
  390. dr = &vio->drings[VIO_DRIVER_TX_RING];
  391. if (!(vio->dr_state & VIO_DR_STATE_TXREQ))
  392. return handshake_failure(vio);
  393. dr->ident = pkt->dring_ident;
  394. vio->dr_state |= VIO_DR_STATE_TXREG;
  395. if (all_drings_registered(vio)) {
  396. if (send_rdx(vio) < 0)
  397. return handshake_failure(vio);
  398. vio->hs_state = VIO_HS_SENT_RDX;
  399. }
  400. return 0;
  401. }
  402. static int process_dreg_nack(struct vio_driver_state *vio,
  403. struct vio_dring_register *pkt)
  404. {
  405. viodbg(HS, "GOT DRING_REG NACK ident[%llx] "
  406. "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
  407. (unsigned long long) pkt->dring_ident,
  408. pkt->num_descr, pkt->descr_size, pkt->options,
  409. pkt->num_cookies);
  410. return handshake_failure(vio);
  411. }
  412. static int process_dreg(struct vio_driver_state *vio,
  413. struct vio_dring_register *pkt)
  414. {
  415. if (!(vio->hs_state & VIO_HS_GOTVERS))
  416. return handshake_failure(vio);
  417. switch (pkt->tag.stype) {
  418. case VIO_SUBTYPE_INFO:
  419. return process_dreg_info(vio, pkt);
  420. case VIO_SUBTYPE_ACK:
  421. return process_dreg_ack(vio, pkt);
  422. case VIO_SUBTYPE_NACK:
  423. return process_dreg_nack(vio, pkt);
  424. default:
  425. return handshake_failure(vio);
  426. }
  427. }
  428. static int process_dunreg(struct vio_driver_state *vio,
  429. struct vio_dring_unregister *pkt)
  430. {
  431. struct vio_dring_state *dr = &vio->drings[VIO_DRIVER_RX_RING];
  432. viodbg(HS, "GOT DRING_UNREG\n");
  433. if (pkt->dring_ident != dr->ident)
  434. return 0;
  435. vio->dr_state &= ~VIO_DR_STATE_RXREG;
  436. memset(dr, 0, sizeof(*dr));
  437. kfree(vio->desc_buf);
  438. vio->desc_buf = NULL;
  439. vio->desc_buf_len = 0;
  440. return 0;
  441. }
  442. static int process_rdx_info(struct vio_driver_state *vio, struct vio_rdx *pkt)
  443. {
  444. viodbg(HS, "GOT RDX INFO\n");
  445. pkt->tag.stype = VIO_SUBTYPE_ACK;
  446. viodbg(HS, "SEND RDX ACK\n");
  447. if (send_ctrl(vio, &pkt->tag, sizeof(*pkt)) < 0)
  448. return handshake_failure(vio);
  449. vio->hs_state |= VIO_HS_SENT_RDX_ACK;
  450. return 0;
  451. }
  452. static int process_rdx_ack(struct vio_driver_state *vio, struct vio_rdx *pkt)
  453. {
  454. viodbg(HS, "GOT RDX ACK\n");
  455. if (!(vio->hs_state & VIO_HS_SENT_RDX))
  456. return handshake_failure(vio);
  457. vio->hs_state |= VIO_HS_GOT_RDX_ACK;
  458. return 0;
  459. }
  460. static int process_rdx_nack(struct vio_driver_state *vio, struct vio_rdx *pkt)
  461. {
  462. viodbg(HS, "GOT RDX NACK\n");
  463. return handshake_failure(vio);
  464. }
  465. static int process_rdx(struct vio_driver_state *vio, struct vio_rdx *pkt)
  466. {
  467. if (!all_drings_registered(vio))
  468. handshake_failure(vio);
  469. switch (pkt->tag.stype) {
  470. case VIO_SUBTYPE_INFO:
  471. return process_rdx_info(vio, pkt);
  472. case VIO_SUBTYPE_ACK:
  473. return process_rdx_ack(vio, pkt);
  474. case VIO_SUBTYPE_NACK:
  475. return process_rdx_nack(vio, pkt);
  476. default:
  477. return handshake_failure(vio);
  478. }
  479. }
  480. int vio_control_pkt_engine(struct vio_driver_state *vio, void *pkt)
  481. {
  482. struct vio_msg_tag *tag = pkt;
  483. u8 prev_state = vio->hs_state;
  484. int err;
  485. switch (tag->stype_env) {
  486. case VIO_VER_INFO:
  487. err = process_ver(vio, pkt);
  488. break;
  489. case VIO_ATTR_INFO:
  490. err = process_attr(vio, pkt);
  491. break;
  492. case VIO_DRING_REG:
  493. err = process_dreg(vio, pkt);
  494. break;
  495. case VIO_DRING_UNREG:
  496. err = process_dunreg(vio, pkt);
  497. break;
  498. case VIO_RDX:
  499. err = process_rdx(vio, pkt);
  500. break;
  501. default:
  502. err = process_unknown(vio, pkt);
  503. break;
  504. }
  505. if (!err &&
  506. vio->hs_state != prev_state &&
  507. (vio->hs_state & VIO_HS_COMPLETE))
  508. vio->ops->handshake_complete(vio);
  509. return err;
  510. }
  511. EXPORT_SYMBOL(vio_control_pkt_engine);
  512. void vio_conn_reset(struct vio_driver_state *vio)
  513. {
  514. }
  515. EXPORT_SYMBOL(vio_conn_reset);
  516. /* The issue is that the Solaris virtual disk server just mirrors the
  517. * SID values it gets from the client peer. So we work around that
  518. * here in vio_{validate,send}_sid() so that the drivers don't need
  519. * to be aware of this crap.
  520. */
  521. int vio_validate_sid(struct vio_driver_state *vio, struct vio_msg_tag *tp)
  522. {
  523. u32 sid;
  524. /* Always let VERSION+INFO packets through unchecked, they
  525. * define the new SID.
  526. */
  527. if (tp->type == VIO_TYPE_CTRL &&
  528. tp->stype == VIO_SUBTYPE_INFO &&
  529. tp->stype_env == VIO_VER_INFO)
  530. return 0;
  531. /* Ok, now figure out which SID to use. */
  532. switch (vio->dev_class) {
  533. case VDEV_NETWORK:
  534. case VDEV_NETWORK_SWITCH:
  535. case VDEV_DISK_SERVER:
  536. default:
  537. sid = vio->_peer_sid;
  538. break;
  539. case VDEV_DISK:
  540. sid = vio->_local_sid;
  541. break;
  542. }
  543. if (sid == tp->sid)
  544. return 0;
  545. viodbg(DATA, "BAD SID tag->sid[%08x] peer_sid[%08x] local_sid[%08x]\n",
  546. tp->sid, vio->_peer_sid, vio->_local_sid);
  547. return -EINVAL;
  548. }
  549. EXPORT_SYMBOL(vio_validate_sid);
  550. u32 vio_send_sid(struct vio_driver_state *vio)
  551. {
  552. switch (vio->dev_class) {
  553. case VDEV_NETWORK:
  554. case VDEV_NETWORK_SWITCH:
  555. case VDEV_DISK:
  556. default:
  557. return vio->_local_sid;
  558. case VDEV_DISK_SERVER:
  559. return vio->_peer_sid;
  560. }
  561. }
  562. EXPORT_SYMBOL(vio_send_sid);
  563. int vio_ldc_alloc(struct vio_driver_state *vio,
  564. struct ldc_channel_config *base_cfg,
  565. void *event_arg)
  566. {
  567. struct ldc_channel_config cfg = *base_cfg;
  568. struct ldc_channel *lp;
  569. cfg.tx_irq = vio->vdev->tx_irq;
  570. cfg.rx_irq = vio->vdev->rx_irq;
  571. lp = ldc_alloc(vio->vdev->channel_id, &cfg, event_arg, vio->name);
  572. if (IS_ERR(lp))
  573. return PTR_ERR(lp);
  574. vio->lp = lp;
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(vio_ldc_alloc);
  578. void vio_ldc_free(struct vio_driver_state *vio)
  579. {
  580. ldc_free(vio->lp);
  581. vio->lp = NULL;
  582. kfree(vio->desc_buf);
  583. vio->desc_buf = NULL;
  584. vio->desc_buf_len = 0;
  585. }
  586. EXPORT_SYMBOL(vio_ldc_free);
  587. void vio_port_up(struct vio_driver_state *vio)
  588. {
  589. unsigned long flags;
  590. int err, state;
  591. spin_lock_irqsave(&vio->lock, flags);
  592. state = ldc_state(vio->lp);
  593. err = 0;
  594. if (state == LDC_STATE_INIT) {
  595. err = ldc_bind(vio->lp);
  596. if (err)
  597. printk(KERN_WARNING "%s: Port %lu bind failed, "
  598. "err=%d\n",
  599. vio->name, vio->vdev->channel_id, err);
  600. }
  601. if (!err) {
  602. err = ldc_connect(vio->lp);
  603. if (err)
  604. printk(KERN_WARNING "%s: Port %lu connect failed, "
  605. "err=%d\n",
  606. vio->name, vio->vdev->channel_id, err);
  607. }
  608. if (err) {
  609. unsigned long expires = jiffies + HZ;
  610. expires = round_jiffies(expires);
  611. mod_timer(&vio->timer, expires);
  612. }
  613. spin_unlock_irqrestore(&vio->lock, flags);
  614. }
  615. EXPORT_SYMBOL(vio_port_up);
  616. static void vio_port_timer(unsigned long _arg)
  617. {
  618. struct vio_driver_state *vio = (struct vio_driver_state *) _arg;
  619. vio_port_up(vio);
  620. }
  621. int vio_driver_init(struct vio_driver_state *vio, struct vio_dev *vdev,
  622. u8 dev_class, struct vio_version *ver_table,
  623. int ver_table_size, struct vio_driver_ops *ops,
  624. char *name)
  625. {
  626. switch (dev_class) {
  627. case VDEV_NETWORK:
  628. case VDEV_NETWORK_SWITCH:
  629. case VDEV_DISK:
  630. case VDEV_DISK_SERVER:
  631. break;
  632. default:
  633. return -EINVAL;
  634. }
  635. if (!ops->send_attr ||
  636. !ops->handle_attr ||
  637. !ops->handshake_complete)
  638. return -EINVAL;
  639. if (!ver_table || ver_table_size < 0)
  640. return -EINVAL;
  641. if (!name)
  642. return -EINVAL;
  643. spin_lock_init(&vio->lock);
  644. vio->name = name;
  645. vio->dev_class = dev_class;
  646. vio->vdev = vdev;
  647. vio->ver_table = ver_table;
  648. vio->ver_table_entries = ver_table_size;
  649. vio->ops = ops;
  650. setup_timer(&vio->timer, vio_port_timer, (unsigned long) vio);
  651. return 0;
  652. }
  653. EXPORT_SYMBOL(vio_driver_init);