--- old/usr/src/uts/common/io/comstar/lu/stmf_sbd/sbd.c	Wed Feb  9 23:37:28 2011
+++ new/usr/src/uts/common/io/comstar/lu/stmf_sbd/sbd.c	Wed Feb  9 23:37:28 2011
@@ -20,6 +20,8 @@
  */
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  */
 
 #include <sys/conf.h>
@@ -83,7 +85,8 @@
     uint32_t *err_ret);
 int sbd_get_lu_props(sbd_lu_props_t *islp, uint32_t islp_sz,
     sbd_lu_props_t *oslp, uint32_t oslp_sz, uint32_t *err_ret);
-char *sbd_get_zvol_name(sbd_lu_t *sl);
+static int sbd_get_unmap_props(sbd_unmap_props_t *sup, sbd_unmap_props_t *osup,
+    uint32_t *err_ret);
 sbd_status_t sbd_create_zfs_meta_object(sbd_lu_t *sl);
 sbd_status_t sbd_open_zfs_meta(sbd_lu_t *sl);
 sbd_status_t sbd_read_zfs_meta(sbd_lu_t *sl, uint8_t *buf, uint64_t sz,
@@ -96,6 +99,8 @@
 int sbd_zvolset(char *zvol_name, char *comstarprop);
 char sbd_ctoi(char c);
 void sbd_close_lu(sbd_lu_t *sl);
+static nvlist_t *sbd_zvol_get_props(char *zvol_name);
+static int sbd_zvol_set_props(char *zvol_name, nvlist_t *nv);
 
 static ldi_ident_t	sbd_zfs_ident;
 static stmf_lu_provider_t *sbd_lp;
@@ -448,6 +453,18 @@
 		ret = 0;
 		iocd->stmf_error = 0;
 		break;
+	case SBD_IOCTL_GET_UNMAP_PROPS:
+		if (iocd->stmf_ibuf_size < sizeof (sbd_unmap_props_t)) {
+			ret = EFAULT;
+			break;
+		}
+		if (iocd->stmf_obuf_size < sizeof (sbd_unmap_props_t)) {
+			ret = EINVAL;
+			break;
+		}
+		ret = sbd_get_unmap_props((sbd_unmap_props_t *)ibuf,
+		    (sbd_unmap_props_t *)obuf, &iocd->stmf_error);
+		break;
 	default:
 		ret = ENOTTY;
 	}
@@ -1372,6 +1389,69 @@
 	return (ret);
 }
 
