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