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
42 * FreeBSD CAM related functions.
46 #include <dev/twa/tw_osl_includes.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_sim.h>
51 #include <cam/cam_xpt_sim.h>
52 #include <cam/cam_debug.h>
53 #include <cam/cam_periph.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_message.h>
58 static TW_VOID twa_action(struct cam_sim *sim, union ccb *ccb);
59 static TW_VOID twa_poll(struct cam_sim *sim);
61 static TW_INT32 tw_osli_execute_scsi(struct tw_osli_req_context *req,
67 * Function name: tw_osli_cam_attach
68 * Description: Attaches the driver to CAM.
70 * Input: sc -- ptr to OSL internal ctlr context
72 * Return value: 0 -- success
76 tw_osli_cam_attach(struct twa_softc *sc)
78 struct cam_devq *devq;
80 tw_osli_dbg_dprintf(3, sc, "entered");
83 * Create the device queue for our SIM.
85 if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_IOS)) == NULL) {
86 tw_osli_printf(sc, "error = %d",
87 TW_CL_SEVERITY_ERROR_STRING,
88 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
90 "Failed to create SIM device queue",
96 * Create a SIM entry. Though we can support TW_OSLI_MAX_NUM_REQUESTS
97 * simultaneous requests, we claim to be able to handle only
98 * TW_OSLI_MAX_NUM_IOS (two less), so that we always have a request
99 * packet available to service ioctls and AENs.
101 tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
102 sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
103 device_get_unit(sc->bus_dev), sc->sim_lock,
104 TW_OSLI_MAX_NUM_IOS, 1, devq);
105 if (sc->sim == NULL) {
107 tw_osli_printf(sc, "error = %d",
108 TW_CL_SEVERITY_ERROR_STRING,
109 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
111 "Failed to create a SIM entry",
119 tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
120 mtx_lock(sc->sim_lock);
121 if (xpt_bus_register(sc->sim, sc->bus_dev, 0) != CAM_SUCCESS) {
122 cam_sim_free(sc->sim, TRUE);
123 sc->sim = NULL; /* so cam_detach will not try to free it */
124 tw_osli_printf(sc, "error = %d",
125 TW_CL_SEVERITY_ERROR_STRING,
126 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
128 "Failed to register the bus",
130 mtx_unlock(sc->sim_lock);
134 tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
135 if (xpt_create_path(&sc->path, NULL,
136 cam_sim_path(sc->sim),
138 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
139 xpt_bus_deregister(cam_sim_path (sc->sim));
140 /* Passing TRUE to cam_sim_free will free the devq as well. */
141 cam_sim_free(sc->sim, TRUE);
142 tw_osli_printf(sc, "error = %d",
143 TW_CL_SEVERITY_ERROR_STRING,
144 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
146 "Failed to create path",
148 mtx_unlock(sc->sim_lock);
151 mtx_unlock(sc->sim_lock);
153 tw_osli_dbg_dprintf(3, sc, "exiting");
160 * Function name: tw_osli_cam_detach
161 * Description: Detaches the driver from CAM.
163 * Input: sc -- ptr to OSL internal ctlr context
168 tw_osli_cam_detach(struct twa_softc *sc)
170 tw_osli_dbg_dprintf(3, sc, "entered");
172 mtx_lock(sc->sim_lock);
175 xpt_free_path(sc->path);
177 xpt_bus_deregister(cam_sim_path(sc->sim));
178 /* Passing TRUE to cam_sim_free will free the devq as well. */
179 cam_sim_free(sc->sim, TRUE);
181 /* It's ok have 1 hold count while destroying the mutex */
182 mtx_destroy(sc->sim_lock);
188 * Function name: tw_osli_execute_scsi
189 * Description: Build a fw cmd, based on a CAM style ccb, and
192 * Input: req -- ptr to OSL internal request context
193 * ccb -- ptr to CAM style ccb
195 * Return value: 0 -- success
199 tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
201 struct twa_softc *sc = req->ctlr;
202 struct tw_cl_req_packet *req_pkt;
203 struct tw_cl_scsi_req_packet *scsi_req;
204 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
205 struct ccb_scsiio *csio = &(ccb->csio);
208 tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
209 csio->cdb_io.cdb_bytes[0]);
211 if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
212 tw_osli_dbg_dprintf(3, sc, "Invalid target. 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_TID_INVALID;
219 if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
220 tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %jx",
221 ccb_h->path_id, ccb_h->target_id,
222 (uintmax_t)ccb_h->target_lun);
223 ccb_h->status |= CAM_LUN_INVALID;
228 if(ccb_h->flags & CAM_CDB_PHYS) {
229 tw_osli_printf(sc, "",
230 TW_CL_SEVERITY_ERROR_STRING,
231 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
233 "Physical CDB address!");
234 ccb_h->status = CAM_REQ_INVALID;
240 * We are going to work on this request. Mark it as enqueued (though
241 * we don't actually queue it...)
243 ccb_h->status |= CAM_SIM_QUEUED;
245 if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
246 if(ccb_h->flags & CAM_DIR_IN)
247 req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
249 req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
252 /* Build the CL understood request packet for SCSI cmds. */
253 req_pkt = &req->req_pkt;
255 req_pkt->tw_osl_callback = tw_osl_complete_io;
256 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
257 scsi_req->unit = ccb_h->target_id;
258 scsi_req->lun = ccb_h->target_lun;
259 scsi_req->sense_len = 0;
260 scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
261 scsi_req->scsi_status = 0;
262 if(ccb_h->flags & CAM_CDB_POINTER)
263 scsi_req->cdb = csio->cdb_io.cdb_ptr;
265 scsi_req->cdb = csio->cdb_io.cdb_bytes;
266 scsi_req->cdb_len = csio->cdb_len;
268 if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
269 tw_osli_printf(sc, "size = %d",
270 TW_CL_SEVERITY_ERROR_STRING,
271 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
275 ccb_h->status = CAM_REQ_TOO_BIG;
276 ccb_h->status &= ~CAM_SIM_QUEUED;
280 if ((ccb_h->flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
281 if ((req->length = csio->dxfer_len) != 0) {
282 req->data = csio->data_ptr;
283 scsi_req->sgl_entries = 1;
286 req->flags |= TW_OSLI_REQ_FLAGS_CCB;
287 req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
290 * twa_map_load_data_callback will fill in the SGL,
291 * and submit the I/O.
293 error = tw_osli_map_request(req);
294 if ((error) && (req->flags & TW_OSLI_REQ_FLAGS_FAILED)) {
296 ccb_h->status = CAM_REQ_CMP_ERR;
297 ccb_h->status &= ~CAM_SIM_QUEUED;
306 * Function name: twa_action
307 * Description: Driver entry point for CAM's use.
309 * Input: sim -- sim corresponding to the ctlr
310 * ccb -- ptr to CAM request
315 twa_action(struct cam_sim *sim, union ccb *ccb)
317 struct twa_softc *sc = (struct twa_softc *)cam_sim_softc(sim);
318 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
320 switch (ccb_h->func_code) {
321 case XPT_SCSI_IO: /* SCSI I/O */
323 struct tw_osli_req_context *req;
325 req = tw_osli_get_request(sc);
327 tw_osli_dbg_dprintf(2, sc, "Cannot get request pkt.");
329 * Freeze the simq to maintain ccb ordering. The next
330 * ccb that gets completed will unfreeze the simq.
332 ccb_h->status &= ~CAM_SIM_QUEUED;
333 ccb_h->status |= CAM_REQUEUE_REQ;
338 if ((tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
339 ccb_h->status &= ~CAM_SIM_QUEUED;
340 ccb_h->status |= CAM_REQUEUE_REQ;
342 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
346 req->req_handle.osl_req_ctxt = req;
347 req->req_handle.is_io = TW_CL_TRUE;
349 if (tw_osli_execute_scsi(req, ccb))
350 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
355 tw_osli_dbg_dprintf(2, sc, "Abort request.");
356 ccb_h->status = CAM_UA_ABORT;
361 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_FALSE,
362 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
363 0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
364 "Received Reset Bus request from CAM",
367 tw_cl_set_reset_needed(&(sc->ctlr_handle));
368 ccb_h->status = CAM_REQ_CMP;
372 case XPT_SET_TRAN_SETTINGS:
373 tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
376 * This command is not supported, since it's very specific
377 * to SCSI, and we are doing ATA.
379 ccb_h->status = CAM_FUNC_NOTAVAIL;
383 case XPT_GET_TRAN_SETTINGS:
385 struct ccb_trans_settings *cts = &ccb->cts;
386 struct ccb_trans_settings_scsi *scsi =
387 &cts->proto_specific.scsi;
388 struct ccb_trans_settings_spi *spi =
389 &cts->xport_specific.spi;
391 cts->protocol = PROTO_SCSI;
392 cts->protocol_version = SCSI_REV_2;
393 cts->transport = XPORT_SPI;
394 cts->transport_version = 2;
396 spi->valid = CTS_SPI_VALID_DISC;
397 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
398 scsi->valid = CTS_SCSI_VALID_TQ;
399 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
400 tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
401 ccb_h->status = CAM_REQ_CMP;
406 case XPT_CALC_GEOMETRY:
407 tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
408 cam_calc_geometry(&ccb->ccg, 1/* extended */);
412 case XPT_PATH_INQ: /* Path inquiry -- get twa properties */
414 struct ccb_pathinq *path_inq = &ccb->cpi;
416 tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
418 path_inq->version_num = 1;
419 path_inq->hba_inquiry = 0;
420 path_inq->target_sprt = 0;
421 path_inq->hba_misc = 0;
422 path_inq->hba_eng_cnt = 0;
423 path_inq->max_target = TW_CL_MAX_NUM_UNITS;
424 path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
425 path_inq->unit_number = cam_sim_unit(sim);
426 path_inq->bus_id = cam_sim_bus(sim);
427 path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
428 path_inq->base_transfer_speed = 100000;
429 strlcpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
430 strlcpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
431 strlcpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
432 path_inq->transport = XPORT_SPI;
433 path_inq->transport_version = 2;
434 path_inq->protocol = PROTO_SCSI;
435 path_inq->protocol_version = SCSI_REV_2;
436 path_inq->maxio = TW_CL_MAX_IO_SIZE;
437 ccb_h->status = CAM_REQ_CMP;
443 tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
444 ccb_h->status = CAM_REQ_INVALID;
453 * Function name: twa_poll
454 * Description: Driver entry point called when interrupts are not
457 * Input: sim -- sim corresponding to the controller
462 twa_poll(struct cam_sim *sim)
464 struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
466 tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
467 tw_cl_interrupt(&(sc->ctlr_handle));
468 tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
474 * Function name: tw_osli_request_bus_scan
475 * Description: Requests CAM for a scan of the bus.
477 * Input: sc -- ptr to per ctlr structure
479 * Return value: 0 -- success
483 tw_osli_request_bus_scan(struct twa_softc *sc)
487 tw_osli_dbg_dprintf(3, sc, "entering");
489 /* If we get here before sc->sim is initialized, return an error. */
492 if ((ccb = xpt_alloc_ccb()) == NULL)
494 mtx_lock(sc->sim_lock);
495 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sc->sim),
496 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
498 mtx_unlock(sc->sim_lock);
503 mtx_unlock(sc->sim_lock);
510 * Function name: tw_osli_disallow_new_requests
511 * Description: Calls the appropriate CAM function, so as to freeze
512 * the flow of new requests from CAM to this controller.
514 * Input: sc -- ptr to OSL internal ctlr context
515 * req_handle -- ptr to request handle sent by OSL.
520 tw_osli_disallow_new_requests(struct twa_softc *sc,
521 struct tw_cl_req_handle *req_handle)
523 /* Only freeze/release the simq for IOs */
524 if (req_handle->is_io) {
525 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
526 union ccb *ccb = (union ccb *)(req->orig_req);
528 xpt_freeze_simq(sc->sim, 1);
529 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
536 * Function name: tw_osl_timeout
537 * Description: Call to timeout().
539 * Input: req_handle -- ptr to request handle sent by OSL.
544 tw_osl_timeout(struct tw_cl_req_handle *req_handle)
546 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
547 union ccb *ccb = (union ccb *)(req->orig_req);
548 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
550 req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
556 * Function name: tw_osl_untimeout
557 * Description: Inverse of call to timeout().
559 * Input: req_handle -- ptr to request handle sent by OSL.
564 tw_osl_untimeout(struct tw_cl_req_handle *req_handle)
566 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
574 * Function name: tw_osl_scan_bus
575 * Description: CL calls this function to request for a bus scan.
577 * Input: ctlr_handle -- ptr to controller handle
582 tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
584 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
587 if ((error = tw_osli_request_bus_scan(sc)))
588 tw_osli_printf(sc, "error = %d",
589 TW_CL_SEVERITY_ERROR_STRING,
590 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
592 "Bus scan request to CAM failed",
599 * Function name: tw_osl_complete_io
600 * Description: Called to complete CAM scsi requests.
602 * Input: req_handle -- ptr to request handle
607 tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
609 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
610 struct tw_cl_req_packet *req_pkt =
611 (struct tw_cl_req_packet *)(&req->req_pkt);
612 struct tw_cl_scsi_req_packet *scsi_req;
613 struct twa_softc *sc = req->ctlr;
614 union ccb *ccb = (union ccb *)(req->orig_req);
616 tw_osli_dbg_dprintf(10, sc, "entering");
618 if (req->state != TW_OSLI_REQ_STATE_BUSY)
619 tw_osli_printf(sc, "request = %p, status = %d",
620 TW_CL_SEVERITY_ERROR_STRING,
621 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
623 "Unposted command completed!!",
627 * Remove request from the busy queue. Just mark it complete.
628 * There's no need to move it into the complete queue as we are
629 * going to be done with it right now.
631 req->state = TW_OSLI_REQ_STATE_COMPLETE;
632 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
634 tw_osli_unmap_request(req);
637 if (req->error_code) {
638 /* This request never got submitted to the firmware. */
639 if (req->error_code == EBUSY) {
641 * Cmd queue is full, or the Common Layer is out of
642 * resources. The simq will already have been frozen.
643 * When this ccb gets completed will unfreeze the simq.
645 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
647 else if (req->error_code == EFBIG)
648 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
650 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
652 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
653 if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
654 ccb->ccb_h.status = CAM_REQ_CMP;
656 if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
657 ccb->ccb_h.status |= CAM_SEL_TIMEOUT;
658 else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
659 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
660 else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
661 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
662 else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
663 ccb->ccb_h.status |= (CAM_REQUEUE_REQ | CAM_SCSI_BUS_RESET);
665 * If none of the above errors occurred, simply
666 * mark completion error.
668 if (ccb->ccb_h.status == 0)
669 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
671 if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
672 ccb->csio.sense_len = scsi_req->sense_len;
673 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
677 ccb->csio.scsi_status = scsi_req->scsi_status;
680 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
681 mtx_lock(sc->sim_lock);
683 mtx_unlock(sc->sim_lock);
684 if (! req->error_code)
685 /* twa_action will free the request otherwise */
686 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);