+/*
+ * Will scribble SL_UNMAP_ENABLED into sl_flags if we succeed.
+ */
+static void
+do_unmap_setup(sbd_lu_t *sl)
+{
+	char *file = NULL;
+	nvlist_t *nv = NULL, *nv2 = NULL;
+	boolean_t already_compressing = B_FALSE;
+	uint64_t val;
+
+	ASSERT((sl->sl_flags & SL_UNMAP_ENABLED) == 0);
+
+	if ((sl->sl_flags & SL_ZFS_META) == 0)
+		return;	/* No UNMAP for you. */
+
+	file = sbd_get_zvol_name(sl);
+	if (file == NULL) {
+		cmn_err(CE_WARN, "sbd has zfs meta but no zvol name");
+		return;	/* No UNMAP for you. */
+	}
+	nv = sbd_zvol_get_props(file);
+	if (nv == NULL) {
+		cmn_err(CE_WARN, "unable to get zvol props");
+		kmem_free(file, strlen(file) + 1);
+		return;	/* No UNMAP for you. */
+	}
+
+	/* See if compression is supported, but turned off on this dataset. */
+	if (nvlist_lookup_nvlist(nv, "compression", &nv2) == 0 &&
+	    nvlist_lookup_uint64(nv2, ZPROP_VALUE, &val) == 0) {
+		already_compressing = (val != ZIO_COMPRESS_OFF);
+	} else {
+		cmn_err(CE_WARN, "prop lookup for compression failed");
+	}
+
+	/*
+	 * Check refreservation because no refreservation means this is a
+	 * sparse zvol and we can enable SCSI unmap.
+	 */
+	if (nvlist_lookup_nvlist(nv, "refreservation", &nv2) != 0) {
+		cmn_err(CE_WARN, "lookup for refreservation nvlist failed");
+	} else if (nvlist_lookup_uint64(nv2, ZPROP_VALUE, &val) != 0) {
+		cmn_err(CE_WARN, "prop lookup for refreservation failed");
+	} else if (val == 0) {
+		sl->sl_flags |= SL_UNMAP_ENABLED;
+		if (!already_compressing) {
+			nvlist_free(nv);
+			nv = NULL;
+			if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0 ||
+			    nvlist_add_uint64(nv, "compression",
+			    ZIO_COMPRESS_ZLE) != 0 ||
+			    sbd_zvol_set_props(file, nv)) {
+				cmn_err(CE_WARN, "Setting zle compression "
+				    "failed for zvol %s", file);
+			}
+		}
+	}
+
+	nvlist_free(nv);
+	kmem_free(file, strlen(file) + 1);
+}
+
 int
 sbd_populate_and_register_lu(sbd_lu_t *sl, uint32_t *err_ret)
 {
@@ -1378,6 +1458,8 @@
 	stmf_lu_t *lu = sl->sl_lu;
 	stmf_status_t ret;
 
+	do_unmap_setup(sl);
+
 	lu->lu_id = (scsi_devid_desc_t *)sl->sl_device_id;
 	if (sl->sl_alias) {
 		lu->lu_alias = sl->sl_alias;
@@ -3132,6 +3214,46 @@
 	return (0);
 }
 
+static int
+sbd_get_unmap_props(sbd_unmap_props_t *sup,
+    sbd_unmap_props_t *osup, uint32_t *err_ret)
+{
+	sbd_status_t sret;
+	sbd_lu_t *sl = NULL;
+
+	if (sup->sup_guid_valid) {
+		sret = sbd_find_and_lock_lu(sup->sup_guid,
+		    NULL, SL_OP_LU_PROPS, &sl);
+	} else {
+		sret = sbd_find_and_lock_lu(NULL,
+		    (uint8_t *)sup->sup_zvol_path, SL_OP_LU_PROPS,
+		    &sl);
+	}
+	if (sret != SBD_SUCCESS) {
+		if (sret == SBD_BUSY) {
+			*err_ret = SBD_RET_LU_BUSY;
+			return (EBUSY);
+		} else if (sret == SBD_NOT_FOUND) {
+			*err_ret = SBD_RET_NOT_FOUND;
+			return (ENOENT);
+		}
+		return (EIO);
+	}
+
+	sup->sup_found_lu = 1;
+	sup->sup_guid_valid = 1;
+	bcopy(sl->sl_device_id + 4, sup->sup_guid, 16);
+	if (sl->sl_flags & SL_UNMAP_ENABLED)
+		sup->sup_unmap_enabled = 1;
+	else
+		sup->sup_unmap_enabled = 0;
+
+	*osup = *sup;
+	sl->sl_trans_op = SL_OP_NONE;
+
+	return (0);
+}
+
 int
 sbd_get_lu_props(sbd_lu_props_t *islp, uint32_t islp_sz,
     sbd_lu_props_t *oslp, uint32_t oslp_sz, uint32_t *err_ret)
@@ -3622,4 +3744,88 @@
 	nvlist_free(nv);
 	(void) ldi_close(zfs_lh, FREAD|FWRITE, kcred);
 	return (rc);
+}
+
+static nvlist_t *
+sbd_zvol_get_props(char *zvol_name)
+{
+	ldi_handle_t	zfs_lh;
+	nvlist_t	*nv = NULL;
+	zfs_cmd_t	*zc;
+	int size = 4096;
+	int unused;
+	int rc;
+
+	if ((rc = ldi_open_by_name("/dev/zfs", FREAD | FWRITE, kcred,
+	    &zfs_lh, sbd_zfs_ident)) != 0) {
+		cmn_err(CE_WARN, "ldi_open %d", rc);
+		return (NULL);
+	}
+
+	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
+	(void) strlcpy(zc->zc_name, zvol_name, sizeof (zc->zc_name));
+szg_again:
+	zc->zc_nvlist_dst = (uint64_t)(intptr_t)kmem_alloc(size,
+	    KM_SLEEP);
+	zc->zc_nvlist_dst_size = size;
+	rc = ldi_ioctl(zfs_lh, ZFS_IOC_OBJSET_STATS, (intptr_t)zc,
+	    FKIOCTL, kcred, &unused);
+	/*
+	 * ENOMEM means the list is larger than what we've allocated
+	 * ldi_ioctl will fail with ENOMEM only once
+	 */
+	if (rc == ENOMEM) {
+		int newsize;
+		newsize = zc->zc_nvlist_dst_size;
+		kmem_free((void *)(uintptr_t)zc->zc_nvlist_dst, size);
+		size = newsize;
+		goto szg_again;
+	} else if (rc != 0) {
+		goto szg_out;
+	}
+	rc = nvlist_unpack((char *)(uintptr_t)zc->zc_nvlist_dst,
+	    zc->zc_nvlist_dst_size, &nv, 0);
+	ASSERT(rc == 0);	/* nvlist_unpack should not fail */
+szg_out:
+	kmem_free((void *)(uintptr_t)zc->zc_nvlist_dst, size);
+	kmem_free(zc, sizeof (zfs_cmd_t));
+	(void) ldi_close(zfs_lh, FREAD|FWRITE, kcred);
+
+	return (nv);
+}
+
+static int
+sbd_zvol_set_props(char *zvol_name, nvlist_t *nv)
+{
+	ldi_handle_t	zfs_lh;
+	char		*packed = NULL;
+	size_t		len;
+	zfs_cmd_t	*zc;
+	int unused;
+	int rc;
+
+	if ((rc = ldi_open_by_name("/dev/zfs", FREAD | FWRITE, kcred,
+	    &zfs_lh, sbd_zfs_ident)) != 0) {
+		cmn_err(CE_WARN, "ldi_open %d", rc);
+		return (ENXIO);
+	}
+	if ((rc = nvlist_pack(nv, &packed, &len, NV_ENCODE_NATIVE, KM_SLEEP))) {
+		goto szs_out;
+	}
+
+	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
+	(void) strlcpy(zc->zc_name, zvol_name, sizeof (zc->zc_name));
+	zc->zc_nvlist_src = (uint64_t)(intptr_t)packed;
+	zc->zc_nvlist_src_size = len;
+	rc = ldi_ioctl(zfs_lh, ZFS_IOC_SET_PROP, (intptr_t)zc,
+	    FKIOCTL, kcred, &unused);
+	if (rc != 0) {
+		cmn_err(CE_NOTE, "ioctl failed %d", rc);
+	}
+	kmem_free(zc, sizeof (zfs_cmd_t));
+	if (packed)
+		kmem_free(packed, len);
+szs_out:
+	(void) ldi_close(zfs_lh, FREAD|FWRITE, kcred);
+	return (rc);
 }
