Linux 4.19.133
[linux/fpc-iii.git] / drivers / isdn / hardware / eicon / um_idi.c
blobdb4dd4ff3642db237c97b5bf48c0f7bc38f3df7b
1 // SPDX-License-Identifier: GPL-2.0
2 /* $Id: um_idi.c,v 1.14 2004/03/21 17:54:37 armin Exp $ */
4 #include "platform.h"
5 #include "di_defs.h"
6 #include "pc.h"
7 #include "dqueue.h"
8 #include "adapter.h"
9 #include "entity.h"
10 #include "um_xdi.h"
11 #include "um_idi.h"
12 #include "debuglib.h"
13 #include "divasync.h"
15 #define DIVAS_MAX_XDI_ADAPTERS 64
17 /* --------------------------------------------------------------------------
18 IMPORTS
19 -------------------------------------------------------------------------- */
20 extern void diva_os_wakeup_read(void *os_context);
21 extern void diva_os_wakeup_close(void *os_context);
22 /* --------------------------------------------------------------------------
23 LOCALS
24 -------------------------------------------------------------------------- */
25 static LIST_HEAD(adapter_q);
26 static diva_os_spin_lock_t adapter_lock;
28 static diva_um_idi_adapter_t *diva_um_idi_find_adapter(dword nr);
29 static void cleanup_adapter(diva_um_idi_adapter_t *a);
30 static void cleanup_entity(divas_um_idi_entity_t *e);
31 static int diva_user_mode_idi_adapter_features(diva_um_idi_adapter_t *a,
32 diva_um_idi_adapter_features_t
33 *features);
34 static int process_idi_request(divas_um_idi_entity_t *e,
35 const diva_um_idi_req_hdr_t *req);
36 static int process_idi_rc(divas_um_idi_entity_t *e, byte rc);
37 static int process_idi_ind(divas_um_idi_entity_t *e, byte ind);
38 static int write_return_code(divas_um_idi_entity_t *e, byte rc);
40 /* --------------------------------------------------------------------------
41 MAIN
42 -------------------------------------------------------------------------- */
43 int diva_user_mode_idi_init(void)
45 diva_os_initialize_spin_lock(&adapter_lock, "adapter");
46 return (0);
49 /* --------------------------------------------------------------------------
50 Copy adapter features to user supplied buffer
51 -------------------------------------------------------------------------- */
52 static int
53 diva_user_mode_idi_adapter_features(diva_um_idi_adapter_t *a,
54 diva_um_idi_adapter_features_t *
55 features)
57 IDI_SYNC_REQ sync_req;
59 if ((a) && (a->d.request)) {
60 features->type = a->d.type;
61 features->features = a->d.features;
62 features->channels = a->d.channels;
63 memset(features->name, 0, sizeof(features->name));
65 sync_req.GetName.Req = 0;
66 sync_req.GetName.Rc = IDI_SYNC_REQ_GET_NAME;
67 (*(a->d.request)) ((ENTITY *)&sync_req);
68 strlcpy(features->name, sync_req.GetName.name,
69 sizeof(features->name));
71 sync_req.GetSerial.Req = 0;
72 sync_req.GetSerial.Rc = IDI_SYNC_REQ_GET_SERIAL;
73 sync_req.GetSerial.serial = 0;
74 (*(a->d.request))((ENTITY *)&sync_req);
75 features->serial_number = sync_req.GetSerial.serial;
78 return ((a) ? 0 : -1);
81 /* --------------------------------------------------------------------------
82 REMOVE ADAPTER
83 -------------------------------------------------------------------------- */
84 void diva_user_mode_idi_remove_adapter(int adapter_nr)
86 struct list_head *tmp;
87 diva_um_idi_adapter_t *a;
89 list_for_each(tmp, &adapter_q) {
90 a = list_entry(tmp, diva_um_idi_adapter_t, link);
91 if (a->adapter_nr == adapter_nr) {
92 list_del(tmp);
93 cleanup_adapter(a);
94 DBG_LOG(("DIDD: del adapter(%d)", a->adapter_nr));
95 diva_os_free(0, a);
96 break;
101 /* --------------------------------------------------------------------------
102 CALLED ON DRIVER EXIT (UNLOAD)
103 -------------------------------------------------------------------------- */
104 void diva_user_mode_idi_finit(void)
106 struct list_head *tmp, *safe;
107 diva_um_idi_adapter_t *a;
109 list_for_each_safe(tmp, safe, &adapter_q) {
110 a = list_entry(tmp, diva_um_idi_adapter_t, link);
111 list_del(tmp);
112 cleanup_adapter(a);
113 DBG_LOG(("DIDD: del adapter(%d)", a->adapter_nr));
114 diva_os_free(0, a);
116 diva_os_destroy_spin_lock(&adapter_lock, "adapter");
119 /* -------------------------------------------------------------------------
120 CREATE AND INIT IDI ADAPTER
121 ------------------------------------------------------------------------- */
122 int diva_user_mode_idi_create_adapter(const DESCRIPTOR *d, int adapter_nr)
124 diva_os_spin_lock_magic_t old_irql;
125 diva_um_idi_adapter_t *a =
126 (diva_um_idi_adapter_t *) diva_os_malloc(0,
127 sizeof
128 (diva_um_idi_adapter_t));
130 if (!a) {
131 return (-1);
133 memset(a, 0x00, sizeof(*a));
134 INIT_LIST_HEAD(&a->entity_q);
136 a->d = *d;
137 a->adapter_nr = adapter_nr;
139 DBG_LOG(("DIDD_ADD A(%d), type:%02x, features:%04x, channels:%d",
140 adapter_nr, a->d.type, a->d.features, a->d.channels));
142 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "create_adapter");
143 list_add_tail(&a->link, &adapter_q);
144 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_adapter");
145 return (0);
148 /* ------------------------------------------------------------------------
149 Find adapter by Adapter number
150 ------------------------------------------------------------------------ */
151 static diva_um_idi_adapter_t *diva_um_idi_find_adapter(dword nr)
153 diva_um_idi_adapter_t *a = NULL;
154 struct list_head *tmp;
156 list_for_each(tmp, &adapter_q) {
157 a = list_entry(tmp, diva_um_idi_adapter_t, link);
158 DBG_TRC(("find_adapter: (%d)-(%d)", nr, a->adapter_nr));
159 if (a->adapter_nr == (int)nr)
160 break;
161 a = NULL;
163 return (a);
166 /* ------------------------------------------------------------------------
167 Cleanup this adapter and cleanup/delete all entities assigned
168 to this adapter
169 ------------------------------------------------------------------------ */
170 static void cleanup_adapter(diva_um_idi_adapter_t *a)
172 struct list_head *tmp, *safe;
173 divas_um_idi_entity_t *e;
175 list_for_each_safe(tmp, safe, &a->entity_q) {
176 e = list_entry(tmp, divas_um_idi_entity_t, link);
177 list_del(tmp);
178 cleanup_entity(e);
179 if (e->os_context) {
180 diva_os_wakeup_read(e->os_context);
181 diva_os_wakeup_close(e->os_context);
184 memset(&a->d, 0x00, sizeof(DESCRIPTOR));
187 /* ------------------------------------------------------------------------
188 Cleanup, but NOT delete this entity
189 ------------------------------------------------------------------------ */
190 static void cleanup_entity(divas_um_idi_entity_t *e)
192 e->os_ref = NULL;
193 e->status = 0;
194 e->adapter = NULL;
195 e->e.Id = 0;
196 e->rc_count = 0;
198 e->status |= DIVA_UM_IDI_REMOVED;
199 e->status |= DIVA_UM_IDI_REMOVE_PENDING;
201 diva_data_q_finit(&e->data);
202 diva_data_q_finit(&e->rc);
206 /* ------------------------------------------------------------------------
207 Create ENTITY, link it to the adapter and remove pointer to entity
208 ------------------------------------------------------------------------ */
209 void *divas_um_idi_create_entity(dword adapter_nr, void *file)
211 divas_um_idi_entity_t *e;
212 diva_um_idi_adapter_t *a;
213 diva_os_spin_lock_magic_t old_irql;
215 if ((e = (divas_um_idi_entity_t *) diva_os_malloc(0, sizeof(*e)))) {
216 memset(e, 0x00, sizeof(*e));
217 if (!
218 (e->os_context =
219 diva_os_malloc(0, diva_os_get_context_size()))) {
220 DBG_LOG(("E(%08x) no memory for os context", e));
221 diva_os_free(0, e);
222 return NULL;
224 memset(e->os_context, 0x00, diva_os_get_context_size());
226 if ((diva_data_q_init(&e->data, 2048 + 512, 16))) {
227 diva_os_free(0, e->os_context);
228 diva_os_free(0, e);
229 return NULL;
231 if ((diva_data_q_init(&e->rc, sizeof(diva_um_idi_ind_hdr_t), 2))) {
232 diva_data_q_finit(&e->data);
233 diva_os_free(0, e->os_context);
234 diva_os_free(0, e);
235 return NULL;
238 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "create_entity");
240 Look for Adapter requested
242 if (!(a = diva_um_idi_find_adapter(adapter_nr))) {
244 No adapter was found, or this adapter was removed
246 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_entity");
248 DBG_LOG(("A: no adapter(%ld)", adapter_nr));
250 cleanup_entity(e);
251 diva_os_free(0, e->os_context);
252 diva_os_free(0, e);
254 return NULL;
257 e->os_ref = file; /* link to os handle */
258 e->adapter = a; /* link to adapter */
260 list_add_tail(&e->link, &a->entity_q); /* link from adapter */
262 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_entity");
264 DBG_LOG(("A(%ld), create E(%08x)", adapter_nr, e));
267 return (e);
270 /* ------------------------------------------------------------------------
271 Unlink entity and free memory
272 ------------------------------------------------------------------------ */
273 int divas_um_idi_delete_entity(int adapter_nr, void *entity)
275 divas_um_idi_entity_t *e;
276 diva_um_idi_adapter_t *a;
277 diva_os_spin_lock_magic_t old_irql;
279 if (!(e = (divas_um_idi_entity_t *) entity))
280 return (-1);
282 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "delete_entity");
283 if ((a = e->adapter)) {
284 list_del(&e->link);
286 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "delete_entity");
288 diva_um_idi_stop_wdog(entity);
289 cleanup_entity(e);
290 diva_os_free(0, e->os_context);
291 memset(e, 0x00, sizeof(*e));
293 DBG_LOG(("A(%d) remove E:%08x", adapter_nr, e));
294 diva_os_free(0, e);
296 return (0);
299 /* --------------------------------------------------------------------------
300 Called by application to read data from IDI
301 -------------------------------------------------------------------------- */
302 int diva_um_idi_read(void *entity,
303 void *os_handle,
304 void *dst,
305 int max_length, divas_um_idi_copy_to_user_fn_t cp_fn)
307 divas_um_idi_entity_t *e;
308 diva_um_idi_adapter_t *a;
309 const void *data;
310 int length, ret = 0;
311 diva_um_idi_data_queue_t *q;
312 diva_os_spin_lock_magic_t old_irql;
314 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "read");
316 e = (divas_um_idi_entity_t *) entity;
317 if (!e || (!(a = e->adapter)) ||
318 (e->status & DIVA_UM_IDI_REMOVE_PENDING) ||
319 (e->status & DIVA_UM_IDI_REMOVED) ||
320 (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
321 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "read");
322 DBG_ERR(("E(%08x) read failed - adapter removed", e))
323 return (-1);
326 DBG_TRC(("A(%d) E(%08x) read(%d)", a->adapter_nr, e, max_length));
329 Try to read return code first
331 data = diva_data_q_get_segment4read(&e->rc);
332 q = &e->rc;
335 No return codes available, read indications now
337 if (!data) {
338 if (!(e->status & DIVA_UM_IDI_RC_PENDING)) {
339 DBG_TRC(("A(%d) E(%08x) read data", a->adapter_nr, e));
340 data = diva_data_q_get_segment4read(&e->data);
341 q = &e->data;
343 } else {
344 e->status &= ~DIVA_UM_IDI_RC_PENDING;
345 DBG_TRC(("A(%d) E(%08x) read rc", a->adapter_nr, e));
348 if (data) {
349 if ((length = diva_data_q_get_segment_length(q)) >
350 max_length) {
352 Not enough space to read message
354 DBG_ERR(("A: A(%d) E(%08x) read small buffer",
355 a->adapter_nr, e, ret));
356 diva_os_leave_spin_lock(&adapter_lock, &old_irql,
357 "read");
358 return (-2);
361 Copy it to user, this function does access ONLY locked an verified
362 memory, also we can access it witch spin lock held
365 if ((ret = (*cp_fn) (os_handle, dst, data, length)) >= 0) {
367 Acknowledge only if read was successful
369 diva_data_q_ack_segment4read(q);
374 DBG_TRC(("A(%d) E(%08x) read=%d", a->adapter_nr, e, ret));
376 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "read");
378 return (ret);
382 int diva_um_idi_write(void *entity,
383 void *os_handle,
384 const void *src,
385 int length, divas_um_idi_copy_from_user_fn_t cp_fn)
387 divas_um_idi_entity_t *e;
388 diva_um_idi_adapter_t *a;
389 diva_um_idi_req_hdr_t *req;
390 void *data;
391 int ret = 0;
392 diva_os_spin_lock_magic_t old_irql;
394 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "write");
396 e = (divas_um_idi_entity_t *) entity;
397 if (!e || (!(a = e->adapter)) ||
398 (e->status & DIVA_UM_IDI_REMOVE_PENDING) ||
399 (e->status & DIVA_UM_IDI_REMOVED) ||
400 (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
401 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
402 DBG_ERR(("E(%08x) write failed - adapter removed", e))
403 return (-1);
406 DBG_TRC(("A(%d) E(%08x) write(%d)", a->adapter_nr, e, length));
408 if ((length < sizeof(*req)) || (length > sizeof(e->buffer))) {
409 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
410 return (-2);
413 if (e->status & DIVA_UM_IDI_RC_PENDING) {
414 DBG_ERR(("A: A(%d) E(%08x) rc pending", a->adapter_nr, e));
415 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
416 return (-1); /* should wait for RC code first */
420 Copy function does access only locked verified memory,
421 also it can be called with spin lock held
423 if ((ret = (*cp_fn) (os_handle, e->buffer, src, length)) < 0) {
424 DBG_TRC(("A: A(%d) E(%08x) write error=%d", a->adapter_nr,
425 e, ret));
426 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
427 return (ret);
430 req = (diva_um_idi_req_hdr_t *)&e->buffer[0];
432 switch (req->type) {
433 case DIVA_UM_IDI_GET_FEATURES:{
434 DBG_LOG(("A(%d) get_features", a->adapter_nr));
435 if (!(data =
436 diva_data_q_get_segment4write(&e->data))) {
437 DBG_ERR(("A(%d) get_features, no free buffer",
438 a->adapter_nr));
439 diva_os_leave_spin_lock(&adapter_lock,
440 &old_irql,
441 "write");
442 return (0);
444 diva_user_mode_idi_adapter_features(a, &(((diva_um_idi_ind_hdr_t
445 *) data)->hdr.features));
446 ((diva_um_idi_ind_hdr_t *) data)->type =
447 DIVA_UM_IDI_IND_FEATURES;
448 ((diva_um_idi_ind_hdr_t *) data)->data_length = 0;
449 diva_data_q_ack_segment4write(&e->data,
450 sizeof(diva_um_idi_ind_hdr_t));
452 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
454 diva_os_wakeup_read(e->os_context);
456 break;
458 case DIVA_UM_IDI_REQ:
459 case DIVA_UM_IDI_REQ_MAN:
460 case DIVA_UM_IDI_REQ_SIG:
461 case DIVA_UM_IDI_REQ_NET:
462 DBG_TRC(("A(%d) REQ(%02d)-(%02d)-(%08x)", a->adapter_nr,
463 req->Req, req->ReqCh,
464 req->type & DIVA_UM_IDI_REQ_TYPE_MASK));
465 switch (process_idi_request(e, req)) {
466 case -1:
467 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
468 return (-1);
469 case -2:
470 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
471 diva_os_wakeup_read(e->os_context);
472 break;
473 default:
474 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
475 break;
477 break;
479 default:
480 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
481 return (-1);
484 DBG_TRC(("A(%d) E(%08x) write=%d", a->adapter_nr, e, ret));
486 return (ret);
489 /* --------------------------------------------------------------------------
490 CALLBACK FROM XDI
491 -------------------------------------------------------------------------- */
492 static void diva_um_idi_xdi_callback(ENTITY *entity)
494 divas_um_idi_entity_t *e = DIVAS_CONTAINING_RECORD(entity,
495 divas_um_idi_entity_t,
497 diva_os_spin_lock_magic_t old_irql;
498 int call_wakeup = 0;
500 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
502 if (e->e.complete == 255) {
503 if (!(e->status & DIVA_UM_IDI_REMOVE_PENDING)) {
504 diva_um_idi_stop_wdog(e);
506 if ((call_wakeup = process_idi_rc(e, e->e.Rc))) {
507 if (e->rc_count) {
508 e->rc_count--;
511 e->e.Rc = 0;
512 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
514 if (call_wakeup) {
515 diva_os_wakeup_read(e->os_context);
516 diva_os_wakeup_close(e->os_context);
518 } else {
519 if (e->status & DIVA_UM_IDI_REMOVE_PENDING) {
520 e->e.RNum = 0;
521 e->e.RNR = 2;
522 } else {
523 call_wakeup = process_idi_ind(e, e->e.Ind);
525 e->e.Ind = 0;
526 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
527 if (call_wakeup) {
528 diva_os_wakeup_read(e->os_context);
533 static int process_idi_request(divas_um_idi_entity_t *e,
534 const diva_um_idi_req_hdr_t *req)
536 int assign = 0;
537 byte Req = (byte) req->Req;
538 dword type = req->type & DIVA_UM_IDI_REQ_TYPE_MASK;
540 if (!e->e.Id || !e->e.callback) { /* not assigned */
541 if (Req != ASSIGN) {
542 DBG_ERR(("A: A(%d) E(%08x) not assigned",
543 e->adapter->adapter_nr, e));
544 return (-1); /* NOT ASSIGNED */
545 } else {
546 switch (type) {
547 case DIVA_UM_IDI_REQ_TYPE_MAN:
548 e->e.Id = MAN_ID;
549 DBG_TRC(("A(%d) E(%08x) assign MAN",
550 e->adapter->adapter_nr, e));
551 break;
553 case DIVA_UM_IDI_REQ_TYPE_SIG:
554 e->e.Id = DSIG_ID;
555 DBG_TRC(("A(%d) E(%08x) assign SIG",
556 e->adapter->adapter_nr, e));
557 break;
559 case DIVA_UM_IDI_REQ_TYPE_NET:
560 e->e.Id = NL_ID;
561 DBG_TRC(("A(%d) E(%08x) assign NET",
562 e->adapter->adapter_nr, e));
563 break;
565 default:
566 DBG_ERR(("A: A(%d) E(%08x) unknown type=%08x",
567 e->adapter->adapter_nr, e,
568 type));
569 return (-1);
572 e->e.XNum = 1;
573 e->e.RNum = 1;
574 e->e.callback = diva_um_idi_xdi_callback;
575 e->e.X = &e->XData;
576 e->e.R = &e->RData;
577 assign = 1;
579 e->status |= DIVA_UM_IDI_RC_PENDING;
580 e->e.Req = Req;
581 e->e.ReqCh = (byte) req->ReqCh;
582 e->e.X->PLength = (word) req->data_length;
583 e->e.X->P = (byte *)&req[1]; /* Our buffer is safe */
585 DBG_TRC(("A(%d) E(%08x) request(%02x-%02x-%02x (%d))",
586 e->adapter->adapter_nr, e, e->e.Id, e->e.Req,
587 e->e.ReqCh, e->e.X->PLength));
589 e->rc_count++;
591 if (e->adapter && e->adapter->d.request) {
592 diva_um_idi_start_wdog(e);
593 (*(e->adapter->d.request)) (&e->e);
596 if (assign) {
597 if (e->e.Rc == OUT_OF_RESOURCES) {
599 XDI has no entities more, call was not forwarded to the card,
600 no callback will be scheduled
602 DBG_ERR(("A: A(%d) E(%08x) XDI out of entities",
603 e->adapter->adapter_nr, e));
605 e->e.Id = 0;
606 e->e.ReqCh = 0;
607 e->e.RcCh = 0;
608 e->e.Ind = 0;
609 e->e.IndCh = 0;
610 e->e.XNum = 0;
611 e->e.RNum = 0;
612 e->e.callback = NULL;
613 e->e.X = NULL;
614 e->e.R = NULL;
615 write_return_code(e, ASSIGN_RC | OUT_OF_RESOURCES);
616 return (-2);
617 } else {
618 e->status |= DIVA_UM_IDI_ASSIGN_PENDING;
622 return (0);
625 static int process_idi_rc(divas_um_idi_entity_t *e, byte rc)
627 DBG_TRC(("A(%d) E(%08x) rc(%02x-%02x-%02x)",
628 e->adapter->adapter_nr, e, e->e.Id, rc, e->e.RcCh));
630 if (e->status & DIVA_UM_IDI_ASSIGN_PENDING) {
631 e->status &= ~DIVA_UM_IDI_ASSIGN_PENDING;
632 if (rc != ASSIGN_OK) {
633 DBG_ERR(("A: A(%d) E(%08x) ASSIGN failed",
634 e->adapter->adapter_nr, e));
635 e->e.callback = NULL;
636 e->e.Id = 0;
637 e->e.Req = 0;
638 e->e.ReqCh = 0;
639 e->e.Rc = 0;
640 e->e.RcCh = 0;
641 e->e.Ind = 0;
642 e->e.IndCh = 0;
643 e->e.X = NULL;
644 e->e.R = NULL;
645 e->e.XNum = 0;
646 e->e.RNum = 0;
649 if ((e->e.Req == REMOVE) && e->e.Id && (rc == 0xff)) {
650 DBG_ERR(("A: A(%d) E(%08x) discard OK in REMOVE",
651 e->adapter->adapter_nr, e));
652 return (0); /* let us do it in the driver */
654 if ((e->e.Req == REMOVE) && (!e->e.Id)) { /* REMOVE COMPLETE */
655 e->e.callback = NULL;
656 e->e.Id = 0;
657 e->e.Req = 0;
658 e->e.ReqCh = 0;
659 e->e.Rc = 0;
660 e->e.RcCh = 0;
661 e->e.Ind = 0;
662 e->e.IndCh = 0;
663 e->e.X = NULL;
664 e->e.R = NULL;
665 e->e.XNum = 0;
666 e->e.RNum = 0;
667 e->rc_count = 0;
669 if ((e->e.Req == REMOVE) && (rc != 0xff)) { /* REMOVE FAILED */
670 DBG_ERR(("A: A(%d) E(%08x) REMOVE FAILED",
671 e->adapter->adapter_nr, e));
673 write_return_code(e, rc);
675 return (1);
678 static int process_idi_ind(divas_um_idi_entity_t *e, byte ind)
680 int do_wakeup = 0;
682 if (e->e.complete != 0x02) {
683 diva_um_idi_ind_hdr_t *pind =
684 (diva_um_idi_ind_hdr_t *)
685 diva_data_q_get_segment4write(&e->data);
686 if (pind) {
687 e->e.RNum = 1;
688 e->e.R->P = (byte *)&pind[1];
689 e->e.R->PLength =
690 (word) (diva_data_q_get_max_length(&e->data) -
691 sizeof(*pind));
692 DBG_TRC(("A(%d) E(%08x) ind_1(%02x-%02x-%02x)-[%d-%d]",
693 e->adapter->adapter_nr, e, e->e.Id, ind,
694 e->e.IndCh, e->e.RLength,
695 e->e.R->PLength));
697 } else {
698 DBG_TRC(("A(%d) E(%08x) ind(%02x-%02x-%02x)-RNR",
699 e->adapter->adapter_nr, e, e->e.Id, ind,
700 e->e.IndCh));
701 e->e.RNum = 0;
702 e->e.RNR = 1;
703 do_wakeup = 1;
705 } else {
706 diva_um_idi_ind_hdr_t *pind =
707 (diva_um_idi_ind_hdr_t *) (e->e.R->P);
709 DBG_TRC(("A(%d) E(%08x) ind(%02x-%02x-%02x)-[%d]",
710 e->adapter->adapter_nr, e, e->e.Id, ind,
711 e->e.IndCh, e->e.R->PLength));
713 pind--;
714 pind->type = DIVA_UM_IDI_IND;
715 pind->hdr.ind.Ind = ind;
716 pind->hdr.ind.IndCh = e->e.IndCh;
717 pind->data_length = e->e.R->PLength;
718 diva_data_q_ack_segment4write(&e->data,
719 (int) (sizeof(*pind) +
720 e->e.R->PLength));
721 do_wakeup = 1;
724 if ((e->status & DIVA_UM_IDI_RC_PENDING) && !e->rc.count) {
725 do_wakeup = 0;
728 return (do_wakeup);
731 /* --------------------------------------------------------------------------
732 Write return code to the return code queue of entity
733 -------------------------------------------------------------------------- */
734 static int write_return_code(divas_um_idi_entity_t *e, byte rc)
736 diva_um_idi_ind_hdr_t *prc;
738 if (!(prc =
739 (diva_um_idi_ind_hdr_t *) diva_data_q_get_segment4write(&e->rc)))
741 DBG_ERR(("A: A(%d) E(%08x) rc(%02x) lost",
742 e->adapter->adapter_nr, e, rc));
743 e->status &= ~DIVA_UM_IDI_RC_PENDING;
744 return (-1);
747 prc->type = DIVA_UM_IDI_IND_RC;
748 prc->hdr.rc.Rc = rc;
749 prc->hdr.rc.RcCh = e->e.RcCh;
750 prc->data_length = 0;
751 diva_data_q_ack_segment4write(&e->rc, sizeof(*prc));
753 return (0);
756 /* --------------------------------------------------------------------------
757 Return amount of entries that can be bead from this entity or
758 -1 if adapter was removed
759 -------------------------------------------------------------------------- */
760 int diva_user_mode_idi_ind_ready(void *entity, void *os_handle)
762 divas_um_idi_entity_t *e;
763 diva_um_idi_adapter_t *a;
764 diva_os_spin_lock_magic_t old_irql;
765 int ret;
767 if (!entity)
768 return (-1);
769 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "ind_ready");
770 e = (divas_um_idi_entity_t *) entity;
771 a = e->adapter;
773 if ((!a) || (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
775 Adapter was unloaded
777 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
778 return (-1); /* adapter was removed */
780 if (e->status & DIVA_UM_IDI_REMOVED) {
782 entity was removed as result of adapter removal
783 user should assign this entity again
785 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
786 return (-1);
789 ret = e->rc.count + e->data.count;
791 if ((e->status & DIVA_UM_IDI_RC_PENDING) && !e->rc.count) {
792 ret = 0;
795 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
797 return (ret);
800 void *diva_um_id_get_os_context(void *entity)
802 return (((divas_um_idi_entity_t *) entity)->os_context);
805 int divas_um_idi_entity_assigned(void *entity)
807 divas_um_idi_entity_t *e;
808 diva_um_idi_adapter_t *a;
809 int ret;
810 diva_os_spin_lock_magic_t old_irql;
812 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "assigned?");
815 e = (divas_um_idi_entity_t *) entity;
816 if (!e || (!(a = e->adapter)) ||
817 (e->status & DIVA_UM_IDI_REMOVED) ||
818 (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
819 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "assigned?");
820 return (0);
823 e->status |= DIVA_UM_IDI_REMOVE_PENDING;
825 ret = (e->e.Id || e->rc_count
826 || (e->status & DIVA_UM_IDI_ASSIGN_PENDING));
828 DBG_TRC(("Id:%02x, rc_count:%d, status:%08x", e->e.Id, e->rc_count,
829 e->status))
831 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "assigned?");
833 return (ret);
836 int divas_um_idi_entity_start_remove(void *entity)
838 divas_um_idi_entity_t *e;
839 diva_um_idi_adapter_t *a;
840 diva_os_spin_lock_magic_t old_irql;
842 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "start_remove");
844 e = (divas_um_idi_entity_t *) entity;
845 if (!e || (!(a = e->adapter)) ||
846 (e->status & DIVA_UM_IDI_REMOVED) ||
847 (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
848 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
849 return (0);
852 if (e->rc_count) {
854 Entity BUSY
856 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
857 return (1);
860 if (!e->e.Id) {
862 Remove request was already pending, and arrived now
864 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
865 return (0); /* REMOVE was pending */
869 Now send remove request
871 e->e.Req = REMOVE;
872 e->e.ReqCh = 0;
874 e->rc_count++;
876 DBG_TRC(("A(%d) E(%08x) request(%02x-%02x-%02x (%d))",
877 e->adapter->adapter_nr, e, e->e.Id, e->e.Req,
878 e->e.ReqCh, e->e.X->PLength));
880 if (a->d.request)
881 (*(a->d.request)) (&e->e);
883 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
885 return (0);