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