--- old/usr/src/uts/common/io/comstar/lu/stmf_sbd/sbd_scsi.c	Wed Feb  9 23:37:29 2011
+++ new/usr/src/uts/common/io/comstar/lu/stmf_sbd/sbd_scsi.c	Wed Feb  9 23:37:29 2011
@@ -20,6 +20,8 @@
  */
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  */
 
 #include <sys/conf.h>
@@ -36,6 +38,11 @@
 #include <sys/sdt.h>
 #include <sys/dkio.h>
 
+#include <sys/dmu.h>
+#include <sys/txg.h>
+#include <sys/refcount.h>
+#include <sys/zvol.h>
+
 #include <sys/stmf.h>
 #include <sys/lpif.h>
 #include <sys/portif.h>
@@ -98,6 +105,12 @@
 void sbd_handle_mode_select(scsi_task_t *task, stmf_data_buf_t *dbuf);
 void sbd_handle_identifying_info(scsi_task_t *task, stmf_data_buf_t *dbuf);
 
+static void sbd_handle_unmap_xfer(scsi_task_t *task, uint8_t *buf,
+    uint32_t buflen);
+static void sbd_handle_unmap(scsi_task_t *task, stmf_data_buf_t *dbuf);
+static int sbd_zvol_unmap(sbd_lu_t *sl, uint64_t offset, uint64_t len);
+static void sbd_handle_write_same(scsi_task_t *task);
+
 extern void sbd_pgr_initialize_it(scsi_task_t *, sbd_it_data_t *);
 extern int sbd_pgr_reservation_conflict(scsi_task_t *);
 extern void sbd_pgr_reset(sbd_lu_t *);
