viohs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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/module.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. BUG_ON(vio->desc_buf);
  339. vio->desc_buf = kzalloc(pkt->descr_size, GFP_ATOMIC);
  340. if (!vio->desc_buf)
  341. goto send_nack;
  342. vio->desc_buf_len = pkt->descr_size;
  343. dr = &vio->drings[VIO_DRIVER_RX_RING];
  344. dr->num_entries = pkt->num_descr;
  345. dr->entry_size = pkt->descr_size;
  346. dr->ncookies = pkt->num_cookies;
  347. for (i = 0; i < dr->ncookies; i++) {
  348. dr->cookies[i] = pkt->cookies[i];
  349. viodbg(HS, "DRING COOKIE(%d) [%016llx:%016llx]\n",
  350. i,
  351. (unsigned long long)
  352. pkt->cookies[i].cookie_addr,
  353. (unsigned long long)
  354. pkt->cookies[i].cookie_size);
  355. }
  356. pkt->tag.stype = VIO_SUBTYPE_ACK;
  357. pkt->dring_ident = ++dr->ident;
  358. viodbg(HS, "SEND DRING_REG ACK ident[%llx]\n",
  359. (unsigned long long) pkt->dring_ident);
  360. len = (sizeof(*pkt) +
  361. (dr->ncookies * sizeof(struct ldc_trans_cookie)));
  362. if (send_ctrl(vio, &pkt->tag, len) < 0)
  363. goto send_nack;
  364. vio->dr_state |= VIO_DR_STATE_RXREG;
  365. return 0;
  366. send_nack:
  367. pkt->tag.stype = VIO_SUBTYPE_NACK;
  368. viodbg(HS, "SEND DRING_REG NACK\n");
  369. (void) send_ctrl(vio, &pkt->tag, sizeof(*pkt));
  370. return handshake_failure(vio);
  371. }
  372. static int process_dreg_ack(struct vio_driver_state *vio,
  373. struct vio_dring_register *pkt)
  374. {
  375. struct vio_dring_state *dr;
  376. viodbg(HS, "GOT DRING_REG ACK ident[%llx] "
  377. "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
  378. (unsigned long long) pkt->dring_ident,
  379. pkt->num_descr, pkt->descr_size, pkt->options,
  380. pkt->num_cookies);
  381. dr = &vio->drings[VIO_DRIVER_TX_RING];
  382. if (!(vio->dr_state & VIO_DR_STATE_TXREQ))
  383. return handshake_failure(vio);
  384. dr->ident = pkt->dring_ident;
  385. vio->dr_state |= VIO_DR_STATE_TXREG;
  386. if (all_drings_registered(vio)) {
  387. if (send_rdx(vio) < 0)
  388. return handshake_failure(vio);
  389. vio->hs_state = VIO_HS_SENT_RDX;
  390. }
  391. return 0;
  392. }
  393. static int process_dreg_nack(struct vio_driver_state *vio,
  394. struct vio_dring_register *pkt)
  395. {
  396. viodbg(HS, "GOT DRING_REG NACK ident[%llx] "
  397. "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
  398. (unsigned long long) pkt->dring_ident,
  399. pkt->num_descr, pkt->descr_size, pkt->options,
  400. pkt->num_cookies);
  401. return handshake_failure(vio);
  402. }
  403. static int process_dreg(struct vio_driver_state *vio,
  404. struct vio_dring_register *pkt)
  405. {
  406. if (!(vio->hs_state & VIO_HS_GOTVERS))
  407. return handshake_failure(vio);
  408. switch (pkt->tag.stype) {
  409. case VIO_SUBTYPE_INFO:
  410. return process_dreg_info(vio, pkt);
  411. case VIO_SUBTYPE_ACK:
  412. return process_dreg_ack(vio, pkt);
  413. case VIO_SUBTYPE_NACK:
  414. return process_dreg_nack(vio, pkt);
  415. default:
  416. return handshake_failure(vio);
  417. }
  418. }
  419. static int process_dunreg(struct vio_driver_state *vio,
  420. struct vio_dring_unregister *pkt)
  421. {
  422. struct vio_dring_state *dr = &vio->drings[VIO_DRIVER_RX_RING];
  423. viodbg(HS, "GOT DRING_UNREG\n");
  424. if (pkt->dring_ident != dr->ident)
  425. return 0;
  426. vio->dr_state &= ~VIO_DR_STATE_RXREG;
  427. memset(dr, 0, sizeof(*dr));
  428. kfree(vio->desc_buf);
  429. vio->desc_buf = NULL;
  430. vio->desc_buf_len = 0;
  431. return 0;
  432. }
  433. static int process_rdx_info(struct vio_driver_state *vio, struct vio_rdx *pkt)
  434. {
  435. viodbg(HS, "GOT RDX INFO\n");
  436. pkt->tag.stype = VIO_SUBTYPE_ACK;
  437. viodbg(HS, "SEND RDX ACK\n");
  438. if (send_ctrl(vio, &pkt->tag, sizeof(*pkt)) < 0)
  439. return handshake_failure(vio);
  440. vio->hs_state |= VIO_HS_SENT_RDX_ACK;
  441. return 0;
  442. }
  443. static int process_rdx_ack(struct vio_driver_state *vio, struct vio_rdx *pkt)
  444. {
  445. viodbg(HS, "GOT RDX ACK\n");
  446. if (!(vio->hs_state & VIO_HS_SENT_RDX))
  447. return handshake_failure(vio);
  448. vio->hs_state |= VIO_HS_GOT_RDX_ACK;
  449. return 0;
  450. }
  451. static int process_rdx_nack(struct vio_driver_state *vio, struct vio_rdx *pkt)
  452. {
  453. viodbg(HS, "GOT RDX NACK\n");
  454. return handshake_failure(vio);
  455. }
  456. static int process_rdx(struct vio_driver_state *vio, struct vio_rdx *pkt)
  457. {
  458. if (!all_drings_registered(vio))
  459. handshake_failure(vio);
  460. switch (pkt->tag.stype) {
  461. case VIO_SUBTYPE_INFO:
  462. return process_rdx_info(vio, pkt);
  463. case VIO_SUBTYPE_ACK:
  464. return process_rdx_ack(vio, pkt);
  465. case VIO_SUBTYPE_NACK:
  466. return process_rdx_nack(vio, pkt);
  467. default:
  468. return handshake_failure(vio);
  469. }
  470. }
  471. int vio_control_pkt_engine(struct vio_driver_state *vio, void *pkt)
  472. {
  473. struct vio_msg_tag *tag = pkt;
  474. u8 prev_state = vio->hs_state;
  475. int err;
  476. switch (tag->stype_env) {
  477. case VIO_VER_INFO:
  478. err = process_ver(vio, pkt);
  479. break;
  480. case VIO_ATTR_INFO:
  481. err = process_attr(vio, pkt);
  482. break;
  483. case VIO_DRING_REG:
  484. err = process_dreg(vio, pkt);
  485. break;
  486. case VIO_DRING_UNREG:
  487. err = process_dunreg(vio, pkt);
  488. break;
  489. case VIO_RDX:
  490. err = process_rdx(vio, pkt);
  491. break;
  492. default:
  493. err = process_unknown(vio, pkt);
  494. break;
  495. }
  496. if (!err &&
  497. vio->hs_state != prev_state &&
  498. (vio->hs_state & VIO_HS_COMPLETE))
  499. vio->ops->handshake_complete(vio);
  500. return err;
  501. }
  502. EXPORT_SYMBOL(vio_control_pkt_engine);
  503. void vio_conn_reset(struct vio_driver_state *vio)
  504. {
  505. }
  506. EXPORT_SYMBOL(vio_conn_reset);
  507. /* The issue is that the Solaris virtual disk server just mirrors the
  508. * SID values it gets from the client peer. So we work around that
  509. * here in vio_{validate,send}_sid() so that the drivers don't need
  510. * to be aware of this crap.
  511. */
  512. int vio_validate_sid(struct vio_driver_state *vio, struct vio_msg_tag *tp)
  513. {
  514. u32 sid;
  515. /* Always let VERSION+INFO packets through unchecked, they
  516. * define the new SID.
  517. */
  518. if (tp->type == VIO_TYPE_CTRL &&
  519. tp->stype == VIO_SUBTYPE_INFO &&
  520. tp->stype_env == VIO_VER_INFO)
  521. return 0;
  522. /* Ok, now figure out which SID to use. */
  523. switch (vio->dev_class) {
  524. case VDEV_NETWORK:
  525. case VDEV_NETWORK_SWITCH:
  526. case VDEV_DISK_SERVER:
  527. default:
  528. sid = vio->_peer_sid;
  529. break;
  530. case VDEV_DISK:
  531. sid = vio->_local_sid;
  532. break;
  533. }
  534. if (sid == tp->sid)
  535. return 0;
  536. viodbg(DATA, "BAD SID tag->sid[%08x] peer_sid[%08x] local_sid[%08x]\n",
  537. tp->sid, vio->_peer_sid, vio->_local_sid);
  538. return -EINVAL;
  539. }
  540. EXPORT_SYMBOL(vio_validate_sid);
  541. u32 vio_send_sid(struct vio_driver_state *vio)
  542. {
  543. switch (vio->dev_class) {
  544. case VDEV_NETWORK:
  545. case VDEV_NETWORK_SWITCH:
  546. case VDEV_DISK:
  547. default:
  548. return vio->_local_sid;
  549. case VDEV_DISK_SERVER:
  550. return vio->_peer_sid;
  551. }
  552. }
  553. EXPORT_SYMBOL(vio_send_sid);
  554. int vio_ldc_alloc(struct vio_driver_state *vio,
  555. struct ldc_channel_config *base_cfg,
  556. void *event_arg)
  557. {
  558. struct ldc_channel_config cfg = *base_cfg;
  559. struct ldc_channel *lp;
  560. cfg.tx_irq = vio->vdev->tx_irq;
  561. cfg.rx_irq = vio->vdev->rx_irq;
  562. lp = ldc_alloc(vio->vdev->channel_id, &cfg, event_arg);
  563. if (IS_ERR(lp))
  564. return PTR_ERR(lp);
  565. vio->lp = lp;
  566. return 0;
  567. }
  568. EXPORT_SYMBOL(vio_ldc_alloc);
  569. void vio_ldc_free(struct vio_driver_state *vio)
  570. {
  571. ldc_free(vio->lp);
  572. vio->lp = NULL;
  573. kfree(vio->desc_buf);
  574. vio->desc_buf = NULL;
  575. vio->desc_buf_len = 0;
  576. }
  577. EXPORT_SYMBOL(vio_ldc_free);
  578. void vio_port_up(struct vio_driver_state *vio)
  579. {
  580. unsigned long flags;
  581. int err, state;
  582. spin_lock_irqsave(&vio->lock, flags);
  583. state = ldc_state(vio->lp);
  584. err = 0;
  585. if (state == LDC_STATE_INIT) {
  586. err = ldc_bind(vio->lp, vio->name);
  587. if (err)
  588. printk(KERN_WARNING "%s: Port %lu bind failed, "
  589. "err=%d\n",
  590. vio->name, vio->vdev->channel_id, err);
  591. }
  592. if (!err) {
  593. err = ldc_connect(vio->lp);
  594. if (err)
  595. printk(KERN_WARNING "%s: Port %lu connect failed, "
  596. "err=%d\n",
  597. vio->name, vio->vdev->channel_id, err);
  598. }
  599. if (err) {
  600. unsigned long expires = jiffies + HZ;
  601. expires = round_jiffies(expires);
  602. mod_timer(&vio->timer, expires);
  603. }
  604. spin_unlock_irqrestore(&vio->lock, flags);
  605. }
  606. EXPORT_SYMBOL(vio_port_up);
  607. static void vio_port_timer(unsigned long _arg)
  608. {
  609. struct vio_driver_state *vio = (struct vio_driver_state *) _arg;
  610. vio_port_up(vio);
  611. }
  612. int vio_driver_init(struct vio_driver_state *vio, struct vio_dev *vdev,
  613. u8 dev_class, struct vio_version *ver_table,
  614. int ver_table_size, struct vio_driver_ops *ops,
  615. char *name)
  616. {
  617. switch (dev_class) {
  618. case VDEV_NETWORK:
  619. case VDEV_NETWORK_SWITCH:
  620. case VDEV_DISK:
  621. case VDEV_DISK_SERVER:
  622. break;
  623. default:
  624. return -EINVAL;
  625. }
  626. if (!ops->send_attr ||
  627. !ops->handle_attr ||
  628. !ops->handshake_complete)
  629. return -EINVAL;
  630. if (!ver_table || ver_table_size < 0)
  631. return -EINVAL;
  632. if (!name)
  633. return -EINVAL;
  634. spin_lock_init(&vio->lock);
  635. vio->name = name;
  636. vio->dev_class = dev_class;
  637. vio->vdev = vdev;
  638. vio->ver_table = ver_table;
  639. vio->ver_table_entries = ver_table_size;
  640. vio->ops = ops;
  641. setup_timer(&vio->timer, vio_port_timer, (unsigned long) vio);
  642. return 0;
  643. }
  644. EXPORT_SYMBOL(vio_driver_init);