xref: /freebsd/sys/cam/cam.c (revision 8dfea46460a0befc49458e87d2078a5ca100e0eb)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Generic utility routines for the Common Access Method layer.
38b8a9b1dSJustin T. Gibbs  *
48b8a9b1dSJustin T. Gibbs  * Copyright (c) 1997 Justin T. Gibbs.
58b8a9b1dSJustin T. Gibbs  * All rights reserved.
68b8a9b1dSJustin T. Gibbs  *
78b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
88b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
98b8a9b1dSJustin T. Gibbs  * are met:
108b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
118b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
128b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
138b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
148b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
158b8a9b1dSJustin T. Gibbs  *
168b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
208b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
278b8a9b1dSJustin T. Gibbs  */
288b8a9b1dSJustin T. Gibbs 
299c963d87SDavid E. O'Brien #include <sys/cdefs.h>
309c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
319c963d87SDavid E. O'Brien 
329c963d87SDavid E. O'Brien #include <sys/param.h>
333393f8daSKenneth D. Merry #ifdef _KERNEL
343393f8daSKenneth D. Merry #include <sys/systm.h>
3565c38256SMike Smith #include <sys/kernel.h>
3665c38256SMike Smith #include <sys/sysctl.h>
373393f8daSKenneth D. Merry #else /* _KERNEL */
383393f8daSKenneth D. Merry #include <stdlib.h>
393393f8daSKenneth D. Merry #include <stdio.h>
4006e79492SKenneth D. Merry #include <string.h>
41912e4916SEd Schouten #include <camlib.h>
423393f8daSKenneth D. Merry #endif /* _KERNEL */
433393f8daSKenneth D. Merry 
448b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
453393f8daSKenneth D. Merry #include <cam/cam_ccb.h>
463393f8daSKenneth D. Merry #include <cam/scsi/scsi_all.h>
4706e79492SKenneth D. Merry #include <cam/scsi/smp_all.h>
483393f8daSKenneth D. Merry #include <sys/sbuf.h>
493393f8daSKenneth D. Merry 
503393f8daSKenneth D. Merry #ifdef _KERNEL
513393f8daSKenneth D. Merry #include <sys/libkern.h>
5252c9ce25SScott Long #include <cam/cam_queue.h>
533393f8daSKenneth D. Merry #include <cam/cam_xpt.h>
54de5b1952SAlexander Leidinger 
55de5b1952SAlexander Leidinger FEATURE(scbus, "SCSI devices support");
56de5b1952SAlexander Leidinger 
573393f8daSKenneth D. Merry #endif
583393f8daSKenneth D. Merry 
593393f8daSKenneth D. Merry static int	camstatusentrycomp(const void *key, const void *member);
603393f8daSKenneth D. Merry 
613393f8daSKenneth D. Merry const struct cam_status_entry cam_status_table[] = {
623393f8daSKenneth D. Merry 	{ CAM_REQ_INPROG,	 "CCB request is in progress"		     },
633393f8daSKenneth D. Merry 	{ CAM_REQ_CMP,		 "CCB request completed without error"	     },
643393f8daSKenneth D. Merry 	{ CAM_REQ_ABORTED,	 "CCB request aborted by the host"	     },
653393f8daSKenneth D. Merry 	{ CAM_UA_ABORT,		 "Unable to abort CCB request"		     },
663393f8daSKenneth D. Merry 	{ CAM_REQ_CMP_ERR,	 "CCB request completed with an error"	     },
67104fad28SBenedict Reuschling 	{ CAM_BUSY,		 "CAM subsystem is busy"		     },
683393f8daSKenneth D. Merry 	{ CAM_REQ_INVALID,	 "CCB request was invalid"		     },
693393f8daSKenneth D. Merry 	{ CAM_PATH_INVALID,	 "Supplied Path ID is invalid"		     },
703393f8daSKenneth D. Merry 	{ CAM_DEV_NOT_THERE,	 "Device Not Present"			     },
713393f8daSKenneth D. Merry 	{ CAM_UA_TERMIO,	 "Unable to terminate I/O CCB request"	     },
723393f8daSKenneth D. Merry 	{ CAM_SEL_TIMEOUT,	 "Selection Timeout"			     },
733393f8daSKenneth D. Merry 	{ CAM_CMD_TIMEOUT,	 "Command timeout"			     },
743393f8daSKenneth D. Merry 	{ CAM_SCSI_STATUS_ERROR, "SCSI Status Error"			     },
753393f8daSKenneth D. Merry 	{ CAM_MSG_REJECT_REC,	 "Message Reject Reveived"		     },
763393f8daSKenneth D. Merry 	{ CAM_SCSI_BUS_RESET,	 "SCSI Bus Reset Sent/Received"		     },
773393f8daSKenneth D. Merry 	{ CAM_UNCOR_PARITY,	 "Uncorrectable parity/CRC error"	     },
783393f8daSKenneth D. Merry 	{ CAM_AUTOSENSE_FAIL,	 "Auto-Sense Retrieval Failed"		     },
793393f8daSKenneth D. Merry 	{ CAM_NO_HBA,		 "No HBA Detected"			     },
803393f8daSKenneth D. Merry 	{ CAM_DATA_RUN_ERR,	 "Data Overrun error"			     },
813393f8daSKenneth D. Merry 	{ CAM_UNEXP_BUSFREE,	 "Unexpected Bus Free"			     },
823393f8daSKenneth D. Merry 	{ CAM_SEQUENCE_FAIL,	 "Target Bus Phase Sequence Failure"	     },
833393f8daSKenneth D. Merry 	{ CAM_CCB_LEN_ERR,	 "CCB length supplied is inadequate"	     },
843393f8daSKenneth D. Merry 	{ CAM_PROVIDE_FAIL,	 "Unable to provide requested capability"    },
853393f8daSKenneth D. Merry 	{ CAM_BDR_SENT,		 "SCSI BDR Message Sent"		     },
863393f8daSKenneth D. Merry 	{ CAM_REQ_TERMIO,	 "CCB request terminated by the host"	     },
873393f8daSKenneth D. Merry 	{ CAM_UNREC_HBA_ERROR,	 "Unrecoverable Host Bus Adapter Error"	     },
883393f8daSKenneth D. Merry 	{ CAM_REQ_TOO_BIG,	 "The request was too large for this host"   },
893393f8daSKenneth D. Merry 	{ CAM_REQUEUE_REQ,	 "Unconditionally Re-queue Request",	     },
9052c9ce25SScott Long 	{ CAM_ATA_STATUS_ERROR,	 "ATA Status Error"			     },
9106e79492SKenneth D. Merry 	{ CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost"               },
9206e79492SKenneth D. Merry 	{ CAM_SMP_STATUS_ERROR,	 "SMP Status Error"                          },
933393f8daSKenneth D. Merry 	{ CAM_IDE,		 "Initiator Detected Error Message Received" },
943393f8daSKenneth D. Merry 	{ CAM_RESRC_UNAVAIL,	 "Resource Unavailable"			     },
953393f8daSKenneth D. Merry 	{ CAM_UNACKED_EVENT,	 "Unacknowledged Event by Host"		     },
963393f8daSKenneth D. Merry 	{ CAM_MESSAGE_RECV,	 "Message Received in Host Target Mode"	     },
973393f8daSKenneth D. Merry 	{ CAM_INVALID_CDB,	 "Invalid CDB received in Host Target Mode"  },
983393f8daSKenneth D. Merry 	{ CAM_LUN_INVALID,	 "Invalid Lun"				     },
993393f8daSKenneth D. Merry 	{ CAM_TID_INVALID,	 "Invalid Target ID"			     },
1003393f8daSKenneth D. Merry 	{ CAM_FUNC_NOTAVAIL,	 "Function Not Available"		     },
1013393f8daSKenneth D. Merry 	{ CAM_NO_NEXUS,		 "Nexus Not Established"		     },
1023393f8daSKenneth D. Merry 	{ CAM_IID_INVALID,	 "Invalid Initiator ID"			     },
1033393f8daSKenneth D. Merry 	{ CAM_CDB_RECVD,	 "CDB Received"				     },
1043393f8daSKenneth D. Merry 	{ CAM_LUN_ALRDY_ENA,	 "LUN Already Enabled for Target Mode"	     },
1053393f8daSKenneth D. Merry 	{ CAM_SCSI_BUSY,	 "SCSI Bus Busy"			     },
1063393f8daSKenneth D. Merry };
1073393f8daSKenneth D. Merry 
10865c38256SMike Smith #ifdef _KERNEL
10965c38256SMike Smith SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
1105f83aee5SSteven Hartland 
1115f83aee5SSteven Hartland #ifndef CAM_DEFAULT_SORT_IO_QUEUES
1125f83aee5SSteven Hartland #define CAM_DEFAULT_SORT_IO_QUEUES 1
1135f83aee5SSteven Hartland #endif
1145f83aee5SSteven Hartland 
1155f83aee5SSteven Hartland int cam_sort_io_queues = CAM_DEFAULT_SORT_IO_QUEUES;
1165f83aee5SSteven Hartland SYSCTL_INT(_kern_cam, OID_AUTO, sort_io_queues, CTLFLAG_RWTUN,
1175f83aee5SSteven Hartland     &cam_sort_io_queues, 0, "Sort IO queues to try and optimise disk access patterns");
11865c38256SMike Smith #endif
11965c38256SMike Smith 
1208b8a9b1dSJustin T. Gibbs void
1218b8a9b1dSJustin T. Gibbs cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
1228b8a9b1dSJustin T. Gibbs {
1238b8a9b1dSJustin T. Gibbs 
12453b062d3SJustin T. Gibbs 	/* Trim leading/trailing spaces, nulls. */
1258b8a9b1dSJustin T. Gibbs 	while (srclen > 0 && src[0] == ' ')
1268b8a9b1dSJustin T. Gibbs 		src++, srclen--;
12753b062d3SJustin T. Gibbs 	while (srclen > 0
12853b062d3SJustin T. Gibbs 	    && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
1298b8a9b1dSJustin T. Gibbs 		srclen--;
1308b8a9b1dSJustin T. Gibbs 
1318b8a9b1dSJustin T. Gibbs 	while (srclen > 0 && dstlen > 1) {
1328b8a9b1dSJustin T. Gibbs 		u_int8_t *cur_pos = dst;
1338b8a9b1dSJustin T. Gibbs 
1348b8a9b1dSJustin T. Gibbs 		if (*src < 0x20 || *src >= 0x80) {
1358b8a9b1dSJustin T. Gibbs 			/* SCSI-II Specifies that these should never occur. */
1368b8a9b1dSJustin T. Gibbs 			/* non-printable character */
1378b8a9b1dSJustin T. Gibbs 			if (dstlen > 4) {
1388b8a9b1dSJustin T. Gibbs 				*cur_pos++ = '\\';
1398b8a9b1dSJustin T. Gibbs 				*cur_pos++ = ((*src & 0300) >> 6) + '0';
1408b8a9b1dSJustin T. Gibbs 				*cur_pos++ = ((*src & 0070) >> 3) + '0';
1418b8a9b1dSJustin T. Gibbs 				*cur_pos++ = ((*src & 0007) >> 0) + '0';
1428b8a9b1dSJustin T. Gibbs 			} else {
1438b8a9b1dSJustin T. Gibbs 				*cur_pos++ = '?';
1448b8a9b1dSJustin T. Gibbs 			}
1458b8a9b1dSJustin T. Gibbs 		} else {
1468b8a9b1dSJustin T. Gibbs 			/* normal character */
1478b8a9b1dSJustin T. Gibbs 			*cur_pos++ = *src;
1488b8a9b1dSJustin T. Gibbs 		}
1498b8a9b1dSJustin T. Gibbs 		src++;
1508b8a9b1dSJustin T. Gibbs 		srclen--;
1518b8a9b1dSJustin T. Gibbs 		dstlen -= cur_pos - dst;
1528b8a9b1dSJustin T. Gibbs 		dst = cur_pos;
1538b8a9b1dSJustin T. Gibbs 	}
1548b8a9b1dSJustin T. Gibbs 	*dst = '\0';
1558b8a9b1dSJustin T. Gibbs }
1568b8a9b1dSJustin T. Gibbs 
1575672fac9SKenneth D. Merry void
1585672fac9SKenneth D. Merry cam_strvis_sbuf(struct sbuf *sb, const u_int8_t *src, int srclen,
1595672fac9SKenneth D. Merry 		uint32_t flags)
1605672fac9SKenneth D. Merry {
1615672fac9SKenneth D. Merry 
1625672fac9SKenneth D. Merry 	/* Trim leading/trailing spaces, nulls. */
1635672fac9SKenneth D. Merry 	while (srclen > 0 && src[0] == ' ')
1645672fac9SKenneth D. Merry 		src++, srclen--;
1655672fac9SKenneth D. Merry 	while (srclen > 0
1665672fac9SKenneth D. Merry 	    && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
1675672fac9SKenneth D. Merry 		srclen--;
1685672fac9SKenneth D. Merry 
1695672fac9SKenneth D. Merry 	while (srclen > 0) {
1705672fac9SKenneth D. Merry 		if (*src < 0x20 || *src >= 0x80) {
1715672fac9SKenneth D. Merry 			/* SCSI-II Specifies that these should never occur. */
1725672fac9SKenneth D. Merry 			/* non-printable character */
1735672fac9SKenneth D. Merry 			switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) {
1745672fac9SKenneth D. Merry 			case CAM_STRVIS_FLAG_NONASCII_ESC:
1755672fac9SKenneth D. Merry 				sbuf_printf(sb, "\\%c%c%c",
1765672fac9SKenneth D. Merry 				    ((*src & 0300) >> 6) + '0',
1775672fac9SKenneth D. Merry 				    ((*src & 0070) >> 3) + '0',
1785672fac9SKenneth D. Merry 				    ((*src & 0007) >> 0) + '0');
1795672fac9SKenneth D. Merry 				break;
1805672fac9SKenneth D. Merry 			case CAM_STRVIS_FLAG_NONASCII_RAW:
1815672fac9SKenneth D. Merry 				/*
1825672fac9SKenneth D. Merry 				 * If we run into a NUL, just transform it
1835672fac9SKenneth D. Merry 				 * into a space.
1845672fac9SKenneth D. Merry 				 */
1855672fac9SKenneth D. Merry 				if (*src != 0x00)
1865672fac9SKenneth D. Merry 					sbuf_putc(sb, *src);
1875672fac9SKenneth D. Merry 				else
1885672fac9SKenneth D. Merry 					sbuf_putc(sb, ' ');
1895672fac9SKenneth D. Merry 				break;
1905672fac9SKenneth D. Merry 			case CAM_STRVIS_FLAG_NONASCII_SPC:
1915672fac9SKenneth D. Merry 				sbuf_putc(sb, ' ');
1925672fac9SKenneth D. Merry 				break;
1935672fac9SKenneth D. Merry 			case CAM_STRVIS_FLAG_NONASCII_TRIM:
1945672fac9SKenneth D. Merry 			default:
1955672fac9SKenneth D. Merry 				break;
1965672fac9SKenneth D. Merry 			}
1975672fac9SKenneth D. Merry 		} else {
1985672fac9SKenneth D. Merry 			/* normal character */
1995672fac9SKenneth D. Merry 			sbuf_putc(sb, *src);
2005672fac9SKenneth D. Merry 		}
2015672fac9SKenneth D. Merry 		src++;
2025672fac9SKenneth D. Merry 		srclen--;
2035672fac9SKenneth D. Merry 	}
2045672fac9SKenneth D. Merry }
2055672fac9SKenneth D. Merry 
2065672fac9SKenneth D. Merry 
2078b8a9b1dSJustin T. Gibbs /*
2088b8a9b1dSJustin T. Gibbs  * Compare string with pattern, returning 0 on match.
2098b8a9b1dSJustin T. Gibbs  * Short pattern matches trailing blanks in name,
2108b8a9b1dSJustin T. Gibbs  * wildcard '*' in pattern matches rest of name,
2118b8a9b1dSJustin T. Gibbs  * wildcard '?' matches a single non-space character.
2128b8a9b1dSJustin T. Gibbs  */
2138b8a9b1dSJustin T. Gibbs int
2148b8a9b1dSJustin T. Gibbs cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
2158b8a9b1dSJustin T. Gibbs {
2168b8a9b1dSJustin T. Gibbs 
2178b8a9b1dSJustin T. Gibbs 	while (*pattern != '\0'&& str_len > 0) {
2188b8a9b1dSJustin T. Gibbs 
2198b8a9b1dSJustin T. Gibbs 		if (*pattern == '*') {
2208b8a9b1dSJustin T. Gibbs 			return (0);
2218b8a9b1dSJustin T. Gibbs 		}
2228b8a9b1dSJustin T. Gibbs 		if ((*pattern != *str)
2238b8a9b1dSJustin T. Gibbs 		 && (*pattern != '?' || *str == ' ')) {
2248b8a9b1dSJustin T. Gibbs 			return (1);
2258b8a9b1dSJustin T. Gibbs 		}
2268b8a9b1dSJustin T. Gibbs 		pattern++;
2278b8a9b1dSJustin T. Gibbs 		str++;
2288b8a9b1dSJustin T. Gibbs 		str_len--;
2298b8a9b1dSJustin T. Gibbs 	}
23030a4094fSAlexander Motin 	while (str_len > 0 && *str == ' ') {
23130a4094fSAlexander Motin 		str++;
2328b8a9b1dSJustin T. Gibbs 		str_len--;
23330a4094fSAlexander Motin 	}
23430a4094fSAlexander Motin 	if (str_len > 0 && *str == 0)
23530a4094fSAlexander Motin 		str_len = 0;
2368b8a9b1dSJustin T. Gibbs 
2378b8a9b1dSJustin T. Gibbs 	return (str_len);
2388b8a9b1dSJustin T. Gibbs }
2398b8a9b1dSJustin T. Gibbs 
2408b8a9b1dSJustin T. Gibbs caddr_t
2418b8a9b1dSJustin T. Gibbs cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
2428b8a9b1dSJustin T. Gibbs 	       int entry_size, cam_quirkmatch_t *comp_func)
2438b8a9b1dSJustin T. Gibbs {
2448b8a9b1dSJustin T. Gibbs 	for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
2458b8a9b1dSJustin T. Gibbs 		if ((*comp_func)(target, quirk_table) == 0)
2468b8a9b1dSJustin T. Gibbs 			return (quirk_table);
2478b8a9b1dSJustin T. Gibbs 	}
2488b8a9b1dSJustin T. Gibbs 	return (NULL);
2498b8a9b1dSJustin T. Gibbs }
2503393f8daSKenneth D. Merry 
2513393f8daSKenneth D. Merry const struct cam_status_entry*
2523393f8daSKenneth D. Merry cam_fetch_status_entry(cam_status status)
2533393f8daSKenneth D. Merry {
2543393f8daSKenneth D. Merry 	status &= CAM_STATUS_MASK;
2553393f8daSKenneth D. Merry 	return (bsearch(&status, &cam_status_table,
256*8dfea464SPedro F. Giffuni 			nitems(cam_status_table),
2573393f8daSKenneth D. Merry 			sizeof(*cam_status_table),
2583393f8daSKenneth D. Merry 			camstatusentrycomp));
2593393f8daSKenneth D. Merry }
2603393f8daSKenneth D. Merry 
2613393f8daSKenneth D. Merry static int
2623393f8daSKenneth D. Merry camstatusentrycomp(const void *key, const void *member)
2633393f8daSKenneth D. Merry {
2643393f8daSKenneth D. Merry 	cam_status status;
2653393f8daSKenneth D. Merry 	const struct cam_status_entry *table_entry;
2663393f8daSKenneth D. Merry 
2673393f8daSKenneth D. Merry 	status = *(const cam_status *)key;
2683393f8daSKenneth D. Merry 	table_entry = (const struct cam_status_entry *)member;
2693393f8daSKenneth D. Merry 
2703393f8daSKenneth D. Merry 	return (status - table_entry->status_code);
2713393f8daSKenneth D. Merry }
2723393f8daSKenneth D. Merry 
2733393f8daSKenneth D. Merry 
2743393f8daSKenneth D. Merry #ifdef _KERNEL
2753393f8daSKenneth D. Merry char *
2763393f8daSKenneth D. Merry cam_error_string(union ccb *ccb, char *str, int str_len,
2773393f8daSKenneth D. Merry 		 cam_error_string_flags flags,
2783393f8daSKenneth D. Merry 		 cam_error_proto_flags proto_flags)
2793393f8daSKenneth D. Merry #else /* !_KERNEL */
2803393f8daSKenneth D. Merry char *
2813393f8daSKenneth D. Merry cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
2823393f8daSKenneth D. Merry 		 int str_len, cam_error_string_flags flags,
2833393f8daSKenneth D. Merry 		 cam_error_proto_flags proto_flags)
2843393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */
2853393f8daSKenneth D. Merry {
2863393f8daSKenneth D. Merry 	char path_str[64];
2873393f8daSKenneth D. Merry 	struct sbuf sb;
2883393f8daSKenneth D. Merry 
2893393f8daSKenneth D. Merry 	if ((ccb == NULL)
2903393f8daSKenneth D. Merry 	 || (str == NULL)
2913393f8daSKenneth D. Merry 	 || (str_len <= 0))
2923393f8daSKenneth D. Merry 		return(NULL);
2933393f8daSKenneth D. Merry 
2943393f8daSKenneth D. Merry 	if (flags == CAM_ESF_NONE)
2953393f8daSKenneth D. Merry 		return(NULL);
2963393f8daSKenneth D. Merry 
2973393f8daSKenneth D. Merry 	switch (ccb->ccb_h.func_code) {
2988691755dSAlexander Motin 		case XPT_ATA_IO:
2998691755dSAlexander Motin 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
3008691755dSAlexander Motin 			case CAM_EPF_NONE:
3018691755dSAlexander Motin 				break;
3028691755dSAlexander Motin 			case CAM_EPF_ALL:
3038691755dSAlexander Motin 			case CAM_EPF_NORMAL:
3048691755dSAlexander Motin 				proto_flags |= CAM_EAF_PRINT_RESULT;
3058691755dSAlexander Motin 				/* FALLTHROUGH */
3068691755dSAlexander Motin 			case CAM_EPF_MINIMAL:
3078691755dSAlexander Motin 				proto_flags |= CAM_EAF_PRINT_STATUS;
3088691755dSAlexander Motin 				/* FALLTHROUGH */
3098691755dSAlexander Motin 			default:
3108691755dSAlexander Motin 				break;
3118691755dSAlexander Motin 			}
3128691755dSAlexander Motin 			break;
3133393f8daSKenneth D. Merry 		case XPT_SCSI_IO:
3143393f8daSKenneth D. Merry 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
3153393f8daSKenneth D. Merry 			case CAM_EPF_NONE:
3163393f8daSKenneth D. Merry 				break;
3173393f8daSKenneth D. Merry 			case CAM_EPF_ALL:
3183393f8daSKenneth D. Merry 			case CAM_EPF_NORMAL:
3193393f8daSKenneth D. Merry 				proto_flags |= CAM_ESF_PRINT_SENSE;
3203393f8daSKenneth D. Merry 				/* FALLTHROUGH */
3213393f8daSKenneth D. Merry 			case CAM_EPF_MINIMAL:
3223393f8daSKenneth D. Merry 				proto_flags |= CAM_ESF_PRINT_STATUS;
32307c6eac9SPoul-Henning Kamp 				/* FALLTHROUGH */
3243393f8daSKenneth D. Merry 			default:
3253393f8daSKenneth D. Merry 				break;
3263393f8daSKenneth D. Merry 			}
3273393f8daSKenneth D. Merry 			break;
32806e79492SKenneth D. Merry 		case XPT_SMP_IO:
32906e79492SKenneth D. Merry 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
33006e79492SKenneth D. Merry 			case CAM_EPF_NONE:
33106e79492SKenneth D. Merry 				break;
33206e79492SKenneth D. Merry 			case CAM_EPF_ALL:
33306e79492SKenneth D. Merry 				proto_flags |= CAM_ESMF_PRINT_FULL_CMD;
33406e79492SKenneth D. Merry 				/* FALLTHROUGH */
33506e79492SKenneth D. Merry 			case CAM_EPF_NORMAL:
33606e79492SKenneth D. Merry 			case CAM_EPF_MINIMAL:
33706e79492SKenneth D. Merry 				proto_flags |= CAM_ESMF_PRINT_STATUS;
33806e79492SKenneth D. Merry 				/* FALLTHROUGH */
33906e79492SKenneth D. Merry 			default:
34006e79492SKenneth D. Merry 				break;
34106e79492SKenneth D. Merry 			}
34206e79492SKenneth D. Merry 			break;
3433393f8daSKenneth D. Merry 		default:
3443393f8daSKenneth D. Merry 			break;
3453393f8daSKenneth D. Merry 	}
3463393f8daSKenneth D. Merry #ifdef _KERNEL
3473393f8daSKenneth D. Merry 	xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
3483393f8daSKenneth D. Merry #else /* !_KERNEL */
3493393f8daSKenneth D. Merry 	cam_path_string(device, path_str, sizeof(path_str));
3503393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */
3513393f8daSKenneth D. Merry 
3523393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
3533393f8daSKenneth D. Merry 
3543393f8daSKenneth D. Merry 	if (flags & CAM_ESF_COMMAND) {
3553393f8daSKenneth D. Merry 		sbuf_cat(&sb, path_str);
3563393f8daSKenneth D. Merry 		switch (ccb->ccb_h.func_code) {
3578691755dSAlexander Motin 		case XPT_ATA_IO:
3588691755dSAlexander Motin 			ata_command_sbuf(&ccb->ataio, &sb);
3598691755dSAlexander Motin 			sbuf_printf(&sb, "\n");
3608691755dSAlexander Motin 			break;
3613393f8daSKenneth D. Merry 		case XPT_SCSI_IO:
3623393f8daSKenneth D. Merry #ifdef _KERNEL
3633393f8daSKenneth D. Merry 			scsi_command_string(&ccb->csio, &sb);
3643393f8daSKenneth D. Merry #else /* !_KERNEL */
3653393f8daSKenneth D. Merry 			scsi_command_string(device, &ccb->csio, &sb);
3663393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */
3673393f8daSKenneth D. Merry 			sbuf_printf(&sb, "\n");
3683393f8daSKenneth D. Merry 			break;
36906e79492SKenneth D. Merry 		case XPT_SMP_IO:
37006e79492SKenneth D. Merry 			smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 -
37106e79492SKenneth D. Merry 					 strlen(path_str), (proto_flags &
37206e79492SKenneth D. Merry 					 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0);
37306e79492SKenneth D. Merry 			sbuf_printf(&sb, "\n");
37406e79492SKenneth D. Merry 			break;
3753393f8daSKenneth D. Merry 		default:
3763393f8daSKenneth D. Merry 			break;
3773393f8daSKenneth D. Merry 		}
3783393f8daSKenneth D. Merry 	}
3793393f8daSKenneth D. Merry 
3803393f8daSKenneth D. Merry 	if (flags & CAM_ESF_CAM_STATUS) {
3813393f8daSKenneth D. Merry 		cam_status status;
3823393f8daSKenneth D. Merry 		const struct cam_status_entry *entry;
3833393f8daSKenneth D. Merry 
3843393f8daSKenneth D. Merry 		sbuf_cat(&sb, path_str);
3853393f8daSKenneth D. Merry 
3863393f8daSKenneth D. Merry 		status = ccb->ccb_h.status & CAM_STATUS_MASK;
3873393f8daSKenneth D. Merry 
3883393f8daSKenneth D. Merry 		entry = cam_fetch_status_entry(status);
3893393f8daSKenneth D. Merry 
3903393f8daSKenneth D. Merry 		if (entry == NULL)
39183c5d981SAlexander Motin 			sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
3923393f8daSKenneth D. Merry 				    ccb->ccb_h.status);
3933393f8daSKenneth D. Merry 		else
39483c5d981SAlexander Motin 			sbuf_printf(&sb, "CAM status: %s\n",
3953393f8daSKenneth D. Merry 				    entry->status_text);
3963393f8daSKenneth D. Merry 	}
3973393f8daSKenneth D. Merry 
3983393f8daSKenneth D. Merry 	if (flags & CAM_ESF_PROTO_STATUS) {
3993393f8daSKenneth D. Merry 
4003393f8daSKenneth D. Merry 		switch (ccb->ccb_h.func_code) {
4018691755dSAlexander Motin 		case XPT_ATA_IO:
4028691755dSAlexander Motin 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
4038691755dSAlexander Motin 			     CAM_ATA_STATUS_ERROR)
4048691755dSAlexander Motin 				break;
4058691755dSAlexander Motin 			if (proto_flags & CAM_EAF_PRINT_STATUS) {
4068691755dSAlexander Motin 				sbuf_cat(&sb, path_str);
4078691755dSAlexander Motin 				ata_status_sbuf(&ccb->ataio, &sb);
4088691755dSAlexander Motin 				sbuf_printf(&sb, "\n");
4098691755dSAlexander Motin 			}
4108691755dSAlexander Motin 			if (proto_flags & CAM_EAF_PRINT_RESULT) {
4118691755dSAlexander Motin 				sbuf_cat(&sb, path_str);
412c9767ca8SScott Long 				sbuf_printf(&sb, "RES: ");
413c9767ca8SScott Long 				ata_res_sbuf(&ccb->ataio.res, &sb);
4148691755dSAlexander Motin 				sbuf_printf(&sb, "\n");
4158691755dSAlexander Motin 			}
4168691755dSAlexander Motin 
4178691755dSAlexander Motin 			break;
4183393f8daSKenneth D. Merry 		case XPT_SCSI_IO:
4193393f8daSKenneth D. Merry 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
4203393f8daSKenneth D. Merry 			     CAM_SCSI_STATUS_ERROR)
4213393f8daSKenneth D. Merry 				break;
4223393f8daSKenneth D. Merry 
4233393f8daSKenneth D. Merry 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
4243393f8daSKenneth D. Merry 				sbuf_cat(&sb, path_str);
42583c5d981SAlexander Motin 				sbuf_printf(&sb, "SCSI status: %s\n",
4263393f8daSKenneth D. Merry 					    scsi_status_string(&ccb->csio));
4273393f8daSKenneth D. Merry 			}
4283393f8daSKenneth D. Merry 
4293393f8daSKenneth D. Merry 			if ((proto_flags & CAM_ESF_PRINT_SENSE)
4303393f8daSKenneth D. Merry 			 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
4313393f8daSKenneth D. Merry 			 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
4323393f8daSKenneth D. Merry 
4333393f8daSKenneth D. Merry #ifdef _KERNEL
4343393f8daSKenneth D. Merry 				scsi_sense_sbuf(&ccb->csio, &sb,
4353393f8daSKenneth D. Merry 						SSS_FLAG_NONE);
4363393f8daSKenneth D. Merry #else /* !_KERNEL */
4373393f8daSKenneth D. Merry 				scsi_sense_sbuf(device, &ccb->csio, &sb,
4383393f8daSKenneth D. Merry 						SSS_FLAG_NONE);
4393393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */
4403393f8daSKenneth D. Merry 			}
4413393f8daSKenneth D. Merry 			break;
44206e79492SKenneth D. Merry 		case XPT_SMP_IO:
44306e79492SKenneth D. Merry 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
44406e79492SKenneth D. Merry 			     CAM_SMP_STATUS_ERROR)
44506e79492SKenneth D. Merry 				break;
44606e79492SKenneth D. Merry 
44706e79492SKenneth D. Merry 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
44806e79492SKenneth D. Merry 				sbuf_cat(&sb, path_str);
44906e79492SKenneth D. Merry 				sbuf_printf(&sb, "SMP status: %s (%#x)\n",
45006e79492SKenneth D. Merry 				    smp_error_desc(ccb->smpio.smp_response[2]),
45106e79492SKenneth D. Merry 						   ccb->smpio.smp_response[2]);
45206e79492SKenneth D. Merry 			}
45306e79492SKenneth D. Merry 			/* There is no SMP equivalent to SCSI sense. */
45406e79492SKenneth D. Merry 			break;
4553393f8daSKenneth D. Merry 		default:
4563393f8daSKenneth D. Merry 			break;
4573393f8daSKenneth D. Merry 		}
4583393f8daSKenneth D. Merry 	}
4593393f8daSKenneth D. Merry 
4603393f8daSKenneth D. Merry 	sbuf_finish(&sb);
4613393f8daSKenneth D. Merry 
4623393f8daSKenneth D. Merry 	return(sbuf_data(&sb));
4633393f8daSKenneth D. Merry }
4643393f8daSKenneth D. Merry 
4653393f8daSKenneth D. Merry #ifdef _KERNEL
4663393f8daSKenneth D. Merry 
4673393f8daSKenneth D. Merry void
4683393f8daSKenneth D. Merry cam_error_print(union ccb *ccb, cam_error_string_flags flags,
4693393f8daSKenneth D. Merry 		cam_error_proto_flags proto_flags)
4703393f8daSKenneth D. Merry {
4713393f8daSKenneth D. Merry 	char str[512];
4723393f8daSKenneth D. Merry 
4733393f8daSKenneth D. Merry 	printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
4743393f8daSKenneth D. Merry 	       proto_flags));
4753393f8daSKenneth D. Merry }
4763393f8daSKenneth D. Merry 
4773393f8daSKenneth D. Merry #else /* !_KERNEL */
4783393f8daSKenneth D. Merry 
4793393f8daSKenneth D. Merry void
4803393f8daSKenneth D. Merry cam_error_print(struct cam_device *device, union ccb *ccb,
4813393f8daSKenneth D. Merry 		cam_error_string_flags flags, cam_error_proto_flags proto_flags,
4823393f8daSKenneth D. Merry 		FILE *ofile)
4833393f8daSKenneth D. Merry {
4843393f8daSKenneth D. Merry 	char str[512];
4853393f8daSKenneth D. Merry 
4863393f8daSKenneth D. Merry 	if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
4873393f8daSKenneth D. Merry 		return;
4883393f8daSKenneth D. Merry 
4893393f8daSKenneth D. Merry 	fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
4903393f8daSKenneth D. Merry 		flags, proto_flags));
4913393f8daSKenneth D. Merry }
4923393f8daSKenneth D. Merry 
4933393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */
494141bdcc1SNate Lawson 
495141bdcc1SNate Lawson /*
496141bdcc1SNate Lawson  * Common calculate geometry fuction
497141bdcc1SNate Lawson  *
498141bdcc1SNate Lawson  * Caller should set ccg->volume_size and block_size.
499141bdcc1SNate Lawson  * The extended parameter should be zero if extended translation
500141bdcc1SNate Lawson  * should not be used.
501141bdcc1SNate Lawson  */
502141bdcc1SNate Lawson void
503141bdcc1SNate Lawson cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
504141bdcc1SNate Lawson {
505141bdcc1SNate Lawson 	uint32_t size_mb, secs_per_cylinder;
506141bdcc1SNate Lawson 
507bae3cbf0SMatt Jacob 	if (ccg->block_size == 0) {
508bae3cbf0SMatt Jacob 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
509bae3cbf0SMatt Jacob 		return;
510bae3cbf0SMatt Jacob 	}
511bae3cbf0SMatt Jacob 	size_mb = (1024L * 1024L) / ccg->block_size;
512bae3cbf0SMatt Jacob 	if (size_mb == 0) {
513bae3cbf0SMatt Jacob 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
514bae3cbf0SMatt Jacob 		return;
515bae3cbf0SMatt Jacob 	}
516bae3cbf0SMatt Jacob 	size_mb = ccg->volume_size / size_mb;
517141bdcc1SNate Lawson 	if (size_mb > 1024 && extended) {
518141bdcc1SNate Lawson 		ccg->heads = 255;
519141bdcc1SNate Lawson 		ccg->secs_per_track = 63;
520141bdcc1SNate Lawson 	} else {
521141bdcc1SNate Lawson 		ccg->heads = 64;
522141bdcc1SNate Lawson 		ccg->secs_per_track = 32;
523141bdcc1SNate Lawson 	}
524141bdcc1SNate Lawson 	secs_per_cylinder = ccg->heads * ccg->secs_per_track;
525bae3cbf0SMatt Jacob 	if (secs_per_cylinder == 0) {
526bae3cbf0SMatt Jacob 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
527bae3cbf0SMatt Jacob 		return;
528bae3cbf0SMatt Jacob 	}
529141bdcc1SNate Lawson 	ccg->cylinders = ccg->volume_size / secs_per_cylinder;
530141bdcc1SNate Lawson 	ccg->ccb_h.status = CAM_REQ_CMP;
531141bdcc1SNate Lawson }
532