@@ -1754,6 +1767,10 @@
 			    dbuf->db_sglist[0].seg_addr, dbuf->db_data_size);
 		}
 		break;
+	case SCMD_UNMAP:
+		sbd_handle_unmap_xfer(task,
+		    dbuf->db_sglist[0].seg_addr, dbuf->db_data_size);
+		break;
 	case SCMD_PERSISTENT_RESERVE_OUT:
 		if (sl->sl_access_state == SBD_LU_STANDBY) {
 			st_ret = stmf_proxy_scsi_cmd(task, dbuf);
@@ -1815,6 +1832,9 @@
 		p[7] = s & 0xff;
 		p[10] = (blksize >> 8) & 0xff;
 		p[11] = blksize & 0xff;
+		if (sl->sl_flags & SL_UNMAP_ENABLED) {
+			p[14] = 0x80;
+		}
 		sbd_handle_short_read_transfers(task, initial_dbuf, p,
 		    cdb_len, 32);
 		break;
@@ -2216,6 +2236,105 @@
 	return (url_length);
 }
 
+static void
+sbd_handle_write_same(scsi_task_t *task)
+{
+	sbd_lu_t *sl = (sbd_lu_t *)task->task_lu->lu_provider_private;
+	uint64_t addr, len;
+	uint8_t *p;
+	int ret;
+
+	task->task_cmd_xfer_length = 0;
+	if (task->task_additional_flags &
+	    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
+		task->task_expected_xfer_length = 0;
+	}
+	if (task->task_cdb[1] & 0xF7) {
+		stmf_scsilib_send_status(task, STATUS_CHECK,
+		    STMF_SAA_INVALID_FIELD_IN_CDB);
+		return;
+	}
+	p = &task->task_cdb[2];
+	addr = READ_SCSI64(p, uint64_t);
+	addr <<= sl->sl_data_blocksize_shift;
+	len = READ_SCSI32(p+8, uint64_t);
+	len <<= sl->sl_data_blocksize_shift;
+	ret = sbd_zvol_unmap(sl, addr, len);
+	if (ret != 0) {
+		stmf_scsilib_send_status(task, STATUS_CHECK,
+		    STMF_SAA_LBA_OUT_OF_RANGE);
+		return;
+	}
+	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
+}
+
+static void
+sbd_handle_unmap(scsi_task_t *task, stmf_data_buf_t *dbuf)
+{
+	uint32_t cmd_xfer_len;
+
+	cmd_xfer_len = READ_SCSI16(&task->task_cdb[7], uint32_t);
+
+	if (task->task_cdb[1] & 1) {
+		stmf_scsilib_send_status(task, STATUS_CHECK,
+		    STMF_SAA_INVALID_FIELD_IN_CDB);
+		return;
+	}
+
+	if (cmd_xfer_len == 0) {
+		task->task_cmd_xfer_length = 0;
+		if (task->task_additional_flags &
+		    TASK_AF_NO_EXPECTED_XFER_LENGTH) {
+			task->task_expected_xfer_length = 0;
+		}
+		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
+		return;
+	}
+
+	sbd_handle_short_write_transfers(task, dbuf, cmd_xfer_len);
+}
+
+static void
+sbd_handle_unmap_xfer(scsi_task_t *task, uint8_t *buf, uint32_t buflen)
+{
+	sbd_lu_t *sl = (sbd_lu_t *)task->task_lu->lu_provider_private;
+	uint32_t ulen, dlen, num_desc;
+	uint64_t addr, len;
+	uint8_t *p;
+	int ret;
+
+	if (buflen < 24) {
+		stmf_scsilib_send_status(task, STATUS_CHECK,
+		    STMF_SAA_INVALID_FIELD_IN_CDB);
+		return;
+	}
+	ulen = READ_SCSI16(buf, uint32_t);
+	dlen = READ_SCSI16(buf + 2, uint32_t);
+	num_desc = dlen >> 4;
+	if (((ulen + 2) != buflen) || ((dlen + 8) != buflen) || (dlen & 0xf) ||
+	    (num_desc == 0)) {
+		stmf_scsilib_send_status(task, STATUS_CHECK,
+		    STMF_SAA_INVALID_FIELD_IN_CDB);
+		return;
+	}
+
+	for (p = buf + 8; num_desc; num_desc--, p += 16) {
+		addr = READ_SCSI64(p, uint64_t);
+		addr <<= sl->sl_data_blocksize_shift;
+		len = READ_SCSI32(p+8, uint64_t);
+		len <<= sl->sl_data_blocksize_shift;
+		ret = sbd_zvol_unmap(sl, addr, len);
+		if (ret != 0) {
+			stmf_scsilib_send_status(task, STATUS_CHECK,
+			    STMF_SAA_LBA_OUT_OF_RANGE);
+			return;
+		}
+	}
+
+unmap_done:
+	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
+}
+
 void
 sbd_handle_inquiry(struct scsi_task *task, struct stmf_data_buf *initial_dbuf)
 {
@@ -2228,6 +2347,8 @@
 	uint16_t cmd_size;
 	uint32_t xfer_size = 4;
 	uint32_t mgmt_url_size = 0;
+	uint8_t exp;
+	uint64_t s;
 	char *mgmt_url = NULL;
 
 
@@ -2380,6 +2501,8 @@
 	switch (cdbp[2]) {
 	case 0x00:
 		page_length = 4 + (mgmt_url_size ? 1 : 0);
+		if (sl->sl_flags & SL_UNMAP_ENABLED)
+			page_length += 2;
 
 		p[0] = byte0;
 		p[3] = page_length;
@@ -2392,6 +2515,10 @@
 			if (mgmt_url_size != 0)
 				p[i++] = 0x85;
 			p[i++] = 0x86;
+			if (sl->sl_flags & SL_UNMAP_ENABLED) {
+				p[i++] = 0xb0;
+				p[i++] = 0xb2;
+			}
 		}
 		xfer_size = page_length + 4;
 		break;
@@ -2482,6 +2609,43 @@
 		xfer_size = page_length + 4;
 		break;
 
+	case 0xb0:
+		if ((sl->sl_flags & SL_UNMAP_ENABLED) == 0) {
+			stmf_scsilib_send_status(task, STATUS_CHECK,
+			    STMF_SAA_INVALID_FIELD_IN_CDB);
+			goto err_done;
+		}
+		page_length = 0x3c;
+		p[0] = byte0;
+		p[1] = 0xb0;
+		p[3] = page_length;
+		p[20] = p[21] = p[22] = p[23] = 0xFF;
+		p[24] = p[25] = p[26] = p[27] = 0xFF;
+		xfer_size = page_length + 4;
+		break;
+
+	case 0xb2:
+		if ((sl->sl_flags & SL_UNMAP_ENABLED) == 0) {
+			stmf_scsilib_send_status(task, STATUS_CHECK,
+			    STMF_SAA_INVALID_FIELD_IN_CDB);
+			goto err_done;
+		}
+		page_length = 4;
+		p[0] = byte0;
+		p[1] = 0xb2;
+		p[3] = page_length;
+
+		exp = (uint8_t)sl->sl_data_blocksize_shift;
+		s = sl->sl_lu_size >> sl->sl_data_blocksize_shift;
+		while (s & ((uint64_t)0xFFFFFFFF80000000)) {
+			s >>= 1;
+			exp++;
+		}
+		p[4] = exp;
+		p[5] = 0xc0;
+		xfer_size = page_length + 4;
+		break;
+
 	default:
 		stmf_scsilib_send_status(task, STATUS_CHECK,
 		    STMF_SAA_INVALID_FIELD_IN_CDB);
@@ -2908,6 +3072,16 @@
 		return;
 	}
 
