The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/cam/cam.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: b5cf2a97b5c25bc46a1ed7bb6061c8c4


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.