920070cc8e063a98c3ad1aa4678b89fd84aee8b2
[dragonfly.git] / sys / bus / cam / cam.c
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  * $FreeBSD: src/sys/cam/cam.c,v 1.4 2001/03/27 05:45:09 ken Exp $
29  * $DragonFly: src/sys/bus/cam/cam.c,v 1.8 2007/12/02 03:16:52 pavalos Exp $
30  */
31 #include <sys/param.h>
32 #ifdef _KERNEL
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 #else
37 #include <stdlib.h>
38 #include <stdio.h>
39 #endif
40
41 #include "cam.h"
42 #include "cam_ccb.h"
43 #include "scsi/scsi_all.h"
44 #include <sys/sbuf.h>
45
46 #ifdef _KERNEL
47 #include <sys/libkern.h>
48 #include "cam_xpt.h"
49 #endif
50
51 static int      camstatusentrycomp(const void *key, const void *member);
52
53 const struct cam_status_entry cam_status_table[] = {
54         { CAM_REQ_INPROG,        "CCB request is in progress"                },
55         { CAM_REQ_CMP,           "CCB request completed without error"       },
56         { CAM_REQ_ABORTED,       "CCB request aborted by the host"           },
57         { CAM_UA_ABORT,          "Unable to abort CCB request"               },
58         { CAM_REQ_CMP_ERR,       "CCB request completed with an error"       },
59         { CAM_BUSY,              "CAM subsytem is busy"                      },
60         { CAM_REQ_INVALID,       "CCB request was invalid"                   },
61         { CAM_PATH_INVALID,      "Supplied Path ID is invalid"               },
62         { CAM_DEV_NOT_THERE,     "Device Not Present"                        },
63         { CAM_UA_TERMIO,         "Unable to terminate I/O CCB request"       },
64         { CAM_SEL_TIMEOUT,       "Selection Timeout"                         },
65         { CAM_CMD_TIMEOUT,       "Command timeout"                           },
66         { CAM_SCSI_STATUS_ERROR, "SCSI Status Error"                         },
67         { CAM_MSG_REJECT_REC,    "Message Reject Reveived"                   },
68         { CAM_SCSI_BUS_RESET,    "SCSI Bus Reset Sent/Received"              },
69         { CAM_UNCOR_PARITY,      "Uncorrectable parity/CRC error"            },
70         { CAM_AUTOSENSE_FAIL,    "Auto-Sense Retrieval Failed"               },
71         { CAM_NO_HBA,            "No HBA Detected"                           },
72         { CAM_DATA_RUN_ERR,      "Data Overrun error"                        },
73         { CAM_UNEXP_BUSFREE,     "Unexpected Bus Free"                       },
74         { CAM_SEQUENCE_FAIL,     "Target Bus Phase Sequence Failure"         },
75         { CAM_CCB_LEN_ERR,       "CCB length supplied is inadequate"         },
76         { CAM_PROVIDE_FAIL,      "Unable to provide requested capability"    },
77         { CAM_BDR_SENT,          "SCSI BDR Message Sent"                     },
78         { CAM_REQ_TERMIO,        "CCB request terminated by the host"        },
79         { CAM_UNREC_HBA_ERROR,   "Unrecoverable Host Bus Adapter Error"      },
80         { CAM_REQ_TOO_BIG,       "The request was too large for this host"   },
81         { CAM_REQUEUE_REQ,       "Unconditionally Re-queue Request",         },
82         { CAM_IDE,               "Initiator Detected Error Message Received" },
83         { CAM_RESRC_UNAVAIL,     "Resource Unavailable"                      },
84         { CAM_UNACKED_EVENT,     "Unacknowledged Event by Host"              },
85         { CAM_MESSAGE_RECV,      "Message Received in Host Target Mode"      },
86         { CAM_INVALID_CDB,       "Invalid CDB received in Host Target Mode"  },
87         { CAM_LUN_INVALID,       "Invalid Lun"                               },
88         { CAM_TID_INVALID,       "Invalid Target ID"                         },
89         { CAM_FUNC_NOTAVAIL,     "Function Not Available"                    },
90         { CAM_NO_NEXUS,          "Nexus Not Established"                     },
91         { CAM_IID_INVALID,       "Invalid Initiator ID"                      },
92         { CAM_CDB_RECVD,         "CDB Received"                              },
93         { CAM_LUN_ALRDY_ENA,     "LUN Already Enabled for Target Mode"       },
94         { CAM_SCSI_BUSY,         "SCSI Bus Busy"                             },
95 };
96
97 const int num_cam_status_entries =
98     sizeof(cam_status_table)/sizeof(*cam_status_table);
99
100 #ifdef _KERNEL
101 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
102 #endif
103
104 void
105 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
106 {
107
108         /* Trim leading/trailing spaces, nulls. */
109         while (srclen > 0 && src[0] == ' ')
110                 src++, srclen--;
111         while (srclen > 0
112             && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
113                 srclen--;
114
115         while (srclen > 0 && dstlen > 1) {
116                 u_int8_t *cur_pos = dst;
117
118                 if (*src < 0x20 || *src >= 0x80) {
119                         /* SCSI-II Specifies that these should never occur. */
120                         /* non-printable character */
121                         if (dstlen > 4) {
122                                 *cur_pos++ = '\\';
123                                 *cur_pos++ = ((*src & 0300) >> 6) + '0';
124                                 *cur_pos++ = ((*src & 0070) >> 3) + '0';
125                                 *cur_pos++ = ((*src & 0007) >> 0) + '0';
126                         } else {
127                                 *cur_pos++ = '?';
128                         }
129                 } else {
130                         /* normal character */
131                         *cur_pos++ = *src;
132                 }
133                 src++;
134                 srclen--;
135                 dstlen -= cur_pos - dst;
136                 dst = cur_pos;
137         }
138         *dst = '\0';
139 }
140
141 /*
142  * Compare string with pattern, returning 0 on match.
143  * Short pattern matches trailing blanks in name,
144  * wildcard '*' in pattern matches rest of name,
145  * wildcard '?' matches a single non-space character.
146  */
147 int
148 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
149 {
150
151         while (*pattern != '\0'&& str_len > 0) {  
152
153                 if (*pattern == '*') {
154                         return (0);
155                 }
156                 if ((*pattern != *str)
157                  && (*pattern != '?' || *str == ' ')) {
158                         return (1);
159                 }
160                 pattern++;
161                 str++;
162                 str_len--;
163         }
164         while (str_len > 0 && *str++ == ' ')
165                 str_len--;
166
167         return (str_len);
168 }
169
170 caddr_t
171 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
172                int entry_size, cam_quirkmatch_t *comp_func)
173 {
174         for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
175                 if ((*comp_func)(target, quirk_table) == 0)
176                         return (quirk_table);
177         }
178         return (NULL);
179 }
180
181 const struct cam_status_entry*
182 cam_fetch_status_entry(cam_status status)
183 {
184         status &= CAM_STATUS_MASK;
185         return (bsearch(&status, &cam_status_table,
186                         num_cam_status_entries,
187                         sizeof(*cam_status_table),
188                         camstatusentrycomp));
189 }
190
191 static int
192 camstatusentrycomp(const void *key, const void *member)
193 {
194         cam_status status;
195         const struct cam_status_entry *table_entry;
196
197         status = *(const cam_status *)key;
198         table_entry = (const struct cam_status_entry *)member;
199
200         return (status - table_entry->status_code);
201 }
202
203
204 #ifdef _KERNEL
205 char *
206 cam_error_string(union ccb *ccb, char *str, int str_len,
207                  cam_error_string_flags flags,
208                  cam_error_proto_flags proto_flags)
209 #else /* !_KERNEL */
210 char *
211 cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
212                  int str_len, cam_error_string_flags flags,
213                  cam_error_proto_flags proto_flags)
214 #endif /* _KERNEL/!_KERNEL */
215 {
216         char path_str[64];
217         struct sbuf sb;
218
219         if ((ccb == NULL)
220          || (str == NULL)
221          || (str_len <= 0))
222                 return(NULL);
223
224         if (flags == CAM_ESF_NONE)
225                 return(NULL);
226
227         switch (ccb->ccb_h.func_code) {
228                 case XPT_SCSI_IO:
229                         switch (proto_flags & CAM_EPF_LEVEL_MASK) {
230                         case CAM_EPF_NONE:
231                                 break;
232                         case CAM_EPF_ALL:
233                         case CAM_EPF_NORMAL:
234                                 proto_flags |= CAM_ESF_PRINT_SENSE;
235                                 /* FALLTHROUGH */
236                         case CAM_EPF_MINIMAL:
237                                 proto_flags |= CAM_ESF_PRINT_STATUS;
238                         default:
239                                 break;
240                         }
241                         break;
242                 default:
243                         break;
244         }
245 #ifdef _KERNEL
246         xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
247 #else /* !_KERNEL */
248         cam_path_string(device, path_str, sizeof(path_str));
249 #endif /* _KERNEL/!_KERNEL */
250
251         sbuf_new(&sb, str, str_len, 0);
252
253         if (flags & CAM_ESF_COMMAND) {
254
255                 sbuf_cat(&sb, path_str);
256
257                 switch (ccb->ccb_h.func_code) {
258                 case XPT_SCSI_IO:
259 #ifdef _KERNEL
260                         scsi_command_string(&ccb->csio, &sb);
261 #else /* !_KERNEL */
262                         scsi_command_string(device, &ccb->csio, &sb);
263 #endif /* _KERNEL/!_KERNEL */
264                         sbuf_printf(&sb, "\n");
265
266                         break;
267                 default:
268                         break;
269                 }
270         }
271
272         if (flags & CAM_ESF_CAM_STATUS) {
273                 cam_status status;
274                 const struct cam_status_entry *entry;
275
276                 sbuf_cat(&sb, path_str);
277
278                 status = ccb->ccb_h.status & CAM_STATUS_MASK;
279
280                 entry = cam_fetch_status_entry(status);
281
282                 if (entry == NULL)
283                         sbuf_printf(&sb, "CAM Status: Unknown (%#x)\n",
284                                     ccb->ccb_h.status);
285                 else
286                         sbuf_printf(&sb, "CAM Status: %s\n",
287                                     entry->status_text);
288         }
289
290         if (flags & CAM_ESF_PROTO_STATUS) {
291
292                 switch (ccb->ccb_h.func_code) {
293                 case XPT_SCSI_IO:
294                         if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
295                              CAM_SCSI_STATUS_ERROR)
296                                 break;
297
298                         if (proto_flags & CAM_ESF_PRINT_STATUS) {
299                                 sbuf_cat(&sb, path_str);
300                                 /*
301                                  * Print out the SCSI status byte as long as
302                                  * the user wants some protocol output.
303                                  */
304                                 sbuf_printf(&sb, "SCSI Status: %s\n",
305                                             scsi_status_string(&ccb->csio));
306                         }
307
308                         if ((proto_flags & CAM_ESF_PRINT_SENSE)
309                          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
310                          && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
311
312 #ifdef _KERNEL
313                                 scsi_sense_sbuf(&ccb->csio, &sb,
314                                                 SSS_FLAG_NONE);
315 #else /* !_KERNEL */
316                                 scsi_sense_sbuf(device, &ccb->csio, &sb,
317                                                 SSS_FLAG_NONE);
318 #endif /* _KERNEL/!_KERNEL */
319                         }
320                         break;
321                 default:
322                         break;
323                 }
324         }
325
326         sbuf_finish(&sb);
327
328         return(sbuf_data(&sb));
329 }
330
331 #ifdef _KERNEL
332
333 void
334 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
335                 cam_error_proto_flags proto_flags)
336 {
337         char str[512];
338
339         kprintf("%s", cam_error_string(ccb, str, sizeof(str), flags,
340                proto_flags));
341 }
342
343 #else /* !_KERNEL */
344
345 void
346 cam_error_print(struct cam_device *device, union ccb *ccb,
347                 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
348                 FILE *ofile)
349 {
350         char str[512];
351
352         if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
353                 return;
354
355         fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
356                 flags, proto_flags));
357 }
358
359 #endif /* _KERNEL/!_KERNEL */
360
361 /*
362  * Common calculate geometry fuction
363  *
364  * Caller should set ccg->volume_size and block_size.
365  * The extended parameter should be zero if extended translation
366  * should not be used.
367  */
368 void
369 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
370 {
371         uint32_t size_mb, secs_per_cylinder;
372
373         if (ccg->block_size == 0) {
374                 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
375                 return;
376         }
377         size_mb = (1024L * 1024L) / ccg->block_size;
378         if (size_mb == 0) {
379                 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
380                 return;
381         }
382         size_mb = ccg->volume_size / size_mb;
383         if (size_mb > 1024 && extended) {
384                 ccg->heads = 255;
385                 ccg->secs_per_track = 63;
386         } else {
387                 ccg->heads = 64;
388                 ccg->secs_per_track = 32;
389         }
390         secs_per_cylinder = ccg->heads * ccg->secs_per_track;
391         if (secs_per_cylinder == 0) {
392                 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
393                 return;
394         }
395         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
396         ccg->ccb_h.status = CAM_REQ_CMP;
397 }
398