+	if ((cdb0 == SCMD_UNMAP) && (sl->sl_flags & SL_UNMAP_ENABLED)) {
+		sbd_handle_unmap(task, initial_dbuf);
+		return;
+	}
+
+	if ((cdb0 == SCMD_WRITE_SAME_G4) && (sl->sl_flags & SL_UNMAP_ENABLED)) {
+		sbd_handle_write_same(task);
+		return;
+	}
+
 	if (cdb0 == SCMD_TEST_UNIT_READY) {	/* Test unit ready */
 		task->task_cmd_xfer_length = 0;
 		stmf_scsilib_send_status(task, STATUS_GOOD, 0);
@@ -3318,4 +3492,35 @@
 	}
 
 	stmf_scsilib_send_status(task, STATUS_GOOD, 0);
+}
+
+static int
+sbd_zvol_unmap(sbd_lu_t *sl, uint64_t offset, uint64_t len)
+{
+	objset_t	*set = NULL;
+	char		*file = NULL;
+	int		error = EIO;
+
+	file = sbd_get_zvol_name(sl);
+	if (file == NULL) {
+		cmn_err(CE_WARN, "sbd has zfs meta but no zvol name");
+		goto szu_done;
+	}
+	if (dmu_objset_hold(file, FTAG, &set) != 0) {
+		cmn_err(CE_WARN, "Failed to get zvol handle");
+		goto szu_done;
+	}
+
+	error = dmu_free_long_range(set, ZVOL_OBJ, offset, len);
+
+	if ((error == 0) && (sl->sl_flags & SL_WRITEBACK_CACHE_DISABLE)) {
+		txg_wait_synced(dmu_objset_pool(set), 0);
+	}
+
+szu_done:
+	if (file != NULL)
+		kmem_free(file, strlen(file) + 1);
+	if (set != NULL)
+		dmu_objset_rele(set, FTAG);
+	return (error);
 }
