-
Notifications
You must be signed in to change notification settings - Fork 50
/
os_linux.cpp
3637 lines (3157 loc) · 116 KB
/
os_linux.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* os_linux.cpp
*
* Home page of code is: https://www.smartmontools.org
*
* Copyright (C) 2003-11 Bruce Allen
* Copyright (C) 2003-11 Doug Gilbert <[email protected]>
* Copyright (C) 2008-22 Christian Franke
*
* Original AACRaid code:
* Copyright (C) 2014 Raghava Aditya <[email protected]>
*
* Original Areca code:
* Copyright (C) 2008-12 Hank Wu <[email protected]>
* Copyright (C) 2008 Oliver Bock <[email protected]>
*
* Original MegaRAID code:
* Copyright (C) 2008 Jordan Hargrave <[email protected]>
*
* 3ware code was derived from code that was:
*
* Written By: Adam Radford <[email protected]>
* Modifications By: Joel Jacobson <[email protected]>
* Arnaldo Carvalho de Melo <[email protected]>
* Brad Strand <[email protected]>
*
* Copyright (C) 1999-2003 3ware Inc.
*
* Kernel compatibility By: Andre Hedrick <[email protected]>
* Non-Copyright (C) 2000 Andre Hedrick <[email protected]>
*
* Other ars of this file are derived from code that was
*
* Copyright (C) 1999-2000 Michael Cornwell <[email protected]>
* Copyright (C) 2000 Andre Hedrick <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <scsi/scsi.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/sg.h>
#include <linux/bsg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <stddef.h> // for offsetof()
#include <sys/uio.h>
#include <sys/types.h>
#include <dirent.h>
#ifdef HAVE_SYS_SYSMACROS_H
// glibc 2.25: The inclusion of <sys/sysmacros.h> by <sys/types.h> is
// deprecated. A warning is printed if major(), minor() or makedev()
// is used but <sys/sysmacros.h> is not included.
#include <sys/sysmacros.h>
#endif
#ifdef HAVE_LIBSELINUX
#include <selinux/selinux.h>
#endif
#include "atacmds.h"
#include "os_linux.h"
#include "scsicmds.h"
#include "utility.h"
#include "cciss.h"
#include "megaraid.h"
#include "sssraid.h"
#include "aacraid.h"
#include "nvmecmds.h"
#include "dev_interface.h"
#include "dev_ata_cmd_set.h"
#include "dev_areca.h"
// "include/uapi/linux/nvme_ioctl.h" from Linux kernel sources
#include "linux_nvme_ioctl.h" // nvme_passthru_cmd, NVME_IOCTL_ADMIN_CMD
#ifndef ENOTSUP
#define ENOTSUP ENOSYS
#endif
#define ARGUSED(x) ((void)(x))
const char * os_linux_cpp_cvsid = "$Id$"
OS_LINUX_H_CVSID;
extern unsigned char failuretest_permissive;
namespace os_linux { // No need to publish anything, name provided for Doxygen
/////////////////////////////////////////////////////////////////////////////
/// Shared open/close routines
class linux_smart_device
: virtual public /*implements*/ smart_device
{
public:
explicit linux_smart_device(int flags, int retry_flags = -1)
: smart_device(never_called),
m_fd(-1),
m_flags(flags), m_retry_flags(retry_flags)
{ }
virtual ~linux_smart_device();
virtual bool is_open() const override;
virtual bool open() override;
virtual bool close() override;
protected:
/// Return filedesc for derived classes.
int get_fd() const
{ return m_fd; }
void set_fd(int fd)
{ m_fd = fd; }
private:
int m_fd; ///< filedesc, -1 if not open.
int m_flags; ///< Flags for ::open()
int m_retry_flags; ///< Flags to retry ::open(), -1 if no retry
};
linux_smart_device::~linux_smart_device()
{
if (m_fd >= 0)
::close(m_fd);
}
bool linux_smart_device::is_open() const
{
return (m_fd >= 0);
}
bool linux_smart_device::open()
{
m_fd = ::open(get_dev_name(), m_flags);
if (m_fd < 0 && errno == EROFS && m_retry_flags != -1)
// Retry
m_fd = ::open(get_dev_name(), m_retry_flags);
if (m_fd < 0) {
if (errno == EBUSY && (m_flags & O_EXCL))
// device is locked
return set_err(EBUSY,
"The requested controller is used exclusively by another process!\n"
"(e.g. smartctl or smartd)\n"
"Please quit the impeding process or try again later...");
return set_err((errno==ENOENT || errno==ENOTDIR) ? ENODEV : errno);
}
if (m_fd >= 0) {
// sets FD_CLOEXEC on the opened device file descriptor. The
// descriptor is otherwise leaked to other applications (mail
// sender) which may be considered a security risk and may result
// in AVC messages on SELinux-enabled systems.
if (-1 == fcntl(m_fd, F_SETFD, FD_CLOEXEC))
// TODO: Provide an error printing routine in class smart_interface
pout("fcntl(set FD_CLOEXEC) failed, errno=%d [%s]\n", errno, strerror(errno));
}
return true;
}
// equivalent to close(file descriptor)
bool linux_smart_device::close()
{
int fd = m_fd; m_fd = -1;
if (::close(fd) < 0)
return set_err(errno);
return true;
}
// examples for smartctl
static const char smartctl_examples[] =
"=================================================== SMARTCTL EXAMPLES =====\n\n"
" smartctl --all /dev/sda (Prints all SMART information)\n\n"
" smartctl --smart=on --offlineauto=on --saveauto=on /dev/sda\n"
" (Enables SMART on first disk)\n\n"
" smartctl --test=long /dev/sda (Executes extended disk self-test)\n\n"
" smartctl --attributes --log=selftest --quietmode=errorsonly /dev/sda\n"
" (Prints Self-Test & Attribute errors)\n"
" smartctl --all --device=3ware,2 /dev/sda\n"
" smartctl --all --device=3ware,2 /dev/twe0\n"
" smartctl --all --device=3ware,2 /dev/twa0\n"
" smartctl --all --device=3ware,2 /dev/twl0\n"
" (Prints all SMART info for 3rd ATA disk on 3ware RAID controller)\n"
" smartctl --all --device=hpt,1/1/3 /dev/sda\n"
" (Prints all SMART info for the SATA disk attached to the 3rd PMPort\n"
" of the 1st channel on the 1st HighPoint RAID controller)\n"
" smartctl --all --device=areca,3/1 /dev/sg2\n"
" (Prints all SMART info for 3rd ATA disk of the 1st enclosure\n"
" on Areca RAID controller)\n"
;
/////////////////////////////////////////////////////////////////////////////
/// Linux ATA support
class linux_ata_device
: public /*implements*/ ata_device_with_command_set,
public /*extends*/ linux_smart_device
{
public:
linux_ata_device(smart_interface * intf, const char * dev_name, const char * req_type);
protected:
virtual int ata_command_interface(smart_command_set command, int select, char * data) override;
};
linux_ata_device::linux_ata_device(smart_interface * intf, const char * dev_name, const char * req_type)
: smart_device(intf, dev_name, "ata", req_type),
linux_smart_device(O_RDONLY | O_NONBLOCK)
{
}
// PURPOSE
// This is an interface routine meant to isolate the OS dependent
// parts of the code, and to provide a debugging interface. Each
// different port and OS needs to provide it's own interface. This
// is the linux one.
// DETAILED DESCRIPTION OF ARGUMENTS
// device: is the file descriptor provided by open()
// command: defines the different operations.
// select: additional input data if needed (which log, which type of
// self-test).
// data: location to write output data, if needed (512 bytes).
// Note: not all commands use all arguments.
// RETURN VALUES
// -1 if the command failed
// 0 if the command succeeded,
// STATUS_CHECK routine:
// -1 if the command failed
// 0 if the command succeeded and disk SMART status is "OK"
// 1 if the command succeeded and disk SMART status is "FAILING"
#define BUFFER_LENGTH (4+512)
int linux_ata_device::ata_command_interface(smart_command_set command, int select, char * data)
{
unsigned char buff[BUFFER_LENGTH];
// positive: bytes to write to caller. negative: bytes to READ from
// caller. zero: non-data command
int copydata=0;
const int HDIO_DRIVE_CMD_OFFSET = 4;
// See struct hd_drive_cmd_hdr in hdreg.h. Before calling ioctl()
// buff[0]: ATA COMMAND CODE REGISTER
// buff[1]: ATA SECTOR NUMBER REGISTER == LBA LOW REGISTER
// buff[2]: ATA FEATURES REGISTER
// buff[3]: ATA SECTOR COUNT REGISTER
// Note that on return:
// buff[2] contains the ATA SECTOR COUNT REGISTER
// clear out buff. Large enough for HDIO_DRIVE_CMD (4+512 bytes)
memset(buff, 0, BUFFER_LENGTH);
buff[0]=ATA_SMART_CMD;
switch (command){
case CHECK_POWER_MODE:
buff[0]=ATA_CHECK_POWER_MODE;
copydata=1;
break;
case READ_VALUES:
buff[2]=ATA_SMART_READ_VALUES;
buff[3]=1;
copydata=512;
break;
case READ_THRESHOLDS:
buff[2]=ATA_SMART_READ_THRESHOLDS;
buff[1]=buff[3]=1;
copydata=512;
break;
case READ_LOG:
buff[2]=ATA_SMART_READ_LOG_SECTOR;
buff[1]=select;
buff[3]=1;
copydata=512;
break;
case WRITE_LOG:
break;
case IDENTIFY:
buff[0]=ATA_IDENTIFY_DEVICE;
buff[3]=1;
copydata=512;
break;
case PIDENTIFY:
buff[0]=ATA_IDENTIFY_PACKET_DEVICE;
buff[3]=1;
copydata=512;
break;
case ENABLE:
buff[2]=ATA_SMART_ENABLE;
buff[1]=1;
break;
case DISABLE:
buff[2]=ATA_SMART_DISABLE;
buff[1]=1;
break;
case STATUS:
// this command only says if SMART is working. It could be
// replaced with STATUS_CHECK below.
buff[2]=ATA_SMART_STATUS;
break;
case AUTO_OFFLINE:
// NOTE: According to ATAPI 4 and UP, this command is obsolete
// select == 241 for enable but no data transfer. Use TASK ioctl.
buff[1]=ATA_SMART_AUTO_OFFLINE;
buff[2]=select;
break;
case AUTOSAVE:
// select == 248 for enable but no data transfer. Use TASK ioctl.
buff[1]=ATA_SMART_AUTOSAVE;
buff[2]=select;
break;
case IMMEDIATE_OFFLINE:
buff[2]=ATA_SMART_IMMEDIATE_OFFLINE;
buff[1]=select;
break;
case STATUS_CHECK:
// This command uses HDIO_DRIVE_TASK and has different syntax than
// the other commands.
buff[1]=ATA_SMART_STATUS;
break;
default:
pout("Unrecognized command %d in linux_ata_command_interface()\n"
"Please contact " PACKAGE_BUGREPORT "\n", command);
errno=ENOSYS;
return -1;
}
// This command uses the HDIO_DRIVE_TASKFILE ioctl(). This is the
// only ioctl() that can be used to WRITE data to the disk.
if (command==WRITE_LOG) {
unsigned char task[sizeof(ide_task_request_t)+512];
ide_task_request_t *reqtask=(ide_task_request_t *) task;
task_struct_t *taskfile=(task_struct_t *) reqtask->io_ports;
memset(task, 0, sizeof(task));
taskfile->data = 0;
taskfile->feature = ATA_SMART_WRITE_LOG_SECTOR;
taskfile->sector_count = 1;
taskfile->sector_number = select;
taskfile->low_cylinder = 0x4f;
taskfile->high_cylinder = 0xc2;
taskfile->device_head = 0;
taskfile->command = ATA_SMART_CMD;
reqtask->data_phase = TASKFILE_OUT;
reqtask->req_cmd = IDE_DRIVE_TASK_OUT;
reqtask->out_size = 512;
reqtask->in_size = 0;
// copy user data into the task request structure
memcpy(task+sizeof(ide_task_request_t), data, 512);
if (ioctl(get_fd(), HDIO_DRIVE_TASKFILE, task)) {
if (errno==EINVAL)
pout("Kernel lacks HDIO_DRIVE_TASKFILE support; compile kernel with CONFIG_IDE_TASK_IOCTL set\n");
return -1;
}
return 0;
}
// There are two different types of ioctls(). The HDIO_DRIVE_TASK
// one is this:
if (command==STATUS_CHECK || command==AUTOSAVE || command==AUTO_OFFLINE){
// NOT DOCUMENTED in /usr/src/linux/include/linux/hdreg.h. You
// have to read the IDE driver source code. Sigh.
// buff[0]: ATA COMMAND CODE REGISTER
// buff[1]: ATA FEATURES REGISTER
// buff[2]: ATA SECTOR_COUNT
// buff[3]: ATA SECTOR NUMBER
// buff[4]: ATA CYL LO REGISTER
// buff[5]: ATA CYL HI REGISTER
// buff[6]: ATA DEVICE HEAD
unsigned const char normal_lo=0x4f, normal_hi=0xc2;
unsigned const char failed_lo=0xf4, failed_hi=0x2c;
buff[4]=normal_lo;
buff[5]=normal_hi;
if (ioctl(get_fd(), HDIO_DRIVE_TASK, buff)) {
if (errno==EINVAL) {
pout("Error SMART Status command via HDIO_DRIVE_TASK failed");
pout("Rebuild older linux 2.2 kernels with HDIO_DRIVE_TASK support added\n");
}
else
syserror("Error SMART Status command failed");
return -1;
}
// Cyl low and Cyl high unchanged means "Good SMART status"
if (buff[4]==normal_lo && buff[5]==normal_hi)
return 0;
// These values mean "Bad SMART status"
if (buff[4]==failed_lo && buff[5]==failed_hi)
return 1;
// We haven't gotten output that makes sense; print out some debugging info
syserror("Error SMART Status command failed");
pout("Please get assistance from " PACKAGE_URL "\n");
pout("Register values returned from SMART Status command are:\n");
pout("ST =0x%02x\n",(int)buff[0]);
pout("ERR=0x%02x\n",(int)buff[1]);
pout("NS =0x%02x\n",(int)buff[2]);
pout("SC =0x%02x\n",(int)buff[3]);
pout("CL =0x%02x\n",(int)buff[4]);
pout("CH =0x%02x\n",(int)buff[5]);
pout("SEL=0x%02x\n",(int)buff[6]);
return -1;
}
#if 1
// Note to people doing ports to other OSes -- don't worry about
// this block -- you can safely ignore it. I have put it here
// because under linux when you do IDENTIFY DEVICE to a packet
// device, it generates an ugly kernel syslog error message. This
// is harmless but frightens users. So this block detects packet
// devices and make IDENTIFY DEVICE fail "nicely" without a syslog
// error message.
//
// If you read only the ATA specs, it appears as if a packet device
// *might* respond to the IDENTIFY DEVICE command. This is
// misleading - it's because around the time that SFF-8020 was
// incorporated into the ATA-3/4 standard, the ATA authors were
// sloppy. See SFF-8020 and you will see that ATAPI devices have
// *always* had IDENTIFY PACKET DEVICE as a mandatory part of their
// command set, and return 'Command Aborted' to IDENTIFY DEVICE.
if (command==IDENTIFY || command==PIDENTIFY){
unsigned short deviceid[256];
// check the device identity, as seen when the system was booted
// or the device was FIRST registered. This will not be current
// if the user has subsequently changed some of the parameters. If
// device is a packet device, swap the command interpretations.
if (!ioctl(get_fd(), HDIO_GET_IDENTITY, deviceid) && (deviceid[0] & 0x8000))
buff[0]=(command==IDENTIFY)?ATA_IDENTIFY_PACKET_DEVICE:ATA_IDENTIFY_DEVICE;
}
#endif
// We are now doing the HDIO_DRIVE_CMD type ioctl.
if ((ioctl(get_fd(), HDIO_DRIVE_CMD, buff)))
return -1;
// CHECK POWER MODE command returns information in the Sector Count
// register (buff[3]). Copy to return data buffer.
if (command==CHECK_POWER_MODE)
buff[HDIO_DRIVE_CMD_OFFSET]=buff[2];
// if the command returns data then copy it back
if (copydata)
memcpy(data, buff+HDIO_DRIVE_CMD_OFFSET, copydata);
return 0;
}
// >>>>>> Start of general SCSI specific linux code
/* Linux specific code.
* Historically smartmontools (and smartsuite before it) used the
* SCSI_IOCTL_SEND_COMMAND ioctl which is available to all linux device
* nodes that use the SCSI subsystem. A better interface has been available
* via the SCSI generic (sg) driver but this involves the extra step of
* mapping disk devices (e.g. /dev/sda) to the corresponding sg device
* (e.g. /dev/sg2). In the linux kernel 2.6 series most of the facilities of
* the sg driver have become available via the SG_IO ioctl which is available
* on all SCSI devices (on SCSI tape devices from lk 2.6.6). Now in lk 5.17
* the SCSI_IOCTL_SEND_COMMAND ioctl is still present but deprecated sending
* a warning to the log the first time (after power up) it is used. The SG_IO
* Version 3 interface is the most widely used (circa lk 5.17 in 2022) and is
* available on the primary block device name (e.g. /dev/sdc) for all SCSI
* disks (and tapes) including all USB attached storage and all ATA/SATA
* storage. */
#define MAX_DXFER_LEN 1024 /* can be increased if necessary */
#define SEND_IOCTL_RESP_SENSE_LEN 16 /* ioctl limitation */
#define SG_IO_RESP_SENSE_LEN 64 /* large enough see buffer */
#define LSCSI_DRIVER_MASK 0xf /* mask out "suggestions" */
#define LSCSI_DRIVER_SENSE 0x8 /* alternate CHECK CONDITION indication */
#define LSCSI_DID_ERROR 0x7 /* Need to work around aacraid driver quirk */
#define LSCSI_DRIVER_TIMEOUT 0x6
#define LSCSI_DID_TIME_OUT 0x3
#define LSCSI_DID_BUS_BUSY 0x2
#define LSCSI_DID_NO_CONNECT 0x1
enum lk_sg_io_ifc_t {
SG_IO_USE_DETECT = 0,
SG_IO_UNSUPP = 1,
SG_IO_USE_V3 = 3,
SG_IO_USE_V4 = 4,
};
static enum lk_sg_io_ifc_t sg_io_interface = SG_IO_USE_DETECT;
/* Preferred implementation for issuing SCSI commands in linux. This
* function uses the SG_IO ioctl. Return 0 if command issued successfully
* (various status values should still be checked). If the SCSI command
* cannot be issued then a negative errno value is returned. */
static int sg_io_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop, int report,
enum lk_sg_io_ifc_t sg_io_ifc)
{
/* we are filling structures for both versions, but using only one requested */
struct sg_io_hdr io_hdr_v3;
struct sg_io_v4 io_hdr_v4;
#ifdef SCSI_CDB_CHECK
bool ok = is_scsi_cdb(iop->cmnd, iop->cmnd_len);
if (! ok) {
int n = iop->cmnd_len;
const unsigned char * ucp = iop->cmnd;
pout(">>>>>>>> %s: cdb seems invalid, opcode=0x%x, len=%d, cdb:\n",
__func__, ((n > 0) ? ucp[0] : 0), n);
if (n > 0) {
if (n > 16)
pout(" <<truncating to first 16 bytes>>\n");
dStrHex((const uint8_t *)ucp, ((n > 16) ? 16 : n), 1);
}
}
#endif
#if 0
if (report > 0) {
int k, j;
const unsigned char * ucp = iop->cmnd;
const char * np;
char buff[256];
const int sz = (int)sizeof(buff);
pout(">>>> %s: sg_io_ifc=%d\n", __func__, (int)sg_io_ifc);
np = scsi_get_opcode_name(ucp);
j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
for (k = 0; k < (int)iop->cmnd_len; ++k)
j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
if ((report > 1) &&
(DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
int trunc = (iop->dxfer_len > 256) ? 1 : 0;
snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
"data, len=%d%s:\n", (int)iop->dxfer_len,
(trunc ? " [only first 256 bytes shown]" : ""));
dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1);
}
else
snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
pout("%s", buff);
pout("%s\n", buff);
}
#endif
memset(&io_hdr_v3, 0, sizeof(struct sg_io_hdr));
memset(&io_hdr_v4, 0, sizeof(struct sg_io_v4));
io_hdr_v3.interface_id = 'S';
io_hdr_v3.cmd_len = iop->cmnd_len;
io_hdr_v3.mx_sb_len = iop->max_sense_len;
io_hdr_v3.dxfer_len = iop->dxfer_len;
io_hdr_v3.dxferp = iop->dxferp;
io_hdr_v3.cmdp = iop->cmnd;
io_hdr_v3.sbp = iop->sensep;
/* sg_io_hdr interface timeout has millisecond units. Timeout of 0
defaults to 60 seconds. */
io_hdr_v3.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000;
io_hdr_v4.guard = 'Q';
io_hdr_v4.request_len = iop->cmnd_len;
io_hdr_v4.request = __u64(iop->cmnd);
io_hdr_v4.max_response_len = iop->max_sense_len;
io_hdr_v4.response = __u64(iop->sensep);
io_hdr_v4.timeout = ((0 == iop->timeout) ? 60 : iop->timeout) * 1000; // msec
switch (iop->dxfer_dir) {
case DXFER_NONE:
io_hdr_v3.dxfer_direction = SG_DXFER_NONE;
break;
case DXFER_FROM_DEVICE:
io_hdr_v3.dxfer_direction = SG_DXFER_FROM_DEV;
io_hdr_v4.din_xfer_len = iop->dxfer_len;
io_hdr_v4.din_xferp = __u64(iop->dxferp);
break;
case DXFER_TO_DEVICE:
io_hdr_v3.dxfer_direction = SG_DXFER_TO_DEV;
io_hdr_v4.dout_xfer_len = iop->dxfer_len;
io_hdr_v4.dout_xferp = __u64(iop->dxferp);
break;
default:
pout("do_scsi_cmnd_io: bad dxfer_dir\n");
return -EINVAL;
}
iop->resp_sense_len = 0;
iop->scsi_status = 0;
iop->resid = 0;
void * io_hdr = NULL;
switch (sg_io_ifc) {
case SG_IO_USE_V3:
io_hdr = &io_hdr_v3;
break;
case SG_IO_USE_V4:
io_hdr = &io_hdr_v4;
break;
default:
// should never be reached
errno = EOPNOTSUPP;
return -errno;
}
if (ioctl(dev_fd, SG_IO, io_hdr) < 0) {
if (report)
pout(" SG_IO ioctl failed, errno=%d [%s], SG_IO_V%d\n", errno,
strerror(errno), (int)sg_io_ifc);
return -errno;
}
unsigned int sg_driver_status = 0, sg_transport_status = 0, sg_info = 0,
sg_duration = 0;
if (sg_io_ifc == SG_IO_USE_V3) {
iop->resid = io_hdr_v3.resid;
iop->scsi_status = io_hdr_v3.status;
sg_driver_status = io_hdr_v3.driver_status;
sg_transport_status = io_hdr_v3.host_status;
sg_info = io_hdr_v3.info;
iop->resp_sense_len = io_hdr_v3.sb_len_wr;
sg_duration = io_hdr_v3.duration;
}
if (sg_io_ifc == SG_IO_USE_V4) {
switch (iop->dxfer_dir) {
case DXFER_NONE:
iop->resid = 0;
break;
case DXFER_FROM_DEVICE:
iop->resid = io_hdr_v4.din_resid;
break;
case DXFER_TO_DEVICE:
iop->resid = io_hdr_v4.dout_resid;
break;
}
iop->scsi_status = io_hdr_v4.device_status;
sg_driver_status = io_hdr_v4.driver_status;
sg_transport_status = io_hdr_v4.transport_status;
sg_info = io_hdr_v4.info;
iop->resp_sense_len = io_hdr_v4.response_len;
sg_duration = io_hdr_v4.duration;
}
if (sg_duration) { } // silence warning
#if 0
if (report > 0) {
pout(" scsi_status=0x%x, sg_transport_status=0x%x, sg_driver_status=0x%x\n"
" sg_info=0x%x sg_duration=%d milliseconds resid=%d\n", iop->scsi_status,
sg_transport_status, sg_driver_status, sg_info,
sg_duration, iop->resid);
if (report > 1) {
if (DXFER_FROM_DEVICE == iop->dxfer_dir) {
int trunc, len;
len = iop->dxfer_len - iop->resid;
trunc = (len > 256) ? 1 : 0;
if (len > 0) {
pout(" Incoming data, len=%d%s:\n", len,
(trunc ? " [only first 256 bytes shown]" : ""));
dStrHex(iop->dxferp, (trunc ? 256 : len), 1);
} else
pout(" Incoming data trimmed to nothing by resid\n");
}
}
}
#endif
if (sg_info & SG_INFO_CHECK) { /* error or warning */
int masked_driver_status = (LSCSI_DRIVER_MASK & sg_driver_status);
if (0 != sg_transport_status) {
if ((LSCSI_DID_NO_CONNECT == sg_transport_status) ||
(LSCSI_DID_BUS_BUSY == sg_transport_status) ||
(LSCSI_DID_TIME_OUT == sg_transport_status))
return -ETIMEDOUT;
else
/* Check for DID_ERROR - workaround for aacraid driver quirk */
if (LSCSI_DID_ERROR != sg_transport_status) {
return -EIO; /* catch all if not DID_ERR */
}
}
if (0 != masked_driver_status) {
if (LSCSI_DRIVER_TIMEOUT == masked_driver_status)
return -ETIMEDOUT;
else if (LSCSI_DRIVER_SENSE != masked_driver_status)
return -EIO;
}
if (LSCSI_DRIVER_SENSE == masked_driver_status)
iop->scsi_status = SCSI_STATUS_CHECK_CONDITION;
if ((SCSI_STATUS_CHECK_CONDITION == iop->scsi_status) &&
iop->sensep && (iop->resp_sense_len > 0)) {
if (report > 1) {
pout(" >>> Sense buffer, len=%d:\n",
(int)iop->resp_sense_len);
dStrHex(iop->sensep, iop->resp_sense_len , 1);
}
}
if (report) {
if (SCSI_STATUS_CHECK_CONDITION == iop->scsi_status && iop->sensep) {
if ((iop->sensep[0] & 0x7f) > 0x71)
pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
iop->scsi_status, iop->sensep[1] & 0xf,
iop->sensep[2], iop->sensep[3]);
else
pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
iop->scsi_status, iop->sensep[2] & 0xf,
iop->sensep[12], iop->sensep[13]);
}
else
pout(" status=0x%x\n", iop->scsi_status);
}
}
return 0;
}
/* SCSI command transmission interface function, linux version.
* Returns 0 if SCSI command successfully launched and response
* received. Even when 0 is returned the caller should check
* scsi_cmnd_io::scsi_status for SCSI defined errors and warnings
* (e.g. CHECK CONDITION). If the SCSI command could not be issued
* (e.g. device not present or timeout) or some other problem
* (e.g. timeout) then returns a negative errno value */
static int do_normal_scsi_cmnd_io(int dev_fd, struct scsi_cmnd_io * iop,
int report)
{
int res;
/* implementation relies on static sg_io_interface variable. If not
* previously set tries the SG_IO ioctl. If that succeeds assume
* that SG_IO ioctl functional. If it fails with an errno value
* other than ENODEV (no device) or a permissions problem then
* assume the SG_IO_USE_V3 interface. */
switch (sg_io_interface) {
case SG_IO_USE_DETECT:
/* ignore report argument */
/* Try SG_IO V3 first */
if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, SG_IO_USE_V3))) {
sg_io_interface = SG_IO_USE_V3;
return 0;
} else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res))
return res; /* wait until we see a device */
/* See if we can use SG_IO V4 * */
if (0 == (res = sg_io_cmnd_io(dev_fd, iop, report, SG_IO_USE_V4))) {
sg_io_interface = SG_IO_USE_V4;
return 0;
} else if ((-ENODEV == res) || (-EACCES == res) || (-EPERM == res))
return res; /* wait until we see a device */
sg_io_interface = SG_IO_UNSUPP;
/* FALLTHRU */
case SG_IO_UNSUPP:
/* previously called SCSI_IOCTL_SEND_COMMAND ioctl which has now
* been removed. The SG_IO_USE_V3 is most widely used now in Linux
* (circa 2022), try it again. */
sg_io_interface = SG_IO_USE_V3;
/* FALLTHRU */
case SG_IO_USE_V3:
case SG_IO_USE_V4:
/* use SG_IO V3 or V4 ioctl, depending on availabiliy */
return sg_io_cmnd_io(dev_fd, iop, report, sg_io_interface);
default:
pout(">>>> do_scsi_cmnd_io: bad sg_io_interface=%d\n",
(int)sg_io_interface);
sg_io_interface = SG_IO_USE_DETECT;
return -EIO; /* report error and reset state */
}
}
// >>>>>> End of general SCSI specific linux code
/////////////////////////////////////////////////////////////////////////////
/// Standard SCSI support
class linux_scsi_device
: public /*implements*/ scsi_device,
public /*extends*/ linux_smart_device
{
public:
linux_scsi_device(smart_interface * intf, const char * dev_name,
const char * req_type, bool scanning = false);
virtual smart_device * autodetect_open() override;
virtual bool scsi_pass_through(scsi_cmnd_io * iop) override;
private:
bool m_scanning; ///< true if created within scan_smart_devices
};
linux_scsi_device::linux_scsi_device(smart_interface * intf,
const char * dev_name, const char * req_type, bool scanning /*= false*/)
: smart_device(intf, dev_name, "scsi", req_type),
// If opened with O_RDWR, a SATA disk in standby mode
// may spin-up after device close().
linux_smart_device(O_RDONLY | O_NONBLOCK),
m_scanning(scanning)
{
}
bool linux_scsi_device::scsi_pass_through(scsi_cmnd_io * iop)
{
int status = do_normal_scsi_cmnd_io(get_fd(), iop, scsi_debugmode);
if (status < 0)
return set_err(-status);
return true;
}
/////////////////////////////////////////////////////////////////////////////
/// PMC AacRAID support
class linux_aacraid_device
:public scsi_device,
public /*extends */ linux_smart_device
{
public:
linux_aacraid_device(smart_interface *intf, const char *dev_name,
unsigned int host, unsigned int channel, unsigned int device);
virtual ~linux_aacraid_device();
virtual bool open() override;
virtual bool scsi_pass_through(scsi_cmnd_io *iop) override;
private:
//Device Host number
int aHost;
//Channel(Lun) of the device
int aLun;
//Id of the device
int aId;
};
linux_aacraid_device::linux_aacraid_device(smart_interface *intf,
const char *dev_name, unsigned int host, unsigned int channel, unsigned int device)
: smart_device(intf,dev_name,"aacraid","aacraid"),
linux_smart_device(O_RDWR|O_NONBLOCK),
aHost(host), aLun(channel), aId(device)
{
set_info().info_name = strprintf("%s [aacraid_disk_%02d_%02d_%d]",dev_name,aHost,aLun,aId);
set_info().dev_type = strprintf("aacraid,%d,%d,%d",aHost,aLun,aId);
}
linux_aacraid_device::~linux_aacraid_device()
{
}
bool linux_aacraid_device::open()
{
//Create the character device name based on the host number
//Required for get stats from disks connected to different controllers
char dev_name[128];
snprintf(dev_name, sizeof(dev_name), "/dev/aac%d", aHost);
//Initial open of dev name to check if it exists
int afd = ::open(dev_name,O_RDWR);
if(afd < 0 && errno == ENOENT) {
FILE *fp = fopen("/proc/devices","r");
if(NULL == fp)
return set_err(errno,"cannot open /proc/devices:%s",
strerror(errno));
char line[256];
int mjr = -1;
while(fgets(line,sizeof(line),fp) !=NULL) {
int nc = -1;
if(sscanf(line,"%d aac%n",&mjr,&nc) == 1
&& nc > 0 && '\n' == line[nc])
break;
mjr = -1;
}
//work with /proc/devices is done
fclose(fp);
if (mjr < 0)
return set_err(ENOENT, "aac entry not found in /proc/devices");
//Create misc device file in /dev/ used for communication with driver
if(mknod(dev_name, S_IFCHR|0600, makedev(mjr,aHost)))
return set_err(errno,"cannot create %s:%s",dev_name,strerror(errno));
afd = ::open(dev_name,O_RDWR);
}
if(afd < 0)
return set_err(errno,"cannot open %s:%s",dev_name,strerror(errno));
set_fd(afd);
return true;
}
bool linux_aacraid_device::scsi_pass_through(scsi_cmnd_io *iop)
{
int report = scsi_debugmode;
if (report > 0) {
int k, j;
const unsigned char * ucp = iop->cmnd;
const char * np;
char buff[256];
const int sz = (int)sizeof(buff);
np = scsi_get_opcode_name(ucp);
j = snprintf(buff, sz, " [%s: ", np ? np : "<unknown opcode>");
for (k = 0; k < (int)iop->cmnd_len; ++k)
j += snprintf(&buff[j], (sz > j ? (sz - j) : 0), "%02x ", ucp[k]);
if ((report > 1) &&
(DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
int trunc = (iop->dxfer_len > 256) ? 1 : 0;
snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n Outgoing "
"data, len=%d%s:\n", (int)iop->dxfer_len,
(trunc ? " [only first 256 bytes shown]" : ""));
dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1);
}
else
snprintf(&buff[j], (sz > j ? (sz - j) : 0), "]\n");
pout("%s", buff);
}
//return test commands
if (iop->cmnd[0] == 0x00)
return true;
user_aac_reply *pReply;
#ifdef ENVIRONMENT64
// Create user 64 bit request
user_aac_srb64 *pSrb;
uint8_t aBuff[sizeof(user_aac_srb64) + sizeof(user_aac_reply)] = {0,};
pSrb = (user_aac_srb64*)aBuff;
pSrb->count = sizeof(user_aac_srb64) - sizeof(user_sgentry64);
#elif defined(ENVIRONMENT32)
//Create user 32 bit request
user_aac_srb32 *pSrb;
uint8_t aBuff[sizeof(user_aac_srb32) + sizeof(user_aac_reply)] = {0,};
pSrb = (user_aac_srb32*)aBuff;
pSrb->count = sizeof(user_aac_srb32) - sizeof(user_sgentry32);
#endif
pSrb->function = SRB_FUNCTION_EXECUTE_SCSI;
//channel is 0 always
pSrb->channel = 0;
pSrb->id = aId;
pSrb->lun = aLun;
pSrb->timeout = 0;
pSrb->retry_limit = 0;
pSrb->cdb_size = iop->cmnd_len;
switch(iop->dxfer_dir) {
case DXFER_NONE:
pSrb->flags = SRB_NoDataXfer;
break;
case DXFER_FROM_DEVICE:
pSrb->flags = SRB_DataIn;
break;
case DXFER_TO_DEVICE:
pSrb->flags = SRB_DataOut;
break;
default:
pout("aacraid: bad dxfer_dir\n");
return set_err(EINVAL, "aacraid: bad dxfer_dir\n");
}
if(iop->dxfer_len > 0) {