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