--- old/usr/src/uts/common/io/comstar/lu/stmf_sbd/stmf_sbd.h	Wed Feb  9 23:37:30 2011
+++ new/usr/src/uts/common/io/comstar/lu/stmf_sbd/stmf_sbd.h	Wed Feb  9 23:37:29 2011
@@ -20,6 +20,8 @@
  */
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  */
 
 #ifndef	_STMF_SBD_H
@@ -247,26 +249,27 @@
 /*
  * sl_flags
  */
-#define	SL_LINKED			    0x00001
-#define	SL_META_OPENED			    0x00002
-#define	SL_REGISTERED			    0x00004
-#define	SL_META_NEEDS_FLUSH		    0x00008
-#define	SL_DATA_NEEDS_FLUSH		    0x00010
-#define	SL_VID_VALID			    0x00020
-#define	SL_PID_VALID			    0x00040
-#define	SL_REV_VALID			    0x00080
-#define	SL_WRITE_PROTECTED		    0x00100
-#define	SL_MEDIA_LOADED			    0x00200
-#define	SL_LU_HAS_SCSI2_RESERVATION	    0x00400
-#define	SL_WRITEBACK_CACHE_DISABLE	    0x00800
-#define	SL_SAVED_WRITE_CACHE_DISABLE	    0x01000
-#define	SL_MEDIUM_REMOVAL_PREVENTED	    0x02000
-#define	SL_NO_DATA_DKIOFLUSH		    0x04000
-#define	SL_SHARED_META			    0x08000
-#define	SL_ZFS_META			    0x10000
-#define	SL_WRITEBACK_CACHE_SET_UNSUPPORTED  0x20000
-#define	SL_FLUSH_ON_DISABLED_WRITECACHE	    0x40000
-#define	SL_CALL_ZVOL			    0x80000
+#define	SL_LINKED			    0x00000001
+#define	SL_META_OPENED			    0x00000002
+#define	SL_REGISTERED			    0x00000004
+#define	SL_META_NEEDS_FLUSH		    0x00000008
+#define	SL_DATA_NEEDS_FLUSH		    0x00000010
+#define	SL_VID_VALID			    0x00000020
+#define	SL_PID_VALID			    0x00000040
+#define	SL_REV_VALID			    0x00000080
+#define	SL_WRITE_PROTECTED		    0x00000100
+#define	SL_MEDIA_LOADED			    0x00000200
+#define	SL_LU_HAS_SCSI2_RESERVATION	    0x00000400
+#define	SL_WRITEBACK_CACHE_DISABLE	    0x00000800
+#define	SL_SAVED_WRITE_CACHE_DISABLE	    0x00001000
+#define	SL_MEDIUM_REMOVAL_PREVENTED	    0x00002000
+#define	SL_NO_DATA_DKIOFLUSH		    0x00004000
+#define	SL_SHARED_META			    0x00008000
+#define	SL_ZFS_META			    0x00010000
+#define	SL_WRITEBACK_CACHE_SET_UNSUPPORTED  0x00020000
+#define	SL_FLUSH_ON_DISABLED_WRITECACHE	    0x00040000
+#define	SL_CALL_ZVOL			    0x00080000
+#define	SL_UNMAP_ENABLED		    0x00100000
 
 /*
  * sl_trans_op. LU is undergoing some transition and this field
@@ -297,6 +300,7 @@
 sbd_status_t sbd_flush_data_cache(sbd_lu_t *sl, int fsync_done);
 sbd_status_t sbd_wcd_set(int wcd, sbd_lu_t *sl);
 void sbd_wcd_get(int *wcd, sbd_lu_t *sl);
+char *sbd_get_zvol_name(sbd_lu_t *sl);
 
 #ifdef	__cplusplus
 }
--- old/usr/src/uts/common/sys/scsi/generic/commands.h	Wed Feb  9 23:37:31 2011
+++ new/usr/src/uts/common/sys/scsi/generic/commands.h	Wed Feb  9 23:37:30 2011
@@ -21,6 +21,8 @@
 
 /*
  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  */
 
 #ifndef	_SYS_SCSI_GENERIC_COMMANDS_H
