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