4 * Home page of code is: http://www.smartmontools.org
6 * Copyright (C) 2007 Sergey Svishchev
8 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include <sys/types.h>
18 #if defined(linux) || defined(__linux__)
19 # include <sys/ioctl.h>
20 # ifdef HAVE_LINUX_COMPILER_H
21 # include <linux/compiler.h>
23 # if defined(HAVE_LINUX_CCISS_IOCTL_H)
24 # include <linux/cciss_ioctl.h>
27 # include <asm/byteorder.h>
29 # define be32toh __be32_to_cpu
31 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
32 # include <sys/endian.h>
33 # include CISS_LOCATION
42 const char * cciss_cpp_cvsid
= "$Id: cciss.cpp 4858 2018-12-16 17:59:59Z chrfranke $"
45 typedef struct _ReportLUNdata_struct
47 uint32_t LUNListLength
; /* always big-endian */
49 uint8_t LUN
[CISS_MAX_LUN
][8];
50 } ReportLunData_struct
;
52 /* Structure/defines of Report Physical LUNS of drive */
54 #define CISS_MAX_LUN 16
56 #define CISS_MAX_PHYS_LUN 1024
57 #define CISS_REPORT_PHYS 0xc3
59 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
60 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
62 static int cciss_getlun(int device
, int target
, unsigned char *physlun
, int report
);
63 static int cciss_sendpassthru(unsigned int cmdtype
, unsigned char *CDB
,
64 unsigned int CDBlen
, char *buff
,
65 unsigned int size
, unsigned int LunID
,
66 unsigned char *scsi3addr
, int fd
);
69 This is an interface that uses the cciss passthrough to talk to the SMART controller on
70 the HP system. The cciss driver provides a way to send SCSI cmds through the CCISS passthrough.
72 int cciss_io_interface(int device
, int target
, struct scsi_cmnd_io
* iop
, int report
)
74 unsigned char pBuf
[512] = {0};
75 unsigned char phylun
[8] = {0};
77 int len
= 0; // used later in the code.
79 int status
= cciss_getlun(device
, target
, phylun
, report
);
81 printf(" cciss_getlun(%d, %d) = 0x%x; scsi3addr: %02x %02x %02x %02x %02x %02x %02x %02x\n",
82 device
, target
, status
,
83 phylun
[0], phylun
[1], phylun
[2], phylun
[3], phylun
[4], phylun
[5], phylun
[6], phylun
[7]);
85 return -ENXIO
; /* give up, assume no device there */
88 status
= cciss_sendpassthru( 2, iop
->cmnd
, iop
->cmnd_len
, (char*) pBuf
, iBufLen
, 1, phylun
, device
);
93 printf(" status=0\n");
94 if (DXFER_FROM_DEVICE
== iop
->dxfer_dir
)
96 memcpy(iop
->dxferp
, pBuf
, iop
->dxfer_len
);
99 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
100 printf(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
101 (trunc
? " [only first 256 bytes shown]" : ""));
102 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
107 iop
->scsi_status
= status
& 0x7e; /* bits 0 and 7 used to be for vendors */
108 if (LSCSI_DRIVER_SENSE
== ((status
>> 24) & 0xf))
109 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
110 len
= (SEND_IOCTL_RESP_SENSE_LEN
< iop
->max_sense_len
) ?
111 SEND_IOCTL_RESP_SENSE_LEN
: iop
->max_sense_len
;
112 if ((SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) &&
113 iop
->sensep
&& (len
> 0))
115 memcpy(iop
->sensep
, pBuf
, len
);
116 iop
->resp_sense_len
= iBufLen
;
119 printf(" >>> Sense buffer, len=%d:\n", (int)len
);
120 dStrHex((const uint8_t *)pBuf
, len
, 1);
125 if (SCSI_STATUS_CHECK_CONDITION
== iop
->scsi_status
) {
126 printf(" status=%x: sense_key=%x asc=%x ascq=%x\n", status
& 0xff,
127 pBuf
[2] & 0xf, pBuf
[12], pBuf
[13]);
130 printf(" status=0x%x\n", status
);
132 if (iop
->scsi_status
> 0)
137 printf(" ioctl status=0x%x but scsi status=0, fail with ENXIO\n", status
);
138 return -ENXIO
; /* give up, assume no device there */
142 static int cciss_sendpassthru(unsigned int cmdtype
, unsigned char *CDB
,
143 unsigned int CDBlen
, char *buff
,
144 unsigned int size
, unsigned int LunID
,
145 unsigned char *scsi3addr
, int fd
)
148 IOCTL_Command_struct iocommand
;
150 memset(&iocommand
, 0, sizeof(iocommand
));
154 // To controller; nothing to do
156 else if (cmdtype
== 1)
158 iocommand
.LUN_info
.LogDev
.VolId
= LunID
;
159 iocommand
.LUN_info
.LogDev
.Mode
= 1;
161 else if (cmdtype
== 2)
163 memcpy(&iocommand
.LUN_info
.LunAddrBytes
,scsi3addr
,8);
164 iocommand
.LUN_info
.LogDev
.Mode
= 0;
168 fprintf(stderr
, "cciss_sendpassthru: bad cmdtype\n");
172 memcpy(&iocommand
.Request
.CDB
[0], CDB
, CDBlen
);
173 iocommand
.Request
.CDBLen
= CDBlen
;
174 iocommand
.Request
.Type
.Type
= TYPE_CMD
;
175 iocommand
.Request
.Type
.Attribute
= ATTR_SIMPLE
;
176 iocommand
.Request
.Type
.Direction
= XFER_READ
;
177 iocommand
.Request
.Timeout
= 0;
179 iocommand
.buf_size
= size
;
180 iocommand
.buf
= (unsigned char *)buff
;
182 if ((err
= ioctl(fd
, CCISS_PASSTHRU
, &iocommand
)))
184 fprintf(stderr
, "CCISS ioctl error %d (fd %d CDBLen %u buf_size %u)\n",
185 fd
, err
, CDBlen
, size
);
190 static int cciss_getlun(int device
, int target
, unsigned char *physlun
, int report
)
192 unsigned char CDB
[16]= {0};
193 ReportLunData_struct
*luns
;
194 int reportlunsize
= sizeof(*luns
) + CISS_MAX_PHYS_LUN
* 8;
197 luns
= (ReportLunData_struct
*)malloc(reportlunsize
);
199 memset(luns
, 0, reportlunsize
);
201 /* Get Physical LUN Info (for physical device) */
202 CDB
[0] = CISS_REPORT_PHYS
;
203 CDB
[6] = (reportlunsize
>> 24) & 0xFF; /* MSB */
204 CDB
[7] = (reportlunsize
>> 16) & 0xFF;
205 CDB
[8] = (reportlunsize
>> 8) & 0xFF;
206 CDB
[9] = reportlunsize
& 0xFF;
208 if ((ret
= cciss_sendpassthru(0, CDB
, 12, (char *)luns
, reportlunsize
, 0, NULL
, device
)))
217 unsigned char *stuff
= (unsigned char *)luns
;
219 pout("\n===== [%s] DATA START (BASE-16) =====\n", "LUN DATA");
220 for (i
=0; i
<(sizeof(_ReportLUNdata_struct
)+15)/16; i
++){
221 pout("%03d-%03d: ", 16*i
, 16*(i
+1)-1);
223 pout("%02x ",*stuff
++);
224 pout("%02x\n",*stuff
++);
226 pout("===== [%s] DATA END (%u Bytes) =====\n\n", "LUN DATA", (unsigned)sizeof(_ReportLUNdata_struct
));
229 if (target
>= 0 && target
< (int) be32toh(luns
->LUNListLength
) / 8)
231 memcpy(physlun
, luns
->LUN
[target
], 8);