Reduce number of places where global control_softc is used.

At some point we may want to have several CTL instances, and that is not
really impossible.

MFC after:	2 weeks
This commit is contained in:
Alexander Motin 2014-12-19 20:35:06 +00:00
parent 6d659a5d9b
commit 9602f43616
7 changed files with 245 additions and 291 deletions

File diff suppressed because it is too large Load Diff

View File

@ -66,33 +66,33 @@ extern struct ctl_softc *control_softc;
int
ctl_backend_register(struct ctl_backend_driver *be)
{
struct ctl_softc *ctl_softc;
struct ctl_softc *softc;
struct ctl_backend_driver *be_tmp;
ctl_softc = control_softc;
softc = control_softc;
mtx_lock(&ctl_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
/*
* Sanity check, make sure this isn't a duplicate registration.
*/
STAILQ_FOREACH(be_tmp, &ctl_softc->be_list, links) {
STAILQ_FOREACH(be_tmp, &softc->be_list, links) {
if (strcmp(be_tmp->name, be->name) == 0) {
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (-1);
}
}
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
/*
* Call the backend's initialization routine.
*/
be->init();
mtx_lock(&ctl_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
STAILQ_INSERT_TAIL(&ctl_softc->be_list, be, links);
STAILQ_INSERT_TAIL(&softc->be_list, be, links);
ctl_softc->num_backends++;
softc->num_backends++;
/*
* Don't want to increment the usage count for internal consumers,
@ -113,7 +113,7 @@ ctl_backend_register(struct ctl_backend_driver *be)
atomic_set(&be->num_luns, 0);
#endif
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (0);
}
@ -121,24 +121,24 @@ ctl_backend_register(struct ctl_backend_driver *be)
int
ctl_backend_deregister(struct ctl_backend_driver *be)
{
struct ctl_softc *ctl_softc;
struct ctl_softc *softc;
ctl_softc = control_softc;
softc = control_softc;
mtx_lock(&ctl_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
#if 0
if (atomic_read(&be->num_luns) != 0) {
#endif
/* XXX KDM fix this! */
if (be->num_luns != 0) {
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (-1);
}
STAILQ_REMOVE(&ctl_softc->be_list, be, ctl_backend_driver, links);
STAILQ_REMOVE(&softc->be_list, be, ctl_backend_driver, links);
ctl_softc->num_backends--;
softc->num_backends--;
/* XXX KDM find a substitute for this? */
#if 0
@ -146,7 +146,7 @@ ctl_backend_deregister(struct ctl_backend_driver *be)
MOD_DEC_USE_COUNT;
#endif
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (0);
}
@ -154,21 +154,21 @@ ctl_backend_deregister(struct ctl_backend_driver *be)
struct ctl_backend_driver *
ctl_backend_find(char *backend_name)
{
struct ctl_softc *ctl_softc;
struct ctl_softc *softc;
struct ctl_backend_driver *be_tmp;
ctl_softc = control_softc;
softc = control_softc;
mtx_lock(&ctl_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(be_tmp, &ctl_softc->be_list, links) {
STAILQ_FOREACH(be_tmp, &softc->be_list, links) {
if (strcmp(be_tmp->name, backend_name) == 0) {
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (be_tmp);
}
}
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (NULL);
}

View File

@ -68,21 +68,22 @@ extern struct ctl_softc *control_softc;
int
ctl_frontend_register(struct ctl_frontend *fe)
{
struct ctl_softc *softc = control_softc;
struct ctl_frontend *fe_tmp;
KASSERT(control_softc != NULL, ("CTL is not initialized"));
KASSERT(softc != NULL, ("CTL is not initialized"));
/*
* Sanity check, make sure this isn't a duplicate registration.
*/
mtx_lock(&control_softc->ctl_lock);
STAILQ_FOREACH(fe_tmp, &control_softc->fe_list, links) {
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(fe_tmp, &softc->fe_list, links) {
if (strcmp(fe_tmp->name, fe->name) == 0) {
mtx_unlock(&control_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (-1);
}
}
mtx_unlock(&control_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
STAILQ_INIT(&fe->port_list);
/*
@ -91,24 +92,25 @@ ctl_frontend_register(struct ctl_frontend *fe)
if (fe->init != NULL)
fe->init();
mtx_lock(&control_softc->ctl_lock);
control_softc->num_frontends++;
STAILQ_INSERT_TAIL(&control_softc->fe_list, fe, links);
mtx_unlock(&control_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
softc->num_frontends++;
STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
mtx_unlock(&softc->ctl_lock);
return (0);
}
int
ctl_frontend_deregister(struct ctl_frontend *fe)
{
struct ctl_softc *softc = control_softc;
if (!STAILQ_EMPTY(&fe->port_list))
return (-1);
mtx_lock(&control_softc->ctl_lock);
STAILQ_REMOVE(&control_softc->fe_list, fe, ctl_frontend, links);
control_softc->num_frontends--;
mtx_unlock(&control_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
softc->num_frontends--;
mtx_unlock(&softc->ctl_lock);
/*
* Call the frontend's shutdown routine.
@ -121,41 +123,42 @@ ctl_frontend_deregister(struct ctl_frontend *fe)
struct ctl_frontend *
ctl_frontend_find(char *frontend_name)
{
struct ctl_softc *ctl_softc = control_softc;
struct ctl_softc *softc = control_softc;
struct ctl_frontend *fe;
mtx_lock(&ctl_softc->ctl_lock);
STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(fe, &softc->fe_list, links) {
if (strcmp(fe->name, frontend_name) == 0) {
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (fe);
}
}
mtx_unlock(&ctl_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (NULL);
}
int
ctl_port_register(struct ctl_port *port)
{
struct ctl_softc *softc = control_softc;
void *pool;
int port_num;
int retval;
retval = 0;
KASSERT(control_softc != NULL, ("CTL is not initialized"));
KASSERT(softc != NULL, ("CTL is not initialized"));
mtx_lock(&control_softc->ctl_lock);
port_num = ctl_ffz(control_softc->ctl_port_mask, CTL_MAX_PORTS);
mtx_lock(&softc->ctl_lock);
port_num = ctl_ffz(softc->ctl_port_mask, CTL_MAX_PORTS);
if ((port_num == -1)
|| (ctl_set_mask(control_softc->ctl_port_mask, port_num) == -1)) {
|| (ctl_set_mask(softc->ctl_port_mask, port_num) == -1)) {
port->targ_port = -1;
mtx_unlock(&control_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
return (1);
}
control_softc->num_ports++;
mtx_unlock(&control_softc->ctl_lock);
softc->num_ports++;
mtx_unlock(&softc->ctl_lock);
/*
* Initialize the initiator and portname mappings
@ -176,15 +179,15 @@ ctl_port_register(struct ctl_port *port)
* pending sense queue on the next command, whether or not it is
* a REQUEST SENSE.
*/
retval = ctl_pool_create(control_softc, port->port_name,
retval = ctl_pool_create(softc, port->port_name,
port->num_requested_ctl_io + 20, &pool);
if (retval != 0) {
free(port->wwpn_iid, M_CTL);
error:
port->targ_port = -1;
mtx_lock(&control_softc->ctl_lock);
ctl_clear_mask(control_softc->ctl_port_mask, port_num);
mtx_unlock(&control_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
ctl_clear_mask(softc->ctl_port_mask, port_num);
mtx_unlock(&softc->ctl_lock);
return (retval);
}
port->ctl_pool_ref = pool;
@ -192,12 +195,12 @@ ctl_port_register(struct ctl_port *port)
if (port->options.stqh_first == NULL)
STAILQ_INIT(&port->options);
mtx_lock(&control_softc->ctl_lock);
port->targ_port = port_num + control_softc->port_offset;
mtx_lock(&softc->ctl_lock);
port->targ_port = port_num + softc->port_offset;
STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
STAILQ_INSERT_TAIL(&control_softc->port_list, port, links);
control_softc->ctl_ports[port_num] = port;
mtx_unlock(&control_softc->ctl_lock);
STAILQ_INSERT_TAIL(&softc->port_list, port, links);
softc->ctl_ports[port_num] = port;
mtx_unlock(&softc->ctl_lock);
return (retval);
}
@ -205,6 +208,7 @@ ctl_port_register(struct ctl_port *port)
int
ctl_port_deregister(struct ctl_port *port)
{
struct ctl_softc *softc = control_softc;
struct ctl_io_pool *pool;
int port_num, retval, i;
@ -217,15 +221,15 @@ ctl_port_deregister(struct ctl_port *port)
goto bailout;
}
mtx_lock(&control_softc->ctl_lock);
STAILQ_REMOVE(&control_softc->port_list, port, ctl_port, links);
mtx_lock(&softc->ctl_lock);
STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
control_softc->num_ports--;
softc->num_ports--;
port_num = (port->targ_port < CTL_MAX_PORTS) ? port->targ_port :
port->targ_port - CTL_MAX_PORTS;
ctl_clear_mask(control_softc->ctl_port_mask, port_num);
control_softc->ctl_ports[port_num] = NULL;
mtx_unlock(&control_softc->ctl_lock);
ctl_clear_mask(softc->ctl_port_mask, port_num);
softc->ctl_ports[port_num] = NULL;
mtx_unlock(&softc->ctl_lock);
ctl_pool_free(pool);
ctl_free_opts(&port->options);

View File

@ -145,8 +145,6 @@ struct tpc_list {
TAILQ_ENTRY(tpc_list) links;
};
extern struct ctl_softc *control_softc;
static void
tpc_timeout(void *arg)
{
@ -216,6 +214,7 @@ ctl_tpc_lun_init(struct ctl_lun *lun)
void
ctl_tpc_lun_shutdown(struct ctl_lun *lun)
{
struct ctl_softc *softc = lun->ctl_softc;
struct tpc_list *list;
struct tpc_token *token, *ttoken;
@ -228,11 +227,11 @@ ctl_tpc_lun_shutdown(struct ctl_lun *lun)
}
/* Free ROD tokens for this LUN. */
mtx_assert(&control_softc->ctl_lock, MA_OWNED);
TAILQ_FOREACH_SAFE(token, &control_softc->tpc_tokens, links, ttoken) {
mtx_assert(&softc->ctl_lock, MA_OWNED);
TAILQ_FOREACH_SAFE(token, &softc->tpc_tokens, links, ttoken) {
if (token->lun != lun->lun || token->active)
continue;
TAILQ_REMOVE(&control_softc->tpc_tokens, token, links);
TAILQ_REMOVE(&softc->tpc_tokens, token, links);
free(token->params, M_CTL);
free(token, M_CTL);
}
@ -796,7 +795,8 @@ tpc_resolve(struct tpc_list *list, uint16_t idx, uint32_t *ss)
}
if (idx >= list->ncscd)
return (UINT64_MAX);
return (tpcl_resolve(list->init_port, &list->cscd[idx], ss));
return (tpcl_resolve(list->lun->ctl_softc,
list->init_port, &list->cscd[idx], ss));
}
static int
@ -1296,6 +1296,7 @@ static void
tpc_process(struct tpc_list *list)
{
struct ctl_lun *lun = list->lun;
struct ctl_softc *softc = lun->ctl_softc;
struct scsi_ec_segment *seg;
struct ctl_scsiio *ctsio = list->ctsio;
int retval = CTL_RETVAL_COMPLETE;
@ -1349,10 +1350,10 @@ tpc_process(struct tpc_list *list)
free(list->params, M_CTL);
list->params = NULL;
if (list->token) {
mtx_lock(&control_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
if (--list->token->active == 0)
list->token->last_active = time_uptime;
mtx_unlock(&control_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
list->token = NULL;
}
mtx_lock(&lun->lun_lock);
@ -1831,6 +1832,7 @@ ctl_populate_token(struct ctl_scsiio *ctsio)
{
struct scsi_populate_token *cdb;
struct scsi_populate_token_data *data;
struct ctl_softc *softc;
struct ctl_lun *lun;
struct ctl_port *port;
struct tpc_list *list, *tlist;
@ -1840,7 +1842,8 @@ ctl_populate_token(struct ctl_scsiio *ctsio)
CTL_DEBUG_PRINT(("ctl_populate_token\n"));
lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
port = control_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
softc = lun->ctl_softc;
port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
cdb = (struct scsi_populate_token *)ctsio->cdb;
len = scsi_4btoul(cdb->length);
@ -1944,9 +1947,9 @@ ctl_populate_token(struct ctl_scsiio *ctsio)
list->curseg = 0;
list->completed = 1;
list->last_active = time_uptime;
mtx_lock(&control_softc->ctl_lock);
TAILQ_INSERT_TAIL(&control_softc->tpc_tokens, token, links);
mtx_unlock(&control_softc->ctl_lock);
mtx_lock(&softc->ctl_lock);
TAILQ_INSERT_TAIL(&softc->tpc_tokens, token, links);
mtx_unlock(&softc->ctl_lock);
ctl_set_success(ctsio);
ctl_done((union ctl_io *)ctsio);
return (CTL_RETVAL_COMPLETE);
@ -1965,6 +1968,7 @@ ctl_write_using_token(struct ctl_scsiio *ctsio)
{
struct scsi_write_using_token *cdb;
struct scsi_write_using_token_data *data;
struct ctl_softc *softc;
struct ctl_lun *lun;
struct tpc_list *list, *tlist;
struct tpc_token *token;
@ -1973,6 +1977,7 @@ ctl_write_using_token(struct ctl_scsiio *ctsio)
CTL_DEBUG_PRINT(("ctl_write_using_token\n"));
lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
softc = lun->ctl_softc;
cdb = (struct scsi_write_using_token *)ctsio->cdb;
len = scsi_4btoul(cdb->length);
@ -2051,8 +2056,8 @@ ctl_write_using_token(struct ctl_scsiio *ctsio)
return (CTL_RETVAL_COMPLETE);
}
mtx_lock(&control_softc->ctl_lock);
TAILQ_FOREACH(token, &control_softc->tpc_tokens, links) {
mtx_lock(&softc->ctl_lock);
TAILQ_FOREACH(token, &softc->tpc_tokens, links) {
if (memcmp(token->token, data->rod_token,
sizeof(data->rod_token)) == 0)
break;
@ -2063,7 +2068,7 @@ ctl_write_using_token(struct ctl_scsiio *ctsio)
if (data->flags & EC_WUT_DEL_TKN)
token->timeout = 0;
}
mtx_unlock(&control_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
if (token == NULL) {
mtx_lock(&lun->lun_lock);
TAILQ_REMOVE(&lun->tpc_lists, list, links);
@ -2188,6 +2193,7 @@ ctl_receive_rod_token_information(struct ctl_scsiio *ctsio)
int
ctl_report_all_rod_tokens(struct ctl_scsiio *ctsio)
{
struct ctl_softc *softc;
struct ctl_lun *lun;
struct scsi_report_all_rod_tokens *cdb;
struct scsi_report_all_rod_tokens_data *data;
@ -2199,14 +2205,15 @@ ctl_report_all_rod_tokens(struct ctl_scsiio *ctsio)
cdb = (struct scsi_report_all_rod_tokens *)ctsio->cdb;
lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
softc = lun->ctl_softc;
retval = CTL_RETVAL_COMPLETE;
tokens = 0;
mtx_lock(&control_softc->ctl_lock);
TAILQ_FOREACH(token, &control_softc->tpc_tokens, links)
mtx_lock(&softc->ctl_lock);
TAILQ_FOREACH(token, &softc->tpc_tokens, links)
tokens++;
mtx_unlock(&control_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
if (tokens > 512)
tokens = 512;
@ -2231,15 +2238,15 @@ ctl_report_all_rod_tokens(struct ctl_scsiio *ctsio)
data = (struct scsi_report_all_rod_tokens_data *)ctsio->kern_data_ptr;
i = 0;
mtx_lock(&control_softc->ctl_lock);
TAILQ_FOREACH(token, &control_softc->tpc_tokens, links) {
mtx_lock(&softc->ctl_lock);
TAILQ_FOREACH(token, &softc->tpc_tokens, links) {
if (i >= tokens)
break;
memcpy(&data->rod_management_token_list[i * 96],
token->token, 96);
i++;
}
mtx_unlock(&control_softc->ctl_lock);
mtx_unlock(&softc->ctl_lock);
scsi_ulto4b(sizeof(*data) - 4 + i * 96, data->available_data);
/*
printf("RART tokens=%d\n", i);

View File

@ -31,7 +31,8 @@
void tpc_done(union ctl_io *io);
uint64_t tpcl_resolve(int init_port, struct scsi_ec_cscd *cscd, uint32_t *ss);
uint64_t tpcl_resolve(struct ctl_softc *softc, int init_port,
struct scsi_ec_cscd *cscd, uint32_t *ss);
union ctl_io * tpcl_alloc_io(void);
int tpcl_queue(union ctl_io *io, uint64_t lun);

View File

@ -63,7 +63,6 @@ struct tpcl_softc {
int cur_tag_num;
};
extern struct ctl_softc *control_softc;
static struct tpcl_softc tpcl_softc;
static int tpcl_init(void);
@ -309,9 +308,9 @@ tpcl_done(union ctl_io *io)
}
uint64_t
tpcl_resolve(int init_port, struct scsi_ec_cscd *cscd, uint32_t *ss)
tpcl_resolve(struct ctl_softc *softc, int init_port,
struct scsi_ec_cscd *cscd, uint32_t *ss)
{
struct ctl_softc *softc = control_softc;
struct scsi_ec_cscd_id *cscdid;
struct ctl_port *port;
struct ctl_lun *lun;

View File

@ -228,8 +228,6 @@ static struct ctl_frontend ctlfe_frontend =
};
CTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
extern struct ctl_softc *control_softc;
void
ctlfeshutdown(void)
{