-/*
- * qla2x00_fcport_bind
- * Locates a target number for FC port.
- *
- * Input:
- * ha = adapter state pointer.
- * fcport = FC port structure pointer.
- *
- * Returns:
- * target number
- *
- * Context:
- * Kernel context.
- */
-static uint16_t
-qla2x00_fcport_bind(scsi_qla_host_t *ha, fc_port_t *fcport)
-{
- int found;
- uint16_t tgt;
- os_tgt_t *tq;
-
- /* Check for persistent binding. */
- for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
- if ((tq = TGT_Q(ha, tgt)) == NULL)
- continue;
-
- found = 0;
- switch (ha->binding_type) {
- case BIND_BY_PORT_ID:
- if (fcport->d_id.b24 == tq->d_id.b24) {
- memcpy(tq->node_name, fcport->node_name,
- WWN_SIZE);
- memcpy(tq->port_name, fcport->port_name,
- WWN_SIZE);
- found++;
- }
- break;
- case BIND_BY_PORT_NAME:
- if (memcmp(fcport->port_name, tq->port_name,
- WWN_SIZE) == 0) {
- /*
- * In case of persistent binding, update the
- * WWNN.
- */
- memcpy(tq->node_name, fcport->node_name,
- WWN_SIZE);
- found++;
- }
- break;
- }
- if (found)
- break;
- }
-
- /* TODO: honor the ConfigRequired flag */
- if (tgt == MAX_TARGETS) {
- /* Check if targetID 0 available. */
- tgt = 0;
-
- if (TGT_Q(ha, tgt) != NULL) {
- /* Locate first free target for device. */
- for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
- if (TGT_Q(ha, tgt) == NULL) {
- break;
- }
- }
- }
- if (tgt != MAX_TARGETS) {
- if ((tq = qla2x00_tgt_alloc(ha, tgt)) != NULL) {
- memcpy(tq->node_name, fcport->node_name,
- WWN_SIZE);
- memcpy(tq->port_name, fcport->port_name,
- WWN_SIZE);
- tq->d_id.b24 = fcport->d_id.b24;
- }
- }
- }
-
- /* Reset target numbers incase it changed. */
- fcport->os_target_id = tgt;
- if (tgt != MAX_TARGETS && tq != NULL) {
- DEBUG2(printk("scsi(%ld): Assigning target ID=%02d @ %p to "
- "loop id=0x%04x, port state=0x%x, port down retry=%d\n",
- ha->host_no, tgt, tq, fcport->loop_id,
- atomic_read(&fcport->state),
- atomic_read(&fcport->port_down_timer)));
-
- fcport->tgt_queue = tq;
- fcport->flags |= FCF_PERSISTENT_BOUND;
- tq->fcport = fcport;
- set_bit(TQF_ONLINE, &tq->flags);
- tq->port_down_retry_count = ha->port_down_retry_count;
-
-#if 0
- qla2x00_get_lun_mask_from_config(ha, fcport, tgt, 0);
-#endif
- }
-
- if (tgt == MAX_TARGETS) {
- qla_printk(KERN_WARNING, ha,
- "Unable to bind fcport, loop_id=%x\n", fcport->loop_id);
- }
-
- return (tgt);
-}
-
-/*
- * qla2x00_fclun_bind
- * Binds all FC device LUNS to OS LUNS.
- *
- * Input:
- * ha: adapter state pointer.
- * fcport: FC port structure pointer.
- *
- * Returns:
- * target number
- *
- * Context:
- * Kernel context.
- */
-static os_lun_t *
-qla2x00_fclun_bind(scsi_qla_host_t *ha, fc_port_t *fcport, fc_lun_t *fclun)
-{
- os_lun_t *lq;
- uint16_t tgt;
- uint16_t lun;
-
- tgt = fcport->os_target_id;
- lun = fclun->lun;
-
- /* Allocate LUNs */
- if (lun >= MAX_LUNS) {
- DEBUG2(printk("scsi(%ld): Unable to bind lun, invalid "
- "lun=(%x).\n", ha->host_no, lun));
- return (NULL);
- }
-
- /* Always alloc LUN 0 so kernel will scan past LUN 0. */
- if (lun != 0 && (EXT_IS_LUN_BIT_SET(&(fcport->lun_mask), lun))) {
- return (NULL);
- }
-
- if ((lq = qla2x00_lun_alloc(ha, tgt, lun)) == NULL) {
- qla_printk(KERN_WARNING, ha,
- "Unable to bind fclun, loop_id=%x lun=%x\n",
- fcport->loop_id, lun);
- return (NULL);
- }
-
- lq->fclun = fclun;
-
- return (lq);
-}
-
-/*
- * qla2x00_tgt_alloc
- * Allocate and pre-initialize target queue.
- *
- * Input:
- * ha = adapter block pointer.
- * t = SCSI target number.
- *
- * Returns:
- * NULL = failure
- *
- * Context:
- * Kernel context.
- */
-os_tgt_t *
-qla2x00_tgt_alloc(scsi_qla_host_t *ha, uint16_t tgt)
-{
- os_tgt_t *tq;
-
- /*
- * If SCSI addressing OK, allocate TGT queue and lock.
- */
- if (tgt >= MAX_TARGETS) {
- DEBUG2(printk("scsi(%ld): Unable to allocate target, invalid "
- "target number %d.\n", ha->host_no, tgt));
- return (NULL);
- }
-
- tq = TGT_Q(ha, tgt);
- if (tq == NULL) {
- tq = kmalloc(sizeof(os_tgt_t), GFP_ATOMIC);
- if (tq != NULL) {
- DEBUG2(printk("scsi(%ld): Alloc Target %d @ %p\n",
- ha->host_no, tgt, tq));
-
- memset(tq, 0, sizeof(os_tgt_t));
- tq->ha = ha;
-
- TGT_Q(ha, tgt) = tq;
- }
- }
- if (tq != NULL) {
- tq->port_down_retry_count = ha->port_down_retry_count;
- } else {
- qla_printk(KERN_WARNING, ha,
- "Unable to allocate target.\n");
- ha->mem_err++;
- }
-
- return (tq);
-}
-
-/*
- * qla2x00_tgt_free
- * Frees target and LUN queues.
- *
- * Input:
- * ha = adapter block pointer.
- * t = SCSI target number.
- *
- * Context:
- * Kernel context.
- */
-void
-qla2x00_tgt_free(scsi_qla_host_t *ha, uint16_t tgt)
-{
- os_tgt_t *tq;
- uint16_t lun;
-
- /*
- * If SCSI addressing OK, allocate TGT queue and lock.
- */
- if (tgt >= MAX_TARGETS) {
- DEBUG2(printk("scsi(%ld): Unable to de-allocate target, "
- "invalid target number %d.\n", ha->host_no, tgt));
-
- return;
- }
-
- tq = TGT_Q(ha, tgt);
- if (tq != NULL) {
- TGT_Q(ha, tgt) = NULL;
-
- /* Free LUN structures. */
- for (lun = 0; lun < MAX_LUNS; lun++)
- qla2x00_lun_free(ha, tgt, lun);
-
- kfree(tq);
- }
-
- return;
-}
-
-/*
- * qla2x00_lun_alloc
- * Allocate and initialize LUN queue.
- *
- * Input:
- * ha = adapter block pointer.
- * t = SCSI target number.
- * l = LUN number.
- *
- * Returns:
- * NULL = failure
- *
- * Context:
- * Kernel context.
- */
-os_lun_t *
-qla2x00_lun_alloc(scsi_qla_host_t *ha, uint16_t tgt, uint16_t lun)
-{
- os_lun_t *lq;
-
- /*
- * If SCSI addressing OK, allocate LUN queue.
- */
- if (tgt >= MAX_TARGETS || lun >= MAX_LUNS || TGT_Q(ha, tgt) == NULL) {
- DEBUG2(printk("scsi(%ld): Unable to allocate lun, invalid "
- "parameter.\n", ha->host_no));
-
- return (NULL);
- }
-
- lq = LUN_Q(ha, tgt, lun);
- if (lq == NULL) {
- lq = kmalloc(sizeof(os_lun_t), GFP_ATOMIC);
- if (lq != NULL) {
- DEBUG2(printk("scsi(%ld): Alloc Lun %d @ tgt %d.\n",
- ha->host_no, lun, tgt));
-
- memset(lq, 0, sizeof (os_lun_t));
- LUN_Q(ha, tgt, lun) = lq;
-
- /*
- * The following lun queue initialization code
- * must be duplicated in alloc_ioctl_mem function
- * for ioctl_lq.
- */
- lq->q_state = LUN_STATE_READY;
- spin_lock_init(&lq->q_lock);
- }
- }
-
- if (lq == NULL) {
- qla_printk(KERN_WARNING, ha, "Unable to allocate lun.\n");
- }
-
- return (lq);
-}
-
-/*
- * qla2x00_lun_free
- * Frees LUN queue.
- *
- * Input:
- * ha = adapter block pointer.
- * t = SCSI target number.
- *
- * Context:
- * Kernel context.
- */
-static void
-qla2x00_lun_free(scsi_qla_host_t *ha, uint16_t tgt, uint16_t lun)
-{
- os_lun_t *lq;
-
- /*
- * If SCSI addressing OK, allocate TGT queue and lock.
- */
- if (tgt >= MAX_TARGETS || lun >= MAX_LUNS) {
- DEBUG2(printk("scsi(%ld): Unable to deallocate lun, invalid "
- "parameter.\n", ha->host_no));
-
- return;
- }
-
- if (TGT_Q(ha, tgt) != NULL && (lq = LUN_Q(ha, tgt, lun)) != NULL) {
- LUN_Q(ha, tgt, lun) = NULL;
- kfree(lq);
- }
-
- return;
-}
-
-
-#if 0
-/*
- * qla2x00_get_lun_mask_from_config
- * Get lun mask from the configuration parameters.
- * Bit order is little endian.
- *
- * Input:
- * ha -- Host adapter
- * tgt -- target/device number
- * port -- pointer to port
- */
-static void
-qla2x00_get_lun_mask_from_config(scsi_qla_host_t *ha,
- fc_port_t *fcport, uint16_t tgt, uint16_t dev_no)
-{
- char propbuf[60]; /* size of search string */
- int rval, lun, bit;
- lun_bit_mask_t lun_mask, *mask_ptr = &lun_mask;
-
- /* Get "target-N-device-N-lun-mask" as a 256 bit lun_mask*/
- sprintf(propbuf, "scsi-qla%ld-tgt-%d-di-%d-lun-disabled",
- ha->instance, tgt, dev_no);
-
- rval = qla2x00_get_prop_xstr(ha, propbuf,
- (uint8_t *)&lun_mask, sizeof(lun_bit_mask_t));
- if (rval == sizeof(lun_bit_mask_t)) {
- memset(&fcport->lun_mask, 0, sizeof(lun_bit_mask_t));
- for (lun = 8 * sizeof(lun_bit_mask_t) - 1, bit = 0;
- lun >= 0; lun--, bit++) {
- if (EXT_IS_LUN_BIT_SET(mask_ptr, lun))
- EXT_SET_LUN_BIT((&fcport->lun_mask), bit);
- }
-
- DEBUG3(printk("scsi(%ld): returning lun mask for fcport "
- "%02x%02x%02x%02x%02x%02x%02x%02x:\n",
- ha->host_no,
- fcport->port_name[0], fcport->port_name[1],
- fcport->port_name[2], fcport->port_name[3],
- fcport->port_name[4], fcport->port_name[5],
- fcport->port_name[6], fcport->port_name[7]));
- DEBUG3(qla2x00_dump_buffer((uint8_t *)&fcport->lun_mask,
- sizeof(lun_bit_mask_t));)
- }
-}
-#endif
-
-/*
- * qla2x00_bstr_to_hex
- * Convert hex byte string to number.
- *
- * Input:
- * s = byte string pointer.
- * bp = byte pointer for number.
- * size = number of bytes.
- *
- * Context:
- * Kernel/Interrupt context.
- */
-static int
-qla2x00_bstr_to_hex(char *s, uint8_t *bp, int size)
-{
- int cnt;
- uint8_t n;
-
- for (cnt = 0; *s != '\0' && cnt / 2 < size; cnt++) {
- if (*s >= 'A' && *s <= 'F') {
- n = (*s++ - 'A') + 10;
- } else if (*s >= 'a' && *s <= 'f') {
- n = (*s++ - 'a') + 10;
- } else if (*s >= '0' && *s <= '9') {
- n = *s++ - '0';
- } else {
- cnt = 0;
- break;
- }
-
- if (cnt & BIT_0)
- *bp++ |= n;
- else
- *bp = n << 4;
- }
-
- /* fixme(dg) Need to swap data little endian */
-
- return (cnt / 2);
-}
-
-/*
- * qla2x00_get_prop_xstr
- * Get a string property value for the specified property name and
- * convert from the property string found in the configuration file,
- * which are ASCII characters representing nibbles, 2 characters represent
- * the hexdecimal value for a byte in the byte array.
- * The byte array is initialized to zero.
- * The resulting converted value is in big endian format (MSB at byte0).
- *
- * Input:
- * ha = adapter state pointer.
- * propname = property name pointer.
- * propval = pointer where to store converted property val.
- * size = max or expected size of 'propval' array.
- *
- * Returns:
- * 0 = empty value string or invalid character in string
- * >0 = count of characters converted
- * -1 = property not found
- *
- * Context:
- * Kernel context.
- */
-int
-qla2x00_get_prop_xstr(scsi_qla_host_t *ha,
- char *propname, uint8_t *propval, int size)
-{
- char *propstr;
- int rval = -1;
- static char buf[LINESIZE];
-
- /* Get the requested property string */
- rval = qla2x00_find_propname(ha, propname, buf, ha->cmdline, size*2);
- DEBUG3(printk("%s(): Ret rval from find propname = %d\n",
- __func__,
- rval);)
-
- propstr = &buf[0];
- if (*propstr == '=')
- propstr++; /* ignore equal sign */
-
- if (rval == 0) { /* not found */
- return (-1);
- }
-
- rval = qla2x00_bstr_to_hex(propstr, (uint8_t *)propval, size);
- if (rval == 0) {
- /* Invalid character in value string */
- qla_printk(KERN_INFO, ha,
- "%s(): %s Invalid hex string for property\n",
- __func__,
- propname);
- qla_printk(KERN_INFO, ha,
- " Invalid string - %s\n",
- propstr);
- }
-
- return (rval);
-}
-
-/*
- * qla2x00_find_propname
- * Get property in database.
- *
- * Input:
- * ha = adapter structure pointer.
- * db = pointer to database
- * propstr = pointer to dest array for string
- * propname = name of property to search for.
- * siz = size of property
- *
- * Returns:
- * 0 = no property
- * size = index of property
- *
- * Context:
- * Kernel context.
- */
-static int
-qla2x00_find_propname(scsi_qla_host_t *ha,
- char *propname, char *propstr,
- char *db, int siz)
-{
- char *cp;
-
- /* find the specified string */
- if (db) {
- /* find the property name */
- if ((cp = strstr(db,propname)) != NULL) {
- while ((*cp) && *cp != '=')
- cp++;
- if (*cp) {
- strncpy(propstr, cp, siz+1);
- propstr[siz+1] = '\0';
- DEBUG(printk("qla2x00_find_propname: found "
- "property = {%s}\n",
- propstr);)
- return (siz); /* match */
- }
- }
- }
-
- return (0);
-}
-
-#if 0
-/*
- * qla2x00_get_prop_16chars
- * Get an 8-byte property value for the specified property name by
- * converting from the property string found in the configuration file.
- * The resulting converted value is in big endian format (MSB at byte0).
- *
- * Input:
- * ha = adapter state pointer.
- * propname = property name pointer.
- * propval = pointer to location for the converted property val.
- * db = pointer to database
- *
- * Returns:
- * 0 = value returned successfully.
- *
- * Context:
- * Kernel context.
- */
-static int
-qla2x00_get_prop_16chars(scsi_qla_host_t *ha,
- char *propname, char *propval, char *db)
-{
- char *propstr;
- int i, k;
- int rval;
- uint8_t nval;
- uint8_t *pchar;
- uint8_t *ret_byte;
- uint8_t *tmp_byte;
- uint8_t *retval = (uint8_t*)propval;
- uint8_t tmpval[8] = {0, 0, 0, 0, 0, 0, 0, 0};
- uint16_t max_byte_cnt = 8; /* 16 chars = 8 bytes */
- uint16_t max_strlen = 16;
- static char buf[LINESIZE];
-
- rval = qla2x00_find_propname(ha, propname, buf, db, max_strlen);
-
- propstr = &buf[0];
- if (*propstr == '=')
- propstr++; /* ignore equal sign */
-
- if (rval == 0) {
- return (1);
- }
-
- /* Convert string to numbers. */
- pchar = (uint8_t *)propstr;
- tmp_byte = (uint8_t *)tmpval;
-
- rval = 0;
- for (i = 0; i < max_strlen; i++) {
- /*
- * Check for invalid character, two at a time,
- * then convert them starting with first byte.
- */
-
- if ((pchar[i] >= '0') && (pchar[i] <= '9')) {
- nval = pchar[i] - '0';
- } else if ((pchar[i] >= 'A') && (pchar[i] <= 'F')) {
- nval = pchar[i] - 'A' + 10;
- } else if ((pchar[i] >= 'a') && (pchar[i] <= 'f')) {
- nval = pchar[i] - 'a' + 10;
- } else {
- /* invalid character */
- rval = 1;
- break;
- }
-
- if (i & BIT_0) {
- *tmp_byte = *tmp_byte | nval;
- tmp_byte++;
- } else {
- *tmp_byte = *tmp_byte | nval << 4;
- }
- }
-
- if (rval != 0) {
- /* Encountered invalid character. */
- return (rval);
- }
-
- /* Copy over the converted value. */
- ret_byte = retval;
- tmp_byte = tmpval;
-
- i = max_byte_cnt;
- k = 0;
- while (i--) {
- *ret_byte++ = *tmp_byte++;
- }
-
- /* big endian retval[0]; */
- return (0);
-}
-
-/*
-* qla2x00_get_properties
-* Find all properties for the specified adapeter in
-* command line.
-*
-* Input:
-* ha = adapter block pointer.
-* cmdline = pointer to command line string
-*
-* Context:
-* Kernel context.
-*/
-static void
-qla2x00_get_properties(scsi_qla_host_t *ha, char *cmdline)
-{
- int rval;
- static char propbuf[LINESIZE];
- uint8_t fc_name[8];
-
- /* Adapter FC node names. */
- sprintf(propbuf, "scsi-qla%d-adapter-node", (int) ha->instance);
- rval = qla2x00_get_prop_16chars(ha, propbuf, fc_name, cmdline);
- if (rval == QLA_SUCCESS)
- memcpy(ha->init_cb->node_name, fc_name, WWN_SIZE);
-
- /* DG 04/07 check portname of adapter */
- sprintf(propbuf, "scsi-qla%d-adapter-port", (int)ha->instance);
- rval = qla2x00_get_prop_16chars(ha, propbuf, fc_name, cmdline);
- if (rval == QLA_SUCCESS &&
- memcmp(ha->init_cb->port_name, fc_name, WWN_SIZE)) {
- /*
- * Adapter port name is WWN, and cannot be changed.
- * Inform users of the mismatch, then just continue driver
- * loading using the original adapter port name in NVRAM.
- */
- qla_printk(KERN_WARNING, ha,
- "Found mismatch in adapter port names.\n");
- qla_printk(KERN_INFO, ha,
- " qla%ld port name found in NVRAM -> "
- "%02x%02x%02x%02x%02x%02x%02x%02x\n",
- ha->instance,
- ha->init_cb->port_name[0],
- ha->init_cb->port_name[1],
- ha->init_cb->port_name[2],
- ha->init_cb->port_name[3],
- ha->init_cb->port_name[4],
- ha->init_cb->port_name[5],
- ha->init_cb->port_name[6],
- ha->init_cb->port_name[7]);
- qla_printk(KERN_INFO, ha,
- " qla%ld port name found on command line -> "
- "%02x%02x%02x%02x%02x%02x%02x%02x\n",
- ha->instance,
- fc_name[0], fc_name[1], fc_name[2], fc_name[3],
- fc_name[4], fc_name[5], fc_name[6], fc_name[7]);
- qla_printk(KERN_INFO, ha,
- " Using port name from NVRAM.\n");
- }
-
- qla2x00_cfg_persistent_binding(ha);
-}
-
-/*
- * qla2x00_cfg_persistent_binding
- * Get driver configuration file target persistent binding entries.
- *
- * Input:
- * ha = adapter block pointer.
- *
- * Context:
- * Kernel context.
- */
-static void
-qla2x00_cfg_persistent_binding(scsi_qla_host_t *ha)
-{
- int rval;
- static char propbuf[LINESIZE];
- char *cmdline = ha->cmdline;
- uint16_t tgt;
- port_id_t d_id;
- uint8_t portid[3];
- uint8_t port_name[8];
-
- for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
- if (ha->binding_type == BIND_BY_PORT_ID) {
- sprintf(propbuf, "scsi-qla%d-tgt-%d-di-0-pid",
- (int)ha->instance, tgt);
- rval = qla2x00_get_prop_xstr(ha,
- propbuf, portid, sizeof(portid));
- if (rval != sizeof(portid))
- continue;
-
- memset(&d_id, 0, sizeof(port_id_t));
- d_id.r.d_id[0] = portid[2];
- d_id.r.d_id[1] = portid[1];
- d_id.r.d_id[2] = portid[0];
- } else {
- sprintf(propbuf, "scsi-qla%d-tgt-%d-di-0-port",
- (int)ha->instance, tgt);
- rval = qla2x00_get_prop_16chars(ha,
- propbuf, port_name, cmdline);
- if (rval != QLA_SUCCESS)
- continue;
-
- /* Fallthru since port_name already populated */
- }
-
- /*
- * Create target context for device.
- */
- if (ha->binding_type == BIND_BY_PORT_ID) {
- qla2x00_persistent_bind(ha, NULL, NULL, &d_id, tgt);
- } else {
- qla2x00_persistent_bind(ha, NULL, port_name, NULL, tgt);
- }
- }
-}
-
-/*
- * qla2x00_persistent_bind
- * Allocates target and fcport.
- *
- * Input:
- * ha: adapter state pointer.
- * node_name: node name pointer.
- * port_name: port name pointer.
- * d_id: port ID pointer.
- * tgt: OS target number.
- *
- * Returns:
- * success = target queue pointer.
- * failure = NULL.
- *
- * Context:
- * Kernel context.
- */
-static os_tgt_t *
-qla2x00_persistent_bind(scsi_qla_host_t *ha, uint8_t *node_name,
- uint8_t *port_name, port_id_t *d_id, uint16_t tgt)
-{
- os_tgt_t *tq;
- uint16_t tgt2;
-
- /*
- * Check for duplicates.
- */
- for (tgt2 = 0; tgt2 < MAX_TARGETS; tgt2++) {
- if ((tq = TGT_Q(ha, tgt2)) == NULL) {
- continue;
- }
-
- if (ha->binding_type == BIND_BY_PORT_ID) {
- if (tq->d_id.b24 != d_id->b24) {
- continue;
- }
- } else if (memcmp(tq->port_name, port_name, WWN_SIZE) != 0) {
- continue;
- }
-
- qla_printk(KERN_WARNING, ha,
- "Duplicate persistent bindings found for "
- "WWPN: %02x%02x%02x%02x%02x%02x%02x%02x.\n",
- port_name[0], port_name[1], port_name[2], port_name[3],
- port_name[4], port_name[5], port_name[6], port_name[7]);
-
- return (tq);
- }
-
- tq = qla2x00_tgt_alloc(ha, tgt);
- if (tq == NULL) {
- return (NULL);
- }
-
- if (node_name != NULL) {
- memcpy(tq->node_name, node_name, WWN_SIZE);
- }
- if (port_name != NULL) {
- memcpy(tq->port_name, port_name, WWN_SIZE);
- }
- if (d_id != NULL) {
- tq->d_id.b24 = d_id->b24;
- }
-
- return (tq);
-}
-#endif
-