-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathos_freebsd.cpp
2536 lines (2159 loc) · 73.1 KB
/
os_freebsd.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_freebsd.cpp
*
* Home page of code is: https://www.smartmontools.org
*
* Copyright (C) 2003-10 Eduard Martinescu
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <sys/param.h>
#include <sys/endian.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <err.h>
#include <errno.h>
#include <camlib.h>
#include <cam/scsi/scsi_message.h>
#include <cam/scsi/scsi_pass.h>
#if defined(__DragonFly__)
#include <sys/nata.h>
#else
#include <sys/ata.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <sys/uio.h>
#include <glob.h>
#include <stddef.h>
#include <paths.h>
#include <sys/utsname.h>
#include "config.h"
// set by /usr/include/sys/ata.h, suppress warning
#undef ATA_READ_LOG_EXT
#include "atacmds.h"
#include "scsicmds.h"
#include "cciss.h"
#include "utility.h"
#include "os_freebsd.h"
#include "dev_interface.h"
#include "dev_ata_cmd_set.h"
#include "dev_areca.h"
#define USBDEV "/dev/usb"
#if defined(__FreeBSD_version)
// This way we define one variable for the GNU/kFreeBSD and FreeBSD
#define FREEBSDVER __FreeBSD_version
#else
#define FREEBSDVER __FreeBSD_kernel_version
#endif
#if (FREEBSDVER >= 800000)
#include <libusb20_desc.h>
#include <libusb20.h>
#elif defined(__DragonFly__)
#include <bus/usb/usb.h>
#include <bus/usb/usbhid.h>
#else
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#endif
// based on "/sys/dev/nvme/nvme.h" from FreeBSD kernel sources
#include "freebsd_nvme_ioctl.h" // NVME_PASSTHROUGH_CMD, nvme_completion_is_error
#define CONTROLLER_3WARE_9000_CHAR 0x01
#define CONTROLLER_3WARE_678K_CHAR 0x02
#ifndef PATHINQ_SETTINGS_SIZE
#define PATHINQ_SETTINGS_SIZE 128
#endif
const char *os_XXXX_c_cvsid="$Id$" \
ATACMDS_H_CVSID CCISS_H_CVSID CONFIG_H_CVSID OS_FREEBSD_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID;
#define NO_RETURN 0
#define BAD_SMART 1
#define NO_DISK_3WARE 2
#define BAD_KERNEL 3
#define MAX_MSG 3
// Utility function for printing warnings
void printwarning(int msgNo, const char* extra) {
if (msgNo >= 0 && msgNo <= MAX_MSG) {
static int printed[] = {0,0,0,0};
if (!printed[msgNo]) {
static const char* message[]={
"The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n can not be retrieved with this version of ATAng, please do not rely on this value\nYou should update to at least 5.2\n",
"Error SMART Status command failed\nPlease get assistance from \n" PACKAGE_URL "\nRegister values returned from SMART Status command are:\n",
"You must specify a DISK # for 3ware drives with -d 3ware,<n> where <n> begins with 1 for first disk drive\n",
"ATA support is not provided for this kernel version. Please ugrade to a recent 5-CURRENT kernel (post 09/01/2003 or so)\n"
};
printed[msgNo] = 1;
pout("%s", message[msgNo]);
if (extra)
pout("%s",extra);
}
}
return;
}
// Interface to ATA devices behind 3ware escalade RAID controller cards. See os_linux.c
#define BUFFER_LEN_678K_CHAR ( sizeof(struct twe_usercommand) ) // 520
#define BUFFER_LEN_9000_CHAR ( sizeof(TW_OSLI_IOCTL_NO_DATA_BUF) + sizeof(TWE_Command) ) // 2048
#define TW_IOCTL_BUFFER_SIZE ( MAX(BUFFER_LEN_678K_CHAR, BUFFER_LEN_9000_CHAR) )
#ifndef ATA_DEVICE
#define ATA_DEVICE "/dev/ata"
#endif
#define ARGUSED(x) ((void)(x))
extern unsigned char failuretest_permissive;
/////////////////////////////////////////////////////////////////////////////
namespace os_freebsd { // No need to publish anything, name provided for Doxygen
/////////////////////////////////////////////////////////////////////////////
/// Implement shared open/close routines with old functions.
class freebsd_smart_device
: virtual public /*implements*/ smart_device
{
public:
explicit freebsd_smart_device()
: smart_device(never_called),
m_fd(-1) { }
virtual ~freebsd_smart_device();
virtual bool is_open() const;
virtual bool open();
virtual bool close();
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.
};
#ifdef __GLIBC__
static inline void * reallocf(void *ptr, size_t size) {
void *rv = realloc(ptr, size);
if((rv == NULL) && (size != 0))
free(ptr);
return rv;
}
#endif
freebsd_smart_device::~freebsd_smart_device()
{
if (m_fd >= 0)
os_freebsd::freebsd_smart_device::close();
}
// migration from the old_style
unsigned char m_controller_type;
unsigned char m_controller_port;
// examples for smartctl
static const char smartctl_examples[] =
"=================================================== SMARTCTL EXAMPLES =====\n\n"
" smartctl -a /dev/ad0 (Prints all SMART information)\n\n"
" smartctl --smart=on --offlineauto=on --saveauto=on /dev/ad0\n"
" (Enables SMART on first disk)\n\n"
" smartctl -t long /dev/ad0 (Executes extended disk self-test)\n\n"
" smartctl --attributes --log=selftest --quietmode=errorsonly /dev/ad0\n"
" (Prints Self-Test & Attribute errors)\n"
" (Prints Self-Test & Attribute errors)\n\n"
" smartctl -a --device=3ware,2 /dev/twa0\n"
" smartctl -a --device=3ware,2 /dev/twe0\n"
" smartctl -a --device=3ware,2 /dev/tws0\n"
" (Prints all SMART information for ATA disk on\n"
" third port of first 3ware RAID controller)\n"
" smartctl -a --device=cciss,0 /dev/ciss0\n"
" (Prints all SMART information for first disk \n"
" on Common Interface for SCSI-3 Support driver)\n"
" smartctl -a --device=areca,3/1 /dev/arcmsr0\n"
" (Prints all SMART information for 3rd disk in the 1st enclosure \n"
" on first ARECA RAID controller)\n"
" smartctl -a --device=megaraid,3 /dev/mrsas0\n"
" (Prints all SMART information for 3rd disk\n"
" on first LSI RAID controller)\n"
;
bool freebsd_smart_device::is_open() const
{
return (m_fd >= 0);
}
bool freebsd_smart_device::open()
{
const char *dev = get_dev_name();
if ((m_fd = ::open(dev,O_RDONLY))<0) {
set_err(errno);
return false;
}
return true;
}
bool freebsd_smart_device::close()
{
int failed = 0;
// close device, if open
if (is_open())
failed=::close(get_fd());
set_fd(-1);
if(failed) return false;
else return true;
}
/////////////////////////////////////////////////////////////////////////////
/// Implement standard ATA support
class freebsd_ata_device
: public /*implements*/ ata_device,
public /*extends*/ freebsd_smart_device
{
public:
freebsd_ata_device(smart_interface * intf, const char * dev_name, const char * req_type);
virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) override;
protected:
virtual int do_cmd(struct ata_ioc_request* request, bool is_48bit_cmd);
};
freebsd_ata_device::freebsd_ata_device(smart_interface * intf, const char * dev_name, const char * req_type)
: smart_device(intf, dev_name, "ata", req_type),
freebsd_smart_device()
{
}
int freebsd_ata_device::do_cmd( struct ata_ioc_request* request, bool is_48bit_cmd)
{
int fd = get_fd(), ret;
ARGUSED(is_48bit_cmd); // no support for 48 bit commands in the IOCATAREQUEST
ret = ioctl(fd, IOCATAREQUEST, request);
if (ret) set_err(errno);
return ret;
}
bool freebsd_ata_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out)
{
bool ata_48bit = false; // no ata_48bit_support via IOCATAREQUEST
if(!strcmp("atacam",get_dev_type())) // enable for atacam interface
ata_48bit = true;
if (!ata_cmd_is_ok(in,
true, // data_out_support
true, // multi_sector_support
ata_48bit)
) {
set_err(ENOSYS, "48-bit ATA commands not implemented for legacy controllers");
return false;
}
struct ata_ioc_request request;
memset(&request, 0, sizeof(struct ata_ioc_request));
request.timeout=SCSI_TIMEOUT_DEFAULT;
request.u.ata.command=in.in_regs.command;
request.u.ata.feature=in.in_regs.features;
request.u.ata.count = in.in_regs.sector_count_16;
request.u.ata.lba = in.in_regs.lba_48;
switch (in.direction) {
case ata_cmd_in::no_data:
request.flags=ATA_CMD_CONTROL;
break;
case ata_cmd_in::data_in:
request.flags=ATA_CMD_READ | ATA_CMD_CONTROL;
request.data=(char *)in.buffer;
request.count=in.size;
break;
case ata_cmd_in::data_out:
request.flags=ATA_CMD_WRITE | ATA_CMD_CONTROL;
request.data=(char *)in.buffer;
request.count=in.size;
break;
default:
return set_err(ENOSYS);
}
clear_err();
errno = 0;
if (do_cmd(&request, in.in_regs.is_48bit_cmd()))
return false;
if (request.error)
return set_err(EIO, "request failed, error code 0x%02x", request.error);
out.out_regs.error = request.error;
out.out_regs.sector_count_16 = request.u.ata.count;
out.out_regs.lba_48 = request.u.ata.lba;
return true;
}
#if FREEBSDVER > 800100
class freebsd_atacam_device : public freebsd_ata_device
{
public:
freebsd_atacam_device(smart_interface * intf, const char * dev_name, const char * req_type)
: smart_device(intf, dev_name, "atacam", req_type), freebsd_ata_device(intf, dev_name, req_type)
{}
virtual bool open();
virtual bool close();
protected:
int m_fd;
struct cam_device *m_camdev;
virtual int do_cmd( struct ata_ioc_request* request , bool is_48bit_cmd);
};
bool freebsd_atacam_device::open(){
const char *dev = get_dev_name();
if ((m_camdev = cam_open_device(dev, O_RDWR)) == NULL) {
set_err(errno);
return false;
}
set_fd(m_camdev->fd);
return true;
}
bool freebsd_atacam_device::close(){
cam_close_device(m_camdev);
set_fd(-1);
return true;
}
int freebsd_atacam_device::do_cmd( struct ata_ioc_request* request, bool is_48bit_cmd)
{
union ccb ccb;
int camflags;
// 48bit commands are broken in ATACAM before r242422/HEAD
// and may cause system hang
// First version with working support should be FreeBSD 9.2.0/RELEASE
#if (FREEBSDVER < 902001)
if(!strcmp("ata",m_camdev->sim_name) && is_48bit_cmd) {
set_err(ENOSYS, "48-bit ATA commands not implemented for legacy controllers");
return -1;
}
#endif
memset(&ccb, 0, sizeof(ccb));
if (request->count == 0)
camflags = CAM_DIR_NONE;
else if (request->flags & ATA_CMD_READ)
camflags = CAM_DIR_IN;
else
camflags = CAM_DIR_OUT;
cam_fill_ataio(&ccb.ataio,
0,
NULL,
camflags,
MSG_SIMPLE_Q_TAG,
(u_int8_t*)request->data,
request->count,
request->timeout * 1000); // timeout in seconds
ccb.ataio.cmd.flags = CAM_ATAIO_NEEDRESULT |
(is_48bit_cmd ? CAM_ATAIO_48BIT : 0);
// ata_28bit_cmd
ccb.ataio.cmd.command = request->u.ata.command;
ccb.ataio.cmd.features = request->u.ata.feature;
ccb.ataio.cmd.lba_low = request->u.ata.lba;
ccb.ataio.cmd.lba_mid = request->u.ata.lba >> 8;
ccb.ataio.cmd.lba_high = request->u.ata.lba >> 16;
// ata_48bit cmd
ccb.ataio.cmd.lba_low_exp = request->u.ata.lba >> 24;
ccb.ataio.cmd.lba_mid_exp = request->u.ata.lba >> 32;
ccb.ataio.cmd.lba_high_exp = request->u.ata.lba >> 40;
ccb.ataio.cmd.device = 0x40 | ((request->u.ata.lba >> 24) & 0x0f);
ccb.ataio.cmd.sector_count = request->u.ata.count;
ccb.ataio.cmd.sector_count_exp = request->u.ata.count >> 8;;
ccb.ccb_h.flags |= CAM_DEV_QFRZDIS;
if (cam_send_ccb(m_camdev, &ccb) < 0) {
set_err(EIO, "cam_send_ccb failed");
return -1;
}
if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
if(scsi_debugmode > 0)
cam_error_print(m_camdev, &ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
set_err(EIO);
return -1;
}
request->u.ata.lba =
((u_int64_t)(ccb.ataio.res.lba_low)) |
((u_int64_t)(ccb.ataio.res.lba_mid) << 8) |
((u_int64_t)(ccb.ataio.res.lba_high) << 16) |
((u_int64_t)(ccb.ataio.res.lba_low_exp) << 24) |
((u_int64_t)(ccb.ataio.res.lba_mid_exp) << 32) |
((u_int64_t)(ccb.ataio.res.lba_high_exp) << 40);
request->u.ata.count = ccb.ataio.res.sector_count | (ccb.ataio.res.sector_count_exp << 8);
request->error = ccb.ataio.res.error;
return 0;
}
#endif
/////////////////////////////////////////////////////////////////////////////
/// NVMe support
class freebsd_nvme_device
: public /*implements*/ nvme_device,
public /*extends*/ freebsd_smart_device
{
public:
freebsd_nvme_device(smart_interface * intf, const char * dev_name,
const char * req_type, unsigned nsid);
virtual bool open() override;
virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out) override;
};
freebsd_nvme_device::freebsd_nvme_device(smart_interface * intf, const char * dev_name,
const char * req_type, unsigned nsid)
: smart_device(intf, dev_name, "nvme", req_type),
nvme_device(nsid),
freebsd_smart_device()
{
}
bool freebsd_nvme_device::open()
{
const char *dev = get_dev_name();
if (!strnstr(dev, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
set_err(EINVAL, "NVMe controller controller/namespace ids must begin with '%s'",
NVME_CTRLR_PREFIX);
return false;
}
int nsid = -1, ctrlid = -1;
char tmp;
if(sscanf(dev, NVME_CTRLR_PREFIX"%d%c", &ctrlid, &tmp) == 1)
{
if(ctrlid < 0) {
set_err(EINVAL, "Invalid NVMe controller number");
return false;
}
nsid = 0xFFFFFFFF; // broadcast id
}
else if (sscanf(dev, NVME_CTRLR_PREFIX"%d" NVME_NS_PREFIX "%d%c",
&ctrlid, &nsid, &tmp) == 2)
{
if(ctrlid < 0 || nsid < 0) {
set_err(EINVAL, "Invalid NVMe controller/namespace number");
return false;
}
}
else {
set_err(EINVAL, "Invalid NVMe controller/namespace syntax");
return false;
}
// we should always open controller, not namespace device
char full_path[64];
snprintf(full_path, sizeof(full_path), NVME_CTRLR_PREFIX"%d", ctrlid);
int fd;
if ((fd = ::open(full_path, O_RDWR))<0) {
set_err(errno);
return false;
}
set_fd(fd);
if (!get_nsid()) {
set_nsid(nsid);
}
return true;
}
bool freebsd_nvme_device::nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out)
{
// nvme_passthru_cmd pt;
struct nvme_pt_command pt;
struct nvme_completion *cp_p;
memset(&pt, 0, sizeof(pt));
#if __FreeBSD_version >= 1200058 && __FreeBSD_version < 1200081
pt.cmd.opc_fuse = NVME_CMD_SET_OPC(in.opcode);
#else
pt.cmd.opc = in.opcode;
#endif
pt.cmd.nsid = htole32(in.nsid);
pt.buf = in.buffer;
pt.len = in.size;
pt.cmd.cdw10 = htole32(in.cdw10);
pt.cmd.cdw11 = htole32(in.cdw11);
pt.cmd.cdw12 = htole32(in.cdw12);
pt.cmd.cdw13 = htole32(in.cdw13);
pt.cmd.cdw14 = htole32(in.cdw14);
pt.cmd.cdw15 = htole32(in.cdw15);
pt.is_read = 1; // should we use in.direction()?
int status = ioctl(get_fd(), NVME_PASSTHROUGH_CMD, &pt);
if (status < 0)
return set_err(errno, "NVME_PASSTHROUGH_CMD: %s", strerror(errno));
#if __FreeBSD_version >= 1200058
nvme_completion_swapbytes(&pt.cpl);
#endif
cp_p = &pt.cpl;
out.result=cp_p->cdw0; // Command specific result (DW0)
if (nvme_completion_is_error(cp_p)) { /* ignore DNR and More bits */
return set_nvme_err(out, nvme_completion_is_error(&pt.cpl));
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
/// Implement AMCC/3ware RAID support
class freebsd_escalade_device
: public /*implements*/ ata_device,
public /*extends*/ freebsd_smart_device
{
public:
freebsd_escalade_device(smart_interface * intf, const char * dev_name,
int escalade_type, int disknum);
protected:
virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) override;
virtual bool open() override;
private:
int m_escalade_type; ///< Type string for escalade_command_interface().
int m_disknum; ///< Disk number.
};
freebsd_escalade_device::freebsd_escalade_device(smart_interface * intf, const char * dev_name,
int escalade_type, int disknum)
: smart_device(intf, dev_name, "3ware", "3ware"),
freebsd_smart_device(),
m_escalade_type(escalade_type), m_disknum(disknum)
{
set_info().info_name = strprintf("%s [3ware_disk_%02d]", dev_name, disknum);
}
bool freebsd_escalade_device::open()
{
const char *dev = get_dev_name();
int fd;
if ((fd = ::open(dev,O_RDWR))<0) {
set_err(errno);
return false;
}
set_fd(fd);
return true;
}
bool freebsd_escalade_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out)
{
// to hold true file descriptor
int fd = get_fd();
if (!ata_cmd_is_ok(in,
true, // data_out_support
false, // TODO: multi_sector_support
true) // ata_48bit_support
)
return false;
struct twe_usercommand* cmd_twe = NULL;
TW_OSLI_IOCTL_NO_DATA_BUF* cmd_twa = NULL;
TWE_Command_ATA* ata = NULL;
// Used by both the SCSI and char interfaces
char ioctl_buffer[TW_IOCTL_BUFFER_SIZE];
if (m_disknum < 0) {
printwarning(NO_DISK_3WARE,NULL);
return false;
}
memset(ioctl_buffer, 0, TW_IOCTL_BUFFER_SIZE);
if (m_escalade_type==CONTROLLER_3WARE_9000_CHAR) {
cmd_twa = (TW_OSLI_IOCTL_NO_DATA_BUF*)ioctl_buffer;
cmd_twa->pdata = ((TW_OSLI_IOCTL_WITH_PAYLOAD*)cmd_twa)->payload.data_buf;
cmd_twa->driver_pkt.buffer_length = in.size;
// using "old" packet format to speak with SATA devices
ata = (TWE_Command_ATA*)&cmd_twa->cmd_pkt.command.cmd_pkt_7k;
} else if (m_escalade_type==CONTROLLER_3WARE_678K_CHAR) {
cmd_twe = (struct twe_usercommand*)ioctl_buffer;
ata = &cmd_twe->tu_command.ata;
} else {
return set_err(ENOSYS,
"Unrecognized escalade_type %d in linux_3ware_command_interface(disk %d)\n"
"Please contact " PACKAGE_BUGREPORT "\n", (int)m_escalade_type, m_disknum);
}
ata->opcode = TWE_OP_ATA_PASSTHROUGH;
// Same for (almost) all commands - but some reset below
ata->request_id = 0xFF;
ata->unit = m_disknum;
ata->status = 0;
ata->flags = 0x1;
ata->size = 0x5; // TODO: multisector support
// Set registers
{
const ata_in_regs_48bit & r = in.in_regs;
ata->features = r.features_16;
ata->sector_count = r.sector_count_16;
ata->sector_num = r.lba_low_16;
ata->cylinder_lo = r.lba_mid_16;
ata->cylinder_hi = r.lba_high_16;
ata->drive_head = r.device;
ata->command = r.command;
}
// Is this a command that reads or returns 512 bytes?
// passthru->param values are:
// 0x0 - non data command without TFR write check,
// 0x8 - non data command with TFR write check,
// 0xD - data command that returns data to host from device
// 0xF - data command that writes data from host to device
// passthru->size values are 0x5 for non-data and 0x07 for data
bool readdata = false;
if (in.direction == ata_cmd_in::data_in) {
if (m_escalade_type==CONTROLLER_3WARE_678K_CHAR) {
cmd_twe->tu_data = in.buffer;
cmd_twe->tu_size = 512;
}
readdata=true;
ata->sgl_offset = 0x5;
ata->param = 0xD;
// For 64-bit to work correctly, up the size of the command packet
// in dwords by 1 to account for the 64-bit single sgl 'address'
// field. Note that this doesn't agree with the typedefs but it's
// right (agree with kernel driver behavior/typedefs).
// if (sizeof(long)==8)
// ata->size++;
}
else if (in.direction == ata_cmd_in::no_data) {
// Non data command -- but doesn't use large sector
// count register values.
ata->sgl_offset = 0x0;
ata->param = 0x8;
ata->sector_count = 0x0;
}
else if (in.direction == ata_cmd_in::data_out) {
ata->sgl_offset = 0x5;
ata->param = 0xF; // PIO data write
if (m_escalade_type==CONTROLLER_3WARE_678K_CHAR) {
cmd_twe->tu_data = in.buffer;
cmd_twe->tu_size = 512;
}
else if (m_escalade_type==CONTROLLER_3WARE_9000_CHAR) {
memcpy(cmd_twa->pdata, in.buffer, in.size);
}
}
else
return set_err(EINVAL);
// 3WARE controller can NOT have packet device internally
if (in.in_regs.command == ATA_IDENTIFY_PACKET_DEVICE) {
return set_err(ENODEV, "No drive on port %d", m_disknum);
}
// Now send the command down through an ioctl()
int ioctlreturn;
if (m_escalade_type==CONTROLLER_3WARE_9000_CHAR) {
ioctlreturn=ioctl(fd,TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH,cmd_twa);
} else {
ioctlreturn=ioctl(fd,TWEIO_COMMAND,cmd_twe);
}
// Deal with the different error cases
if (ioctlreturn) {
return set_err(EIO);
}
// See if the ATA command failed. Now that we have returned from
// the ioctl() call, if passthru is valid, then:
// - ata->status contains the 3ware controller STATUS
// - ata->command contains the ATA STATUS register
// - ata->features contains the ATA ERROR register
//
// Check bits 0 (error bit) and 5 (device fault) of the ATA STATUS
// If bit 0 (error bit) is set, then ATA ERROR register is valid.
// While we *might* decode the ATA ERROR register, at the moment it
// doesn't make much sense: we don't care in detail why the error
// happened.
if (ata->status || (ata->command & 0x21)) {
if (scsi_debugmode)
pout("Command failed, ata.status=(0x%2.2x), ata.command=(0x%2.2x), ata.flags=(0x%2.2x)\n",ata->status,ata->command,ata->flags);
return set_err(EIO);
}
// If this is a read data command, copy data to output buffer
if (readdata) {
if (m_escalade_type==CONTROLLER_3WARE_9000_CHAR)
memcpy(in.buffer, cmd_twa->pdata, in.size);
else if(m_escalade_type==CONTROLLER_3WARE_678K_CHAR) {
memcpy(in.buffer, cmd_twe->tu_data, in.size); // untested
}
}
// Return register values
if (ata) {
ata_out_regs_48bit & r = out.out_regs;
r.error = ata->features;
r.sector_count_16 = ata->sector_count;
r.lba_low_16 = ata->sector_num;
r.lba_mid_16 = ata->cylinder_lo;
r.lba_high_16 = ata->cylinder_hi;
r.device = ata->drive_head;
r.status = ata->command;
}
// look for nonexistent devices/ports
if (in.in_regs.command == ATA_IDENTIFY_DEVICE
&& !nonempty((unsigned char *)in.buffer, in.size)) {
return set_err(ENODEV, "No drive on port %d", m_disknum);
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
/// LSI MegaRAID support
class freebsd_megaraid_device
: public /* implements */ scsi_device,
public /* extends */ freebsd_smart_device
{
public:
freebsd_megaraid_device(smart_interface *intf, const char *name,
unsigned int tgt);
virtual ~freebsd_megaraid_device();
virtual smart_device * autodetect_open() override;
virtual bool open() override;
virtual bool close() override;
virtual bool scsi_pass_through(scsi_cmnd_io *iop) override;
private:
unsigned int m_disknum;
unsigned int m_hba;
int m_fd;
bool (freebsd_megaraid_device::*pt_cmd)(int cdblen, void *cdb, int dataLen, void *data,
int senseLen, void *sense, int report, int direction, int timeout);
bool megasas_cmd(int cdbLen, void *cdb, int dataLen, void *data,
int senseLen, void *sense, int report, int direction, int timeout);
};
freebsd_megaraid_device::freebsd_megaraid_device(smart_interface *intf,
const char *dev_name, unsigned int tgt)
: smart_device(intf, dev_name, "megaraid", "megaraid"),
freebsd_smart_device(),
m_disknum(tgt), m_hba(0),
m_fd(-1), pt_cmd(0)
{
set_info().info_name = strprintf("%s [megaraid_disk_%02d]", dev_name, m_disknum);
set_info().dev_type = strprintf("megaraid,%d", tgt);
}
freebsd_megaraid_device::~freebsd_megaraid_device()
{
if (m_fd >= 0)
::close(m_fd);
}
smart_device * freebsd_megaraid_device::autodetect_open()
{
int report = scsi_debugmode;
// Open device
if (!open())
return this;
// The code below is based on smartd.cpp:SCSIFilterKnown()
if (strcmp(get_req_type(), "megaraid"))
return this;
// Get INQUIRY
unsigned char req_buff[64] = {0, };
int req_len = 36;
if (scsiStdInquiry(this, req_buff, req_len)) {
close();
set_err(EIO, "INQUIRY failed");
return this;
}
int avail_len = req_buff[4] + 5;
int len = (avail_len < req_len ? avail_len : req_len);
if (len < 36)
return this;
if (report)
pout("Got MegaRAID inquiry.. %s\n", req_buff+8);
// Use INQUIRY to detect type
{
// SAT?
ata_device * newdev = smi()->autodetect_sat_device(this, req_buff, len);
if (newdev) // NOTE: 'this' is now owned by '*newdev'
return newdev;
}
// Nothing special found
return this;
}
bool freebsd_megaraid_device::open()
{
/* Open Device IOCTL node */
if ((m_fd = ::open(get_dev_name(), O_RDWR)) >= 0) {
pt_cmd = &freebsd_megaraid_device::megasas_cmd;
}
else {
int err = errno;
freebsd_smart_device::close();
return set_err(err, "cannot open %s",get_dev_name());
}
set_fd(m_fd);
return true;
}
bool freebsd_megaraid_device::close()
{
if (m_fd >= 0)
::close(m_fd);
m_fd = -1; m_hba = 0; pt_cmd = 0;
set_fd(m_fd);
return true;
}
bool freebsd_megaraid_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);
}
// Controller rejects Test Unit Ready
if (iop->cmnd[0] == 0x00)
return true;
if (iop->cmnd[0] == SAT_ATA_PASSTHROUGH_12 || iop->cmnd[0] == SAT_ATA_PASSTHROUGH_16) {
// Controller does not return ATA output registers in SAT sense data
if (iop->cmnd[2] & (1 << 5)) // chk_cond
return set_err(ENOSYS, "ATA return descriptor not supported by controller firmware");
}
// SMART WRITE LOG SECTOR causing media errors
if ((iop->cmnd[0] == SAT_ATA_PASSTHROUGH_16 // SAT16 WRITE LOG
&& iop->cmnd[14] == ATA_SMART_CMD && iop->cmnd[3]==0 && iop->cmnd[4] == ATA_SMART_WRITE_LOG_SECTOR) ||
(iop->cmnd[0] == SAT_ATA_PASSTHROUGH_12 // SAT12 WRITE LOG
&& iop->cmnd[9] == ATA_SMART_CMD && iop->cmnd[3] == ATA_SMART_WRITE_LOG_SECTOR))
{
if(!failuretest_permissive)
return set_err(ENOSYS, "SMART WRITE LOG SECTOR may cause problems, try with -T permissive to force");
}
if (pt_cmd == NULL)
return false;
return (this->*pt_cmd)(iop->cmnd_len, iop->cmnd,
iop->dxfer_len, iop->dxferp,
iop->max_sense_len, iop->sensep, report, iop->dxfer_dir, iop->timeout);
}
bool freebsd_megaraid_device::megasas_cmd(int cdbLen, void *cdb,
int dataLen, void *data,
int senseLen, void * sense, int /*report*/, int dxfer_dir, int timeout)
{
struct mfi_pass_frame * pthru;
struct mfi_ioc_packet uio;
pthru = (struct mfi_pass_frame *)&uio.mfi_frame.raw;
memset(&uio, 0, sizeof(uio));
pthru->header.cmd = MFI_CMD_PD_SCSI_IO;
pthru->header.cmd_status = 0;
pthru->header.scsi_status = 0x0;
pthru->header.target_id = m_disknum;
pthru->header.lun_id = 0; // FIXME, should be bus number?
pthru->header.sense_len = senseLen;
pthru->sense_addr_lo = (uintptr_t)sense ;
pthru->sense_addr_hi = (uintptr_t)((uint64_t)sense >> 32);
pthru->header.cdb_len = cdbLen;
pthru->header.timeout = timeout;
switch (dxfer_dir) {
case DXFER_FROM_DEVICE:
pthru->header.flags = MFI_FRAME_DIR_READ;
break;
case DXFER_TO_DEVICE:
pthru->header.flags = MFI_FRAME_DIR_WRITE;
break;
case DXFER_NONE:
pthru->header.flags = MFI_FRAME_DIR_NONE;
break;
}
if (dataLen > 0) {
uio.mfi_sge_count = 1;
uio.mfi_sgl_off = offsetof(struct mfi_pass_frame,sgl);
uio.mfi_sgl[0].iov_base = data;
uio.mfi_sgl[0].iov_len = dataLen;
pthru->header.sg_count = 1;
pthru->header.data_len = dataLen;
// tested on amd64 kernel in native and 32bit mode
pthru->sgl.sg64[0].addr = (intptr_t)data;
pthru->sgl.sg64[0].len = (uint32_t)dataLen;
}
memcpy(pthru->cdb, cdb, cdbLen);
uio.mfi_adapter_no = m_hba;
uio.mfi_sense_len = senseLen;
uio.mfi_sense_off = offsetof(struct mfi_pass_frame, sense_addr_lo);
errno = 0;
int rc = ioctl(m_fd, MFI_CMD, &uio);
if (pthru->header.cmd_status || rc != 0) {
if (pthru->header.cmd_status == 12) {
return set_err(EIO, "megasas_cmd: Device %d does not exist\n", m_disknum);
}
return set_err((errno ? errno : EIO), "megasas_cmd result: %d.%d = %d/%d",
m_hba, m_disknum, errno,
pthru->header.cmd_status);
}
return true;
}