xref: /freebsd/sys/cam/cam.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1 /*-
2  * Generic utility routines for the Common Access Method layer.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #ifdef _KERNEL
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
37 #else /* _KERNEL */
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <camlib.h>
42 #endif /* _KERNEL */
43 
44 #include <cam/cam.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/smp_all.h>
48 #include <sys/sbuf.h>
49 
50 #ifdef _KERNEL
51 #include <sys/libkern.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_xpt.h>
54 
55 FEATURE(scbus, "SCSI devices support");
56 
57 #endif
58 
59 static int	camstatusentrycomp(const void *key, const void *member);
60 
61 const struct cam_status_entry cam_status_table[] = {
62 	{ CAM_REQ_INPROG,	 "CCB request is in progress"		     },
63 	{ CAM_REQ_CMP,		 "CCB request completed without error"	     },
64 	{ CAM_REQ_ABORTED,	 "CCB request aborted by the host"	     },
65 	{ CAM_UA_ABORT,		 "Unable to abort CCB request"		     },
66 	{ CAM_REQ_CMP_ERR,	 "CCB request completed with an error"	     },
67 	{ CAM_BUSY,		 "CAM subsystem is busy"		     },
68 	{ CAM_REQ_INVALID,	 "CCB request was invalid"		     },
69 	{ CAM_PATH_INVALID,	 "Supplied Path ID is invalid"		     },
70 	{ CAM_DEV_NOT_THERE,	 "Device Not Present"			     },
71 	{ CAM_UA_TERMIO,	 "Unable to terminate I/O CCB request"	     },
72 	{ CAM_SEL_TIMEOUT,	 "Selection Timeout"			     },
73 	{ CAM_CMD_TIMEOUT,	 "Command timeout"			     },
74 	{ CAM_SCSI_STATUS_ERROR, "SCSI Status Error"			     },
75 	{ CAM_MSG_REJECT_REC,	 "Message Reject Reveived"		     },
76 	{ CAM_SCSI_BUS_RESET,	 "SCSI Bus Reset Sent/Received"		     },
77 	{ CAM_UNCOR_PARITY,	 "Uncorrectable parity/CRC error"	     },
78 	{ CAM_AUTOSENSE_FAIL,	 "Auto-Sense Retrieval Failed"		     },
79 	{ CAM_NO_HBA,		 "No HBA Detected"			     },
80 	{ CAM_DATA_RUN_ERR,	 "Data Overrun error"			     },
81 	{ CAM_UNEXP_BUSFREE,	 "Unexpected Bus Free"			     },
82 	{ CAM_SEQUENCE_FAIL,	 "Target Bus Phase Sequence Failure"	     },
83 	{ CAM_CCB_LEN_ERR,	 "CCB length supplied is inadequate"	     },
84 	{ CAM_PROVIDE_FAIL,	 "Unable to provide requested capability"    },
85 	{ CAM_BDR_SENT,		 "SCSI BDR Message Sent"		     },
86 	{ CAM_REQ_TERMIO,	 "CCB request terminated by the host"	     },
87 	{ CAM_UNREC_HBA_ERROR,	 "Unrecoverable Host Bus Adapter Error"	     },
88 	{ CAM_REQ_TOO_BIG,	 "The request was too large for this host"   },
89 	{ CAM_REQUEUE_REQ,	 "Unconditionally Re-queue Request",	     },
90 	{ CAM_ATA_STATUS_ERROR,	 "ATA Status Error"			     },
91 	{ CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost"               },
92 	{ CAM_SMP_STATUS_ERROR,	 "SMP Status Error"                          },
93 	{ CAM_IDE,		 "Initiator Detected Error Message Received" },
94 	{ CAM_RESRC_UNAVAIL,	 "Resource Unavailable"			     },
95 	{ CAM_UNACKED_EVENT,	 "Unacknowledged Event by Host"		     },
96 	{ CAM_MESSAGE_RECV,	 "Message Received in Host Target Mode"	     },
97 	{ CAM_INVALID_CDB,	 "Invalid CDB received in Host Target Mode"  },
98 	{ CAM_LUN_INVALID,	 "Invalid Lun"				     },
99 	{ CAM_TID_INVALID,	 "Invalid Target ID"			     },
100 	{ CAM_FUNC_NOTAVAIL,	 "Function Not Available"		     },
101 	{ CAM_NO_NEXUS,		 "Nexus Not Established"		     },
102 	{ CAM_IID_INVALID,	 "Invalid Initiator ID"			     },
103 	{ CAM_CDB_RECVD,	 "CDB Received"				     },
104 	{ CAM_LUN_ALRDY_ENA,	 "LUN Already Enabled for Target Mode"	     },
105 	{ CAM_SCSI_BUSY,	 "SCSI Bus Busy"			     },
106 };
107 
108 const int num_cam_status_entries =
109     sizeof(cam_status_table)/sizeof(*cam_status_table);
110 
111 #ifdef _KERNEL
112 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
113 #endif
114 
115 void
116 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
117 {
118 
119 	/* Trim leading/trailing spaces, nulls. */
120 	while (srclen > 0 && src[0] == ' ')
121 		src++, srclen--;
122 	while (srclen > 0
123 	    && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
124 		srclen--;
125 
126 	while (srclen > 0 && dstlen > 1) {
127 		u_int8_t *cur_pos = dst;
128 
129 		if (*src < 0x20 || *src >= 0x80) {
130 			/* SCSI-II Specifies that these should never occur. */
131 			/* non-printable character */
132 			if (dstlen > 4) {
133 				*cur_pos++ = '\\';
134 				*cur_pos++ = ((*src & 0300) >> 6) + '0';
135 				*cur_pos++ = ((*src & 0070) >> 3) + '0';
136 				*cur_pos++ = ((*src & 0007) >> 0) + '0';
137 			} else {
138 				*cur_pos++ = '?';
139 			}
140 		} else {
141 			/* normal character */
142 			*cur_pos++ = *src;
143 		}
144 		src++;
145 		srclen--;
146 		dstlen -= cur_pos - dst;
147 		dst = cur_pos;
148 	}
149 	*dst = '\0';
150 }
151 
152 /*
153  * Compare string with pattern, returning 0 on match.
154  * Short pattern matches trailing blanks in name,
155  * wildcard '*' in pattern matches rest of name,
156  * wildcard '?' matches a single non-space character.
157  */
158 int
159 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
160 {
161 
162 	while (*pattern != '\0'&& str_len > 0) {
163 
164 		if (*pattern == '*') {
165 			return (0);
166 		}
167 		if ((*pattern != *str)
168 		 && (*pattern != '?' || *str == ' ')) {
169 			return (1);
170 		}
171 		pattern++;
172 		str++;
173 		str_len--;
174 	}
175 	while (str_len > 0 && *str == ' ') {
176 		str++;
177 		str_len--;
178 	}
179 	if (str_len > 0 && *str == 0)
180 		str_len = 0;
181 
182 	return (str_len);
183 }
184 
185 caddr_t
186 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
187 	       int entry_size, cam_quirkmatch_t *comp_func)
188 {
189 	for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
190 		if ((*comp_func)(target, quirk_table) == 0)
191 			return (quirk_table);
192 	}
193 	return (NULL);
194 }
195 
196 const struct cam_status_entry*
197 cam_fetch_status_entry(cam_status status)
198 {
199 	status &= CAM_STATUS_MASK;
200 	return (bsearch(&status, &cam_status_table,
201 			num_cam_status_entries,
202 			sizeof(*cam_status_table),
203 			camstatusentrycomp));
204 }
205 
206 static int
207 camstatusentrycomp(const void *key, const void *member)
208 {
209 	cam_status status;
210 	const struct cam_status_entry *table_entry;
211 
212 	status = *(const cam_status *)key;
213 	table_entry = (const struct cam_status_entry *)member;
214 
215 	return (status - table_entry->status_code);
216 }
217 
218 
219 #ifdef _KERNEL
220 char *
221 cam_error_string(union ccb *ccb, char *str, int str_len,
222 		 cam_error_string_flags flags,
223 		 cam_error_proto_flags proto_flags)
224 #else /* !_KERNEL */
225 char *
226 cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
227 		 int str_len, cam_error_string_flags flags,
228 		 cam_error_proto_flags proto_flags)
229 #endif /* _KERNEL/!_KERNEL */
230 {
231 	char path_str[64];
232 	struct sbuf sb;
233 
234 	if ((ccb == NULL)
235 	 || (str == NULL)
236 	 || (str_len <= 0))
237 		return(NULL);
238 
239 	if (flags == CAM_ESF_NONE)
240 		return(NULL);
241 
242 	switch (ccb->ccb_h.func_code) {
243 		case XPT_ATA_IO:
244 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
245 			case CAM_EPF_NONE:
246 				break;
247 			case CAM_EPF_ALL:
248 			case CAM_EPF_NORMAL:
249 				proto_flags |= CAM_EAF_PRINT_RESULT;
250 				/* FALLTHROUGH */
251 			case CAM_EPF_MINIMAL:
252 				proto_flags |= CAM_EAF_PRINT_STATUS;
253 				/* FALLTHROUGH */
254 			default:
255 				break;
256 			}
257 			break;
258 		case XPT_SCSI_IO:
259 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
260 			case CAM_EPF_NONE:
261 				break;
262 			case CAM_EPF_ALL:
263 			case CAM_EPF_NORMAL:
264 				proto_flags |= CAM_ESF_PRINT_SENSE;
265 				/* FALLTHROUGH */
266 			case CAM_EPF_MINIMAL:
267 				proto_flags |= CAM_ESF_PRINT_STATUS;
268 				/* FALLTHROUGH */
269 			default:
270 				break;
271 			}
272 			break;
273 		case XPT_SMP_IO:
274 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
275 			case CAM_EPF_NONE:
276 				break;
277 			case CAM_EPF_ALL:
278 				proto_flags |= CAM_ESMF_PRINT_FULL_CMD;
279 				/* FALLTHROUGH */
280 			case CAM_EPF_NORMAL:
281 			case CAM_EPF_MINIMAL:
282 				proto_flags |= CAM_ESMF_PRINT_STATUS;
283 				/* FALLTHROUGH */
284 			default:
285 				break;
286 			}
287 			break;
288 		default:
289 			break;
290 	}
291 #ifdef _KERNEL
292 	xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
293 #else /* !_KERNEL */
294 	cam_path_string(device, path_str, sizeof(path_str));
295 #endif /* _KERNEL/!_KERNEL */
296 
297 	sbuf_new(&sb, str, str_len, 0);
298 
299 	if (flags & CAM_ESF_COMMAND) {
300 		sbuf_cat(&sb, path_str);
301 		switch (ccb->ccb_h.func_code) {
302 		case XPT_ATA_IO:
303 			ata_command_sbuf(&ccb->ataio, &sb);
304 			sbuf_printf(&sb, "\n");
305 			break;
306 		case XPT_SCSI_IO:
307 #ifdef _KERNEL
308 			scsi_command_string(&ccb->csio, &sb);
309 #else /* !_KERNEL */
310 			scsi_command_string(device, &ccb->csio, &sb);
311 #endif /* _KERNEL/!_KERNEL */
312 			sbuf_printf(&sb, "\n");
313 			break;
314 		case XPT_SMP_IO:
315 			smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 -
316 					 strlen(path_str), (proto_flags &
317 					 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0);
318 			sbuf_printf(&sb, "\n");
319 			break;
320 		default:
321 			break;
322 		}
323 	}
324 
325 	if (flags & CAM_ESF_CAM_STATUS) {
326 		cam_status status;
327 		const struct cam_status_entry *entry;
328 
329 		sbuf_cat(&sb, path_str);
330 
331 		status = ccb->ccb_h.status & CAM_STATUS_MASK;
332 
333 		entry = cam_fetch_status_entry(status);
334 
335 		if (entry == NULL)
336 			sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
337 				    ccb->ccb_h.status);
338 		else
339 			sbuf_printf(&sb, "CAM status: %s\n",
340 				    entry->status_text);
341 	}
342 
343 	if (flags & CAM_ESF_PROTO_STATUS) {
344 
345 		switch (ccb->ccb_h.func_code) {
346 		case XPT_ATA_IO:
347 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
348 			     CAM_ATA_STATUS_ERROR)
349 				break;
350 			if (proto_flags & CAM_EAF_PRINT_STATUS) {
351 				sbuf_cat(&sb, path_str);
352 				ata_status_sbuf(&ccb->ataio, &sb);
353 				sbuf_printf(&sb, "\n");
354 			}
355 			if (proto_flags & CAM_EAF_PRINT_RESULT) {
356 				sbuf_cat(&sb, path_str);
357 				ata_res_sbuf(&ccb->ataio, &sb);
358 				sbuf_printf(&sb, "\n");
359 			}
360 
361 			break;
362 		case XPT_SCSI_IO:
363 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
364 			     CAM_SCSI_STATUS_ERROR)
365 				break;
366 
367 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
368 				sbuf_cat(&sb, path_str);
369 				sbuf_printf(&sb, "SCSI status: %s\n",
370 					    scsi_status_string(&ccb->csio));
371 			}
372 
373 			if ((proto_flags & CAM_ESF_PRINT_SENSE)
374 			 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
375 			 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
376 
377 #ifdef _KERNEL
378 				scsi_sense_sbuf(&ccb->csio, &sb,
379 						SSS_FLAG_NONE);
380 #else /* !_KERNEL */
381 				scsi_sense_sbuf(device, &ccb->csio, &sb,
382 						SSS_FLAG_NONE);
383 #endif /* _KERNEL/!_KERNEL */
384 			}
385 			break;
386 		case XPT_SMP_IO:
387 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
388 			     CAM_SMP_STATUS_ERROR)
389 				break;
390 
391 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
392 				sbuf_cat(&sb, path_str);
393 				sbuf_printf(&sb, "SMP status: %s (%#x)\n",
394 				    smp_error_desc(ccb->smpio.smp_response[2]),
395 						   ccb->smpio.smp_response[2]);
396 			}
397 			/* There is no SMP equivalent to SCSI sense. */
398 			break;
399 		default:
400 			break;
401 		}
402 	}
403 
404 	sbuf_finish(&sb);
405 
406 	return(sbuf_data(&sb));
407 }
408 
409 #ifdef _KERNEL
410 
411 void
412 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
413 		cam_error_proto_flags proto_flags)
414 {
415 	char str[512];
416 
417 	printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
418 	       proto_flags));
419 }
420 
421 #else /* !_KERNEL */
422 
423 void
424 cam_error_print(struct cam_device *device, union ccb *ccb,
425 		cam_error_string_flags flags, cam_error_proto_flags proto_flags,
426 		FILE *ofile)
427 {
428 	char str[512];
429 
430 	if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
431 		return;
432 
433 	fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
434 		flags, proto_flags));
435 }
436 
437 #endif /* _KERNEL/!_KERNEL */
438 
439 /*
440  * Common calculate geometry fuction
441  *
442  * Caller should set ccg->volume_size and block_size.
443  * The extended parameter should be zero if extended translation
444  * should not be used.
445  */
446 void
447 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
448 {
449 	uint32_t size_mb, secs_per_cylinder;
450 
451 	if (ccg->block_size == 0) {
452 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
453 		return;
454 	}
455 	size_mb = (1024L * 1024L) / ccg->block_size;
456 	if (size_mb == 0) {
457 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
458 		return;
459 	}
460 	size_mb = ccg->volume_size / size_mb;
461 	if (size_mb > 1024 && extended) {
462 		ccg->heads = 255;
463 		ccg->secs_per_track = 63;
464 	} else {
465 		ccg->heads = 64;
466 		ccg->secs_per_track = 32;
467 	}
468 	secs_per_cylinder = ccg->heads * ccg->secs_per_track;
469 	if (secs_per_cylinder == 0) {
470 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
471 		return;
472 	}
473 	ccg->cylinders = ccg->volume_size / secs_per_cylinder;
474 	ccg->ccb_h.status = CAM_REQ_CMP;
475 }
476