@@ -233,13 +235,15 @@
 #define	SCMD_READ_BUFFER	0x3c
 #define	SCMD_READ_LONG		0x3E
 #define	SCMD_WRITE_LONG		0x3F
+#define	SCMD_WRITE_SAME_G1	0x41
+#define	SCMD_UNMAP		0x42
+#define	SCMD_GET_CONFIGURATION	0x46
+#define	SCMD_LOG_SELECT_G1	0x4c
+#define	SCMD_LOG_SENSE_G1	0x4d
 #define	SCMD_RESERVE_G1		0x56
 #define	SCMD_RELEASE_G1		0x57
 #define	SCMD_MODE_SELECT_G1	0x55
 #define	SCMD_MODE_SENSE_G1	0x5A
-#define	SCMD_GET_CONFIGURATION	0x46
-#define	SCMD_LOG_SELECT_G1	0x4C
-#define	SCMD_LOG_SENSE_G1	0x4d
 
 
 /*
@@ -333,6 +337,7 @@
 #define	SCMD_READ_G4		0x88
 #define	SCMD_WRITE_G4		0x8a
 #define	SCMD_WRITE_VERIFY_G4	0x8e
+#define	SCMD_WRITE_SAME_G4	0x93
 #define	SCMD_SVC_ACTION_IN_G4	0x9e
 #define	SCMD_SVC_ACTION_OUT_G4	0x9f
 
@@ -455,6 +460,8 @@
 /* 0x3c */ SCMD_READ_BUFFER,		"read_buffer",			\
 /* 0x3e */ SCMD_READ_LONG,		"read_long",			\
 /* 0x3f */ SCMD_WRITE_LONG,		"write_long",			\
