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

Cache object: 8f0131fcad47bb2dbb0ef3480fff6d6e


[ 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.