FreeBSD Bugzilla – Attachment 188807 Details for
Bug 224250
Sending SCSI command to ATA device makes kernel triple fault.
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
C# snippet
fbsd224250.cs (text/x-csharp), 12.71 KB, created by
Natalia Portillo
on 2017-12-13 18:20:15 UTC
(
hide
)
Description:
C# snippet
Filename:
MIME Type:
Creator:
Natalia Portillo
Created:
2017-12-13 18:20:15 UTC
Size:
12.71 KB
patch
obsolete
>using System; >using System.Runtime.InteropServices; > >namespace FreeBSD224250 >{ > > [StructLayout(LayoutKind.Sequential)] > struct timeval > { > public long tv_sec; > /// <summary>long</summary> > public IntPtr tv_usec; > } > > [StructLayout(LayoutKind.Sequential)] > struct ccb_qos_area > { > public timeval etime; > public UIntPtr sim_data; > public UIntPtr periph_data; > } > > [Flags] > enum ccb_flags : uint > { > /// <summary> > /// Data direction (01:DATA IN) > /// </summary> > CAM_DIR_IN = 0x00000040, > /// <summary> > /// Disable DEV Q freezing > /// </summary> > CAM_DEV_QFRZDIS = 0x00000400, > } > > enum cam_status : uint > { > /// <summary>CCB request is in progress</summary> > CAM_REQ_INPROG = 0x00, > > /// <summary>CCB request completed without error</summary> > CAM_REQ_CMP = 0x01, > > /// <summary>CCB request aborted by the host</summary> > CAM_REQ_ABORTED = 0x02, > > /// <summary>Unable to abort CCB request</summary> > CAM_UA_ABORT = 0x03, > > /// <summary>CCB request completed with an error</summary> > CAM_REQ_CMP_ERR = 0x04, > > /// <summary>CAM subsystem is busy</summary> > CAM_BUSY = 0x05, > > /// <summary>CCB request was invalid</summary> > CAM_REQ_INVALID = 0x06, > > /// <summary>Supplied Path ID is invalid</summary> > CAM_PATH_INVALID = 0x07, > > /// <summary>SCSI Device Not Installed/there</summary> > CAM_DEV_NOT_THERE = 0x08, > > /// <summary>Unable to terminate I/O CCB request</summary> > CAM_UA_TERMIO = 0x09, > > /// <summary>Target Selection Timeout</summary> > CAM_SEL_TIMEOUT = 0x0a, > > /// <summary>Command timeout</summary> > CAM_CMD_TIMEOUT = 0x0b, > > /// <summary>SCSI error, look at error code in CCB</summary> > CAM_SCSI_STATUS_ERROR = 0x0c, > > /// <summary>Message Reject Received</summary> > CAM_MSG_REJECT_REC = 0x0d, > > /// <summary>SCSI Bus Reset Sent/Received</summary> > CAM_SCSI_BUS_RESET = 0x0e, > > /// <summary>Uncorrectable parity error occurred</summary> > CAM_UNCOR_PARITY = 0x0f, > > /// <summary>Autosense: request sense cmd fail</summary> > CAM_AUTOSENSE_FAIL = 0x10, > > /// <summary>No HBA Detected error</summary> > CAM_NO_HBA = 0x11, > > /// <summary>Data Overrun error</summary> > CAM_DATA_RUN_ERR = 0x12, > > /// <summary>Unexpected Bus Free</summary> > CAM_UNEXP_BUSFREE = 0x13, > > /// <summary>Target Bus Phase Sequence Failure</summary> > CAM_SEQUENCE_FAIL = 0x14, > > /// <summary>CCB length supplied is inadequate</summary> > CAM_CCB_LEN_ERR = 0x15, > > /// <summary>Unable to provide requested capability</summary> > CAM_PROVIDE_FAIL = 0x16, > > /// <summary>A SCSI BDR msg was sent to target</summary> > CAM_BDR_SENT = 0x17, > > /// <summary>CCB request terminated by the host</summary> > CAM_REQ_TERMIO = 0x18, > > /// <summary>Unrecoverable Host Bus Adapter Error</summary> > CAM_UNREC_HBA_ERROR = 0x19, > > /// <summary>Request was too large for this host</summary> > CAM_REQ_TOO_BIG = 0x1a, > > /// <summary>This request should be requeued to preserve transaction ordering. This typically occurs when the SIM recognizes an error that should freeze the queue and must place additional requests for the target at the sim level back into the XPT queue.</summary> > CAM_REQUEUE_REQ = 0x1b, > > /// <summary>ATA error, look at error code in CCB</summary> > CAM_ATA_STATUS_ERROR = 0x1c, > > /// <summary>Initiator/Target Nexus lost.</summary> > CAM_SCSI_IT_NEXUS_LOST = 0x1d, > > /// <summary>SMP error, look at error code in CCB</summary> > CAM_SMP_STATUS_ERROR = 0x1e, > > /// <summary>Command completed without error but exceeded the soft timeout threshold.</summary> > CAM_REQ_SOFTTIMEOUT = 0x1f, > > /* > * 0x20 - 0x32 are unassigned > */ > > /// <summary>Initiator Detected Error</summary> > CAM_IDE = 0x33, > > /// <summary>Resource Unavailable</summary> > CAM_RESRC_UNAVAIL = 0x34, > > /// <summary>Unacknowledged Event by Host</summary> > CAM_UNACKED_EVENT = 0x35, > > /// <summary>Message Received in Host Target Mode</summary> > CAM_MESSAGE_RECV = 0x36, > > /// <summary>Invalid CDB received in Host Target Mode</summary> > CAM_INVALID_CDB = 0x37, > > /// <summary>Lun supplied is invalid</summary> > CAM_LUN_INVALID = 0x38, > > /// <summary>Target ID supplied is invalid</summary> > CAM_TID_INVALID = 0x39, > > /// <summary>The requested function is not available</summary> > CAM_FUNC_NOTAVAIL = 0x3a, > > /// <summary>Nexus is not established</summary> > CAM_NO_NEXUS = 0x3b, > > /// <summary>The initiator ID is invalid</summary> > CAM_IID_INVALID = 0x3c, > > /// <summary>The SCSI CDB has been received</summary> > CAM_CDB_RECVD = 0x3d, > > /// <summary>The LUN is already enabled for target mode</summary> > CAM_LUN_ALRDY_ENA = 0x3e, > > /// <summary>SCSI Bus Busy</summary> > CAM_SCSI_BUSY = 0x3f, > > > /* > * Flags > */ > > /// <summary>The DEV queue is frozen w/this err</summary> > CAM_DEV_QFRZN = 0x40, > > /// <summary>Autosense data valid for target</summary> > CAM_AUTOSNS_VALID = 0x80, > > /// <summary>SIM ready to take more commands</summary> > CAM_RELEASE_SIMQ = 0x100, > > /// <summary>SIM has this command in its queue</summary> > CAM_SIM_QUEUED = 0x200, > > /// <summary>Quality of service data is valid</summary> > CAM_QOS_VALID = 0x400, > > /// <summary>Mask bits for just the status #</summary> > CAM_STATUS_MASK = 0x3F, > > /* > * Target Specific Adjunct Status > */ > > /// <summary>sent sense with status</summary> > CAM_SENT_SENSE = 0x40000000 > } > > [Flags] > enum FileFlags > { > O_RDWR = 0x00000002, > } > > [Flags] > enum xpt_opcode > { > XPT_FC_QUEUED = 0x100, > XPT_FC_DEV_QUEUED = 0x800 | XPT_FC_QUEUED, > XPT_SCSI_IO = 0x01 | XPT_FC_DEV_QUEUED, > } > > [StructLayout(LayoutKind.Sequential)] > struct cam_pinfo > { > public uint priority; > public uint generation; > public int index; > } > > struct LIST_ENTRY > { > /// <summary> > /// LIST_ENTRY(ccb_hdr)=le->*le_next > /// </summary> > public IntPtr le_next; > /// <summary> > /// LIST_ENTRY(ccb_hdr)=le->**le_prev > /// </summary> > public IntPtr le_prev; > } > > struct SLIST_ENTRY > { > /// <summary> > /// SLIST_ENTRY(ccb_hdr)=sle->*sle_next > /// </summary> > public IntPtr sle_next; > } > > struct TAILQ_ENTRY > { > /// <summary> > /// TAILQ_ENTRY(ccb_hdr)=tqe->*tqe_next > /// </summary> > public IntPtr tqe_next; > /// <summary> > /// TAILQ_ENTRY(ccb_hdr)=tqe->**tqe_prev > /// </summary> > public IntPtr tqe_prev; > } > > struct STAILQ_ENTRY > { > /// <summary> > /// STAILQ_ENTRY(ccb_hdr)=stqe->*stqe_next > /// </summary> > public IntPtr stqe_next; > } > > [StructLayout(LayoutKind.Explicit)] > struct camq_entry > { > [FieldOffset(0)] public LIST_ENTRY le; > [FieldOffset(0)] public SLIST_ENTRY sle; > [FieldOffset(0)] public TAILQ_ENTRY tqe; > [FieldOffset(0)] public STAILQ_ENTRY stqe; > } > > [StructLayout(LayoutKind.Sequential)] > struct ccb_hdr > { > public cam_pinfo pinfo; > public camq_entry xpt_links; > public camq_entry sim_links; > public camq_entry periph_links; > public uint retry_count; > public IntPtr cbfcnp; > public xpt_opcode func_code; > public cam_status status; > public IntPtr path; > public uint path_id; > public uint target_id; > public ulong target_lun; > public ccb_flags flags; > public uint xflags; > [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] > public IntPtr[] periph_priv; > [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] > public IntPtr[] sim_priv; > public ccb_qos_area qos; > public uint timeout; > public timeval softtimeout; > } > > /// <summary> > /// SCSI I/O Request CCB used for the XPT_SCSI_IO and XPT_CONT_TARGET_IO function codes. > /// </summary> > [StructLayout(LayoutKind.Sequential)] > struct ccb_scsiio64 > { > public ccb_hdr ccb_h; > /// <summary>Ptr for next CCB for action</summary> > public IntPtr next_ccb; > /// <summary>Ptr to mapping info</summary> > public IntPtr req_map; > /// <summary>Ptr to the data buf/SG list</summary> > public IntPtr data_ptr; > /// <summary>Data transfer length</summary> > public uint dxfer_len; > /// <summary>Autosense storage</summary> > public scsi_sense_data sense_data; > /// <summary>Number of bytes to autosense</summary> > public byte sense_len; > /// <summary>Number of bytes for the CDB</summary> > public byte cdb_len; > /// <summary>Number of SG list entries</summary> > public short sglist_cnt; > /// <summary>Returned SCSI status</summary> > public byte scsi_status; > /// <summary>Autosense resid length: 2's comp</summary> > public sbyte sense_resid; > /// <summary>Transfer residual length: 2's comp</summary> > public int resid; > public uint alignment; > /// <summary> > /// Area for the CDB send, or pointer to the CDB bytes to send > /// </summary> > const int IOCDBLEN = 16; > [MarshalAs(UnmanagedType.ByValArray, SizeConst = IOCDBLEN)] public byte[] cdb_bytes; > /// <summary>Pointer to the message buffer</summary> > public IntPtr msg_ptr; > /// <summary>Number of bytes for the Message</summary> > public short msg_len; > /// <summary>What to do for tag queueing. The tag action should be either the define below (to send a non-tagged transaction) or one of the defined scsi tag messages from scsi_message.h.</summary> > public byte tag_action; > /// <summary>tag id from initator (target mode)</summary> > public uint tag_id; > /// <summary>initiator id of who selected</summary> > public uint init_id; > } > > [StructLayout(LayoutKind.Sequential)] > struct scsi_sense_data > { > const int SSD_FULL_SIZE = 252; > public byte error_code; > [MarshalAs(UnmanagedType.ByValArray, SizeConst = SSD_FULL_SIZE - 1)] public byte[] sense_buf; > } > > class MainClass > { > [DllImport("libcam", CharSet = CharSet.Ansi, SetLastError = true)] > internal static extern IntPtr cam_open_device( > string path, > [MarshalAs(UnmanagedType.U4)] > FileFlags flags); > > [DllImport("libcam", CharSet = CharSet.Ansi, SetLastError = true)] > internal static extern void cam_close_device(IntPtr dev); > > [DllImport("libcam", CharSet = CharSet.Ansi, SetLastError = true)] > internal static extern IntPtr cam_getccb(IntPtr dev); > > [DllImport("libcam", CharSet = CharSet.Ansi, SetLastError = true)] > internal static extern void cam_freeccb(IntPtr ccb); > > [DllImport("libcam", CharSet = CharSet.Ansi, SetLastError = true)] > internal static extern int cam_send_ccb(IntPtr dev, IntPtr ccb); > > const int CAM_MAX_CDBLEN = 16; > > public static void Main(string[] args) > { > IntPtr dev = cam_open_device(args[0], FileFlags.O_RDWR); > byte[] buffer = new byte[36]; > > IntPtr ccbPtr = cam_getccb(dev); > > ccb_scsiio64 csio = (ccb_scsiio64)Marshal.PtrToStructure(ccbPtr, typeof(ccb_scsiio64)); > csio.ccb_h.func_code = xpt_opcode.XPT_SCSI_IO; > csio.ccb_h.flags = ccb_flags.CAM_DIR_IN; > csio.ccb_h.xflags = 0; > csio.ccb_h.retry_count = 1; > csio.ccb_h.cbfcnp = IntPtr.Zero; > csio.ccb_h.timeout = 10000; > csio.data_ptr = Marshal.AllocHGlobal(buffer.Length); > csio.dxfer_len = (uint)buffer.Length; > csio.sense_len = 32; > csio.cdb_len = 6; > csio.tag_action = 0x20; > csio.cdb_bytes = new byte[] { 0x12, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; > csio.ccb_h.flags |= ccb_flags.CAM_DEV_QFRZDIS; > > Marshal.Copy(buffer, 0, csio.data_ptr, buffer.Length); > Marshal.StructureToPtr(csio, ccbPtr, false); > > cam_send_ccb(dev, ccbPtr); > > Marshal.FreeHGlobal(csio.data_ptr); > cam_freeccb(ccbPtr); > cam_close_device(dev); > } > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 224250
:
188806
| 188807 |
188831