License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / drivers / isdn / hardware / eicon / um_idi.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/* $Id: um_idi.c,v 1.14 2004/03/21 17:54:37 armin Exp $ */
3
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"
14
15#define DIVAS_MAX_XDI_ADAPTERS 64
16
17/* --------------------------------------------------------------------------
475be4d8 18 IMPORTS
1da177e4
LT
19 -------------------------------------------------------------------------- */
20extern void diva_os_wakeup_read(void *os_context);
21extern void diva_os_wakeup_close(void *os_context);
22/* --------------------------------------------------------------------------
475be4d8 23 LOCALS
1da177e4
LT
24 -------------------------------------------------------------------------- */
25static LIST_HEAD(adapter_q);
26static diva_os_spin_lock_t adapter_lock;
27
28static diva_um_idi_adapter_t *diva_um_idi_find_adapter(dword nr);
475be4d8
JP
29static void cleanup_adapter(diva_um_idi_adapter_t *a);
30static void cleanup_entity(divas_um_idi_entity_t *e);
31static int diva_user_mode_idi_adapter_features(diva_um_idi_adapter_t *a,
1da177e4 32 diva_um_idi_adapter_features_t
475be4d8
JP
33 *features);
34static int process_idi_request(divas_um_idi_entity_t *e,
35 const diva_um_idi_req_hdr_t *req);
36static int process_idi_rc(divas_um_idi_entity_t *e, byte rc);
37static int process_idi_ind(divas_um_idi_entity_t *e, byte ind);
38static int write_return_code(divas_um_idi_entity_t *e, byte rc);
1da177e4
LT
39
40/* --------------------------------------------------------------------------
475be4d8 41 MAIN
1da177e4
LT
42 -------------------------------------------------------------------------- */
43int diva_user_mode_idi_init(void)
44{
45 diva_os_initialize_spin_lock(&adapter_lock, "adapter");
46 return (0);
47}
48
49/* --------------------------------------------------------------------------
475be4d8 50 Copy adapter features to user supplied buffer
1da177e4
LT
51 -------------------------------------------------------------------------- */
52static int
475be4d8 53diva_user_mode_idi_adapter_features(diva_um_idi_adapter_t *a,
1da177e4
LT
54 diva_um_idi_adapter_features_t *
55 features)
56{
57 IDI_SYNC_REQ sync_req;
58
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));
64
65 sync_req.GetName.Req = 0;
66 sync_req.GetName.Rc = IDI_SYNC_REQ_GET_NAME;
475be4d8 67 (*(a->d.request)) ((ENTITY *)&sync_req);
1da177e4
LT
68 strlcpy(features->name, sync_req.GetName.name,
69 sizeof(features->name));
70
71 sync_req.GetSerial.Req = 0;
72 sync_req.GetSerial.Rc = IDI_SYNC_REQ_GET_SERIAL;
73 sync_req.GetSerial.serial = 0;
475be4d8 74 (*(a->d.request))((ENTITY *)&sync_req);
1da177e4
LT
75 features->serial_number = sync_req.GetSerial.serial;
76 }
77
78 return ((a) ? 0 : -1);
79}
80
81/* --------------------------------------------------------------------------
475be4d8 82 REMOVE ADAPTER
1da177e4
LT
83 -------------------------------------------------------------------------- */
84void diva_user_mode_idi_remove_adapter(int adapter_nr)
85{
86 struct list_head *tmp;
87 diva_um_idi_adapter_t *a;
88
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;
97 }
98 }
99}
100
101/* --------------------------------------------------------------------------
475be4d8 102 CALLED ON DRIVER EXIT (UNLOAD)
1da177e4
LT
103 -------------------------------------------------------------------------- */
104void diva_user_mode_idi_finit(void)
105{
106 struct list_head *tmp, *safe;
107 diva_um_idi_adapter_t *a;
108
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);
115 }
116 diva_os_destroy_spin_lock(&adapter_lock, "adapter");
117}
118
119/* -------------------------------------------------------------------------
475be4d8
JP
120 CREATE AND INIT IDI ADAPTER
121 ------------------------------------------------------------------------- */
122int diva_user_mode_idi_create_adapter(const DESCRIPTOR *d, int adapter_nr)
1da177e4
LT
123{
124 diva_os_spin_lock_magic_t old_irql;
125 diva_um_idi_adapter_t *a =
475be4d8
JP
126 (diva_um_idi_adapter_t *) diva_os_malloc(0,
127 sizeof
128 (diva_um_idi_adapter_t));
1da177e4
LT
129
130 if (!a) {
131 return (-1);
132 }
133 memset(a, 0x00, sizeof(*a));
134 INIT_LIST_HEAD(&a->entity_q);
135
136 a->d = *d;
137 a->adapter_nr = adapter_nr;
138
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));
141
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);
146}
147
148/* ------------------------------------------------------------------------
475be4d8 149 Find adapter by Adapter number
1da177e4
LT
150 ------------------------------------------------------------------------ */
151static diva_um_idi_adapter_t *diva_um_idi_find_adapter(dword nr)
152{
153 diva_um_idi_adapter_t *a = NULL;
154 struct list_head *tmp;
155
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;
162 }
475be4d8 163 return (a);
1da177e4
LT
164}
165
166/* ------------------------------------------------------------------------
475be4d8
JP
167 Cleanup this adapter and cleanup/delete all entities assigned
168 to this adapter
1da177e4 169 ------------------------------------------------------------------------ */
475be4d8 170static void cleanup_adapter(diva_um_idi_adapter_t *a)
1da177e4
LT
171{
172 struct list_head *tmp, *safe;
173 divas_um_idi_entity_t *e;
174
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);
182 }
183 }
184 memset(&a->d, 0x00, sizeof(DESCRIPTOR));
185}
186
187/* ------------------------------------------------------------------------
475be4d8 188 Cleanup, but NOT delete this entity
1da177e4 189 ------------------------------------------------------------------------ */
475be4d8 190static void cleanup_entity(divas_um_idi_entity_t *e)
1da177e4
LT
191{
192 e->os_ref = NULL;
193 e->status = 0;
194 e->adapter = NULL;
195 e->e.Id = 0;
196 e->rc_count = 0;
197
198 e->status |= DIVA_UM_IDI_REMOVED;
199 e->status |= DIVA_UM_IDI_REMOVE_PENDING;
200
201 diva_data_q_finit(&e->data);
202 diva_data_q_finit(&e->rc);
203}
204
205
206/* ------------------------------------------------------------------------
475be4d8 207 Create ENTITY, link it to the adapter and remove pointer to entity
1da177e4
LT
208 ------------------------------------------------------------------------ */
209void *divas_um_idi_create_entity(dword adapter_nr, void *file)
210{
211 divas_um_idi_entity_t *e;
212 diva_um_idi_adapter_t *a;
213 diva_os_spin_lock_magic_t old_irql;
214
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;
223 }
224 memset(e->os_context, 0x00, diva_os_get_context_size());
225
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;
230 }
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;
236 }
237
238 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "create_entity");
239 /*
475be4d8
JP
240 Look for Adapter requested
241 */
1da177e4
LT
242 if (!(a = diva_um_idi_find_adapter(adapter_nr))) {
243 /*
475be4d8
JP
244 No adapter was found, or this adapter was removed
245 */
1da177e4
LT
246 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_entity");
247
248 DBG_LOG(("A: no adapter(%ld)", adapter_nr));
249
250 cleanup_entity(e);
251 diva_os_free(0, e->os_context);
252 diva_os_free(0, e);
253
254 return NULL;
255 }
256
257 e->os_ref = file; /* link to os handle */
258 e->adapter = a; /* link to adapter */
259
260 list_add_tail(&e->link, &a->entity_q); /* link from adapter */
261
262 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "create_entity");
263
264 DBG_LOG(("A(%ld), create E(%08x)", adapter_nr, e));
265 }
266
267 return (e);
268}
269
270/* ------------------------------------------------------------------------
475be4d8 271 Unlink entity and free memory
1da177e4
LT
272 ------------------------------------------------------------------------ */
273int divas_um_idi_delete_entity(int adapter_nr, void *entity)
274{
275 divas_um_idi_entity_t *e;
276 diva_um_idi_adapter_t *a;
277 diva_os_spin_lock_magic_t old_irql;
278
279 if (!(e = (divas_um_idi_entity_t *) entity))
280 return (-1);
281
282 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "delete_entity");
283 if ((a = e->adapter)) {
284 list_del(&e->link);
285 }
286 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "delete_entity");
287
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));
1da177e4
LT
292
293 DBG_LOG(("A(%d) remove E:%08x", adapter_nr, e));
84557783 294 diva_os_free(0, e);
1da177e4
LT
295
296 return (0);
297}
298
299/* --------------------------------------------------------------------------
475be4d8
JP
300 Called by application to read data from IDI
301 -------------------------------------------------------------------------- */
1da177e4
LT
302int 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)
306{
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;
313
314 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "read");
315
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))
475be4d8 323 return (-1);
1da177e4
LT
324 }
325
326 DBG_TRC(("A(%d) E(%08x) read(%d)", a->adapter_nr, e, max_length));
327
328 /*
475be4d8
JP
329 Try to read return code first
330 */
1da177e4
LT
331 data = diva_data_q_get_segment4read(&e->rc);
332 q = &e->rc;
333
334 /*
475be4d8
JP
335 No return codes available, read indications now
336 */
1da177e4
LT
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;
342 }
343 } else {
344 e->status &= ~DIVA_UM_IDI_RC_PENDING;
345 DBG_TRC(("A(%d) E(%08x) read rc", a->adapter_nr, e));
346 }
347
348 if (data) {
349 if ((length = diva_data_q_get_segment_length(q)) >
350 max_length) {
351 /*
475be4d8
JP
352 Not enough space to read message
353 */
1da177e4
LT
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);
359 }
360 /*
475be4d8
JP
361 Copy it to user, this function does access ONLY locked an verified
362 memory, also we can access it witch spin lock held
363 */
1da177e4
LT
364
365 if ((ret = (*cp_fn) (os_handle, dst, data, length)) >= 0) {
366 /*
475be4d8
JP
367 Acknowledge only if read was successful
368 */
1da177e4
LT
369 diva_data_q_ack_segment4read(q);
370 }
371 }
372
373
374 DBG_TRC(("A(%d) E(%08x) read=%d", a->adapter_nr, e, ret));
375
376 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "read");
377
378 return (ret);
379}
380
381
382int 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)
386{
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;
393
394 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "write");
395
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))
475be4d8 403 return (-1);
1da177e4
LT
404 }
405
406 DBG_TRC(("A(%d) E(%08x) write(%d)", a->adapter_nr, e, length));
407
408 if ((length < sizeof(*req)) || (length > sizeof(e->buffer))) {
409 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
410 return (-2);
411 }
412
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 */
417 }
418
419 /*
475be4d8
JP
420 Copy function does access only locked verified memory,
421 also it can be called with spin lock held
422 */
1da177e4
LT
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);
428 }
429
475be4d8 430 req = (diva_um_idi_req_hdr_t *)&e->buffer[0];
1da177e4
LT
431
432 switch (req->type) {
433 case DIVA_UM_IDI_GET_FEATURES:{
475be4d8
JP
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);
443 }
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));
1da177e4 451
475be4d8 452 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
1da177e4 453
475be4d8
JP
454 diva_os_wakeup_read(e->os_context);
455 }
1da177e4
LT
456 break;
457
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;
476 }
477 break;
478
479 default:
480 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "write");
481 return (-1);
482 }
483
484 DBG_TRC(("A(%d) E(%08x) write=%d", a->adapter_nr, e, ret));
485
486 return (ret);
487}
488
489/* --------------------------------------------------------------------------
475be4d8
JP
490 CALLBACK FROM XDI
491 -------------------------------------------------------------------------- */
492static void diva_um_idi_xdi_callback(ENTITY *entity)
1da177e4
LT
493{
494 divas_um_idi_entity_t *e = DIVAS_CONTAINING_RECORD(entity,
495 divas_um_idi_entity_t,
496 e);
497 diva_os_spin_lock_magic_t old_irql;
498 int call_wakeup = 0;
499
500 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
501
502 if (e->e.complete == 255) {
503 if (!(e->status & DIVA_UM_IDI_REMOVE_PENDING)) {
504 diva_um_idi_stop_wdog(e);
505 }
506 if ((call_wakeup = process_idi_rc(e, e->e.Rc))) {
507 if (e->rc_count) {
508 e->rc_count--;
509 }
510 }
511 e->e.Rc = 0;
512 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "xdi_callback");
513
514 if (call_wakeup) {
515 diva_os_wakeup_read(e->os_context);
516 diva_os_wakeup_close(e->os_context);
517 }
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);
524 }
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);
529 }
530 }
531}
532
475be4d8
JP
533static int process_idi_request(divas_um_idi_entity_t *e,
534 const diva_um_idi_req_hdr_t *req)
1da177e4
LT
535{
536 int assign = 0;
537 byte Req = (byte) req->Req;
538 dword type = req->type & DIVA_UM_IDI_REQ_TYPE_MASK;
539
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;
552
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;
558
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;
564
565 default:
566 DBG_ERR(("A: A(%d) E(%08x) unknown type=%08x",
567 e->adapter->adapter_nr, e,
568 type));
569 return (-1);
570 }
571 }
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;
578 }
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;
475be4d8 583 e->e.X->P = (byte *)&req[1]; /* Our buffer is safe */
1da177e4
LT
584
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));
588
589 e->rc_count++;
590
591 if (e->adapter && e->adapter->d.request) {
592 diva_um_idi_start_wdog(e);
593 (*(e->adapter->d.request)) (&e->e);
594 }
595
596 if (assign) {
597 if (e->e.Rc == OUT_OF_RESOURCES) {
598 /*
475be4d8
JP
599 XDI has no entities more, call was not forwarded to the card,
600 no callback will be scheduled
601 */
1da177e4
LT
602 DBG_ERR(("A: A(%d) E(%08x) XDI out of entities",
603 e->adapter->adapter_nr, e));
604
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;
619 }
620 }
621
622 return (0);
623}
624
475be4d8 625static int process_idi_rc(divas_um_idi_entity_t *e, byte rc)
1da177e4
LT
626{
627 DBG_TRC(("A(%d) E(%08x) rc(%02x-%02x-%02x)",
628 e->adapter->adapter_nr, e, e->e.Id, rc, e->e.RcCh));
629
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;
647 }
648 }
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 */
653 }
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;
668 }
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));
672 }
673 write_return_code(e, rc);
674
675 return (1);
676}
677
475be4d8 678static int process_idi_ind(divas_um_idi_entity_t *e, byte ind)
1da177e4
LT
679{
680 int do_wakeup = 0;
681
682 if (e->e.complete != 0x02) {
683 diva_um_idi_ind_hdr_t *pind =
475be4d8
JP
684 (diva_um_idi_ind_hdr_t *)
685 diva_data_q_get_segment4write(&e->data);
1da177e4
LT
686 if (pind) {
687 e->e.RNum = 1;
475be4d8 688 e->e.R->P = (byte *)&pind[1];
1da177e4 689 e->e.R->PLength =
475be4d8
JP
690 (word) (diva_data_q_get_max_length(&e->data) -
691 sizeof(*pind));
1da177e4
LT
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));
696
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;
704 }
705 } else {
706 diva_um_idi_ind_hdr_t *pind =
475be4d8 707 (diva_um_idi_ind_hdr_t *) (e->e.R->P);
1da177e4
LT
708
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));
712
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;
722 }
723
724 if ((e->status & DIVA_UM_IDI_RC_PENDING) && !e->rc.count) {
725 do_wakeup = 0;
726 }
727
728 return (do_wakeup);
729}
730
731/* --------------------------------------------------------------------------
475be4d8
JP
732 Write return code to the return code queue of entity
733 -------------------------------------------------------------------------- */
734static int write_return_code(divas_um_idi_entity_t *e, byte rc)
1da177e4
LT
735{
736 diva_um_idi_ind_hdr_t *prc;
737
738 if (!(prc =
475be4d8 739 (diva_um_idi_ind_hdr_t *) diva_data_q_get_segment4write(&e->rc)))
1da177e4
LT
740 {
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);
745 }
746
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));
752
753 return (0);
754}
755
756/* --------------------------------------------------------------------------
475be4d8
JP
757 Return amount of entries that can be bead from this entity or
758 -1 if adapter was removed
759 -------------------------------------------------------------------------- */
1da177e4
LT
760int diva_user_mode_idi_ind_ready(void *entity, void *os_handle)
761{
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;
766
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;
772
773 if ((!a) || (a->status & DIVA_UM_IDI_ADAPTER_REMOVED)) {
774 /*
475be4d8
JP
775 Adapter was unloaded
776 */
1da177e4
LT
777 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
778 return (-1); /* adapter was removed */
779 }
780 if (e->status & DIVA_UM_IDI_REMOVED) {
781 /*
475be4d8
JP
782 entity was removed as result of adapter removal
783 user should assign this entity again
784 */
1da177e4
LT
785 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
786 return (-1);
787 }
788
789 ret = e->rc.count + e->data.count;
790
791 if ((e->status & DIVA_UM_IDI_RC_PENDING) && !e->rc.count) {
792 ret = 0;
793 }
794
795 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "ind_ready");
796
797 return (ret);
798}
799
800void *diva_um_id_get_os_context(void *entity)
801{
802 return (((divas_um_idi_entity_t *) entity)->os_context);
803}
804
805int divas_um_idi_entity_assigned(void *entity)
806{
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;
811
812 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "assigned?");
813
814
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);
821 }
822
823 e->status |= DIVA_UM_IDI_REMOVE_PENDING;
824
825 ret = (e->e.Id || e->rc_count
826 || (e->status & DIVA_UM_IDI_ASSIGN_PENDING));
827
828 DBG_TRC(("Id:%02x, rc_count:%d, status:%08x", e->e.Id, e->rc_count,
829 e->status))
830
475be4d8 831 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "assigned?");
1da177e4
LT
832
833 return (ret);
834}
835
836int divas_um_idi_entity_start_remove(void *entity)
837{
838 divas_um_idi_entity_t *e;
839 diva_um_idi_adapter_t *a;
840 diva_os_spin_lock_magic_t old_irql;
841
842 diva_os_enter_spin_lock(&adapter_lock, &old_irql, "start_remove");
843
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);
850 }
851
852 if (e->rc_count) {
853 /*
475be4d8
JP
854 Entity BUSY
855 */
1da177e4
LT
856 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
857 return (1);
858 }
859
860 if (!e->e.Id) {
861 /*
475be4d8
JP
862 Remove request was already pending, and arrived now
863 */
1da177e4
LT
864 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
865 return (0); /* REMOVE was pending */
866 }
867
868 /*
475be4d8
JP
869 Now send remove request
870 */
1da177e4
LT
871 e->e.Req = REMOVE;
872 e->e.ReqCh = 0;
873
874 e->rc_count++;
875
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));
879
880 if (a->d.request)
881 (*(a->d.request)) (&e->e);
882
883 diva_os_leave_spin_lock(&adapter_lock, &old_irql, "start_remove");
884
885 return (0);
886}