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
|
/*
* 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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
*/
/*
* nx1394.c
* 1394 Services Layer Nexus Support Routines
* Routines in this file implement nexus bus_ops.
*/
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/modctl.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/types.h>
#include <sys/ddi_impldefs.h>
#include <sys/1394/t1394.h>
#include <sys/1394/s1394.h>
#include <sys/1394/h1394.h>
static int nx1394_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
ddi_dma_attr_t *attr, int (*waitfnp)(caddr_t), caddr_t arg,
ddi_dma_handle_t *handlep);
static int nx1394_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op,
void *arg, void *result);
static int nx1394_get_event_cookie(dev_info_t *dip, dev_info_t *rdip,
char *name, ddi_eventcookie_t *event_cookiep);
static int nx1394_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
ddi_eventcookie_t eventhdl, void (*callback)(), void *arg,
ddi_callback_id_t *cb_id);
static int nx1394_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id);
static int nx1394_post_event(dev_info_t *dip, dev_info_t *rdip,
ddi_eventcookie_t eventhdl, void *impl_data);
struct bus_ops nx1394_busops = {
BUSO_REV,
nullbusmap, /* bus_map */
NULL, /* bus_get_intrspec */
NULL, /* bus_add_intrspec */
NULL, /* bus_remove_intrspec */
i_ddi_map_fault, /* XXXX bus_map_fault */
NULL, /* bus_dma_map */
nx1394_dma_allochdl,
ddi_dma_freehdl,
ddi_dma_bindhdl,
ddi_dma_unbindhdl,
ddi_dma_flush,
ddi_dma_win,
ddi_dma_mctl, /* bus_dma_ctl */
nx1394_bus_ctl, /* bus_ctl */
ddi_bus_prop_op, /* bus_prop_op */
nx1394_get_event_cookie, /* (*bus_get_eventcookie() */
nx1394_add_eventcall, /* (*bus_add_eventcall)(); */
nx1394_remove_eventcall, /* (*bus_remove_eventcall)(); */
nx1394_post_event, /* (*bus_post_event)(); */
0, /* (*interrupt control)(); */
0, /* (*bus_config)(); */
0, /* (*bus_unconfig)(); */
0, /* (*bus_fm_init)(); */
0, /* (*bus_fm_fini)(); */
0, /* (*bus_fm_access_enter)(); */
0, /* (*bus_fm_access_exit)(); */
0, /* (*bus_power)(); */
i_ddi_intr_ops /* (*bus_intr_op)(); */
};
/*
* removal/insertion/reset events
*/
#define NX1394_EVENT_TAG_HOT_REMOVAL 0
#define NX1394_EVENT_TAG_HOT_INSERTION 1
#define NX1394_EVENT_TAG_BUS_RESET 2
static ndi_event_definition_t nx1394_event_defs[] = {
{NX1394_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
NDI_EVENT_POST_TO_TGT},
{NX1394_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
NDI_EVENT_POST_TO_TGT},
{NX1394_EVENT_TAG_BUS_RESET, DDI_DEVI_BUS_RESET_EVENT, EPL_KERNEL,
NDI_EVENT_POST_TO_ALL},
};
#define NX1394_N_EVENTS \
(sizeof (nx1394_event_defs) / sizeof (ndi_event_definition_t))
static ndi_event_set_t nx1394_events = {
NDI_EVENTS_REV1, NX1394_N_EVENTS, nx1394_event_defs
};
/*
* nx1394_bus_ctl()
* This routine implements nexus bus ctl operations. Of importance are
* DDI_CTLOPS_REPORTDEV, DDI_CTLOPS_INITCHILD, DDI_CTLOPS_UNINITCHILD
* and DDI_CTLOPS_POWER. For DDI_CTLOPS_INITCHILD, it tries to lookup
* reg property on the child node and builds and sets the name
* (name is of the form GGGGGGGGGGGGGGGG[,AAAAAAAAAAAA], where
* GGGGGGGGGGGGGGGG is the GUID and AAAAAAAAAAAA is the optional unit
* address).
*/
static int
nx1394_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op, void *arg,
void *result)
{
int status;
switch (op) {
case DDI_CTLOPS_REPORTDEV: {
dev_info_t *pdip = ddi_get_parent(rdip);
cmn_err(CE_CONT, "?%s%d at %s%d",
ddi_node_name(rdip), ddi_get_instance(rdip),
ddi_node_name(pdip), ddi_get_instance(pdip));
return (DDI_SUCCESS);
}
case DDI_CTLOPS_INITCHILD: {
dev_info_t *ocdip, *cdip = (dev_info_t *)arg;
dev_info_t *pdip = ddi_get_parent(cdip);
int reglen, i;
uint32_t *regptr;
char addr[MAXNAMELEN];
i = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, cdip,
DDI_PROP_DONTPASS, "reg", (int **)®ptr,
(uint_t *)®len);
if (i != DDI_PROP_SUCCESS) {
cmn_err(CE_NOTE, "!%s(%d): \"reg\" property not found",
ddi_node_name(cdip), ddi_get_instance(cdip));
return (DDI_NOT_WELL_FORMED);
}
ASSERT(reglen != 0);
/*
* addr is of the format GGGGGGGGGGGGGGGG[,AAAAAAAAAAAA]
*/
if (regptr[2] || regptr[3]) {
(void) sprintf(addr, "%08x%08x,%04x%08x", regptr[0],
regptr[1], regptr[2], regptr[3]);
} else {
(void) sprintf(addr, "%08x%08x", regptr[0], regptr[1]);
}
ddi_prop_free(regptr);
ddi_set_name_addr(cdip, addr);
/*
* Check for a node with the same name & addr as the current
* node. If such a node exists, return failure.
*/
if ((ocdip = ndi_devi_find(pdip, ddi_node_name(cdip), addr)) !=
NULL && ocdip != cdip) {
cmn_err(CE_NOTE,
"!%s(%d): Duplicate dev_info node found %s@%s",
ddi_node_name(cdip), ddi_get_instance(cdip),
ddi_node_name(ocdip), addr);
ddi_set_name_addr(cdip, NULL);
return (DDI_NOT_WELL_FORMED);
}
/*
* If HAL (parent dip) has "active-dma-flush" property, then
* add property to child as well. Workaround for active
* context flushing bug in Schizo rev 2.1 and 2.2.
*/
if (ddi_prop_exists(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
"active-dma-flush") != 0) {
status = ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
"active-dma-flush", 1);
if (status != NDI_SUCCESS) {
cmn_err(CE_NOTE, "!%s(%d): Unable to add "
"\"active-dma-flush\" property",
ddi_node_name(cdip),
ddi_get_instance(cdip));
ddi_set_name_addr(cdip, NULL);
return (DDI_NOT_WELL_FORMED);
}
}
return (DDI_SUCCESS);
}
case DDI_CTLOPS_UNINITCHILD: {
ddi_prop_remove_all((dev_info_t *)arg);
ddi_set_name_addr((dev_info_t *)arg, NULL);
return (DDI_SUCCESS);
}
case DDI_CTLOPS_IOMIN: {
status = ddi_ctlops(dip, rdip, op, arg, result);
return (status);
}
case DDI_CTLOPS_POWER: {
return (DDI_SUCCESS);
}
/*
* These ops correspond to functions that "shouldn't" be called
* by a 1394 client driver.
*/
case DDI_CTLOPS_DMAPMAPC:
case DDI_CTLOPS_REPORTINT:
case DDI_CTLOPS_REGSIZE:
case DDI_CTLOPS_NREGS:
case DDI_CTLOPS_SIDDEV:
case DDI_CTLOPS_SLAVEONLY:
case DDI_CTLOPS_AFFINITY:
case DDI_CTLOPS_POKE:
case DDI_CTLOPS_PEEK: {
cmn_err(CE_CONT, "!%s(%d): invalid op (%d) from %s(%d)",
ddi_node_name(dip), ddi_get_instance(dip),
op, ddi_node_name(rdip), ddi_get_instance(rdip));
return (DDI_FAILURE);
}
/*
* Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
*/
default: {
status = ddi_ctlops(dip, rdip, op, arg, result);
return (status);
}
}
}
/*
* nx1394_dma_allochdl()
* Merges the ddi_dma_attr_t passed in by the target (using
* ddi_dma_alloc_handle() call) with that of the hal and passes the alloc
* handle request up the device by calling ddi_dma_allochdl().
*/
static int
nx1394_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
int (*waitfnp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
{
s1394_hal_t *hal;
ddi_dma_attr_t *hal_attr;
int status;
_NOTE(SCHEME_PROTECTS_DATA("unique (per thread)", ddi_dma_attr_t))
/*
* If hal calls ddi_dma_alloc_handle, dip == rdip == hal dip.
* Unfortunately, we cannot verify this (by way of looking up for hal
* dip) here because h1394_attach() may happen much later.
*/
if (dip != rdip) {
hal = s1394_dip_to_hal(ddi_get_parent(rdip));
ASSERT(hal);
hal_attr = &hal->halinfo.dma_attr;
ASSERT(hal_attr);
ddi_dma_attr_merge(attr, hal_attr);
}
status = ddi_dma_allochdl(dip, rdip, attr, waitfnp, arg, handlep);
return (status);
}
/*
* nx1394_get_event_cookie()
* Called when a child node calls ddi_get_eventcookie().
* Returns event cookie corresponding to event "name".
*/
static int
nx1394_get_event_cookie(dev_info_t *dip, dev_info_t *rdip, char *name,
ddi_eventcookie_t *event_cookiep)
{
int ret;
s1394_hal_t *hal;
hal = s1394_dip_to_hal(dip);
ASSERT(hal);
ret = ndi_event_retrieve_cookie(hal->hal_ndi_event_hdl,
rdip, name, event_cookiep, 0);
return (ret);
}
/*
* nx1394_add_eventcall()
* This gets called when a child node calls ddi_add_eventcall(). Registers
* the specified callback for the requested event cookie with the ndi
* event framework.
* dip is the hal dip. This routine calls ndi_event_add_callback(),
* allowing requests for events we don't generate to pass up the tree.
*/
static int
nx1394_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
ddi_eventcookie_t cookie, void (*callback)(), void *arg,
ddi_callback_id_t *cb_id)
{
int ret;
s1394_hal_t *hal;
#if defined(DEBUG)
char *event_name = NULL;
#endif
hal = s1394_dip_to_hal(dip);
ASSERT(hal);
ret = ndi_event_add_callback(hal->hal_ndi_event_hdl, rdip, cookie,
callback, arg, NDI_NOSLEEP, cb_id);
#if defined(DEBUG)
event_name = ndi_event_cookie_to_name(hal->hal_ndi_event_hdl, cookie);
if (event_name == NULL)
event_name = "";
#endif
return (ret);
}
/*
* nx1394_remove_eventcall()
* Called as a result of a child node calling ddi_remove_eventcall().
* Unregisters the callback corresponding to the callback id passed in.
*/
static int
nx1394_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
{
int ret;
s1394_hal_t *hal;
ddi_eventcookie_t cookie;
#if defined(DEBUG)
char *event_name = NULL;
#endif
ASSERT(cb_id);
cookie = ((ndi_event_callbacks_t *)cb_id)->ndi_evtcb_cookie;
hal = s1394_dip_to_hal(dip);
ASSERT(hal);
ret = ndi_event_remove_callback(hal->hal_ndi_event_hdl, cb_id);
#if defined(DEBUG)
event_name = ndi_event_cookie_to_name(hal->hal_ndi_event_hdl, cookie);
if (event_name == NULL)
event_name = "";
#endif
return (ret);
}
/*
* nx1394_post_event()
* Called when a child node calls ddi_post_event. If the event is one of
* the events supported by us (bus reset/insert/remove, for now), builds
* a t1394_localinfo_t structure and calls ndi_event_run_callbacks(). This
* will result in all registered callbacks being invoked with
* t1394_localinfo_t as the impl_data. (see ddi_add_eventcall for callback
* arguments.) If the event is not defined by us, the request is
* propagated up the device tree by calling ndi_post_event().
*/
static int
nx1394_post_event(dev_info_t *dip, dev_info_t *rdip, ddi_eventcookie_t cookie,
void *impl_data)
{
int ret;
char *name;
s1394_hal_t *hal;
t1394_localinfo_t localinfo;
hal = s1394_dip_to_hal(dip);
ASSERT(hal);
name = ndi_event_cookie_to_name(hal->hal_ndi_event_hdl, cookie);
/* name is NULL if we don't generate the event */
if (name != NULL) {
mutex_enter(&hal->topology_tree_mutex);
localinfo.bus_generation = hal->generation_count;
localinfo.local_nodeID = hal->node_id;
mutex_exit(&hal->topology_tree_mutex);
impl_data = &localinfo;
ret = ndi_event_run_callbacks(hal->hal_ndi_event_hdl,
rdip, cookie, impl_data);
return (ret);
} else {
ret = ndi_post_event(ddi_get_parent(dip), rdip, cookie,
impl_data);
return (ret);
}
}
/*
* nx1394_define_events()
* Allocates event handle for the hal dip and binds event set to it.
*/
int
nx1394_define_events(s1394_hal_t *hal)
{
int ret;
/* get event handle */
ret = ndi_event_alloc_hdl(hal->halinfo.dip, hal->halinfo.hw_interrupt,
&hal->hal_ndi_event_hdl, NDI_SLEEP);
if (ret == NDI_SUCCESS) {
/* and bind to it */
ret = ndi_event_bind_set(hal->hal_ndi_event_hdl, &nx1394_events,
NDI_SLEEP);
if (ret != NDI_SUCCESS) {
(void) ndi_event_free_hdl(hal->hal_ndi_event_hdl);
return (DDI_FAILURE);
}
}
return (DDI_SUCCESS);
}
/*
* nx1394_undefine_events()
* Unbinds event set bound to the hal and frees the event handle.
*/
void
nx1394_undefine_events(s1394_hal_t *hal)
{
int ret;
ret = ndi_event_unbind_set(hal->hal_ndi_event_hdl, &nx1394_events,
NDI_SLEEP);
if (ret == NDI_SUCCESS)
ret = ndi_event_free_hdl(hal->hal_ndi_event_hdl);
}
|