freebsd-nq/sys/dev/mfi/mfi_cam.c
Steven Hartland 08c89430bd Fixes queuing issues where mfi_release_command blindly sets the cm_flags = 0
without first removing the command from the relavent queue.

This was causing panics in the queue functions which check to ensure a command
is not on another queue.

Fixed some cases where the error from mfi_mapcmd was lost and where the command
was never released / dequeued in error cases.

Ensure that all failures to mfi_mapcmd are logged.

Fixed possible null pointer exception in mfi_aen_setup if mfi_get_log_state
failed.

Fixed mfi_parse_entries & mfi_aen_setup not returning possible errors.

Corrected MFI_DUMP_CMDS calls with invalid vars SC vs sc.

Commands which have timed out now set cm_error to ETIMEDOUT and call
mfi_complete which prevents them getting stuck in the busy queue forever.

Fixed possible use of NULL pointer in mfi_tbolt_get_cmd.

Changed output formats to be more easily recognisable when debugging.

Optimised mfi_cmd_pool_tbolt cleanup.

Made information about driver limiting commands always display as for modern
cards this can be severe.

Fixed mfi_tbolt_alloc_cmd out of memory case which previously didnt return an
error.

Added malloc checks for request_desc_pool including free when subsiquent errors
are detected.

Fixed overflow error in SIMD reply descriptor check.

Fixed tbolt_cmd leak in mfi_build_and_issue_cmd if there's an error during IO
build.

Elimintated double checks on sc->mfi_aen_cm & sc->mfi_map_sync_cm in
mfi_shutdown.

Move local hdr calculation after error check in mfi_aen_complete.

Fixed wakeup on NULL in mfi_aen_complete.

Fixed mfi_aen_cm cleanup in mfi_process_fw_state_chg_isr not checking if it was
NULL.

Changed mfi_alloc_commands to error if bus_dmamap_create fails. Previously we
would try to continue with the number of allocated commands but lots of places
in the driver assume sc->mfi_max_fw_cmds is whats available so its unsafe to do
this without lots of changes.

Removed mfi_total_cmds as its no longer used due the above change.

Corrected mfi_tbolt_alloc_cmd to return ENOMEM where appropriate.

Fixed timeouts actually firing at double what they should.

Setting hw.mfi.max_cmds=-1 now configures to use the controller max.

A few style (9) fixes e.g. braced single line conditions and double blank lines

Cleaned up queuing macros

Removed invalid queuing tests for multiple queues

Trap and deal with errors when doing sends in mfi_data_cb

Refactored frame sending into one method with error checking of the return
code so we can ensure commands aren't left on the queue after error. This
ensures that mfi_mapcmd & mfi_data_cb leave the queue in a valid state.

Refactored how commands are cleaned up, mfi_release_command now ensures
that all queues and command state is maintained in a consistent state.

Prevent NULL pointer use in mfi_tbolt_complete_cmd

Fixed use of NULL sc->mfi_map_sync_cm in wakeup

Added defines to help with output of mfi_cmd and header flags.

Fixed mfi_tbolt_init_MFI_queue invalidating cm_index of the acquired mfi_cmd.

Reset now reinitialises sync map as well as AEN.

Fixed possible use of NULL pointer in mfi_build_and_issue_cmd

Fixed mfi_tbolt_init_MFI_queue call to mfi_process_fw_state_chg_isr causing
panic on failure.

Ensure that tbolt cards always initialise next_host_reply_index and
free_host_reply_index (based off mfi_max_fw_cmds) on both startup and
reset as per the linux driver.

Fixed mfi_tbolt_complete_cmd not acknowledging unknown commands so
it didn't clear the controller.

Prevent locks from being dropped and re-acquired in the following functions
which was allowing multiple threads to enter critical methods such as
mfi_tbolt_complete_cmd & mfi_process_fw_state_chg_isr:-
* mfi_tbolt_init_MFI_queue
* mfi_aen_complete / mfi_aen_register
* mfi_tbolt_sync_map_info
* mfi_get_log_state
* mfi_parse_entries

The locking for these functions was promoting to higher level methods. This
also fixed MFI_LINUX_SET_AEN_2 which was already acquiring the lock, so would
have paniced for recursive lock.

This also required changing malloc of ld_sync in mfi_tbolt_sync_map_info to
M_NOWAIT which can hence now fail but this was already expected as its return
was being tested.

Removed the assignment of cm_index in mfi_tbolt_init_MFI_queue which breaks
the world if the cmd returned by mfi_dequeue_free isn't the first cmd.