+/* 0x41 */ SCMD_WRITE_SAME_G1,		"write_same(10)",		\
+/* 0x42 */ SCMD_UNMAP,			"unmap",			\
 /* 0x44 */ SCMD_REPORT_DENSITIES |					\
 		/* SCMD_READ_HEADER (from cdio.h) | */			\
 		0,			"report_densities/read_header",	\
@@ -477,6 +484,7 @@
 /* 0x8f */ SCMD_VERIFY_G4,		"verify(16)",			\
 /* 0x91 */ SCMD_SPACE_G4,		"space(16)",			\
 /* 0x92 */ SCMD_LOCATE_G4,		"locate(16)",			\
+/* 0x92 */ SCMD_WRITE_SAME_G4,		"write_same(16)",		\
 /* 0x9e */ SCMD_SVC_ACTION_IN_G4,	"service_action_in(16)",	\
 /* 0x9f */ SCMD_SVC_ACTION_OUT_G4,	"service_action_out(16)",	\
 /* 0xa0 */ SCMD_REPORT_LUNS,		"report_luns",			\
--- old/usr/src/uts/common/sys/stmf_sbd_ioctl.h	Wed Feb  9 23:37:31 2011
+++ new/usr/src/uts/common/sys/stmf_sbd_ioctl.h	Wed Feb  9 23:37:31 2011
@@ -21,6 +21,8 @@
 /*
  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  */
 
 #ifndef	_STMF_SBD_IOCTL_H
@@ -76,6 +78,7 @@
 #define	SBD_IOCTL_SET_LU_STANDBY			SBD_IOCTL_DEF(7)
 #define	SBD_IOCTL_SET_GLOBAL_LU				SBD_IOCTL_DEF(8)
 #define	SBD_IOCTL_GET_GLOBAL_LU				SBD_IOCTL_DEF(9)
+#define	SBD_IOCTL_GET_UNMAP_PROPS			SBD_IOCTL_DEF(10)
 
 typedef struct sbd_create_and_reg_lu {
 	uint32_t	slu_struct_size;
@@ -222,6 +225,16 @@
 	uint8_t		slp_buf[8];	/* likely more than 8 */
 } sbd_lu_props_t;
 
+typedef struct sbd_unmap_props {
+	uint32_t	sup_found_lu:1,
+			sup_zvol_path_valid:1,
+			sup_guid_valid:1,
+			sup_unmap_enabled;
+	uint32_t	sup_rsvd;
+	char		sup_zvol_path[256];
+	uint8_t		sup_guid[16];
+} sbd_unmap_props_t;
+
 #ifdef	__cplusplus
 }
 #endif
