1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #ifndef _ISCI_H 34 #define _ISCI_H 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/bus.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/types.h> 43 #include <sys/malloc.h> 44 #include <sys/rman.h> 45 46 #include <machine/bus.h> 47 #include <machine/resource.h> 48 49 #include <cam/cam.h> 50 #include <cam/cam_ccb.h> 51 #include <cam/cam_sim.h> 52 #include <cam/cam_xpt_sim.h> 53 54 #include <dev/isci/environment.h> 55 #include <dev/isci/scil/intel_pci.h> 56 57 #include <dev/isci/scil/sci_types.h> 58 #include <dev/isci/scil/sci_object.h> 59 #include <dev/isci/scil/sci_status.h> 60 #include <dev/isci/scil/sci_pool.h> 61 #include <dev/isci/scil/sci_fast_list.h> 62 63 #include <dev/isci/scil/sci_controller_constants.h> 64 65 #include <dev/isci/scil/scic_controller.h> 66 #include <dev/isci/scil/scic_config_parameters.h> 67 68 #define DEVICE2SOFTC(dev) ((struct isci_softc *) device_get_softc(dev)) 69 70 #define DEVICE_TIMEOUT 1000 71 #define SCI_MAX_TIMERS 32 72 73 #define ISCI_NUM_PCI_BARS 2 74 #define ISCI_MAX_LUN 8 75 76 MALLOC_DECLARE(M_ISCI); 77 78 struct ISCI_TIMER { 79 struct callout callout; 80 SCI_TIMER_CALLBACK_T callback; 81 void *cookie; 82 BOOL is_started; 83 }; 84 85 struct ISCI_REMOTE_DEVICE { 86 uint32_t index; 87 struct ISCI_DOMAIN *domain; 88 SCI_REMOTE_DEVICE_HANDLE_T sci_object; 89 BOOL is_resetting; 90 uint32_t frozen_lun_mask; 91 SCI_FAST_LIST_ELEMENT_T pending_device_reset_element; 92 93 /* 94 * This queue maintains CCBs that have been returned with 95 * SCI_IO_FAILURE_INVALID_STATE from the SCI layer. These CCBs 96 * need to be retried, but we cannot return CAM_REQUEUE_REQ because 97 * this status gets passed all the way back up to users of the pass(4) 98 * interface and breaks things like smartctl. So instead, we queue 99 * these CCBs internally. 100 */ 101 TAILQ_HEAD(,ccb_hdr) queued_ccbs; 102 103 /* 104 * Marker denoting this remote device needs its first queued ccb to 105 * be retried. 106 */ 107 BOOL release_queued_ccb; 108 109 /* 110 * Points to a CCB in the queue that is currently being processed by 111 * SCIL. This allows us to keep in flight CCBs in the queue so as to 112 * maintain ordering (i.e. in case we retry an I/O and then find out 113 * it needs to be retried again - it just keeps its same place in the 114 * queue. 115 */ 116 union ccb * queued_ccb_in_progress; 117 }; 118 119 struct ISCI_DOMAIN { 120 struct ISCI_CONTROLLER *controller; 121 SCI_DOMAIN_HANDLE_T sci_object; 122 uint8_t index; 123 struct ISCI_REMOTE_DEVICE *da_remote_device; 124 }; 125 126 struct ISCI_MEMORY 127 { 128 bus_addr_t physical_address; 129 bus_dma_tag_t dma_tag; 130 bus_dmamap_t dma_map; 131 POINTER_UINT virtual_address; 132 uint32_t size; 133 int error; 134 }; 135 136 struct ISCI_INTERRUPT_INFO 137 { 138 SCIC_CONTROLLER_HANDLER_METHODS_T *handlers; 139 void *interrupt_target_handle; 140 struct resource *res; 141 int rid; 142 void *tag; 143 144 }; 145 146 struct ISCI_CONTROLLER 147 { 148 struct isci_softc *isci; 149 uint8_t index; 150 SCI_CONTROLLER_HANDLE_T scif_controller_handle; 151 struct ISCI_DOMAIN domain[SCI_MAX_DOMAINS]; 152 BOOL is_started; 153 BOOL has_been_scanned; 154 uint32_t initial_discovery_mask; 155 BOOL is_frozen; 156 BOOL release_queued_ccbs; 157 uint8_t *remote_device_memory; 158 struct ISCI_MEMORY cached_controller_memory; 159 struct ISCI_MEMORY uncached_controller_memory; 160 struct ISCI_MEMORY request_memory; 161 bus_dma_tag_t buffer_dma_tag; 162 struct mtx lock; 163 struct cam_sim *sim; 164 struct cam_path *path; 165 struct ISCI_REMOTE_DEVICE *remote_device[SCI_MAX_REMOTE_DEVICES]; 166 void *timer_memory; 167 SCIC_OEM_PARAMETERS_T oem_parameters; 168 uint32_t oem_parameters_version; 169 uint32_t queue_depth; 170 uint32_t sim_queue_depth; 171 SCI_FAST_LIST_T pending_device_reset_list; 172 173 SCI_MEMORY_DESCRIPTOR_LIST_HANDLE_T mdl; 174 175 SCI_POOL_CREATE(remote_device_pool, struct ISCI_REMOTE_DEVICE *, SCI_MAX_REMOTE_DEVICES); 176 SCI_POOL_CREATE(request_pool, struct ISCI_REQUEST *, SCI_MAX_IO_REQUESTS); 177 SCI_POOL_CREATE(timer_pool, struct ISCI_TIMER *, SCI_MAX_TIMERS); 178 }; 179 180 struct ISCI_REQUEST 181 { 182 SCI_CONTROLLER_HANDLE_T controller_handle; 183 SCI_REMOTE_DEVICE_HANDLE_T remote_device_handle; 184 bus_dma_tag_t dma_tag; 185 bus_dmamap_t dma_map; 186 SCI_PHYSICAL_ADDRESS physical_address; 187 struct callout timer; 188 }; 189 190 struct ISCI_IO_REQUEST 191 { 192 struct ISCI_REQUEST parent; 193 SCI_IO_REQUEST_HANDLE_T sci_object; 194 union ccb *ccb; 195 uint32_t num_segments; 196 uint32_t current_sge_index; 197 bus_dma_segment_t *sge; 198 }; 199 200 struct ISCI_TASK_REQUEST 201 { 202 struct ISCI_REQUEST parent; 203 struct scsi_sense_data sense_data; 204 SCI_TASK_REQUEST_HANDLE_T sci_object; 205 union ccb *ccb; 206 207 }; 208 209 struct ISCI_PCI_BAR { 210 211 bus_space_tag_t bus_tag; 212 bus_space_handle_t bus_handle; 213 int resource_id; 214 struct resource *resource; 215 216 }; 217 218 /* 219 * One of these per allocated PCI device. 220 */ 221 struct isci_softc { 222 223 struct ISCI_PCI_BAR pci_bar[ISCI_NUM_PCI_BARS]; 224 struct ISCI_CONTROLLER controllers[SCI_MAX_CONTROLLERS]; 225 SCI_LIBRARY_HANDLE_T sci_library_handle; 226 void * sci_library_memory; 227 SCIC_CONTROLLER_HANDLER_METHODS_T handlers[4]; 228 struct ISCI_INTERRUPT_INFO interrupt_info[4]; 229 uint32_t controller_count; 230 uint32_t num_interrupts; 231 uint32_t coalesce_number; 232 uint32_t coalesce_timeout; 233 device_t device; 234 SCI_PCI_COMMON_HEADER_T pci_common_header; 235 BOOL oem_parameters_found; 236 struct intr_config_hook config_hook; 237 }; 238 239 int isci_allocate_resources(device_t device); 240 241 int isci_allocate_dma_buffer(device_t device, struct ISCI_MEMORY *memory); 242 243 void isci_remote_device_reset(struct ISCI_REMOTE_DEVICE *remote_device, 244 union ccb *ccb); 245 246 /** 247 * Returns the negotiated link rate (in KB/s) for the associated 248 * remote device. Used to fill out bitrate field for GET_TRANS_SETTINGS. 249 * Will match the negotiated link rate for the lowest numbered local phy 250 * in the port/domain containing this remote device. 251 */ 252 uint32_t isci_remote_device_get_bitrate( 253 struct ISCI_REMOTE_DEVICE *remote_device); 254 255 void isci_remote_device_freeze_lun_queue( 256 struct ISCI_REMOTE_DEVICE *remote_device, lun_id_t lun); 257 258 void isci_remote_device_release_lun_queue( 259 struct ISCI_REMOTE_DEVICE *remote_device, lun_id_t lun); 260 261 void isci_remote_device_release_device_queue( 262 struct ISCI_REMOTE_DEVICE * remote_device); 263 264 void isci_request_construct(struct ISCI_REQUEST *request, 265 SCI_CONTROLLER_HANDLE_T scif_controller_handle, 266 bus_dma_tag_t io_buffer_dma_tag, bus_addr_t physical_address); 267 268 #define isci_io_request_get_max_io_size() \ 269 ((SCI_MAX_SCATTER_GATHER_ELEMENTS - 1) * PAGE_SIZE) 270 271 #define isci_task_request_get_object_size() \ 272 (sizeof(struct ISCI_TASK_REQUEST) + scif_task_request_get_object_size()) 273 274 #define isci_io_request_get_object_size() \ 275 (sizeof(struct ISCI_IO_REQUEST) + scif_io_request_get_object_size()) 276 277 #define isci_request_get_object_size() \ 278 max( \ 279 isci_task_request_get_object_size(), \ 280 isci_io_request_get_object_size() \ 281 ) 282 283 284 void isci_io_request_execute_scsi_io(union ccb *ccb, 285 struct ISCI_CONTROLLER *controller); 286 287 #if __FreeBSD_version >= 900026 288 void isci_io_request_execute_smp_io( 289 union ccb *ccb, struct ISCI_CONTROLLER *controller); 290 #endif 291 292 void isci_io_request_timeout(void *); 293 294 void isci_get_oem_parameters(struct isci_softc *isci); 295 296 void isci_io_request_complete( 297 SCI_CONTROLLER_HANDLE_T scif_controller, 298 SCI_REMOTE_DEVICE_HANDLE_T remote_device, 299 struct ISCI_IO_REQUEST * isci_request, SCI_IO_STATUS completion_status); 300 301 void isci_task_request_complete( 302 SCI_CONTROLLER_HANDLE_T scif_controller, 303 SCI_REMOTE_DEVICE_HANDLE_T remote_device, 304 SCI_TASK_REQUEST_HANDLE_T io_request, SCI_TASK_STATUS completion_status); 305 306 void isci_sysctl_initialize(struct isci_softc *isci); 307 308 void isci_controller_construct(struct ISCI_CONTROLLER *controller, 309 struct isci_softc *isci); 310 311 SCI_STATUS isci_controller_initialize(struct ISCI_CONTROLLER *controller); 312 313 int isci_controller_allocate_memory(struct ISCI_CONTROLLER *controller); 314 315 void isci_controller_domain_discovery_complete( 316 struct ISCI_CONTROLLER *isci_controller, struct ISCI_DOMAIN *isci_domain); 317 318 int isci_controller_attach_to_cam(struct ISCI_CONTROLLER *controller); 319 320 void isci_controller_start(void *controller); 321 322 void isci_controller_release_queued_ccbs(struct ISCI_CONTROLLER *controller); 323 324 void isci_domain_construct(struct ISCI_DOMAIN *domain, uint32_t domain_index, 325 struct ISCI_CONTROLLER *controller); 326 327 void isci_interrupt_setup(struct isci_softc *isci); 328 void isci_interrupt_poll_handler(struct ISCI_CONTROLLER *controller); 329 330 void isci_log_message(uint32_t verbosity, char *log_message_prefix, 331 char *log_message, ...); 332 333 extern uint32_t g_isci_debug_level; 334 335 #endif /* #ifndef _ISCI_H */ 336