2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * AMCC'S 3ware driver for 9000 series storage controllers.
33 * Author: Vinod Kashyap
34 * Modifications by: Adam Radford
35 * Modifications by: Manjunath Ranganathaiah
40 * 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,
65 * Function name: tw_osli_cam_attach
66 * Description: Attaches the driver to CAM.
68 * Input: sc -- ptr to OSL internal ctlr context
70 * Return value: 0 -- success
74 tw_osli_cam_attach(struct twa_softc *sc)
76 struct cam_devq *devq;
78 tw_osli_dbg_dprintf(3, sc, "entered");
81 * Create the device queue for our SIM.
83 if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_IOS)) == NULL) {
84 tw_osli_printf(sc, "error = %d",
85 TW_CL_SEVERITY_ERROR_STRING,
86 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
88 "Failed to create SIM device queue",
94 * Create a SIM entry. Though we can support TW_OSLI_MAX_NUM_REQUESTS
95 * simultaneous requests, we claim to be able to handle only
96 * TW_OSLI_MAX_NUM_IOS (two less), so that we always have a request
97 * packet available to service ioctls and AENs.
99 tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
100 sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
101 device_get_unit(sc->bus_dev), sc->sim_lock,
102 TW_OSLI_MAX_NUM_IOS, 1, devq);
103 if (sc->sim == NULL) {
105 tw_osli_printf(sc, "error = %d",
106 TW_CL_SEVERITY_ERROR_STRING,
107 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
109 "Failed to create a SIM entry",
117 tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
118 mtx_lock(sc->sim_lock);
119 if (xpt_bus_register(sc->sim, sc->bus_dev, 0) != CAM_SUCCESS) {
120 cam_sim_free(sc->sim, TRUE);
121 sc->sim = NULL; /* so cam_detach will not try to free it */
122 tw_osli_printf(sc, "error = %d",
123 TW_CL_SEVERITY_ERROR_STRING,
124 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
126 "Failed to register the bus",
128 mtx_unlock(sc->sim_lock);
132 tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
133 if (xpt_create_path(&sc->path, NULL,
134 cam_sim_path(sc->sim),
136 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
137 xpt_bus_deregister(cam_sim_path (sc->sim));
138 /* Passing TRUE to cam_sim_free will free the devq as well. */
139 cam_sim_free(sc->sim, TRUE);
140 tw_osli_printf(sc, "error = %d",
141 TW_CL_SEVERITY_ERROR_STRING,
142 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
144 "Failed to create path",
146 mtx_unlock(sc->sim_lock);
149 mtx_unlock(sc->sim_lock);
151 tw_osli_dbg_dprintf(3, sc, "exiting");
158 * Function name: tw_osli_cam_detach
159 * Description: Detaches the driver from CAM.
161 * Input: sc -- ptr to OSL internal ctlr context
166 tw_osli_cam_detach(struct twa_softc *sc)
168 tw_osli_dbg_dprintf(3, sc, "entered");
170 mtx_lock(sc->sim_lock);
173 xpt_free_path(sc->path);
175 xpt_bus_deregister(cam_sim_path(sc->sim));
176 /* Passing TRUE to cam_sim_free will free the devq as well. */
177 cam_sim_free(sc->sim, TRUE);
179 /* It's ok have 1 hold count while destroying the mutex */
180 mtx_destroy(sc->sim_lock);
186 * Function name: tw_osli_execute_scsi
187 * Description: Build a fw cmd, based on a CAM style ccb, and
190 * Input: req -- ptr to OSL internal request context
191 * ccb -- ptr to CAM style ccb
193 * Return value: 0 -- success
197 tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
199 struct twa_softc *sc = req->ctlr;
200 struct tw_cl_req_packet *req_pkt;
201 struct tw_cl_scsi_req_packet *scsi_req;
202 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
203 struct ccb_scsiio *csio = &(ccb->csio);
206 tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
207 csio->cdb_io.cdb_bytes[0]);
209 if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
210 tw_osli_dbg_dprintf(3, sc, "Invalid target. PTL = %x %x %x",
211 ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
212 ccb_h->status |= CAM_TID_INVALID;
216 if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
217 tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %x",
218 ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
219 ccb_h->status |= CAM_LUN_INVALID;
224 if(ccb_h->flags & CAM_CDB_PHYS) {
225 tw_osli_printf(sc, "",
226 TW_CL_SEVERITY_ERROR_STRING,
227 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
229 "Physical CDB address!");
230 ccb_h->status = CAM_REQ_INVALID;
236 * We are going to work on this request. Mark it as enqueued (though
237 * we don't actually queue it...)
239 ccb_h->status |= CAM_SIM_QUEUED;
241 if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
242 if(ccb_h->flags & CAM_DIR_IN)
243 req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
245 req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
248 /* Build the CL understood request packet for SCSI cmds. */
249 req_pkt = &req->req_pkt;
251 req_pkt->tw_osl_callback = tw_osl_complete_io;
252 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
253 scsi_req->unit = ccb_h->target_id;
254 scsi_req->lun = ccb_h->target_lun;
255 scsi_req->sense_len = 0;
256 scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
257 scsi_req->scsi_status = 0;
258 if(ccb_h->flags & CAM_CDB_POINTER)
259 scsi_req->cdb = csio->cdb_io.cdb_ptr;
261 scsi_req->cdb = csio->cdb_io.cdb_bytes;
262 scsi_req->cdb_len = csio->cdb_len;
264 if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
265 tw_osli_printf(sc, "size = %d",
266 TW_CL_SEVERITY_ERROR_STRING,
267 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
271 ccb_h->status = CAM_REQ_TOO_BIG;
272 ccb_h->status &= ~CAM_SIM_QUEUED;
276 if ((ccb_h->flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
277 if ((req->length = csio->dxfer_len) != 0) {
278 req->data = csio->data_ptr;
279 scsi_req->sgl_entries = 1;
282 req->flags |= TW_OSLI_REQ_FLAGS_CCB;
283 req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
286 * twa_map_load_data_callback will fill in the SGL,
287 * and submit the I/O.
289 error = tw_osli_map_request(req);
290 if ((error) && (req->flags & TW_OSLI_REQ_FLAGS_FAILED)) {
292 ccb_h->status = CAM_REQ_CMP_ERR;
293 ccb_h->status &= ~CAM_SIM_QUEUED;
302 * Function name: twa_action
303 * Description: Driver entry point for CAM's use.
305 * Input: sim -- sim corresponding to the ctlr
306 * ccb -- ptr to CAM request
311 twa_action(struct cam_sim *sim, union ccb *ccb)
313 struct twa_softc *sc = (struct twa_softc *)cam_sim_softc(sim);
314 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
316 switch (ccb_h->func_code) {
317 case XPT_SCSI_IO: /* SCSI I/O */
319 struct tw_osli_req_context *req;
321 req = tw_osli_get_request(sc);
323 tw_osli_dbg_dprintf(2, sc, "Cannot get request pkt.");
325 * Freeze the simq to maintain ccb ordering. The next
326 * ccb that gets completed will unfreeze the simq.
328 ccb_h->status &= ~CAM_SIM_QUEUED;
329 ccb_h->status |= CAM_REQUEUE_REQ;
334 if ((tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
335 ccb_h->status &= ~CAM_SIM_QUEUED;
336 ccb_h->status |= CAM_REQUEUE_REQ;
338 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
342 req->req_handle.osl_req_ctxt = req;
343 req->req_handle.is_io = TW_CL_TRUE;
345 if (tw_osli_execute_scsi(req, ccb))
346 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
351 tw_osli_dbg_dprintf(2, sc, "Abort request.");
352 ccb_h->status = CAM_UA_ABORT;
357 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_FALSE,
358 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
359 0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
360 "Received Reset Bus request from CAM",
363 tw_cl_set_reset_needed(&(sc->ctlr_handle));
364 ccb_h->status = CAM_REQ_CMP;
368 case XPT_SET_TRAN_SETTINGS:
369 tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
372 * This command is not supported, since it's very specific
373 * to SCSI, and we are doing ATA.
375 ccb_h->status = CAM_FUNC_NOTAVAIL;
379 case XPT_GET_TRAN_SETTINGS:
381 struct ccb_trans_settings *cts = &ccb->cts;
382 struct ccb_trans_settings_scsi *scsi =
383 &cts->proto_specific.scsi;
384 struct ccb_trans_settings_spi *spi =
385 &cts->xport_specific.spi;
387 cts->protocol = PROTO_SCSI;
388 cts->protocol_version = SCSI_REV_2;
389 cts->transport = XPORT_SPI;
390 cts->transport_version = 2;
392 spi->valid = CTS_SPI_VALID_DISC;
393 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
394 scsi->valid = CTS_SCSI_VALID_TQ;
395 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
396 tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
397 ccb_h->status = CAM_REQ_CMP;
402 case XPT_CALC_GEOMETRY:
403 tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
404 cam_calc_geometry(&ccb->ccg, 1/* extended */);
408 case XPT_PATH_INQ: /* Path inquiry -- get twa properties */
410 struct ccb_pathinq *path_inq = &ccb->cpi;
412 tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
414 path_inq->version_num = 1;
415 path_inq->hba_inquiry = 0;
416 path_inq->target_sprt = 0;
417 path_inq->hba_misc = 0;
418 path_inq->hba_eng_cnt = 0;
419 path_inq->max_target = TW_CL_MAX_NUM_UNITS;
420 path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
421 path_inq->unit_number = cam_sim_unit(sim);
422 path_inq->bus_id = cam_sim_bus(sim);
423 path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
424 path_inq->base_transfer_speed = 100000;
425 strncpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
426 strncpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
427 strncpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
428 path_inq->transport = XPORT_SPI;
429 path_inq->transport_version = 2;
430 path_inq->protocol = PROTO_SCSI;
431 path_inq->protocol_version = SCSI_REV_2;
432 path_inq->maxio = TW_CL_MAX_IO_SIZE;
433 ccb_h->status = CAM_REQ_CMP;
439 tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
440 ccb_h->status = CAM_REQ_INVALID;
449 * Function name: twa_poll
450 * Description: Driver entry point called when interrupts are not
453 * Input: sim -- sim corresponding to the controller
458 twa_poll(struct cam_sim *sim)
460 struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
462 tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
463 tw_cl_interrupt(&(sc->ctlr_handle));
464 tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
470 * Function name: tw_osli_request_bus_scan
471 * Description: Requests CAM for a scan of the bus.
473 * Input: sc -- ptr to per ctlr structure
475 * Return value: 0 -- success
479 tw_osli_request_bus_scan(struct twa_softc *sc)
483 tw_osli_dbg_dprintf(3, sc, "entering");
485 /* If we get here before sc->sim is initialized, return an error. */
488 if ((ccb = xpt_alloc_ccb()) == NULL)
490 mtx_lock(sc->sim_lock);
491 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sc->sim),
492 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
494 mtx_unlock(sc->sim_lock);
499 mtx_unlock(sc->sim_lock);
506 * Function name: tw_osli_disallow_new_requests
507 * Description: Calls the appropriate CAM function, so as to freeze
508 * the flow of new requests from CAM to this controller.
510 * Input: sc -- ptr to OSL internal ctlr context
511 * req_handle -- ptr to request handle sent by OSL.
516 tw_osli_disallow_new_requests(struct twa_softc *sc,
517 struct tw_cl_req_handle *req_handle)
519 /* Only freeze/release the simq for IOs */
520 if (req_handle->is_io) {
521 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
522 union ccb *ccb = (union ccb *)(req->orig_req);
524 xpt_freeze_simq(sc->sim, 1);
525 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
532 * Function name: tw_osl_timeout
533 * Description: Call to timeout().
535 * Input: req_handle -- ptr to request handle sent by OSL.
540 tw_osl_timeout(struct tw_cl_req_handle *req_handle)
542 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
543 union ccb *ccb = (union ccb *)(req->orig_req);
544 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
546 req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
552 * Function name: tw_osl_untimeout
553 * Description: Inverse of call to timeout().
555 * Input: req_handle -- ptr to request handle sent by OSL.
560 tw_osl_untimeout(struct tw_cl_req_handle *req_handle)
562 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
570 * Function name: tw_osl_scan_bus
571 * Description: CL calls this function to request for a bus scan.
573 * Input: ctlr_handle -- ptr to controller handle
578 tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
580 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
583 if ((error = tw_osli_request_bus_scan(sc)))
584 tw_osli_printf(sc, "error = %d",
585 TW_CL_SEVERITY_ERROR_STRING,
586 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
588 "Bus scan request to CAM failed",
595 * Function name: tw_osl_complete_io
596 * Description: Called to complete CAM scsi requests.
598 * Input: req_handle -- ptr to request handle
603 tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
605 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
606 struct tw_cl_req_packet *req_pkt =
607 (struct tw_cl_req_packet *)(&req->req_pkt);
608 struct tw_cl_scsi_req_packet *scsi_req;
609 struct twa_softc *sc = req->ctlr;
610 union ccb *ccb = (union ccb *)(req->orig_req);
612 tw_osli_dbg_dprintf(10, sc, "entering");
614 if (req->state != TW_OSLI_REQ_STATE_BUSY)
615 tw_osli_printf(sc, "request = %p, status = %d",
616 TW_CL_SEVERITY_ERROR_STRING,
617 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
619 "Unposted command completed!!",
623 * Remove request from the busy queue. Just mark it complete.
624 * There's no need to move it into the complete queue as we are
625 * going to be done with it right now.
627 req->state = TW_OSLI_REQ_STATE_COMPLETE;
628 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
630 tw_osli_unmap_request(req);
633 if (req->error_code) {
634 /* This request never got submitted to the firmware. */
635 if (req->error_code == EBUSY) {
637 * Cmd queue is full, or the Common Layer is out of
638 * resources. The simq will already have been frozen.
639 * When this ccb gets completed will unfreeze the simq.
641 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
643 else if (req->error_code == EFBIG)
644 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
646 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
648 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
649 if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
650 ccb->ccb_h.status = CAM_REQ_CMP;
652 if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
653 ccb->ccb_h.status |= CAM_SEL_TIMEOUT;
654 else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
655 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
656 else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
657 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
658 else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
659 ccb->ccb_h.status |= (CAM_REQUEUE_REQ | CAM_SCSI_BUS_RESET);
661 * If none of the above errors occurred, simply
662 * mark completion error.
664 if (ccb->ccb_h.status == 0)
665 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
667 if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
668 ccb->csio.sense_len = scsi_req->sense_len;
669 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
673 ccb->csio.scsi_status = scsi_req->scsi_status;
676 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
677 mtx_lock(sc->sim_lock);
679 mtx_unlock(sc->sim_lock);
680 if (! req->error_code)
681 /* twa_action will free the request otherwise */
682 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);