]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - os_linux.cpp
Merge branch 'upstream'
[mirror_smartmontools-debian.git] / os_linux.cpp
1 /*
2 * os_linux.c
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2003-8 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2003-8 Doug Gilbert <dougg@torque.net>
8 *
9 * Parts of this file are derived from code that was
10 *
11 * Written By: Adam Radford <linux@3ware.com>
12 * Modifications By: Joel Jacobson <linux@3ware.com>
13 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
14 * Brad Strand <linux@3ware.com>
15 *
16 * Copyright (C) 1999-2003 3ware Inc.
17 *
18 * Kernel compatablity By: Andre Hedrick <andre@suse.com>
19 * Non-Copyright (C) 2000 Andre Hedrick <andre@suse.com>
20 *
21 * Other ars of this file are derived from code that was
22 *
23 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
24 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
25 *
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2, or (at your option)
29 * any later version.
30 *
31 * You should have received a copy of the GNU General Public License
32 * (for example COPYING); if not, write to the Free
33 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 *
35 * This code was originally developed as a Senior Thesis by Michael Cornwell
36 * at the Concurrent Systems Laboratory (now part of the Storage Systems
37 * Research Center), Jack Baskin School of Engineering, University of
38 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
39 *
40 */
41
42 // This file contains the linux-specific IOCTL parts of
43 // smartmontools. It includes one interface routine for ATA devices,
44 // one for SCSI devices, and one for ATA devices behind escalade
45 // controllers.
46
47 #include "config.h"
48
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <glob.h>
52
53 #include <scsi/scsi_ioctl.h>
54 #include <scsi/sg.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sys/ioctl.h>
58 #include <sys/stat.h>
59 #include <unistd.h>
60 #ifndef makedev // old versions of types.h do not include sysmacros.h
61 #include <sys/sysmacros.h>
62 #endif
63
64 #include "int64.h"
65 #include "atacmds.h"
66 #include "extern.h"
67 extern smartmonctrl * con;
68 #include "os_linux.h"
69 #include "scsicmds.h"
70 #include "utility.h"
71 #include "extern.h"
72 #include "cciss.h"
73
74 #ifndef ENOTSUP
75 #define ENOTSUP ENOSYS
76 #endif
77 typedef unsigned long long u8;
78
79
80 #define ARGUSED(x) ((void)(x))
81
82 static const char *filenameandversion="$Id: os_linux.cpp,v 1.100 2008/03/04 22:09:47 ballen4705 Exp $";
83
84 const char *os_XXXX_c_cvsid="$Id: os_linux.cpp,v 1.100 2008/03/04 22:09:47 ballen4705 Exp $" \
85 ATACMDS_H_CVSID CONFIG_H_CVSID INT64_H_CVSID OS_LINUX_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID;
86
87 // to hold onto exit code for atexit routine
88 extern int exitstatus;
89
90 // global variable holding byte count of allocated memory
91 extern long long bytes;
92
93 /* for passing global control variables */
94 extern smartmonctrl *con;
95
96 /* This function will setup and fix device nodes for a 3ware controller. */
97 #define MAJOR_STRING_LENGTH 3
98 #define DEVICE_STRING_LENGTH 32
99 #define NODE_STRING_LENGTH 16
100 int setup_3ware_nodes(char *nodename, char *driver_name) {
101 int tw_major = 0;
102 int index = 0;
103 char majorstring[MAJOR_STRING_LENGTH+1];
104 char device_name[DEVICE_STRING_LENGTH+1];
105 char nodestring[NODE_STRING_LENGTH];
106 struct stat stat_buf;
107 FILE *file;
108
109 /* First try to open up /proc/devices */
110 if (!(file = fopen("/proc/devices", "r"))) {
111 pout("Error opening /proc/devices to check/create 3ware device nodes\n");
112 syserror("fopen");
113 return 0; // don't fail here: user might not have /proc !
114 }
115
116 /* Attempt to get device major number */
117 while (EOF != fscanf(file, "%3s %32s", majorstring, device_name)) {
118 majorstring[MAJOR_STRING_LENGTH]='\0';
119 device_name[DEVICE_STRING_LENGTH]='\0';
120 if (!strncmp(device_name, nodename, DEVICE_STRING_LENGTH)) {
121 tw_major = atoi(majorstring);
122 break;
123 }
124 }
125 fclose(file);
126
127 /* See if we found a major device number */
128 if (!tw_major) {
129 pout("No major number for /dev/%s listed in /proc/devices. Is the %s driver loaded?\n", nodename, driver_name);
130 return 2;
131 }
132
133 /* Now check if nodes are correct */
134 for (index=0; index<16; index++) {
135 sprintf(nodestring, "/dev/%s%d", nodename, index);
136
137 /* Try to stat the node */
138 if ((stat(nodestring, &stat_buf))) {
139 /* Create a new node if it doesn't exist */
140 if (mknod(nodestring, S_IFCHR|0600, makedev(tw_major, index))) {
141 pout("problem creating 3ware device nodes %s", nodestring);
142 syserror("mknod");
143 return 3;
144 }
145 }
146
147 /* See if nodes major and minor numbers are correct */
148 if ((tw_major != (int)(major(stat_buf.st_rdev))) ||
149 (index != (int)(minor(stat_buf.st_rdev))) ||
150 (!S_ISCHR(stat_buf.st_mode))) {
151
152 /* Delete the old node */
153 if (unlink(nodestring)) {
154 pout("problem unlinking stale 3ware device node %s", nodestring);
155 syserror("unlink");
156 return 4;
157 }
158
159 /* Make a new node */
160 if (mknod(nodestring, S_IFCHR|0600, makedev(tw_major, index))) {
161 pout("problem creating 3ware device nodes %s", nodestring);
162 syserror("mknod");
163 return 5;
164 }
165 }
166 }
167 return 0;
168 }
169
170 static char prev_scsi_dev[128];
171
172 // equivalent to open(path, flags)
173 int deviceopen(const char *pathname, char *type){
174 int fd;
175
176 if (0 == strcmp(type,"SCSI")) {
177 strncpy(prev_scsi_dev, pathname, sizeof(prev_scsi_dev) - 1);
178 fd = open(pathname, O_RDWR | O_NONBLOCK);
179 if (fd < 0 && errno == EROFS)
180 fd = open(pathname, O_RDONLY | O_NONBLOCK);
181 return fd;
182 } else if (0 == strcmp(type,"ATA")) {
183 // smartd re-opens SCSI devices with "type"==ATA for some reason.
184 // If that was a SCSI generic device (e.g. /dev/sg0) then the
185 // sg driver wants O_RDWR to allow through ATA PASS-THROUGH commands.
186 // The purpose of the next code line is to limit the scope of
187 // this change as a release is pending (and smartd needs a rewrite).
188 if (0 == strncmp(pathname, prev_scsi_dev, sizeof(prev_scsi_dev)))
189 return open(pathname, O_RDWR | O_NONBLOCK);
190 else
191 return open(pathname, O_RDONLY | O_NONBLOCK);
192 } else if (0 == strcmp(type,"ATA_3WARE_9000")) {
193 // the device nodes for this controller are dynamically assigned,
194 // so we need to check that they exist with the correct major
195 // numbers and if not, create them
196 if (setup_3ware_nodes("twa", "3w-9xxx")) {
197 if (!errno)
198 errno=ENXIO;
199 return -1;
200 }
201 return open(pathname, O_RDONLY | O_NONBLOCK);
202 }
203 else if (0 == strcmp(type,"ATA_3WARE_678K")) {
204 // the device nodes for this controller are dynamically assigned,
205 // so we need to check that they exist with the correct major
206 // numbers and if not, create them
207 if (setup_3ware_nodes("twe", "3w-xxxx")) {
208 if (!errno)
209 errno=ENXIO;
210 return -1;
211 }
212 return open(pathname, O_RDONLY | O_NONBLOCK);
213 }
214 else if(0 == strcmp(type, "CCISS")) {
215 // the device is a cciss smart array device.
216 return open(pathname, O_RDWR | O_NONBLOCK);
217 }
218 else
219 return -1;
220
221 }
222
223 // equivalent to close(file descriptor)
224 int deviceclose(int fd){
225 return close(fd);
226 }
227
228 // print examples for smartctl
229 void print_smartctl_examples(){
230 printf("=================================================== SMARTCTL EXAMPLES =====\n\n");
231 #ifdef HAVE_GETOPT_LONG
232 printf(
233 " smartctl --all /dev/hda (Prints all SMART information)\n\n"
234 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
235 " (Enables SMART on first disk)\n\n"
236 " smartctl --test=long /dev/hda (Executes extended disk self-test)\n\n"
237 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
238 " (Prints Self-Test & Attribute errors)\n"
239 " smartctl --all --device=3ware,2 /dev/sda\n"
240 " smartctl --all --device=3ware,2 /dev/twe0\n"
241 " smartctl --all --device=3ware,2 /dev/twa0\n"
242 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
243 " smartctl --all --device=hpt,1/1/3 /dev/sda\n"
244 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
245 " of the 1st channel on the 1st HighPoint RAID controller)\n"
246 );
247 #else
248 printf(
249 " smartctl -a /dev/hda (Prints all SMART information)\n"
250 " smartctl -s on -o on -S on /dev/hda (Enables SMART on first disk)\n"
251 " smartctl -t long /dev/hda (Executes extended disk self-test)\n"
252 " smartctl -A -l selftest -q errorsonly /dev/hda\n"
253 " (Prints Self-Test & Attribute errors)\n"
254 " smartctl -a -d 3ware,2 /dev/sda\n"
255 " smartctl -a -d 3ware,2 /dev/twa0\n"
256 " smartctl -a -d 3ware,2 /dev/twe0\n"
257 " (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
258 " smartctl -a -d hpt,1/1/3 /dev/sda\n"
259 " (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
260 " of the 1st channel on the 1st HighPoint RAID controller)\n"
261 );
262 #endif
263 return;
264 }
265
266
267 // we are going to take advantage of the fact that Linux's devfs will only
268 // have device entries for devices that exist. So if we get the equivalent of
269 // ls /dev/hd[a-t], we have all the ATA devices on the system
270 //
271 // If any errors occur, leave errno set as it was returned by the
272 // system call, and return <0.
273 int get_dev_names(char*** names, const char* pattern, const char* name, int max) {
274 int n = 0, retglob, i, lim;
275 char** mp;
276 glob_t globbuf;
277
278 memset(&globbuf, 0, sizeof(globbuf));
279
280 // in case of non-clean exit
281 *names=NULL;
282
283 // Use glob to look for any directory entries matching the pattern
284 if ((retglob=glob(pattern, GLOB_ERR, NULL, &globbuf))) {
285
286 // glob failed: free memory and return
287 globfree(&globbuf);
288
289 if (retglob==GLOB_NOMATCH){
290 pout("glob(3) found no matches for pattern %s\n", pattern);
291 return 0;
292 }
293
294 if (retglob==GLOB_NOSPACE)
295 pout("glob(3) ran out of memory matching pattern %s\n", pattern);
296 #ifdef GLOB_ABORTED // missing in old versions of glob.h
297 else if (retglob==GLOB_ABORTED)
298 pout("glob(3) aborted matching pattern %s\n", pattern);
299 #endif
300 else
301 pout("Unexplained error in glob(3) of pattern %s\n", pattern);
302
303 return -1;
304 }
305
306 // did we find too many paths?
307 lim = ((int)globbuf.gl_pathc < max) ? (int)globbuf.gl_pathc : max;
308 if (lim < (int)globbuf.gl_pathc)
309 pout("glob(3) found %d > MAX=%d devices matching pattern %s: ignoring %d paths\n",
310 (int)globbuf.gl_pathc, max, pattern, (int)(globbuf.gl_pathc-max));
311
312 // allocate space for up to lim number of ATA devices
313 if (!(mp = (char **)calloc(lim, sizeof(char*)))){
314 pout("Out of memory constructing scan device list\n");
315 return -1;
316 }
317
318 // now step through the list returned by glob. If not a link, copy
319 // to list. If it is a link, evaluate it and see if the path ends
320 // in "disc".
321 for (i=0; i<lim; i++){
322 int retlink;
323
324 // prepare a buffer for storing the link
325 char linkbuf[1024];
326
327 // see if path is a link
328 retlink=readlink(globbuf.gl_pathv[i], linkbuf, 1023);
329
330 // if not a link (or a strange link), keep it
331 if (retlink<=0 || retlink>1023)
332 mp[n++] = CustomStrDup(globbuf.gl_pathv[i], 1, __LINE__, filenameandversion);
333 else {
334 // or if it's a link that points to a disc, follow it
335 char *p;
336 linkbuf[retlink]='\0';
337 if ((p=strrchr(linkbuf,'/')) && !strcmp(p+1, "disc"))
338 // This is the branch of the code that gets followed if we are
339 // using devfs WITH traditional compatibility links. In this
340 // case, we add the traditional device name to the list that
341 // is returned.
342 mp[n++] = CustomStrDup(globbuf.gl_pathv[i], 1, __LINE__, filenameandversion);
343 else {
344 // This is the branch of the code that gets followed if we are
345 // using devfs WITHOUT traditional compatibility links. In
346 // this case, we check that the link to the directory is of
347 // the correct type, and then append "disc" to it.
348 char tmpname[1024]={0};
349 const char * type = (strcmp(name,"ATA") ? "scsi" : "ide");
350 if (strstr(linkbuf, type)){
351 snprintf(tmpname, 1024, "%s/disc", globbuf.gl_pathv[i]);
352 mp[n++] = CustomStrDup(tmpname, 1, __LINE__, filenameandversion);
353 }
354 }
355 }
356 }
357
358 // free memory, track memory usage
359 globfree(&globbuf);
360 mp = static_cast<char **>(realloc(mp,n*(sizeof(char*))));
361 bytes += n*(sizeof(char*));
362
363 // and set up return values
364 *names=mp;
365 return n;
366 }
367
368 // makes a list of device names to scan, for either ATA or SCSI
369 // devices. Return -1 if no memory remaining, else the number of
370 // devices on the list, which can be >=0.
371 int make_device_names (char*** devlist, const char* name) {
372 int retval, maxdev;
373
374 #if 0
375 // for testing case where no device names are found
376 return 0;
377 #endif
378
379 if (!strcmp(name,"SCSI"))
380 retval=get_dev_names(devlist,"/dev/sd[a-z]", name, maxdev=26);
381 else if (!strcmp(name,"ATA"))
382 retval=get_dev_names(devlist,"/dev/hd[a-t]", name, maxdev=20);
383 else
384 // don't recognize disk type!
385 return 0;
386
387 // if we found traditional links, we are done
388 if (retval>0)
389 return retval;
390
391 // else look for devfs entries without traditional links
392 return get_dev_names(devlist,"/dev/discs/disc*", name, maxdev);
393 }
394
395
396 // PURPOSE
397 // This is an interface routine meant to isolate the OS dependent
398 // parts of the code, and to provide a debugging interface. Each
399 // different port and OS needs to provide it's own interface. This
400 // is the linux one.
401 // DETAILED DESCRIPTION OF ARGUMENTS
402 // device: is the file descriptor provided by open()
403 // command: defines the different operations.
404 // select: additional input data if needed (which log, which type of
405 // self-test).
406 // data: location to write output data, if needed (512 bytes).
407 // Note: not all commands use all arguments.
408 // RETURN VALUES
409 // -1 if the command failed
410 // 0 if the command succeeded,
411 // STATUS_CHECK routine:
412 // -1 if the command failed
413 // 0 if the command succeeded and disk SMART status is "OK"
414 // 1 if the command succeeded and disk SMART status is "FAILING"
415
416
417 #define BUFFER_LENGTH (4+512)
418
419 int ata_command_interface(int device, smart_command_set command, int select, char *data){
420 unsigned char buff[BUFFER_LENGTH];
421 // positive: bytes to write to caller. negative: bytes to READ from
422 // caller. zero: non-data command
423 int copydata=0;
424
425 const int HDIO_DRIVE_CMD_OFFSET = 4;
426
427 // See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
428 // buff[0]: ATA COMMAND CODE REGISTER
429 // buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
430 // buff[2]: ATA FEATURES REGISTER
431 // buff[3]: ATA SECTOR COUNT REGISTER
432
433 // Note that on return:
434 // buff[2] contains the ATA SECTOR COUNT REGISTER
435
436 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
437 memset(buff, 0, BUFFER_LENGTH);
438
439 buff[0]=ATA_SMART_CMD;
440 switch (command){
441 case CHECK_POWER_MODE:
442 buff[0]=ATA_CHECK_POWER_MODE;
443 copydata=1;
444 break;
445 case READ_VALUES:
446 buff[2]=ATA_SMART_READ_VALUES;
447 buff[3]=1;
448 copydata=512;
449 break;
450 case READ_THRESHOLDS:
451 buff[2]=ATA_SMART_READ_THRESHOLDS;
452 buff[1]=buff[3]=1;
453 copydata=512;
454 break;
455 case READ_LOG:
456 buff[2]=ATA_SMART_READ_LOG_SECTOR;
457 buff[1]=select;
458 buff[3]=1;
459 copydata=512;
460 break;
461 case WRITE_LOG:
462 break;
463 case IDENTIFY:
464 buff[0]=ATA_IDENTIFY_DEVICE;
465 buff[3]=1;
466 copydata=512;
467 break;
468 case PIDENTIFY:
469 buff[0]=ATA_IDENTIFY_PACKET_DEVICE;
470 buff[3]=1;
471 copydata=512;
472 break;
473 case ENABLE:
474 buff[2]=ATA_SMART_ENABLE;
475 buff[1]=1;
476 break;
477 case DISABLE:
478 buff[2]=ATA_SMART_DISABLE;
479 buff[1]=1;
480 break;
481 case STATUS:
482 // this command only says if SMART is working. It could be
483 // replaced with STATUS_CHECK below.
484 buff[2]=ATA_SMART_STATUS;
485 break;
486 case AUTO_OFFLINE:
487 // NOTE: According to ATAPI 4 and UP, this command is obsolete
488 // select == 241 for enable but no data transfer. Use TASK ioctl.
489 buff[1]=ATA_SMART_AUTO_OFFLINE;
490 buff[2]=select;
491 break;
492 case AUTOSAVE:
493 // select == 248 for enable but no data transfer. Use TASK ioctl.
494 buff[1]=ATA_SMART_AUTOSAVE;
495 buff[2]=select;
496 break;
497 case IMMEDIATE_OFFLINE:
498 buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
499 buff[1]=select;
500 break;
501 case STATUS_CHECK:
502 // This command uses HDIO_DRIVE_TASK and has different syntax than
503 // the other commands.
504 buff[1]=ATA_SMART_STATUS;
505 break;
506 default:
507 pout("Unrecognized command %d in linux_ata_command_interface()\n"
508 "Please contact " PACKAGE_BUGREPORT "\n", command);
509 errno=ENOSYS;
510 return -1;
511 }
512
513 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
514 // only ioctl() that can be used to WRITE data to the disk.
515 if (command==WRITE_LOG) {
516 unsigned char task[sizeof(ide_task_request_t)+512];
517 ide_task_request_t *reqtask=(ide_task_request_t *) task;
518 task_struct_t *taskfile=(task_struct_t *) reqtask->io_ports;
519 int retval;
520
521 memset(task, 0, sizeof(task));
522
523 taskfile->data = 0;
524 taskfile->feature = ATA_SMART_WRITE_LOG_SECTOR;
525 taskfile->sector_count = 1;
526 taskfile->sector_number = select;
527 taskfile->low_cylinder = 0x4f;
528 taskfile->high_cylinder = 0xc2;
529 taskfile->device_head = 0;
530 taskfile->command = ATA_SMART_CMD;
531
532 reqtask->data_phase = TASKFILE_OUT;
533 reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
534 reqtask->out_size = 512;
535 reqtask->in_size = 0;
536
537 // copy user data into the task request structure
538 memcpy(task+sizeof(ide_task_request_t), data, 512);
539
540 if ((retval=ioctl(device, HDIO_DRIVE_TASKFILE, task))) {
541 if (retval==-EINVAL)
542 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
543 return -1;
544 }
545 return 0;
546 }
547
548 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
549 // one is this:
550 if (command==STATUS_CHECK || command==AUTOSAVE || command==AUTO_OFFLINE){
551 int retval;
552
553 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
554 // have to read the IDE driver source code. Sigh.
555 // buff[0]: ATA COMMAND CODE REGISTER
556 // buff[1]: ATA FEATURES REGISTER
557 // buff[2]: ATA SECTOR_COUNT
558 // buff[3]: ATA SECTOR NUMBER
559 // buff[4]: ATA CYL LO REGISTER
560 // buff[5]: ATA CYL HI REGISTER
561 // buff[6]: ATA DEVICE HEAD
562
563 unsigned const char normal_lo=0x4f, normal_hi=0xc2;
564 unsigned const char failed_lo=0xf4, failed_hi=0x2c;
565 buff[4]=normal_lo;
566 buff[5]=normal_hi;
567
568 if ((retval=ioctl(device, HDIO_DRIVE_TASK, buff))) {
569 if (retval==-EINVAL) {
570 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
571 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
572 }
573 else
574 syserror("Error SMART Status command failed");
575 return -1;
576 }
577
578 // Cyl low and Cyl high unchanged means "Good SMART status"
579 if (buff[4]==normal_lo && buff[5]==normal_hi)
580 return 0;
581
582 // These values mean "Bad SMART status"
583 if (buff[4]==failed_lo && buff[5]==failed_hi)
584 return 1;
585
586 // We haven't gotten output that makes sense; print out some debugging info
587 syserror("Error SMART Status command failed");
588 pout("Please get assistance from " PACKAGE_HOMEPAGE "\n");
589 pout("Register values returned from SMART Status command are:\n");
590 pout("ST =0x%02x\n",(int)buff[0]);
591 pout("ERR=0x%02x\n",(int)buff[1]);
592 pout("NS =0x%02x\n",(int)buff[2]);
593 pout("SC =0x%02x\n",(int)buff[3]);
594 pout("CL =0x%02x\n",(int)buff[4]);
595 pout("CH =0x%02x\n",(int)buff[5]);
596 pout("SEL=0x%02x\n",(int)buff[6]);
597 return -1;
598 }
599
600 #if 1
601 // Note to people doing ports to other OSes -- don't worry about
602 // this block -- you can safely ignore it. I have put it here
603 // because under linux when you do IDENTIFY DEVICE to a packet
604 // device, it generates an ugly kernel syslog error message. This
605 // is harmless but frightens users. So this block detects packet
606 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
607 // error message.
608 //
609 // If you read only the ATA specs, it appears as if a packet device
610 // *might* respond to the IDENTIFY DEVICE command. This is
611 // misleading - it's because around the time that SFF-8020 was
612 // incorporated into the ATA-3/4 standard, the ATA authors were
613 // sloppy. See SFF-8020 and you will see that ATAPI devices have
614 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
615 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
616 if (command==IDENTIFY || command==PIDENTIFY){
617 unsigned short deviceid[256];
618 // check the device identity, as seen when the system was booted
619 // or the device was FIRST registered. This will not be current
620 // if the user has subsequently changed some of the parameters. If
621 // device is a packet device, swap the command interpretations.
622 if (!ioctl(device, HDIO_GET_IDENTITY, deviceid) && (deviceid[0] & 0x8000))
623 buff[0]=(command==IDENTIFY)?ATA_IDENTIFY_PACKET_DEVICE:ATA_IDENTIFY_DEVICE;
624 }
625 #endif
626
627 // We are now doing the HDIO_DRIVE_CMD type ioctl.
628 if ((ioctl(device, HDIO_DRIVE_CMD, buff)))
629 return -1;
630
631 // CHECK POWER MODE command returns information in the Sector Count
632 // register (buff[3]). Copy to return data buffer.
633 if (command==CHECK_POWER_MODE)
634 buff[HDIO_DRIVE_CMD_OFFSET]=buff[2];
635
636 // if the command returns data then copy it back
637 if (copydata)
638 memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
639
640 return 0;
641 }
642
643 // >>>>>> Start of general SCSI specific linux code
644
645 /* Linux specific code.
646 * Historically smartmontools (and smartsuite before it) used the
647 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
648 * nodes that use the SCSI subsystem. A better interface has been available
649 * via the SCSI generic (sg) driver but this involves the extra step of
650 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
651 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
652 * the sg driver have become available via the SG_IO ioctl which is available
653 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
654 * So the strategy below is to find out if the SG_IO ioctl is available and
655 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
656 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
657
658 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
659 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
660 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
661 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
662 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
663 #define LSCSI_DRIVER_TIMEOUT 0x6
664 #define LSCSI_DID_TIME_OUT 0x3
665 #define LSCSI_DID_BUS_BUSY 0x2
666 #define LSCSI_DID_NO_CONNECT 0x1
667
668 #ifndef SCSI_IOCTL_SEND_COMMAND
669 #define SCSI_IOCTL_SEND_COMMAND 1
670 #endif
671
672 #define SG_IO_PRESENT_UNKNOWN 0
673 #define SG_IO_PRESENT_YES 1
674 #define SG_IO_PRESENT_NO 2
675
676 static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report,
677 int unknown);
678 static int sisc_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report);
679
680 static int sg_io_state = SG_IO_PRESENT_UNKNOWN;
681
682 /* Preferred implementation for issuing SCSI commands in linux. This
683 * function uses the SG_IO ioctl. Return 0 if command issued successfully
684 * (various status values should still be checked). If the SCSI command
685 * cannot be issued then a negative errno value is returned. */
686 static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report,
687 int unknown)
688 {
689 #ifndef SG_IO
690 ARGUSED(dev_fd); ARGUSED(iop); ARGUSED(report);
691 return -ENOTTY;
692 #else
693 struct sg_io_hdr io_hdr;
694
695 if (report > 0) {
696 int k, j;
697 const unsigned char * ucp = iop->cmnd;
698 const char * np;
699 char buff[256];
700 const int sz = (int)sizeof(buff);
701
702 np = scsi_get_opcode_name(ucp[0]);
703 j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
704 for (k = 0; k < (int)iop->cmnd_len; ++k)
705 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
706 if ((report > 1) &&
707 (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
708 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
709
710 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
711 "data, len=%d%s:\n", (int)iop->dxfer_len,
712 (trunc ? " [only first 256 bytes shown]" : ""));
713 dStrHex((const char *)iop->dxferp,
714 (trunc ? 256 : iop->dxfer_len) , 1);
715 }
716 else
717 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
718 pout(buff);
719 }
720 memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
721 io_hdr.interface_id = 'S';
722 io_hdr.cmd_len = iop->cmnd_len;
723 io_hdr.mx_sb_len = iop->max_sense_len;
724 io_hdr.dxfer_len = iop->dxfer_len;
725 io_hdr.dxferp = iop->dxferp;
726 io_hdr.cmdp = iop->cmnd;
727 io_hdr.sbp = iop->sensep;
728 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
729 defaults to 60 seconds. */
730 io_hdr.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000;
731 switch (iop->dxfer_dir) {
732 case DXFER_NONE:
733 io_hdr.dxfer_direction = SG_DXFER_NONE;
734 break;
735 case DXFER_FROM_DEVICE:
736 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
737 break;
738 case DXFER_TO_DEVICE:
739 io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
740 break;
741 default:
742 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
743 return -EINVAL;
744 }
745 iop->resp_sense_len = 0;
746 iop->scsi_status = 0;
747 iop->resid = 0;
748 if (ioctl(dev_fd, SG_IO, &io_hdr) < 0) {
749 if (report && (! unknown))
750 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno,
751 strerror(errno));
752 return -errno;
753 }
754 iop->resid = io_hdr.resid;
755 iop->scsi_status = io_hdr.status;
756 if (report > 0) {
757 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
758 " info=0x%x duration=%d milliseconds resid=%d\n", io_hdr.status,
759 io_hdr.host_status, io_hdr.driver_status, io_hdr.info,
760 io_hdr.duration, io_hdr.resid);
761 if (report > 1) {
762 if (DXFER_FROM_DEVICE == iop->dxfer_dir) {
763 int trunc, len;
764
765 len = iop->dxfer_len - iop->resid;
766 trunc = (len > 256) ? 1 : 0;
767 if (len > 0) {
768 pout(" Incoming data, len=%d%s:\n", len,
769 (trunc ? " [only first 256 bytes shown]" : ""));
770 dStrHex((const char*)iop->dxferp, (trunc ? 256 : len),
771 1);
772 } else
773 pout(" Incoming data trimmed to nothing by resid\n");
774 }
775 }
776 }
777
778 if (io_hdr.info | SG_INFO_CHECK) { /* error or warning */
779 int masked_driver_status = (LSCSI_DRIVER_MASK & io_hdr.driver_status);
780
781 if (0 != io_hdr.host_status) {
782 if ((LSCSI_DID_NO_CONNECT == io_hdr.host_status) ||
783 (LSCSI_DID_BUS_BUSY == io_hdr.host_status) ||
784 (LSCSI_DID_TIME_OUT == io_hdr.host_status))
785 return -ETIMEDOUT;
786 else
787 return -EIO; /* catch all */
788 }
789 if (0 != masked_driver_status) {
790 if (LSCSI_DRIVER_TIMEOUT == masked_driver_status)
791 return -ETIMEDOUT;
792 else if (LSCSI_DRIVER_SENSE != masked_driver_status)
793 return -EIO;
794 }
795 if (LSCSI_DRIVER_SENSE == masked_driver_status)
796 iop->scsi_status = SCSI_STATUS_CHECK_CONDITION;
797 iop->resp_sense_len = io_hdr.sb_len_wr;
798 if ((SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) &&
799 iop->sensep && (iop->resp_sense_len > 0)) {
800 if (report > 1) {
801 pout(" >>> Sense buffer, len=%d:\n",
802 (int)iop->resp_sense_len);
803 dStrHex((const char *)iop->sensep, iop->resp_sense_len , 1);
804 }
805 }
806 if (report) {
807 if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) {
808 if ((iop->sensep[0] & 0x7f) > 0x71)
809 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
810 iop->scsi_status, iop->sensep[1] & 0xf,
811 iop->sensep[2], iop->sensep[3]);
812 else
813 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
814 iop->scsi_status, iop->sensep[2] & 0xf,
815 iop->sensep[12], iop->sensep[13]);
816 }
817 else
818 pout(" status=0x%x\n", iop->scsi_status);
819 }
820 }
821 return 0;
822 #endif
823 }
824
825 struct linux_ioctl_send_command
826 {
827 int inbufsize;
828 int outbufsize;
829 UINT8 buff[MAX_DXFER_LEN + 16];
830 };
831
832 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
833 * support: CDB length (guesses it from opcode), resid and timeout.
834 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
835 * to 2 hours in order to allow long foreground extended self tests. */
836 static int sisc_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report)
837 {
838 struct linux_ioctl_send_command wrk;
839 int status, buff_offset;
840 size_t len;
841
842 memcpy(wrk.buff, iop->cmnd, iop->cmnd_len);
843 buff_offset = iop->cmnd_len;
844 if (report > 0) {
845 int k, j;
846 const unsigned char * ucp = iop->cmnd;
847 const char * np;
848 char buff[256];
849 const int sz = (int)sizeof(buff);
850
851 np = scsi_get_opcode_name(ucp[0]);
852 j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
853 for (k = 0; k < (int)iop->cmnd_len; ++k)
854 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
855 if ((report > 1) &&
856 (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
857 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
858
859 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
860 "data, len=%d%s:\n", (int)iop->dxfer_len,
861 (trunc ? " [only first 256 bytes shown]" : ""));
862 dStrHex((const char *)iop->dxferp,
863 (trunc ? 256 : iop->dxfer_len) , 1);
864 }
865 else
866 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
867 pout(buff);
868 }
869 switch (iop->dxfer_dir) {
870 case DXFER_NONE:
871 wrk.inbufsize = 0;
872 wrk.outbufsize = 0;
873 break;
874 case DXFER_FROM_DEVICE:
875 wrk.inbufsize = 0;
876 if (iop->dxfer_len > MAX_DXFER_LEN)
877 return -EINVAL;
878 wrk.outbufsize = iop->dxfer_len;
879 break;
880 case DXFER_TO_DEVICE:
881 if (iop->dxfer_len > MAX_DXFER_LEN)
882 return -EINVAL;
883 memcpy(wrk.buff + buff_offset, iop->dxferp, iop->dxfer_len);
884 wrk.inbufsize = iop->dxfer_len;
885 wrk.outbufsize = 0;
886 break;
887 default:
888 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
889 return -EINVAL;
890 }
891 iop->resp_sense_len = 0;
892 iop->scsi_status = 0;
893 iop->resid = 0;
894 status = ioctl(dev_fd, SCSI_IOCTL_SEND_COMMAND, &wrk);
895 if (-1 == status) {
896 if (report)
897 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
898 errno, strerror(errno));
899 return -errno;
900 }
901 if (0 == status) {
902 if (report > 0)
903 pout(" status=0\n");
904 if (DXFER_FROM_DEVICE == iop->dxfer_dir) {
905 memcpy(iop->dxferp, wrk.buff, iop->dxfer_len);
906 if (report > 1) {
907 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
908
909 pout(" Incoming data, len=%d%s:\n", (int)iop->dxfer_len,
910 (trunc ? " [only first 256 bytes shown]" : ""));
911 dStrHex((const char*)iop->dxferp,
912 (trunc ? 256 : iop->dxfer_len) , 1);
913 }
914 }
915 return 0;
916 }
917 iop->scsi_status = status & 0x7e; /* bits 0 and 7 used to be for vendors */
918 if (LSCSI_DRIVER_SENSE == ((status >> 24) & 0xf))
919 iop->scsi_status = SCSI_STATUS_CHECK_CONDITION;
920 len = (SEND_IOCTL_RESP_SENSE_LEN < iop->max_sense_len) ?
921 SEND_IOCTL_RESP_SENSE_LEN : iop->max_sense_len;
922 if ((SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) &&
923 iop->sensep && (len > 0)) {
924 memcpy(iop->sensep, wrk.buff, len);
925 iop->resp_sense_len = len;
926 if (report > 1) {
927 pout(" >>> Sense buffer, len=%d:\n", (int)len);
928 dStrHex((const char *)wrk.buff, len , 1);
929 }
930 }
931 if (report) {
932 if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) {
933 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status & 0xff,
934 wrk.buff[2] & 0xf, wrk.buff[12], wrk.buff[13]);
935 }
936 else
937 pout(" status=0x%x\n", status);
938 }
939 if (iop->scsi_status > 0)
940 return 0;
941 else {
942 if (report > 0)
943 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
944 status);
945 return -EIO; /* give up, assume no device there */
946 }
947 }
948
949 /* SCSI command transmission interface function, linux version.
950 * Returns 0 if SCSI command successfully launched and response
951 * received. Even when 0 is returned the caller should check
952 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
953 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
954 * (e.g. device not present or timeout) or some other problem
955 * (e.g. timeout) then returns a negative errno value */
956 static int do_normal_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop,
957 int report)
958 {
959 int res;
960
961 /* implementation relies on static sg_io_state variable. If not
962 * previously set tries the SG_IO ioctl. If that succeeds assume
963 * that SG_IO ioctl functional. If it fails with an errno value
964 * other than ENODEV (no device) or permission then assume
965 * SCSI_IOCTL_SEND_COMMAND is the only option. */
966 switch (sg_io_state) {
967 case SG_IO_PRESENT_UNKNOWN:
968 /* ignore report argument */
969 if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, 1))) {
970 sg_io_state = SG_IO_PRESENT_YES;
971 return 0;
972 } else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res))
973 return res; /* wait until we see a device */
974 sg_io_state = SG_IO_PRESENT_NO;
975 /* drop through by design */
976 case SG_IO_PRESENT_NO:
977 return sisc_cmnd_io(dev_fd, iop, report);
978 case SG_IO_PRESENT_YES:
979 return sg_io_cmnd_io(dev_fd, iop, report, 0);
980 default:
981 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state);
982 sg_io_state = SG_IO_PRESENT_UNKNOWN;
983 return -EIO; /* report error and reset state */
984 }
985 }
986
987 /* Check and call the right interface. Maybe when the do_generic_scsi_cmd_io interface is better
988 we can take off this crude way of calling the right interface */
989 int do_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report)
990 {
991 switch(con->controller_type)
992 {
993 case CONTROLLER_CCISS:
994 #ifdef HAVE_LINUX_CCISS_IOCTL_H
995 return cciss_io_interface(dev_fd, con->controller_port-1, iop, report);
996 #else
997 {
998 static int warned = 0;
999 if (!warned) {
1000 pout("CCISS support is not available in this build of smartmontools,\n"
1001 "<linux/cciss_ioctl.h> was not available at build time.\n\n");
1002 warned = 1;
1003 }
1004 }
1005 errno = ENOSYS;
1006 return -1;
1007 #endif
1008 // not reached
1009 break;
1010 default:
1011 return do_normal_scsi_cmnd_io(dev_fd, iop, report);
1012 // not reached
1013 break;
1014 }
1015 }
1016
1017 // >>>>>> End of general SCSI specific linux code
1018
1019 // prototype
1020 void printwarning(smart_command_set command);
1021
1022 // PURPOSE
1023 // This is an interface routine meant to isolate the OS dependent
1024 // parts of the code, and to provide a debugging interface. Each
1025 // different port and OS needs to provide it's own interface. This
1026 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1027 // commands to be passed through the SCSI driver.
1028 // DETAILED DESCRIPTION OF ARGUMENTS
1029 // fd: is the file descriptor provided by open()
1030 // disknum is the disk number (0 to 15) in the RAID array
1031 // escalade_type indicates the type of controller type, and if scsi or char interface is used
1032 // command: defines the different operations.
1033 // select: additional input data if needed (which log, which type of
1034 // self-test).
1035 // data: location to write output data, if needed (512 bytes).
1036 // Note: not all commands use all arguments.
1037 // RETURN VALUES
1038 // -1 if the command failed
1039 // 0 if the command succeeded,
1040 // STATUS_CHECK routine:
1041 // -1 if the command failed
1042 // 0 if the command succeeded and disk SMART status is "OK"
1043 // 1 if the command succeeded and disk SMART status is "FAILING"
1044
1045
1046 /* 512 is the max payload size: increase if needed */
1047 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1048 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1049 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1050 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1051
1052 int escalade_command_interface(int fd, int disknum, int escalade_type, smart_command_set command, int select, char *data){
1053
1054 // return value and buffer for ioctl()
1055 int ioctlreturn, readdata=0;
1056
1057 // Used by both the SCSI and char interfaces
1058 TW_Passthru *passthru=NULL;
1059 char ioctl_buffer[TW_IOCTL_BUFFER_SIZE];
1060
1061 // only used for SCSI device interface
1062 TW_Ioctl *tw_ioctl=NULL;
1063 TW_Output *tw_output=NULL;
1064
1065 // only used for 6000/7000/8000 char device interface
1066 TW_New_Ioctl *tw_ioctl_char=NULL;
1067
1068 // only used for 9000 character device interface
1069 TW_Ioctl_Buf_Apache *tw_ioctl_apache=NULL;
1070
1071 memset(ioctl_buffer, 0, TW_IOCTL_BUFFER_SIZE);
1072
1073 if (escalade_type==CONTROLLER_3WARE_9000_CHAR) {
1074 tw_ioctl_apache = (TW_Ioctl_Buf_Apache *)ioctl_buffer;
1075 tw_ioctl_apache->driver_command.control_code = TW_IOCTL_FIRMWARE_PASS_THROUGH;
1076 tw_ioctl_apache->driver_command.buffer_length = 512; /* payload size */
1077 passthru = (TW_Passthru *)&(tw_ioctl_apache->firmware_command.command.oldcommand);
1078 }
1079 else if (escalade_type==CONTROLLER_3WARE_678K_CHAR) {
1080 tw_ioctl_char = (TW_New_Ioctl *)ioctl_buffer;
1081 tw_ioctl_char->data_buffer_length = 512;
1082 passthru = (TW_Passthru *)&(tw_ioctl_char->firmware_command);
1083 }
1084 else if (escalade_type==CONTROLLER_3WARE_678K) {
1085 tw_ioctl = (TW_Ioctl *)ioctl_buffer;
1086 tw_ioctl->cdb[0] = TW_IOCTL;
1087 tw_ioctl->opcode = TW_ATA_PASSTHRU;
1088 tw_ioctl->input_length = 512; // correct even for non-data commands
1089 tw_ioctl->output_length = 512; // correct even for non-data commands
1090 tw_output = (TW_Output *)tw_ioctl;
1091 passthru = (TW_Passthru *)&(tw_ioctl->input_data);
1092 }
1093 else {
1094 pout("Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1095 "Please contact " PACKAGE_BUGREPORT "\n", escalade_type, disknum);
1096 errno=ENOSYS;
1097 return -1;
1098 }
1099
1100 // Same for (almost) all commands - but some reset below
1101 passthru->byte0.opcode = TW_OP_ATA_PASSTHRU;
1102 passthru->request_id = 0xFF;
1103 passthru->unit = disknum;
1104 passthru->status = 0;
1105 passthru->flags = 0x1;
1106 passthru->drive_head = 0x0;
1107 passthru->sector_num = 0;
1108
1109 // All SMART commands use this CL/CH signature. These are magic
1110 // values from the ATA specifications.
1111 passthru->cylinder_lo = 0x4F;
1112 passthru->cylinder_hi = 0xC2;
1113
1114 // SMART ATA COMMAND REGISTER value
1115 passthru->command = ATA_SMART_CMD;
1116
1117 // Is this a command that reads or returns 512 bytes?
1118 // passthru->param values are:
1119 // 0x0 - non data command without TFR write check,
1120 // 0x8 - non data command with TFR write check,
1121 // 0xD - data command that returns data to host from device
1122 // 0xF - data command that writes data from host to device
1123 // passthru->size values are 0x5 for non-data and 0x07 for data
1124 if (command == READ_VALUES ||
1125 command == READ_THRESHOLDS ||
1126 command == READ_LOG ||
1127 command == IDENTIFY ||
1128 command == WRITE_LOG ) {
1129 readdata=1;
1130 passthru->byte0.sgloff = 0x5;
1131 passthru->size = 0x7;
1132 passthru->param = 0xD;
1133 passthru->sector_count = 0x1;
1134 // For 64-bit to work correctly, up the size of the command packet
1135 // in dwords by 1 to account for the 64-bit single sgl 'address'
1136 // field. Note that this doesn't agree with the typedefs but it's
1137 // right (agree with kernel driver behavior/typedefs).
1138 if (escalade_type==CONTROLLER_3WARE_9000_CHAR && sizeof(long)==8)
1139 passthru->size++;
1140 }
1141 else {
1142 // Non data command -- but doesn't use large sector
1143 // count register values.
1144 passthru->byte0.sgloff = 0x0;
1145 passthru->size = 0x5;
1146 passthru->param = 0x8;
1147 passthru->sector_count = 0x0;
1148 }
1149
1150 // Now set ATA registers depending upon command
1151 switch (command){
1152 case CHECK_POWER_MODE:
1153 passthru->command = ATA_CHECK_POWER_MODE;
1154 passthru->features = 0;
1155 passthru->cylinder_lo = 0;
1156 passthru->cylinder_hi = 0;
1157 break;
1158 case READ_VALUES:
1159 passthru->features = ATA_SMART_READ_VALUES;
1160 break;
1161 case READ_THRESHOLDS:
1162 passthru->features = ATA_SMART_READ_THRESHOLDS;
1163 break;
1164 case READ_LOG:
1165 passthru->features = ATA_SMART_READ_LOG_SECTOR;
1166 // log number to return
1167 passthru->sector_num = select;
1168 break;
1169 case WRITE_LOG:
1170 if (escalade_type == CONTROLLER_3WARE_9000_CHAR)
1171 memcpy((unsigned char *)tw_ioctl_apache->data_buffer, data, 512);
1172 else if (escalade_type == CONTROLLER_3WARE_678K_CHAR)
1173 memcpy((unsigned char *)tw_ioctl_char->data_buffer, data, 512);
1174 else {
1175 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1176 // memcpy(tw_output->output_data, data, 512);
1177 printwarning(command);
1178 errno=ENOTSUP;
1179 return -1;
1180 }
1181 readdata=0;
1182 passthru->features = ATA_SMART_WRITE_LOG_SECTOR;
1183 passthru->sector_count = 1;
1184 passthru->sector_num = select;
1185 passthru->param = 0xF; // PIO data write
1186 break;
1187 case IDENTIFY:
1188 // ATA IDENTIFY DEVICE
1189 passthru->command = ATA_IDENTIFY_DEVICE;
1190 passthru->features = 0;
1191 passthru->cylinder_lo = 0;
1192 passthru->cylinder_hi = 0;
1193 break;
1194 case PIDENTIFY:
1195 // 3WARE controller can NOT have packet device internally
1196 pout("WARNING - NO DEVICE FOUND ON 3WARE CONTROLLER (disk %d)\n", disknum);
1197 pout("Note: /dev/sdX many need to be replaced with /dev/tweN or /dev/twaN\n");
1198 errno=ENODEV;
1199 return -1;
1200 case ENABLE:
1201 passthru->features = ATA_SMART_ENABLE;
1202 break;
1203 case DISABLE:
1204 passthru->features = ATA_SMART_DISABLE;
1205 break;
1206 case AUTO_OFFLINE:
1207 passthru->features = ATA_SMART_AUTO_OFFLINE;
1208 // Enable or disable?
1209 passthru->sector_count = select;
1210 break;
1211 case AUTOSAVE:
1212 passthru->features = ATA_SMART_AUTOSAVE;
1213 // Enable or disable?
1214 passthru->sector_count = select;
1215 break;
1216 case IMMEDIATE_OFFLINE:
1217 passthru->features = ATA_SMART_IMMEDIATE_OFFLINE;
1218 // What test type to run?
1219 passthru->sector_num = select;
1220 break;
1221 case STATUS_CHECK:
1222 passthru->features = ATA_SMART_STATUS;
1223 break;
1224 case STATUS:
1225 // This is JUST to see if SMART is enabled, by giving SMART status
1226 // command. But it doesn't say if status was good, or failing.
1227 // See below for the difference.
1228 passthru->features = ATA_SMART_STATUS;
1229 break;
1230 default:
1231 pout("Unrecognized command %d in linux_3ware_command_interface(disk %d)\n"
1232 "Please contact " PACKAGE_BUGREPORT "\n", command, disknum);
1233 errno=ENOSYS;
1234 return -1;
1235 }
1236
1237 // Now send the command down through an ioctl()
1238 if (escalade_type==CONTROLLER_3WARE_9000_CHAR)
1239 ioctlreturn=ioctl(fd, TW_IOCTL_FIRMWARE_PASS_THROUGH, tw_ioctl_apache);
1240 else if (escalade_type==CONTROLLER_3WARE_678K_CHAR)
1241 ioctlreturn=ioctl(fd, TW_CMD_PACKET_WITH_DATA, tw_ioctl_char);
1242 else
1243 ioctlreturn=ioctl(fd, SCSI_IOCTL_SEND_COMMAND, tw_ioctl);
1244
1245 // Deal with the different error cases
1246 if (ioctlreturn) {
1247 if (CONTROLLER_3WARE_678K==escalade_type && ((command==AUTO_OFFLINE || command==AUTOSAVE) && select)){
1248 // error here is probably a kernel driver whose version is too old
1249 printwarning(command);
1250 errno=ENOTSUP;
1251 }
1252 if (!errno)
1253 errno=EIO;
1254 return -1;
1255 }
1256
1257 // The passthru structure is valid after return from an ioctl if:
1258 // - we are using the character interface OR
1259 // - we are using the SCSI interface and this is a NON-READ-DATA command
1260 // For SCSI interface, note that we set passthru to a different
1261 // value after ioctl().
1262 if (CONTROLLER_3WARE_678K==escalade_type) {
1263 if (readdata)
1264 passthru=NULL;
1265 else
1266 passthru=(TW_Passthru *)&(tw_output->output_data);
1267 }
1268
1269 // See if the ATA command failed. Now that we have returned from
1270 // the ioctl() call, if passthru is valid, then:
1271 // - passthru->status contains the 3ware controller STATUS
1272 // - passthru->command contains the ATA STATUS register
1273 // - passthru->features contains the ATA ERROR register
1274 //
1275 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1276 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1277 // While we *might* decode the ATA ERROR register, at the moment it
1278 // doesn't make much sense: we don't care in detail why the error
1279 // happened.
1280
1281 if (passthru && (passthru->status || (passthru->command & 0x21))) {
1282 errno=EIO;
1283 return -1;
1284 }
1285
1286 // If this is a read data command, copy data to output buffer
1287 if (readdata) {
1288 if (escalade_type==CONTROLLER_3WARE_9000_CHAR)
1289 memcpy(data, (unsigned char *)tw_ioctl_apache->data_buffer, 512);
1290 else if (escalade_type==CONTROLLER_3WARE_678K_CHAR)
1291 memcpy(data, (unsigned char *)tw_ioctl_char->data_buffer, 512);
1292 else
1293 memcpy(data, tw_output->output_data, 512);
1294 }
1295
1296 // For STATUS_CHECK, we need to check register values
1297 if (command==STATUS_CHECK) {
1298
1299 // To find out if the SMART RETURN STATUS is good or failing, we
1300 // need to examine the values of the Cylinder Low and Cylinder
1301 // High Registers.
1302
1303 unsigned short cyl_lo=passthru->cylinder_lo;
1304 unsigned short cyl_hi=passthru->cylinder_hi;
1305
1306 // If values in Cyl-LO and Cyl-HI are unchanged, SMART status is good.
1307 if (cyl_lo==0x4F && cyl_hi==0xC2)
1308 return 0;
1309
1310 // If values in Cyl-LO and Cyl-HI are as follows, SMART status is FAIL
1311 if (cyl_lo==0xF4 && cyl_hi==0x2C)
1312 return 1;
1313
1314 // Any other values mean that something has gone wrong with the command
1315 if (CONTROLLER_3WARE_678K==escalade_type) {
1316 printwarning(command);
1317 errno=ENOSYS;
1318 return 0;
1319 }
1320 else {
1321 errno=EIO;
1322 return -1;
1323 }
1324 }
1325
1326 // copy sector count register (one byte!) to return data
1327 if (command==CHECK_POWER_MODE)
1328 *data=*(char *)&(passthru->sector_count);
1329
1330 // look for nonexistent devices/ports
1331 if (command==IDENTIFY && !nonempty((unsigned char *)data, 512)) {
1332 errno=ENODEV;
1333 return -1;
1334 }
1335
1336 return 0;
1337 }
1338
1339
1340
1341 int marvell_command_interface(int device,
1342 smart_command_set command,
1343 int select,
1344 char *data) {
1345 typedef struct {
1346 int inlen;
1347 int outlen;
1348 char cmd[540];
1349 } mvsata_scsi_cmd;
1350
1351 int copydata = 0;
1352 mvsata_scsi_cmd smart_command;
1353 unsigned char *buff = (unsigned char *)&smart_command.cmd[6];
1354 // See struct hd_drive_cmd_hdr in hdreg.h
1355 // buff[0]: ATA COMMAND CODE REGISTER
1356 // buff[1]: ATA SECTOR NUMBER REGISTER
1357 // buff[2]: ATA FEATURES REGISTER
1358 // buff[3]: ATA SECTOR COUNT REGISTER
1359
1360 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1361 memset(&smart_command, 0, sizeof(smart_command));
1362 smart_command.inlen = 540;
1363 smart_command.outlen = 540;
1364 smart_command.cmd[0] = 0xC; //Vendor-specific code
1365 smart_command.cmd[4] = 6; //command length
1366
1367 buff[0] = ATA_SMART_CMD;
1368 switch (command){
1369 case CHECK_POWER_MODE:
1370 buff[0]=ATA_CHECK_POWER_MODE;
1371 break;
1372 case READ_VALUES:
1373 buff[2]=ATA_SMART_READ_VALUES;
1374 copydata=buff[3]=1;
1375 break;
1376 case READ_THRESHOLDS:
1377 buff[2]=ATA_SMART_READ_THRESHOLDS;
1378 copydata=buff[1]=buff[3]=1;
1379 break;
1380 case READ_LOG:
1381 buff[2]=ATA_SMART_READ_LOG_SECTOR;
1382 buff[1]=select;
1383 copydata=buff[3]=1;
1384 break;
1385 case IDENTIFY:
1386 buff[0]=ATA_IDENTIFY_DEVICE;
1387 copydata=buff[3]=1;
1388 break;
1389 case PIDENTIFY:
1390 buff[0]=ATA_IDENTIFY_PACKET_DEVICE;
1391 copydata=buff[3]=1;
1392 break;
1393 case ENABLE:
1394 buff[2]=ATA_SMART_ENABLE;
1395 buff[1]=1;
1396 break;
1397 case DISABLE:
1398 buff[2]=ATA_SMART_DISABLE;
1399 buff[1]=1;
1400 break;
1401 case STATUS:
1402 case STATUS_CHECK:
1403 // this command only says if SMART is working. It could be
1404 // replaced with STATUS_CHECK below.
1405 buff[2] = ATA_SMART_STATUS;
1406 break;
1407 case AUTO_OFFLINE:
1408 buff[2]=ATA_SMART_AUTO_OFFLINE;
1409 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1410 break;
1411 case AUTOSAVE:
1412 buff[2]=ATA_SMART_AUTOSAVE;
1413 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1414 break;
1415 case IMMEDIATE_OFFLINE:
1416 buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
1417 buff[1]=select;
1418 break;
1419 default:
1420 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command);
1421 exit(1);
1422 break;
1423 }
1424 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1425 // one is this:
1426 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1427 if (ioctl(device, SCSI_IOCTL_SEND_COMMAND, (void *)&smart_command))
1428 return -1;
1429
1430 if (command==CHECK_POWER_MODE) {
1431 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1432 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1433 data[0]=buff[3];
1434 return 0;
1435 }
1436
1437 // Always succeed on a SMART status, as a disk that failed returned
1438 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1439 if (command == STATUS)
1440 return 0;
1441 //Data returned is starting from 0 offset
1442 if (command == STATUS_CHECK)
1443 {
1444 // Cyl low and Cyl high unchanged means "Good SMART status"
1445 if (buff[4] == 0x4F && buff[5] == 0xC2)
1446 return 0;
1447 // These values mean "Bad SMART status"
1448 if (buff[4] == 0xF4 && buff[5] == 0x2C)
1449 return 1;
1450 // We haven't gotten output that makes sense; print out some debugging info
1451 syserror("Error SMART Status command failed");
1452 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT);
1453 pout("Register values returned from SMART Status command are:\n");
1454 pout("CMD =0x%02x\n",(int)buff[0]);
1455 pout("FR =0x%02x\n",(int)buff[1]);
1456 pout("NS =0x%02x\n",(int)buff[2]);
1457 pout("SC =0x%02x\n",(int)buff[3]);
1458 pout("CL =0x%02x\n",(int)buff[4]);
1459 pout("CH =0x%02x\n",(int)buff[5]);
1460 pout("SEL=0x%02x\n",(int)buff[6]);
1461 return -1;
1462 }
1463
1464 if (copydata)
1465 memcpy(data, buff, 512);
1466 return 0;
1467 }
1468
1469 // this implementation is derived from ata_command_interface with a header
1470 // packing for highpoint linux driver ioctl interface
1471 //
1472 // ioctl(fd,HPTIO_CTL,buff)
1473 // ^^^^^^^^^
1474 //
1475 // structure of hpt_buff
1476 // +----+----+----+----+--------------------.....---------------------+
1477 // | 1 | 2 | 3 | 4 | 5 |
1478 // +----+----+----+----+--------------------.....---------------------+
1479 //
1480 // 1: The target controller [ int ( 4 Bytes ) ]
1481 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
1482 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
1483 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
1484 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
1485 // if no pmport device, set to 1 or leave blank
1486 // 5: data [ void * ( var leangth ) ]
1487 //
1488 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
1489
1490 int highpoint_command_interface(int device, smart_command_set command,
1491 int select, char *data)
1492
1493
1494 {
1495 unsigned char hpt_buff[4*sizeof(int) + STRANGE_BUFFER_LENGTH];
1496 unsigned int *hpt = (unsigned int *)hpt_buff;
1497 unsigned char *buff = &hpt_buff[4*sizeof(int)];
1498 int copydata = 0;
1499 const int HDIO_DRIVE_CMD_OFFSET = 4;
1500
1501 memset(hpt_buff, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH);
1502 hpt[0] = con->hpt_data[0]; // controller id
1503 hpt[1] = con->hpt_data[1]; // channel number
1504 hpt[3] = con->hpt_data[2]; // pmport number
1505
1506 buff[0]=ATA_SMART_CMD;
1507 switch (command){
1508 case CHECK_POWER_MODE:
1509 buff[0]=ATA_CHECK_POWER_MODE;
1510 copydata=1;
1511 break;
1512 case READ_VALUES:
1513 buff[2]=ATA_SMART_READ_VALUES;
1514 buff[3]=1;
1515 copydata=512;
1516 break;
1517 case READ_THRESHOLDS:
1518 buff[2]=ATA_SMART_READ_THRESHOLDS;
1519 buff[1]=buff[3]=1;
1520 copydata=512;
1521 break;
1522 case READ_LOG:
1523 buff[2]=ATA_SMART_READ_LOG_SECTOR;
1524 buff[1]=select;
1525 buff[3]=1;
1526 copydata=512;
1527 break;
1528 case WRITE_LOG:
1529 break;
1530 case IDENTIFY:
1531 buff[0]=ATA_IDENTIFY_DEVICE;
1532 buff[3]=1;
1533 copydata=512;
1534 break;
1535 case PIDENTIFY:
1536 buff[0]=ATA_IDENTIFY_PACKET_DEVICE;
1537 buff[3]=1;
1538 copydata=512;
1539 break;
1540 case ENABLE:
1541 buff[2]=ATA_SMART_ENABLE;
1542 buff[1]=1;
1543 break;
1544 case DISABLE:
1545 buff[2]=ATA_SMART_DISABLE;
1546 buff[1]=1;
1547 break;
1548 case STATUS:
1549 buff[2]=ATA_SMART_STATUS;
1550 break;
1551 case AUTO_OFFLINE:
1552 buff[2]=ATA_SMART_AUTO_OFFLINE;
1553 buff[3]=select;
1554 break;
1555 case AUTOSAVE:
1556 buff[2]=ATA_SMART_AUTOSAVE;
1557 buff[3]=select;
1558 break;
1559 case IMMEDIATE_OFFLINE:
1560 buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
1561 buff[1]=select;
1562 break;
1563 case STATUS_CHECK:
1564 buff[1]=ATA_SMART_STATUS;
1565 break;
1566 default:
1567 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
1568 "Please contact " PACKAGE_BUGREPORT "\n", command);
1569 errno=ENOSYS;
1570 return -1;
1571 }
1572
1573 if (command==WRITE_LOG) {
1574 unsigned char task[4*sizeof(int)+sizeof(ide_task_request_t)+512];
1575 unsigned int *hpt = (unsigned int *)task;
1576 ide_task_request_t *reqtask = (ide_task_request_t *)(&task[4*sizeof(int)]);
1577 task_struct_t *taskfile = (task_struct_t *)reqtask->io_ports;
1578 int retval;
1579
1580 memset(task, 0, sizeof(task));
1581
1582 hpt[0] = con->hpt_data[0]; // controller id
1583 hpt[1] = con->hpt_data[1]; // channel number
1584 hpt[3] = con->hpt_data[2]; // pmport number
1585 hpt[2] = HDIO_DRIVE_TASKFILE; // real hd ioctl
1586
1587 taskfile->data = 0;
1588 taskfile->feature = ATA_SMART_WRITE_LOG_SECTOR;
1589 taskfile->sector_count = 1;
1590 taskfile->sector_number = select;
1591 taskfile->low_cylinder = 0x4f;
1592 taskfile->high_cylinder = 0xc2;
1593 taskfile->device_head = 0;
1594 taskfile->command = ATA_SMART_CMD;
1595
1596 reqtask->data_phase = TASKFILE_OUT;
1597 reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
1598 reqtask->out_size = 512;
1599 reqtask->in_size = 0;
1600
1601 memcpy(task+sizeof(ide_task_request_t)+4*sizeof(int), data, 512);
1602
1603 if ((retval=ioctl(device, HPTIO_CTL, task))) {
1604 if (retval==-EINVAL)
1605 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
1606 return -1;
1607 }
1608 return 0;
1609 }
1610
1611 if (command==STATUS_CHECK){
1612 int retval;
1613 unsigned const char normal_lo=0x4f, normal_hi=0xc2;
1614 unsigned const char failed_lo=0xf4, failed_hi=0x2c;
1615 buff[4]=normal_lo;
1616 buff[5]=normal_hi;
1617
1618 hpt[2] = HDIO_DRIVE_TASK;
1619
1620 if ((retval=ioctl(device, HPTIO_CTL, hpt_buff))) {
1621 if (retval==-EINVAL) {
1622 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
1623 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
1624 }
1625 else
1626 syserror("Error SMART Status command failed");
1627 return -1;
1628 }
1629
1630 if (buff[4]==normal_lo && buff[5]==normal_hi)
1631 return 0;
1632
1633 if (buff[4]==failed_lo && buff[5]==failed_hi)
1634 return 1;
1635
1636 syserror("Error SMART Status command failed");
1637 pout("Please get assistance from " PACKAGE_HOMEPAGE "\n");
1638 pout("Register values returned from SMART Status command are:\n");
1639 pout("CMD=0x%02x\n",(int)buff[0]);
1640 pout("FR =0x%02x\n",(int)buff[1]);
1641 pout("NS =0x%02x\n",(int)buff[2]);
1642 pout("SC =0x%02x\n",(int)buff[3]);
1643 pout("CL =0x%02x\n",(int)buff[4]);
1644 pout("CH =0x%02x\n",(int)buff[5]);
1645 pout("SEL=0x%02x\n",(int)buff[6]);
1646 return -1;
1647 }
1648
1649 #if 1
1650 if (command==IDENTIFY || command==PIDENTIFY) {
1651 unsigned char deviceid[4*sizeof(int)+512*sizeof(char)];
1652 unsigned int *hpt = (unsigned int *)deviceid;
1653
1654 hpt[0] = con->hpt_data[0]; // controller id
1655 hpt[1] = con->hpt_data[1]; // channel number
1656 hpt[3] = con->hpt_data[2]; // pmport number
1657
1658 hpt[2] = HDIO_GET_IDENTITY;
1659 if (!ioctl(device, HPTIO_CTL, deviceid) && (deviceid[4*sizeof(int)] & 0x8000))
1660 buff[0]=(command==IDENTIFY)?ATA_IDENTIFY_PACKET_DEVICE:ATA_IDENTIFY_DEVICE;
1661 }
1662 #endif
1663
1664 hpt[2] = HDIO_DRIVE_CMD;
1665 if ((ioctl(device, HPTIO_CTL, hpt_buff)))
1666 return -1;
1667
1668 if (command==CHECK_POWER_MODE)
1669 buff[HDIO_DRIVE_CMD_OFFSET]=buff[2];
1670
1671 if (copydata)
1672 memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
1673
1674 return 0;
1675 }
1676
1677
1678 // Utility function for printing warnings
1679 void printwarning(smart_command_set command){
1680 static int printed[4]={0,0,0,0};
1681 const char* message=
1682 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
1683 "applying a simple 3w-xxxx driver patch that can be found here:\n"
1684 PACKAGE_HOMEPAGE "\n"
1685 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
1686
1687 if (command==AUTO_OFFLINE && !printed[0]) {
1688 printed[0]=1;
1689 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message);
1690 }
1691 else if (command==AUTOSAVE && !printed[1]) {
1692 printed[1]=1;
1693 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message);
1694 }
1695 else if (command==STATUS_CHECK && !printed[2]) {
1696 printed[2]=1;
1697 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message);
1698 }
1699 else if (command==WRITE_LOG && !printed[3]) {
1700 printed[3]=1;
1701 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
1702 }
1703
1704 return;
1705 }
1706
1707 // Guess device type (ata or scsi) based on device name (Linux
1708 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
1709 // osst, nosst and sg.
1710 static const char * lin_dev_prefix = "/dev/";
1711 static const char * lin_dev_ata_disk_plus = "h";
1712 static const char * lin_dev_ata_devfs_disk_plus = "ide/";
1713 static const char * lin_dev_scsi_devfs_disk_plus = "scsi/";
1714 static const char * lin_dev_scsi_disk_plus = "s";
1715 static const char * lin_dev_scsi_tape1 = "ns";
1716 static const char * lin_dev_scsi_tape2 = "os";
1717 static const char * lin_dev_scsi_tape3 = "nos";
1718 static const char * lin_dev_3ware_9000_char = "twa";
1719 static const char * lin_dev_3ware_678k_char = "twe";
1720 static const char * lin_dev_cciss_dir = "cciss/";
1721
1722 int guess_device_type(const char * dev_name) {
1723 int len;
1724 int dev_prefix_len = strlen(lin_dev_prefix);
1725
1726 // if dev_name null, or string length zero
1727 if (!dev_name || !(len = strlen(dev_name)))
1728 return CONTROLLER_UNKNOWN;
1729
1730 // Remove the leading /dev/... if it's there
1731 if (!strncmp(lin_dev_prefix, dev_name, dev_prefix_len)) {
1732 if (len <= dev_prefix_len)
1733 // if nothing else in the string, unrecognized
1734 return CONTROLLER_UNKNOWN;
1735 // else advance pointer to following characters
1736 dev_name += dev_prefix_len;
1737 }
1738
1739 // form /dev/h* or h*
1740 if (!strncmp(lin_dev_ata_disk_plus, dev_name,
1741 strlen(lin_dev_ata_disk_plus)))
1742 return CONTROLLER_ATA;
1743
1744 // form /dev/ide/* or ide/*
1745 if (!strncmp(lin_dev_ata_devfs_disk_plus, dev_name,
1746 strlen(lin_dev_ata_devfs_disk_plus)))
1747 return CONTROLLER_ATA;
1748
1749 // form /dev/s* or s*
1750 if (!strncmp(lin_dev_scsi_disk_plus, dev_name,
1751 strlen(lin_dev_scsi_disk_plus)))
1752 return CONTROLLER_SCSI;
1753
1754 // form /dev/scsi/* or scsi/*
1755 if (!strncmp(lin_dev_scsi_devfs_disk_plus, dev_name,
1756 strlen(lin_dev_scsi_devfs_disk_plus)))
1757 return CONTROLLER_SCSI;
1758
1759 // form /dev/ns* or ns*
1760 if (!strncmp(lin_dev_scsi_tape1, dev_name,
1761 strlen(lin_dev_scsi_tape1)))
1762 return CONTROLLER_SCSI;
1763
1764 // form /dev/os* or os*
1765 if (!strncmp(lin_dev_scsi_tape2, dev_name,
1766 strlen(lin_dev_scsi_tape2)))
1767 return CONTROLLER_SCSI;
1768
1769 // form /dev/nos* or nos*
1770 if (!strncmp(lin_dev_scsi_tape3, dev_name,
1771 strlen(lin_dev_scsi_tape3)))
1772 return CONTROLLER_SCSI;
1773
1774 // form /dev/twa*
1775 if (!strncmp(lin_dev_3ware_9000_char, dev_name,
1776 strlen(lin_dev_3ware_9000_char)))
1777 return CONTROLLER_3WARE_9000_CHAR;
1778
1779 // form /dev/twe*
1780 if (!strncmp(lin_dev_3ware_678k_char, dev_name,
1781 strlen(lin_dev_3ware_678k_char)))
1782 return CONTROLLER_3WARE_678K_CHAR;
1783 // form /dev/cciss*
1784 if (!strncmp(lin_dev_cciss_dir, dev_name,
1785 strlen(lin_dev_cciss_dir)))
1786 return CONTROLLER_CCISS;
1787
1788 // we failed to recognize any of the forms
1789 return CONTROLLER_UNKNOWN;
1790 }
1791
1792
1793 #if 0
1794
1795 [ed@firestorm ed]$ ls -l /dev/discs
1796 total 0
1797 lr-xr-xr-x 1 root root 30 Dec 31 1969 disc0 -> ../ide/host2/bus0/target0/lun0/
1798 lr-xr-xr-x 1 root root 30 Dec 31 1969 disc1 -> ../ide/host2/bus1/target0/lun0/
1799 [ed@firestorm ed]$ ls -l dev/ide/host*/bus*/target*/lun*/disc
1800 ls: dev/ide/host*/bus*/target*/lun*/disc: No such file or directory
1801 [ed@firestorm ed]$ ls -l /dev/ide/host*/bus*/target*/lun*/disc
1802 brw------- 1 root root 33, 0 Dec 31 1969 /dev/ide/host2/bus0/target0/lun0/disc
1803 brw------- 1 root root 34, 0 Dec 31 1969 /dev/ide/host2/bus1/target0/lun0/disc
1804 [ed@firestorm ed]$ ls -l /dev/ide/c*b*t*u*
1805 ls: /dev/ide/c*b*t*u*: No such file or directory
1806 [ed@firestorm ed]$
1807 Script done on Fri Nov 7 13:46:28 2003
1808
1809 #endif