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