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