1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2017 Joyent, Inc.
*/
/*
* av1394 asynchronous module
*/
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/1394/targets/av1394/av1394_impl.h>
/* configuration routines */
static void av1394_async_cleanup(av1394_inst_t *, int);
static int av1394_async_create_minor_node(av1394_inst_t *);
static void av1394_async_remove_minor_node(av1394_inst_t *);
static int av1394_async_update_targetinfo(av1394_inst_t *);
static int av1394_async_db2arq_type(int);
static void av1394_async_putbq(av1394_queue_t *, mblk_t *);
static int av1394_ioctl_arq_get_ibuf_size(av1394_inst_t *, void *, int);
static int av1394_ioctl_arq_set_ibuf_size(av1394_inst_t *, void *, int);
/* tunables */
int av1394_ibuf_size_default = 64 * 1024; /* default ibuf size */
int av1394_ibuf_size_max = 1024 * 1024; /* max ibuf size */
/*
*
* --- configuration entry points
*
*/
int
av1394_async_attach(av1394_inst_t *avp)
{
av1394_async_t *ap = &avp->av_a;
ddi_iblock_cookie_t ibc = avp->av_attachinfo.iblock_cookie;
mutex_init(&ap->a_mutex, NULL, MUTEX_DRIVER, ibc);
av1394_initq(&ap->a_rq, ibc, av1394_ibuf_size_default);
if (av1394_fcp_attach(avp) != DDI_SUCCESS) {
av1394_async_cleanup(avp, 1);
return (DDI_FAILURE);
}
if (av1394_cfgrom_init(avp) != DDI_SUCCESS) {
av1394_async_cleanup(avp, 2);
return (DDI_FAILURE);
}
if (av1394_async_create_minor_node(avp) != DDI_SUCCESS) {
av1394_async_cleanup(avp, 3);
return (DDI_FAILURE);
}
if (av1394_async_update_targetinfo(avp) != DDI_SUCCESS) {
av1394_async_cleanup(avp, 4);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
void
av1394_async_detach(av1394_inst_t *avp)
{
av1394_async_cleanup(avp, AV1394_CLEANUP_LEVEL_MAX);
}
void
av1394_async_bus_reset(av1394_inst_t *avp)
{
av1394_async_t *ap = &avp->av_a;
mblk_t *bp;
(void) av1394_async_update_targetinfo(avp);
mutex_enter(&ap->a_mutex);
if (ap->a_nopen > 0) {
mutex_exit(&ap->a_mutex);
return;
}
mutex_exit(&ap->a_mutex);
/* queue up a bus reset message */
if ((bp = allocb(1, BPRI_HI)) != NULL) {
DB_TYPE(bp) = AV1394_M_BUS_RESET;
av1394_async_putq_rq(avp, bp);
}
}
int
av1394_async_cpr_resume(av1394_inst_t *avp)
{
int ret;
ret = av1394_async_update_targetinfo(avp);
return (ret);
}
void
av1394_async_reconnect(av1394_inst_t *avp)
{
(void) av1394_async_update_targetinfo(avp);
}
int
av1394_async_open(av1394_inst_t *avp, int flag)
{
av1394_async_t *ap = &avp->av_a;
mutex_enter(&ap->a_mutex);
if (ap->a_nopen == 0) {
ap->a_pollevents = 0;
}
ap->a_nopen++;
ap->a_oflag = flag;
mutex_exit(&ap->a_mutex);
return (0);
}
/*ARGSUSED*/
int
av1394_async_close(av1394_inst_t *avp, int flag)
{
av1394_async_t *ap = &avp->av_a;
av1394_cfgrom_close(avp);
av1394_flushq(&ap->a_rq);
mutex_enter(&ap->a_mutex);
ap->a_nopen = 0;
ap->a_pollevents = 0;
mutex_exit(&ap->a_mutex);
return (0);
}
int
av1394_async_read(av1394_inst_t *avp, struct uio *uiop)
{
av1394_async_t *ap = &avp->av_a;
av1394_queue_t *q = &ap->a_rq;
iec61883_arq_t arq;
int ret = 0;
mblk_t *mp;
int dbtype;
int len;
/* copyout as much as we can */
while ((uiop->uio_resid > 0) && (ret == 0)) {
/*
* if data is available, copy it out. otherwise wait until
* data arrives, unless opened with non-blocking flag
*/
if ((mp = av1394_getq(q)) == NULL) {
if (ap->a_oflag & FNDELAY) {
return (EAGAIN);
}
if (av1394_qwait_sig(q) <= 0) {
ret = EINTR;
}
continue;
}
dbtype = AV1394_DBTYPE(mp);
/* generate and copyout ARQ header, if not already */
if (!AV1394_IS_NOHDR(mp)) {
/* headers cannot be partially read */
if (uiop->uio_resid < sizeof (arq)) {
av1394_async_putbq(q, mp);
ret = EINVAL;
break;
}
arq.arq_type = av1394_async_db2arq_type(dbtype);
arq.arq_len = MBLKL(mp);
arq.arq_data.octlet = 0;
/* copy ARQ-embedded data */
len = min(arq.arq_len, sizeof (arq.arq_data));
bcopy(mp->b_rptr, &arq.arq_data.buf[0], len);
/* copyout the ARQ */
ret = uiomove(&arq, sizeof (arq), UIO_READ, uiop);
if (ret != 0) {
av1394_async_putbq(q, mp);
break;
}
mp->b_rptr += len;
AV1394_MARK_NOHDR(mp);
}
/* any data left? */
if (MBLKL(mp) == 0) {
freemsg(mp);
continue;
}
/* now we have some data and some user buffer space to fill */
len = min(uiop->uio_resid, MBLKL(mp));
if (len > 0) {
ret = uiomove(mp->b_rptr, len, UIO_READ, uiop);
if (ret != 0) {
av1394_async_putbq(q, mp);
break;
}
mp->b_rptr += len;
}
/* save the rest of the data for later */
if (MBLKL(mp) > 0) {
av1394_async_putbq(q, mp);
}
}
return (0);
}
int
av1394_async_write(av1394_inst_t *avp, struct uio *uiop)
{
iec61883_arq_t arq;
int ret;
/* all data should arrive in ARQ format */
while (uiop->uio_resid >= sizeof (arq)) {
if ((ret = uiomove(&arq, sizeof (arq), UIO_WRITE, uiop)) != 0) {
break;
}
switch (arq.arq_type) {
case IEC61883_ARQ_FCP_CMD:
case IEC61883_ARQ_FCP_RESP:
ret = av1394_fcp_write(avp, &arq, uiop);
break;
default:
ret = EINVAL;
}
if (ret != 0) {
break;
}
}
return (ret);
}
/*ARGSUSED*/
int
av1394_async_ioctl(av1394_inst_t *avp, int cmd, intptr_t arg, int mode,
int *rvalp)
{
int ret = EINVAL;
switch (cmd) {
case IEC61883_ARQ_GET_IBUF_SIZE:
ret = av1394_ioctl_arq_get_ibuf_size(avp, (void *)arg, mode);
break;
case IEC61883_ARQ_SET_IBUF_SIZE:
ret = av1394_ioctl_arq_set_ibuf_size(avp, (void *)arg, mode);
break;
case IEC61883_NODE_GET_BUS_NAME:
ret = av1394_ioctl_node_get_bus_name(avp, (void *)arg, mode);
break;
case IEC61883_NODE_GET_UID:
ret = av1394_ioctl_node_get_uid(avp, (void *)arg, mode);
break;
case IEC61883_NODE_GET_TEXT_LEAF:
ret = av1394_ioctl_node_get_text_leaf(avp, (void *)arg, mode);
}
return (ret);
}
int
av1394_async_poll(av1394_inst_t *avp, short events, int anyyet, short *reventsp,
struct pollhead **phpp)
{
av1394_async_t *ap = &avp->av_a;
av1394_queue_t *rq = &ap->a_rq;
if (events & (POLLIN | POLLET)) {
if ((events & POLLIN) && av1394_peekq(rq)) {
*reventsp |= POLLIN;
}
if ((!*reventsp && !anyyet) || (events & POLLET)) {
mutex_enter(&ap->a_mutex);
if (events & POLLIN) {
ap->a_pollevents |= POLLIN;
}
*phpp = &ap->a_pollhead;
mutex_exit(&ap->a_mutex);
}
}
return (0);
}
/*
* put a message on the read queue, take care of polling
*/
void
av1394_async_putq_rq(av1394_inst_t *avp, mblk_t *mp)
{
av1394_async_t *ap = &avp->av_a;
if (!av1394_putq(&ap->a_rq, mp)) {
freemsg(mp);
} else {
mutex_enter(&ap->a_mutex);
if (ap->a_pollevents & POLLIN) {
ap->a_pollevents &= ~POLLIN;
mutex_exit(&ap->a_mutex);
pollwakeup(&ap->a_pollhead, POLLIN);
} else {
mutex_exit(&ap->a_mutex);
}
}
}
/*
*
* --- configuration routines
*
* av1394_async_cleanup()
* Cleanup after attach
*/
static void
av1394_async_cleanup(av1394_inst_t *avp, int level)
{
av1394_async_t *ap = &avp->av_a;
ASSERT((level > 0) && (level <= AV1394_CLEANUP_LEVEL_MAX));
switch (level) {
default:
av1394_async_remove_minor_node(avp);
/* FALLTHRU */
case 3:
av1394_cfgrom_fini(avp);
/* FALLTHRU */
case 2:
av1394_fcp_detach(avp);
/* FALLTHRU */
case 1:
av1394_destroyq(&ap->a_rq);
mutex_destroy(&ap->a_mutex);
}
}
/*
* av1394_async_create_minor_node()
* Create async minor node
*/
static int
av1394_async_create_minor_node(av1394_inst_t *avp)
{
int ret;
ret = ddi_create_minor_node(avp->av_dip, "async",
S_IFCHR, AV1394_ASYNC_INST2MINOR(avp->av_instance),
DDI_NT_AV_ASYNC, 0);
return (ret);
}
/*
* av1394_async_remove_minor_node()
* Remove async minor node
*/
static void
av1394_async_remove_minor_node(av1394_inst_t *avp)
{
ddi_remove_minor_node(avp->av_dip, "async");
}
/*
* av1394_async_update_targetinfo()
* Retrieve target info and bus generation
*/
static int
av1394_async_update_targetinfo(av1394_inst_t *avp)
{
av1394_async_t *ap = &avp->av_a;
uint_t bg;
int ret;
mutex_enter(&avp->av_mutex);
bg = avp->av_attachinfo.localinfo.bus_generation;
mutex_exit(&avp->av_mutex);
mutex_enter(&ap->a_mutex);
ret = t1394_get_targetinfo(avp->av_t1394_hdl, bg, 0, &ap->a_targetinfo);
ap->a_bus_generation = bg;
mutex_exit(&ap->a_mutex);
return (ret);
}
static int
av1394_async_db2arq_type(int dbtype)
{
int arq_type;
switch (dbtype) {
case AV1394_M_FCP_RESP:
arq_type = IEC61883_ARQ_FCP_RESP;
break;
case AV1394_M_FCP_CMD:
arq_type = IEC61883_ARQ_FCP_CMD;
break;
case AV1394_M_BUS_RESET:
arq_type = IEC61883_ARQ_BUS_RESET;
break;
default:
ASSERT(0); /* cannot happen */
}
return (arq_type);
}
static void
av1394_async_putbq(av1394_queue_t *q, mblk_t *mp)
{
if (!av1394_putbq(q, mp)) {
freemsg(mp);
}
}
/*ARGSUSED*/
static int
av1394_ioctl_arq_get_ibuf_size(av1394_inst_t *avp, void *arg, int mode)
{
av1394_async_t *ap = &avp->av_a;
int sz;
int ret = 0;
sz = av1394_getmaxq(&ap->a_rq);
if (ddi_copyout(&sz, arg, sizeof (sz), mode) != 0) {
ret = EFAULT;
}
return (ret);
}
/*ARGSUSED*/
static int
av1394_ioctl_arq_set_ibuf_size(av1394_inst_t *avp, void *arg, int mode)
{
av1394_async_t *ap = &avp->av_a;
int sz;
int ret = 0;
sz = (int)(intptr_t)arg;
if ((sz < 0) || (sz > av1394_ibuf_size_max)) {
ret = EINVAL;
} else {
av1394_setmaxq(&ap->a_rq, sz);
}
return (ret);
}
|