um_idi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /* $Id: um_idi.c,v 1.14 2004/03/21 17:54:37 armin Exp $ */
  2. #include "platform.h"
  3. #include "di_defs.h"
  4. #include "pc.h"
  5. #include "dqueue.h"
  6. #include "adapter.h"
  7. #include "entity.h"
  8. #include "um_xdi.h"
  9. #include "um_idi.h"
  10. #include "debuglib.h"
  11. #include "divasync.h"
  12. #define DIVAS_MAX_XDI_ADAPTERS 64
  13. /* --------------------------------------------------------------------------
  14. IMPORTS
  15. -------------------------------------------------------------------------- */
  16. extern void diva_os_wakeup_read(void *os_context);
  17. extern void diva_os_wakeup_close(void *os_context);
  18. /* --------------------------------------------------------------------------
  19. LOCALS
  20. -------------------------------------------------------------------------- */
  21. static LIST_HEAD(adapter_q);
  22. static diva_os_spin_lock_t adapter_lock;
  23. static diva_um_idi_adapter_t *diva_um_idi_find_adapter(dword nr);
  24. static void cleanup_adapter(diva_um_idi_adapter_t *a);
  25. static void cleanup_entity(divas_um_idi_entity_t *e);
  26. static int diva_user_mode_idi_adapter_features(diva_um_idi_adapter_t *a,
  27. diva_um_idi_adapter_features_t
  28. *features);
  29. static int process_idi_request(divas_um_idi_entity_t *e,
  30. const diva_um_idi_req_hdr_t *req);
  31. static int process_idi_rc(divas_um_idi_entity_t *e, byte rc);
  32. static int process_idi_ind(divas_um_idi_entity_t *e, byte ind);
  33. static int write_return_code(divas_um_idi_entity_t *e, byte rc);
  34. /* --------------------------------------------------------------------------
  35. MAIN
  36. -------------------------------------------------------------------------- */
  37. int diva_user_mode_idi_init(void)
  38. {
  39. diva_os_initialize_spin_lock(&adapter_lock, "adapter");
  40. return (0);
  41. }
  42. /* --------------------------------------------------------------------------
  43. Copy adapter features to user supplied buffer
  44. -------------------------------------------------------------------------- */
  45. static int
  46. diva_user_mode_idi_adapter_features(diva_um_idi_adapter_t *a,
  47. diva_um_idi_adapter_features_t *
  48. features)
  49. {
  50. IDI_SYNC_REQ sync_req;
  51. if ((a) && (a->d.request)) {
  52. features->type = a->d.type;
  53. features->features = a->d.features;
  54. features->channels = a->d.channels;
  55. memset(features->name, 0, sizeof(features->name));
  56. sync_req.GetName.Req = 0;
  57. sync_req.GetName.Rc = IDI_SYNC_REQ_GET_NAME;
  58. (*(a->d.request)) ((ENTITY *)&sync_req);
  59. strlcpy(features->name, sync_req.GetName.name,
  60. sizeof(features->name));
  61. sync_req.GetSerial.Req = 0;
  62. sync_req.GetSerial.Rc = IDI_SYNC_REQ_GET_SERIAL;
  63. sync_req.GetSerial.serial = 0;
  64. (*(a->d.request))((ENTITY *)&sync_req);
  65. features->serial_number = sync_req.GetSerial.serial;
  66. }
  67. return ((a) ? 0 : -1);
  68. }
  69. /* --------------------------------------------------------------------------
  70. REMOVE ADAPTER
  71. -------------------------------------------------------------------------- */
  72. void diva_user_mode_idi_remove_adapter(int adapter_nr)
  73. {
  74. struct list_head *tmp;
  75. diva_um_idi_adapter_t *a;
  76. list_for_each(tmp, &adapter_q) {
  77. a = list_entry(tmp, diva_um_idi_adapter_t, link);
  78. if (a->adapter_nr == adapter_nr) {
  79. list_del(tmp);
  80. cleanup_adapter(a);
  81. DBG_LOG(("DIDD: del adapter(%d)", a->adapter_nr));
  82. diva_os_free(0, a);
  83. break;
  84. }
  85. }
  86. }
  87. /* --------------------------------------------------------------------------
  88. CALLED ON DRIVER EXIT (UNLOAD)
  89. -------------------------------------------------------------------------- */
  90. void diva_user_mode_idi_finit(void)
  91. {
  92. struct list_head *tmp, *safe;
  93. diva_um_idi_adapter_t *a;
  94. list_for_each_safe(tmp, safe, &adapter_q) {
  95. a = list_entry(tmp, diva_um_idi_adapter_t, link);
  96. list_del(tmp);
  97. cleanup_adapter(a);
  98. DBG_LOG(("DIDD: del adapter(%d)", a->adapter_nr));
  99. diva_os_free(0, a);
  100. }
  101. diva_os_destroy_spin_lock(&adapter_lock, "adapter");
  102. }
  103. /* -------------------------------------------------------------------------
  104. CREATE AND INIT IDI ADAPTER
  105. ------------------------------------------------------------------------- */
  106. int diva_user_mode_idi_create_adapter(const DESCRIPTOR *d, int adapter_nr)
  107. {
  108. diva_os_spin_lock_magic_t old_irql;
  109. diva_um_idi_adapter_t *a =
  110. (diva_um_idi_adapter_t *) diva_os_malloc(0,
  111. sizeof
  112. (diva_um_idi_adapter_t));
  113. if (!a) {
  114. return (-1);
  115. }
  116. memset(a, 0x00, sizeof(*a));
  117. INIT_LIST_HEAD(&a->entity_q);
  118. a->d = *d;
  119. a->adapter_nr = adapter_nr;
  120. DBG_LOG(("DIDD_ADD A(%d), type:%02x, features:%04x, channels:%d",
  121. adapter_nr, a->d.type, a->d.features, a->d.channels));
  122. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "create_adapter");
  123. list_add_tail(&a->link, &adapter_q);
  124. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_adapter");
  125. return (0);
  126. }
  127. /* ------------------------------------------------------------------------
  128. Find adapter by Adapter number
  129. ------------------------------------------------------------------------ */
  130. static diva_um_idi_adapter_t *diva_um_idi_find_adapter(dword nr)
  131. {
  132. diva_um_idi_adapter_t *a = NULL;
  133. struct list_head *tmp;
  134. list_for_each(tmp, &adapter_q) {
  135. a = list_entry(tmp, diva_um_idi_adapter_t, link);
  136. DBG_TRC(("find_adapter: (%d)-(%d)", nr, a->adapter_nr));
  137. if (a->adapter_nr == (int)nr)
  138. break;
  139. a = NULL;
  140. }
  141. return (a);
  142. }
  143. /* ------------------------------------------------------------------------
  144. Cleanup this adapter and cleanup/delete all entities assigned
  145. to this adapter
  146. ------------------------------------------------------------------------ */
  147. static void cleanup_adapter(diva_um_idi_adapter_t *a)
  148. {
  149. struct list_head *tmp, *safe;
  150. divas_um_idi_entity_t *e;
  151. list_for_each_safe(tmp, safe, &a->entity_q) {
  152. e = list_entry(tmp, divas_um_idi_entity_t, link);
  153. list_del(tmp);
  154. cleanup_entity(e);
  155. if (e->os_context) {
  156. diva_os_wakeup_read(e->os_context);
  157. diva_os_wakeup_close(e->os_context);
  158. }
  159. }
  160. memset(&a->d, 0x00, sizeof(DESCRIPTOR));
  161. }
  162. /* ------------------------------------------------------------------------
  163. Cleanup, but NOT delete this entity
  164. ------------------------------------------------------------------------ */
  165. static void cleanup_entity(divas_um_idi_entity_t *e)
  166. {
  167. e->os_ref = NULL;
  168. e->status = 0;
  169. e->adapter = NULL;
  170. e->e.Id = 0;
  171. e->rc_count = 0;
  172. e->status |= DIVA_UM_IDI_REMOVED;
  173. e->status |= DIVA_UM_IDI_REMOVE_PENDING;
  174. diva_data_q_finit(&e->data);
  175. diva_data_q_finit(&e->rc);
  176. }
  177. /* ------------------------------------------------------------------------
  178. Create ENTITY, link it to the adapter and remove pointer to entity
  179. ------------------------------------------------------------------------ */
  180. void *divas_um_idi_create_entity(dword adapter_nr, void *file)
  181. {
  182. divas_um_idi_entity_t *e;
  183. diva_um_idi_adapter_t *a;
  184. diva_os_spin_lock_magic_t old_irql;
  185. if ((e = (divas_um_idi_entity_t *) diva_os_malloc(0, sizeof(*e)))) {
  186. memset(e, 0x00, sizeof(*e));
  187. if (!
  188. (e->os_context =
  189. diva_os_malloc(0, diva_os_get_context_size()))) {
  190. DBG_LOG(("E(%08x) no memory for os context", e));
  191. diva_os_free(0, e);
  192. return NULL;
  193. }
  194. memset(e->os_context, 0x00, diva_os_get_context_size());
  195. if ((diva_data_q_init(&e->data, 2048 + 512, 16))) {
  196. diva_os_free(0, e->os_context);
  197. diva_os_free(0, e);
  198. return NULL;
  199. }
  200. if ((diva_data_q_init(&e->rc, sizeof(diva_um_idi_ind_hdr_t), 2))) {
  201. diva_data_q_finit(&e->data);
  202. diva_os_free(0, e->os_context);
  203. diva_os_free(0, e);
  204. return NULL;
  205. }
  206. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "create_entity");
  207. /*
  208. Look for Adapter requested
  209. */
  210. if (!(a = diva_um_idi_find_adapter(adapter_nr))) {
  211. /*
  212. No adapter was found, or this adapter was removed
  213. */
  214. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_entity");
  215. DBG_LOG(("A: no adapter(%ld)", adapter_nr));
  216. cleanup_entity(e);
  217. diva_os_free(0, e->os_context);
  218. diva_os_free(0, e);
  219. return NULL;
  220. }
  221. e->os_ref = file; /* link to os handle */
  222. e->adapter = a; /* link to adapter */
  223. list_add_tail(&e->link, &a->entity_q); /* link from adapter */
  224. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_entity");
  225. DBG_LOG(("A(%ld), create E(%08x)", adapter_nr, e));
  226. }
  227. return (e);
  228. }
  229. /* ------------------------------------------------------------------------
  230. Unlink entity and free memory
  231. ------------------------------------------------------------------------ */
  232. int divas_um_idi_delete_entity(int adapter_nr, void *entity)
  233. {
  234. divas_um_idi_entity_t *e;
  235. diva_um_idi_adapter_t *a;
  236. diva_os_spin_lock_magic_t old_irql;
  237. if (!(e = (divas_um_idi_entity_t *) entity))
  238. return (-1);
  239. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "delete_entity");
  240. if ((a = e->adapter)) {
  241. list_del(&e->link);
  242. }
  243. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "delete_entity");
  244. diva_um_idi_stop_wdog(entity);
  245. cleanup_entity(e);
  246. diva_os_free(0, e->os_context);
  247. memset(e, 0x00, sizeof(*e));
  248. DBG_LOG(("A(%d) remove E:%08x", adapter_nr, e));
  249. diva_os_free(0, e);
  250. return (0);
  251. }
  252. /* --------------------------------------------------------------------------
  253. Called by application to read data from IDI
  254. -------------------------------------------------------------------------- */
  255. int diva_um_idi_read(void *entity,
  256. void *os_handle,
  257. void *dst,
  258. int max_length, divas_um_idi_copy_to_user_fn_t cp_fn)
  259. {
  260. divas_um_idi_entity_t *e;
  261. diva_um_idi_adapter_t *a;
  262. const void *data;
  263. int length, ret = 0;
  264. diva_um_idi_data_queue_t *q;
  265. diva_os_spin_lock_magic_t old_irql;
  266. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "read");
  267. e = (divas_um_idi_entity_t *) entity;
  268. if (!e || (!(a = e->adapter)) ||
  269. (e->status & DIVA_UM_IDI_REMOVE_PENDING) ||
  270. (e->status & DIVA_UM_IDI_REMOVED) ||
  271. (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
  272. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "read");
  273. DBG_ERR(("E(%08x) read failed - adapter removed", e))
  274. return (-1);
  275. }
  276. DBG_TRC(("A(%d) E(%08x) read(%d)", a->adapter_nr, e, max_length));
  277. /*
  278. Try to read return code first
  279. */
  280. data = diva_data_q_get_segment4read(&e->rc);
  281. q = &e->rc;
  282. /*
  283. No return codes available, read indications now
  284. */
  285. if (!data) {
  286. if (!(e->status & DIVA_UM_IDI_RC_PENDING)) {
  287. DBG_TRC(("A(%d) E(%08x) read data", a->adapter_nr, e));
  288. data = diva_data_q_get_segment4read(&e->data);
  289. q = &e->data;
  290. }
  291. } else {
  292. e->status &= ~DIVA_UM_IDI_RC_PENDING;
  293. DBG_TRC(("A(%d) E(%08x) read rc", a->adapter_nr, e));
  294. }
  295. if (data) {
  296. if ((length = diva_data_q_get_segment_length(q)) >
  297. max_length) {
  298. /*
  299. Not enough space to read message
  300. */
  301. DBG_ERR(("A: A(%d) E(%08x) read small buffer",
  302. a->adapter_nr, e, ret));
  303. diva_os_leave_spin_lock(&adapter_lock, &old_irql,
  304. "read");
  305. return (-2);
  306. }
  307. /*
  308. Copy it to user, this function does access ONLY locked an verified
  309. memory, also we can access it witch spin lock held
  310. */
  311. if ((ret = (*cp_fn) (os_handle, dst, data, length)) >= 0) {
  312. /*
  313. Acknowledge only if read was successful
  314. */
  315. diva_data_q_ack_segment4read(q);
  316. }
  317. }
  318. DBG_TRC(("A(%d) E(%08x) read=%d", a->adapter_nr, e, ret));
  319. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "read");
  320. return (ret);
  321. }
  322. int diva_um_idi_write(void *entity,
  323. void *os_handle,
  324. const void *src,
  325. int length, divas_um_idi_copy_from_user_fn_t cp_fn)
  326. {
  327. divas_um_idi_entity_t *e;
  328. diva_um_idi_adapter_t *a;
  329. diva_um_idi_req_hdr_t *req;
  330. void *data;
  331. int ret = 0;
  332. diva_os_spin_lock_magic_t old_irql;
  333. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "write");
  334. e = (divas_um_idi_entity_t *) entity;
  335. if (!e || (!(a = e->adapter)) ||
  336. (e->status & DIVA_UM_IDI_REMOVE_PENDING) ||
  337. (e->status & DIVA_UM_IDI_REMOVED) ||
  338. (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
  339. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  340. DBG_ERR(("E(%08x) write failed - adapter removed", e))
  341. return (-1);
  342. }
  343. DBG_TRC(("A(%d) E(%08x) write(%d)", a->adapter_nr, e, length));
  344. if ((length < sizeof(*req)) || (length > sizeof(e->buffer))) {
  345. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  346. return (-2);
  347. }
  348. if (e->status & DIVA_UM_IDI_RC_PENDING) {
  349. DBG_ERR(("A: A(%d) E(%08x) rc pending", a->adapter_nr, e));
  350. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  351. return (-1); /* should wait for RC code first */
  352. }
  353. /*
  354. Copy function does access only locked verified memory,
  355. also it can be called with spin lock held
  356. */
  357. if ((ret = (*cp_fn) (os_handle, e->buffer, src, length)) < 0) {
  358. DBG_TRC(("A: A(%d) E(%08x) write error=%d", a->adapter_nr,
  359. e, ret));
  360. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  361. return (ret);
  362. }
  363. req = (diva_um_idi_req_hdr_t *)&e->buffer[0];
  364. switch (req->type) {
  365. case DIVA_UM_IDI_GET_FEATURES:{
  366. DBG_LOG(("A(%d) get_features", a->adapter_nr));
  367. if (!(data =
  368. diva_data_q_get_segment4write(&e->data))) {
  369. DBG_ERR(("A(%d) get_features, no free buffer",
  370. a->adapter_nr));
  371. diva_os_leave_spin_lock(&adapter_lock,
  372. &old_irql,
  373. "write");
  374. return (0);
  375. }
  376. diva_user_mode_idi_adapter_features(a, &(((diva_um_idi_ind_hdr_t
  377. *) data)->hdr.features));
  378. ((diva_um_idi_ind_hdr_t *) data)->type =
  379. DIVA_UM_IDI_IND_FEATURES;
  380. ((diva_um_idi_ind_hdr_t *) data)->data_length = 0;
  381. diva_data_q_ack_segment4write(&e->data,
  382. sizeof(diva_um_idi_ind_hdr_t));
  383. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  384. diva_os_wakeup_read(e->os_context);
  385. }
  386. break;
  387. case DIVA_UM_IDI_REQ:
  388. case DIVA_UM_IDI_REQ_MAN:
  389. case DIVA_UM_IDI_REQ_SIG:
  390. case DIVA_UM_IDI_REQ_NET:
  391. DBG_TRC(("A(%d) REQ(%02d)-(%02d)-(%08x)", a->adapter_nr,
  392. req->Req, req->ReqCh,
  393. req->type & DIVA_UM_IDI_REQ_TYPE_MASK));
  394. switch (process_idi_request(e, req)) {
  395. case -1:
  396. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  397. return (-1);
  398. case -2:
  399. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  400. diva_os_wakeup_read(e->os_context);
  401. break;
  402. default:
  403. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  404. break;
  405. }
  406. break;
  407. default:
  408. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
  409. return (-1);
  410. }
  411. DBG_TRC(("A(%d) E(%08x) write=%d", a->adapter_nr, e, ret));
  412. return (ret);
  413. }
  414. /* --------------------------------------------------------------------------
  415. CALLBACK FROM XDI
  416. -------------------------------------------------------------------------- */
  417. static void diva_um_idi_xdi_callback(ENTITY *entity)
  418. {
  419. divas_um_idi_entity_t *e = DIVAS_CONTAINING_RECORD(entity,
  420. divas_um_idi_entity_t,
  421. e);
  422. diva_os_spin_lock_magic_t old_irql;
  423. int call_wakeup = 0;
  424. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
  425. if (e->e.complete == 255) {
  426. if (!(e->status & DIVA_UM_IDI_REMOVE_PENDING)) {
  427. diva_um_idi_stop_wdog(e);
  428. }
  429. if ((call_wakeup = process_idi_rc(e, e->e.Rc))) {
  430. if (e->rc_count) {
  431. e->rc_count--;
  432. }
  433. }
  434. e->e.Rc = 0;
  435. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
  436. if (call_wakeup) {
  437. diva_os_wakeup_read(e->os_context);
  438. diva_os_wakeup_close(e->os_context);
  439. }
  440. } else {
  441. if (e->status & DIVA_UM_IDI_REMOVE_PENDING) {
  442. e->e.RNum = 0;
  443. e->e.RNR = 2;
  444. } else {
  445. call_wakeup = process_idi_ind(e, e->e.Ind);
  446. }
  447. e->e.Ind = 0;
  448. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
  449. if (call_wakeup) {
  450. diva_os_wakeup_read(e->os_context);
  451. }
  452. }
  453. }
  454. static int process_idi_request(divas_um_idi_entity_t *e,
  455. const diva_um_idi_req_hdr_t *req)
  456. {
  457. int assign = 0;
  458. byte Req = (byte) req->Req;
  459. dword type = req->type & DIVA_UM_IDI_REQ_TYPE_MASK;
  460. if (!e->e.Id || !e->e.callback) { /* not assigned */
  461. if (Req != ASSIGN) {
  462. DBG_ERR(("A: A(%d) E(%08x) not assigned",
  463. e->adapter->adapter_nr, e));
  464. return (-1); /* NOT ASSIGNED */
  465. } else {
  466. switch (type) {
  467. case DIVA_UM_IDI_REQ_TYPE_MAN:
  468. e->e.Id = MAN_ID;
  469. DBG_TRC(("A(%d) E(%08x) assign MAN",
  470. e->adapter->adapter_nr, e));
  471. break;
  472. case DIVA_UM_IDI_REQ_TYPE_SIG:
  473. e->e.Id = DSIG_ID;
  474. DBG_TRC(("A(%d) E(%08x) assign SIG",
  475. e->adapter->adapter_nr, e));
  476. break;
  477. case DIVA_UM_IDI_REQ_TYPE_NET:
  478. e->e.Id = NL_ID;
  479. DBG_TRC(("A(%d) E(%08x) assign NET",
  480. e->adapter->adapter_nr, e));
  481. break;
  482. default:
  483. DBG_ERR(("A: A(%d) E(%08x) unknown type=%08x",
  484. e->adapter->adapter_nr, e,
  485. type));
  486. return (-1);
  487. }
  488. }
  489. e->e.XNum = 1;
  490. e->e.RNum = 1;
  491. e->e.callback = diva_um_idi_xdi_callback;
  492. e->e.X = &e->XData;
  493. e->e.R = &e->RData;
  494. assign = 1;
  495. }
  496. e->status |= DIVA_UM_IDI_RC_PENDING;
  497. e->e.Req = Req;
  498. e->e.ReqCh = (byte) req->ReqCh;
  499. e->e.X->PLength = (word) req->data_length;
  500. e->e.X->P = (byte *)&req[1]; /* Our buffer is safe */
  501. DBG_TRC(("A(%d) E(%08x) request(%02x-%02x-%02x (%d))",
  502. e->adapter->adapter_nr, e, e->e.Id, e->e.Req,
  503. e->e.ReqCh, e->e.X->PLength));
  504. e->rc_count++;
  505. if (e->adapter && e->adapter->d.request) {
  506. diva_um_idi_start_wdog(e);
  507. (*(e->adapter->d.request)) (&e->e);
  508. }
  509. if (assign) {
  510. if (e->e.Rc == OUT_OF_RESOURCES) {
  511. /*
  512. XDI has no entities more, call was not forwarded to the card,
  513. no callback will be scheduled
  514. */
  515. DBG_ERR(("A: A(%d) E(%08x) XDI out of entities",
  516. e->adapter->adapter_nr, e));
  517. e->e.Id = 0;
  518. e->e.ReqCh = 0;
  519. e->e.RcCh = 0;
  520. e->e.Ind = 0;
  521. e->e.IndCh = 0;
  522. e->e.XNum = 0;
  523. e->e.RNum = 0;
  524. e->e.callback = NULL;
  525. e->e.X = NULL;
  526. e->e.R = NULL;
  527. write_return_code(e, ASSIGN_RC | OUT_OF_RESOURCES);
  528. return (-2);
  529. } else {
  530. e->status |= DIVA_UM_IDI_ASSIGN_PENDING;
  531. }
  532. }
  533. return (0);
  534. }
  535. static int process_idi_rc(divas_um_idi_entity_t *e, byte rc)
  536. {
  537. DBG_TRC(("A(%d) E(%08x) rc(%02x-%02x-%02x)",
  538. e->adapter->adapter_nr, e, e->e.Id, rc, e->e.RcCh));
  539. if (e->status & DIVA_UM_IDI_ASSIGN_PENDING) {
  540. e->status &= ~DIVA_UM_IDI_ASSIGN_PENDING;
  541. if (rc != ASSIGN_OK) {
  542. DBG_ERR(("A: A(%d) E(%08x) ASSIGN failed",
  543. e->adapter->adapter_nr, e));
  544. e->e.callback = NULL;
  545. e->e.Id = 0;
  546. e->e.Req = 0;
  547. e->e.ReqCh = 0;
  548. e->e.Rc = 0;
  549. e->e.RcCh = 0;
  550. e->e.Ind = 0;
  551. e->e.IndCh = 0;
  552. e->e.X = NULL;
  553. e->e.R = NULL;
  554. e->e.XNum = 0;
  555. e->e.RNum = 0;
  556. }
  557. }
  558. if ((e->e.Req == REMOVE) && e->e.Id && (rc == 0xff)) {
  559. DBG_ERR(("A: A(%d) E(%08x) discard OK in REMOVE",
  560. e->adapter->adapter_nr, e));
  561. return (0); /* let us do it in the driver */
  562. }
  563. if ((e->e.Req == REMOVE) && (!e->e.Id)) { /* REMOVE COMPLETE */
  564. e->e.callback = NULL;
  565. e->e.Id = 0;
  566. e->e.Req = 0;
  567. e->e.ReqCh = 0;
  568. e->e.Rc = 0;
  569. e->e.RcCh = 0;
  570. e->e.Ind = 0;
  571. e->e.IndCh = 0;
  572. e->e.X = NULL;
  573. e->e.R = NULL;
  574. e->e.XNum = 0;
  575. e->e.RNum = 0;
  576. e->rc_count = 0;
  577. }
  578. if ((e->e.Req == REMOVE) && (rc != 0xff)) { /* REMOVE FAILED */
  579. DBG_ERR(("A: A(%d) E(%08x) REMOVE FAILED",
  580. e->adapter->adapter_nr, e));
  581. }
  582. write_return_code(e, rc);
  583. return (1);
  584. }
  585. static int process_idi_ind(divas_um_idi_entity_t *e, byte ind)
  586. {
  587. int do_wakeup = 0;
  588. if (e->e.complete != 0x02) {
  589. diva_um_idi_ind_hdr_t *pind =
  590. (diva_um_idi_ind_hdr_t *)
  591. diva_data_q_get_segment4write(&e->data);
  592. if (pind) {
  593. e->e.RNum = 1;
  594. e->e.R->P = (byte *)&pind[1];
  595. e->e.R->PLength =
  596. (word) (diva_data_q_get_max_length(&e->data) -
  597. sizeof(*pind));
  598. DBG_TRC(("A(%d) E(%08x) ind_1(%02x-%02x-%02x)-[%d-%d]",
  599. e->adapter->adapter_nr, e, e->e.Id, ind,
  600. e->e.IndCh, e->e.RLength,
  601. e->e.R->PLength));
  602. } else {
  603. DBG_TRC(("A(%d) E(%08x) ind(%02x-%02x-%02x)-RNR",
  604. e->adapter->adapter_nr, e, e->e.Id, ind,
  605. e->e.IndCh));
  606. e->e.RNum = 0;
  607. e->e.RNR = 1;
  608. do_wakeup = 1;
  609. }
  610. } else {
  611. diva_um_idi_ind_hdr_t *pind =
  612. (diva_um_idi_ind_hdr_t *) (e->e.R->P);
  613. DBG_TRC(("A(%d) E(%08x) ind(%02x-%02x-%02x)-[%d]",
  614. e->adapter->adapter_nr, e, e->e.Id, ind,
  615. e->e.IndCh, e->e.R->PLength));
  616. pind--;
  617. pind->type = DIVA_UM_IDI_IND;
  618. pind->hdr.ind.Ind = ind;
  619. pind->hdr.ind.IndCh = e->e.IndCh;
  620. pind->data_length = e->e.R->PLength;
  621. diva_data_q_ack_segment4write(&e->data,
  622. (int) (sizeof(*pind) +
  623. e->e.R->PLength));
  624. do_wakeup = 1;
  625. }
  626. if ((e->status & DIVA_UM_IDI_RC_PENDING) && !e->rc.count) {
  627. do_wakeup = 0;
  628. }
  629. return (do_wakeup);
  630. }
  631. /* --------------------------------------------------------------------------
  632. Write return code to the return code queue of entity
  633. -------------------------------------------------------------------------- */
  634. static int write_return_code(divas_um_idi_entity_t *e, byte rc)
  635. {
  636. diva_um_idi_ind_hdr_t *prc;
  637. if (!(prc =
  638. (diva_um_idi_ind_hdr_t *) diva_data_q_get_segment4write(&e->rc)))
  639. {
  640. DBG_ERR(("A: A(%d) E(%08x) rc(%02x) lost",
  641. e->adapter->adapter_nr, e, rc));
  642. e->status &= ~DIVA_UM_IDI_RC_PENDING;
  643. return (-1);
  644. }
  645. prc->type = DIVA_UM_IDI_IND_RC;
  646. prc->hdr.rc.Rc = rc;
  647. prc->hdr.rc.RcCh = e->e.RcCh;
  648. prc->data_length = 0;
  649. diva_data_q_ack_segment4write(&e->rc, sizeof(*prc));
  650. return (0);
  651. }
  652. /* --------------------------------------------------------------------------
  653. Return amount of entries that can be bead from this entity or
  654. -1 if adapter was removed
  655. -------------------------------------------------------------------------- */
  656. int diva_user_mode_idi_ind_ready(void *entity, void *os_handle)
  657. {
  658. divas_um_idi_entity_t *e;
  659. diva_um_idi_adapter_t *a;
  660. diva_os_spin_lock_magic_t old_irql;
  661. int ret;
  662. if (!entity)
  663. return (-1);
  664. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "ind_ready");
  665. e = (divas_um_idi_entity_t *) entity;
  666. a = e->adapter;
  667. if ((!a) || (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
  668. /*
  669. Adapter was unloaded
  670. */
  671. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
  672. return (-1); /* adapter was removed */
  673. }
  674. if (e->status & DIVA_UM_IDI_REMOVED) {
  675. /*
  676. entity was removed as result of adapter removal
  677. user should assign this entity again
  678. */
  679. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
  680. return (-1);
  681. }
  682. ret = e->rc.count + e->data.count;
  683. if ((e->status & DIVA_UM_IDI_RC_PENDING) && !e->rc.count) {
  684. ret = 0;
  685. }
  686. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
  687. return (ret);
  688. }
  689. void *diva_um_id_get_os_context(void *entity)
  690. {
  691. return (((divas_um_idi_entity_t *) entity)->os_context);
  692. }
  693. int divas_um_idi_entity_assigned(void *entity)
  694. {
  695. divas_um_idi_entity_t *e;
  696. diva_um_idi_adapter_t *a;
  697. int ret;
  698. diva_os_spin_lock_magic_t old_irql;
  699. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "assigned?");
  700. e = (divas_um_idi_entity_t *) entity;
  701. if (!e || (!(a = e->adapter)) ||
  702. (e->status & DIVA_UM_IDI_REMOVED) ||
  703. (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
  704. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "assigned?");
  705. return (0);
  706. }
  707. e->status |= DIVA_UM_IDI_REMOVE_PENDING;
  708. ret = (e->e.Id || e->rc_count
  709. || (e->status & DIVA_UM_IDI_ASSIGN_PENDING));
  710. DBG_TRC(("Id:%02x, rc_count:%d, status:%08x", e->e.Id, e->rc_count,
  711. e->status))
  712. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "assigned?");
  713. return (ret);
  714. }
  715. int divas_um_idi_entity_start_remove(void *entity)
  716. {
  717. divas_um_idi_entity_t *e;
  718. diva_um_idi_adapter_t *a;
  719. diva_os_spin_lock_magic_t old_irql;
  720. diva_os_enter_spin_lock(&adapter_lock, &old_irql, "start_remove");
  721. e = (divas_um_idi_entity_t *) entity;
  722. if (!e || (!(a = e->adapter)) ||
  723. (e->status & DIVA_UM_IDI_REMOVED) ||
  724. (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
  725. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
  726. return (0);
  727. }
  728. if (e->rc_count) {
  729. /*
  730. Entity BUSY
  731. */
  732. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
  733. return (1);
  734. }
  735. if (!e->e.Id) {
  736. /*
  737. Remove request was already pending, and arrived now
  738. */
  739. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
  740. return (0); /* REMOVE was pending */
  741. }
  742. /*
  743. Now send remove request
  744. */
  745. e->e.Req = REMOVE;
  746. e->e.ReqCh = 0;
  747. e->rc_count++;
  748. DBG_TRC(("A(%d) E(%08x) request(%02x-%02x-%02x (%d))",
  749. e->adapter->adapter_nr, e, e->e.Id, e->e.Req,
  750. e->e.ReqCh, e->e.X->PLength));
  751. if (a->d.request)
  752. (*(a->d.request)) (&e->e);
  753. diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
  754. return (0);
  755. }