xref: /haiku/src/add-ons/kernel/bus_managers/scsi/ccb.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2002-2003, Thomas Kurschel. All rights reserved.
3  * Copyright 2024, Haiku, Inc. All rights reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 /*
8 	Part of Open SCSI bus manager
9 
10 	CCB manager
11 
12 	As allocation of ccb can be on the paging path we must use a
13 	locked pool.
14 */
15 
16 #include "scsi_internal.h"
17 
18 #include <slab/Slab.h>
19 
20 
21 
22 static object_cache* sCcbPool = NULL;
23 
24 
25 scsi_ccb *
26 scsi_alloc_ccb(scsi_device_info *device)
27 {
28 	SHOW_FLOW0( 3, "" );
29 
30 	scsi_ccb* ccb = (scsi_ccb*)object_cache_alloc(sCcbPool, 0);
31 	ccb->completion_cond.Init(ccb, "scsi ccb");
32 
33 	ccb->bus = device->bus;
34 	ccb->path_id = device->bus->path_id;
35 
36 	ccb->state = SCSI_STATE_FINISHED;
37 	ccb->device = device;
38 	ccb->target_id = device->target_id;
39 	ccb->target_lun = device->target_lun;
40 
41 	// reset some very important fields
42 	// TODO: should we better omit that to find bugs easier?
43 	ccb->sg_list = NULL;
44 	ccb->io_operation = NULL;
45 	ccb->sort = -1;
46 
47 	SHOW_FLOW(3, "path=%d", ccb->path_id);
48 
49 	return ccb;
50 }
51 
52 
53 void
54 scsi_free_ccb(scsi_ccb *ccb)
55 {
56 	SHOW_FLOW0( 3, "" );
57 
58 	if (ccb->state != SCSI_STATE_FINISHED)
59 		panic("Tried to free ccb that's still in use (state %d)\n", ccb->state);
60 
61 	object_cache_free(sCcbPool, ccb, 0);
62 }
63 
64 
65 status_t
66 init_ccb_alloc()
67 {
68 	sCcbPool = create_object_cache("scsi ccb", sizeof(scsi_ccb), 0, NULL, NULL, NULL);
69 	if (sCcbPool == NULL)
70 		return B_NO_MEMORY;
71 
72 	// it must be at least 1 for normal use and 1 for stand-by autosense request
73 	object_cache_set_minimum_reserve(sCcbPool, 2);
74 
75 	return B_OK;
76 }
77 
78 
79 void
80 uninit_ccb_alloc()
81 {
82 	delete_object_cache(sCcbPool);
83 }
84