]>
Commit | Line | Data |
---|---|---|
a37e7145 | 1 | /* |
2127e193 | 2 | * os_linux.cpp |
a37e7145 | 3 | * |
a86ec89e | 4 | * Home page of code is: http://www.smartmontools.org |
832b75ed | 5 | * |
a86ec89e | 6 | * Copyright (C) 2003-11 Bruce Allen |
cfbba5b9 | 7 | * Copyright (C) 2003-11 Doug Gilbert <dgilbert@interlog.com> |
ff28b140 | 8 | * Copyright (C) 2008-18 Christian Franke |
832b75ed | 9 | * |
d2e702cf GI |
10 | * Original AACRaid code: |
11 | * Copyright (C) 2014 Raghava Aditya <raghava.aditya@pmcs.com> | |
12 | * | |
13 | * Original Areca code: | |
14 | * Copyright (C) 2008-12 Hank Wu <hank@areca.com.tw> | |
15 | * Copyright (C) 2008 Oliver Bock <brevilo@users.sourceforge.net> | |
16 | * | |
17 | * Original MegaRAID code: | |
18 | * Copyright (C) 2008 Jordan Hargrave <jordan_hargrave@dell.com> | |
19 | * | |
20 | * 3ware code was derived from code that was: | |
832b75ed GG |
21 | * |
22 | * Written By: Adam Radford <linux@3ware.com> | |
23 | * Modifications By: Joel Jacobson <linux@3ware.com> | |
d2e702cf | 24 | * Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
832b75ed GG |
25 | * Brad Strand <linux@3ware.com> |
26 | * | |
27 | * Copyright (C) 1999-2003 3ware Inc. | |
28 | * | |
ff28b140 | 29 | * Kernel compatibility By: Andre Hedrick <andre@suse.com> |
832b75ed GG |
30 | * Non-Copyright (C) 2000 Andre Hedrick <andre@suse.com> |
31 | * | |
32 | * Other ars of this file are derived from code that was | |
a37e7145 | 33 | * |
832b75ed GG |
34 | * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org> |
35 | * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org> | |
36 | * | |
ff28b140 | 37 | * SPDX-License-Identifier: GPL-2.0-or-later |
832b75ed GG |
38 | */ |
39 | ||
40 | // This file contains the linux-specific IOCTL parts of | |
41 | // smartmontools. It includes one interface routine for ATA devices, | |
42 | // one for SCSI devices, and one for ATA devices behind escalade | |
43 | // controllers. | |
44 | ||
a37e7145 GG |
45 | #include "config.h" |
46 | ||
832b75ed GG |
47 | #include <errno.h> |
48 | #include <fcntl.h> | |
49 | #include <glob.h> | |
ba59cff1 | 50 | |
2127e193 | 51 | #include <scsi/scsi.h> |
832b75ed GG |
52 | #include <scsi/scsi_ioctl.h> |
53 | #include <scsi/sg.h> | |
f9e10201 | 54 | #include <linux/bsg.h> |
832b75ed GG |
55 | #include <stdlib.h> |
56 | #include <string.h> | |
57 | #include <sys/ioctl.h> | |
58 | #include <sys/stat.h> | |
cfbba5b9 | 59 | #include <sys/utsname.h> |
832b75ed | 60 | #include <unistd.h> |
cfbba5b9 | 61 | #include <stddef.h> // for offsetof() |
2127e193 GI |
62 | #include <sys/uio.h> |
63 | #include <sys/types.h> | |
ee38a438 | 64 | #include <dirent.h> |
ff28b140 TL |
65 | #ifdef HAVE_SYS_SYSMACROS_H |
66 | // glibc 2.25: The inclusion of <sys/sysmacros.h> by <sys/types.h> is | |
67 | // deprecated. A warning is printed if major(), minor() or makedev() | |
68 | // is used but <sys/sysmacros.h> is not included. | |
832b75ed GG |
69 | #include <sys/sysmacros.h> |
70 | #endif | |
ff28b140 | 71 | #ifdef HAVE_LIBSELINUX |
2127e193 GI |
72 | #include <selinux/selinux.h> |
73 | #endif | |
832b75ed | 74 | |
832b75ed GG |
75 | #include "atacmds.h" |
76 | #include "os_linux.h" | |
77 | #include "scsicmds.h" | |
78 | #include "utility.h" | |
a37e7145 | 79 | #include "cciss.h" |
2127e193 | 80 | #include "megaraid.h" |
d2e702cf | 81 | #include "aacraid.h" |
ff28b140 | 82 | #include "nvmecmds.h" |
2127e193 GI |
83 | |
84 | #include "dev_interface.h" | |
85 | #include "dev_ata_cmd_set.h" | |
ee38a438 | 86 | #include "dev_areca.h" |
832b75ed | 87 | |
a86ec89e GI |
88 | // "include/uapi/linux/nvme_ioctl.h" from Linux kernel sources |
89 | #include "linux_nvme_ioctl.h" // nvme_passthru_cmd, NVME_IOCTL_ADMIN_CMD | |
90 | ||
832b75ed GG |
91 | #ifndef ENOTSUP |
92 | #define ENOTSUP ENOSYS | |
93 | #endif | |
ba59cff1 | 94 | |
832b75ed GG |
95 | #define ARGUSED(x) ((void)(x)) |
96 | ||
ff28b140 | 97 | const char * os_linux_cpp_cvsid = "$Id: os_linux.cpp 4854 2018-12-11 20:32:29Z chrfranke $" |
cfbba5b9 | 98 | OS_LINUX_H_CVSID; |
ee38a438 | 99 | extern unsigned char failuretest_permissive; |
a37e7145 | 100 | |
2127e193 | 101 | namespace os_linux { // No need to publish anything, name provided for Doxygen |
a37e7145 | 102 | |
2127e193 GI |
103 | ///////////////////////////////////////////////////////////////////////////// |
104 | /// Shared open/close routines | |
a37e7145 | 105 | |
2127e193 GI |
106 | class linux_smart_device |
107 | : virtual public /*implements*/ smart_device | |
108 | { | |
109 | public: | |
110 | explicit linux_smart_device(int flags, int retry_flags = -1) | |
111 | : smart_device(never_called), | |
112 | m_fd(-1), | |
113 | m_flags(flags), m_retry_flags(retry_flags) | |
114 | { } | |
a37e7145 | 115 | |
2127e193 | 116 | virtual ~linux_smart_device() throw(); |
a37e7145 | 117 | |
2127e193 | 118 | virtual bool is_open() const; |
a37e7145 | 119 | |
2127e193 | 120 | virtual bool open(); |
a37e7145 | 121 | |
2127e193 | 122 | virtual bool close(); |
a37e7145 | 123 | |
2127e193 GI |
124 | protected: |
125 | /// Return filedesc for derived classes. | |
126 | int get_fd() const | |
127 | { return m_fd; } | |
832b75ed | 128 | |
ee38a438 GI |
129 | void set_fd(int fd) |
130 | { m_fd = fd; } | |
131 | ||
2127e193 GI |
132 | private: |
133 | int m_fd; ///< filedesc, -1 if not open. | |
134 | int m_flags; ///< Flags for ::open() | |
135 | int m_retry_flags; ///< Flags to retry ::open(), -1 if no retry | |
136 | }; | |
ba59cff1 | 137 | |
2127e193 GI |
138 | linux_smart_device::~linux_smart_device() throw() |
139 | { | |
140 | if (m_fd >= 0) | |
141 | ::close(m_fd); | |
832b75ed GG |
142 | } |
143 | ||
2127e193 GI |
144 | bool linux_smart_device::is_open() const |
145 | { | |
146 | return (m_fd >= 0); | |
832b75ed GG |
147 | } |
148 | ||
2127e193 GI |
149 | bool linux_smart_device::open() |
150 | { | |
151 | m_fd = ::open(get_dev_name(), m_flags); | |
152 | ||
153 | if (m_fd < 0 && errno == EROFS && m_retry_flags != -1) | |
154 | // Retry | |
155 | m_fd = ::open(get_dev_name(), m_retry_flags); | |
156 | ||
157 | if (m_fd < 0) { | |
158 | if (errno == EBUSY && (m_flags & O_EXCL)) | |
159 | // device is locked | |
160 | return set_err(EBUSY, | |
161 | "The requested controller is used exclusively by another process!\n" | |
162 | "(e.g. smartctl or smartd)\n" | |
163 | "Please quit the impeding process or try again later..."); | |
164 | return set_err((errno==ENOENT || errno==ENOTDIR) ? ENODEV : errno); | |
832b75ed | 165 | } |
a37e7145 | 166 | |
2127e193 GI |
167 | if (m_fd >= 0) { |
168 | // sets FD_CLOEXEC on the opened device file descriptor. The | |
169 | // descriptor is otherwise leaked to other applications (mail | |
170 | // sender) which may be considered a security risk and may result | |
171 | // in AVC messages on SELinux-enabled systems. | |
172 | if (-1 == fcntl(m_fd, F_SETFD, FD_CLOEXEC)) | |
173 | // TODO: Provide an error printing routine in class smart_interface | |
174 | pout("fcntl(set FD_CLOEXEC) failed, errno=%d [%s]\n", errno, strerror(errno)); | |
832b75ed | 175 | } |
a37e7145 | 176 | |
2127e193 | 177 | return true; |
832b75ed GG |
178 | } |
179 | ||
2127e193 GI |
180 | // equivalent to close(file descriptor) |
181 | bool linux_smart_device::close() | |
182 | { | |
183 | int fd = m_fd; m_fd = -1; | |
184 | if (::close(fd) < 0) | |
185 | return set_err(errno); | |
186 | return true; | |
187 | } | |
a37e7145 | 188 | |
2127e193 GI |
189 | // examples for smartctl |
190 | static const char smartctl_examples[] = | |
191 | "=================================================== SMARTCTL EXAMPLES =====\n\n" | |
a86ec89e GI |
192 | " smartctl --all /dev/sda (Prints all SMART information)\n\n" |
193 | " smartctl --smart=on --offlineauto=on --saveauto=on /dev/sda\n" | |
2127e193 | 194 | " (Enables SMART on first disk)\n\n" |
a86ec89e GI |
195 | " smartctl --test=long /dev/sda (Executes extended disk self-test)\n\n" |
196 | " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/sda\n" | |
2127e193 GI |
197 | " (Prints Self-Test & Attribute errors)\n" |
198 | " smartctl --all --device=3ware,2 /dev/sda\n" | |
199 | " smartctl --all --device=3ware,2 /dev/twe0\n" | |
200 | " smartctl --all --device=3ware,2 /dev/twa0\n" | |
cfbba5b9 | 201 | " smartctl --all --device=3ware,2 /dev/twl0\n" |
2127e193 GI |
202 | " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n" |
203 | " smartctl --all --device=hpt,1/1/3 /dev/sda\n" | |
204 | " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n" | |
205 | " of the 1st channel on the 1st HighPoint RAID controller)\n" | |
f4e463df GI |
206 | " smartctl --all --device=areca,3/1 /dev/sg2\n" |
207 | " (Prints all SMART info for 3rd ATA disk of the 1st enclosure\n" | |
208 | " on Areca RAID controller)\n" | |
2127e193 GI |
209 | ; |
210 | ||
2127e193 GI |
211 | ///////////////////////////////////////////////////////////////////////////// |
212 | /// Linux ATA support | |
213 | ||
214 | class linux_ata_device | |
215 | : public /*implements*/ ata_device_with_command_set, | |
216 | public /*extends*/ linux_smart_device | |
217 | { | |
218 | public: | |
219 | linux_ata_device(smart_interface * intf, const char * dev_name, const char * req_type); | |
832b75ed | 220 | |
2127e193 GI |
221 | protected: |
222 | virtual int ata_command_interface(smart_command_set command, int select, char * data); | |
223 | }; | |
a37e7145 | 224 | |
2127e193 GI |
225 | linux_ata_device::linux_ata_device(smart_interface * intf, const char * dev_name, const char * req_type) |
226 | : smart_device(intf, dev_name, "ata", req_type), | |
227 | linux_smart_device(O_RDONLY | O_NONBLOCK) | |
228 | { | |
832b75ed GG |
229 | } |
230 | ||
832b75ed GG |
231 | // PURPOSE |
232 | // This is an interface routine meant to isolate the OS dependent | |
233 | // parts of the code, and to provide a debugging interface. Each | |
234 | // different port and OS needs to provide it's own interface. This | |
235 | // is the linux one. | |
236 | // DETAILED DESCRIPTION OF ARGUMENTS | |
237 | // device: is the file descriptor provided by open() | |
238 | // command: defines the different operations. | |
239 | // select: additional input data if needed (which log, which type of | |
240 | // self-test). | |
241 | // data: location to write output data, if needed (512 bytes). | |
242 | // Note: not all commands use all arguments. | |
243 | // RETURN VALUES | |
244 | // -1 if the command failed | |
245 | // 0 if the command succeeded, | |
a37e7145 | 246 | // STATUS_CHECK routine: |
832b75ed GG |
247 | // -1 if the command failed |
248 | // 0 if the command succeeded and disk SMART status is "OK" | |
249 | // 1 if the command succeeded and disk SMART status is "FAILING" | |
250 | ||
a37e7145 | 251 | #define BUFFER_LENGTH (4+512) |
832b75ed | 252 | |
2127e193 GI |
253 | int linux_ata_device::ata_command_interface(smart_command_set command, int select, char * data) |
254 | { | |
a37e7145 | 255 | unsigned char buff[BUFFER_LENGTH]; |
832b75ed GG |
256 | // positive: bytes to write to caller. negative: bytes to READ from |
257 | // caller. zero: non-data command | |
258 | int copydata=0; | |
259 | ||
260 | const int HDIO_DRIVE_CMD_OFFSET = 4; | |
261 | ||
262 | // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl() | |
263 | // buff[0]: ATA COMMAND CODE REGISTER | |
264 | // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER | |
265 | // buff[2]: ATA FEATURES REGISTER | |
266 | // buff[3]: ATA SECTOR COUNT REGISTER | |
267 | ||
268 | // Note that on return: | |
269 | // buff[2] contains the ATA SECTOR COUNT REGISTER | |
a37e7145 | 270 | |
832b75ed | 271 | // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes) |
a37e7145 | 272 | memset(buff, 0, BUFFER_LENGTH); |
832b75ed GG |
273 | |
274 | buff[0]=ATA_SMART_CMD; | |
275 | switch (command){ | |
276 | case CHECK_POWER_MODE: | |
277 | buff[0]=ATA_CHECK_POWER_MODE; | |
278 | copydata=1; | |
279 | break; | |
280 | case READ_VALUES: | |
281 | buff[2]=ATA_SMART_READ_VALUES; | |
282 | buff[3]=1; | |
283 | copydata=512; | |
284 | break; | |
285 | case READ_THRESHOLDS: | |
286 | buff[2]=ATA_SMART_READ_THRESHOLDS; | |
287 | buff[1]=buff[3]=1; | |
288 | copydata=512; | |
289 | break; | |
290 | case READ_LOG: | |
291 | buff[2]=ATA_SMART_READ_LOG_SECTOR; | |
292 | buff[1]=select; | |
293 | buff[3]=1; | |
294 | copydata=512; | |
295 | break; | |
296 | case WRITE_LOG: | |
297 | break; | |
298 | case IDENTIFY: | |
299 | buff[0]=ATA_IDENTIFY_DEVICE; | |
300 | buff[3]=1; | |
301 | copydata=512; | |
302 | break; | |
303 | case PIDENTIFY: | |
304 | buff[0]=ATA_IDENTIFY_PACKET_DEVICE; | |
305 | buff[3]=1; | |
306 | copydata=512; | |
307 | break; | |
308 | case ENABLE: | |
309 | buff[2]=ATA_SMART_ENABLE; | |
310 | buff[1]=1; | |
311 | break; | |
312 | case DISABLE: | |
313 | buff[2]=ATA_SMART_DISABLE; | |
314 | buff[1]=1; | |
315 | break; | |
316 | case STATUS: | |
317 | // this command only says if SMART is working. It could be | |
318 | // replaced with STATUS_CHECK below. | |
319 | buff[2]=ATA_SMART_STATUS; | |
320 | break; | |
321 | case AUTO_OFFLINE: | |
34ad0c5f GG |
322 | // NOTE: According to ATAPI 4 and UP, this command is obsolete |
323 | // select == 241 for enable but no data transfer. Use TASK ioctl. | |
a37e7145 GG |
324 | buff[1]=ATA_SMART_AUTO_OFFLINE; |
325 | buff[2]=select; | |
832b75ed GG |
326 | break; |
327 | case AUTOSAVE: | |
34ad0c5f | 328 | // select == 248 for enable but no data transfer. Use TASK ioctl. |
a37e7145 GG |
329 | buff[1]=ATA_SMART_AUTOSAVE; |
330 | buff[2]=select; | |
832b75ed GG |
331 | break; |
332 | case IMMEDIATE_OFFLINE: | |
333 | buff[2]=ATA_SMART_IMMEDIATE_OFFLINE; | |
334 | buff[1]=select; | |
335 | break; | |
336 | case STATUS_CHECK: | |
337 | // This command uses HDIO_DRIVE_TASK and has different syntax than | |
338 | // the other commands. | |
339 | buff[1]=ATA_SMART_STATUS; | |
340 | break; | |
341 | default: | |
342 | pout("Unrecognized command %d in linux_ata_command_interface()\n" | |
343 | "Please contact " PACKAGE_BUGREPORT "\n", command); | |
344 | errno=ENOSYS; | |
345 | return -1; | |
346 | } | |
a37e7145 | 347 | |
832b75ed GG |
348 | // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the |
349 | // only ioctl() that can be used to WRITE data to the disk. | |
a37e7145 | 350 | if (command==WRITE_LOG) { |
832b75ed GG |
351 | unsigned char task[sizeof(ide_task_request_t)+512]; |
352 | ide_task_request_t *reqtask=(ide_task_request_t *) task; | |
353 | task_struct_t *taskfile=(task_struct_t *) reqtask->io_ports; | |
832b75ed GG |
354 | |
355 | memset(task, 0, sizeof(task)); | |
a37e7145 | 356 | |
832b75ed GG |
357 | taskfile->data = 0; |
358 | taskfile->feature = ATA_SMART_WRITE_LOG_SECTOR; | |
359 | taskfile->sector_count = 1; | |
360 | taskfile->sector_number = select; | |
361 | taskfile->low_cylinder = 0x4f; | |
362 | taskfile->high_cylinder = 0xc2; | |
363 | taskfile->device_head = 0; | |
364 | taskfile->command = ATA_SMART_CMD; | |
a37e7145 | 365 | |
832b75ed GG |
366 | reqtask->data_phase = TASKFILE_OUT; |
367 | reqtask->req_cmd = IDE_DRIVE_TASK_OUT; | |
368 | reqtask->out_size = 512; | |
369 | reqtask->in_size = 0; | |
a37e7145 | 370 | |
832b75ed GG |
371 | // copy user data into the task request structure |
372 | memcpy(task+sizeof(ide_task_request_t), data, 512); | |
a37e7145 | 373 | |
a86ec89e GI |
374 | if (ioctl(get_fd(), HDIO_DRIVE_TASKFILE, task)) { |
375 | if (errno==EINVAL) | |
376 | pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASK_IOCTL set\n"); | |
832b75ed GG |
377 | return -1; |
378 | } | |
379 | return 0; | |
380 | } | |
a37e7145 | 381 | |
832b75ed GG |
382 | // There are two different types of ioctls(). The HDIO_DRIVE_TASK |
383 | // one is this: | |
a37e7145 | 384 | if (command==STATUS_CHECK || command==AUTOSAVE || command==AUTO_OFFLINE){ |
832b75ed GG |
385 | // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You |
386 | // have to read the IDE driver source code. Sigh. | |
387 | // buff[0]: ATA COMMAND CODE REGISTER | |
388 | // buff[1]: ATA FEATURES REGISTER | |
389 | // buff[2]: ATA SECTOR_COUNT | |
390 | // buff[3]: ATA SECTOR NUMBER | |
391 | // buff[4]: ATA CYL LO REGISTER | |
392 | // buff[5]: ATA CYL HI REGISTER | |
393 | // buff[6]: ATA DEVICE HEAD | |
394 | ||
395 | unsigned const char normal_lo=0x4f, normal_hi=0xc2; | |
396 | unsigned const char failed_lo=0xf4, failed_hi=0x2c; | |
397 | buff[4]=normal_lo; | |
398 | buff[5]=normal_hi; | |
a37e7145 | 399 | |
a86ec89e GI |
400 | if (ioctl(get_fd(), HDIO_DRIVE_TASK, buff)) { |
401 | if (errno==EINVAL) { | |
4d59bff9 GG |
402 | pout("Error SMART Status command via HDIO_DRIVE_TASK failed"); |
403 | pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n"); | |
832b75ed GG |
404 | } |
405 | else | |
4d59bff9 | 406 | syserror("Error SMART Status command failed"); |
832b75ed GG |
407 | return -1; |
408 | } | |
a37e7145 | 409 | |
832b75ed GG |
410 | // Cyl low and Cyl high unchanged means "Good SMART status" |
411 | if (buff[4]==normal_lo && buff[5]==normal_hi) | |
412 | return 0; | |
a37e7145 | 413 | |
832b75ed GG |
414 | // These values mean "Bad SMART status" |
415 | if (buff[4]==failed_lo && buff[5]==failed_hi) | |
416 | return 1; | |
a37e7145 | 417 | |
832b75ed GG |
418 | // We haven't gotten output that makes sense; print out some debugging info |
419 | syserror("Error SMART Status command failed"); | |
420 | pout("Please get assistance from " PACKAGE_HOMEPAGE "\n"); | |
421 | pout("Register values returned from SMART Status command are:\n"); | |
a37e7145 GG |
422 | pout("ST =0x%02x\n",(int)buff[0]); |
423 | pout("ERR=0x%02x\n",(int)buff[1]); | |
832b75ed GG |
424 | pout("NS =0x%02x\n",(int)buff[2]); |
425 | pout("SC =0x%02x\n",(int)buff[3]); | |
426 | pout("CL =0x%02x\n",(int)buff[4]); | |
427 | pout("CH =0x%02x\n",(int)buff[5]); | |
428 | pout("SEL=0x%02x\n",(int)buff[6]); | |
a37e7145 | 429 | return -1; |
832b75ed | 430 | } |
a37e7145 | 431 | |
832b75ed GG |
432 | #if 1 |
433 | // Note to people doing ports to other OSes -- don't worry about | |
434 | // this block -- you can safely ignore it. I have put it here | |
435 | // because under linux when you do IDENTIFY DEVICE to a packet | |
436 | // device, it generates an ugly kernel syslog error message. This | |
437 | // is harmless but frightens users. So this block detects packet | |
438 | // devices and make IDENTIFY DEVICE fail "nicely" without a syslog | |
439 | // error message. | |
440 | // | |
441 | // If you read only the ATA specs, it appears as if a packet device | |
442 | // *might* respond to the IDENTIFY DEVICE command. This is | |
443 | // misleading - it's because around the time that SFF-8020 was | |
444 | // incorporated into the ATA-3/4 standard, the ATA authors were | |
445 | // sloppy. See SFF-8020 and you will see that ATAPI devices have | |
446 | // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their | |
447 | // command set, and return 'Command Aborted' to IDENTIFY DEVICE. | |
448 | if (command==IDENTIFY || command==PIDENTIFY){ | |
449 | unsigned short deviceid[256]; | |
450 | // check the device identity, as seen when the system was booted | |
451 | // or the device was FIRST registered. This will not be current | |
452 | // if the user has subsequently changed some of the parameters. If | |
453 | // device is a packet device, swap the command interpretations. | |
2127e193 | 454 | if (!ioctl(get_fd(), HDIO_GET_IDENTITY, deviceid) && (deviceid[0] & 0x8000)) |
832b75ed GG |
455 | buff[0]=(command==IDENTIFY)?ATA_IDENTIFY_PACKET_DEVICE:ATA_IDENTIFY_DEVICE; |
456 | } | |
457 | #endif | |
a37e7145 | 458 | |
832b75ed | 459 | // We are now doing the HDIO_DRIVE_CMD type ioctl. |
2127e193 | 460 | if ((ioctl(get_fd(), HDIO_DRIVE_CMD, buff))) |
832b75ed GG |
461 | return -1; |
462 | ||
463 | // CHECK POWER MODE command returns information in the Sector Count | |
464 | // register (buff[3]). Copy to return data buffer. | |
465 | if (command==CHECK_POWER_MODE) | |
466 | buff[HDIO_DRIVE_CMD_OFFSET]=buff[2]; | |
467 | ||
468 | // if the command returns data then copy it back | |
469 | if (copydata) | |
470 | memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata); | |
ba59cff1 | 471 | |
a37e7145 | 472 | return 0; |
ba59cff1 | 473 | } |
ba59cff1 | 474 | |
832b75ed GG |
475 | // >>>>>> Start of general SCSI specific linux code |
476 | ||
477 | /* Linux specific code. | |
478 | * Historically smartmontools (and smartsuite before it) used the | |
479 | * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device | |
480 | * nodes that use the SCSI subsystem. A better interface has been available | |
481 | * via the SCSI generic (sg) driver but this involves the extra step of | |
482 | * mapping disk devices (e.g. /dev/sda) to the corresponding sg device | |
483 | * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of | |
484 | * the sg driver have become available via the SG_IO ioctl which is available | |
485 | * on all SCSI devices (on SCSI tape devices from lk 2.6.6). | |
486 | * So the strategy below is to find out if the SG_IO ioctl is available and | |
487 | * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl. | |
488 | * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */ | |
489 | ||
490 | #define MAX_DXFER_LEN 1024 /* can be increased if necessary */ | |
491 | #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */ | |
492 | #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */ | |
493 | #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */ | |
494 | #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */ | |
bed94269 | 495 | #define LSCSI_DID_ERROR 0x7 /* Need to work around aacraid driver quirk */ |
832b75ed GG |
496 | #define LSCSI_DRIVER_TIMEOUT 0x6 |
497 | #define LSCSI_DID_TIME_OUT 0x3 | |
498 | #define LSCSI_DID_BUS_BUSY 0x2 | |
499 | #define LSCSI_DID_NO_CONNECT 0x1 | |
500 | ||
501 | #ifndef SCSI_IOCTL_SEND_COMMAND | |
502 | #define SCSI_IOCTL_SEND_COMMAND 1 | |
503 | #endif | |
504 | ||
f9e10201 JD |
505 | #define SG_IO_USE_DETECT 0 |
506 | #define SG_IO_UNSUPP 1 | |
507 | #define SG_IO_USE_V3 3 | |
508 | #define SG_IO_USE_V4 4 | |
832b75ed GG |
509 | |
510 | static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report, | |
f9e10201 | 511 | int sgio_ver); |
832b75ed GG |
512 | static int sisc_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report); |
513 | ||
f9e10201 | 514 | static int sg_io_state = SG_IO_USE_DETECT; |
832b75ed GG |
515 | |
516 | /* Preferred implementation for issuing SCSI commands in linux. This | |
517 | * function uses the SG_IO ioctl. Return 0 if command issued successfully | |
518 | * (various status values should still be checked). If the SCSI command | |
519 | * cannot be issued then a negative errno value is returned. */ | |
520 | static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report, | |
f9e10201 | 521 | int sg_io_ver) |
832b75ed GG |
522 | { |
523 | #ifndef SG_IO | |
524 | ARGUSED(dev_fd); ARGUSED(iop); ARGUSED(report); | |
525 | return -ENOTTY; | |
526 | #else | |
f9e10201 JD |
527 | |
528 | /* we are filling structures for both versions, but using only one requested */ | |
529 | struct sg_io_hdr io_hdr_v3; | |
530 | struct sg_io_v4 io_hdr_v4; | |
832b75ed | 531 | |
ff28b140 TL |
532 | #ifdef SCSI_CDB_CHECK |
533 | bool ok = is_scsi_cdb(iop->cmnd, iop->cmnd_len); | |
534 | if (! ok) { | |
535 | int n = iop->cmnd_len; | |
536 | const unsigned char * ucp = iop->cmnd; | |
537 | ||
538 | pout(">>>>>>>> %s: cdb seems invalid, opcode=0x%x, len=%d, cdb:\n", | |
539 | __func__, ((n > 0) ? ucp[0] : 0), n); | |
540 | if (n > 0) { | |
541 | if (n > 16) | |
542 | pout(" <<truncating to first 16 bytes>>\n"); | |
543 | dStrHex((const uint8_t *)ucp, ((n > 16) ? 16 : n), 1); | |
544 | } | |
545 | } | |
546 | #endif | |
547 | ||
832b75ed GG |
548 | if (report > 0) { |
549 | int k, j; | |
550 | const unsigned char * ucp = iop->cmnd; | |
551 | const char * np; | |
552 | char buff[256]; | |
553 | const int sz = (int)sizeof(buff); | |
554 | ||
f9e10201 | 555 | pout(">>>> do_scsi_cmnd_io: sg_io_ver=%d\n", sg_io_ver); |
832b75ed GG |
556 | np = scsi_get_opcode_name(ucp[0]); |
557 | j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>"); | |
558 | for (k = 0; k < (int)iop->cmnd_len; ++k) | |
559 | j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]); | |
a37e7145 | 560 | if ((report > 1) && |
832b75ed GG |
561 | (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) { |
562 | int trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
563 | ||
a86ec89e GI |
564 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing " |
565 | "data, len=%d%s:\n", (int)iop->dxfer_len, | |
566 | (trunc ? " [only first 256 bytes shown]" : "")); | |
ff28b140 | 567 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1); |
832b75ed GG |
568 | } |
569 | else | |
a86ec89e | 570 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n"); |
2127e193 | 571 | pout("%s", buff); |
832b75ed | 572 | } |
f9e10201 JD |
573 | memset(&io_hdr_v3, 0, sizeof(struct sg_io_hdr)); |
574 | memset(&io_hdr_v4, 0, sizeof(struct sg_io_v4)); | |
575 | ||
576 | io_hdr_v3.interface_id = 'S'; | |
577 | io_hdr_v3.cmd_len = iop->cmnd_len; | |
578 | io_hdr_v3.mx_sb_len = iop->max_sense_len; | |
579 | io_hdr_v3.dxfer_len = iop->dxfer_len; | |
580 | io_hdr_v3.dxferp = iop->dxferp; | |
581 | io_hdr_v3.cmdp = iop->cmnd; | |
582 | io_hdr_v3.sbp = iop->sensep; | |
832b75ed GG |
583 | /* sg_io_hdr interface timeout has millisecond units. Timeout of 0 |
584 | defaults to 60 seconds. */ | |
f9e10201 JD |
585 | io_hdr_v3.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000; |
586 | ||
587 | io_hdr_v4.guard = 'Q'; | |
588 | io_hdr_v4.request_len = iop->cmnd_len; | |
589 | io_hdr_v4.request = __u64(iop->cmnd); | |
590 | io_hdr_v4.max_response_len = iop->max_sense_len; | |
591 | io_hdr_v4.response = __u64(iop->sensep); | |
592 | io_hdr_v4.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000; // msec | |
593 | ||
832b75ed GG |
594 | switch (iop->dxfer_dir) { |
595 | case DXFER_NONE: | |
f9e10201 | 596 | io_hdr_v3.dxfer_direction = SG_DXFER_NONE; |
832b75ed GG |
597 | break; |
598 | case DXFER_FROM_DEVICE: | |
f9e10201 JD |
599 | io_hdr_v3.dxfer_direction = SG_DXFER_FROM_DEV; |
600 | io_hdr_v4.din_xfer_len = iop->dxfer_len; | |
601 | io_hdr_v4.din_xferp = __u64(iop->dxferp); | |
832b75ed GG |
602 | break; |
603 | case DXFER_TO_DEVICE: | |
f9e10201 JD |
604 | io_hdr_v3.dxfer_direction = SG_DXFER_TO_DEV; |
605 | io_hdr_v4.dout_xfer_len = iop->dxfer_len; | |
606 | io_hdr_v4.dout_xferp = __u64(iop->dxferp); | |
832b75ed GG |
607 | break; |
608 | default: | |
609 | pout("do_scsi_cmnd_io: bad dxfer_dir\n"); | |
610 | return -EINVAL; | |
611 | } | |
f9e10201 | 612 | |
832b75ed GG |
613 | iop->resp_sense_len = 0; |
614 | iop->scsi_status = 0; | |
615 | iop->resid = 0; | |
f9e10201 JD |
616 | |
617 | void * io_hdr = NULL; | |
618 | ||
619 | switch (sg_io_ver) { | |
620 | case SG_IO_USE_V3: | |
621 | io_hdr = &io_hdr_v3; | |
622 | break; | |
623 | case SG_IO_USE_V4: | |
624 | io_hdr = &io_hdr_v4; | |
625 | break; | |
626 | default: | |
627 | // should never be reached | |
628 | errno = EOPNOTSUPP; | |
629 | return -errno; | |
630 | } | |
631 | ||
632 | if (ioctl(dev_fd, SG_IO, io_hdr) < 0) { | |
633 | if (report) | |
634 | pout(" SG_IO ioctl failed, errno=%d [%s], SG_IO_V%d\n", errno, | |
635 | strerror(errno), sg_io_ver); | |
832b75ed GG |
636 | return -errno; |
637 | } | |
f9e10201 JD |
638 | |
639 | unsigned int sg_driver_status = 0, sg_transport_status = 0, sg_info = 0, | |
640 | sg_duration = 0; | |
641 | ||
642 | if (sg_io_ver == SG_IO_USE_V3) { | |
643 | iop->resid = io_hdr_v3.resid; | |
644 | iop->scsi_status = io_hdr_v3.status; | |
645 | sg_driver_status = io_hdr_v3.driver_status; | |
646 | sg_transport_status = io_hdr_v3.host_status; | |
647 | sg_info = io_hdr_v3.info; | |
648 | iop->resp_sense_len = io_hdr_v3.sb_len_wr; | |
649 | sg_duration = io_hdr_v3.duration; | |
650 | } | |
651 | ||
652 | if (sg_io_ver == SG_IO_USE_V4) { | |
653 | switch (iop->dxfer_dir) { | |
654 | case DXFER_NONE: | |
655 | iop->resid = 0; | |
656 | break; | |
657 | case DXFER_FROM_DEVICE: | |
658 | iop->resid = io_hdr_v4.din_resid; | |
659 | break; | |
660 | case DXFER_TO_DEVICE: | |
661 | iop->resid = io_hdr_v4.dout_resid; | |
662 | break; | |
663 | } | |
664 | iop->scsi_status = io_hdr_v4.device_status; | |
665 | sg_driver_status = io_hdr_v4.driver_status; | |
666 | sg_transport_status = io_hdr_v4.transport_status; | |
667 | sg_info = io_hdr_v4.info; | |
668 | iop->resp_sense_len = io_hdr_v4.response_len; | |
669 | sg_duration = io_hdr_v4.duration; | |
670 | } | |
671 | ||
832b75ed | 672 | if (report > 0) { |
f9e10201 JD |
673 | pout(" scsi_status=0x%x, sg_transport_status=0x%x, sg_driver_status=0x%x\n" |
674 | " sg_info=0x%x sg_duration=%d milliseconds resid=%d\n", iop->scsi_status, | |
675 | sg_transport_status, sg_driver_status, sg_info, | |
676 | sg_duration, iop->resid); | |
677 | ||
832b75ed GG |
678 | if (report > 1) { |
679 | if (DXFER_FROM_DEVICE == iop->dxfer_dir) { | |
a37e7145 GG |
680 | int trunc, len; |
681 | ||
682 | len = iop->dxfer_len - iop->resid; | |
683 | trunc = (len > 256) ? 1 : 0; | |
684 | if (len > 0) { | |
685 | pout(" Incoming data, len=%d%s:\n", len, | |
686 | (trunc ? " [only first 256 bytes shown]" : "")); | |
ff28b140 | 687 | dStrHex(iop->dxferp, (trunc ? 256 : len), 1); |
a37e7145 GG |
688 | } else |
689 | pout(" Incoming data trimmed to nothing by resid\n"); | |
832b75ed GG |
690 | } |
691 | } | |
692 | } | |
832b75ed | 693 | |
f9e10201 JD |
694 | if (sg_info & SG_INFO_CHECK) { /* error or warning */ |
695 | int masked_driver_status = (LSCSI_DRIVER_MASK & sg_driver_status); | |
832b75ed | 696 | |
f9e10201 JD |
697 | if (0 != sg_transport_status) { |
698 | if ((LSCSI_DID_NO_CONNECT == sg_transport_status) || | |
699 | (LSCSI_DID_BUS_BUSY == sg_transport_status) || | |
700 | (LSCSI_DID_TIME_OUT == sg_transport_status)) | |
832b75ed | 701 | return -ETIMEDOUT; |
4d59bff9 | 702 | else |
bed94269 | 703 | /* Check for DID_ERROR - workaround for aacraid driver quirk */ |
f9e10201 | 704 | if (LSCSI_DID_ERROR != sg_transport_status) { |
bed94269 GI |
705 | return -EIO; /* catch all if not DID_ERR */ |
706 | } | |
832b75ed GG |
707 | } |
708 | if (0 != masked_driver_status) { | |
709 | if (LSCSI_DRIVER_TIMEOUT == masked_driver_status) | |
710 | return -ETIMEDOUT; | |
4d59bff9 GG |
711 | else if (LSCSI_DRIVER_SENSE != masked_driver_status) |
712 | return -EIO; | |
713 | } | |
832b75ed GG |
714 | if (LSCSI_DRIVER_SENSE == masked_driver_status) |
715 | iop->scsi_status = SCSI_STATUS_CHECK_CONDITION; | |
a37e7145 | 716 | if ((SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) && |
832b75ed GG |
717 | iop->sensep && (iop->resp_sense_len > 0)) { |
718 | if (report > 1) { | |
719 | pout(" >>> Sense buffer, len=%d:\n", | |
4d59bff9 | 720 | (int)iop->resp_sense_len); |
ff28b140 | 721 | dStrHex(iop->sensep, iop->resp_sense_len , 1); |
832b75ed GG |
722 | } |
723 | } | |
724 | if (report) { | |
a86ec89e | 725 | if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status && iop->sensep) { |
4d59bff9 GG |
726 | if ((iop->sensep[0] & 0x7f) > 0x71) |
727 | pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n", | |
728 | iop->scsi_status, iop->sensep[1] & 0xf, | |
729 | iop->sensep[2], iop->sensep[3]); | |
730 | else | |
731 | pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", | |
732 | iop->scsi_status, iop->sensep[2] & 0xf, | |
733 | iop->sensep[12], iop->sensep[13]); | |
832b75ed GG |
734 | } |
735 | else | |
736 | pout(" status=0x%x\n", iop->scsi_status); | |
737 | } | |
738 | } | |
739 | return 0; | |
740 | #endif | |
741 | } | |
742 | ||
743 | struct linux_ioctl_send_command | |
744 | { | |
745 | int inbufsize; | |
746 | int outbufsize; | |
ff28b140 | 747 | uint8_t buff[MAX_DXFER_LEN + 16]; |
832b75ed GG |
748 | }; |
749 | ||
a37e7145 | 750 | /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't |
832b75ed GG |
751 | * support: CDB length (guesses it from opcode), resid and timeout. |
752 | * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout | |
753 | * to 2 hours in order to allow long foreground extended self tests. */ | |
754 | static int sisc_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report) | |
755 | { | |
756 | struct linux_ioctl_send_command wrk; | |
757 | int status, buff_offset; | |
758 | size_t len; | |
759 | ||
760 | memcpy(wrk.buff, iop->cmnd, iop->cmnd_len); | |
761 | buff_offset = iop->cmnd_len; | |
762 | if (report > 0) { | |
763 | int k, j; | |
764 | const unsigned char * ucp = iop->cmnd; | |
765 | const char * np; | |
766 | char buff[256]; | |
767 | const int sz = (int)sizeof(buff); | |
768 | ||
769 | np = scsi_get_opcode_name(ucp[0]); | |
770 | j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>"); | |
771 | for (k = 0; k < (int)iop->cmnd_len; ++k) | |
772 | j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]); | |
a86ec89e | 773 | if ((report > 1) && (DXFER_TO_DEVICE == iop->dxfer_dir)) { |
832b75ed GG |
774 | int trunc = (iop->dxfer_len > 256) ? 1 : 0; |
775 | ||
a86ec89e GI |
776 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing " |
777 | "data, len=%d%s:\n", (int)iop->dxfer_len, | |
778 | (trunc ? " [only first 256 bytes shown]" : "")); | |
ff28b140 | 779 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1); |
832b75ed GG |
780 | } |
781 | else | |
a86ec89e | 782 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n"); |
2127e193 | 783 | pout("%s", buff); |
832b75ed GG |
784 | } |
785 | switch (iop->dxfer_dir) { | |
786 | case DXFER_NONE: | |
787 | wrk.inbufsize = 0; | |
788 | wrk.outbufsize = 0; | |
789 | break; | |
790 | case DXFER_FROM_DEVICE: | |
791 | wrk.inbufsize = 0; | |
792 | if (iop->dxfer_len > MAX_DXFER_LEN) | |
793 | return -EINVAL; | |
794 | wrk.outbufsize = iop->dxfer_len; | |
795 | break; | |
796 | case DXFER_TO_DEVICE: | |
797 | if (iop->dxfer_len > MAX_DXFER_LEN) | |
798 | return -EINVAL; | |
799 | memcpy(wrk.buff + buff_offset, iop->dxferp, iop->dxfer_len); | |
800 | wrk.inbufsize = iop->dxfer_len; | |
801 | wrk.outbufsize = 0; | |
802 | break; | |
803 | default: | |
804 | pout("do_scsi_cmnd_io: bad dxfer_dir\n"); | |
805 | return -EINVAL; | |
806 | } | |
807 | iop->resp_sense_len = 0; | |
808 | iop->scsi_status = 0; | |
809 | iop->resid = 0; | |
810 | status = ioctl(dev_fd, SCSI_IOCTL_SEND_COMMAND, &wrk); | |
811 | if (-1 == status) { | |
812 | if (report) | |
813 | pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n", | |
814 | errno, strerror(errno)); | |
815 | return -errno; | |
816 | } | |
817 | if (0 == status) { | |
818 | if (report > 0) | |
819 | pout(" status=0\n"); | |
820 | if (DXFER_FROM_DEVICE == iop->dxfer_dir) { | |
821 | memcpy(iop->dxferp, wrk.buff, iop->dxfer_len); | |
822 | if (report > 1) { | |
823 | int trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
824 | ||
825 | pout(" Incoming data, len=%d%s:\n", (int)iop->dxfer_len, | |
826 | (trunc ? " [only first 256 bytes shown]" : "")); | |
ff28b140 | 827 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1); |
832b75ed GG |
828 | } |
829 | } | |
830 | return 0; | |
831 | } | |
832 | iop->scsi_status = status & 0x7e; /* bits 0 and 7 used to be for vendors */ | |
833 | if (LSCSI_DRIVER_SENSE == ((status >> 24) & 0xf)) | |
834 | iop->scsi_status = SCSI_STATUS_CHECK_CONDITION; | |
835 | len = (SEND_IOCTL_RESP_SENSE_LEN < iop->max_sense_len) ? | |
836 | SEND_IOCTL_RESP_SENSE_LEN : iop->max_sense_len; | |
a37e7145 | 837 | if ((SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) && |
832b75ed GG |
838 | iop->sensep && (len > 0)) { |
839 | memcpy(iop->sensep, wrk.buff, len); | |
840 | iop->resp_sense_len = len; | |
841 | if (report > 1) { | |
842 | pout(" >>> Sense buffer, len=%d:\n", (int)len); | |
ff28b140 | 843 | dStrHex(wrk.buff, len , 1); |
832b75ed GG |
844 | } |
845 | } | |
846 | if (report) { | |
847 | if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) { | |
848 | pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status & 0xff, | |
849 | wrk.buff[2] & 0xf, wrk.buff[12], wrk.buff[13]); | |
850 | } | |
851 | else | |
852 | pout(" status=0x%x\n", status); | |
853 | } | |
854 | if (iop->scsi_status > 0) | |
855 | return 0; | |
856 | else { | |
857 | if (report > 0) | |
a37e7145 | 858 | pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n", |
832b75ed GG |
859 | status); |
860 | return -EIO; /* give up, assume no device there */ | |
861 | } | |
862 | } | |
863 | ||
864 | /* SCSI command transmission interface function, linux version. | |
865 | * Returns 0 if SCSI command successfully launched and response | |
a37e7145 | 866 | * received. Even when 0 is returned the caller should check |
832b75ed GG |
867 | * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings |
868 | * (e.g. CHECK CONDITION). If the SCSI command could not be issued | |
869 | * (e.g. device not present or timeout) or some other problem | |
870 | * (e.g. timeout) then returns a negative errno value */ | |
ba59cff1 GG |
871 | static int do_normal_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, |
872 | int report) | |
832b75ed GG |
873 | { |
874 | int res; | |
875 | ||
876 | /* implementation relies on static sg_io_state variable. If not | |
877 | * previously set tries the SG_IO ioctl. If that succeeds assume | |
878 | * that SG_IO ioctl functional. If it fails with an errno value | |
a37e7145 | 879 | * other than ENODEV (no device) or permission then assume |
832b75ed GG |
880 | * SCSI_IOCTL_SEND_COMMAND is the only option. */ |
881 | switch (sg_io_state) { | |
f9e10201 | 882 | case SG_IO_USE_DETECT: |
832b75ed | 883 | /* ignore report argument */ |
f9e10201 JD |
884 | /* Try SG_IO V3 first */ |
885 | if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, SG_IO_USE_V3))) { | |
886 | sg_io_state = SG_IO_USE_V3; | |
4d59bff9 GG |
887 | return 0; |
888 | } else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res)) | |
889 | return res; /* wait until we see a device */ | |
f9e10201 JD |
890 | /* See if we can use SG_IO V4 * */ |
891 | if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, SG_IO_USE_V4))) { | |
892 | sg_io_state = SG_IO_USE_V4; | |
893 | return 0; | |
894 | } else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res)) | |
895 | return res; /* wait until we see a device */ | |
896 | /* fallback to the SCSI_IOCTL_SEND_COMMAND */ | |
897 | sg_io_state = SG_IO_UNSUPP; | |
898 | /* FALLTHRU */ | |
899 | case SG_IO_UNSUPP: | |
ff28b140 | 900 | /* deprecated SCSI_IOCTL_SEND_COMMAND ioctl */ |
4d59bff9 | 901 | return sisc_cmnd_io(dev_fd, iop, report); |
f9e10201 JD |
902 | case SG_IO_USE_V3: |
903 | case SG_IO_USE_V4: | |
904 | /* use SG_IO V3 or V4 ioctl, depending on availabiliy */ | |
905 | return sg_io_cmnd_io(dev_fd, iop, report, sg_io_state); | |
832b75ed | 906 | default: |
a37e7145 | 907 | pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state); |
f9e10201 | 908 | sg_io_state = SG_IO_USE_DETECT; |
4d59bff9 | 909 | return -EIO; /* report error and reset state */ |
832b75ed GG |
910 | } |
911 | } | |
912 | ||
a37e7145 | 913 | // >>>>>> End of general SCSI specific linux code |
832b75ed | 914 | |
2127e193 GI |
915 | ///////////////////////////////////////////////////////////////////////////// |
916 | /// Standard SCSI support | |
832b75ed | 917 | |
2127e193 GI |
918 | class linux_scsi_device |
919 | : public /*implements*/ scsi_device, | |
920 | public /*extends*/ linux_smart_device | |
921 | { | |
922 | public: | |
f4ebf3d1 GI |
923 | linux_scsi_device(smart_interface * intf, const char * dev_name, |
924 | const char * req_type, bool scanning = false); | |
832b75ed | 925 | |
2127e193 | 926 | virtual smart_device * autodetect_open(); |
832b75ed | 927 | |
2127e193 | 928 | virtual bool scsi_pass_through(scsi_cmnd_io * iop); |
f4ebf3d1 GI |
929 | |
930 | private: | |
931 | bool m_scanning; ///< true if created within scan_smart_devices | |
2127e193 | 932 | }; |
832b75ed | 933 | |
2127e193 | 934 | linux_scsi_device::linux_scsi_device(smart_interface * intf, |
f4ebf3d1 | 935 | const char * dev_name, const char * req_type, bool scanning /*= false*/) |
2127e193 | 936 | : smart_device(intf, dev_name, "scsi", req_type), |
f4ebf3d1 GI |
937 | // If opened with O_RDWR, a SATA disk in standby mode |
938 | // may spin-up after device close(). | |
939 | linux_smart_device(O_RDONLY | O_NONBLOCK), | |
940 | m_scanning(scanning) | |
2127e193 GI |
941 | { |
942 | } | |
832b75ed | 943 | |
2127e193 GI |
944 | bool linux_scsi_device::scsi_pass_through(scsi_cmnd_io * iop) |
945 | { | |
cfbba5b9 | 946 | int status = do_normal_scsi_cmnd_io(get_fd(), iop, scsi_debugmode); |
2127e193 GI |
947 | if (status < 0) |
948 | return set_err(-status); | |
949 | return true; | |
950 | } | |
832b75ed | 951 | |
d2e702cf GI |
952 | ///////////////////////////////////////////////////////////////////////////// |
953 | /// PMC AacRAID support | |
954 | ||
955 | class linux_aacraid_device | |
956 | :public scsi_device, | |
957 | public /*extends */ linux_smart_device | |
958 | { | |
959 | public: | |
960 | linux_aacraid_device(smart_interface *intf, const char *dev_name, | |
961 | unsigned int host, unsigned int channel, unsigned int device); | |
962 | ||
963 | virtual ~linux_aacraid_device() throw(); | |
964 | ||
965 | virtual bool open(); | |
966 | ||
967 | virtual bool scsi_pass_through(scsi_cmnd_io *iop); | |
968 | ||
969 | private: | |
970 | //Device Host number | |
971 | int aHost; | |
972 | ||
973 | //Channel(Lun) of the device | |
974 | int aLun; | |
975 | ||
976 | //Id of the device | |
977 | int aId; | |
978 | ||
979 | }; | |
980 | ||
981 | linux_aacraid_device::linux_aacraid_device(smart_interface *intf, | |
982 | const char *dev_name, unsigned int host, unsigned int channel, unsigned int device) | |
983 | : smart_device(intf,dev_name,"aacraid","aacraid"), | |
984 | linux_smart_device(O_RDWR|O_NONBLOCK), | |
985 | aHost(host), aLun(channel), aId(device) | |
986 | { | |
987 | set_info().info_name = strprintf("%s [aacraid_disk_%02d_%02d_%d]",dev_name,aHost,aLun,aId); | |
988 | set_info().dev_type = strprintf("aacraid,%d,%d,%d",aHost,aLun,aId); | |
989 | } | |
990 | ||
991 | linux_aacraid_device::~linux_aacraid_device() throw() | |
992 | { | |
993 | } | |
994 | ||
995 | bool linux_aacraid_device::open() | |
996 | { | |
997 | //Create the character device name based on the host number | |
998 | //Required for get stats from disks connected to different controllers | |
999 | char dev_name[128]; | |
1000 | snprintf(dev_name, sizeof(dev_name), "/dev/aac%d", aHost); | |
1001 | ||
1002 | //Initial open of dev name to check if it exsists | |
1003 | int afd = ::open(dev_name,O_RDWR); | |
1004 | ||
1005 | if(afd < 0 && errno == ENOENT) { | |
1006 | ||
1007 | FILE *fp = fopen("/proc/devices","r"); | |
1008 | if(NULL == fp) | |
1009 | return set_err(errno,"cannot open /proc/devices:%s", | |
1010 | strerror(errno)); | |
1011 | ||
1012 | char line[256]; | |
1013 | int mjr = -1; | |
1014 | ||
1015 | while(fgets(line,sizeof(line),fp) !=NULL) { | |
1016 | int nc = -1; | |
1017 | if(sscanf(line,"%d aac%n",&mjr,&nc) == 1 | |
1018 | && nc > 0 && '\n' == line[nc]) | |
1019 | break; | |
1020 | mjr = -1; | |
1021 | } | |
1022 | ||
1023 | //work with /proc/devices is done | |
1024 | fclose(fp); | |
1025 | ||
1026 | if (mjr < 0) | |
1027 | return set_err(ENOENT, "aac entry not found in /proc/devices"); | |
1028 | ||
1029 | //Create misc device file in /dev/ used for communication with driver | |
1030 | if(mknod(dev_name,S_IFCHR,makedev(mjr,aHost))) | |
1031 | return set_err(errno,"cannot create %s:%s",dev_name,strerror(errno)); | |
1032 | ||
1033 | afd = ::open(dev_name,O_RDWR); | |
1034 | } | |
1035 | ||
1036 | if(afd < 0) | |
1037 | return set_err(errno,"cannot open %s:%s",dev_name,strerror(errno)); | |
1038 | ||
1039 | set_fd(afd); | |
1040 | return true; | |
1041 | } | |
1042 | ||
1043 | bool linux_aacraid_device::scsi_pass_through(scsi_cmnd_io *iop) | |
1044 | { | |
1045 | int report = scsi_debugmode; | |
1046 | ||
1047 | if (report > 0) { | |
1048 | int k, j; | |
1049 | const unsigned char * ucp = iop->cmnd; | |
1050 | const char * np; | |
1051 | char buff[256]; | |
1052 | const int sz = (int)sizeof(buff); | |
1053 | ||
1054 | np = scsi_get_opcode_name(ucp[0]); | |
1055 | j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>"); | |
1056 | for (k = 0; k < (int)iop->cmnd_len; ++k) | |
1057 | j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]); | |
f9e10201 | 1058 | if ((report > 1) && |
d2e702cf GI |
1059 | (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) { |
1060 | int trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
1061 | ||
a86ec89e GI |
1062 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing " |
1063 | "data, len=%d%s:\n", (int)iop->dxfer_len, | |
1064 | (trunc ? " [only first 256 bytes shown]" : "")); | |
ff28b140 | 1065 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1); |
d2e702cf GI |
1066 | } |
1067 | else | |
a86ec89e | 1068 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n"); |
d2e702cf GI |
1069 | |
1070 | pout("%s", buff); | |
1071 | } | |
1072 | ||
1073 | ||
1074 | //return test commands | |
1075 | if (iop->cmnd[0] == 0x00) | |
1076 | return true; | |
1077 | ||
1078 | user_aac_reply *pReply; | |
1079 | ||
1080 | #ifdef ENVIRONMENT64 | |
1081 | // Create user 64 bit request | |
1082 | user_aac_srb64 *pSrb; | |
1083 | uint8_t aBuff[sizeof(user_aac_srb64) + sizeof(user_aac_reply)] = {0,}; | |
1084 | ||
1085 | pSrb = (user_aac_srb64*)aBuff; | |
a86ec89e | 1086 | pSrb->count = sizeof(user_aac_srb64) - sizeof(user_sgentry64); |
d2e702cf GI |
1087 | |
1088 | #elif defined(ENVIRONMENT32) | |
1089 | //Create user 32 bit request | |
1090 | user_aac_srb32 *pSrb; | |
1091 | uint8_t aBuff[sizeof(user_aac_srb32) + sizeof(user_aac_reply)] = {0,}; | |
1092 | ||
1093 | pSrb = (user_aac_srb32*)aBuff; | |
a86ec89e | 1094 | pSrb->count = sizeof(user_aac_srb32) - sizeof(user_sgentry32); |
d2e702cf GI |
1095 | #endif |
1096 | ||
1097 | pSrb->function = SRB_FUNCTION_EXECUTE_SCSI; | |
1098 | //channel is 0 always | |
1099 | pSrb->channel = 0; | |
1100 | pSrb->id = aId; | |
1101 | pSrb->lun = aLun; | |
1102 | pSrb->timeout = 0; | |
1103 | ||
1104 | pSrb->retry_limit = 0; | |
1105 | pSrb->cdb_size = iop->cmnd_len; | |
1106 | ||
1107 | switch(iop->dxfer_dir) { | |
1108 | case DXFER_NONE: | |
1109 | pSrb->flags = SRB_NoDataXfer; | |
1110 | break; | |
1111 | case DXFER_FROM_DEVICE: | |
1112 | pSrb->flags = SRB_DataIn; | |
1113 | break; | |
1114 | case DXFER_TO_DEVICE: | |
1115 | pSrb->flags = SRB_DataOut; | |
1116 | break; | |
1117 | default: | |
1118 | pout("aacraid: bad dxfer_dir\n"); | |
1119 | return set_err(EINVAL, "aacraid: bad dxfer_dir\n"); | |
1120 | } | |
1121 | ||
1122 | if(iop->dxfer_len > 0) { | |
1123 | ||
1124 | #ifdef ENVIRONMENT64 | |
1125 | pSrb->sg64.count = 1; | |
1126 | pSrb->sg64.sg64[0].addr64.lo32 = ((intptr_t)iop->dxferp) & | |
1127 | 0x00000000ffffffff; | |
1128 | pSrb->sg64.sg64[0].addr64.hi32 = ((intptr_t)iop->dxferp) >> 32; | |
1129 | ||
1130 | pSrb->sg64.sg64[0].length = (uint32_t)iop->dxfer_len; | |
a86ec89e | 1131 | pSrb->count += pSrb->sg64.count * sizeof(user_sgentry64); |
d2e702cf GI |
1132 | #elif defined(ENVIRONMENT32) |
1133 | pSrb->sg32.count = 1; | |
1134 | pSrb->sg32.sg32[0].addr32 = (intptr_t)iop->dxferp; | |
1135 | ||
1136 | pSrb->sg32.sg32[0].length = (uint32_t)iop->dxfer_len; | |
a86ec89e | 1137 | pSrb->count += pSrb->sg32.count * sizeof(user_sgentry32); |
d2e702cf GI |
1138 | #endif |
1139 | ||
1140 | } | |
1141 | ||
a86ec89e GI |
1142 | pReply = (user_aac_reply*)(aBuff+pSrb->count); |
1143 | ||
d2e702cf GI |
1144 | memcpy(pSrb->cdb,iop->cmnd,iop->cmnd_len); |
1145 | ||
1146 | int rc = 0; | |
1147 | errno = 0; | |
1148 | rc = ioctl(get_fd(),FSACTL_SEND_RAW_SRB,pSrb); | |
a86ec89e GI |
1149 | |
1150 | if (rc != 0) | |
1151 | return set_err(errno, "aacraid send_raw_srb: %d.%d = %s", | |
1152 | aLun, aId, strerror(errno)); | |
1153 | ||
1154 | /* see kernel aacraid.h and MSDN SCSI_REQUEST_BLOCK documentation */ | |
1155 | #define SRB_STATUS_SUCCESS 0x1 | |
1156 | #define SRB_STATUS_ERROR 0x4 | |
1157 | #define SRB_STATUS_NO_DEVICE 0x08 | |
1158 | #define SRB_STATUS_SELECTION_TIMEOUT 0x0a | |
1159 | #define SRB_STATUS_AUTOSENSE_VALID 0x80 | |
1160 | ||
1161 | iop->scsi_status = pReply->scsi_status; | |
1162 | ||
1163 | if (pReply->srb_status == (SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_ERROR) | |
1164 | && iop->scsi_status == SCSI_STATUS_CHECK_CONDITION) { | |
1165 | memcpy(iop->sensep, pReply->sense_data, pReply->sense_data_size); | |
1166 | iop->resp_sense_len = pReply->sense_data_size; | |
1167 | return true; /* request completed with sense data */ | |
1168 | } | |
1169 | ||
1170 | switch (pReply->srb_status & 0x3f) { | |
1171 | ||
1172 | case SRB_STATUS_SUCCESS: | |
1173 | return true; /* request completed successfully */ | |
1174 | ||
1175 | case SRB_STATUS_NO_DEVICE: | |
1176 | return set_err(EIO, "aacraid: Device %d %d does not exist", aLun, aId); | |
1177 | ||
1178 | case SRB_STATUS_SELECTION_TIMEOUT: | |
1179 | return set_err(EIO, "aacraid: Device %d %d not responding", aLun, aId); | |
1180 | ||
1181 | default: | |
1182 | return set_err(EIO, "aacraid result: %d.%d = 0x%x", | |
1183 | aLun, aId, pReply->srb_status); | |
d2e702cf | 1184 | } |
d2e702cf GI |
1185 | } |
1186 | ||
1187 | ||
2127e193 GI |
1188 | ///////////////////////////////////////////////////////////////////////////// |
1189 | /// LSI MegaRAID support | |
832b75ed | 1190 | |
2127e193 GI |
1191 | class linux_megaraid_device |
1192 | : public /* implements */ scsi_device, | |
1193 | public /* extends */ linux_smart_device | |
1194 | { | |
1195 | public: | |
1196 | linux_megaraid_device(smart_interface *intf, const char *name, | |
a86ec89e | 1197 | unsigned int tgt); |
2127e193 GI |
1198 | |
1199 | virtual ~linux_megaraid_device() throw(); | |
1200 | ||
1201 | virtual smart_device * autodetect_open(); | |
1202 | ||
1203 | virtual bool open(); | |
1204 | virtual bool close(); | |
ee38a438 | 1205 | |
2127e193 GI |
1206 | virtual bool scsi_pass_through(scsi_cmnd_io *iop); |
1207 | ||
1208 | private: | |
1209 | unsigned int m_disknum; | |
2127e193 GI |
1210 | unsigned int m_hba; |
1211 | int m_fd; | |
1212 | ||
1213 | bool (linux_megaraid_device::*pt_cmd)(int cdblen, void *cdb, int dataLen, void *data, | |
ee38a438 | 1214 | int senseLen, void *sense, int report, int direction); |
2127e193 | 1215 | bool megasas_cmd(int cdbLen, void *cdb, int dataLen, void *data, |
ee38a438 | 1216 | int senseLen, void *sense, int report, int direction); |
2127e193 | 1217 | bool megadev_cmd(int cdbLen, void *cdb, int dataLen, void *data, |
ee38a438 | 1218 | int senseLen, void *sense, int report, int direction); |
2127e193 | 1219 | }; |
832b75ed | 1220 | |
2127e193 | 1221 | linux_megaraid_device::linux_megaraid_device(smart_interface *intf, |
a86ec89e | 1222 | const char *dev_name, unsigned int tgt) |
2127e193 GI |
1223 | : smart_device(intf, dev_name, "megaraid", "megaraid"), |
1224 | linux_smart_device(O_RDWR | O_NONBLOCK), | |
a86ec89e | 1225 | m_disknum(tgt), m_hba(0), |
2127e193 GI |
1226 | m_fd(-1), pt_cmd(0) |
1227 | { | |
1228 | set_info().info_name = strprintf("%s [megaraid_disk_%02d]", dev_name, m_disknum); | |
ee38a438 | 1229 | set_info().dev_type = strprintf("megaraid,%d", tgt); |
2127e193 | 1230 | } |
a37e7145 | 1231 | |
2127e193 GI |
1232 | linux_megaraid_device::~linux_megaraid_device() throw() |
1233 | { | |
1234 | if (m_fd >= 0) | |
1235 | ::close(m_fd); | |
1236 | } | |
832b75ed | 1237 | |
2127e193 GI |
1238 | smart_device * linux_megaraid_device::autodetect_open() |
1239 | { | |
cfbba5b9 | 1240 | int report = scsi_debugmode; |
bed94269 | 1241 | |
2127e193 GI |
1242 | // Open device |
1243 | if (!open()) | |
1244 | return this; | |
1245 | ||
1246 | // The code below is based on smartd.cpp:SCSIFilterKnown() | |
1247 | if (strcmp(get_req_type(), "megaraid")) | |
1248 | return this; | |
1249 | ||
1250 | // Get INQUIRY | |
1251 | unsigned char req_buff[64] = {0, }; | |
1252 | int req_len = 36; | |
1253 | if (scsiStdInquiry(this, req_buff, req_len)) { | |
1254 | close(); | |
1255 | set_err(EIO, "INQUIRY failed"); | |
1256 | return this; | |
832b75ed | 1257 | } |
2127e193 GI |
1258 | |
1259 | int avail_len = req_buff[4] + 5; | |
1260 | int len = (avail_len < req_len ? avail_len : req_len); | |
1261 | if (len < 36) | |
1262 | return this; | |
1263 | ||
bed94269 | 1264 | if (report) |
cfbba5b9 | 1265 | pout("Got MegaRAID inquiry.. %s\n", req_buff+8); |
2127e193 GI |
1266 | |
1267 | // Use INQUIRY to detect type | |
bed94269 | 1268 | { |
ee38a438 | 1269 | // SAT? |
bed94269 | 1270 | ata_device * newdev = smi()->autodetect_sat_device(this, req_buff, len); |
ee38a438 | 1271 | if (newdev) // NOTE: 'this' is now owned by '*newdev' |
2127e193 | 1272 | return newdev; |
832b75ed | 1273 | } |
832b75ed | 1274 | |
2127e193 GI |
1275 | // Nothing special found |
1276 | return this; | |
1277 | } | |
832b75ed | 1278 | |
2127e193 GI |
1279 | bool linux_megaraid_device::open() |
1280 | { | |
1281 | char line[128]; | |
ee38a438 | 1282 | int mjr; |
cfbba5b9 | 1283 | int report = scsi_debugmode; |
a37e7145 | 1284 | |
a86ec89e | 1285 | if (sscanf(get_dev_name(), "/dev/bus/%u", &m_hba) == 0) { |
ee38a438 GI |
1286 | if (!linux_smart_device::open()) |
1287 | return false; | |
1288 | /* Get device HBA */ | |
1289 | struct sg_scsi_id sgid; | |
1290 | if (ioctl(get_fd(), SG_GET_SCSI_ID, &sgid) == 0) { | |
1291 | m_hba = sgid.host_no; | |
1292 | } | |
1293 | else if (ioctl(get_fd(), SCSI_IOCTL_GET_BUS_NUMBER, &m_hba) != 0) { | |
1294 | int err = errno; | |
1295 | linux_smart_device::close(); | |
1296 | return set_err(err, "can't get bus number"); | |
ff28b140 | 1297 | } // we don't need this device anymore |
2127e193 | 1298 | linux_smart_device::close(); |
2127e193 | 1299 | } |
2127e193 | 1300 | /* Perform mknod of device ioctl node */ |
ee38a438 | 1301 | FILE * fp = fopen("/proc/devices", "r"); |
ff28b140 TL |
1302 | if (fp) { |
1303 | while (fgets(line, sizeof(line), fp) != NULL) { | |
1304 | int n1 = 0; | |
1305 | if (sscanf(line, "%d megaraid_sas_ioctl%n", &mjr, &n1) == 1 && n1 == 22) { | |
1306 | n1=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR, makedev(mjr, 0)); | |
1307 | if(report > 0) | |
1308 | pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1 >= 0 ? 0 : errno); | |
1309 | if (n1 >= 0 || errno == EEXIST) | |
1310 | break; | |
1311 | } | |
1312 | else if (sscanf(line, "%d megadev%n", &mjr, &n1) == 1 && n1 == 11) { | |
1313 | n1=mknod("/dev/megadev0", S_IFCHR, makedev(mjr, 0)); | |
1314 | if(report > 0) | |
1315 | pout("Creating /dev/megadev0 = %d\n", n1 >= 0 ? 0 : errno); | |
1316 | if (n1 >= 0 || errno == EEXIST) | |
1317 | break; | |
1318 | } | |
ee38a438 | 1319 | } |
ff28b140 | 1320 | fclose(fp); |
2127e193 | 1321 | } |
2127e193 GI |
1322 | |
1323 | /* Open Device IOCTL node */ | |
1324 | if ((m_fd = ::open("/dev/megaraid_sas_ioctl_node", O_RDWR)) >= 0) { | |
1325 | pt_cmd = &linux_megaraid_device::megasas_cmd; | |
1326 | } | |
1327 | else if ((m_fd = ::open("/dev/megadev0", O_RDWR)) >= 0) { | |
1328 | pt_cmd = &linux_megaraid_device::megadev_cmd; | |
1329 | } | |
1330 | else { | |
1331 | int err = errno; | |
1332 | linux_smart_device::close(); | |
1333 | return set_err(err, "cannot open /dev/megaraid_sas_ioctl_node or /dev/megadev0"); | |
1334 | } | |
ee38a438 | 1335 | set_fd(m_fd); |
2127e193 GI |
1336 | return true; |
1337 | } | |
1338 | ||
1339 | bool linux_megaraid_device::close() | |
1340 | { | |
1341 | if (m_fd >= 0) | |
1342 | ::close(m_fd); | |
1343 | m_fd = -1; m_hba = 0; pt_cmd = 0; | |
ee38a438 GI |
1344 | set_fd(m_fd); |
1345 | return true; | |
2127e193 GI |
1346 | } |
1347 | ||
1348 | bool linux_megaraid_device::scsi_pass_through(scsi_cmnd_io *iop) | |
1349 | { | |
cfbba5b9 | 1350 | int report = scsi_debugmode; |
2127e193 GI |
1351 | |
1352 | if (report > 0) { | |
1353 | int k, j; | |
1354 | const unsigned char * ucp = iop->cmnd; | |
1355 | const char * np; | |
1356 | char buff[256]; | |
1357 | const int sz = (int)sizeof(buff); | |
1358 | ||
1359 | np = scsi_get_opcode_name(ucp[0]); | |
1360 | j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>"); | |
1361 | for (k = 0; k < (int)iop->cmnd_len; ++k) | |
1362 | j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]); | |
1363 | if ((report > 1) && | |
1364 | (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) { | |
1365 | int trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
1366 | ||
a86ec89e GI |
1367 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing " |
1368 | "data, len=%d%s:\n", (int)iop->dxfer_len, | |
1369 | (trunc ? " [only first 256 bytes shown]" : "")); | |
ff28b140 | 1370 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1); |
2127e193 GI |
1371 | } |
1372 | else | |
a86ec89e | 1373 | snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n"); |
2127e193 GI |
1374 | pout("%s", buff); |
1375 | } | |
1376 | ||
cfbba5b9 | 1377 | // Controller rejects Test Unit Ready |
2127e193 GI |
1378 | if (iop->cmnd[0] == 0x00) |
1379 | return true; | |
cfbba5b9 | 1380 | |
d008864d | 1381 | if (iop->cmnd[0] == SAT_ATA_PASSTHROUGH_12 || iop->cmnd[0] == SAT_ATA_PASSTHROUGH_16) { |
cfbba5b9 GI |
1382 | // Controller does not return ATA output registers in SAT sense data |
1383 | if (iop->cmnd[2] & (1 << 5)) // chk_cond | |
1384 | return set_err(ENOSYS, "ATA return descriptor not supported by controller firmware"); | |
2127e193 | 1385 | } |
d008864d | 1386 | // SMART WRITE LOG SECTOR causing media errors |
ee38a438 GI |
1387 | if ((iop->cmnd[0] == SAT_ATA_PASSTHROUGH_16 // SAT16 WRITE LOG |
1388 | && iop->cmnd[14] == ATA_SMART_CMD && iop->cmnd[3]==0 && iop->cmnd[4] == ATA_SMART_WRITE_LOG_SECTOR) || | |
1389 | (iop->cmnd[0] == SAT_ATA_PASSTHROUGH_12 // SAT12 WRITE LOG | |
1390 | && iop->cmnd[9] == ATA_SMART_CMD && iop->cmnd[3] == ATA_SMART_WRITE_LOG_SECTOR)) | |
1391 | { | |
1392 | if(!failuretest_permissive) | |
1393 | return set_err(ENOSYS, "SMART WRITE LOG SECTOR may cause problems, try with -T permissive to force"); | |
1394 | } | |
2127e193 GI |
1395 | if (pt_cmd == NULL) |
1396 | return false; | |
ee38a438 | 1397 | return (this->*pt_cmd)(iop->cmnd_len, iop->cmnd, |
2127e193 | 1398 | iop->dxfer_len, iop->dxferp, |
ee38a438 | 1399 | iop->max_sense_len, iop->sensep, report, iop->dxfer_dir); |
2127e193 GI |
1400 | } |
1401 | ||
1402 | /* Issue passthrough scsi command to PERC5/6 controllers */ | |
1403 | bool linux_megaraid_device::megasas_cmd(int cdbLen, void *cdb, | |
1404 | int dataLen, void *data, | |
ee38a438 | 1405 | int /*senseLen*/, void * /*sense*/, int /*report*/, int dxfer_dir) |
2127e193 GI |
1406 | { |
1407 | struct megasas_pthru_frame *pthru; | |
1408 | struct megasas_iocpacket uio; | |
2127e193 GI |
1409 | |
1410 | memset(&uio, 0, sizeof(uio)); | |
cfbba5b9 | 1411 | pthru = &uio.frame.pthru; |
2127e193 GI |
1412 | pthru->cmd = MFI_CMD_PD_SCSI_IO; |
1413 | pthru->cmd_status = 0xFF; | |
1414 | pthru->scsi_status = 0x0; | |
1415 | pthru->target_id = m_disknum; | |
1416 | pthru->lun = 0; | |
1417 | pthru->cdb_len = cdbLen; | |
1418 | pthru->timeout = 0; | |
ee38a438 GI |
1419 | switch (dxfer_dir) { |
1420 | case DXFER_NONE: | |
1421 | pthru->flags = MFI_FRAME_DIR_NONE; | |
1422 | break; | |
1423 | case DXFER_FROM_DEVICE: | |
1424 | pthru->flags = MFI_FRAME_DIR_READ; | |
1425 | break; | |
1426 | case DXFER_TO_DEVICE: | |
1427 | pthru->flags = MFI_FRAME_DIR_WRITE; | |
1428 | break; | |
1429 | default: | |
1430 | pout("megasas_cmd: bad dxfer_dir\n"); | |
1431 | return set_err(EINVAL, "megasas_cmd: bad dxfer_dir\n"); | |
1432 | } | |
1433 | ||
cfbba5b9 GI |
1434 | if (dataLen > 0) { |
1435 | pthru->sge_count = 1; | |
1436 | pthru->data_xfer_len = dataLen; | |
1437 | pthru->sgl.sge32[0].phys_addr = (intptr_t)data; | |
1438 | pthru->sgl.sge32[0].length = (uint32_t)dataLen; | |
1439 | } | |
2127e193 GI |
1440 | memcpy(pthru->cdb, cdb, cdbLen); |
1441 | ||
1442 | uio.host_no = m_hba; | |
cfbba5b9 GI |
1443 | if (dataLen > 0) { |
1444 | uio.sge_count = 1; | |
1445 | uio.sgl_off = offsetof(struct megasas_pthru_frame, sgl); | |
1446 | uio.sgl[0].iov_base = data; | |
1447 | uio.sgl[0].iov_len = dataLen; | |
1448 | } | |
2127e193 | 1449 | |
2127e193 | 1450 | errno = 0; |
a86ec89e | 1451 | int rc = ioctl(m_fd, MEGASAS_IOC_FIRMWARE, &uio); |
2127e193 GI |
1452 | if (pthru->cmd_status || rc != 0) { |
1453 | if (pthru->cmd_status == 12) { | |
1454 | return set_err(EIO, "megasas_cmd: Device %d does not exist\n", m_disknum); | |
1455 | } | |
1456 | return set_err((errno ? errno : EIO), "megasas_cmd result: %d.%d = %d/%d", | |
1457 | m_hba, m_disknum, errno, | |
1458 | pthru->cmd_status); | |
1459 | } | |
1460 | return true; | |
1461 | } | |
1462 | ||
1463 | /* Issue passthrough scsi commands to PERC2/3/4 controllers */ | |
1464 | bool linux_megaraid_device::megadev_cmd(int cdbLen, void *cdb, | |
1465 | int dataLen, void *data, | |
ee38a438 | 1466 | int /*senseLen*/, void * /*sense*/, int /*report*/, int /* dir */) |
2127e193 GI |
1467 | { |
1468 | struct uioctl_t uio; | |
1469 | int rc; | |
1470 | ||
2127e193 GI |
1471 | /* Don't issue to the controller */ |
1472 | if (m_disknum == 7) | |
1473 | return false; | |
1474 | ||
1475 | memset(&uio, 0, sizeof(uio)); | |
1476 | uio.inlen = dataLen; | |
1477 | uio.outlen = dataLen; | |
1478 | ||
1479 | memset(data, 0, dataLen); | |
1480 | uio.ui.fcs.opcode = 0x80; // M_RD_IOCTL_CMD | |
1481 | uio.ui.fcs.adapno = MKADAP(m_hba); | |
1482 | ||
1483 | uio.data.pointer = (uint8_t *)data; | |
1484 | ||
1485 | uio.mbox.cmd = MEGA_MBOXCMD_PASSTHRU; | |
1486 | uio.mbox.xferaddr = (intptr_t)&uio.pthru; | |
1487 | ||
1488 | uio.pthru.ars = 1; | |
1489 | uio.pthru.timeout = 2; | |
1490 | uio.pthru.channel = 0; | |
1491 | uio.pthru.target = m_disknum; | |
1492 | uio.pthru.cdblen = cdbLen; | |
1493 | uio.pthru.reqsenselen = MAX_REQ_SENSE_LEN; | |
1494 | uio.pthru.dataxferaddr = (intptr_t)data; | |
1495 | uio.pthru.dataxferlen = dataLen; | |
1496 | memcpy(uio.pthru.cdb, cdb, cdbLen); | |
1497 | ||
1498 | rc=ioctl(m_fd, MEGAIOCCMD, &uio); | |
1499 | if (uio.pthru.scsistatus || rc != 0) { | |
1500 | return set_err((errno ? errno : EIO), "megadev_cmd result: %d.%d = %d/%d", | |
1501 | m_hba, m_disknum, errno, | |
1502 | uio.pthru.scsistatus); | |
1503 | } | |
1504 | return true; | |
1505 | } | |
1506 | ||
1507 | ///////////////////////////////////////////////////////////////////////////// | |
1508 | /// CCISS RAID support | |
1509 | ||
1510 | #ifdef HAVE_LINUX_CCISS_IOCTL_H | |
1511 | ||
1512 | class linux_cciss_device | |
1513 | : public /*implements*/ scsi_device, | |
1514 | public /*extends*/ linux_smart_device | |
1515 | { | |
1516 | public: | |
1517 | linux_cciss_device(smart_interface * intf, const char * name, unsigned char disknum); | |
1518 | ||
1519 | virtual bool scsi_pass_through(scsi_cmnd_io * iop); | |
1520 | ||
1521 | private: | |
1522 | unsigned char m_disknum; ///< Disk number. | |
1523 | }; | |
1524 | ||
1525 | linux_cciss_device::linux_cciss_device(smart_interface * intf, | |
1526 | const char * dev_name, unsigned char disknum) | |
1527 | : smart_device(intf, dev_name, "cciss", "cciss"), | |
1528 | linux_smart_device(O_RDWR | O_NONBLOCK), | |
1529 | m_disknum(disknum) | |
1530 | { | |
1531 | set_info().info_name = strprintf("%s [cciss_disk_%02d]", dev_name, disknum); | |
1532 | } | |
1533 | ||
1534 | bool linux_cciss_device::scsi_pass_through(scsi_cmnd_io * iop) | |
1535 | { | |
cfbba5b9 | 1536 | int status = cciss_io_interface(get_fd(), m_disknum, iop, scsi_debugmode); |
2127e193 GI |
1537 | if (status < 0) |
1538 | return set_err(-status); | |
1539 | return true; | |
1540 | } | |
1541 | ||
1542 | #endif // HAVE_LINUX_CCISS_IOCTL_H | |
1543 | ||
1544 | ///////////////////////////////////////////////////////////////////////////// | |
1545 | /// AMCC/3ware RAID support | |
1546 | ||
1547 | class linux_escalade_device | |
1548 | : public /*implements*/ ata_device, | |
1549 | public /*extends*/ linux_smart_device | |
1550 | { | |
1551 | public: | |
1552 | enum escalade_type_t { | |
1553 | AMCC_3WARE_678K, | |
1554 | AMCC_3WARE_678K_CHAR, | |
cfbba5b9 GI |
1555 | AMCC_3WARE_9000_CHAR, |
1556 | AMCC_3WARE_9700_CHAR | |
2127e193 GI |
1557 | }; |
1558 | ||
1559 | linux_escalade_device(smart_interface * intf, const char * dev_name, | |
1560 | escalade_type_t escalade_type, int disknum); | |
1561 | ||
1562 | virtual bool open(); | |
1563 | ||
1564 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); | |
1565 | ||
1566 | private: | |
1567 | escalade_type_t m_escalade_type; ///< Controller type | |
1568 | int m_disknum; ///< Disk number. | |
1569 | }; | |
1570 | ||
1571 | linux_escalade_device::linux_escalade_device(smart_interface * intf, const char * dev_name, | |
1572 | escalade_type_t escalade_type, int disknum) | |
1573 | : smart_device(intf, dev_name, "3ware", "3ware"), | |
1574 | linux_smart_device(O_RDONLY | O_NONBLOCK), | |
1575 | m_escalade_type(escalade_type), m_disknum(disknum) | |
1576 | { | |
1577 | set_info().info_name = strprintf("%s [3ware_disk_%02d]", dev_name, disknum); | |
1578 | } | |
1579 | ||
1580 | /* This function will setup and fix device nodes for a 3ware controller. */ | |
1581 | #define MAJOR_STRING_LENGTH 3 | |
1582 | #define DEVICE_STRING_LENGTH 32 | |
1583 | #define NODE_STRING_LENGTH 16 | |
cfbba5b9 GI |
1584 | static int setup_3ware_nodes(const char *nodename, const char *driver_name) |
1585 | { | |
2127e193 GI |
1586 | int tw_major = 0; |
1587 | int index = 0; | |
1588 | char majorstring[MAJOR_STRING_LENGTH+1]; | |
1589 | char device_name[DEVICE_STRING_LENGTH+1]; | |
1590 | char nodestring[NODE_STRING_LENGTH]; | |
1591 | struct stat stat_buf; | |
1592 | FILE *file; | |
1593 | int retval = 0; | |
ff28b140 | 1594 | #ifdef HAVE_LIBSELINUX |
2127e193 GI |
1595 | security_context_t orig_context = NULL; |
1596 | security_context_t node_context = NULL; | |
1597 | int selinux_enabled = is_selinux_enabled(); | |
1598 | int selinux_enforced = security_getenforce(); | |
1599 | #endif | |
1600 | ||
2127e193 GI |
1601 | /* First try to open up /proc/devices */ |
1602 | if (!(file = fopen("/proc/devices", "r"))) { | |
1603 | pout("Error opening /proc/devices to check/create 3ware device nodes\n"); | |
1604 | syserror("fopen"); | |
1605 | return 0; // don't fail here: user might not have /proc ! | |
1606 | } | |
1607 | ||
1608 | /* Attempt to get device major number */ | |
1609 | while (EOF != fscanf(file, "%3s %32s", majorstring, device_name)) { | |
1610 | majorstring[MAJOR_STRING_LENGTH]='\0'; | |
1611 | device_name[DEVICE_STRING_LENGTH]='\0'; | |
1612 | if (!strncmp(device_name, nodename, DEVICE_STRING_LENGTH)) { | |
1613 | tw_major = atoi(majorstring); | |
1614 | break; | |
1615 | } | |
1616 | } | |
1617 | fclose(file); | |
1618 | ||
1619 | /* See if we found a major device number */ | |
1620 | if (!tw_major) { | |
1621 | pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename, driver_name); | |
1622 | return 2; | |
1623 | } | |
ff28b140 | 1624 | #ifdef HAVE_LIBSELINUX |
2127e193 GI |
1625 | /* Prepare a database of contexts for files in /dev |
1626 | * and save the current context */ | |
1627 | if (selinux_enabled) { | |
1628 | if (matchpathcon_init_prefix(NULL, "/dev") < 0) | |
1629 | pout("Error initializing contexts database for /dev"); | |
1630 | if (getfscreatecon(&orig_context) < 0) { | |
1631 | pout("Error retrieving original SELinux fscreate context"); | |
ff28b140 | 1632 | if (selinux_enforced) { |
2127e193 GI |
1633 | matchpathcon_fini(); |
1634 | return 6; | |
1635 | } | |
ff28b140 | 1636 | } |
2127e193 GI |
1637 | } |
1638 | #endif | |
1639 | /* Now check if nodes are correct */ | |
1640 | for (index=0; index<16; index++) { | |
ee38a438 | 1641 | snprintf(nodestring, sizeof(nodestring), "/dev/%s%d", nodename, index); |
ff28b140 | 1642 | #ifdef HAVE_LIBSELINUX |
2127e193 GI |
1643 | /* Get context of the node and set it as the default */ |
1644 | if (selinux_enabled) { | |
1645 | if (matchpathcon(nodestring, S_IRUSR | S_IWUSR, &node_context) < 0) { | |
1646 | pout("Could not retrieve context for %s", nodestring); | |
1647 | if (selinux_enforced) { | |
1648 | retval = 6; | |
1649 | break; | |
1650 | } | |
1651 | } | |
1652 | if (setfscreatecon(node_context) < 0) { | |
1653 | pout ("Error setting default fscreate context"); | |
1654 | if (selinux_enforced) { | |
1655 | retval = 6; | |
1656 | break; | |
1657 | } | |
1658 | } | |
1659 | } | |
1660 | #endif | |
1661 | /* Try to stat the node */ | |
1662 | if ((stat(nodestring, &stat_buf))) { | |
1663 | pout("Node %s does not exist and must be created. Check the udev rules.\n", nodestring); | |
1664 | /* Create a new node if it doesn't exist */ | |
1665 | if (mknod(nodestring, S_IFCHR|0600, makedev(tw_major, index))) { | |
1666 | pout("problem creating 3ware device nodes %s", nodestring); | |
1667 | syserror("mknod"); | |
1668 | retval = 3; | |
1669 | break; | |
1670 | } else { | |
ff28b140 | 1671 | #ifdef HAVE_LIBSELINUX |
2127e193 GI |
1672 | if (selinux_enabled && node_context) { |
1673 | freecon(node_context); | |
1674 | node_context = NULL; | |
1675 | } | |
1676 | #endif | |
1677 | continue; | |
1678 | } | |
1679 | } | |
1680 | ||
1681 | /* See if nodes major and minor numbers are correct */ | |
1682 | if ((tw_major != (int)(major(stat_buf.st_rdev))) || | |
1683 | (index != (int)(minor(stat_buf.st_rdev))) || | |
1684 | (!S_ISCHR(stat_buf.st_mode))) { | |
1685 | pout("Node %s has wrong major/minor number and must be created anew." | |
1686 | " Check the udev rules.\n", nodestring); | |
1687 | /* Delete the old node */ | |
1688 | if (unlink(nodestring)) { | |
1689 | pout("problem unlinking stale 3ware device node %s", nodestring); | |
1690 | syserror("unlink"); | |
1691 | retval = 4; | |
1692 | break; | |
1693 | } | |
1694 | ||
1695 | /* Make a new node */ | |
1696 | if (mknod(nodestring, S_IFCHR|0600, makedev(tw_major, index))) { | |
1697 | pout("problem creating 3ware device nodes %s", nodestring); | |
1698 | syserror("mknod"); | |
1699 | retval = 5; | |
1700 | break; | |
1701 | } | |
1702 | } | |
ff28b140 | 1703 | #ifdef HAVE_LIBSELINUX |
2127e193 GI |
1704 | if (selinux_enabled && node_context) { |
1705 | freecon(node_context); | |
1706 | node_context = NULL; | |
1707 | } | |
1708 | #endif | |
1709 | } | |
1710 | ||
ff28b140 | 1711 | #ifdef HAVE_LIBSELINUX |
2127e193 GI |
1712 | if (selinux_enabled) { |
1713 | if(setfscreatecon(orig_context) < 0) { | |
1714 | pout("Error re-setting original fscreate context"); | |
1715 | if (selinux_enforced) | |
1716 | retval = 6; | |
1717 | } | |
1718 | if(orig_context) | |
1719 | freecon(orig_context); | |
1720 | if(node_context) | |
1721 | freecon(node_context); | |
1722 | matchpathcon_fini(); | |
1723 | } | |
1724 | #endif | |
1725 | return retval; | |
1726 | } | |
1727 | ||
1728 | bool linux_escalade_device::open() | |
1729 | { | |
cfbba5b9 GI |
1730 | if (m_escalade_type == AMCC_3WARE_9700_CHAR || m_escalade_type == AMCC_3WARE_9000_CHAR || |
1731 | m_escalade_type == AMCC_3WARE_678K_CHAR) { | |
2127e193 GI |
1732 | // the device nodes for these controllers are dynamically assigned, |
1733 | // so we need to check that they exist with the correct major | |
1734 | // numbers and if not, create them | |
cfbba5b9 GI |
1735 | const char * node = (m_escalade_type == AMCC_3WARE_9700_CHAR ? "twl" : |
1736 | m_escalade_type == AMCC_3WARE_9000_CHAR ? "twa" : | |
1737 | "twe" ); | |
1738 | const char * driver = (m_escalade_type == AMCC_3WARE_9700_CHAR ? "3w-sas" : | |
1739 | m_escalade_type == AMCC_3WARE_9000_CHAR ? "3w-9xxx" : | |
1740 | "3w-xxxx" ); | |
2127e193 GI |
1741 | if (setup_3ware_nodes(node, driver)) |
1742 | return set_err((errno ? errno : ENXIO), "setup_3ware_nodes(\"%s\", \"%s\") failed", node, driver); | |
1743 | } | |
1744 | // Continue with default open | |
1745 | return linux_smart_device::open(); | |
1746 | } | |
1747 | ||
1748 | // TODO: Function no longer useful | |
1749 | //void printwarning(smart_command_set command); | |
1750 | ||
1751 | // PURPOSE | |
1752 | // This is an interface routine meant to isolate the OS dependent | |
1753 | // parts of the code, and to provide a debugging interface. Each | |
1754 | // different port and OS needs to provide it's own interface. This | |
1755 | // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA | |
1756 | // commands to be passed through the SCSI driver. | |
1757 | // DETAILED DESCRIPTION OF ARGUMENTS | |
1758 | // fd: is the file descriptor provided by open() | |
1759 | // disknum is the disk number (0 to 15) in the RAID array | |
1760 | // escalade_type indicates the type of controller type, and if scsi or char interface is used | |
1761 | // command: defines the different operations. | |
1762 | // select: additional input data if needed (which log, which type of | |
1763 | // self-test). | |
1764 | // data: location to write output data, if needed (512 bytes). | |
1765 | // Note: not all commands use all arguments. | |
1766 | // RETURN VALUES | |
1767 | // -1 if the command failed | |
1768 | // 0 if the command succeeded, | |
1769 | // STATUS_CHECK routine: | |
1770 | // -1 if the command failed | |
1771 | // 0 if the command succeeded and disk SMART status is "OK" | |
1772 | // 1 if the command succeeded and disk SMART status is "FAILING" | |
1773 | ||
2127e193 GI |
1774 | /* 512 is the max payload size: increase if needed */ |
1775 | #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed | |
1776 | #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed | |
1777 | #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed | |
1778 | #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) ) | |
1779 | ||
1780 | bool linux_escalade_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) | |
1781 | { | |
1782 | if (!ata_cmd_is_ok(in, | |
1783 | true, // data_out_support | |
1784 | false, // TODO: multi_sector_support | |
1785 | true) // ata_48bit_support | |
1786 | ) | |
1787 | return false; | |
1788 | ||
1789 | // Used by both the SCSI and char interfaces | |
1790 | TW_Passthru *passthru=NULL; | |
1791 | char ioctl_buffer[TW_IOCTL_BUFFER_SIZE]; | |
1792 | ||
1793 | // only used for SCSI device interface | |
1794 | TW_Ioctl *tw_ioctl=NULL; | |
1795 | TW_Output *tw_output=NULL; | |
1796 | ||
1797 | // only used for 6000/7000/8000 char device interface | |
1798 | TW_New_Ioctl *tw_ioctl_char=NULL; | |
1799 | ||
1800 | // only used for 9000 character device interface | |
1801 | TW_Ioctl_Buf_Apache *tw_ioctl_apache=NULL; | |
1802 | ||
1803 | memset(ioctl_buffer, 0, TW_IOCTL_BUFFER_SIZE); | |
1804 | ||
1805 | // TODO: Handle controller differences by different classes | |
cfbba5b9 | 1806 | if (m_escalade_type == AMCC_3WARE_9700_CHAR || m_escalade_type == AMCC_3WARE_9000_CHAR) { |
2127e193 GI |
1807 | tw_ioctl_apache = (TW_Ioctl_Buf_Apache *)ioctl_buffer; |
1808 | tw_ioctl_apache->driver_command.control_code = TW_IOCTL_FIRMWARE_PASS_THROUGH; | |
1809 | tw_ioctl_apache->driver_command.buffer_length = 512; /* payload size */ | |
1810 | passthru = (TW_Passthru *)&(tw_ioctl_apache->firmware_command.command.oldcommand); | |
1811 | } | |
1812 | else if (m_escalade_type==AMCC_3WARE_678K_CHAR) { | |
1813 | tw_ioctl_char = (TW_New_Ioctl *)ioctl_buffer; | |
1814 | tw_ioctl_char->data_buffer_length = 512; | |
1815 | passthru = (TW_Passthru *)&(tw_ioctl_char->firmware_command); | |
1816 | } | |
1817 | else if (m_escalade_type==AMCC_3WARE_678K) { | |
1818 | tw_ioctl = (TW_Ioctl *)ioctl_buffer; | |
1819 | tw_ioctl->cdb[0] = TW_IOCTL; | |
1820 | tw_ioctl->opcode = TW_ATA_PASSTHRU; | |
1821 | tw_ioctl->input_length = 512; // correct even for non-data commands | |
1822 | tw_ioctl->output_length = 512; // correct even for non-data commands | |
1823 | tw_output = (TW_Output *)tw_ioctl; | |
1824 | passthru = (TW_Passthru *)&(tw_ioctl->input_data); | |
1825 | } | |
1826 | else { | |
1827 | return set_err(ENOSYS, | |
1828 | "Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n" | |
1829 | "Please contact " PACKAGE_BUGREPORT "\n", (int)m_escalade_type, m_disknum); | |
1830 | } | |
1831 | ||
1832 | // Same for (almost) all commands - but some reset below | |
1833 | passthru->byte0.opcode = TW_OP_ATA_PASSTHRU; | |
1834 | passthru->request_id = 0xFF; | |
1835 | passthru->unit = m_disknum; | |
1836 | passthru->status = 0; | |
1837 | passthru->flags = 0x1; | |
1838 | ||
1839 | // Set registers | |
1840 | { | |
1841 | const ata_in_regs_48bit & r = in.in_regs; | |
1842 | passthru->features = r.features_16; | |
1843 | passthru->sector_count = r.sector_count_16; | |
1844 | passthru->sector_num = r.lba_low_16; | |
1845 | passthru->cylinder_lo = r.lba_mid_16; | |
1846 | passthru->cylinder_hi = r.lba_high_16; | |
1847 | passthru->drive_head = r.device; | |
1848 | passthru->command = r.command; | |
1849 | } | |
1850 | ||
1851 | // Is this a command that reads or returns 512 bytes? | |
1852 | // passthru->param values are: | |
1853 | // 0x0 - non data command without TFR write check, | |
1854 | // 0x8 - non data command with TFR write check, | |
1855 | // 0xD - data command that returns data to host from device | |
1856 | // 0xF - data command that writes data from host to device | |
1857 | // passthru->size values are 0x5 for non-data and 0x07 for data | |
1858 | bool readdata = false; | |
1859 | if (in.direction == ata_cmd_in::data_in) { | |
1860 | readdata=true; | |
1861 | passthru->byte0.sgloff = 0x5; | |
1862 | passthru->size = 0x7; // TODO: Other value for multi-sector ? | |
832b75ed | 1863 | passthru->param = 0xD; |
832b75ed GG |
1864 | // For 64-bit to work correctly, up the size of the command packet |
1865 | // in dwords by 1 to account for the 64-bit single sgl 'address' | |
1866 | // field. Note that this doesn't agree with the typedefs but it's | |
1867 | // right (agree with kernel driver behavior/typedefs). | |
cfbba5b9 GI |
1868 | if ((m_escalade_type == AMCC_3WARE_9700_CHAR || m_escalade_type == AMCC_3WARE_9000_CHAR) |
1869 | && sizeof(long) == 8) | |
832b75ed GG |
1870 | passthru->size++; |
1871 | } | |
2127e193 | 1872 | else if (in.direction == ata_cmd_in::no_data) { |
a37e7145 GG |
1873 | // Non data command -- but doesn't use large sector |
1874 | // count register values. | |
832b75ed GG |
1875 | passthru->byte0.sgloff = 0x0; |
1876 | passthru->size = 0x5; | |
1877 | passthru->param = 0x8; | |
1878 | passthru->sector_count = 0x0; | |
1879 | } | |
2127e193 | 1880 | else if (in.direction == ata_cmd_in::data_out) { |
cfbba5b9 | 1881 | if (m_escalade_type == AMCC_3WARE_9700_CHAR || m_escalade_type == AMCC_3WARE_9000_CHAR) |
2127e193 GI |
1882 | memcpy(tw_ioctl_apache->data_buffer, in.buffer, in.size); |
1883 | else if (m_escalade_type == AMCC_3WARE_678K_CHAR) | |
1884 | memcpy(tw_ioctl_char->data_buffer, in.buffer, in.size); | |
832b75ed GG |
1885 | else { |
1886 | // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE | |
1887 | // memcpy(tw_output->output_data, data, 512); | |
2127e193 GI |
1888 | // printwarning(command); // TODO: Parameter no longer valid |
1889 | return set_err(ENOTSUP, "DATA OUT not supported for this 3ware controller type"); | |
832b75ed | 1890 | } |
2127e193 GI |
1891 | passthru->byte0.sgloff = 0x5; |
1892 | passthru->size = 0x7; // TODO: Other value for multi-sector ? | |
832b75ed | 1893 | passthru->param = 0xF; // PIO data write |
cfbba5b9 GI |
1894 | if ((m_escalade_type == AMCC_3WARE_9700_CHAR || m_escalade_type == AMCC_3WARE_9000_CHAR) |
1895 | && sizeof(long) == 8) | |
2127e193 | 1896 | passthru->size++; |
832b75ed | 1897 | } |
2127e193 | 1898 | else |
bed94269 | 1899 | return set_err(EINVAL); |
832b75ed GG |
1900 | |
1901 | // Now send the command down through an ioctl() | |
2127e193 | 1902 | int ioctlreturn; |
cfbba5b9 | 1903 | if (m_escalade_type == AMCC_3WARE_9700_CHAR || m_escalade_type == AMCC_3WARE_9000_CHAR) |
2127e193 GI |
1904 | ioctlreturn=ioctl(get_fd(), TW_IOCTL_FIRMWARE_PASS_THROUGH, tw_ioctl_apache); |
1905 | else if (m_escalade_type==AMCC_3WARE_678K_CHAR) | |
1906 | ioctlreturn=ioctl(get_fd(), TW_CMD_PACKET_WITH_DATA, tw_ioctl_char); | |
832b75ed | 1907 | else |
2127e193 | 1908 | ioctlreturn=ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND, tw_ioctl); |
a37e7145 | 1909 | |
832b75ed GG |
1910 | // Deal with the different error cases |
1911 | if (ioctlreturn) { | |
2127e193 GI |
1912 | if (AMCC_3WARE_678K==m_escalade_type |
1913 | && in.in_regs.command==ATA_SMART_CMD | |
1914 | && ( in.in_regs.features == ATA_SMART_AUTO_OFFLINE | |
1915 | || in.in_regs.features == ATA_SMART_AUTOSAVE ) | |
1916 | && in.in_regs.lba_low) { | |
832b75ed | 1917 | // error here is probably a kernel driver whose version is too old |
2127e193 GI |
1918 | // printwarning(command); // TODO: Parameter no longer valid |
1919 | return set_err(ENOTSUP, "Probably kernel driver too old"); | |
832b75ed | 1920 | } |
2127e193 GI |
1921 | return set_err(EIO); |
1922 | } | |
1923 | ||
1924 | // The passthru structure is valid after return from an ioctl if: | |
1925 | // - we are using the character interface OR | |
1926 | // - we are using the SCSI interface and this is a NON-READ-DATA command | |
1927 | // For SCSI interface, note that we set passthru to a different | |
1928 | // value after ioctl(). | |
1929 | if (AMCC_3WARE_678K==m_escalade_type) { | |
1930 | if (readdata) | |
1931 | passthru=NULL; | |
1932 | else | |
1933 | passthru=(TW_Passthru *)&(tw_output->output_data); | |
832b75ed | 1934 | } |
a37e7145 | 1935 | |
2127e193 GI |
1936 | // See if the ATA command failed. Now that we have returned from |
1937 | // the ioctl() call, if passthru is valid, then: | |
1938 | // - passthru->status contains the 3ware controller STATUS | |
1939 | // - passthru->command contains the ATA STATUS register | |
1940 | // - passthru->features contains the ATA ERROR register | |
1941 | // | |
1942 | // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS | |
1943 | // If bit 0 (error bit) is set, then ATA ERROR register is valid. | |
1944 | // While we *might* decode the ATA ERROR register, at the moment it | |
1945 | // doesn't make much sense: we don't care in detail why the error | |
1946 | // happened. | |
1947 | ||
1948 | if (passthru && (passthru->status || (passthru->command & 0x21))) { | |
1949 | return set_err(EIO); | |
1950 | } | |
1951 | ||
1952 | // If this is a read data command, copy data to output buffer | |
1953 | if (readdata) { | |
cfbba5b9 | 1954 | if (m_escalade_type == AMCC_3WARE_9700_CHAR || m_escalade_type == AMCC_3WARE_9000_CHAR) |
2127e193 GI |
1955 | memcpy(in.buffer, tw_ioctl_apache->data_buffer, in.size); |
1956 | else if (m_escalade_type==AMCC_3WARE_678K_CHAR) | |
1957 | memcpy(in.buffer, tw_ioctl_char->data_buffer, in.size); | |
1958 | else | |
1959 | memcpy(in.buffer, tw_output->output_data, in.size); | |
1960 | } | |
1961 | ||
1962 | // Return register values | |
bed94269 | 1963 | if (passthru) { |
2127e193 GI |
1964 | ata_out_regs_48bit & r = out.out_regs; |
1965 | r.error = passthru->features; | |
1966 | r.sector_count_16 = passthru->sector_count; | |
1967 | r.lba_low_16 = passthru->sector_num; | |
1968 | r.lba_mid_16 = passthru->cylinder_lo; | |
1969 | r.lba_high_16 = passthru->cylinder_hi; | |
1970 | r.device = passthru->drive_head; | |
1971 | r.status = passthru->command; | |
1972 | } | |
1973 | ||
1974 | // look for nonexistent devices/ports | |
1975 | if ( in.in_regs.command == ATA_IDENTIFY_DEVICE | |
a23d5117 | 1976 | && !nonempty(in.buffer, in.size)) { |
2127e193 GI |
1977 | return set_err(ENODEV, "No drive on port %d", m_disknum); |
1978 | } | |
1979 | ||
1980 | return true; | |
1981 | } | |
1982 | ||
2127e193 GI |
1983 | ///////////////////////////////////////////////////////////////////////////// |
1984 | /// Areca RAID support | |
1985 | ||
ee38a438 GI |
1986 | /////////////////////////////////////////////////////////////////// |
1987 | // SATA(ATA) device behind Areca RAID Controller | |
1988 | class linux_areca_ata_device | |
1989 | : public /*implements*/ areca_ata_device, | |
2127e193 GI |
1990 | public /*extends*/ linux_smart_device |
1991 | { | |
1992 | public: | |
ee38a438 GI |
1993 | linux_areca_ata_device(smart_interface * intf, const char * dev_name, int disknum, int encnum = 1); |
1994 | virtual smart_device * autodetect_open(); | |
1995 | virtual bool arcmsr_lock(); | |
1996 | virtual bool arcmsr_unlock(); | |
1997 | virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io * iop); | |
2127e193 GI |
1998 | }; |
1999 | ||
ee38a438 GI |
2000 | /////////////////////////////////////////////////////////////////// |
2001 | // SAS(SCSI) device behind Areca RAID Controller | |
2002 | class linux_areca_scsi_device | |
2003 | : public /*implements*/ areca_scsi_device, | |
2004 | public /*extends*/ linux_smart_device | |
2127e193 | 2005 | { |
ee38a438 GI |
2006 | public: |
2007 | linux_areca_scsi_device(smart_interface * intf, const char * dev_name, int disknum, int encnum = 1); | |
2008 | virtual smart_device * autodetect_open(); | |
2009 | virtual bool arcmsr_lock(); | |
2010 | virtual bool arcmsr_unlock(); | |
2011 | virtual int arcmsr_do_scsi_io(struct scsi_cmnd_io * iop); | |
2012 | }; | |
2127e193 GI |
2013 | |
2014 | // Looks in /proc/scsi to suggest correct areca devices | |
ee38a438 | 2015 | static int find_areca_in_proc() |
cfbba5b9 | 2016 | { |
2127e193 GI |
2017 | const char* proc_format_string="host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n"; |
2018 | ||
2019 | // check data formwat | |
2020 | FILE *fp=fopen("/proc/scsi/sg/device_hdr", "r"); | |
2021 | if (!fp) { | |
2022 | pout("Unable to open /proc/scsi/sg/device_hdr for reading\n"); | |
2023 | return 1; | |
2024 | } | |
a37e7145 | 2025 | |
2127e193 GI |
2026 | // get line, compare to format |
2027 | char linebuf[256]; | |
2028 | linebuf[255]='\0'; | |
2029 | char *out = fgets(linebuf, 256, fp); | |
2030 | fclose(fp); | |
2031 | if (!out) { | |
2032 | pout("Unable to read contents of /proc/scsi/sg/device_hdr\n"); | |
2033 | return 2; | |
2034 | } | |
a37e7145 | 2035 | |
2127e193 GI |
2036 | if (strcmp(linebuf, proc_format_string)) { |
2037 | // wrong format! | |
2038 | // Fix this by comparing only tokens not white space!! | |
2039 | pout("Unexpected format %s in /proc/scsi/sg/device_hdr\n", proc_format_string); | |
2040 | return 3; | |
2041 | } | |
832b75ed | 2042 | |
2127e193 GI |
2043 | // Format is understood, now search for correct device |
2044 | fp=fopen("/proc/scsi/sg/devices", "r"); | |
2045 | if (!fp) return 1; | |
2046 | int host, chan, id, lun, type, opens, qdepth, busy, online; | |
2047 | int dev=-1; | |
2048 | int found=0; | |
2049 | // search all lines of /proc/scsi/sg/devices | |
2050 | while (9 == fscanf(fp, "%d %d %d %d %d %d %d %d %d", &host, &chan, &id, &lun, &type, &opens, &qdepth, &busy, &online)) { | |
2051 | dev++; | |
2052 | if (id == 16 && type == 3) { | |
2053 | // devices with id=16 and type=3 might be Areca controllers | |
2127e193 GI |
2054 | pout("Device /dev/sg%d appears to be an Areca controller.\n", dev); |
2055 | found++; | |
2056 | } | |
2057 | } | |
2058 | fclose(fp); | |
2059 | return 0; | |
2060 | } | |
a37e7145 | 2061 | |
ee38a438 GI |
2062 | // Areca RAID Controller(SATA Disk) |
2063 | linux_areca_ata_device::linux_areca_ata_device(smart_interface * intf, const char * dev_name, int disknum, int encnum) | |
2064 | : smart_device(intf, dev_name, "areca", "areca"), | |
2065 | linux_smart_device(O_RDWR | O_EXCL | O_NONBLOCK) | |
2066 | { | |
2067 | set_disknum(disknum); | |
2068 | set_encnum(encnum); | |
2069 | set_info().info_name = strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name, disknum, encnum); | |
2127e193 | 2070 | } |
a37e7145 | 2071 | |
ee38a438 GI |
2072 | smart_device * linux_areca_ata_device::autodetect_open() |
2073 | { | |
ee38a438 | 2074 | // autodetect device type |
a86ec89e | 2075 | int is_ata = arcmsr_get_dev_type(); |
ee38a438 GI |
2076 | if(is_ata < 0) |
2077 | { | |
2078 | set_err(EIO); | |
2079 | return this; | |
2080 | } | |
2127e193 | 2081 | |
ee38a438 GI |
2082 | if(is_ata == 1) |
2083 | { | |
2084 | // SATA device | |
2085 | return this; | |
2086 | } | |
2127e193 | 2087 | |
ee38a438 GI |
2088 | // SAS device |
2089 | smart_device_auto_ptr newdev(new linux_areca_scsi_device(smi(), get_dev_name(), get_disknum(), get_encnum())); | |
2090 | close(); | |
2091 | delete this; | |
2092 | newdev->open(); // TODO: Can possibly pass open fd | |
2127e193 | 2093 | |
ee38a438 GI |
2094 | return newdev.release(); |
2095 | } | |
2127e193 | 2096 | |
ee38a438 GI |
2097 | int linux_areca_ata_device::arcmsr_do_scsi_io(struct scsi_cmnd_io * iop) |
2098 | { | |
2099 | int ioctlreturn = 0; | |
2127e193 | 2100 | |
ee38a438 GI |
2101 | if(!is_open()) { |
2102 | if(!open()){ | |
2103 | find_areca_in_proc(); | |
2104 | } | |
2105 | } | |
2127e193 | 2106 | |
ee38a438 GI |
2107 | ioctlreturn = do_normal_scsi_cmnd_io(get_fd(), iop, scsi_debugmode); |
2108 | if ( ioctlreturn || iop->scsi_status ) | |
2109 | { | |
2110 | // errors found | |
2111 | return -1; | |
2112 | } | |
2127e193 | 2113 | |
ee38a438 | 2114 | return ioctlreturn; |
2127e193 | 2115 | } |
a37e7145 | 2116 | |
ee38a438 GI |
2117 | bool linux_areca_ata_device::arcmsr_lock() |
2118 | { | |
2119 | return true; | |
2120 | } | |
a37e7145 | 2121 | |
ee38a438 GI |
2122 | bool linux_areca_ata_device::arcmsr_unlock() |
2123 | { | |
2124 | return true; | |
2125 | } | |
2126 | ||
2127 | // Areca RAID Controller(SAS Device) | |
2128 | linux_areca_scsi_device::linux_areca_scsi_device(smart_interface * intf, const char * dev_name, int disknum, int encnum) | |
2127e193 | 2129 | : smart_device(intf, dev_name, "areca", "areca"), |
ee38a438 | 2130 | linux_smart_device(O_RDWR | O_EXCL | O_NONBLOCK) |
2127e193 | 2131 | { |
ee38a438 GI |
2132 | set_disknum(disknum); |
2133 | set_encnum(encnum); | |
f4e463df | 2134 | set_info().info_name = strprintf("%s [areca_disk#%02d_enc#%02d]", dev_name, disknum, encnum); |
2127e193 GI |
2135 | } |
2136 | ||
ee38a438 GI |
2137 | smart_device * linux_areca_scsi_device::autodetect_open() |
2138 | { | |
2139 | return this; | |
2140 | } | |
2127e193 | 2141 | |
ee38a438 GI |
2142 | int linux_areca_scsi_device::arcmsr_do_scsi_io(struct scsi_cmnd_io * iop) |
2143 | { | |
2144 | int ioctlreturn = 0; | |
2127e193 | 2145 | |
ee38a438 GI |
2146 | if(!is_open()) { |
2147 | if(!open()){ | |
2148 | find_areca_in_proc(); | |
2149 | } | |
2150 | } | |
2127e193 | 2151 | |
ee38a438 GI |
2152 | ioctlreturn = do_normal_scsi_cmnd_io(get_fd(), iop, scsi_debugmode); |
2153 | if ( ioctlreturn || iop->scsi_status ) | |
2154 | { | |
2155 | // errors found | |
2156 | return -1; | |
2157 | } | |
2127e193 | 2158 | |
ee38a438 GI |
2159 | return ioctlreturn; |
2160 | } | |
2127e193 | 2161 | |
ee38a438 GI |
2162 | bool linux_areca_scsi_device::arcmsr_lock() |
2163 | { | |
2164 | return true; | |
832b75ed GG |
2165 | } |
2166 | ||
ee38a438 GI |
2167 | bool linux_areca_scsi_device::arcmsr_unlock() |
2168 | { | |
2169 | return true; | |
2170 | } | |
832b75ed | 2171 | |
2127e193 GI |
2172 | ///////////////////////////////////////////////////////////////////////////// |
2173 | /// Marvell support | |
2174 | ||
2175 | class linux_marvell_device | |
2176 | : public /*implements*/ ata_device_with_command_set, | |
2177 | public /*extends*/ linux_smart_device | |
2178 | { | |
2179 | public: | |
2180 | linux_marvell_device(smart_interface * intf, const char * dev_name, const char * req_type); | |
2181 | ||
2182 | protected: | |
2183 | virtual int ata_command_interface(smart_command_set command, int select, char * data); | |
2184 | }; | |
2185 | ||
2186 | linux_marvell_device::linux_marvell_device(smart_interface * intf, | |
2187 | const char * dev_name, const char * req_type) | |
2188 | : smart_device(intf, dev_name, "marvell", req_type), | |
2189 | linux_smart_device(O_RDONLY | O_NONBLOCK) | |
2190 | { | |
2191 | } | |
832b75ed | 2192 | |
2127e193 GI |
2193 | int linux_marvell_device::ata_command_interface(smart_command_set command, int select, char * data) |
2194 | { | |
a37e7145 | 2195 | typedef struct { |
832b75ed GG |
2196 | int inlen; |
2197 | int outlen; | |
2198 | char cmd[540]; | |
2199 | } mvsata_scsi_cmd; | |
a37e7145 | 2200 | |
832b75ed GG |
2201 | int copydata = 0; |
2202 | mvsata_scsi_cmd smart_command; | |
2203 | unsigned char *buff = (unsigned char *)&smart_command.cmd[6]; | |
2204 | // See struct hd_drive_cmd_hdr in hdreg.h | |
2205 | // buff[0]: ATA COMMAND CODE REGISTER | |
2206 | // buff[1]: ATA SECTOR NUMBER REGISTER | |
2207 | // buff[2]: ATA FEATURES REGISTER | |
2208 | // buff[3]: ATA SECTOR COUNT REGISTER | |
a37e7145 | 2209 | |
832b75ed GG |
2210 | // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes) |
2211 | memset(&smart_command, 0, sizeof(smart_command)); | |
2212 | smart_command.inlen = 540; | |
2213 | smart_command.outlen = 540; | |
2214 | smart_command.cmd[0] = 0xC; //Vendor-specific code | |
2215 | smart_command.cmd[4] = 6; //command length | |
a37e7145 | 2216 | |
832b75ed GG |
2217 | buff[0] = ATA_SMART_CMD; |
2218 | switch (command){ | |
2219 | case CHECK_POWER_MODE: | |
2220 | buff[0]=ATA_CHECK_POWER_MODE; | |
2221 | break; | |
2222 | case READ_VALUES: | |
2223 | buff[2]=ATA_SMART_READ_VALUES; | |
2224 | copydata=buff[3]=1; | |
2225 | break; | |
2226 | case READ_THRESHOLDS: | |
2227 | buff[2]=ATA_SMART_READ_THRESHOLDS; | |
2228 | copydata=buff[1]=buff[3]=1; | |
2229 | break; | |
2230 | case READ_LOG: | |
2231 | buff[2]=ATA_SMART_READ_LOG_SECTOR; | |
2232 | buff[1]=select; | |
2233 | copydata=buff[3]=1; | |
2234 | break; | |
2235 | case IDENTIFY: | |
2236 | buff[0]=ATA_IDENTIFY_DEVICE; | |
2237 | copydata=buff[3]=1; | |
2238 | break; | |
2239 | case PIDENTIFY: | |
2240 | buff[0]=ATA_IDENTIFY_PACKET_DEVICE; | |
2241 | copydata=buff[3]=1; | |
2242 | break; | |
2243 | case ENABLE: | |
2244 | buff[2]=ATA_SMART_ENABLE; | |
2245 | buff[1]=1; | |
2246 | break; | |
2247 | case DISABLE: | |
2248 | buff[2]=ATA_SMART_DISABLE; | |
2249 | buff[1]=1; | |
2250 | break; | |
2251 | case STATUS: | |
2252 | case STATUS_CHECK: | |
2253 | // this command only says if SMART is working. It could be | |
2254 | // replaced with STATUS_CHECK below. | |
2255 | buff[2] = ATA_SMART_STATUS; | |
2256 | break; | |
2257 | case AUTO_OFFLINE: | |
2258 | buff[2]=ATA_SMART_AUTO_OFFLINE; | |
2259 | buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!! | |
2260 | break; | |
2261 | case AUTOSAVE: | |
2262 | buff[2]=ATA_SMART_AUTOSAVE; | |
2263 | buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!! | |
2264 | break; | |
2265 | case IMMEDIATE_OFFLINE: | |
2266 | buff[2]=ATA_SMART_IMMEDIATE_OFFLINE; | |
2267 | buff[1]=select; | |
2268 | break; | |
2269 | default: | |
2270 | pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command); | |
ff28b140 TL |
2271 | errno = EINVAL; |
2272 | return -1; | |
a37e7145 | 2273 | } |
832b75ed GG |
2274 | // There are two different types of ioctls(). The HDIO_DRIVE_TASK |
2275 | // one is this: | |
2276 | // We are now doing the HDIO_DRIVE_CMD type ioctl. | |
2127e193 | 2277 | if (ioctl(get_fd(), SCSI_IOCTL_SEND_COMMAND, (void *)&smart_command)) |
832b75ed GG |
2278 | return -1; |
2279 | ||
2280 | if (command==CHECK_POWER_MODE) { | |
2281 | // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT | |
2282 | // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce | |
2283 | data[0]=buff[3]; | |
2284 | return 0; | |
2285 | } | |
2286 | ||
a37e7145 | 2287 | // Always succeed on a SMART status, as a disk that failed returned |
832b75ed GG |
2288 | // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below). |
2289 | if (command == STATUS) | |
2290 | return 0; | |
a37e7145 | 2291 | //Data returned is starting from 0 offset |
832b75ed GG |
2292 | if (command == STATUS_CHECK) |
2293 | { | |
2294 | // Cyl low and Cyl high unchanged means "Good SMART status" | |
2295 | if (buff[4] == 0x4F && buff[5] == 0xC2) | |
a37e7145 | 2296 | return 0; |
832b75ed GG |
2297 | // These values mean "Bad SMART status" |
2298 | if (buff[4] == 0xF4 && buff[5] == 0x2C) | |
a37e7145 | 2299 | return 1; |
832b75ed GG |
2300 | // We haven't gotten output that makes sense; print out some debugging info |
2301 | syserror("Error SMART Status command failed"); | |
2302 | pout("Please get assistance from %s\n",PACKAGE_BUGREPORT); | |
2303 | pout("Register values returned from SMART Status command are:\n"); | |
2304 | pout("CMD =0x%02x\n",(int)buff[0]); | |
2305 | pout("FR =0x%02x\n",(int)buff[1]); | |
2306 | pout("NS =0x%02x\n",(int)buff[2]); | |
2307 | pout("SC =0x%02x\n",(int)buff[3]); | |
2308 | pout("CL =0x%02x\n",(int)buff[4]); | |
2309 | pout("CH =0x%02x\n",(int)buff[5]); | |
2310 | pout("SEL=0x%02x\n",(int)buff[6]); | |
a37e7145 GG |
2311 | return -1; |
2312 | } | |
832b75ed GG |
2313 | |
2314 | if (copydata) | |
2315 | memcpy(data, buff, 512); | |
a37e7145 | 2316 | return 0; |
832b75ed GG |
2317 | } |
2318 | ||
2127e193 GI |
2319 | ///////////////////////////////////////////////////////////////////////////// |
2320 | /// Highpoint RAID support | |
2321 | ||
2322 | class linux_highpoint_device | |
2323 | : public /*implements*/ ata_device_with_command_set, | |
2324 | public /*extends*/ linux_smart_device | |
2325 | { | |
2326 | public: | |
2327 | linux_highpoint_device(smart_interface * intf, const char * dev_name, | |
2328 | unsigned char controller, unsigned char channel, unsigned char port); | |
2329 | ||
2330 | protected: | |
2331 | virtual int ata_command_interface(smart_command_set command, int select, char * data); | |
2332 | ||
2333 | private: | |
2334 | unsigned char m_hpt_data[3]; ///< controller/channel/port | |
2335 | }; | |
2336 | ||
2337 | linux_highpoint_device::linux_highpoint_device(smart_interface * intf, const char * dev_name, | |
2338 | unsigned char controller, unsigned char channel, unsigned char port) | |
2339 | : smart_device(intf, dev_name, "hpt", "hpt"), | |
2340 | linux_smart_device(O_RDONLY | O_NONBLOCK) | |
2341 | { | |
2342 | m_hpt_data[0] = controller; m_hpt_data[1] = channel; m_hpt_data[2] = port; | |
2343 | set_info().info_name = strprintf("%s [hpt_disk_%u/%u/%u]", dev_name, m_hpt_data[0], m_hpt_data[1], m_hpt_data[2]); | |
2344 | } | |
2345 | ||
4d59bff9 GG |
2346 | // this implementation is derived from ata_command_interface with a header |
2347 | // packing for highpoint linux driver ioctl interface | |
a37e7145 | 2348 | // |
4d59bff9 GG |
2349 | // ioctl(fd,HPTIO_CTL,buff) |
2350 | // ^^^^^^^^^ | |
2351 | // | |
2352 | // structure of hpt_buff | |
2353 | // +----+----+----+----+--------------------.....---------------------+ | |
2354 | // | 1 | 2 | 3 | 4 | 5 | | |
2355 | // +----+----+----+----+--------------------.....---------------------+ | |
a37e7145 | 2356 | // |
4d59bff9 GG |
2357 | // 1: The target controller [ int ( 4 Bytes ) ] |
2358 | // 2: The channel of the target controllee [ int ( 4 Bytes ) ] | |
2359 | // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ] | |
2360 | // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio | |
2361 | // 4: the pmport that disk attached, [ int ( 4 Bytes ) ] | |
2362 | // if no pmport device, set to 1 or leave blank | |
2363 | // 5: data [ void * ( var leangth ) ] | |
a37e7145 GG |
2364 | // |
2365 | #define STRANGE_BUFFER_LENGTH (4+512*0xf8) | |
2366 | ||
2127e193 | 2367 | int linux_highpoint_device::ata_command_interface(smart_command_set command, int select, char * data) |
4d59bff9 GG |
2368 | { |
2369 | unsigned char hpt_buff[4*sizeof(int) + STRANGE_BUFFER_LENGTH]; | |
2370 | unsigned int *hpt = (unsigned int *)hpt_buff; | |
2371 | unsigned char *buff = &hpt_buff[4*sizeof(int)]; | |
2372 | int copydata = 0; | |
2373 | const int HDIO_DRIVE_CMD_OFFSET = 4; | |
2374 | ||
2375 | memset(hpt_buff, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH); | |
2127e193 GI |
2376 | hpt[0] = m_hpt_data[0]; // controller id |
2377 | hpt[1] = m_hpt_data[1]; // channel number | |
2378 | hpt[3] = m_hpt_data[2]; // pmport number | |
4d59bff9 GG |
2379 | |
2380 | buff[0]=ATA_SMART_CMD; | |
2381 | switch (command){ | |
2382 | case CHECK_POWER_MODE: | |
2383 | buff[0]=ATA_CHECK_POWER_MODE; | |
2384 | copydata=1; | |
2385 | break; | |
2386 | case READ_VALUES: | |
2387 | buff[2]=ATA_SMART_READ_VALUES; | |
2388 | buff[3]=1; | |
2389 | copydata=512; | |
2390 | break; | |
2391 | case READ_THRESHOLDS: | |
2392 | buff[2]=ATA_SMART_READ_THRESHOLDS; | |
2393 | buff[1]=buff[3]=1; | |
2394 | copydata=512; | |
2395 | break; | |
2396 | case READ_LOG: | |
2397 | buff[2]=ATA_SMART_READ_LOG_SECTOR; | |
2398 | buff[1]=select; | |
2399 | buff[3]=1; | |
2400 | copydata=512; | |
2401 | break; | |
2402 | case WRITE_LOG: | |
2403 | break; | |
2404 | case IDENTIFY: | |
2405 | buff[0]=ATA_IDENTIFY_DEVICE; | |
2406 | buff[3]=1; | |
2407 | copydata=512; | |
2408 | break; | |
2409 | case PIDENTIFY: | |
2410 | buff[0]=ATA_IDENTIFY_PACKET_DEVICE; | |
2411 | buff[3]=1; | |
2412 | copydata=512; | |
2413 | break; | |
2414 | case ENABLE: | |
2415 | buff[2]=ATA_SMART_ENABLE; | |
2416 | buff[1]=1; | |
2417 | break; | |
2418 | case DISABLE: | |
2419 | buff[2]=ATA_SMART_DISABLE; | |
2420 | buff[1]=1; | |
2421 | break; | |
2422 | case STATUS: | |
2423 | buff[2]=ATA_SMART_STATUS; | |
2424 | break; | |
2425 | case AUTO_OFFLINE: | |
2426 | buff[2]=ATA_SMART_AUTO_OFFLINE; | |
2427 | buff[3]=select; | |
2428 | break; | |
2429 | case AUTOSAVE: | |
2430 | buff[2]=ATA_SMART_AUTOSAVE; | |
2431 | buff[3]=select; | |
2432 | break; | |
2433 | case IMMEDIATE_OFFLINE: | |
2434 | buff[2]=ATA_SMART_IMMEDIATE_OFFLINE; | |
2435 | buff[1]=select; | |
2436 | break; | |
2437 | case STATUS_CHECK: | |
2438 | buff[1]=ATA_SMART_STATUS; | |
2439 | break; | |
2440 | default: | |
2441 | pout("Unrecognized command %d in linux_highpoint_command_interface()\n" | |
2442 | "Please contact " PACKAGE_BUGREPORT "\n", command); | |
2443 | errno=ENOSYS; | |
2444 | return -1; | |
2445 | } | |
2446 | ||
2447 | if (command==WRITE_LOG) { | |
2448 | unsigned char task[4*sizeof(int)+sizeof(ide_task_request_t)+512]; | |
cfbba5b9 | 2449 | unsigned int *hpt_tf = (unsigned int *)task; |
4d59bff9 GG |
2450 | ide_task_request_t *reqtask = (ide_task_request_t *)(&task[4*sizeof(int)]); |
2451 | task_struct_t *taskfile = (task_struct_t *)reqtask->io_ports; | |
4d59bff9 GG |
2452 | |
2453 | memset(task, 0, sizeof(task)); | |
2454 | ||
cfbba5b9 GI |
2455 | hpt_tf[0] = m_hpt_data[0]; // controller id |
2456 | hpt_tf[1] = m_hpt_data[1]; // channel number | |
2457 | hpt_tf[3] = m_hpt_data[2]; // pmport number | |
2458 | hpt_tf[2] = HDIO_DRIVE_TASKFILE; // real hd ioctl | |
4d59bff9 GG |
2459 | |
2460 | taskfile->data = 0; | |
2461 | taskfile->feature = ATA_SMART_WRITE_LOG_SECTOR; | |
2462 | taskfile->sector_count = 1; | |
2463 | taskfile->sector_number = select; | |
2464 | taskfile->low_cylinder = 0x4f; | |
2465 | taskfile->high_cylinder = 0xc2; | |
2466 | taskfile->device_head = 0; | |
2467 | taskfile->command = ATA_SMART_CMD; | |
a37e7145 | 2468 | |
4d59bff9 GG |
2469 | reqtask->data_phase = TASKFILE_OUT; |
2470 | reqtask->req_cmd = IDE_DRIVE_TASK_OUT; | |
2471 | reqtask->out_size = 512; | |
2472 | reqtask->in_size = 0; | |
a37e7145 | 2473 | |
4d59bff9 GG |
2474 | memcpy(task+sizeof(ide_task_request_t)+4*sizeof(int), data, 512); |
2475 | ||
a86ec89e | 2476 | if (ioctl(get_fd(), HPTIO_CTL, task)) |
4d59bff9 | 2477 | return -1; |
a86ec89e | 2478 | |
4d59bff9 GG |
2479 | return 0; |
2480 | } | |
a37e7145 | 2481 | |
4d59bff9 | 2482 | if (command==STATUS_CHECK){ |
4d59bff9 GG |
2483 | unsigned const char normal_lo=0x4f, normal_hi=0xc2; |
2484 | unsigned const char failed_lo=0xf4, failed_hi=0x2c; | |
2485 | buff[4]=normal_lo; | |
2486 | buff[5]=normal_hi; | |
2487 | ||
2488 | hpt[2] = HDIO_DRIVE_TASK; | |
2489 | ||
a86ec89e | 2490 | if (ioctl(get_fd(), HPTIO_CTL, hpt_buff)) |
4d59bff9 | 2491 | return -1; |
a37e7145 | 2492 | |
4d59bff9 GG |
2493 | if (buff[4]==normal_lo && buff[5]==normal_hi) |
2494 | return 0; | |
a37e7145 | 2495 | |
4d59bff9 GG |
2496 | if (buff[4]==failed_lo && buff[5]==failed_hi) |
2497 | return 1; | |
a37e7145 | 2498 | |
4d59bff9 GG |
2499 | syserror("Error SMART Status command failed"); |
2500 | pout("Please get assistance from " PACKAGE_HOMEPAGE "\n"); | |
2501 | pout("Register values returned from SMART Status command are:\n"); | |
2502 | pout("CMD=0x%02x\n",(int)buff[0]); | |
2503 | pout("FR =0x%02x\n",(int)buff[1]); | |
2504 | pout("NS =0x%02x\n",(int)buff[2]); | |
2505 | pout("SC =0x%02x\n",(int)buff[3]); | |
2506 | pout("CL =0x%02x\n",(int)buff[4]); | |
2507 | pout("CH =0x%02x\n",(int)buff[5]); | |
2508 | pout("SEL=0x%02x\n",(int)buff[6]); | |
a37e7145 | 2509 | return -1; |
4d59bff9 | 2510 | } |
a37e7145 | 2511 | |
4d59bff9 GG |
2512 | #if 1 |
2513 | if (command==IDENTIFY || command==PIDENTIFY) { | |
2514 | unsigned char deviceid[4*sizeof(int)+512*sizeof(char)]; | |
cfbba5b9 | 2515 | unsigned int *hpt_id = (unsigned int *)deviceid; |
4d59bff9 | 2516 | |
cfbba5b9 GI |
2517 | hpt_id[0] = m_hpt_data[0]; // controller id |
2518 | hpt_id[1] = m_hpt_data[1]; // channel number | |
2519 | hpt_id[3] = m_hpt_data[2]; // pmport number | |
4d59bff9 | 2520 | |
cfbba5b9 | 2521 | hpt_id[2] = HDIO_GET_IDENTITY; |
2127e193 | 2522 | if (!ioctl(get_fd(), HPTIO_CTL, deviceid) && (deviceid[4*sizeof(int)] & 0x8000)) |
4d59bff9 GG |
2523 | buff[0]=(command==IDENTIFY)?ATA_IDENTIFY_PACKET_DEVICE:ATA_IDENTIFY_DEVICE; |
2524 | } | |
2525 | #endif | |
a37e7145 | 2526 | |
4d59bff9 | 2527 | hpt[2] = HDIO_DRIVE_CMD; |
2127e193 | 2528 | if ((ioctl(get_fd(), HPTIO_CTL, hpt_buff))) |
4d59bff9 GG |
2529 | return -1; |
2530 | ||
2531 | if (command==CHECK_POWER_MODE) | |
2532 | buff[HDIO_DRIVE_CMD_OFFSET]=buff[2]; | |
2533 | ||
2534 | if (copydata) | |
2535 | memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata); | |
a37e7145 GG |
2536 | |
2537 | return 0; | |
4d59bff9 GG |
2538 | } |
2539 | ||
2127e193 | 2540 | #if 0 // TODO: Migrate from 'smart_command_set' to 'ata_in_regs' OR remove the function |
832b75ed GG |
2541 | // Utility function for printing warnings |
2542 | void printwarning(smart_command_set command){ | |
2543 | static int printed[4]={0,0,0,0}; | |
2544 | const char* message= | |
2545 | "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n" | |
2546 | "applying a simple 3w-xxxx driver patch that can be found here:\n" | |
2547 | PACKAGE_HOMEPAGE "\n" | |
2548 | "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n"; | |
2549 | ||
2550 | if (command==AUTO_OFFLINE && !printed[0]) { | |
2551 | printed[0]=1; | |
2552 | pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message); | |
a37e7145 | 2553 | } |
832b75ed GG |
2554 | else if (command==AUTOSAVE && !printed[1]) { |
2555 | printed[1]=1; | |
2556 | pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message); | |
2557 | } | |
2558 | else if (command==STATUS_CHECK && !printed[2]) { | |
2559 | printed[2]=1; | |
2560 | pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message); | |
2561 | } | |
2562 | else if (command==WRITE_LOG && !printed[3]) { | |
2563 | printed[3]=1; | |
2564 | pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n"); | |
2565 | } | |
a37e7145 | 2566 | |
832b75ed GG |
2567 | return; |
2568 | } | |
2127e193 GI |
2569 | #endif |
2570 | ||
2127e193 GI |
2571 | ///////////////////////////////////////////////////////////////////////////// |
2572 | /// SCSI open with autodetection support | |
2573 | ||
2574 | smart_device * linux_scsi_device::autodetect_open() | |
2575 | { | |
2576 | // Open device | |
2577 | if (!open()) | |
2578 | return this; | |
2579 | ||
2580 | // No Autodetection if device type was specified by user | |
f4ebf3d1 GI |
2581 | bool sat_only = false; |
2582 | if (*get_req_type()) { | |
2583 | // Detect SAT if device object was created by scan_smart_devices(). | |
2584 | if (!(m_scanning && !strcmp(get_req_type(), "sat"))) | |
2585 | return this; | |
2586 | sat_only = true; | |
2587 | } | |
2127e193 GI |
2588 | |
2589 | // The code below is based on smartd.cpp:SCSIFilterKnown() | |
2590 | ||
2591 | // Get INQUIRY | |
2592 | unsigned char req_buff[64] = {0, }; | |
2593 | int req_len = 36; | |
2594 | if (scsiStdInquiry(this, req_buff, req_len)) { | |
2595 | // Marvell controllers fail on a 36 bytes StdInquiry, but 64 suffices | |
2596 | // watch this spot ... other devices could lock up here | |
2597 | req_len = 64; | |
2598 | if (scsiStdInquiry(this, req_buff, req_len)) { | |
2599 | // device doesn't like INQUIRY commands | |
2600 | close(); | |
2601 | set_err(EIO, "INQUIRY failed"); | |
2602 | return this; | |
2603 | } | |
2604 | } | |
2605 | ||
2606 | int avail_len = req_buff[4] + 5; | |
2607 | int len = (avail_len < req_len ? avail_len : req_len); | |
f4ebf3d1 GI |
2608 | if (len < 36) { |
2609 | if (sat_only) { | |
2610 | close(); | |
2611 | set_err(EIO, "INQUIRY too short for SAT"); | |
2612 | } | |
bed94269 | 2613 | return this; |
f4ebf3d1 | 2614 | } |
2127e193 GI |
2615 | |
2616 | // Use INQUIRY to detect type | |
f4ebf3d1 | 2617 | if (!sat_only) { |
2127e193 | 2618 | |
f4ebf3d1 GI |
2619 | // 3ware ? |
2620 | if (!memcmp(req_buff + 8, "3ware", 5) || !memcmp(req_buff + 8, "AMCC", 4)) { | |
2621 | close(); | |
2622 | set_err(EINVAL, "AMCC/3ware controller, please try adding '-d 3ware,N',\n" | |
cfbba5b9 | 2623 | "you may need to replace %s with /dev/twlN, /dev/twaN or /dev/tweN", get_dev_name()); |
f4ebf3d1 GI |
2624 | return this; |
2625 | } | |
bed94269 | 2626 | |
f4ebf3d1 | 2627 | // DELL? |
ee38a438 GI |
2628 | if (!memcmp(req_buff + 8, "DELL PERC", 12) || !memcmp(req_buff + 8, "MegaRAID", 8) |
2629 | || !memcmp(req_buff + 16, "PERC H700", 9) || !memcmp(req_buff + 8, "LSI\0",4) | |
2630 | ) { | |
f4ebf3d1 GI |
2631 | close(); |
2632 | set_err(EINVAL, "DELL or MegaRaid controller, please try adding '-d megaraid,N'"); | |
2633 | return this; | |
2634 | } | |
bed94269 | 2635 | |
f4ebf3d1 GI |
2636 | // Marvell ? |
2637 | if (len >= 42 && !memcmp(req_buff + 36, "MVSATA", 6)) { | |
2638 | //pout("Device %s: using '-d marvell' for ATA disk with Marvell driver\n", get_dev_name()); | |
2639 | close(); | |
2640 | smart_device_auto_ptr newdev( | |
2641 | new linux_marvell_device(smi(), get_dev_name(), get_req_type()) | |
2642 | ); | |
2643 | newdev->open(); // TODO: Can possibly pass open fd | |
2644 | delete this; | |
2645 | return newdev.release(); | |
2646 | } | |
bed94269 GI |
2647 | } |
2648 | ||
2649 | // SAT or USB ? | |
2650 | { | |
2651 | smart_device * newdev = smi()->autodetect_sat_device(this, req_buff, len); | |
2127e193 GI |
2652 | if (newdev) |
2653 | // NOTE: 'this' is now owned by '*newdev' | |
2654 | return newdev; | |
2655 | } | |
2127e193 GI |
2656 | |
2657 | // Nothing special found | |
f4ebf3d1 GI |
2658 | |
2659 | if (sat_only) { | |
2660 | close(); | |
2661 | set_err(EIO, "Not a SAT device"); | |
2662 | } | |
2127e193 GI |
2663 | return this; |
2664 | } | |
2665 | ||
a86ec89e GI |
2666 | ///////////////////////////////////////////////////////////////////////////// |
2667 | /// NVMe support | |
2668 | ||
2669 | class linux_nvme_device | |
2670 | : public /*implements*/ nvme_device, | |
2671 | public /*extends*/ linux_smart_device | |
2672 | { | |
2673 | public: | |
2674 | linux_nvme_device(smart_interface * intf, const char * dev_name, | |
2675 | const char * req_type, unsigned nsid); | |
2676 | ||
2677 | virtual bool open(); | |
2678 | ||
2679 | virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out); | |
2680 | }; | |
2681 | ||
2682 | linux_nvme_device::linux_nvme_device(smart_interface * intf, const char * dev_name, | |
2683 | const char * req_type, unsigned nsid) | |
2684 | : smart_device(intf, dev_name, "nvme", req_type), | |
2685 | nvme_device(nsid), | |
2686 | linux_smart_device(O_RDONLY | O_NONBLOCK) | |
2687 | { | |
2688 | } | |
2689 | ||
2690 | bool linux_nvme_device::open() | |
2691 | { | |
2692 | if (!linux_smart_device::open()) | |
2693 | return false; | |
2694 | ||
2695 | if (!get_nsid()) { | |
2696 | // Use actual NSID (/dev/nvmeXnN) if available, | |
2697 | // else use broadcast namespace (/dev/nvmeX) | |
2698 | int nsid = ioctl(get_fd(), NVME_IOCTL_ID, (void*)0); | |
2699 | set_nsid(nsid); | |
2700 | } | |
2701 | ||
2702 | return true; | |
2703 | } | |
2704 | ||
2705 | bool linux_nvme_device::nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out) | |
2706 | { | |
2707 | nvme_passthru_cmd pt; | |
2708 | memset(&pt, 0, sizeof(pt)); | |
2709 | ||
2710 | pt.opcode = in.opcode; | |
2711 | pt.nsid = in.nsid; | |
2712 | pt.addr = (uint64_t)in.buffer; | |
2713 | pt.data_len = in.size; | |
2714 | pt.cdw10 = in.cdw10; | |
2715 | pt.cdw11 = in.cdw11; | |
2716 | pt.cdw12 = in.cdw12; | |
2717 | pt.cdw13 = in.cdw13; | |
2718 | pt.cdw14 = in.cdw14; | |
2719 | pt.cdw15 = in.cdw15; | |
2720 | // Kernel default for NVMe admin commands is 60 seconds | |
2721 | // pt.timeout_ms = 60 * 1000; | |
2722 | ||
2723 | int status = ioctl(get_fd(), NVME_IOCTL_ADMIN_CMD, &pt); | |
2724 | ||
2725 | if (status < 0) | |
2726 | return set_err(errno, "NVME_IOCTL_ADMIN_CMD: %s", strerror(errno)); | |
2727 | ||
2728 | if (status > 0) | |
2729 | return set_nvme_err(out, status); | |
2730 | ||
2731 | out.result = pt.result; | |
2732 | return true; | |
2733 | } | |
2734 | ||
2735 | ||
2127e193 GI |
2736 | ////////////////////////////////////////////////////////////////////// |
2737 | // USB bridge ID detection | |
2738 | ||
2739 | // Read USB ID from /sys file | |
2740 | static bool read_id(const std::string & path, unsigned short & id) | |
2741 | { | |
2742 | FILE * f = fopen(path.c_str(), "r"); | |
2743 | if (!f) | |
2744 | return false; | |
2745 | int n = -1; | |
2746 | bool ok = (fscanf(f, "%hx%n", &id, &n) == 1 && n == 4); | |
2747 | fclose(f); | |
2748 | return ok; | |
2749 | } | |
2750 | ||
ff28b140 | 2751 | // Get USB bridge ID for "sdX" or "sgN" |
54965743 | 2752 | static bool get_usb_id(const char * name, unsigned short & vendor_id, |
2127e193 GI |
2753 | unsigned short & product_id, unsigned short & version) |
2754 | { | |
ff28b140 TL |
2755 | // Only "sdX" or "sgN" supported |
2756 | if (!(name[0] == 's' && (name[1] == 'd' || name[1] == 'g') && !strchr(name, '/'))) | |
2127e193 GI |
2757 | return false; |
2758 | ||
ff28b140 TL |
2759 | // Start search at dir referenced by symlink |
2760 | // "/sys/block/sdX/device" or | |
2761 | // "/sys/class/scsi_generic/sgN" | |
2127e193 | 2762 | // -> "/sys/devices/.../usb*/.../host*/target*/..." |
ff28b140 TL |
2763 | std::string dir = strprintf("/sys/%s/%s%s", |
2764 | (name[1] == 'd' ? "block" : "class/scsi_generic"), name, | |
2765 | (name[1] == 'd' ? "/device" : "")); | |
2127e193 GI |
2766 | |
2767 | // Stop search at "/sys/devices" | |
2768 | struct stat st; | |
2769 | if (stat("/sys/devices", &st)) | |
2770 | return false; | |
2771 | ino_t stop_ino = st.st_ino; | |
2772 | ||
2773 | // Search in parent directories until "idVendor" is found, | |
2774 | // fail if "/sys/devices" reached or too many iterations | |
2775 | int cnt = 0; | |
2776 | do { | |
2777 | dir += "/.."; | |
2778 | if (!(++cnt < 10 && !stat(dir.c_str(), &st) && st.st_ino != stop_ino)) | |
2779 | return false; | |
2780 | } while (access((dir + "/idVendor").c_str(), 0)); | |
2781 | ||
2782 | // Read IDs | |
2783 | if (!( read_id(dir + "/idVendor", vendor_id) | |
2784 | && read_id(dir + "/idProduct", product_id) | |
2785 | && read_id(dir + "/bcdDevice", version) )) | |
2786 | return false; | |
2787 | ||
cfbba5b9 | 2788 | if (scsi_debugmode > 1) |
2127e193 GI |
2789 | pout("USB ID = 0x%04x:0x%04x (0x%03x)\n", vendor_id, product_id, version); |
2790 | return true; | |
2791 | } | |
2792 | ||
2127e193 GI |
2793 | ////////////////////////////////////////////////////////////////////// |
2794 | /// Linux interface | |
2795 | ||
2796 | class linux_smart_interface | |
2797 | : public /*implements*/ smart_interface | |
2798 | { | |
2799 | public: | |
cfbba5b9 GI |
2800 | virtual std::string get_os_version_str(); |
2801 | ||
54965743 | 2802 | virtual std::string get_app_examples(const char * appname); |
2127e193 | 2803 | |
ff28b140 TL |
2804 | virtual bool scan_smart_devices(smart_device_list & devlist, |
2805 | const smart_devtype_list & types, const char * pattern = 0); | |
2127e193 GI |
2806 | |
2807 | protected: | |
2808 | virtual ata_device * get_ata_device(const char * name, const char * type); | |
2809 | ||
2810 | virtual scsi_device * get_scsi_device(const char * name, const char * type); | |
2811 | ||
a86ec89e GI |
2812 | virtual nvme_device * get_nvme_device(const char * name, const char * type, |
2813 | unsigned nsid); | |
2814 | ||
2127e193 GI |
2815 | virtual smart_device * autodetect_smart_device(const char * name); |
2816 | ||
2817 | virtual smart_device * get_custom_smart_device(const char * name, const char * type); | |
2818 | ||
54965743 | 2819 | virtual std::string get_valid_custom_dev_types_str(); |
2127e193 GI |
2820 | |
2821 | private: | |
ff28b140 TL |
2822 | static const int devxy_to_n_max = 103; // Max value of devxy_to_n() below |
2823 | ||
2824 | void get_dev_list(smart_device_list & devlist, const char * pattern, | |
2825 | bool scan_scsi, bool (* p_dev_sdxy_seen)[devxy_to_n_max+1], | |
2826 | bool scan_nvme, const char * req_type, bool autodetect); | |
a86ec89e | 2827 | |
ee38a438 | 2828 | bool get_dev_megasas(smart_device_list & devlist); |
2127e193 | 2829 | smart_device * missing_option(const char * opt); |
ee38a438 GI |
2830 | int megasas_dcmd_cmd(int bus_no, uint32_t opcode, void *buf, |
2831 | size_t bufsize, uint8_t *mbox, size_t mboxlen, uint8_t *statusp); | |
2832 | int megasas_pd_add_list(int bus_no, smart_device_list & devlist); | |
2127e193 GI |
2833 | }; |
2834 | ||
cfbba5b9 GI |
2835 | std::string linux_smart_interface::get_os_version_str() |
2836 | { | |
2837 | struct utsname u; | |
a7e8ffec GI |
2838 | if (!uname(&u)) |
2839 | return strprintf("%s-linux-%s", u.machine, u.release); | |
2840 | else | |
2841 | return SMARTMONTOOLS_BUILD_HOST; | |
cfbba5b9 GI |
2842 | } |
2843 | ||
54965743 | 2844 | std::string linux_smart_interface::get_app_examples(const char * appname) |
2127e193 GI |
2845 | { |
2846 | if (!strcmp(appname, "smartctl")) | |
2847 | return smartctl_examples; | |
54965743 | 2848 | return ""; |
2127e193 GI |
2849 | } |
2850 | ||
ff28b140 TL |
2851 | // "/dev/sdXY" -> 0-103 |
2852 | // "/dev/disk/by-id/NAME" -> "../../sdXY" -> 0-103 | |
2853 | // Other -> -1 | |
2854 | static int devxy_to_n(const char * name, bool debug) | |
2855 | { | |
2856 | const char * xy; | |
2857 | char dest[256]; | |
2858 | if (str_starts_with(name, "/dev/sd")) { | |
2859 | // Assume "/dev/sdXY" | |
2860 | xy = name + sizeof("/dev/sd") - 1; | |
2861 | } | |
2862 | else { | |
2863 | // Assume "/dev/disk/by-id/NAME", check link target | |
2864 | int sz = readlink(name, dest, sizeof(dest)-1); | |
2865 | if (!(0 < sz && sz < (int)sizeof(dest))) | |
2866 | return -1; | |
2867 | dest[sz] = 0; | |
2868 | if (!str_starts_with(dest, "../../sd")) | |
2869 | return -1; | |
2870 | if (debug) | |
2871 | pout("%s -> %s\n", name, dest); | |
2872 | xy = dest + sizeof("../../sd") - 1; | |
2873 | } | |
2874 | ||
2875 | char x = xy[0]; | |
2876 | if (!('a' <= x && x <= 'z')) | |
2877 | return -1; | |
2878 | char y = xy[1]; | |
2879 | if (!y) | |
2880 | // "[a-z]" -> 0-25 | |
2881 | return x - 'a'; | |
2882 | ||
2883 | if (!(x <= 'c' && 'a' <= y && y <= 'z' && !xy[2])) | |
2884 | return -1; | |
2885 | // "[a-c][a-z]" -> 26-103 | |
2886 | return (x - 'a' + 1) * ('z' - 'a' + 1) + (y - 'a'); | |
2887 | } | |
2888 | ||
2889 | void linux_smart_interface::get_dev_list(smart_device_list & devlist, | |
2890 | const char * pattern, bool scan_scsi, bool (* p_dev_sdxy_seen)[devxy_to_n_max+1], | |
2891 | bool scan_nvme, const char * req_type, bool autodetect) | |
2127e193 | 2892 | { |
ff28b140 TL |
2893 | bool debug = (ata_debugmode || scsi_debugmode || nvme_debugmode); |
2894 | ||
2127e193 GI |
2895 | // Use glob to look for any directory entries matching the pattern |
2896 | glob_t globbuf; | |
2897 | memset(&globbuf, 0, sizeof(globbuf)); | |
2898 | int retglob = glob(pattern, GLOB_ERR, NULL, &globbuf); | |
2899 | if (retglob) { | |
ff28b140 | 2900 | // glob failed: free memory and return |
2127e193 GI |
2901 | globfree(&globbuf); |
2902 | ||
ff28b140 TL |
2903 | if (debug) |
2904 | pout("glob(3) error %d for pattern %s\n", retglob, pattern); | |
2127e193 | 2905 | |
ff28b140 TL |
2906 | if (retglob == GLOB_NOSPACE) |
2907 | throw std::bad_alloc(); | |
2908 | return; | |
2127e193 GI |
2909 | } |
2910 | ||
2911 | // did we find too many paths? | |
d2e702cf | 2912 | const int max_pathc = 1024; |
2127e193 GI |
2913 | int n = (int)globbuf.gl_pathc; |
2914 | if (n > max_pathc) { | |
2915 | pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n", | |
2916 | n, max_pathc, pattern, n - max_pathc); | |
2917 | n = max_pathc; | |
2918 | } | |
2919 | ||
ff28b140 TL |
2920 | // now step through the list returned by glob. |
2921 | for (int i = 0; i < n; i++) { | |
2922 | const char * name = globbuf.gl_pathv[i]; | |
2923 | ||
2924 | if (p_dev_sdxy_seen) { | |
2925 | // Follow "/dev/disk/by-id/*" symlink and check for duplicate "/dev/sdXY" | |
2926 | int dev_n = devxy_to_n(name, debug); | |
2927 | if (!(0 <= dev_n && dev_n <= devxy_to_n_max)) | |
2928 | continue; | |
2929 | if ((*p_dev_sdxy_seen)[dev_n]) { | |
2930 | if (debug) | |
2931 | pout("%s: duplicate, ignored\n", name); | |
2932 | continue; | |
2127e193 | 2933 | } |
ff28b140 | 2934 | (*p_dev_sdxy_seen)[dev_n] = true; |
2127e193 GI |
2935 | } |
2936 | ||
ff28b140 TL |
2937 | smart_device * dev; |
2938 | if (autodetect) { | |
2939 | dev = autodetect_smart_device(name); | |
2940 | if (!dev) | |
2941 | continue; | |
2127e193 | 2942 | } |
ff28b140 TL |
2943 | else if (scan_scsi) |
2944 | dev = new linux_scsi_device(this, name, req_type, true /*scanning*/); | |
2945 | else if (scan_nvme) | |
2946 | dev = new linux_nvme_device(this, name, req_type, 0 /* use default nsid */); | |
2947 | else | |
2948 | dev = new linux_ata_device(this, name, req_type); | |
2949 | devlist.push_back(dev); | |
2127e193 GI |
2950 | } |
2951 | ||
2952 | // free memory | |
2953 | globfree(&globbuf); | |
ee38a438 | 2954 | } |
2127e193 | 2955 | |
ee38a438 GI |
2956 | // getting devices from LSI SAS MegaRaid, if available |
2957 | bool linux_smart_interface::get_dev_megasas(smart_device_list & devlist) | |
2958 | { | |
2959 | /* Scanning of disks on MegaRaid device */ | |
2960 | /* Perform mknod of device ioctl node */ | |
2961 | int mjr, n1; | |
2962 | char line[128]; | |
2963 | bool scan_megasas = false; | |
2964 | FILE * fp = fopen("/proc/devices", "r"); | |
ff28b140 TL |
2965 | if (!fp) |
2966 | return false; | |
ee38a438 GI |
2967 | while (fgets(line, sizeof(line), fp) != NULL) { |
2968 | n1=0; | |
2969 | if (sscanf(line, "%d megaraid_sas_ioctl%n", &mjr, &n1) == 1 && n1 == 22) { | |
2970 | scan_megasas = true; | |
2971 | n1=mknod("/dev/megaraid_sas_ioctl_node", S_IFCHR, makedev(mjr, 0)); | |
2972 | if(scsi_debugmode > 0) | |
2973 | pout("Creating /dev/megaraid_sas_ioctl_node = %d\n", n1 >= 0 ? 0 : errno); | |
2974 | if (n1 >= 0 || errno == EEXIST) | |
2975 | break; | |
2976 | } | |
2977 | } | |
2978 | fclose(fp); | |
2979 | ||
2980 | if(!scan_megasas) | |
2981 | return false; | |
2982 | ||
2983 | // getting bus numbers with megasas devices | |
a86ec89e | 2984 | // we are using sysfs to get list of all scsi hosts |
ee38a438 GI |
2985 | DIR * dp = opendir ("/sys/class/scsi_host/"); |
2986 | if (dp != NULL) | |
2987 | { | |
a86ec89e | 2988 | struct dirent *ep; |
ee38a438 | 2989 | while ((ep = readdir (dp)) != NULL) { |
a86ec89e GI |
2990 | unsigned int host_no = 0; |
2991 | if (!sscanf(ep->d_name, "host%u", &host_no)) | |
ee38a438 GI |
2992 | continue; |
2993 | /* proc_name should be megaraid_sas */ | |
a86ec89e | 2994 | char sysfsdir[256]; |
ee38a438 | 2995 | snprintf(sysfsdir, sizeof(sysfsdir) - 1, |
a86ec89e | 2996 | "/sys/class/scsi_host/host%u/proc_name", host_no); |
ee38a438 GI |
2997 | if((fp = fopen(sysfsdir, "r")) == NULL) |
2998 | continue; | |
2999 | if(fgets(line, sizeof(line), fp) != NULL && !strncmp(line,"megaraid_sas",12)) { | |
3000 | megasas_pd_add_list(host_no, devlist); | |
3001 | } | |
3002 | fclose(fp); | |
3003 | } | |
3004 | (void) closedir (dp); | |
3005 | } else { /* sysfs not mounted ? */ | |
3006 | for(unsigned i = 0; i <=16; i++) // trying to add devices on first 16 buses | |
3007 | megasas_pd_add_list(i, devlist); | |
3008 | } | |
2127e193 GI |
3009 | return true; |
3010 | } | |
3011 | ||
3012 | bool linux_smart_interface::scan_smart_devices(smart_device_list & devlist, | |
ff28b140 TL |
3013 | const smart_devtype_list & types, const char * pattern /*= 0*/) |
3014 | { | |
3015 | if (pattern) | |
3016 | return set_err(EINVAL, "DEVICESCAN with pattern not implemented yet"); | |
3017 | ||
3018 | // Scan type list | |
3019 | bool by_id = false; | |
3020 | const char * type_ata = 0, * type_scsi = 0, * type_sat = 0, * type_nvme = 0; | |
3021 | for (unsigned i = 0; i < types.size(); i++) { | |
3022 | const char * type = types[i].c_str(); | |
3023 | if (!strcmp(type, "by-id")) | |
3024 | by_id = true; | |
3025 | else if (!strcmp(type, "ata")) | |
3026 | type_ata = "ata"; | |
3027 | else if (!strcmp(type, "scsi")) | |
3028 | type_scsi = "scsi"; | |
3029 | else if (!strcmp(type, "sat")) | |
3030 | type_sat = "sat"; | |
3031 | else if (!strcmp(type, "nvme")) | |
3032 | type_nvme = "nvme"; | |
3033 | else | |
3034 | return set_err(EINVAL, "Invalid type '%s', valid arguments are: by-id, ata, scsi, sat, nvme", | |
3035 | type); | |
3036 | } | |
3037 | // Use default if no type specified | |
3038 | if (!(type_ata || type_scsi || type_sat || type_nvme)) { | |
3039 | type_ata = type_scsi = type_sat = ""; | |
3040 | #ifdef WITH_NVME_DEVICESCAN // TODO: Remove when NVMe support is no longer EXPERIMENTAL | |
3041 | type_nvme = ""; | |
3042 | #endif | |
2127e193 GI |
3043 | } |
3044 | ||
ff28b140 TL |
3045 | if (type_ata) |
3046 | get_dev_list(devlist, "/dev/hd[a-t]", false, 0, false, type_ata, false); | |
2127e193 | 3047 | |
ff28b140 TL |
3048 | if (type_scsi || type_sat) { |
3049 | // "sat" detection will be later handled in linux_scsi_device::autodetect_open() | |
3050 | const char * type_scsi_sat = ((type_scsi && type_sat) ? "" // detect both | |
3051 | : (type_scsi ? type_scsi : type_sat)); | |
3052 | bool autodetect = !*type_scsi_sat; // If no type specified, detect USB also | |
a86ec89e | 3053 | |
ff28b140 TL |
3054 | bool dev_sdxy_seen[devxy_to_n_max+1] = {false, }; |
3055 | bool (*p_dev_sdxy_seen)[devxy_to_n_max+1] = 0; | |
3056 | if (by_id) { | |
3057 | // Scan unique symlinks first | |
3058 | get_dev_list(devlist, "/dev/disk/by-id/*", true, &dev_sdxy_seen, false, | |
3059 | type_scsi_sat, autodetect); | |
3060 | p_dev_sdxy_seen = &dev_sdxy_seen; // Check for duplicates below | |
3061 | } | |
a86ec89e | 3062 | |
ff28b140 TL |
3063 | get_dev_list(devlist, "/dev/sd[a-z]", true, p_dev_sdxy_seen, false, type_scsi_sat, autodetect); |
3064 | get_dev_list(devlist, "/dev/sd[a-c][a-z]", true, p_dev_sdxy_seen, false, type_scsi_sat, autodetect); | |
2127e193 | 3065 | |
ee38a438 GI |
3066 | // get device list from the megaraid device |
3067 | get_dev_megasas(devlist); | |
7f0798ef | 3068 | } |
2127e193 | 3069 | |
ff28b140 TL |
3070 | if (type_nvme) { |
3071 | get_dev_list(devlist, "/dev/nvme[0-9]", false, 0, true, type_nvme, false); | |
3072 | get_dev_list(devlist, "/dev/nvme[1-9][0-9]", false, 0, true, type_nvme, false); | |
3073 | } | |
2127e193 | 3074 | |
ff28b140 | 3075 | return true; |
2127e193 GI |
3076 | } |
3077 | ||
3078 | ata_device * linux_smart_interface::get_ata_device(const char * name, const char * type) | |
3079 | { | |
3080 | return new linux_ata_device(this, name, type); | |
3081 | } | |
3082 | ||
3083 | scsi_device * linux_smart_interface::get_scsi_device(const char * name, const char * type) | |
3084 | { | |
3085 | return new linux_scsi_device(this, name, type); | |
3086 | } | |
3087 | ||
a86ec89e GI |
3088 | nvme_device * linux_smart_interface::get_nvme_device(const char * name, const char * type, |
3089 | unsigned nsid) | |
3090 | { | |
3091 | return new linux_nvme_device(this, name, type, nsid); | |
3092 | } | |
3093 | ||
2127e193 GI |
3094 | smart_device * linux_smart_interface::missing_option(const char * opt) |
3095 | { | |
3096 | set_err(EINVAL, "requires option '%s'", opt); | |
3097 | return 0; | |
3098 | } | |
832b75ed | 3099 | |
ee38a438 GI |
3100 | int |
3101 | linux_smart_interface::megasas_dcmd_cmd(int bus_no, uint32_t opcode, void *buf, | |
3102 | size_t bufsize, uint8_t *mbox, size_t mboxlen, uint8_t *statusp) | |
3103 | { | |
3104 | struct megasas_iocpacket ioc; | |
3105 | ||
3106 | if ((mbox != NULL && (mboxlen == 0 || mboxlen > MFI_MBOX_SIZE)) || | |
3107 | (mbox == NULL && mboxlen != 0)) | |
3108 | { | |
3109 | errno = EINVAL; | |
3110 | return (-1); | |
3111 | } | |
3112 | ||
3113 | bzero(&ioc, sizeof(ioc)); | |
3114 | struct megasas_dcmd_frame * dcmd = &ioc.frame.dcmd; | |
3115 | ioc.host_no = bus_no; | |
3116 | if (mbox) | |
3117 | bcopy(mbox, dcmd->mbox.w, mboxlen); | |
3118 | dcmd->cmd = MFI_CMD_DCMD; | |
3119 | dcmd->timeout = 0; | |
3120 | dcmd->flags = 0; | |
3121 | dcmd->data_xfer_len = bufsize; | |
3122 | dcmd->opcode = opcode; | |
3123 | ||
3124 | if (bufsize > 0) { | |
3125 | dcmd->sge_count = 1; | |
3126 | dcmd->data_xfer_len = bufsize; | |
3127 | dcmd->sgl.sge32[0].phys_addr = (intptr_t)buf; | |
3128 | dcmd->sgl.sge32[0].length = (uint32_t)bufsize; | |
3129 | ioc.sge_count = 1; | |
3130 | ioc.sgl_off = offsetof(struct megasas_dcmd_frame, sgl); | |
3131 | ioc.sgl[0].iov_base = buf; | |
3132 | ioc.sgl[0].iov_len = bufsize; | |
3133 | } | |
3134 | ||
3135 | int fd; | |
3136 | if ((fd = ::open("/dev/megaraid_sas_ioctl_node", O_RDWR)) <= 0) { | |
3137 | return (errno); | |
3138 | } | |
3139 | ||
3140 | int r = ioctl(fd, MEGASAS_IOC_FIRMWARE, &ioc); | |
a86ec89e | 3141 | ::close(fd); |
ee38a438 GI |
3142 | if (r < 0) { |
3143 | return (r); | |
3144 | } | |
3145 | ||
3146 | if (statusp != NULL) | |
3147 | *statusp = dcmd->cmd_status; | |
3148 | else if (dcmd->cmd_status != MFI_STAT_OK) { | |
3149 | fprintf(stderr, "command %x returned error status %x\n", | |
3150 | opcode, dcmd->cmd_status); | |
3151 | errno = EIO; | |
3152 | return (-1); | |
3153 | } | |
3154 | return (0); | |
3155 | } | |
3156 | ||
3157 | int | |
3158 | linux_smart_interface::megasas_pd_add_list(int bus_no, smart_device_list & devlist) | |
3159 | { | |
3160 | /* | |
3161 | * Keep fetching the list in a loop until we have a large enough | |
3162 | * buffer to hold the entire list. | |
3163 | */ | |
3164 | megasas_pd_list * list = 0; | |
3165 | for (unsigned list_size = 1024; ; ) { | |
a86ec89e | 3166 | list = reinterpret_cast<megasas_pd_list *>(realloc(list, list_size)); |
ee38a438 GI |
3167 | if (!list) |
3168 | throw std::bad_alloc(); | |
3169 | bzero(list, list_size); | |
3170 | if (megasas_dcmd_cmd(bus_no, MFI_DCMD_PD_GET_LIST, list, list_size, NULL, 0, | |
3171 | NULL) < 0) | |
3172 | { | |
3173 | free(list); | |
3174 | return (-1); | |
3175 | } | |
3176 | if (list->size <= list_size) | |
3177 | break; | |
3178 | list_size = list->size; | |
3179 | } | |
3180 | ||
3181 | // adding all SCSI devices | |
3182 | for (unsigned i = 0; i < list->count; i++) { | |
3183 | if(list->addr[i].scsi_dev_type) | |
3184 | continue; /* non disk device found */ | |
3185 | char line[128]; | |
3186 | snprintf(line, sizeof(line) - 1, "/dev/bus/%d", bus_no); | |
a86ec89e | 3187 | smart_device * dev = new linux_megaraid_device(this, line, list->addr[i].device_id); |
ee38a438 GI |
3188 | devlist.push_back(dev); |
3189 | } | |
3190 | free(list); | |
3191 | return (0); | |
3192 | } | |
3193 | ||
cfbba5b9 GI |
3194 | // Return kernel release as integer ("2.6.31" -> 206031) |
3195 | static unsigned get_kernel_release() | |
3196 | { | |
3197 | struct utsname u; | |
3198 | if (uname(&u)) | |
3199 | return 0; | |
3200 | unsigned x = 0, y = 0, z = 0; | |
3201 | if (!(sscanf(u.release, "%u.%u.%u", &x, &y, &z) == 3 | |
3202 | && x < 100 && y < 100 && z < 1000 )) | |
3203 | return 0; | |
3204 | return x * 100000 + y * 1000 + z; | |
3205 | } | |
3206 | ||
f9e10201 JD |
3207 | // Check for SCSI host proc_name "hpsa" |
3208 | static bool is_hpsa(const char * name) | |
3209 | { | |
3210 | char path[128]; | |
3211 | snprintf(path, sizeof(path), "/sys/block/%s/device", name); | |
ff28b140 | 3212 | char * syshostpath = realpath(path, (char *)0); |
f9e10201 JD |
3213 | if (!syshostpath) |
3214 | return false; | |
3215 | ||
3216 | char * syshost = strrchr(syshostpath, '/'); | |
3217 | if (!syshost) { | |
3218 | free(syshostpath); | |
3219 | return false; | |
3220 | } | |
3221 | ||
3222 | char * hostsep = strchr(++syshost, ':'); | |
3223 | if (hostsep) | |
3224 | *hostsep = 0; | |
3225 | ||
3226 | snprintf(path, sizeof(path), "/sys/class/scsi_host/host%s/proc_name", syshost); | |
3227 | free(syshostpath); | |
3228 | int fd = open(path, O_RDONLY); | |
3229 | if (fd < 0) | |
3230 | return false; | |
3231 | ||
3232 | char proc_name[32]; | |
3233 | ssize_t n = read(fd, proc_name, sizeof(proc_name) - 1); | |
3234 | close(fd); | |
3235 | if (n < 4) | |
3236 | return false; | |
3237 | ||
3238 | proc_name[n] = 0; | |
3239 | if (proc_name[n - 1] == '\n') | |
3240 | proc_name[n - 1] = 0; | |
3241 | ||
3242 | if (scsi_debugmode > 1) | |
3243 | pout("%s -> %s: \"%s\"\n", name, path, proc_name); | |
3244 | ||
3245 | if (strcmp(proc_name, "hpsa")) | |
3246 | return false; | |
3247 | ||
3248 | return true; | |
3249 | } | |
3250 | ||
832b75ed GG |
3251 | // Guess device type (ata or scsi) based on device name (Linux |
3252 | // specific) SCSI device name in linux can be sd, sr, scd, st, nst, | |
3253 | // osst, nosst and sg. | |
2127e193 GI |
3254 | smart_device * linux_smart_interface::autodetect_smart_device(const char * name) |
3255 | { | |
cfbba5b9 | 3256 | const char * test_name = name; |
a37e7145 | 3257 | |
cfbba5b9 GI |
3258 | // Dereference symlinks |
3259 | struct stat st; | |
3260 | std::string pathbuf; | |
3261 | if (!lstat(name, &st) && S_ISLNK(st.st_mode)) { | |
3262 | char * p = realpath(name, (char *)0); | |
3263 | if (p) { | |
3264 | pathbuf = p; | |
3265 | free(p); | |
3266 | test_name = pathbuf.c_str(); | |
3267 | } | |
54965743 | 3268 | } |
cfbba5b9 | 3269 | |
832b75ed | 3270 | // Remove the leading /dev/... if it's there |
cfbba5b9 GI |
3271 | static const char dev_prefix[] = "/dev/"; |
3272 | if (str_starts_with(test_name, dev_prefix)) | |
3273 | test_name += strlen(dev_prefix); | |
a37e7145 | 3274 | |
832b75ed | 3275 | // form /dev/h* or h* |
cfbba5b9 | 3276 | if (str_starts_with(test_name, "h")) |
2127e193 | 3277 | return new linux_ata_device(this, name, ""); |
a37e7145 | 3278 | |
832b75ed | 3279 | // form /dev/ide/* or ide/* |
cfbba5b9 | 3280 | if (str_starts_with(test_name, "ide/")) |
2127e193 | 3281 | return new linux_ata_device(this, name, ""); |
832b75ed GG |
3282 | |
3283 | // form /dev/s* or s* | |
cfbba5b9 | 3284 | if (str_starts_with(test_name, "s")) { |
2127e193 GI |
3285 | |
3286 | // Try to detect possible USB->(S)ATA bridge | |
3287 | unsigned short vendor_id = 0, product_id = 0, version = 0; | |
cfbba5b9 | 3288 | if (get_usb_id(test_name, vendor_id, product_id, version)) { |
2127e193 GI |
3289 | const char * usbtype = get_usb_dev_type_by_id(vendor_id, product_id, version); |
3290 | if (!usbtype) | |
3291 | return 0; | |
cfbba5b9 GI |
3292 | |
3293 | // Kernels before 2.6.29 do not support the sense data length | |
3294 | // required for SAT ATA PASS-THROUGH(16) | |
3295 | if (!strcmp(usbtype, "sat") && get_kernel_release() < 206029) | |
2127e193 | 3296 | usbtype = "sat,12"; |
cfbba5b9 | 3297 | |
2127e193 GI |
3298 | // Return SAT/USB device for this type |
3299 | // (Note: linux_scsi_device::autodetect_open() will not be called in this case) | |
ff28b140 | 3300 | return get_scsi_passthrough_device(usbtype, new linux_scsi_device(this, name, "")); |
2127e193 GI |
3301 | } |
3302 | ||
f9e10201 JD |
3303 | // Fail if hpsa driver |
3304 | if (is_hpsa(test_name)) | |
3305 | return missing_option("-d cciss,N"); | |
3306 | ||
3307 | // No USB bridge or hpsa driver found, assume regular SCSI device | |
2127e193 GI |
3308 | return new linux_scsi_device(this, name, ""); |
3309 | } | |
832b75ed GG |
3310 | |
3311 | // form /dev/scsi/* or scsi/* | |
cfbba5b9 | 3312 | if (str_starts_with(test_name, "scsi/")) |
2127e193 | 3313 | return new linux_scsi_device(this, name, ""); |
a37e7145 | 3314 | |
f9e10201 JD |
3315 | // form /dev/bsg/* or bsg/* |
3316 | if (str_starts_with(test_name, "bsg/")) | |
3317 | return new linux_scsi_device(this, name, ""); | |
3318 | ||
832b75ed | 3319 | // form /dev/ns* or ns* |
cfbba5b9 | 3320 | if (str_starts_with(test_name, "ns")) |
2127e193 | 3321 | return new linux_scsi_device(this, name, ""); |
a37e7145 | 3322 | |
832b75ed | 3323 | // form /dev/os* or os* |
cfbba5b9 | 3324 | if (str_starts_with(test_name, "os")) |
2127e193 | 3325 | return new linux_scsi_device(this, name, ""); |
a37e7145 | 3326 | |
832b75ed | 3327 | // form /dev/nos* or nos* |
cfbba5b9 | 3328 | if (str_starts_with(test_name, "nos")) |
2127e193 | 3329 | return new linux_scsi_device(this, name, ""); |
832b75ed | 3330 | |
a86ec89e GI |
3331 | // form /dev/nvme* or nvme* |
3332 | if (str_starts_with(test_name, "nvme")) | |
3333 | return new linux_nvme_device(this, name, "", 0 /* use default nsid */); | |
3334 | ||
cfbba5b9 GI |
3335 | // form /dev/tw[ael]* or tw[ael]* |
3336 | if (str_starts_with(test_name, "tw") && strchr("ael", test_name[2])) | |
2127e193 GI |
3337 | return missing_option("-d 3ware,N"); |
3338 | ||
cfbba5b9 GI |
3339 | // form /dev/cciss/* or cciss/* |
3340 | if (str_starts_with(test_name, "cciss/")) | |
2127e193 GI |
3341 | return missing_option("-d cciss,N"); |
3342 | ||
832b75ed | 3343 | // we failed to recognize any of the forms |
2127e193 | 3344 | return 0; |
832b75ed GG |
3345 | } |
3346 | ||
2127e193 GI |
3347 | smart_device * linux_smart_interface::get_custom_smart_device(const char * name, const char * type) |
3348 | { | |
3349 | // Marvell ? | |
3350 | if (!strcmp(type, "marvell")) | |
3351 | return new linux_marvell_device(this, name, type); | |
3352 | ||
3353 | // 3Ware ? | |
3354 | int disknum = -1, n1 = -1, n2 = -1; | |
3355 | if (sscanf(type, "3ware,%n%d%n", &n1, &disknum, &n2) == 1 || n1 == 6) { | |
3356 | if (n2 != (int)strlen(type)) { | |
3357 | set_err(EINVAL, "Option -d 3ware,N requires N to be a non-negative integer"); | |
3358 | return 0; | |
3359 | } | |
3360 | if (!(0 <= disknum && disknum <= 127)) { | |
3361 | set_err(EINVAL, "Option -d 3ware,N (N=%d) must have 0 <= N <= 127", disknum); | |
3362 | return 0; | |
3363 | } | |
3364 | ||
cfbba5b9 GI |
3365 | if (!strncmp(name, "/dev/twl", 8)) |
3366 | return new linux_escalade_device(this, name, linux_escalade_device::AMCC_3WARE_9700_CHAR, disknum); | |
3367 | else if (!strncmp(name, "/dev/twa", 8)) | |
2127e193 GI |
3368 | return new linux_escalade_device(this, name, linux_escalade_device::AMCC_3WARE_9000_CHAR, disknum); |
3369 | else if (!strncmp(name, "/dev/twe", 8)) | |
3370 | return new linux_escalade_device(this, name, linux_escalade_device::AMCC_3WARE_678K_CHAR, disknum); | |
3371 | else | |
3372 | return new linux_escalade_device(this, name, linux_escalade_device::AMCC_3WARE_678K, disknum); | |
3373 | } | |
832b75ed | 3374 | |
2127e193 GI |
3375 | // Areca? |
3376 | disknum = n1 = n2 = -1; | |
f4e463df GI |
3377 | int encnum = 1; |
3378 | if (sscanf(type, "areca,%n%d/%d%n", &n1, &disknum, &encnum, &n2) >= 1 || n1 == 6) { | |
3379 | if (!(1 <= disknum && disknum <= 128)) { | |
3380 | set_err(EINVAL, "Option -d areca,N/E (N=%d) must have 1 <= N <= 128", disknum); | |
2127e193 GI |
3381 | return 0; |
3382 | } | |
f4e463df GI |
3383 | if (!(1 <= encnum && encnum <= 8)) { |
3384 | set_err(EINVAL, "Option -d areca,N/E (E=%d) must have 1 <= E <= 8", encnum); | |
2127e193 GI |
3385 | return 0; |
3386 | } | |
ee38a438 | 3387 | return new linux_areca_ata_device(this, name, disknum, encnum); |
2127e193 GI |
3388 | } |
3389 | ||
3390 | // Highpoint ? | |
3391 | int controller = -1, channel = -1; disknum = 1; | |
3392 | n1 = n2 = -1; int n3 = -1; | |
3393 | if (sscanf(type, "hpt,%n%d/%d%n/%d%n", &n1, &controller, &channel, &n2, &disknum, &n3) >= 2 || n1 == 4) { | |
3394 | int len = strlen(type); | |
3395 | if (!(n2 == len || n3 == len)) { | |
3396 | set_err(EINVAL, "Option '-d hpt,L/M/N' supports 2-3 items"); | |
3397 | return 0; | |
3398 | } | |
3399 | if (!(1 <= controller && controller <= 8)) { | |
3400 | set_err(EINVAL, "Option '-d hpt,L/M/N' invalid controller id L supplied"); | |
3401 | return 0; | |
3402 | } | |
3d17a85c | 3403 | if (!(1 <= channel && channel <= 128)) { |
2127e193 GI |
3404 | set_err(EINVAL, "Option '-d hpt,L/M/N' invalid channel number M supplied"); |
3405 | return 0; | |
3406 | } | |
3407 | if (!(1 <= disknum && disknum <= 15)) { | |
3408 | set_err(EINVAL, "Option '-d hpt,L/M/N' invalid pmport number N supplied"); | |
3409 | return 0; | |
3410 | } | |
3411 | return new linux_highpoint_device(this, name, controller, channel, disknum); | |
3412 | } | |
3413 | ||
3414 | #ifdef HAVE_LINUX_CCISS_IOCTL_H | |
3415 | // CCISS ? | |
3416 | disknum = n1 = n2 = -1; | |
3417 | if (sscanf(type, "cciss,%n%d%n", &n1, &disknum, &n2) == 1 || n1 == 6) { | |
3418 | if (n2 != (int)strlen(type)) { | |
3419 | set_err(EINVAL, "Option -d cciss,N requires N to be a non-negative integer"); | |
3420 | return 0; | |
3421 | } | |
e9583e0c GI |
3422 | if (!(0 <= disknum && disknum <= 127)) { |
3423 | set_err(EINVAL, "Option -d cciss,N (N=%d) must have 0 <= N <= 127", disknum); | |
2127e193 GI |
3424 | return 0; |
3425 | } | |
ee38a438 | 3426 | return get_sat_device("sat,auto", new linux_cciss_device(this, name, disknum)); |
2127e193 GI |
3427 | } |
3428 | #endif // HAVE_LINUX_CCISS_IOCTL_H | |
832b75ed | 3429 | |
2127e193 GI |
3430 | // MegaRAID ? |
3431 | if (sscanf(type, "megaraid,%d", &disknum) == 1) { | |
a86ec89e | 3432 | return new linux_megaraid_device(this, name, disknum); |
2127e193 | 3433 | } |
d2e702cf GI |
3434 | |
3435 | //aacraid? | |
a86ec89e GI |
3436 | unsigned host, chan, device; |
3437 | if (sscanf(type, "aacraid,%u,%u,%u", &host, &chan, &device) == 3) { | |
d2e702cf GI |
3438 | //return new linux_aacraid_device(this,name,channel,device); |
3439 | return get_sat_device("sat,auto", | |
a86ec89e | 3440 | new linux_aacraid_device(this, name, host, chan, device)); |
d2e702cf GI |
3441 | |
3442 | } | |
3443 | ||
2127e193 GI |
3444 | return 0; |
3445 | } | |
832b75ed | 3446 | |
54965743 | 3447 | std::string linux_smart_interface::get_valid_custom_dev_types_str() |
2127e193 | 3448 | { |
d2e702cf | 3449 | return "marvell, areca,N/E, 3ware,N, hpt,L/M/N, megaraid,N, aacraid,H,L,ID" |
2127e193 GI |
3450 | #ifdef HAVE_LINUX_CCISS_IOCTL_H |
3451 | ", cciss,N" | |
832b75ed | 3452 | #endif |
2127e193 GI |
3453 | ; |
3454 | } | |
3455 | ||
3456 | } // namespace | |
3457 | ||
2127e193 GI |
3458 | ///////////////////////////////////////////////////////////////////////////// |
3459 | /// Initialize platform interface and register with smi() | |
3460 | ||
3461 | void smart_interface::init() | |
3462 | { | |
3463 | static os_linux::linux_smart_interface the_interface; | |
3464 | smart_interface::set(&the_interface); | |
3465 | } |