xref: /haiku/src/add-ons/kernel/bus_managers/scsi/sim_interface.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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 
26 static status_t
27 scsi_controller_added(device_node *parent)
28 {
29 	const char *controller_name;
30 	int 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 			{ SCSI_BUS_PATH_ID_ITEM, B_UINT8_TYPE, { ui8: pathID }},
52 
53 			// tell PnP manager to clean up ID
54 //			{ PNP_MANAGER_ID_GENERATOR, B_STRING_TYPE, { string: SCSI_PATHID_GENERATOR }},
55 //			{ PNP_MANAGER_AUTO_ID, B_UINT32_TYPE, { ui32: path_id }},
56 			{}
57 		};
58 
59 		return pnp->register_node(parent, SCSI_BUS_MODULE_NAME, attrs, NULL,
60 			NULL);
61 	}
62 }
63 
64 
65 static status_t
66 scsi_controller_init(device_node *node, void **_cookie)
67 {
68 	*_cookie = node;
69 	return B_OK;
70 }
71 
72 
73 static status_t
74 scsi_controller_register_raw_device(void *_cookie)
75 {
76 	device_node *node = (device_node *)_cookie;
77 	uint32 channel;
78 	uint8 pathID;
79 	char *name;
80 
81 #if 1
82 // TODO: this seems to cause a crash in some configurations, and needs to be investigated!
83 //		see bug #389 and #393.
84 // TODO: check if the above is still true
85 	if (pnp->get_attr_uint32(node, "ide/channel_id", &channel, true) == B_OK) {
86 		// this is actually an IDE device, we don't need to publish
87 		// a bus device for those
88 		return B_OK;
89 	}
90 #endif
91 	pnp->get_attr_uint8(node, SCSI_BUS_PATH_ID_ITEM, &pathID, false);
92 
93 	// put that on heap to not overflow the limited kernel stack
94 	name = (char*)malloc(PATH_MAX + 1);
95 	if (name == NULL)
96 		return B_NO_MEMORY;
97 
98 	snprintf(name, PATH_MAX + 1, "bus/scsi/%d/bus_raw", pathID);
99 
100 	return pnp->publish_device(node, name, SCSI_BUS_RAW_MODULE_NAME);
101 }
102 
103 
104 static status_t
105 std_ops(int32 op, ...)
106 {
107 	switch (op) {
108 		case B_MODULE_INIT:
109 		case B_MODULE_UNINIT:
110 			return B_OK;
111 
112 		default:
113 			return B_ERROR;
114 	}
115 }
116 
117 
118 scsi_for_sim_interface scsi_for_sim_module =
119 {
120 	{
121 		{
122 			SCSI_FOR_SIM_MODULE_NAME,
123 			0,
124 			std_ops
125 		},
126 
127 		NULL,	// supported devices
128 		scsi_controller_added,
129 		scsi_controller_init,
130 		NULL,	// uninit
131 		scsi_controller_register_raw_device,
132 		NULL,	// rescan
133 		NULL,	// removed
134 	},
135 
136 	scsi_requeue_request,
137 	scsi_resubmit_request,
138 	scsi_request_finished,
139 
140 	scsi_alloc_dpc,
141 	scsi_free_dpc,
142 	scsi_schedule_dpc,
143 
144 	scsi_block_bus,
145 	scsi_unblock_bus,
146 	scsi_block_device,
147 	scsi_unblock_device,
148 
149 	scsi_cont_send_bus,
150 	scsi_cont_send_device
151 };
152