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