2005-01-05 22:34:37 +00:00
|
|
|
/*-
|
1998-09-15 06:33:23 +00:00
|
|
|
* Common functions for SCSI Interface Modules (SIMs).
|
|
|
|
*
|
2017-11-27 15:12:43 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
1998-09-15 06:33:23 +00:00
|
|
|
* Copyright (c) 1997 Justin T. Gibbs.
|
|
|
|
* 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,
|
|
|
|
* without modification, immediately at the beginning of the file.
|
|
|
|
* 2. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-06-10 17:50:20 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-09-15 06:33:23 +00:00
|
|
|
#include <sys/param.h>
|
2021-05-22 00:52:55 +00:00
|
|
|
#include <sys/bus.h>
|
2005-07-01 15:21:30 +00:00
|
|
|
#include <sys/kernel.h>
|
2007-04-15 08:49:19 +00:00
|
|
|
#include <sys/lock.h>
|
2021-05-22 00:52:55 +00:00
|
|
|
#include <sys/malloc.h>
|
2007-04-15 08:49:19 +00:00
|
|
|
#include <sys/mutex.h>
|
1998-09-15 06:33:23 +00:00
|
|
|
|
|
|
|
#include <cam/cam.h>
|
|
|
|
#include <cam/cam_ccb.h>
|
|
|
|
#include <cam/cam_queue.h>
|
2021-05-22 00:52:55 +00:00
|
|
|
#include <cam/cam_sim.h>
|
2013-03-27 18:55:01 +00:00
|
|
|
#include <cam/cam_xpt.h>
|
1998-09-15 06:33:23 +00:00
|
|
|
|
|
|
|
#define CAM_PATH_ANY (u_int32_t)-1
|
|
|
|
|
2011-11-07 06:44:47 +00:00
|
|
|
static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
|
2005-07-01 15:21:30 +00:00
|
|
|
|
2017-03-21 09:12:41 +00:00
|
|
|
static struct mtx cam_sim_free_mtx;
|
|
|
|
MTX_SYSINIT(cam_sim_free_init, &cam_sim_free_mtx, "CAM SIM free lock", MTX_DEF);
|
|
|
|
|
1998-09-15 06:33:23 +00:00
|
|
|
struct cam_devq *
|
|
|
|
cam_simq_alloc(u_int32_t max_sim_transactions)
|
|
|
|
{
|
|
|
|
return (cam_devq_alloc(/*size*/0, max_sim_transactions));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cam_simq_free(struct cam_devq *devq)
|
|
|
|
{
|
|
|
|
cam_devq_free(devq);
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:44:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief allocate a new sim and fill in the details
|
|
|
|
*
|
|
|
|
* A Storage Interface Module (SIM) is the interface between CAM and
|
|
|
|
* hardware. SIM receives CCBs from CAM via @p sim_action callback and
|
|
|
|
* translates them into DMA or other hardware transactions. During system
|
|
|
|
* dumps, it can be polled with the @p sim_poll callback. CCB processing is
|
|
|
|
* terminated by calling @c xpt_done().
|
|
|
|
*
|
|
|
|
* The @p mtx acts as a perimeter lock for the SIM. All calls into the SIM's
|
|
|
|
* @p sim_action are made with this lock held. It is also used to hold/release
|
|
|
|
* a SIM, managing its reference count. When the lock is NULL, the SIM is 100%
|
|
|
|
* responsible for locking (and the reference counting is done with a shared
|
|
|
|
* lock.
|
|
|
|
*
|
|
|
|
* The cam_devq passed in (@c queue) is used to arbitrate the number of
|
|
|
|
* outstanding transactions to the SIM. For HBAs that have global limits shared
|
|
|
|
* between the different buses, the same devq should be specified for each bus
|
|
|
|
* attached to the SIM.
|
|
|
|
*
|
|
|
|
* @param sim_action Function to call to process CCBs
|
|
|
|
* @param sim_poll Function to poll the hardware for completions
|
|
|
|
* @param sim_name Name of SIM class
|
|
|
|
* @param softc Software context associated with the SIM
|
|
|
|
* @param unit Unit number of SIM
|
|
|
|
* @param mtx Mutex to lock while interacting with the SIM, or NULL
|
|
|
|
* for a SIM that handle its own locking to enable multi
|
|
|
|
* queue support.
|
|
|
|
* @param max_dev_transactions Maximum number of concurrent untagged
|
|
|
|
* transactions possible
|
|
|
|
* @param max_tagged_dev_transactions Maximum number of concurrent tagged
|
|
|
|
* transactions possible.
|
|
|
|
* @param queue The cam_devq to use for this SIM.
|
|
|
|
*/
|
1998-09-15 06:33:23 +00:00
|
|
|
struct cam_sim *
|
|
|
|
cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
|
2001-01-24 01:46:18 +00:00
|
|
|
const char *sim_name, void *softc, u_int32_t unit,
|
2007-04-15 08:49:19 +00:00
|
|
|
struct mtx *mtx, int max_dev_transactions,
|
Add a number of interrelated CAM feature enhancements and bug fixes.
NOTE: These changes will require recompilation of any userland
applications, like cdrecord, xmcd, etc., that use the CAM passthrough
interface. A make world is recommended.
camcontrol.[c8]:
- We now support two new commands, "tags" and "negotiate".
- The tags commands allows users to view the number of tagged
openings for a device as well as a number of other related
parameters, and it allows users to set tagged openings for
a device.
- The negotiate command allows users to enable and disable
disconnection and tagged queueing, set sync rates, offsets
and bus width. Note that not all of those features are
available for all controllers. Only the adv, ahc, and ncr
drivers fully support all of the features at this point.
Some cards do not allow the setting of sync rates, offsets and
the like, and some of the drivers don't have any facilities to
do so. Some drivers, like the adw driver, only support enabling
or disabling sync negotiation, but do not support setting sync
rates.
- new description in the camcontrol man page of how to format a disk
- cleanup of the camcontrol inquiry command
- add support in the 'devlist' command for skipping unconfigured devices if
-v was not specified on the command line.
- make use of the new base_transfer_speed in the path inquiry CCB.
- fix CCB bzero cases
cam_xpt.c, cam_sim.[ch], cam_ccb.h:
- new flags on many CCB function codes to designate whether they're
non-immediate, use a user-supplied CCB, and can only be passed from
userland programs via the xpt device. Use these flags in the transport
layer and pass driver to categorize CCBs.
- new flag in the transport layer device matching code for device nodes
that indicates whether a device is unconfigured
- bump the CAM version from 0x10 to 0x11
- Change the CAM ioctls to use the version as their group code, so we can
force users to recompile code even when the CCB size doesn't change.
- add + fill in a new value in the path inquiry CCB, base_transfer_speed.
Remove a corresponding field from the cam_sim structure, and add code to
every SIM to set this field to the proper value.
- Fix the set transfer settings code in the transport layer.
scsi_cd.c:
- make some variables volatile instead of just casting them in various
places
- fix a race condition in the changer code
- attach unless we get a "logical unit not supported" error. This should
fix all of the cases where people have devices that return weird errors
when they don't have media in the drive.
scsi_da.c:
- attach unless we get a "logical unit not supported" error
scsi_pass.c:
- for immediate CCBs, just malloc a CCB to send the user request in. This
gets rid of the 'held' count problem in camcontrol tags.
scsi_pass.h:
- change the CAM ioctls to use the CAM version as their group code.
adv driver:
- Allow changing the sync rate and offset separately.
adw driver
- Allow changing the sync rate and offset separately.
aha driver:
- Don't return CAM_REQ_CMP for SET_TRAN_SETTINGS CCBs.
ahc driver:
- Allow setting offset and sync rate separately
bt driver:
- Don't return CAM_REQ_CMP for SET_TRAN_SETTINGS CCBs.
NCR driver:
- Fix the ultra/ultra 2 negotiation bug
- allow setting both the sync rate and offset separately
Other HBA drivers:
- Put code in to set the base_transfer_speed field for
XPT_GET_TRAN_SETTINGS CCBs.
Reviewed by: gibbs, mjacob (isp), imp (aha)
1999-05-06 20:16:39 +00:00
|
|
|
int max_tagged_dev_transactions, struct cam_devq *queue)
|
1998-09-15 06:33:23 +00:00
|
|
|
{
|
|
|
|
struct cam_sim *sim;
|
|
|
|
|
2021-01-03 16:34:10 +00:00
|
|
|
sim = malloc(sizeof(struct cam_sim), M_CAMSIM, M_ZERO | M_NOWAIT);
|
2007-04-15 08:49:19 +00:00
|
|
|
if (sim == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
sim->sim_action = sim_action;
|
|
|
|
sim->sim_poll = sim_poll;
|
|
|
|
sim->sim_name = sim_name;
|
|
|
|
sim->softc = softc;
|
|
|
|
sim->path_id = CAM_PATH_ANY;
|
|
|
|
sim->unit_number = unit;
|
|
|
|
sim->bus_id = 0; /* set in xpt_bus_register */
|
|
|
|
sim->max_tagged_dev_openings = max_tagged_dev_transactions;
|
|
|
|
sim->max_dev_openings = max_dev_transactions;
|
|
|
|
sim->flags = 0;
|
2008-12-16 16:57:33 +00:00
|
|
|
sim->refcount = 1;
|
2007-04-15 08:49:19 +00:00
|
|
|
sim->devq = queue;
|
|
|
|
sim->mtx = mtx;
|
1998-09-15 06:33:23 +00:00
|
|
|
return (sim);
|
|
|
|
}
|
|
|
|
|
2021-10-05 13:07:17 +00:00
|
|
|
/**
|
|
|
|
* @brief frees up the sim
|
|
|
|
*
|
|
|
|
* Frees up the CAM @c sim and optionally the devq. If a mutex is associated
|
|
|
|
* with the sim, it must be locked on entry. It will remain locked on
|
|
|
|
* return.
|
|
|
|
*
|
|
|
|
* This function will wait for all outstanding reference to the sim to clear
|
|
|
|
* before returning.
|
|
|
|
*
|
|
|
|
* @param sim The sim to free
|
|
|
|
* @param free_devq Free the devq associated with the sim at creation.
|
|
|
|
*/
|
1998-09-15 06:33:23 +00:00
|
|
|
void
|
|
|
|
cam_sim_free(struct cam_sim *sim, int free_devq)
|
|
|
|
{
|
2020-09-04 18:18:05 +00:00
|
|
|
struct mtx *mtx;
|
2021-11-25 03:17:54 +00:00
|
|
|
int error __diagused;
|
2008-12-16 16:57:33 +00:00
|
|
|
|
2020-09-04 18:18:05 +00:00
|
|
|
if (sim->mtx == NULL) {
|
2017-03-21 09:12:41 +00:00
|
|
|
mtx = &cam_sim_free_mtx;
|
|
|
|
mtx_lock(mtx);
|
2020-09-04 18:18:05 +00:00
|
|
|
} else {
|
|
|
|
mtx = sim->mtx;
|
|
|
|
mtx_assert(mtx, MA_OWNED);
|
2017-03-21 09:12:41 +00:00
|
|
|
}
|
2020-10-06 23:33:56 +00:00
|
|
|
KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
|
2008-12-16 16:57:33 +00:00
|
|
|
sim->refcount--;
|
|
|
|
if (sim->refcount > 0) {
|
2017-03-21 09:12:41 +00:00
|
|
|
error = msleep(sim, mtx, PRIBIO, "simfree", 0);
|
2008-12-16 16:57:33 +00:00
|
|
|
KASSERT(error == 0, ("invalid error value for msleep(9)"));
|
|
|
|
}
|
|
|
|
KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
|
2020-09-04 18:18:05 +00:00
|
|
|
if (mtx == &cam_sim_free_mtx) /* sim->mtx == NULL */
|
2017-03-21 09:12:41 +00:00
|
|
|
mtx_unlock(mtx);
|
2008-12-16 16:57:33 +00:00
|
|
|
|
1998-09-15 06:33:23 +00:00
|
|
|
if (free_devq)
|
|
|
|
cam_simq_free(sim->devq);
|
2005-07-01 15:21:30 +00:00
|
|
|
free(sim, M_CAMSIM);
|
1998-09-15 06:33:23 +00:00
|
|
|
}
|
|
|
|
|
2008-12-16 16:57:33 +00:00
|
|
|
void
|
|
|
|
cam_sim_release(struct cam_sim *sim)
|
|
|
|
{
|
2020-09-04 18:18:05 +00:00
|
|
|
struct mtx *mtx;
|
2008-12-16 16:57:33 +00:00
|
|
|
|
2020-09-04 18:18:05 +00:00
|
|
|
if (sim->mtx == NULL)
|
2017-03-21 09:12:41 +00:00
|
|
|
mtx = &cam_sim_free_mtx;
|
2020-09-04 18:18:05 +00:00
|
|
|
else if (!mtx_owned(sim->mtx))
|
|
|
|
mtx = sim->mtx;
|
|
|
|
else
|
|
|
|
mtx = NULL; /* We hold the lock. */
|
|
|
|
if (mtx)
|
2017-03-21 09:12:41 +00:00
|
|
|
mtx_lock(mtx);
|
Merge CAM locking changes from the projects/camlock branch to radically
reduce lock congestion and improve SMP scalability of the SCSI/ATA stack,
preparing the ground for the coming next GEOM direct dispatch support.
Replace big per-SIM locks with bunch of smaller ones:
- per-LUN locks to protect device and peripheral drivers state;
- per-target locks to protect list of LUNs on target;
- per-bus locks to protect reference counting;
- per-send queue locks to protect queue of CCBs to be sent;
- per-done queue locks to protect queue of completed CCBs;
- remaining per-SIM locks now protect only HBA driver internals.
While holding LUN lock it is allowed (while not recommended for performance
reasons) to take SIM lock. The opposite acquisition order is forbidden.
All the other locks are leaf locks, that can be taken anywhere, but should
not be cascaded. Many functions, such as: xpt_action(), xpt_done(),
xpt_async(), xpt_create_path(), etc. are no longer require (but allow) SIM
lock to be held.
To keep compatibility and solve cases where SIM lock can't be dropped, all
xpt_async() calls in addition to xpt_done() calls are queued to completion
threads for async processing in clean environment without SIM lock held.
Instead of single CAM SWI thread, used for commands completion processing
before, use multiple (depending on number of CPUs) threads. Load balanced
between them using "hash" of the device B:T:L address.
HBA drivers that can drop SIM lock during completion processing and have
sufficient number of completion threads to efficiently scale to multiple
CPUs can use new function xpt_done_direct() to avoid extra context switch.
Make ahci(4) driver to use this mechanism depending on hardware setup.
Sponsored by: iXsystems, Inc.
MFC after: 2 months
2013-10-21 12:00:26 +00:00
|
|
|
KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
|
2008-12-16 16:57:33 +00:00
|
|
|
sim->refcount--;
|
2008-12-19 14:33:29 +00:00
|
|
|
if (sim->refcount == 0)
|
2008-12-16 16:57:33 +00:00
|
|
|
wakeup(sim);
|
2017-03-21 09:12:41 +00:00
|
|
|
if (mtx)
|
|
|
|
mtx_unlock(mtx);
|
2008-12-16 16:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cam_sim_hold(struct cam_sim *sim)
|
|
|
|
{
|
2020-09-04 18:18:05 +00:00
|
|
|
struct mtx *mtx;
|
2008-12-16 16:57:33 +00:00
|
|
|
|
2020-09-04 18:18:05 +00:00
|
|
|
if (sim->mtx == NULL)
|
2017-03-21 09:12:41 +00:00
|
|
|
mtx = &cam_sim_free_mtx;
|
2020-09-04 18:18:05 +00:00
|
|
|
else if (!mtx_owned(sim->mtx))
|
|
|
|
mtx = sim->mtx;
|
|
|
|
else
|
|
|
|
mtx = NULL; /* We hold the lock. */
|
|
|
|
if (mtx)
|
2017-03-21 09:12:41 +00:00
|
|
|
mtx_lock(mtx);
|
Merge CAM locking changes from the projects/camlock branch to radically
reduce lock congestion and improve SMP scalability of the SCSI/ATA stack,
preparing the ground for the coming next GEOM direct dispatch support.
Replace big per-SIM locks with bunch of smaller ones:
- per-LUN locks to protect device and peripheral drivers state;
- per-target locks to protect list of LUNs on target;
- per-bus locks to protect reference counting;
- per-send queue locks to protect queue of CCBs to be sent;
- per-done queue locks to protect queue of completed CCBs;
- remaining per-SIM locks now protect only HBA driver internals.
While holding LUN lock it is allowed (while not recommended for performance
reasons) to take SIM lock. The opposite acquisition order is forbidden.
All the other locks are leaf locks, that can be taken anywhere, but should
not be cascaded. Many functions, such as: xpt_action(), xpt_done(),
xpt_async(), xpt_create_path(), etc. are no longer require (but allow) SIM
lock to be held.
To keep compatibility and solve cases where SIM lock can't be dropped, all
xpt_async() calls in addition to xpt_done() calls are queued to completion
threads for async processing in clean environment without SIM lock held.
Instead of single CAM SWI thread, used for commands completion processing
before, use multiple (depending on number of CPUs) threads. Load balanced
between them using "hash" of the device B:T:L address.
HBA drivers that can drop SIM lock during completion processing and have
sufficient number of completion threads to efficiently scale to multiple
CPUs can use new function xpt_done_direct() to avoid extra context switch.
Make ahci(4) driver to use this mechanism depending on hardware setup.
Sponsored by: iXsystems, Inc.
MFC after: 2 months
2013-10-21 12:00:26 +00:00
|
|
|
KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
|
2008-12-16 16:57:33 +00:00
|
|
|
sim->refcount++;
|
2017-03-21 09:12:41 +00:00
|
|
|
if (mtx)
|
|
|
|
mtx_unlock(mtx);
|
2008-12-16 16:57:33 +00:00
|
|
|
}
|
|
|
|
|
1998-09-15 06:33:23 +00:00
|
|
|
void
|
|
|
|
cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
|
|
|
|
{
|
|
|
|
sim->path_id = path_id;
|
|
|
|
}
|