scsi_serial.c revision ab261e1226efac3d47b2dc4a0e0165aeb8bb528e
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) IBM Corp. 2003
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Author: Patrick Mansfield<patmans@us.ibm.com>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This program is free software: you can redistribute it and/or modify
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * it under the terms of the GNU General Public License as published by
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * the Free Software Foundation, either version 2 of the License, or
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * (at your option) any later version.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This program is distributed in the hope that it will be useful,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * GNU General Public License for more details.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * You should have received a copy of the GNU General Public License
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * along with this program. If not, see <http://www.gnu.org/licenses/>.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/types.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/ioctl.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/stat.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdio.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <errno.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <string.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <fcntl.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdlib.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <unistd.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <syslog.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <time.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <inttypes.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <scsi/scsi.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <scsi/sg.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <linux/types.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <linux/bsg.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "libudev.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "libudev-private.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "scsi.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "scsi_id.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * A priority based list of id, naa, and binary/ascii for the identifier
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * descriptor in VPD page 0x83.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Brute force search for a match starting with the first value in the
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * following id_search_list. This is not a performance issue, since there
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * is normally one or some small number of descriptors.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic const struct scsi_id_search_values id_search_list[] = {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_TGTGROUP, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_BINARY },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_ASCII },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_BINARY },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_ASCII },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Devices already exist using NAA values that are now marked
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * reserved. These should not conflict with other values, or it is
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * a bug in the device. As long as we find the IEEE extended one
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * first, we really don't care what other ones are used. Using
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * don't care here means that a device that returns multiple
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * non-IEEE descriptors in a random order will get different
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * names.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic const char hex_str[]="0123456789abcdef";
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Values returned in the result/status, only the ones used by the code
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * are used here.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DID_NO_CONNECT 0x01 /* Unable to connect before timeout */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DID_BUS_BUSY 0x02 /* Bus remain busy until timeout */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DID_TIME_OUT 0x03 /* Timed out for some other reason */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DRIVER_TIMEOUT 0x06
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define DRIVER_SENSE 0x08 /* Sense_buffer has been set */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* The following "category" function returns one of the following */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_CLEAN 0 /* No errors or other information */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_TIMEOUT 3
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_NOTSUPPORTED 5 /* Illegal / unsupported command */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#define SG_ERR_CAT_OTHER 99 /* Some other error/warning */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int do_scsi_page80_inquiry(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, int fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *serial, char *serial_short, int max_len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int sg_err_category_new(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int scsi_status, int msg_status, int
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen host_status, int driver_status, const
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char *sense_buffer, int sb_len)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen scsi_status &= 0x7e;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * XXX change to return only two values - failed or OK.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!scsi_status && !host_status && !driver_status)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_CLEAN;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((scsi_status == SCSI_CHECK_CONDITION) ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (scsi_status == SCSI_COMMAND_TERMINATED) ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ((driver_status & 0xf) == DRIVER_SENSE)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sense_buffer && (sb_len > 2)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int sense_key;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char asc;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sense_buffer[0] & 0x2) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sense_key = sense_buffer[1] & 0xf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen asc = sense_buffer[2];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sense_key = sense_buffer[2] & 0xf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen asc = (sb_len > 12) ? sense_buffer[12] : 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sense_key == RECOVERED_ERROR)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_RECOVERED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (sense_key == UNIT_ATTENTION) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (0x28 == asc)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_MEDIA_CHANGED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (0x29 == asc)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_RESET;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (sense_key == ILLEGAL_REQUEST) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_NOTSUPPORTED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_SENSE;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (host_status) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((host_status == DID_NO_CONNECT) ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (host_status == DID_BUS_BUSY) ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen (host_status == DID_TIME_OUT))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_TIMEOUT;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (driver_status) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (driver_status == DRIVER_TIMEOUT)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_TIMEOUT;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return SG_ERR_CAT_OTHER;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int sg_err_category3(struct udev *udev, struct sg_io_hdr *hp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return sg_err_category_new(udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen hp->status, hp->msg_status,
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen hp->host_status, hp->driver_status,
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen hp->sbp, hp->sb_len_wr);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen}
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int sg_err_category4(struct udev *udev, struct sg_io_v4 *hp)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return sg_err_category_new(udev, hp->device_status, 0,
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen hp->transport_status, hp->driver_status,
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen (unsigned char *)(uintptr_t)hp->response,
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen hp->response_len);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int scsi_dump_sense(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi,
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen unsigned char *sense_buffer, int sb_len)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int s;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int code;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int sense_class;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int sense_key;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int asc, ascq;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#ifdef DUMP_SENSE
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char out_buffer[256];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int i, j;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
52d629010db73a9466c359201916494bd55186d1Tom Gundersen /*
52d629010db73a9466c359201916494bd55186d1Tom Gundersen * Figure out and print the sense key, asc and ascq.
52d629010db73a9466c359201916494bd55186d1Tom Gundersen *
52d629010db73a9466c359201916494bd55186d1Tom Gundersen * If you want to suppress these for a particular drive model, add
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * a black list entry in the scsi_id config file.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * XXX We probably need to: lookup the sense/asc/ascq in a retry
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * table, and if found return 1 (after dumping the sense, asc, and
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * ascq). So, if/when we get something like a power on/reset,
52d629010db73a9466c359201916494bd55186d1Tom Gundersen * we'll retry the command.
52d629010db73a9466c359201916494bd55186d1Tom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sb_len < 1) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sense buffer empty\n", dev_scsi->kernel);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sense_class = (sense_buffer[0] >> 4) & 0x07;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen code = sense_buffer[0] & 0xf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sense_class == 7) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * extended sense data.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen s = sense_buffer[7] + 8;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sb_len < s) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sense buffer too small %d bytes, %d bytes too short\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, sb_len, s - sb_len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((code == 0x0) || (code == 0x1)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sense_key = sense_buffer[2] & 0xf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (s < 14) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Possible?
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sense result too" " small %d bytes\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen asc = sense_buffer[12];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ascq = sense_buffer[13];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((code == 0x2) || (code == 0x3)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sense_key = sense_buffer[1] & 0xf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen asc = sense_buffer[2];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ascq = sense_buffer[3];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: invalid sense code 0x%x\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, code);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, sense_key, asc, ascq);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sb_len < 4) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sense buffer too small %d bytes, %d bytes too short\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, sb_len, 4 - sb_len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sense_buffer[0] < 15)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: old sense key: 0x%x\n", dev_scsi->kernel, sense_buffer[0] & 0x0f);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sense = %2x %2x\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, sense_buffer[0], sense_buffer[2]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: non-extended sense class %d code 0x%0x\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, sense_class, code);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#ifdef DUMP_SENSE
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0, j = 0; (i < s) && (j < 254); i++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen out_buffer[j++] = hex_str[(sense_buffer[i] & 0xf0) >> 4];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen out_buffer[j++] = hex_str[sense_buffer[i] & 0x0f];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen out_buffer[j++] = ' ';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen out_buffer[j] = '\0';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sense dump:\n", dev_scsi->kernel);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: %s\n", dev_scsi->kernel, out_buffer);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#endif
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int scsi_dump(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, struct sg_io_hdr *io)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!io->status && !io->host_status && !io->msg_status &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !io->driver_status) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Impossible, should not be called.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: called with no error\n", __FUNCTION__);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (io->status == SCSI_CHECK_CONDITION)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return scsi_dump_sense(udev, dev_scsi, io->sbp, io->sb_len_wr);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int scsi_dump_v4(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, struct sg_io_v4 *io)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!io->device_status && !io->transport_status &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !io->driver_status) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Impossible, should not be called.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: called with no error\n", __FUNCTION__);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, io->driver_status, io->transport_status,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io->device_status);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (io->device_status == SCSI_CHECK_CONDITION)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return scsi_dump_sense(udev, dev_scsi, (unsigned char *)(uintptr_t)io->response,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io->response_len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int scsi_inquiry(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, int fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char evpd, unsigned char page,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char *buf, unsigned int buflen)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char inq_cmd[INQUIRY_CMDLEN] =
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { INQUIRY_CMD, evpd, page, 0, buflen, 0 };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char sense[SENSE_BUFF_LEN];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen void *io_buf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct sg_io_v4 io_v4;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct sg_io_hdr io_hdr;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int retry = 3; /* rather random */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (buflen > SCSI_INQ_BUFF_LEN) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("buflen %d too long\n", buflen);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenresend:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (dev_scsi->use_sg == 4) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(&io_v4, 0, sizeof(struct sg_io_v4));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.guard = 'Q';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.protocol = BSG_PROTOCOL_SCSI;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.request_len = sizeof(inq_cmd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.request = (uintptr_t)inq_cmd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.max_response_len = sizeof(sense);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.response = (uintptr_t)sense;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.din_xfer_len = buflen;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_v4.din_xferp = (uintptr_t)buf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_buf = (void *)&io_v4;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.interface_id = 'S';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.cmd_len = sizeof(inq_cmd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.mx_sb_len = sizeof(sense);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.dxfer_len = buflen;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.dxferp = buf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.cmdp = inq_cmd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.sbp = sense;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_hdr.timeout = DEF_TIMEOUT;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen io_buf = (void *)&io_hdr;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = ioctl(fd, SG_IO, io_buf);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (retval < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((errno == EINVAL || errno == ENOSYS) && dev_scsi->use_sg == 4) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->use_sg = 3;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto resend;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: ioctl failed: %s\n", dev_scsi->kernel, strerror(errno));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto error;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (dev_scsi->use_sg == 4)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = sg_err_category4(udev, io_buf);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = sg_err_category3(udev, io_buf);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen switch (retval) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SG_ERR_CAT_NOTSUPPORTED:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen buf[1] = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* Fallthrough */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SG_ERR_CAT_CLEAN:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SG_ERR_CAT_RECOVERED:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen default:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (dev_scsi->use_sg == 4)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = scsi_dump_v4(udev, dev_scsi, io_buf);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = scsi_dump(udev, dev_scsi, io_buf);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!retval) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = buflen;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (retval > 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (--retry > 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto resend;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenerror:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (retval < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, evpd, page);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* Get list of supported EVPD pages */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int do_scsi_page0_inquiry(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, int fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char *buffer, unsigned int len)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(buffer, 0, len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = scsi_inquiry(udev, dev_scsi, fd, 1, 0x0, buffer, len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (retval < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (buffer[1] != 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: page 0 not available.\n", dev_scsi->kernel);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (buffer[3] > len) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: page 0 buffer too long %d\n", dev_scsi->kernel, buffer[3]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Following check is based on code once included in the 2.5.x
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * kernel.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Some ill behaved devices return the standard inquiry here
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * rather than the evpd data, snoop the data to verify.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (buffer[3] > MODEL_LENGTH) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * If the vendor id appears in the page assume the page is
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * invalid.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!strncmp((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: invalid page0 data\n", dev_scsi->kernel);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * The caller checks that serial is long enough to include the vendor +
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * model.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int prepend_vendor_model(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, char *serial)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int ind;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strncpy(serial, dev_scsi->vendor, VENDOR_LENGTH);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strncat(serial, dev_scsi->model, MODEL_LENGTH);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ind = strlen(serial);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This is not a complete check, since we are using strncat/cpy
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * above, ind will never be too large.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: expected length %d, got length %d\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return ind;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * serial number.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int check_fill_0x83_id(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char *page_83,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const struct scsi_id_search_values
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *id_search, char *serial, char *serial_short,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int max_len, char *wwn,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *wwn_vendor_extension, char *tgpt_group)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int i, j, s, len;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * ASSOCIATION must be with the device (value 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * or with the target port for SCSI_ID_TGTPORT
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((page_83[1] & 0x30) == 0x10) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (id_search->id_type != SCSI_ID_TGTGROUP)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((page_83[1] & 0x30) != 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((page_83[1] & 0x0f) != id_search->id_type)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Possibly check NAA sub-type.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
7141e4f62c3f220872df3114c42d9e4b9525e43eTom Gundersen if ((id_search->naa_type != SCSI_ID_NAA_DONT_CARE) &&
7141e4f62c3f220872df3114c42d9e4b9525e43eTom Gundersen (id_search->naa_type != (page_83[4] & 0xf0) >> 4))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Check for matching code set - ASCII or BINARY.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((page_83[0] & 0x0f) != id_search->code_set)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen /*
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen * page_83[3]: identifier length
2a2137401b3aef20618308d2b2694e21b0124f89Tom Gundersen */
2a2137401b3aef20618308d2b2694e21b0124f89Tom Gundersen len = page_83[3];
2a2137401b3aef20618308d2b2694e21b0124f89Tom Gundersen if ((page_83[0] & 0x0f) != SCSI_ID_ASCII)
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * If not ASCII, use two bytes for each binary value.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen len *= 2;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Add one byte for the NUL termination, and one for the id_type.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen len += 2;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen len += VENDOR_LENGTH + MODEL_LENGTH;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (max_len < len) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: length %d too short - need %d\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->kernel, max_len, len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (id_search->id_type == SCSI_ID_TGTGROUP && tgpt_group != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned int group;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen group = ((unsigned int)page_83[6] << 8) | page_83[7];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sprintf(tgpt_group,"%x", group);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[0] = hex_str[id_search->id_type];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * For SCSI_ID_VENDOR_SPECIFIC prepend the vendor and model before
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * the id since it is not unique across all vendors and models,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * this differs from SCSI_ID_T10_VENDOR, where the vendor is
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * included in the identifier.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (prepend_vendor_model(udev, dev_scsi, &serial[1]) < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen i = 4; /* offset to the start of the identifier */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen s = j = strlen(serial);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * ASCII descriptor.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen while (i < (4 + page_83[3]))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[j++] = page_83[i++];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Binary descriptor, convert to ASCII, using two bytes of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * ASCII for each byte in the page_83.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen while (i < (4 + page_83[3])) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[j++] = hex_str[page_83[i] & 0x0f];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen i++;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strcpy(serial_short, &serial[s]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (id_search->id_type == SCSI_ID_NAA && wwn != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strncpy(wwn, &serial[s], 16);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (wwn_vendor_extension != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strncpy(wwn_vendor_extension, &serial[s + 16], 16);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* Extract the raw binary from VPD 0x83 pre-SPC devices */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int check_fill_0x83_prespc3(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char *page_83,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const struct scsi_id_search_values
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *id_search, char *serial, char *serial_short, int max_len)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int i, j;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[0] = hex_str[id_search->id_type];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* serial has been memset to zero before */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen j = strlen(serial); /* j = 1; */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; (i < page_83[3]) && (j < max_len-3); ++i) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[j++] = hex_str[(page_83[4+i] & 0xf0) >> 4];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[j++] = hex_str[ page_83[4+i] & 0x0f];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[max_len-1] = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strncpy(serial_short, serial, max_len-1);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* Get device identification VPD page */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int do_scsi_page83_inquiry(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, int fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *serial, char *serial_short, int len,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *unit_serial_number, char *wwn,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *wwn_vendor_extension, char *tgpt_group)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned int id_ind, j;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char page_83[SCSI_INQ_BUFF_LEN];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* also pick up the page 80 serial number */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen do_scsi_page80_inquiry(udev, dev_scsi, fd, NULL, unit_serial_number, MAX_SERIAL_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(page_83, 0, SCSI_INQ_BUFF_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen SCSI_INQ_BUFF_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (retval < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (page_83[1] != PAGE_83) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: Invalid page 0x83\n", dev_scsi->kernel);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * XXX Some devices (IBM 3542) return all spaces for an identifier if
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * the LUN is not actually configured. This leads to identifiers of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * the form: "1 ".
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * The SCSI-2 page 83 format returns an IEEE WWN in binary
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * encoded hexi-decimal in the 16 bytes following the initial
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * 4-byte page 83 reply header.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * of an Identification descriptor. The 3rd byte of the first
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Identification descriptor is a reserved (BSZ) byte field.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Reference the 7th byte of the page 83 reply to determine
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * whether the reply is compliant with SCSI-2 or SPC-2/3
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * specifications. A zero value in the 7th byte indicates
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * an SPC-2/3 conformant reply, (i.e., the reserved field of the
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * first Identification descriptor). This byte will be non-zero
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * for a SCSI-2 conformant page 83 reply from these EMC
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Symmetrix models since the 7th byte of the reply corresponds
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * 0x006048.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (page_83[6] != 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return check_fill_0x83_prespc3(udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi, page_83, id_search_list,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial, serial_short, len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Search for a match in the prioritized id_search_list - since WWN ids
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * come first we can pick up the WWN in check_fill_0x83_id().
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (id_ind = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen id_ind++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Examine each descriptor returned. There is normally only
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * one or a small number of descriptors.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (j = 4; j <= (unsigned int)page_83[3] + 3; j += page_83[j + 3] + 4) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = check_fill_0x83_id(udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi, &page_83[j],
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen &id_search_list[id_ind],
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial, serial_short, len,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen wwn, wwn_vendor_extension,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen tgpt_group);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!retval)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (retval < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Get device identification VPD page for older SCSI-2 device which is not
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * compliant with either SPC-2 or SPC-3 format.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Return the hard coded error code value 2 if the page 83 reply is not
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * conformant to the SCSI-2 format.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int do_scsi_page83_prespc3_inquiry(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, int fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *serial, char *serial_short, int len)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int i, j;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char page_83[SCSI_INQ_BUFF_LEN];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(page_83, 0, SCSI_INQ_BUFF_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen if (retval < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (page_83[1] != PAGE_83) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: Invalid page 0x83\n", dev_scsi->kernel);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * The SCSI-2 page 83 format returns an IEEE WWN in binary
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * encoded hexi-decimal in the 16 bytes following the initial
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * 4-byte page 83 reply header.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * of an Identification descriptor. The 3rd byte of the first
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Identification descriptor is a reserved (BSZ) byte field.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Reference the 7th byte of the page 83 reply to determine
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * whether the reply is compliant with SCSI-2 or SPC-2/3
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * specifications. A zero value in the 7th byte indicates
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * an SPC-2/3 conformant reply, (i.e., the reserved field of the
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * first Identification descriptor). This byte will be non-zero
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * for a SCSI-2 conformant page 83 reply from these EMC
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Symmetrix models since the 7th byte of the reply corresponds
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * 0x006048.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (page_83[6] == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 2;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[0] = hex_str[id_search_list[0].id_type];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * The first four bytes contain data, not a descriptor.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen i = 4;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen j = strlen(serial);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Binary descriptor, convert to ASCII,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * using two bytes of ASCII for each byte
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * in the page_83.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen while (i < (page_83[3]+4)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[j++] = hex_str[page_83[i] & 0x0f];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen i++;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* Get unit serial number VPD page */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int do_scsi_page80_inquiry(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, int fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *serial, char *serial_short, int max_len)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int ser_ind;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int i;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int len;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char buf[SCSI_INQ_BUFF_LEN];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(buf, 0, SCSI_INQ_BUFF_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (retval < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (buf[1] != PAGE_80) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: Invalid page 0x80\n", dev_scsi->kernel);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
85091685af65831f379580c75b40776c20e245eeTom Gundersen len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (max_len < len) {
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen log_debug("%s: length %d too short - need %d\n",
5a917c064b23c1b8a12d6abd2f9f31c575ddebc6Tom Gundersen dev_scsi->kernel, max_len, len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * specific type where we prepend '0' + vendor + model.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen len = buf[3];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (serial != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[0] = 'S';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ser_ind = prepend_vendor_model(udev, dev_scsi, &serial[1]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ser_ind < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ser_ind++; /* for the leading 'S' */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 4; i < len + 4; i++, ser_ind++)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial[ser_ind] = buf[i];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (serial_short != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memcpy(serial_short, &buf[4], len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen serial_short[len] = '\0';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint scsi_std_inquiry(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, const char *devname)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char buf[SCSI_INQ_BUFF_LEN];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct stat statbuf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int err = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd = open(devname, O_RDONLY | O_NONBLOCK);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("scsi_id: cannot open %s: %s\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen devname, strerror(errno));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fstat(fd, &statbuf) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("scsi_id: cannot stat %s: %s\n",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen devname, strerror(errno));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen err = 2;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sprintf(dev_scsi->kernel,"%d:%d", major(statbuf.st_rdev),
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen minor(statbuf.st_rdev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(buf, 0, SCSI_INQ_BUFF_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen err = scsi_inquiry(udev, dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (err < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen err = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memcpy(dev_scsi->vendor, buf + 8, 8);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->vendor[8] = '\0';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memcpy(dev_scsi->model, buf + 16, 16);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->model[16] = '\0';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memcpy(dev_scsi->revision, buf + 32, 4);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->revision[4] = '\0';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sprintf(dev_scsi->type,"%x", buf[0] & 0x1f);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenout:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return err;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint scsi_get_serial(struct udev *udev,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct scsi_id_device *dev_scsi, const char *devname,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int page_code, int len)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen{
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned char page0[SCSI_INQ_BUFF_LEN];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int cnt;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int ind;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memset(dev_scsi->serial, 0, len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen srand((unsigned int)getpid());
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (cnt = 20; cnt > 0; cnt--) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct timespec duration;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd = open(devname, O_RDONLY | O_NONBLOCK);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd >= 0 || errno != EBUSY)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen duration.tv_sec = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen duration.tv_nsec = (200 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen nanosleep(&duration, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (page_code == PAGE_80) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (page_code == PAGE_83) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (page_code == PAGE_83_PRE_SPC3) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (retval) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Fallback to servicing a SPC-2/3 compliant page 83
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * inquiry if the page 83 reply format does not
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * conform to pre-SPC3 expectations.
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen */
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen if (retval == 2) {
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) {
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen retval = 1;
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (page_code != 0x00) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("%s: unsupported page code 0x%d\n", dev_scsi->kernel, page_code);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Get page 0, the page of the pages. By default, try from best to
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * worst of supported pages: 0x83 then 0x80.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (do_scsi_page0_inquiry(udev, dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Don't try anything else. Black list if a specific page
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * should be used for this vendor+model, or maybe have an
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * optional fall-back to page 0x80 or page 0x83.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (ind = 4; ind <= page0[3] + 3; ind++)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (page0[ind] == PAGE_83)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!do_scsi_page83_inquiry(udev, dev_scsi, fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Success
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 0;
afcac065c0f649ebcf0f450475a8d7c3bc776d14Tom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (ind = 4; ind <= page0[3] + 3; ind++)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (page0[ind] == PAGE_80)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!do_scsi_page80_inquiry(udev, dev_scsi, fd,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_scsi->serial, dev_scsi->serial_short, len)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Success
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto completed;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen retval = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersencompleted:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return retval;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen