2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
5 * Copyright (c) 2004-05 Vinod Kashyap.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * AMCC'S 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 * Modifications by: Manjunath Ranganathaiah
41 * FreeBSD CAM related functions.
44 #include <dev/twa/tw_osl_includes.h>
47 #include <cam/cam_ccb.h>
48 #include <cam/cam_sim.h>
49 #include <cam/cam_xpt_sim.h>
50 #include <cam/cam_debug.h>
51 #include <cam/cam_periph.h>
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_message.h>
56 static TW_VOID twa_action(struct cam_sim *sim, union ccb *ccb);
57 static TW_VOID twa_poll(struct cam_sim *sim);
59 static TW_INT32 tw_osli_execute_scsi(struct tw_osli_req_context *req,
63 * Function name: tw_osli_cam_attach
64 * Description: Attaches the driver to CAM.
66 * Input: sc -- ptr to OSL internal ctlr context
68 * Return value: 0 -- success
72 tw_osli_cam_attach(struct twa_softc *sc)
74 struct cam_devq *devq;
76 tw_osli_dbg_dprintf(3, sc, "entered");
79 * Create the device queue for our SIM.
81 if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_IOS)) == NULL) {
82 tw_osli_printf(sc, "error = %d",
83 TW_CL_SEVERITY_ERROR_STRING,
84 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
86 "Failed to create SIM device queue",
92 * Create a SIM entry. Though we can support TW_OSLI_MAX_NUM_REQUESTS
93 * simultaneous requests, we claim to be able to handle only
94 * TW_OSLI_MAX_NUM_IOS (two less), so that we always have a request
95 * packet available to service ioctls and AENs.
97 tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
98 sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
99 device_get_unit(sc->bus_dev), sc->sim_lock,
100 TW_OSLI_MAX_NUM_IOS, 1, devq);
101 if (sc->sim == NULL) {
103 tw_osli_printf(sc, "error = %d",
104 TW_CL_SEVERITY_ERROR_STRING,
105 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
107 "Failed to create a SIM entry",
115 tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
116 mtx_lock(sc->sim_lock);
117 if (xpt_bus_register(sc->sim, sc->bus_dev, 0) != CAM_SUCCESS) {
118 cam_sim_free(sc->sim, TRUE);
119 sc->sim = NULL; /* so cam_detach will not try to free it */
120 tw_osli_printf(sc, "error = %d",
121 TW_CL_SEVERITY_ERROR_STRING,
122 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
124 "Failed to register the bus",
126 mtx_unlock(sc->sim_lock);
130 tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
131 if (xpt_create_path(&sc->path, NULL,
132 cam_sim_path(sc->sim),
134 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
135 xpt_bus_deregister(cam_sim_path (sc->sim));
136 /* Passing TRUE to cam_sim_free will free the devq as well. */
137 cam_sim_free(sc->sim, TRUE);
138 tw_osli_printf(sc, "error = %d",
139 TW_CL_SEVERITY_ERROR_STRING,
140 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
142 "Failed to create path",
144 mtx_unlock(sc->sim_lock);
147 mtx_unlock(sc->sim_lock);
149 tw_osli_dbg_dprintf(3, sc, "exiting");
154 * Function name: tw_osli_cam_detach
155 * Description: Detaches the driver from CAM.
157 * Input: sc -- ptr to OSL internal ctlr context
162 tw_osli_cam_detach(struct twa_softc *sc)
164 tw_osli_dbg_dprintf(3, sc, "entered");
166 mtx_lock(sc->sim_lock);
169 xpt_free_path(sc->path);
171 xpt_bus_deregister(cam_sim_path(sc->sim));
172 /* Passing TRUE to cam_sim_free will free the devq as well. */
173 cam_sim_free(sc->sim, TRUE);
175 /* It's ok have 1 hold count while destroying the mutex */
176 mtx_destroy(sc->sim_lock);
180 * Function name: tw_osli_execute_scsi
181 * Description: Build a fw cmd, based on a CAM style ccb, and
184 * Input: req -- ptr to OSL internal request context
185 * ccb -- ptr to CAM style ccb
187 * Return value: 0 -- success
191 tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
193 struct twa_softc *sc = req->ctlr;
194 struct tw_cl_req_packet *req_pkt;
195 struct tw_cl_scsi_req_packet *scsi_req;
196 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
197 struct ccb_scsiio *csio = &(ccb->csio);
200 tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
201 csio->cdb_io.cdb_bytes[0]);
203 if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
204 tw_osli_dbg_dprintf(3, sc, "Invalid target. PTL = %x %x %jx",
205 ccb_h->path_id, ccb_h->target_id,
206 (uintmax_t)ccb_h->target_lun);
207 ccb_h->status |= CAM_TID_INVALID;
211 if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
212 tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %jx",
213 ccb_h->path_id, ccb_h->target_id,
214 (uintmax_t)ccb_h->target_lun);
215 ccb_h->status |= CAM_LUN_INVALID;
220 if(ccb_h->flags & CAM_CDB_PHYS) {
221 tw_osli_printf(sc, "",
222 TW_CL_SEVERITY_ERROR_STRING,
223 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
225 "Physical CDB address!");
226 ccb_h->status = CAM_REQ_INVALID;
232 * We are going to work on this request. Mark it as enqueued (though
233 * we don't actually queue it...)
235 ccb_h->status |= CAM_SIM_QUEUED;
237 if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
238 if(ccb_h->flags & CAM_DIR_IN)
239 req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
241 req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
244 /* Build the CL understood request packet for SCSI cmds. */
245 req_pkt = &req->req_pkt;
247 req_pkt->tw_osl_callback = tw_osl_complete_io;
248 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
249 scsi_req->unit = ccb_h->target_id;
250 scsi_req->lun = ccb_h->target_lun;
251 scsi_req->sense_len = 0;
252 scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
253 scsi_req->scsi_status = 0;
254 if(ccb_h->flags & CAM_CDB_POINTER)
255 scsi_req->cdb = csio->cdb_io.cdb_ptr;
257 scsi_req->cdb = csio->cdb_io.cdb_bytes;
258 scsi_req->cdb_len = csio->cdb_len;
260 if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
261 tw_osli_printf(sc, "size = %d",
262 TW_CL_SEVERITY_ERROR_STRING,
263 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
267 ccb_h->status = CAM_REQ_TOO_BIG;
268 ccb_h->status &= ~CAM_SIM_QUEUED;
272 if ((ccb_h->flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
273 if ((req->length = csio->dxfer_len) != 0) {
274 req->data = csio->data_ptr;
275 scsi_req->sgl_entries = 1;
278 req->flags |= TW_OSLI_REQ_FLAGS_CCB;
279 req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
282 * twa_map_load_data_callback will fill in the SGL,
283 * and submit the I/O.
285 error = tw_osli_map_request(req);
286 if ((error) && (req->flags & TW_OSLI_REQ_FLAGS_FAILED)) {
288 ccb_h->status = CAM_REQ_CMP_ERR;
289 ccb_h->status &= ~CAM_SIM_QUEUED;
296 * Function name: twa_action
297 * Description: Driver entry point for CAM's use.
299 * Input: sim -- sim corresponding to the ctlr
300 * ccb -- ptr to CAM request
305 twa_action(struct cam_sim *sim, union ccb *ccb)
307 struct twa_softc *sc = (struct twa_softc *)cam_sim_softc(sim);
308 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
310 switch (ccb_h->func_code) {
311 case XPT_SCSI_IO: /* SCSI I/O */
313 struct tw_osli_req_context *req;
315 req = tw_osli_get_request(sc);
317 tw_osli_dbg_dprintf(2, sc, "Cannot get request pkt.");
319 * Freeze the simq to maintain ccb ordering. The next
320 * ccb that gets completed will unfreeze the simq.
322 ccb_h->status &= ~CAM_SIM_QUEUED;
323 ccb_h->status |= CAM_REQUEUE_REQ;
328 if ((tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
329 ccb_h->status &= ~CAM_SIM_QUEUED;
330 ccb_h->status |= CAM_REQUEUE_REQ;
332 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
336 req->req_handle.osl_req_ctxt = req;
337 req->req_handle.is_io = TW_CL_TRUE;
339 if (tw_osli_execute_scsi(req, ccb))
340 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
345 tw_osli_dbg_dprintf(2, sc, "Abort request.");
346 ccb_h->status = CAM_UA_ABORT;
351 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_FALSE,
352 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
353 0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
354 "Received Reset Bus request from CAM",
357 tw_cl_set_reset_needed(&(sc->ctlr_handle));
358 ccb_h->status = CAM_REQ_CMP;
362 case XPT_SET_TRAN_SETTINGS:
363 tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
366 * This command is not supported, since it's very specific
367 * to SCSI, and we are doing ATA.
369 ccb_h->status = CAM_FUNC_NOTAVAIL;
373 case XPT_GET_TRAN_SETTINGS:
375 struct ccb_trans_settings *cts = &ccb->cts;
376 struct ccb_trans_settings_scsi *scsi =
377 &cts->proto_specific.scsi;
378 struct ccb_trans_settings_spi *spi =
379 &cts->xport_specific.spi;
381 cts->protocol = PROTO_SCSI;
382 cts->protocol_version = SCSI_REV_2;
383 cts->transport = XPORT_SPI;
384 cts->transport_version = 2;
386 spi->valid = CTS_SPI_VALID_DISC;
387 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
388 scsi->valid = CTS_SCSI_VALID_TQ;
389 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
390 tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
391 ccb_h->status = CAM_REQ_CMP;
396 case XPT_CALC_GEOMETRY:
397 tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
398 cam_calc_geometry(&ccb->ccg, 1/* extended */);
402 case XPT_PATH_INQ: /* Path inquiry -- get twa properties */
404 struct ccb_pathinq *path_inq = &ccb->cpi;
406 tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
408 path_inq->version_num = 1;
409 path_inq->hba_inquiry = 0;
410 path_inq->target_sprt = 0;
411 path_inq->hba_misc = 0;
412 path_inq->hba_eng_cnt = 0;
413 path_inq->max_target = TW_CL_MAX_NUM_UNITS;
414 path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
415 path_inq->unit_number = cam_sim_unit(sim);
416 path_inq->bus_id = cam_sim_bus(sim);
417 path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
418 path_inq->base_transfer_speed = 100000;
419 strlcpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
420 strlcpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
421 strlcpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
422 path_inq->transport = XPORT_SPI;
423 path_inq->transport_version = 2;
424 path_inq->protocol = PROTO_SCSI;
425 path_inq->protocol_version = SCSI_REV_2;
426 path_inq->maxio = TW_CL_MAX_IO_SIZE;
427 ccb_h->status = CAM_REQ_CMP;
433 tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
434 ccb_h->status = CAM_REQ_INVALID;
441 * Function name: twa_poll
442 * Description: Driver entry point called when interrupts are not
445 * Input: sim -- sim corresponding to the controller
450 twa_poll(struct cam_sim *sim)
452 struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
454 tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
455 tw_cl_interrupt(&(sc->ctlr_handle));
456 tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
460 * Function name: tw_osli_request_bus_scan
461 * Description: Requests CAM for a scan of the bus.
463 * Input: sc -- ptr to per ctlr structure
465 * Return value: 0 -- success
469 tw_osli_request_bus_scan(struct twa_softc *sc)
473 tw_osli_dbg_dprintf(3, sc, "entering");
475 /* If we get here before sc->sim is initialized, return an error. */
478 if ((ccb = xpt_alloc_ccb()) == NULL)
480 mtx_lock(sc->sim_lock);
481 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sc->sim),
482 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
484 mtx_unlock(sc->sim_lock);
489 mtx_unlock(sc->sim_lock);
494 * Function name: tw_osli_disallow_new_requests
495 * Description: Calls the appropriate CAM function, so as to freeze
496 * the flow of new requests from CAM to this controller.
498 * Input: sc -- ptr to OSL internal ctlr context
499 * req_handle -- ptr to request handle sent by OSL.
504 tw_osli_disallow_new_requests(struct twa_softc *sc,
505 struct tw_cl_req_handle *req_handle)
507 /* Only freeze/release the simq for IOs */
508 if (req_handle->is_io) {
509 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
510 union ccb *ccb = (union ccb *)(req->orig_req);
512 xpt_freeze_simq(sc->sim, 1);
513 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
518 * Function name: tw_osl_timeout
519 * Description: Call to timeout().
521 * Input: req_handle -- ptr to request handle sent by OSL.
526 tw_osl_timeout(struct tw_cl_req_handle *req_handle)
528 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
529 union ccb *ccb = (union ccb *)(req->orig_req);
530 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
532 req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
536 * Function name: tw_osl_untimeout
537 * Description: Inverse of call to timeout().
539 * Input: req_handle -- ptr to request handle sent by OSL.
544 tw_osl_untimeout(struct tw_cl_req_handle *req_handle)
546 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
552 * Function name: tw_osl_scan_bus
553 * Description: CL calls this function to request for a bus scan.
555 * Input: ctlr_handle -- ptr to controller handle
560 tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
562 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
565 if ((error = tw_osli_request_bus_scan(sc)))
566 tw_osli_printf(sc, "error = %d",
567 TW_CL_SEVERITY_ERROR_STRING,
568 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
570 "Bus scan request to CAM failed",
575 * Function name: tw_osl_complete_io
576 * Description: Called to complete CAM scsi requests.
578 * Input: req_handle -- ptr to request handle
583 tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
585 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
586 struct tw_cl_req_packet *req_pkt =
587 (struct tw_cl_req_packet *)(&req->req_pkt);
588 struct tw_cl_scsi_req_packet *scsi_req;
589 struct twa_softc *sc = req->ctlr;
590 union ccb *ccb = (union ccb *)(req->orig_req);
592 tw_osli_dbg_dprintf(10, sc, "entering");
594 if (req->state != TW_OSLI_REQ_STATE_BUSY)
595 tw_osli_printf(sc, "request = %p, status = %d",
596 TW_CL_SEVERITY_ERROR_STRING,
597 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
599 "Unposted command completed!!",
603 * Remove request from the busy queue. Just mark it complete.
604 * There's no need to move it into the complete queue as we are
605 * going to be done with it right now.
607 req->state = TW_OSLI_REQ_STATE_COMPLETE;
608 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
610 tw_osli_unmap_request(req);
613 if (req->error_code) {
614 /* This request never got submitted to the firmware. */
615 if (req->error_code == EBUSY) {
617 * Cmd queue is full, or the Common Layer is out of
618 * resources. The simq will already have been frozen.
619 * When this ccb gets completed will unfreeze the simq.
621 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
623 else if (req->error_code == EFBIG)
624 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
626 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
628 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
629 if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
630 ccb->ccb_h.status = CAM_REQ_CMP;
632 if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
633 ccb->ccb_h.status |= CAM_SEL_TIMEOUT;
634 else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
635 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
636 else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
637 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
638 else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
639 ccb->ccb_h.status |= (CAM_REQUEUE_REQ | CAM_SCSI_BUS_RESET);
641 * If none of the above errors occurred, simply
642 * mark completion error.
644 if (ccb->ccb_h.status == 0)
645 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
647 if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
648 ccb->csio.sense_len = scsi_req->sense_len;
649 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
653 ccb->csio.scsi_status = scsi_req->scsi_status;
656 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
657 mtx_lock(sc->sim_lock);
659 mtx_unlock(sc->sim_lock);
660 if (! req->error_code)
661 /* twa_action will free the request otherwise */
662 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);