]> 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-7 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2003-7 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.98 2008/02/17 00:30:47 dpgilbert Exp $";
83
84 const char *os_XXXX_c_cvsid="$Id: os_linux.cpp,v 1.98 2008/02/17 00:30:47 dpgilbert 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 // NSECT is 241 for enable but no data transfer. Use TASK ioctl.
488 buff[1]=ATA_SMART_AUTO_OFFLINE;
489 buff[2]=select;
490 break;
491 case AUTOSAVE:
492 // NSECT is 248 for enable but no data transfer. Use TASK ioctl.
493 buff[1]=ATA_SMART_AUTOSAVE;
494 buff[2]=select;
495 break;
496 case IMMEDIATE_OFFLINE:
497 buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
498 buff[1]=select;
499 break;
500 case STATUS_CHECK:
501 // This command uses HDIO_DRIVE_TASK and has different syntax than
502 // the other commands.
503 buff[1]=ATA_SMART_STATUS;
504 break;
505 default:
506 pout("Unrecognized command %d in linux_ata_command_interface()\n"
507 "Please contact " PACKAGE_BUGREPORT "\n", command);
508 errno=ENOSYS;
509 return -1;
510 }
511
512 // This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
513 // only ioctl() that can be used to WRITE data to the disk.
514 if (command==WRITE_LOG) {
515 unsigned char task[sizeof(ide_task_request_t)+512];
516 ide_task_request_t *reqtask=(ide_task_request_t *) task;
517 task_struct_t *taskfile=(task_struct_t *) reqtask->io_ports;
518 int retval;
519
520 memset(task, 0, sizeof(task));
521
522 taskfile->data = 0;
523 taskfile->feature = ATA_SMART_WRITE_LOG_SECTOR;
524 taskfile->sector_count = 1;
525 taskfile->sector_number = select;
526 taskfile->low_cylinder = 0x4f;
527 taskfile->high_cylinder = 0xc2;
528 taskfile->device_head = 0;
529 taskfile->command = ATA_SMART_CMD;
530
531 reqtask->data_phase = TASKFILE_OUT;
532 reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
533 reqtask->out_size = 512;
534 reqtask->in_size = 0;
535
536 // copy user data into the task request structure
537 memcpy(task+sizeof(ide_task_request_t), data, 512);
538
539 if ((retval=ioctl(device, HDIO_DRIVE_TASKFILE, task))) {
540 if (retval==-EINVAL)
541 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
542 return -1;
543 }
544 return 0;
545 }
546
547 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
548 // one is this:
549 if (command==STATUS_CHECK || command==AUTOSAVE || command==AUTO_OFFLINE){
550 int retval;
551
552 // NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
553 // have to read the IDE driver source code. Sigh.
554 // buff[0]: ATA COMMAND CODE REGISTER
555 // buff[1]: ATA FEATURES REGISTER
556 // buff[2]: ATA SECTOR_COUNT
557 // buff[3]: ATA SECTOR NUMBER
558 // buff[4]: ATA CYL LO REGISTER
559 // buff[5]: ATA CYL HI REGISTER
560 // buff[6]: ATA DEVICE HEAD
561
562 unsigned const char normal_lo=0x4f, normal_hi=0xc2;
563 unsigned const char failed_lo=0xf4, failed_hi=0x2c;
564 buff[4]=normal_lo;
565 buff[5]=normal_hi;
566
567 if ((retval=ioctl(device, HDIO_DRIVE_TASK, buff))) {
568 if (retval==-EINVAL) {
569 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
570 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
571 }
572 else
573 syserror("Error SMART Status command failed");
574 return -1;
575 }
576
577 // Cyl low and Cyl high unchanged means "Good SMART status"
578 if (buff[4]==normal_lo && buff[5]==normal_hi)
579 return 0;
580
581 // These values mean "Bad SMART status"
582 if (buff[4]==failed_lo && buff[5]==failed_hi)
583 return 1;
584
585 // We haven't gotten output that makes sense; print out some debugging info
586 syserror("Error SMART Status command failed");
587 pout("Please get assistance from " PACKAGE_HOMEPAGE "\n");
588 pout("Register values returned from SMART Status command are:\n");
589 pout("ST =0x%02x\n",(int)buff[0]);
590 pout("ERR=0x%02x\n",(int)buff[1]);
591 pout("NS =0x%02x\n",(int)buff[2]);
592 pout("SC =0x%02x\n",(int)buff[3]);
593 pout("CL =0x%02x\n",(int)buff[4]);
594 pout("CH =0x%02x\n",(int)buff[5]);
595 pout("SEL=0x%02x\n",(int)buff[6]);
596 return -1;
597 }
598
599 #if 1
600 // Note to people doing ports to other OSes -- don't worry about
601 // this block -- you can safely ignore it. I have put it here
602 // because under linux when you do IDENTIFY DEVICE to a packet
603 // device, it generates an ugly kernel syslog error message. This
604 // is harmless but frightens users. So this block detects packet
605 // devices and make IDENTIFY DEVICE fail "nicely" without a syslog
606 // error message.
607 //
608 // If you read only the ATA specs, it appears as if a packet device
609 // *might* respond to the IDENTIFY DEVICE command. This is
610 // misleading - it's because around the time that SFF-8020 was
611 // incorporated into the ATA-3/4 standard, the ATA authors were
612 // sloppy. See SFF-8020 and you will see that ATAPI devices have
613 // *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
614 // command set, and return 'Command Aborted' to IDENTIFY DEVICE.
615 if (command==IDENTIFY || command==PIDENTIFY){
616 unsigned short deviceid[256];
617 // check the device identity, as seen when the system was booted
618 // or the device was FIRST registered. This will not be current
619 // if the user has subsequently changed some of the parameters. If
620 // device is a packet device, swap the command interpretations.
621 if (!ioctl(device, HDIO_GET_IDENTITY, deviceid) && (deviceid[0] & 0x8000))
622 buff[0]=(command==IDENTIFY)?ATA_IDENTIFY_PACKET_DEVICE:ATA_IDENTIFY_DEVICE;
623 }
624 #endif
625
626 // We are now doing the HDIO_DRIVE_CMD type ioctl.
627 if ((ioctl(device, HDIO_DRIVE_CMD, buff)))
628 return -1;
629
630 // CHECK POWER MODE command returns information in the Sector Count
631 // register (buff[3]). Copy to return data buffer.
632 if (command==CHECK_POWER_MODE)
633 buff[HDIO_DRIVE_CMD_OFFSET]=buff[2];
634
635 // if the command returns data then copy it back
636 if (copydata)
637 memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
638
639 return 0;
640 }
641
642 // >>>>>> Start of general SCSI specific linux code
643
644 /* Linux specific code.
645 * Historically smartmontools (and smartsuite before it) used the
646 * SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
647 * nodes that use the SCSI subsystem. A better interface has been available
648 * via the SCSI generic (sg) driver but this involves the extra step of
649 * mapping disk devices (e.g. /dev/sda) to the corresponding sg device
650 * (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
651 * the sg driver have become available via the SG_IO ioctl which is available
652 * on all SCSI devices (on SCSI tape devices from lk 2.6.6).
653 * So the strategy below is to find out if the SG_IO ioctl is available and
654 * if so use it; failing that use the older SCSI_IOCTL_SEND_COMMAND ioctl.
655 * Should work in 2.0, 2.2, 2.4 and 2.6 series linux kernels. */
656
657 #define MAX_DXFER_LEN 1024 /* can be increased if necessary */
658 #define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
659 #define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
660 #define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
661 #define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
662 #define LSCSI_DRIVER_TIMEOUT 0x6
663 #define LSCSI_DID_TIME_OUT 0x3
664 #define LSCSI_DID_BUS_BUSY 0x2
665 #define LSCSI_DID_NO_CONNECT 0x1
666
667 #ifndef SCSI_IOCTL_SEND_COMMAND
668 #define SCSI_IOCTL_SEND_COMMAND 1
669 #endif
670
671 #define SG_IO_PRESENT_UNKNOWN 0
672 #define SG_IO_PRESENT_YES 1
673 #define SG_IO_PRESENT_NO 2
674
675 static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report,
676 int unknown);
677 static int sisc_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report);
678
679 static int sg_io_state = SG_IO_PRESENT_UNKNOWN;
680
681 /* Preferred implementation for issuing SCSI commands in linux. This
682 * function uses the SG_IO ioctl. Return 0 if command issued successfully
683 * (various status values should still be checked). If the SCSI command
684 * cannot be issued then a negative errno value is returned. */
685 static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report,
686 int unknown)
687 {
688 #ifndef SG_IO
689 ARGUSED(dev_fd); ARGUSED(iop); ARGUSED(report);
690 return -ENOTTY;
691 #else
692 struct sg_io_hdr io_hdr;
693
694 if (report > 0) {
695 int k, j;
696 const unsigned char * ucp = iop->cmnd;
697 const char * np;
698 char buff[256];
699 const int sz = (int)sizeof(buff);
700
701 np = scsi_get_opcode_name(ucp[0]);
702 j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
703 for (k = 0; k < (int)iop->cmnd_len; ++k)
704 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
705 if ((report > 1) &&
706 (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
707 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
708
709 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
710 "data, len=%d%s:\n", (int)iop->dxfer_len,
711 (trunc ? " [only first 256 bytes shown]" : ""));
712 dStrHex((const char *)iop->dxferp,
713 (trunc ? 256 : iop->dxfer_len) , 1);
714 }
715 else
716 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
717 pout(buff);
718 }
719 memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
720 io_hdr.interface_id = 'S';
721 io_hdr.cmd_len = iop->cmnd_len;
722 io_hdr.mx_sb_len = iop->max_sense_len;
723 io_hdr.dxfer_len = iop->dxfer_len;
724 io_hdr.dxferp = iop->dxferp;
725 io_hdr.cmdp = iop->cmnd;
726 io_hdr.sbp = iop->sensep;
727 /* sg_io_hdr interface timeout has millisecond units. Timeout of 0
728 defaults to 60 seconds. */
729 io_hdr.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000;
730 switch (iop->dxfer_dir) {
731 case DXFER_NONE:
732 io_hdr.dxfer_direction = SG_DXFER_NONE;
733 break;
734 case DXFER_FROM_DEVICE:
735 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
736 break;
737 case DXFER_TO_DEVICE:
738 io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
739 break;
740 default:
741 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
742 return -EINVAL;
743 }
744 iop->resp_sense_len = 0;
745 iop->scsi_status = 0;
746 iop->resid = 0;
747 if (ioctl(dev_fd, SG_IO, &io_hdr) < 0) {
748 if (report && (! unknown))
749 pout(" SG_IO ioctl failed, errno=%d [%s]\n", errno,
750 strerror(errno));
751 return -errno;
752 }
753 iop->resid = io_hdr.resid;
754 iop->scsi_status = io_hdr.status;
755 if (report > 0) {
756 pout(" scsi_status=0x%x, host_status=0x%x, driver_status=0x%x\n"
757 " info=0x%x duration=%d milliseconds resid=%d\n", io_hdr.status,
758 io_hdr.host_status, io_hdr.driver_status, io_hdr.info,
759 io_hdr.duration, io_hdr.resid);
760 if (report > 1) {
761 if (DXFER_FROM_DEVICE == iop->dxfer_dir) {
762 int trunc, len;
763
764 len = iop->dxfer_len - iop->resid;
765 trunc = (len > 256) ? 1 : 0;
766 if (len > 0) {
767 pout(" Incoming data, len=%d%s:\n", len,
768 (trunc ? " [only first 256 bytes shown]" : ""));
769 dStrHex((const char*)iop->dxferp, (trunc ? 256 : len),
770 1);
771 } else
772 pout(" Incoming data trimmed to nothing by resid\n");
773 }
774 }
775 }
776
777 if (io_hdr.info | SG_INFO_CHECK) { /* error or warning */
778 int masked_driver_status = (LSCSI_DRIVER_MASK & io_hdr.driver_status);
779
780 if (0 != io_hdr.host_status) {
781 if ((LSCSI_DID_NO_CONNECT == io_hdr.host_status) ||
782 (LSCSI_DID_BUS_BUSY == io_hdr.host_status) ||
783 (LSCSI_DID_TIME_OUT == io_hdr.host_status))
784 return -ETIMEDOUT;
785 else
786 return -EIO; /* catch all */
787 }
788 if (0 != masked_driver_status) {
789 if (LSCSI_DRIVER_TIMEOUT == masked_driver_status)
790 return -ETIMEDOUT;
791 else if (LSCSI_DRIVER_SENSE != masked_driver_status)
792 return -EIO;
793 }
794 if (LSCSI_DRIVER_SENSE == masked_driver_status)
795 iop->scsi_status = SCSI_STATUS_CHECK_CONDITION;
796 iop->resp_sense_len = io_hdr.sb_len_wr;
797 if ((SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) &&
798 iop->sensep && (iop->resp_sense_len > 0)) {
799 if (report > 1) {
800 pout(" >>> Sense buffer, len=%d:\n",
801 (int)iop->resp_sense_len);
802 dStrHex((const char *)iop->sensep, iop->resp_sense_len , 1);
803 }
804 }
805 if (report) {
806 if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) {
807 if ((iop->sensep[0] & 0x7f) > 0x71)
808 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
809 iop->scsi_status, iop->sensep[1] & 0xf,
810 iop->sensep[2], iop->sensep[3]);
811 else
812 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
813 iop->scsi_status, iop->sensep[2] & 0xf,
814 iop->sensep[12], iop->sensep[13]);
815 }
816 else
817 pout(" status=0x%x\n", iop->scsi_status);
818 }
819 }
820 return 0;
821 #endif
822 }
823
824 struct linux_ioctl_send_command
825 {
826 int inbufsize;
827 int outbufsize;
828 UINT8 buff[MAX_DXFER_LEN + 16];
829 };
830
831 /* The Linux SCSI_IOCTL_SEND_COMMAND ioctl is primitive and it doesn't
832 * support: CDB length (guesses it from opcode), resid and timeout.
833 * Patches in Linux 2.4.21 and 2.5.70 to extend SEND DIAGNOSTIC timeout
834 * to 2 hours in order to allow long foreground extended self tests. */
835 static int sisc_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report)
836 {
837 struct linux_ioctl_send_command wrk;
838 int status, buff_offset;
839 size_t len;
840
841 memcpy(wrk.buff, iop->cmnd, iop->cmnd_len);
842 buff_offset = iop->cmnd_len;
843 if (report > 0) {
844 int k, j;
845 const unsigned char * ucp = iop->cmnd;
846 const char * np;
847 char buff[256];
848 const int sz = (int)sizeof(buff);
849
850 np = scsi_get_opcode_name(ucp[0]);
851 j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
852 for (k = 0; k < (int)iop->cmnd_len; ++k)
853 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
854 if ((report > 1) &&
855 (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
856 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
857
858 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
859 "data, len=%d%s:\n", (int)iop->dxfer_len,
860 (trunc ? " [only first 256 bytes shown]" : ""));
861 dStrHex((const char *)iop->dxferp,
862 (trunc ? 256 : iop->dxfer_len) , 1);
863 }
864 else
865 j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
866 pout(buff);
867 }
868 switch (iop->dxfer_dir) {
869 case DXFER_NONE:
870 wrk.inbufsize = 0;
871 wrk.outbufsize = 0;
872 break;
873 case DXFER_FROM_DEVICE:
874 wrk.inbufsize = 0;
875 if (iop->dxfer_len > MAX_DXFER_LEN)
876 return -EINVAL;
877 wrk.outbufsize = iop->dxfer_len;
878 break;
879 case DXFER_TO_DEVICE:
880 if (iop->dxfer_len > MAX_DXFER_LEN)
881 return -EINVAL;
882 memcpy(wrk.buff + buff_offset, iop->dxferp, iop->dxfer_len);
883 wrk.inbufsize = iop->dxfer_len;
884 wrk.outbufsize = 0;
885 break;
886 default:
887 pout("do_scsi_cmnd_io: bad dxfer_dir\n");
888 return -EINVAL;
889 }
890 iop->resp_sense_len = 0;
891 iop->scsi_status = 0;
892 iop->resid = 0;
893 status = ioctl(dev_fd, SCSI_IOCTL_SEND_COMMAND, &wrk);
894 if (-1 == status) {
895 if (report)
896 pout(" SCSI_IOCTL_SEND_COMMAND ioctl failed, errno=%d [%s]\n",
897 errno, strerror(errno));
898 return -errno;
899 }
900 if (0 == status) {
901 if (report > 0)
902 pout(" status=0\n");
903 if (DXFER_FROM_DEVICE == iop->dxfer_dir) {
904 memcpy(iop->dxferp, wrk.buff, iop->dxfer_len);
905 if (report > 1) {
906 int trunc = (iop->dxfer_len > 256) ? 1 : 0;
907
908 pout(" Incoming data, len=%d%s:\n", (int)iop->dxfer_len,
909 (trunc ? " [only first 256 bytes shown]" : ""));
910 dStrHex((const char*)iop->dxferp,
911 (trunc ? 256 : iop->dxfer_len) , 1);
912 }
913 }
914 return 0;
915 }
916 iop->scsi_status = status & 0x7e; /* bits 0 and 7 used to be for vendors */
917 if (LSCSI_DRIVER_SENSE == ((status >> 24) & 0xf))
918 iop->scsi_status = SCSI_STATUS_CHECK_CONDITION;
919 len = (SEND_IOCTL_RESP_SENSE_LEN < iop->max_sense_len) ?
920 SEND_IOCTL_RESP_SENSE_LEN : iop->max_sense_len;
921 if ((SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) &&
922 iop->sensep && (len > 0)) {
923 memcpy(iop->sensep, wrk.buff, len);
924 iop->resp_sense_len = len;
925 if (report > 1) {
926 pout(" >>> Sense buffer, len=%d:\n", (int)len);
927 dStrHex((const char *)wrk.buff, len , 1);
928 }
929 }
930 if (report) {
931 if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) {
932 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n", status & 0xff,
933 wrk.buff[2] & 0xf, wrk.buff[12], wrk.buff[13]);
934 }
935 else
936 pout(" status=0x%x\n", status);
937 }
938 if (iop->scsi_status > 0)
939 return 0;
940 else {
941 if (report > 0)
942 pout(" ioctl status=0x%x but scsi status=0, fail with EIO\n",
943 status);
944 return -EIO; /* give up, assume no device there */
945 }
946 }
947
948 /* SCSI command transmission interface function, linux version.
949 * Returns 0 if SCSI command successfully launched and response
950 * received. Even when 0 is returned the caller should check
951 * scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
952 * (e.g. CHECK CONDITION). If the SCSI command could not be issued
953 * (e.g. device not present or timeout) or some other problem
954 * (e.g. timeout) then returns a negative errno value */
955 static int do_normal_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop,
956 int report)
957 {
958 int res;
959
960 /* implementation relies on static sg_io_state variable. If not
961 * previously set tries the SG_IO ioctl. If that succeeds assume
962 * that SG_IO ioctl functional. If it fails with an errno value
963 * other than ENODEV (no device) or permission then assume
964 * SCSI_IOCTL_SEND_COMMAND is the only option. */
965 switch (sg_io_state) {
966 case SG_IO_PRESENT_UNKNOWN:
967 /* ignore report argument */
968 if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, 1))) {
969 sg_io_state = SG_IO_PRESENT_YES;
970 return 0;
971 } else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res))
972 return res; /* wait until we see a device */
973 sg_io_state = SG_IO_PRESENT_NO;
974 /* drop through by design */
975 case SG_IO_PRESENT_NO:
976 return sisc_cmnd_io(dev_fd, iop, report);
977 case SG_IO_PRESENT_YES:
978 return sg_io_cmnd_io(dev_fd, iop, report, 0);
979 default:
980 pout(">>>> do_scsi_cmnd_io: bad sg_io_state=%d\n", sg_io_state);
981 sg_io_state = SG_IO_PRESENT_UNKNOWN;
982 return -EIO; /* report error and reset state */
983 }
984 }
985
986 /* Check and call the right interface. Maybe when the do_generic_scsi_cmd_io interface is better
987 we can take off this crude way of calling the right interface */
988 int do_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report)
989 {
990 switch(con->controller_type)
991 {
992 case CONTROLLER_CCISS:
993 #ifdef HAVE_LINUX_CCISS_IOCTL_H
994 return cciss_io_interface(dev_fd, con->controller_port-1, iop, report);
995 #else
996 {
997 static int warned = 0;
998 if (!warned) {
999 pout("CCISS support is not available in this build of smartmontools,\n"
1000 "<linux/cciss_ioctl.h> was not available at build time.\n\n");
1001 warned = 1;
1002 }
1003 }
1004 errno = ENOSYS;
1005 return -1;
1006 #endif
1007 // not reached
1008 break;
1009 default:
1010 return do_normal_scsi_cmnd_io(dev_fd, iop, report);
1011 // not reached
1012 break;
1013 }
1014 }
1015
1016 // >>>>>> End of general SCSI specific linux code
1017
1018 // prototype
1019 void printwarning(smart_command_set command);
1020
1021 // PURPOSE
1022 // This is an interface routine meant to isolate the OS dependent
1023 // parts of the code, and to provide a debugging interface. Each
1024 // different port and OS needs to provide it's own interface. This
1025 // is the linux interface to the 3ware 3w-xxxx driver. It allows ATA
1026 // commands to be passed through the SCSI driver.
1027 // DETAILED DESCRIPTION OF ARGUMENTS
1028 // fd: is the file descriptor provided by open()
1029 // disknum is the disk number (0 to 15) in the RAID array
1030 // escalade_type indicates the type of controller type, and if scsi or char interface is used
1031 // command: defines the different operations.
1032 // select: additional input data if needed (which log, which type of
1033 // self-test).
1034 // data: location to write output data, if needed (512 bytes).
1035 // Note: not all commands use all arguments.
1036 // RETURN VALUES
1037 // -1 if the command failed
1038 // 0 if the command succeeded,
1039 // STATUS_CHECK routine:
1040 // -1 if the command failed
1041 // 0 if the command succeeded and disk SMART status is "OK"
1042 // 1 if the command succeeded and disk SMART status is "FAILING"
1043
1044
1045 /* 512 is the max payload size: increase if needed */
1046 #define BUFFER_LEN_678K ( sizeof(TW_Ioctl) ) // 1044 unpacked, 1041 packed
1047 #define BUFFER_LEN_678K_CHAR ( sizeof(TW_New_Ioctl)+512-1 ) // 1539 unpacked, 1536 packed
1048 #define BUFFER_LEN_9000 ( sizeof(TW_Ioctl_Buf_Apache)+512-1 ) // 2051 unpacked, 2048 packed
1049 #define TW_IOCTL_BUFFER_SIZE ( MAX(MAX(BUFFER_LEN_678K, BUFFER_LEN_9000), BUFFER_LEN_678K_CHAR) )
1050
1051 int escalade_command_interface(int fd, int disknum, int escalade_type, smart_command_set command, int select, char *data){
1052
1053 // return value and buffer for ioctl()
1054 int ioctlreturn, readdata=0;
1055
1056 // Used by both the SCSI and char interfaces
1057 TW_Passthru *passthru=NULL;
1058 char ioctl_buffer[TW_IOCTL_BUFFER_SIZE];
1059
1060 // only used for SCSI device interface
1061 TW_Ioctl *tw_ioctl=NULL;
1062 TW_Output *tw_output=NULL;
1063
1064 // only used for 6000/7000/8000 char device interface
1065 TW_New_Ioctl *tw_ioctl_char=NULL;
1066
1067 // only used for 9000 character device interface
1068 TW_Ioctl_Buf_Apache *tw_ioctl_apache=NULL;
1069
1070 memset(ioctl_buffer, 0, TW_IOCTL_BUFFER_SIZE);
1071
1072 if (escalade_type==CONTROLLER_3WARE_9000_CHAR) {
1073 tw_ioctl_apache = (TW_Ioctl_Buf_Apache *)ioctl_buffer;
1074 tw_ioctl_apache->driver_command.control_code = TW_IOCTL_FIRMWARE_PASS_THROUGH;
1075 tw_ioctl_apache->driver_command.buffer_length = 512; /* payload size */
1076 passthru = (TW_Passthru *)&(tw_ioctl_apache->firmware_command.command.oldcommand);
1077 }
1078 else if (escalade_type==CONTROLLER_3WARE_678K_CHAR) {
1079 tw_ioctl_char = (TW_New_Ioctl *)ioctl_buffer;
1080 tw_ioctl_char->data_buffer_length = 512;
1081 passthru = (TW_Passthru *)&(tw_ioctl_char->firmware_command);
1082 }
1083 else if (escalade_type==CONTROLLER_3WARE_678K) {
1084 tw_ioctl = (TW_Ioctl *)ioctl_buffer;
1085 tw_ioctl->cdb[0] = TW_IOCTL;
1086 tw_ioctl->opcode = TW_ATA_PASSTHRU;
1087 tw_ioctl->input_length = 512; // correct even for non-data commands
1088 tw_ioctl->output_length = 512; // correct even for non-data commands
1089 tw_output = (TW_Output *)tw_ioctl;
1090 passthru = (TW_Passthru *)&(tw_ioctl->input_data);
1091 }
1092 else {
1093 pout("Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
1094 "Please contact " PACKAGE_BUGREPORT "\n", escalade_type, disknum);
1095 errno=ENOSYS;
1096 return -1;
1097 }
1098
1099 // Same for (almost) all commands - but some reset below
1100 passthru->byte0.opcode = TW_OP_ATA_PASSTHRU;
1101 passthru->request_id = 0xFF;
1102 passthru->unit = disknum;
1103 passthru->status = 0;
1104 passthru->flags = 0x1;
1105 passthru->drive_head = 0x0;
1106 passthru->sector_num = 0;
1107
1108 // All SMART commands use this CL/CH signature. These are magic
1109 // values from the ATA specifications.
1110 passthru->cylinder_lo = 0x4F;
1111 passthru->cylinder_hi = 0xC2;
1112
1113 // SMART ATA COMMAND REGISTER value
1114 passthru->command = ATA_SMART_CMD;
1115
1116 // Is this a command that reads or returns 512 bytes?
1117 // passthru->param values are:
1118 // 0x0 - non data command without TFR write check,
1119 // 0x8 - non data command with TFR write check,
1120 // 0xD - data command that returns data to host from device
1121 // 0xF - data command that writes data from host to device
1122 // passthru->size values are 0x5 for non-data and 0x07 for data
1123 if (command == READ_VALUES ||
1124 command == READ_THRESHOLDS ||
1125 command == READ_LOG ||
1126 command == IDENTIFY ||
1127 command == WRITE_LOG ) {
1128 readdata=1;
1129 passthru->byte0.sgloff = 0x5;
1130 passthru->size = 0x7;
1131 passthru->param = 0xD;
1132 passthru->sector_count = 0x1;
1133 // For 64-bit to work correctly, up the size of the command packet
1134 // in dwords by 1 to account for the 64-bit single sgl 'address'
1135 // field. Note that this doesn't agree with the typedefs but it's
1136 // right (agree with kernel driver behavior/typedefs).
1137 if (escalade_type==CONTROLLER_3WARE_9000_CHAR && sizeof(long)==8)
1138 passthru->size++;
1139 }
1140 else {
1141 // Non data command -- but doesn't use large sector
1142 // count register values.
1143 passthru->byte0.sgloff = 0x0;
1144 passthru->size = 0x5;
1145 passthru->param = 0x8;
1146 passthru->sector_count = 0x0;
1147 }
1148
1149 // Now set ATA registers depending upon command
1150 switch (command){
1151 case CHECK_POWER_MODE:
1152 passthru->command = ATA_CHECK_POWER_MODE;
1153 passthru->features = 0;
1154 passthru->cylinder_lo = 0;
1155 passthru->cylinder_hi = 0;
1156 break;
1157 case READ_VALUES:
1158 passthru->features = ATA_SMART_READ_VALUES;
1159 break;
1160 case READ_THRESHOLDS:
1161 passthru->features = ATA_SMART_READ_THRESHOLDS;
1162 break;
1163 case READ_LOG:
1164 passthru->features = ATA_SMART_READ_LOG_SECTOR;
1165 // log number to return
1166 passthru->sector_num = select;
1167 break;
1168 case WRITE_LOG:
1169 if (escalade_type == CONTROLLER_3WARE_9000_CHAR)
1170 memcpy((unsigned char *)tw_ioctl_apache->data_buffer, data, 512);
1171 else if (escalade_type == CONTROLLER_3WARE_678K_CHAR)
1172 memcpy((unsigned char *)tw_ioctl_char->data_buffer, data, 512);
1173 else {
1174 // COMMAND NOT SUPPORTED VIA SCSI IOCTL INTERFACE
1175 // memcpy(tw_output->output_data, data, 512);
1176 printwarning(command);
1177 errno=ENOTSUP;
1178 return -1;
1179 }
1180 readdata=0;
1181 passthru->features = ATA_SMART_WRITE_LOG_SECTOR;
1182 passthru->sector_count = 1;
1183 passthru->sector_num = select;
1184 passthru->param = 0xF; // PIO data write
1185 break;
1186 case IDENTIFY:
1187 // ATA IDENTIFY DEVICE
1188 passthru->command = ATA_IDENTIFY_DEVICE;
1189 passthru->features = 0;
1190 passthru->cylinder_lo = 0;
1191 passthru->cylinder_hi = 0;
1192 break;
1193 case PIDENTIFY:
1194 // 3WARE controller can NOT have packet device internally
1195 pout("WARNING - NO DEVICE FOUND ON 3WARE CONTROLLER (disk %d)\n", disknum);
1196 pout("Note: /dev/sdX many need to be replaced with /dev/tweN or /dev/twaN\n");
1197 errno=ENODEV;
1198 return -1;
1199 case ENABLE:
1200 passthru->features = ATA_SMART_ENABLE;
1201 break;
1202 case DISABLE:
1203 passthru->features = ATA_SMART_DISABLE;
1204 break;
1205 case AUTO_OFFLINE:
1206 passthru->features = ATA_SMART_AUTO_OFFLINE;
1207 // Enable or disable?
1208 passthru->sector_count = select;
1209 break;
1210 case AUTOSAVE:
1211 passthru->features = ATA_SMART_AUTOSAVE;
1212 // Enable or disable?
1213 passthru->sector_count = select;
1214 break;
1215 case IMMEDIATE_OFFLINE:
1216 passthru->features = ATA_SMART_IMMEDIATE_OFFLINE;
1217 // What test type to run?
1218 passthru->sector_num = select;
1219 break;
1220 case STATUS_CHECK:
1221 passthru->features = ATA_SMART_STATUS;
1222 break;
1223 case STATUS:
1224 // This is JUST to see if SMART is enabled, by giving SMART status
1225 // command. But it doesn't say if status was good, or failing.
1226 // See below for the difference.
1227 passthru->features = ATA_SMART_STATUS;
1228 break;
1229 default:
1230 pout("Unrecognized command %d in linux_3ware_command_interface(disk %d)\n"
1231 "Please contact " PACKAGE_BUGREPORT "\n", command, disknum);
1232 errno=ENOSYS;
1233 return -1;
1234 }
1235
1236 // Now send the command down through an ioctl()
1237 if (escalade_type==CONTROLLER_3WARE_9000_CHAR)
1238 ioctlreturn=ioctl(fd, TW_IOCTL_FIRMWARE_PASS_THROUGH, tw_ioctl_apache);
1239 else if (escalade_type==CONTROLLER_3WARE_678K_CHAR)
1240 ioctlreturn=ioctl(fd, TW_CMD_PACKET_WITH_DATA, tw_ioctl_char);
1241 else
1242 ioctlreturn=ioctl(fd, SCSI_IOCTL_SEND_COMMAND, tw_ioctl);
1243
1244 // Deal with the different error cases
1245 if (ioctlreturn) {
1246 if (CONTROLLER_3WARE_678K==escalade_type && ((command==AUTO_OFFLINE || command==AUTOSAVE) && select)){
1247 // error here is probably a kernel driver whose version is too old
1248 printwarning(command);
1249 errno=ENOTSUP;
1250 }
1251 if (!errno)
1252 errno=EIO;
1253 return -1;
1254 }
1255
1256 // The passthru structure is valid after return from an ioctl if:
1257 // - we are using the character interface OR
1258 // - we are using the SCSI interface and this is a NON-READ-DATA command
1259 // For SCSI interface, note that we set passthru to a different
1260 // value after ioctl().
1261 if (CONTROLLER_3WARE_678K==escalade_type) {
1262 if (readdata)
1263 passthru=NULL;
1264 else
1265 passthru=(TW_Passthru *)&(tw_output->output_data);
1266 }
1267
1268 // See if the ATA command failed. Now that we have returned from
1269 // the ioctl() call, if passthru is valid, then:
1270 // - passthru->status contains the 3ware controller STATUS
1271 // - passthru->command contains the ATA STATUS register
1272 // - passthru->features contains the ATA ERROR register
1273 //
1274 // Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
1275 // If bit 0 (error bit) is set, then ATA ERROR register is valid.
1276 // While we *might* decode the ATA ERROR register, at the moment it
1277 // doesn't make much sense: we don't care in detail why the error
1278 // happened.
1279
1280 if (passthru && (passthru->status || (passthru->command & 0x21))) {
1281 errno=EIO;
1282 return -1;
1283 }
1284
1285 // If this is a read data command, copy data to output buffer
1286 if (readdata) {
1287 if (escalade_type==CONTROLLER_3WARE_9000_CHAR)
1288 memcpy(data, (unsigned char *)tw_ioctl_apache->data_buffer, 512);
1289 else if (escalade_type==CONTROLLER_3WARE_678K_CHAR)
1290 memcpy(data, (unsigned char *)tw_ioctl_char->data_buffer, 512);
1291 else
1292 memcpy(data, tw_output->output_data, 512);
1293 }
1294
1295 // For STATUS_CHECK, we need to check register values
1296 if (command==STATUS_CHECK) {
1297
1298 // To find out if the SMART RETURN STATUS is good or failing, we
1299 // need to examine the values of the Cylinder Low and Cylinder
1300 // High Registers.
1301
1302 unsigned short cyl_lo=passthru->cylinder_lo;
1303 unsigned short cyl_hi=passthru->cylinder_hi;
1304
1305 // If values in Cyl-LO and Cyl-HI are unchanged, SMART status is good.
1306 if (cyl_lo==0x4F && cyl_hi==0xC2)
1307 return 0;
1308
1309 // If values in Cyl-LO and Cyl-HI are as follows, SMART status is FAIL
1310 if (cyl_lo==0xF4 && cyl_hi==0x2C)
1311 return 1;
1312
1313 // Any other values mean that something has gone wrong with the command
1314 if (CONTROLLER_3WARE_678K==escalade_type) {
1315 printwarning(command);
1316 errno=ENOSYS;
1317 return 0;
1318 }
1319 else {
1320 errno=EIO;
1321 return -1;
1322 }
1323 }
1324
1325 // copy sector count register (one byte!) to return data
1326 if (command==CHECK_POWER_MODE)
1327 *data=*(char *)&(passthru->sector_count);
1328
1329 // look for nonexistent devices/ports
1330 if (command==IDENTIFY && !nonempty((unsigned char *)data, 512)) {
1331 errno=ENODEV;
1332 return -1;
1333 }
1334
1335 return 0;
1336 }
1337
1338
1339
1340 int marvell_command_interface(int device,
1341 smart_command_set command,
1342 int select,
1343 char *data) {
1344 typedef struct {
1345 int inlen;
1346 int outlen;
1347 char cmd[540];
1348 } mvsata_scsi_cmd;
1349
1350 int copydata = 0;
1351 mvsata_scsi_cmd smart_command;
1352 unsigned char *buff = (unsigned char *)&smart_command.cmd[6];
1353 // See struct hd_drive_cmd_hdr in hdreg.h
1354 // buff[0]: ATA COMMAND CODE REGISTER
1355 // buff[1]: ATA SECTOR NUMBER REGISTER
1356 // buff[2]: ATA FEATURES REGISTER
1357 // buff[3]: ATA SECTOR COUNT REGISTER
1358
1359 // clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
1360 memset(&smart_command, 0, sizeof(smart_command));
1361 smart_command.inlen = 540;
1362 smart_command.outlen = 540;
1363 smart_command.cmd[0] = 0xC; //Vendor-specific code
1364 smart_command.cmd[4] = 6; //command length
1365
1366 buff[0] = ATA_SMART_CMD;
1367 switch (command){
1368 case CHECK_POWER_MODE:
1369 buff[0]=ATA_CHECK_POWER_MODE;
1370 break;
1371 case READ_VALUES:
1372 buff[2]=ATA_SMART_READ_VALUES;
1373 copydata=buff[3]=1;
1374 break;
1375 case READ_THRESHOLDS:
1376 buff[2]=ATA_SMART_READ_THRESHOLDS;
1377 copydata=buff[1]=buff[3]=1;
1378 break;
1379 case READ_LOG:
1380 buff[2]=ATA_SMART_READ_LOG_SECTOR;
1381 buff[1]=select;
1382 copydata=buff[3]=1;
1383 break;
1384 case IDENTIFY:
1385 buff[0]=ATA_IDENTIFY_DEVICE;
1386 copydata=buff[3]=1;
1387 break;
1388 case PIDENTIFY:
1389 buff[0]=ATA_IDENTIFY_PACKET_DEVICE;
1390 copydata=buff[3]=1;
1391 break;
1392 case ENABLE:
1393 buff[2]=ATA_SMART_ENABLE;
1394 buff[1]=1;
1395 break;
1396 case DISABLE:
1397 buff[2]=ATA_SMART_DISABLE;
1398 buff[1]=1;
1399 break;
1400 case STATUS:
1401 case STATUS_CHECK:
1402 // this command only says if SMART is working. It could be
1403 // replaced with STATUS_CHECK below.
1404 buff[2] = ATA_SMART_STATUS;
1405 break;
1406 case AUTO_OFFLINE:
1407 buff[2]=ATA_SMART_AUTO_OFFLINE;
1408 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1409 break;
1410 case AUTOSAVE:
1411 buff[2]=ATA_SMART_AUTOSAVE;
1412 buff[3]=select; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1413 break;
1414 case IMMEDIATE_OFFLINE:
1415 buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
1416 buff[1]=select;
1417 break;
1418 default:
1419 pout("Unrecognized command %d in mvsata_os_specific_handler()\n", command);
1420 exit(1);
1421 break;
1422 }
1423 // There are two different types of ioctls(). The HDIO_DRIVE_TASK
1424 // one is this:
1425 // We are now doing the HDIO_DRIVE_CMD type ioctl.
1426 if (ioctl(device, SCSI_IOCTL_SEND_COMMAND, (void *)&smart_command))
1427 return -1;
1428
1429 if (command==CHECK_POWER_MODE) {
1430 // LEON -- CHECK THIS PLEASE. THIS SHOULD BE THE SECTOR COUNT
1431 // REGISTER, AND IT MIGHT BE buff[2] NOT buff[3]. Bruce
1432 data[0]=buff[3];
1433 return 0;
1434 }
1435
1436 // Always succeed on a SMART status, as a disk that failed returned
1437 // buff[4]=0xF4, buff[5]=0x2C, i.e. "Bad SMART status" (see below).
1438 if (command == STATUS)
1439 return 0;
1440 //Data returned is starting from 0 offset
1441 if (command == STATUS_CHECK)
1442 {
1443 // Cyl low and Cyl high unchanged means "Good SMART status"
1444 if (buff[4] == 0x4F && buff[5] == 0xC2)
1445 return 0;
1446 // These values mean "Bad SMART status"
1447 if (buff[4] == 0xF4 && buff[5] == 0x2C)
1448 return 1;
1449 // We haven't gotten output that makes sense; print out some debugging info
1450 syserror("Error SMART Status command failed");
1451 pout("Please get assistance from %s\n",PACKAGE_BUGREPORT);
1452 pout("Register values returned from SMART Status command are:\n");
1453 pout("CMD =0x%02x\n",(int)buff[0]);
1454 pout("FR =0x%02x\n",(int)buff[1]);
1455 pout("NS =0x%02x\n",(int)buff[2]);
1456 pout("SC =0x%02x\n",(int)buff[3]);
1457 pout("CL =0x%02x\n",(int)buff[4]);
1458 pout("CH =0x%02x\n",(int)buff[5]);
1459 pout("SEL=0x%02x\n",(int)buff[6]);
1460 return -1;
1461 }
1462
1463 if (copydata)
1464 memcpy(data, buff, 512);
1465 return 0;
1466 }
1467
1468 // this implementation is derived from ata_command_interface with a header
1469 // packing for highpoint linux driver ioctl interface
1470 //
1471 // ioctl(fd,HPTIO_CTL,buff)
1472 // ^^^^^^^^^
1473 //
1474 // structure of hpt_buff
1475 // +----+----+----+----+--------------------.....---------------------+
1476 // | 1 | 2 | 3 | 4 | 5 |
1477 // +----+----+----+----+--------------------.....---------------------+
1478 //
1479 // 1: The target controller [ int ( 4 Bytes ) ]
1480 // 2: The channel of the target controllee [ int ( 4 Bytes ) ]
1481 // 3: HDIO_ ioctl call [ int ( 4 Bytes ) ]
1482 // available from ${LINUX_KERNEL_SOURCE}/Documentation/ioctl/hdio
1483 // 4: the pmport that disk attached, [ int ( 4 Bytes ) ]
1484 // if no pmport device, set to 1 or leave blank
1485 // 5: data [ void * ( var leangth ) ]
1486 //
1487 #define STRANGE_BUFFER_LENGTH (4+512*0xf8)
1488
1489 int highpoint_command_interface(int device, smart_command_set command,
1490 int select, char *data)
1491
1492
1493 {
1494 unsigned char hpt_buff[4*sizeof(int) + STRANGE_BUFFER_LENGTH];
1495 unsigned int *hpt = (unsigned int *)hpt_buff;
1496 unsigned char *buff = &hpt_buff[4*sizeof(int)];
1497 int copydata = 0;
1498 const int HDIO_DRIVE_CMD_OFFSET = 4;
1499
1500 memset(hpt_buff, 0, 4*sizeof(int) + STRANGE_BUFFER_LENGTH);
1501 hpt[0] = con->hpt_data[0]; // controller id
1502 hpt[1] = con->hpt_data[1]; // channel number
1503 hpt[3] = con->hpt_data[2]; // pmport number
1504
1505 buff[0]=ATA_SMART_CMD;
1506 switch (command){
1507 case CHECK_POWER_MODE:
1508 buff[0]=ATA_CHECK_POWER_MODE;
1509 copydata=1;
1510 break;
1511 case READ_VALUES:
1512 buff[2]=ATA_SMART_READ_VALUES;
1513 buff[3]=1;
1514 copydata=512;
1515 break;
1516 case READ_THRESHOLDS:
1517 buff[2]=ATA_SMART_READ_THRESHOLDS;
1518 buff[1]=buff[3]=1;
1519 copydata=512;
1520 break;
1521 case READ_LOG:
1522 buff[2]=ATA_SMART_READ_LOG_SECTOR;
1523 buff[1]=select;
1524 buff[3]=1;
1525 copydata=512;
1526 break;
1527 case WRITE_LOG:
1528 break;
1529 case IDENTIFY:
1530 buff[0]=ATA_IDENTIFY_DEVICE;
1531 buff[3]=1;
1532 copydata=512;
1533 break;
1534 case PIDENTIFY:
1535 buff[0]=ATA_IDENTIFY_PACKET_DEVICE;
1536 buff[3]=1;
1537 copydata=512;
1538 break;
1539 case ENABLE:
1540 buff[2]=ATA_SMART_ENABLE;
1541 buff[1]=1;
1542 break;
1543 case DISABLE:
1544 buff[2]=ATA_SMART_DISABLE;
1545 buff[1]=1;
1546 break;
1547 case STATUS:
1548 buff[2]=ATA_SMART_STATUS;
1549 break;
1550 case AUTO_OFFLINE:
1551 buff[2]=ATA_SMART_AUTO_OFFLINE;
1552 buff[3]=select;
1553 break;
1554 case AUTOSAVE:
1555 buff[2]=ATA_SMART_AUTOSAVE;
1556 buff[3]=select;
1557 break;
1558 case IMMEDIATE_OFFLINE:
1559 buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
1560 buff[1]=select;
1561 break;
1562 case STATUS_CHECK:
1563 buff[1]=ATA_SMART_STATUS;
1564 break;
1565 default:
1566 pout("Unrecognized command %d in linux_highpoint_command_interface()\n"
1567 "Please contact " PACKAGE_BUGREPORT "\n", command);
1568 errno=ENOSYS;
1569 return -1;
1570 }
1571
1572 if (command==WRITE_LOG) {
1573 unsigned char task[4*sizeof(int)+sizeof(ide_task_request_t)+512];
1574 unsigned int *hpt = (unsigned int *)task;
1575 ide_task_request_t *reqtask = (ide_task_request_t *)(&task[4*sizeof(int)]);
1576 task_struct_t *taskfile = (task_struct_t *)reqtask->io_ports;
1577 int retval;
1578
1579 memset(task, 0, sizeof(task));
1580
1581 hpt[0] = con->hpt_data[0]; // controller id
1582 hpt[1] = con->hpt_data[1]; // channel number
1583 hpt[3] = con->hpt_data[2]; // pmport number
1584 hpt[2] = HDIO_DRIVE_TASKFILE; // real hd ioctl
1585
1586 taskfile->data = 0;
1587 taskfile->feature = ATA_SMART_WRITE_LOG_SECTOR;
1588 taskfile->sector_count = 1;
1589 taskfile->sector_number = select;
1590 taskfile->low_cylinder = 0x4f;
1591 taskfile->high_cylinder = 0xc2;
1592 taskfile->device_head = 0;
1593 taskfile->command = ATA_SMART_CMD;
1594
1595 reqtask->data_phase = TASKFILE_OUT;
1596 reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
1597 reqtask->out_size = 512;
1598 reqtask->in_size = 0;
1599
1600 memcpy(task+sizeof(ide_task_request_t)+4*sizeof(int), data, 512);
1601
1602 if ((retval=ioctl(device, HPTIO_CTL, task))) {
1603 if (retval==-EINVAL)
1604 pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASKFILE_IO set\n");
1605 return -1;
1606 }
1607 return 0;
1608 }
1609
1610 if (command==STATUS_CHECK){
1611 int retval;
1612 unsigned const char normal_lo=0x4f, normal_hi=0xc2;
1613 unsigned const char failed_lo=0xf4, failed_hi=0x2c;
1614 buff[4]=normal_lo;
1615 buff[5]=normal_hi;
1616
1617 hpt[2] = HDIO_DRIVE_TASK;
1618
1619 if ((retval=ioctl(device, HPTIO_CTL, hpt_buff))) {
1620 if (retval==-EINVAL) {
1621 pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
1622 pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
1623 }
1624 else
1625 syserror("Error SMART Status command failed");
1626 return -1;
1627 }
1628
1629 if (buff[4]==normal_lo && buff[5]==normal_hi)
1630 return 0;
1631
1632 if (buff[4]==failed_lo && buff[5]==failed_hi)
1633 return 1;
1634
1635 syserror("Error SMART Status command failed");
1636 pout("Please get assistance from " PACKAGE_HOMEPAGE "\n");
1637 pout("Register values returned from SMART Status command are:\n");
1638 pout("CMD=0x%02x\n",(int)buff[0]);
1639 pout("FR =0x%02x\n",(int)buff[1]);
1640 pout("NS =0x%02x\n",(int)buff[2]);
1641 pout("SC =0x%02x\n",(int)buff[3]);
1642 pout("CL =0x%02x\n",(int)buff[4]);
1643 pout("CH =0x%02x\n",(int)buff[5]);
1644 pout("SEL=0x%02x\n",(int)buff[6]);
1645 return -1;
1646 }
1647
1648 #if 1
1649 if (command==IDENTIFY || command==PIDENTIFY) {
1650 unsigned char deviceid[4*sizeof(int)+512*sizeof(char)];
1651 unsigned int *hpt = (unsigned int *)deviceid;
1652
1653 hpt[0] = con->hpt_data[0]; // controller id
1654 hpt[1] = con->hpt_data[1]; // channel number
1655 hpt[3] = con->hpt_data[2]; // pmport number
1656
1657 hpt[2] = HDIO_GET_IDENTITY;
1658 if (!ioctl(device, HPTIO_CTL, deviceid) && (deviceid[4*sizeof(int)] & 0x8000))
1659 buff[0]=(command==IDENTIFY)?ATA_IDENTIFY_PACKET_DEVICE:ATA_IDENTIFY_DEVICE;
1660 }
1661 #endif
1662
1663 hpt[2] = HDIO_DRIVE_CMD;
1664 if ((ioctl(device, HPTIO_CTL, hpt_buff)))
1665 return -1;
1666
1667 if (command==CHECK_POWER_MODE)
1668 buff[HDIO_DRIVE_CMD_OFFSET]=buff[2];
1669
1670 if (copydata)
1671 memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
1672
1673 return 0;
1674 }
1675
1676
1677 // Utility function for printing warnings
1678 void printwarning(smart_command_set command){
1679 static int printed[4]={0,0,0,0};
1680 const char* message=
1681 "can not be passed through the 3ware 3w-xxxx driver. This can be fixed by\n"
1682 "applying a simple 3w-xxxx driver patch that can be found here:\n"
1683 PACKAGE_HOMEPAGE "\n"
1684 "Alternatively, upgrade your 3w-xxxx driver to version 1.02.00.037 or greater.\n\n";
1685
1686 if (command==AUTO_OFFLINE && !printed[0]) {
1687 printed[0]=1;
1688 pout("The SMART AUTO-OFFLINE ENABLE command (smartmontools -o on option/Directive)\n%s", message);
1689 }
1690 else if (command==AUTOSAVE && !printed[1]) {
1691 printed[1]=1;
1692 pout("The SMART AUTOSAVE ENABLE command (smartmontools -S on option/Directive)\n%s", message);
1693 }
1694 else if (command==STATUS_CHECK && !printed[2]) {
1695 printed[2]=1;
1696 pout("The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n%s", message);
1697 }
1698 else if (command==WRITE_LOG && !printed[3]) {
1699 printed[3]=1;
1700 pout("The SMART WRITE LOG command (smartmontools -t selective) only supported via char /dev/tw[ae] interface\n");
1701 }
1702
1703 return;
1704 }
1705
1706 // Guess device type (ata or scsi) based on device name (Linux
1707 // specific) SCSI device name in linux can be sd, sr, scd, st, nst,
1708 // osst, nosst and sg.
1709 static const char * lin_dev_prefix = "/dev/";
1710 static const char * lin_dev_ata_disk_plus = "h";
1711 static const char * lin_dev_ata_devfs_disk_plus = "ide/";
1712 static const char * lin_dev_scsi_devfs_disk_plus = "scsi/";
1713 static const char * lin_dev_scsi_disk_plus = "s";
1714 static const char * lin_dev_scsi_tape1 = "ns";
1715 static const char * lin_dev_scsi_tape2 = "os";
1716 static const char * lin_dev_scsi_tape3 = "nos";
1717 static const char * lin_dev_3ware_9000_char = "twa";
1718 static const char * lin_dev_3ware_678k_char = "twe";
1719 static const char * lin_dev_cciss_dir = "cciss/";
1720
1721 int guess_device_type(const char * dev_name) {
1722 int len;
1723 int dev_prefix_len = strlen(lin_dev_prefix);
1724
1725 // if dev_name null, or string length zero
1726 if (!dev_name || !(len = strlen(dev_name)))
1727 return CONTROLLER_UNKNOWN;
1728
1729 // Remove the leading /dev/... if it's there
1730 if (!strncmp(lin_dev_prefix, dev_name, dev_prefix_len)) {
1731 if (len <= dev_prefix_len)
1732 // if nothing else in the string, unrecognized
1733 return CONTROLLER_UNKNOWN;
1734 // else advance pointer to following characters
1735 dev_name += dev_prefix_len;
1736 }
1737
1738 // form /dev/h* or h*
1739 if (!strncmp(lin_dev_ata_disk_plus, dev_name,
1740 strlen(lin_dev_ata_disk_plus)))
1741 return CONTROLLER_ATA;
1742
1743 // form /dev/ide/* or ide/*
1744 if (!strncmp(lin_dev_ata_devfs_disk_plus, dev_name,
1745 strlen(lin_dev_ata_devfs_disk_plus)))
1746 return CONTROLLER_ATA;
1747
1748 // form /dev/s* or s*
1749 if (!strncmp(lin_dev_scsi_disk_plus, dev_name,
1750 strlen(lin_dev_scsi_disk_plus)))
1751 return CONTROLLER_SCSI;
1752
1753 // form /dev/scsi/* or scsi/*
1754 if (!strncmp(lin_dev_scsi_devfs_disk_plus, dev_name,
1755 strlen(lin_dev_scsi_devfs_disk_plus)))
1756 return CONTROLLER_SCSI;
1757
1758 // form /dev/ns* or ns*
1759 if (!strncmp(lin_dev_scsi_tape1, dev_name,
1760 strlen(lin_dev_scsi_tape1)))
1761 return CONTROLLER_SCSI;
1762
1763 // form /dev/os* or os*
1764 if (!strncmp(lin_dev_scsi_tape2, dev_name,
1765 strlen(lin_dev_scsi_tape2)))
1766 return CONTROLLER_SCSI;
1767
1768 // form /dev/nos* or nos*
1769 if (!strncmp(lin_dev_scsi_tape3, dev_name,
1770 strlen(lin_dev_scsi_tape3)))
1771 return CONTROLLER_SCSI;
1772
1773 // form /dev/twa*
1774 if (!strncmp(lin_dev_3ware_9000_char, dev_name,
1775 strlen(lin_dev_3ware_9000_char)))
1776 return CONTROLLER_3WARE_9000_CHAR;
1777
1778 // form /dev/twe*
1779 if (!strncmp(lin_dev_3ware_678k_char, dev_name,
1780 strlen(lin_dev_3ware_678k_char)))
1781 return CONTROLLER_3WARE_678K_CHAR;
1782 // form /dev/cciss*
1783 if (!strncmp(lin_dev_cciss_dir, dev_name,
1784 strlen(lin_dev_cciss_dir)))
1785 return CONTROLLER_CCISS;
1786
1787 // we failed to recognize any of the forms
1788 return CONTROLLER_UNKNOWN;
1789 }
1790
1791
1792 #if 0
1793
1794 [ed@firestorm ed]$ ls -l /dev/discs
1795 total 0
1796 lr-xr-xr-x 1 root root 30 Dec 31 1969 disc0 -> ../ide/host2/bus0/target0/lun0/
1797 lr-xr-xr-x 1 root root 30 Dec 31 1969 disc1 -> ../ide/host2/bus1/target0/lun0/
1798 [ed@firestorm ed]$ ls -l dev/ide/host*/bus*/target*/lun*/disc
1799 ls: dev/ide/host*/bus*/target*/lun*/disc: No such file or directory
1800 [ed@firestorm ed]$ ls -l /dev/ide/host*/bus*/target*/lun*/disc
1801 brw------- 1 root root 33, 0 Dec 31 1969 /dev/ide/host2/bus0/target0/lun0/disc
1802 brw------- 1 root root 34, 0 Dec 31 1969 /dev/ide/host2/bus1/target0/lun0/disc
1803 [ed@firestorm ed]$ ls -l /dev/ide/c*b*t*u*
1804 ls: /dev/ide/c*b*t*u*: No such file or directory
1805 [ed@firestorm ed]$
1806 Script done on Fri Nov 7 13:46:28 2003
1807
1808 #endif