1/*
2 * Copyright 2004-2008, Haiku, Inc. All RightsReserved.
3 * Copyright 2002/03, Thomas Kurschel. All rights reserved.
4 *
5 * Distributed under the terms of the MIT License.
6 */
7
8/*
9	Controllers use this interface to interact with bus manager.
10*/
11
12
13#include "scsi_internal.h"
14#include "queuing.h"
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20
21/**	new scsi controller added
22 *	in return, we register a new scsi bus node and let its fixed
23 *	consumer (the SCSI device layer) automatically scan it for devices
24 */
25
26static status_t
27scsi_controller_added(device_node *parent)
28{
29	const char *controller_name;
30	int32 pathID;
31
32	SHOW_FLOW0(4, "");
33
34	if (pnp->get_attr_string(parent, SCSI_DESCRIPTION_CONTROLLER_NAME,
35			&controller_name, false) != B_OK) {
36		dprintf("scsi: ignored controller - controller name missing\n");
37		return B_ERROR;
38	}
39
40	pathID = pnp->create_id(SCSI_PATHID_GENERATOR);
41	if (pathID < 0) {
42		dprintf("scsi: Cannot register SCSI controller %s - out of path IDs\n",
43			controller_name);
44		return B_ERROR;
45	}
46
47	{
48		device_attr attrs[] = {
49			// remember who we are
50			// (could use the controller name, but probably some software would choke)
51			// TODO create_id() generates a 32 bit ranged integer but we need only 8 bits
52			{ SCSI_BUS_PATH_ID_ITEM, B_UINT8_TYPE, { ui8: (uint8)pathID }},
53
54			// tell PnP manager to clean up ID
55//			{ PNP_MANAGER_ID_GENERATOR, B_STRING_TYPE, { string: SCSI_PATHID_GENERATOR }},
56//			{ PNP_MANAGER_AUTO_ID, B_UINT32_TYPE, { ui32: path_id }},
57			{}
58		};
59
60		return pnp->register_node(parent, SCSI_BUS_MODULE_NAME, attrs, NULL,
61			NULL);
62	}
63}
64
65
66static status_t
67scsi_controller_init(device_node *node, void **_cookie)
68{
69	*_cookie = node;
70	return B_OK;
71}
72
73
74static status_t
75scsi_controller_register_raw_device(void *_cookie)
76{
77	device_node *node = (device_node *)_cookie;
78	uint32 channel;
79	uint8 pathID;
80	char *name;
81
82#if 1
83// TODO: this seems to cause a crash in some configurations, and needs to be investigated!
84//		see bug #389 and #393.
85// TODO: check if the above is still true
86	if (pnp->get_attr_uint32(node, "ide/channel_id", &channel, true) == B_OK) {
87		// this is actually an IDE device, we don't need to publish
88		// a bus device for those
89		return B_OK;
90	}
91#endif
92	pnp->get_attr_uint8(node, SCSI_BUS_PATH_ID_ITEM, &pathID, false);
93
94	// put that on heap to not overflow the limited kernel stack
95	name = (char*)malloc(PATH_MAX + 1);
96	if (name == NULL)
97		return B_NO_MEMORY;
98
99	snprintf(name, PATH_MAX + 1, "bus/scsi/%d/bus_raw", pathID);
100
101	return pnp->publish_device(node, name, SCSI_BUS_RAW_MODULE_NAME);
102}
103
104
105static status_t
106std_ops(int32 op, ...)
107{
108	switch (op) {
109		case B_MODULE_INIT:
110		case B_MODULE_UNINIT:
111			return B_OK;
112
113		default:
114			return B_ERROR;
115	}
116}
117
118
119scsi_for_sim_interface scsi_for_sim_module =
120{
121	{
122		{
123			SCSI_FOR_SIM_MODULE_NAME,
124			0,
125			std_ops
126		},
127
128		NULL,	// supported devices
129		scsi_controller_added,
130		scsi_controller_init,
131		NULL,	// uninit
132		scsi_controller_register_raw_device,
133		NULL,	// rescan
134		NULL,	// removed
135	},
136
137	scsi_requeue_request,
138	scsi_resubmit_request,
139	scsi_request_finished,
140
141	scsi_alloc_dpc,
142	scsi_free_dpc,
143	scsi_schedule_dpc,
144
145	scsi_block_bus,
146	scsi_unblock_bus,
147	scsi_block_device,
148	scsi_unblock_device,
149
150	scsi_cont_send_bus,
151	scsi_cont_send_device
152};
153