/*
* os_freebsd.c
*
- * Home page of code is: http://smartmontools.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
*
- * Copyright (C) 2003-10 Eduard Martinescu <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2003-10 Eduard Martinescu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include "config.h"
#include "int64.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 <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
#define PATHINQ_SETTINGS_SIZE 128
#endif
-const char *os_XXXX_c_cvsid="$Id: os_freebsd.cpp 3902 2014-05-23 19:14:15Z samm2 $" \
+const char *os_XXXX_c_cvsid="$Id: os_freebsd.cpp 4257 2016-03-27 23:32:54Z samm2 $" \
ATACMDS_H_CVSID CCISS_H_CVSID CONFIG_H_CVSID INT64_H_CVSID OS_FREEBSD_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID;
#define NO_RETURN 0
// Utility function for printing warnings
void printwarning(int msgNo, const char* extra) {
- static int printed[] = {0,0,0,0};
- 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_HOMEPAGE "\nRegister values returned from SMART Status command are:\n",
+ if (msgNo >= 0 && msgNo <= MAX_MSG) {
+ static int printed[] = {0,0,0,0};
+ if (!printed[msgNo]) {
- "You must specify a DISK # for 3ware drives with -d 3ware,<n> where <n> begins with 1 for first disk drive\n",
+ 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",
- "ATA support is not provided for this kernel version. Please ugrade to a recent 5-CURRENT kernel (post 09/01/2003 or so)\n"
- };
+ "Error SMART Status command failed\nPlease get assistance from \n" PACKAGE_HOMEPAGE "\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"
+ };
- if (msgNo >= 0 && msgNo <= MAX_MSG) {
- if (!printed[msgNo]) {
printed[msgNo] = 1;
pout("%s", message[msgNo]);
if (extra)
#define ARGUSED(x) ((void)(x))
-// global variable holding byte count of allocated memory
-long long bytes;
extern unsigned char failuretest_permissive;
/////////////////////////////////////////////////////////////////////////////
#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();
+
+ virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out);
+};
+
+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;
+ memset(&pt, 0, sizeof(pt));
+
+ pt.cmd.opc = in.opcode;
+ pt.cmd.nsid = in.nsid;
+ pt.buf = in.buffer;
+ pt.len = in.size;
+ pt.cmd.cdw10 = in.cdw10;
+ pt.cmd.cdw11 = in.cdw11;
+ pt.cmd.cdw12 = in.cdw12;
+ pt.cmd.cdw13 = in.cdw13;
+ pt.cmd.cdw14 = in.cdw14;
+ pt.cmd.cdw15 = 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));
+
+ out.result=pt.cpl.cdw0; // Command specific result (DW0)
+
+ if (nvme_completion_is_error(&pt.cpl))
+ return set_nvme_err(out, nvme_completion_is_error(&pt.cpl));
+
+ return true;
+}
+
/////////////////////////////////////////////////////////////////////////////
/// Implement AMCC/3ware RAID support
freebsd_scsi_device::freebsd_scsi_device(smart_interface * intf,
const char * dev_name, const char * req_type)
: smart_device(intf, dev_name, "scsi", req_type),
- freebsd_smart_device()
+ freebsd_smart_device(),
+ m_camdev(0)
{
}
smart_device * freebsd_areca_ata_device::autodetect_open()
{
- int is_ata = 1;
-
// autodetect device type
- is_ata = arcmsr_get_dev_type();
+ int is_ata = arcmsr_get_dev_type();
if(is_ata < 0)
{
set_err(EIO);
#endif
virtual scsi_device * get_scsi_device(const char * name, const char * type);
+
+ virtual nvme_device * get_nvme_device(const char * name, const char * type,
+ unsigned nsid);
virtual smart_device * autodetect_smart_device(const char * name);
return new freebsd_scsi_device(this, name, type);
}
+nvme_device * freebsd_smart_interface::get_nvme_device(const char * name, const char * type,
+ unsigned nsid)
+{
+ return new freebsd_nvme_device(this, name, type, nsid);
+}
+
// we are using CAM subsystem XPT enumerator to found all CAM (scsi/usb/ada/...)
// devices on system despite of it's names
//
}
for (unsigned i = 0; i < ccb.cdm.num_matches; i++) {
- struct bus_match_result *bus_result;
struct device_match_result *dev_result;
struct periph_match_result *periph_result;
if (ccb.cdm.matches[i].type == DEV_MATCH_BUS) {
+ struct bus_match_result *bus_result;
+
bus_result = &ccb.cdm.matches[i].result.bus_result;
if (strcmp(bus_result->dev_name,"xpt") == 0) /* skip XPT bus at all */
n = -1;
goto end;
};
- bytes+=1+strlen(mp[n]);
n++;
};
};
- };
+ };
+ if (n <= 0)
+ goto end;
mp = (char **)reallocf(mp,n*(sizeof (char*))); // shrink to correct size
- if (mp == NULL && n > 0 ) { // reallocf never fail for size=0, but may return NULL
+ if (mp == NULL) {
serrno=errno;
pout("Out of memory constructing scan device list (on line %d)\n", __LINE__);
n = -1;
goto end;
};
- bytes += (n)*(sizeof(char*)); // and set allocated byte count
end:
if (fd>=0)
return false;
#else // freebsd < 8.0 USB stack, ioctl interface
- int i, f, a, rc;
+ int i, a, rc;
char buf[50];
int ncont;
for (ncont = 0, i = 0; i < 10; i++) {
snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
- f = open(buf, O_RDONLY);
+ int f = open(buf, O_RDONLY);
if (f >= 0) {
memset(done, 0, sizeof done);
for (a = 1; a < USB_MAX_DEVICES; a++) {
struct cam_device *cam_dev;
union ccb ccb;
int bus=-1;
- int i,c;
- int len;
+ int i;
const char * test_name = name;
// if dev_name null, or string length zero
- if (!name || !(len = strlen(name)))
+ if (!name || !*name)
return 0;
// Dereference symlinks
// check ATA/ATAPI devices
for (i = 0; i < numata; i++) {
if(!strcmp(atanames[i],test_name)) {
- for (c = i; c < numata; c++) free(atanames[c]);
+ for (int c = i; c < numata; c++) free(atanames[c]);
free(atanames);
return new freebsd_ata_device(this, test_name, "");
}
// device is LSI raid supported by mfi driver
if(!strncmp("/dev/mfid", test_name, strlen("/dev/mfid")))
set_err(EINVAL, "To monitor disks on LSI RAID load mfip.ko module and run 'smartctl -a /dev/passX' to show SMART information");
+
+ // form /dev/nvme* or nvme*
+ if(!strncmp("/dev/nvme", test_name, strlen("/dev/nvme")))
+ return new freebsd_nvme_device(this, name, "", 0 /* use default nsid */);
+
// device type unknown
return 0;
}
smart_device * freebsd_smart_interface::get_custom_smart_device(const char * name, const char * type)
{
- // 3Ware ?
- static const char * fbsd_dev_twe_ctrl = "/dev/twe";
- static const char * fbsd_dev_twa_ctrl = "/dev/twa";
- static const char * fbsd_dev_tws_ctrl = "/dev/tws";
- int disknum = -1, n1 = -1, n2 = -1, contr = -1;
+ int disknum = -1, n1 = -1, n2 = -1;
if (sscanf(type, "3ware,%n%d%n", &n1, &disknum, &n2) == 1 || n1 == 6) {
+ // 3Ware ?
+ static const char * fbsd_dev_twe_ctrl = "/dev/twe";
+ static const char * fbsd_dev_twa_ctrl = "/dev/twa";
+ static const char * fbsd_dev_tws_ctrl = "/dev/tws";
+ int contr = -1;
+
if (n2 != (int)strlen(type)) {
set_err(EINVAL, "Option -d 3ware,N requires N to be a non-negative integer");
return 0;