Fixed locking in mfi_data_cb, this is an async callback from bus_dmamap_load
which could hence be called after the caller has dropped the lock. If we
don't have the lock we aquire it and ensure we unlock before returning.

Fixed locking mfi_comms_init when mfi_dequeue_free fails.

Fixed mfi_build_and_issue_cmd not returning tbolt cmds aquired to the pool
on error.

Fixed mfi_abort not dropping the io lock when mfi_dequeue_free fails.

Added hw.mfi.polled_cmd_timeout sysctl that enables tuning of polled
timeouts. This shouldn't be reduced below 50 seconds as its used for
firmware patching which can take quite some time.

Added hw.mfi.fw_reset_test sysctl which is avaliable when compiled with
MFI_DEBUG and allows the testing of controller reset that was provoking a
large number of the issues encountered here.

Reviewed by:	Doug Ambrisko
Approved by:	pjd (mentor)
MFC after:	1 month
2013-02-27 02:21:10 +00:00

474 lines
12 KiB
C

/*-
* Copyright 2007 Scott Long
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_mfi.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/selinfo.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/eventhandler.h>
#include <sys/rman.h>
#include <sys/bus_dma.h>
#include <sys/bio.h>
#include <sys/ioccom.h>
#include <sys/uio.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/sysctl.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_debug.h>
#include <cam/cam_periph.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt_periph.h>
#include <cam/cam_xpt_sim.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#include <machine/md_var.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <dev/mfi/mfireg.h>
#include <dev/mfi/mfi_ioctl.h>
#include <dev/mfi/mfivar.h>
enum mfip_state {
MFIP_STATE_NONE,
MFIP_STATE_DETACH,
MFIP_STATE_RESCAN
};
struct mfip_softc {
device_t dev;
struct mfi_softc *mfi_sc;
struct cam_devq *devq;
struct cam_sim *sim;
struct cam_path *path;
enum mfip_state state;
};
static int mfip_probe(device_t);
static int mfip_attach(device_t);
static int mfip_detach(device_t);
static void mfip_cam_action(struct cam_sim *, union ccb *);
static void mfip_cam_poll(struct cam_sim *);
static void mfip_cam_rescan(struct mfi_softc *, uint32_t tid);
static struct mfi_command * mfip_start(void *);
static void mfip_done(struct mfi_command *cm);
static int mfi_allow_disks = 0;
TUNABLE_INT("hw.mfi.allow_cam_disk_passthrough", &mfi_allow_disks);
SYSCTL_INT(_hw_mfi, OID_AUTO, allow_cam_disk_passthrough, CTLFLAG_RD,
&mfi_allow_disks, 0, "event message locale");
static devclass_t mfip_devclass;
static device_method_t mfip_methods[] = {
DEVMETHOD(device_probe, mfip_probe),
DEVMETHOD(device_attach, mfip_attach),
DEVMETHOD(device_detach, mfip_detach),
DEVMETHOD_END
};
static driver_t mfip_driver = {
"mfip",
mfip_methods,
sizeof(struct mfip_softc)
};
DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
MODULE_DEPEND(mfip, cam, 1, 1, 1);
MODULE_DEPEND(mfip, mfi, 1, 1, 1);
#define ccb_mfip_ptr sim_priv.entries[0].ptr
static int
mfip_probe(device_t dev)
{
device_set_desc(dev, "SCSI Passthrough Bus");
return (0);
}
static int
mfip_attach(device_t dev)
{
struct mfip_softc *sc;
struct mfi_softc *mfisc;
sc = device_get_softc(dev);
if (sc == NULL)
return (EINVAL);
mfisc = device_get_softc(device_get_parent(dev));
sc->dev = dev;
sc->state = MFIP_STATE_NONE;
sc->mfi_sc = mfisc;
mfisc->mfi_cam_start = mfip_start;
if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
return (ENOMEM);
sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
device_get_unit(dev), &mfisc->mfi_io_lock, 1,
MFI_SCSI_MAX_CMDS, sc->devq);
if (sc->sim == NULL) {
cam_simq_free(sc->devq);
sc->devq = NULL;
device_printf(dev, "CAM SIM attach failed\n");
return (EINVAL);
}
mfisc->mfi_cam_rescan_cb = mfip_cam_rescan;
mtx_lock(&mfisc->mfi_io_lock);
if (xpt_bus_register(sc->sim, dev, 0) != 0) {
device_printf(dev, "XPT bus registration failed\n");
cam_sim_free(sc->sim, FALSE);
sc->sim = NULL;
cam_simq_free(sc->devq);
sc->devq = NULL;
mtx_unlock(&mfisc->mfi_io_lock);
return (EINVAL);
}
mtx_unlock(&mfisc->mfi_io_lock);
return (0);
}
static int
mfip_detach(device_t dev)
{
struct mfip_softc *sc;
sc = device_get_softc(dev);
if (sc == NULL)
return (EINVAL);
mtx_lock(&sc->mfi_sc->mfi_io_lock);
if (sc->state == MFIP_STATE_RESCAN) {
mtx_unlock(&sc->mfi_sc->mfi_io_lock);
return (EBUSY);
}
sc->state = MFIP_STATE_DETACH;
mtx_unlock(&sc->mfi_sc->mfi_io_lock);
sc->mfi_sc->mfi_cam_rescan_cb = NULL;
if (sc->sim != NULL) {
mtx_lock(&sc->mfi_sc->mfi_io_lock);
xpt_bus_deregister(cam_sim_path(sc->sim));
cam_sim_free(sc->sim, FALSE);
sc->sim = NULL;
mtx_unlock(&sc->mfi_sc->mfi_io_lock);
}
if (sc->devq != NULL) {
cam_simq_free(sc->devq);
sc->devq = NULL;
}
return (0);
}
static void
mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
{
struct mfip_softc *sc = cam_sim_softc(sim);
struct mfi_softc *mfisc = sc->mfi_sc;
mtx_assert(&mfisc->mfi_io_lock, MA_OWNED);
switch (ccb->ccb_h.func_code) {
case XPT_PATH_INQ:
{
struct ccb_pathinq *cpi = &ccb->cpi;
cpi->version_num = 1;
cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
cpi->target_sprt = 0;
cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
cpi->hba_eng_cnt = 0;
cpi->max_target = MFI_SCSI_MAX_TARGETS;
cpi->max_lun = MFI_SCSI_MAX_LUNS;
cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
cpi->unit_number = cam_sim_unit(sim);
cpi->bus_id = cam_sim_bus(sim);
cpi->base_transfer_speed = 150000;
cpi->transport = XPORT_SAS;
cpi->transport_version = 0;
cpi->protocol = PROTO_SCSI;
cpi->protocol_version = SCSI_REV_2;
cpi->ccb_h.status = CAM_REQ_CMP;
break;
}
case XPT_RESET_BUS:
ccb->ccb_h.status = CAM_REQ_CMP;
break;
case XPT_RESET_DEV:
ccb->ccb_h.status = CAM_REQ_CMP;
break;
case XPT_GET_TRAN_SETTINGS:
{
struct ccb_trans_settings_sas *sas =
&ccb->cts.xport_specific.sas;
ccb->cts.protocol = PROTO_SCSI;
ccb->cts.protocol_version = SCSI_REV_2;
ccb->cts.transport = XPORT_SAS;
ccb->cts.transport_version = 0;
sas->valid &= ~CTS_SAS_VALID_SPEED;
sas->bitrate = 150000;
ccb->ccb_h.status = CAM_REQ_CMP;
break;
}
case XPT_SET_TRAN_SETTINGS:
ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
break;
case XPT_SCSI_IO:
{
struct ccb_hdr *ccbh = &ccb->ccb_h;
struct ccb_scsiio *csio = &ccb->csio;
ccbh->status = CAM_REQ_INPROG;
if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
ccbh->status = CAM_REQ_INVALID;
break;
}
ccbh->ccb_mfip_ptr = sc;
TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
mfi_startio(mfisc);
return;
}
default:
ccb->ccb_h.status = CAM_REQ_INVALID;
break;
}
xpt_done(ccb);
return;
}
static void
mfip_cam_rescan(struct mfi_softc *sc, uint32_t tid)
{
union ccb *ccb;
struct mfip_softc *camsc;
struct cam_sim *sim;
device_t mfip_dev;
mtx_lock(&Giant);
mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1);
mtx_unlock(&Giant);
if (mfip_dev == NULL) {
device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n");
return;
}
mtx_lock(&sc->mfi_io_lock);
camsc = device_get_softc(mfip_dev);
if (camsc->state == MFIP_STATE_DETACH) {
mtx_unlock(&sc->mfi_io_lock);
return;
}
camsc->state = MFIP_STATE_RESCAN;
mtx_unlock(&sc->mfi_io_lock);
ccb = xpt_alloc_ccb_nowait();
if (ccb == NULL) {
device_printf(sc->mfi_dev,
"Cannot allocate ccb for bus rescan.\n");
return;
}
sim = camsc->sim;
if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim),
tid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
xpt_free_ccb(ccb);
device_printf(sc->mfi_dev,
"Cannot create path for bus rescan.\n");
return;
}
xpt_rescan(ccb);
mtx_lock(&sc->mfi_io_lock);
camsc->state = MFIP_STATE_NONE;
mtx_unlock(&sc->mfi_io_lock);
}
static struct mfi_command *
mfip_start(void *data)
{
union ccb *ccb = data;
struct ccb_hdr *ccbh = &ccb->ccb_h;
struct ccb_scsiio *csio = &ccb->csio;
struct mfip_softc *sc;
struct mfi_pass_frame *pt;
struct mfi_command *cm;
uint32_t context = 0;
sc = ccbh->ccb_mfip_ptr;
if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
return (NULL);
/* Zero out the MFI frame */
context = cm->cm_frame->header.context;
bzero(cm->cm_frame, sizeof(union mfi_frame));
cm->cm_frame->header.context = context;
pt = &cm->cm_frame->pass;
pt->header.cmd = MFI_CMD_PD_SCSI_IO;
pt->header.cmd_status = 0;
pt->header.scsi_status = 0;
pt->header.target_id = ccbh->target_id;
pt->header.lun_id = ccbh->target_lun;
pt->header.flags = 0;
pt->header.timeout = 0;
pt->header.data_len = csio->dxfer_len;
pt->header.sense_len = MFI_SENSE_LEN;
pt->header.cdb_len = csio->cdb_len;
pt->sense_addr_lo = (uint32_t)cm->cm_sense_busaddr;
pt->sense_addr_hi = (uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32);
if (ccbh->flags & CAM_CDB_POINTER)
bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
else
bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
cm->cm_complete = mfip_done;
cm->cm_private = ccb;
cm->cm_sg = &pt->sgl;
cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
cm->cm_data = ccb;
cm->cm_len = csio->dxfer_len;
switch (ccbh->flags & CAM_DIR_MASK) {
case CAM_DIR_IN:
cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_CCB;
break;
case CAM_DIR_OUT:
cm->cm_flags = MFI_CMD_DATAOUT | MFI_CMD_CCB;
break;
case CAM_DIR_NONE:
default:
cm->cm_data = NULL;
cm->cm_len = 0;
cm->cm_flags = 0;
break;
}
TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
return (cm);
}
static void
mfip_done(struct mfi_command *cm)
{
union ccb *ccb = cm->cm_private;
struct ccb_hdr *ccbh = &ccb->ccb_h;
struct ccb_scsiio *csio = &ccb->csio;
struct mfip_softc *sc;
struct mfi_pass_frame *pt;
sc = ccbh->ccb_mfip_ptr;
pt = &cm->cm_frame->pass;
switch (pt->header.cmd_status) {
case MFI_STAT_OK:
{
uint8_t command, device;
ccbh->status = CAM_REQ_CMP;
csio->scsi_status = pt->header.scsi_status;
if (ccbh->flags & CAM_CDB_POINTER)
command = csio->cdb_io.cdb_ptr[0];
else
command = csio->cdb_io.cdb_bytes[0];
if (command == INQUIRY) {
device = csio->data_ptr[0] & 0x1f;
if ((!mfi_allow_disks && device == T_DIRECT) ||
(device == T_PROCESSOR))
csio->data_ptr[0] =
(csio->data_ptr[0] & 0xe0) | T_NODEVICE;
}
break;
}
case MFI_STAT_SCSI_DONE_WITH_ERROR:
{
int sense_len;
ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
csio->scsi_status = pt->header.scsi_status;
if (pt->header.sense_len < csio->sense_len)
csio->sense_resid = csio->sense_len -
pt->header.sense_len;
else
csio->sense_resid = 0;
sense_len = min(pt->header.sense_len,
sizeof(struct scsi_sense_data));
bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
break;
}
case MFI_STAT_DEVICE_NOT_FOUND:
ccbh->status = CAM_SEL_TIMEOUT;
break;
case MFI_STAT_SCSI_IO_FAILED:
ccbh->status = CAM_REQ_CMP_ERR;
csio->scsi_status = pt->header.scsi_status;
break;
default:
ccbh->status = CAM_REQ_CMP_ERR;
csio->scsi_status = pt->header.scsi_status;
break;
}
mfi_release_command(cm);
xpt_done(ccb);
}
static void
mfip_cam_poll(struct cam_sim *sim)
{
struct mfip_softc *sc = cam_sim_softc(sim);
struct mfi_softc *mfisc = sc->mfi_sc;
mfisc->mfi_intr_ptr(mfisc);
}