Emulex Logo
OneCoreā„¢ Storage SDK Release 11.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ocs_ramd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015, Emulex
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 /**
34  * @file
35  * Implement the SCSI RAMD target-server.
36  */
37 
38 /**
39  * @defgroup io_sm RAMD Target IO State Machine
40  * @defgroup scsi_cmd SCSI Command Processing
41  * @defgroup scsi_api_target SCSI Target
42  */
43 
44 #include "ocs.h"
45 #include "ocs_ramd.h"
46 #include "ocs_ramdisc.h"
47 #include "scsi_cmds.h"
48 #include "ocs_mgmt.h"
49 
50 /**
51  * @brief A structure that contains the status and extended status fields.
52  */
53 typedef struct {
54  uint32_t status;
55  uint32_t ext_status;
57 
58 /* globals */
59 static ocs_lock_t ramd_lock;
60 static ocs_ramdisc_t **ocs_ramd = NULL;
61 static uint32_t ocs_ramd_count = 0;
62 static int ramd_ref_cnt = 0;
63 
64 typedef struct {
66  uint32_t tgt_id;
68  uint32_t rdisc_count;
70 
71 
72 #define INVALID_APP_TAG 0xffffffff
73 #define RAMD_APP_TAG 0x0
74 
75 #define IOFMT "[%04x][i:%0*x t:%0*x h:%04x][c:%02x]"
76 #define IOFMT_ITT_SIZE(ocs) ((ocs->ocs_xport == OCS_XPORT_FC) ? 4 : 8)
77 #define IOFMT_ARGS(io) io->instance_index, IOFMT_ITT_SIZE(io->ocs), io->init_task_tag, IOFMT_ITT_SIZE(io->ocs), io->tgt_task_tag, io->hw_tag, io->tgt_io.cdb[0]
78 
79 #define io_sm_trace() \
80  do { \
81  if (OCS_LOG_ENABLE_SCSI_TGT_TRACE(io->node->ocs)) \
82  ocs_log_info(io->ocs, "[%s]" IOFMT " (IO) %-20s %-20s\n", \
83  (io->node ? io->node->display_name : ""), \
84  IOFMT_ARGS(io), __func__, ocs_sm_event_name(evt)); \
85  } while (0)
86 
87 #define tmfio_sm_trace() \
88  do { \
89  if (OCS_LOG_ENABLE_SCSI_TGT_TRACE(tmfio->ocs)) \
90  ocs_log_info(tmfio->ocs, "[%s]" IOFMT " (TMF) %-20s %-20s\n", \
91  (tmfio->node ? tmfio->node->display_name : ""), \
92  IOFMT_ARGS(tmfio), __func__, ocs_sm_event_name(evt)); \
93  } while (0)
94 
95 #define io_printf(io, fmt, ...) \
96  do { \
97  ocs_log_debug(io->ocs, "[%s]" IOFMT " %-20s " fmt, \
98  (io->node ? io->node->display_name : ""), \
99  IOFMT_ARGS(io), __func__, ##__VA_ARGS__); \
100  } while (0)
101 
102 #define OCS_RAMD_MIN_SIZE (1024*1024)
103 
104 #define ENABLE_CMD_TRACE 0
105 #define ENABLE_API_TRACE 0
106 #define ENABLE_BUFFER_DUMP 0
107 
108 #define trace(io, enable_flag, fmt, ...) \
109  do { \
110  if (enable_flag) { \
111  if (io) { \
112  ocs_log_debug(NULL, "[%s]" IOFMT " %s: " fmt, io->node->display_name, \
113  IOFMT_ARGS(io), __func__, ##__VA_ARGS__); \
114  } else { \
115  ocs_log_debug(NULL, "%s: " fmt, __func__, ##__VA_ARGS__); \
116  } \
117  } \
118  } while(0)
119 
120 #define cmd_trace(io, fmt, ...) trace(io, ENABLE_CMD_TRACE, fmt, ##__VA_ARGS__)
121 
122 #define api_trace(...) \
123  do { \
124  if (ENABLE_API_TRACE) \
125  ocs_log_debug(NULL, "%s:\n", __func__); \
126  } while(0)
127 
128 #ifndef container_of
129 #define container_of(p, stype, field) ((stype *)(((uint8_t *)(p)) - offsetof(stype, field)))
130 #endif
131 
132 #if defined(OCS_INCLUDE_ISCSI)
133 #define ACTIVE_IO_COUNT(ocs) ocs_atomic_read(&ocs->io_active_count)
134 #else /* For FC*/
135 #define ACTIVE_IO_COUNT(ocs) ocs_atomic_read(&ocs->xport->io_active_count)
136 #endif
137 
138 typedef enum {
154 
155 static const char *ocs_ramd_state_names[] = {
156  "__ocs_ramd_io_init",
157  "__ocs_ramd_io_init_err_injection",
158  "__ocs_ramd_io_wait_first_burst",
159  "__ocs_ramd_io_wait_dataphase_compl",
160  "__ocs_ramd_io_wait_and_validate_dataphase",
161  "__ocs_ramd_io_wait_and_complete_writesame_dataphase",
162  "__ocs_ramd_io_wait_resp_compl",
163  "__ocs_ramd_io_aborting",
164  "__ocs_ramd_io_aborting_wait_io_cmpl",
165  "__ocs_ramd_io_aborting_wait_abort_cmpl",
166  "__ocs_ramd_tmf_wait_resp_cmpl",
167  "__ocs_ramd_abts_wait_io_abort_resp",
168  "__ocs_ramd_wait_abts_acc_cmpl",
169  "__ocs_ramd_io_wait_first_burst_send_resp"
170 };
171 
172 static void * __ocs_ramd_io_init(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
173 static void * __ocs_ramd_io_init_err_injection(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
174 static void * __ocs_ramd_io_wait_first_burst(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
175 static void * __ocs_ramd_io_common(const char *funcname, ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
176 static void * __ocs_ramd_io_wait_dataphase_compl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
177 static void * __ocs_ramd_io_wait_and_validate_dataphase(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
178 static void * __ocs_ramd_io_wait_and_complete_writesame_dataphase(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
179 static void * __ocs_ramd_io_wait_resp_compl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
180 static void * __ocs_ramd_io_aborting(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
181 static void * __ocs_ramd_io_aborting_wait_io_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
182 static void * __ocs_ramd_io_aborting_wait_abort_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
183 static void *__ocs_ramd_tmf_wait_resp_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
184 static void *__ocs_ramd_abts_wait_io_abort_resp(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
185 static void *__ocs_ramd_wait_abts_acc_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
186 static void * __ocs_ramd_io_wait_first_burst_send_resp(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg);
187 static int32_t ocs_ramd_data_phase_start(ocs_io_t *io);
188 
189 /* SCSI command handlers */
190 static ocs_ramd_hndlr_rtn_e ocs_ramd_write6(ocs_io_t *io, uint8_t cdb[]);
191 static ocs_ramd_hndlr_rtn_e ocs_ramd_write10(ocs_io_t *io, uint8_t cdb[]);
192 static ocs_ramd_hndlr_rtn_e ocs_ramd_write12(ocs_io_t *io, uint8_t cdb[]);
193 static ocs_ramd_hndlr_rtn_e ocs_ramd_write16(ocs_io_t *io, uint8_t cdb[]);
194 static ocs_ramd_hndlr_rtn_e ocs_ramd_write32(ocs_io_t *io, uint8_t cdb[]);
195 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_long10(ocs_io_t *io, uint8_t cdb[]);
196 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_long16(ocs_io_t *io, uint8_t cdb[]);
197 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same10(ocs_io_t *io, uint8_t cdb[]);
198 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same16(ocs_io_t *io, uint8_t cdb[]);
199 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same32(ocs_io_t *io, uint8_t cdb[]);
200 static ocs_ramd_hndlr_rtn_e ocs_ramd_read6(ocs_io_t *io, uint8_t cdb[]);
201 static ocs_ramd_hndlr_rtn_e ocs_ramd_read10(ocs_io_t *io, uint8_t cdb[]);
202 static ocs_ramd_hndlr_rtn_e ocs_ramd_read12(ocs_io_t *io, uint8_t cdb[]);
203 static ocs_ramd_hndlr_rtn_e ocs_ramd_read16(ocs_io_t *io, uint8_t cdb[]);
204 static ocs_ramd_hndlr_rtn_e ocs_ramd_read32(ocs_io_t *io, uint8_t cdb[]);
205 static ocs_ramd_hndlr_rtn_e ocs_ramd_read_long10(ocs_io_t *io, uint8_t cdb[]);
206 static ocs_ramd_hndlr_rtn_e ocs_ramd_read_long16(ocs_io_t *io, uint8_t cdb[]);
207 static ocs_ramd_hndlr_rtn_e ocs_ramd_inquiry(ocs_io_t *io, uint8_t cdb[]);
208 static ocs_ramd_hndlr_rtn_e ocs_ramd_test_unit_ready(ocs_io_t *io, uint8_t cdb[]);
209 static ocs_ramd_hndlr_rtn_e ocs_ramd_reserve_release(ocs_io_t *io, uint8_t cdb[]);
210 static ocs_ramd_hndlr_rtn_e ocs_ramd_read_capacity10(ocs_io_t *io, uint8_t cdb[]);
211 static ocs_ramd_hndlr_rtn_e ocs_ramd_read_capacity16(ocs_io_t *io, uint8_t cdb[]);
212 static ocs_ramd_hndlr_rtn_e ocs_ramd_report_luns(ocs_io_t *io, uint8_t cdb[]);
213 static ocs_ramd_hndlr_rtn_e ocs_ramd_format_unit(ocs_io_t *io, uint8_t cdb[]);
214 static ocs_ramd_hndlr_rtn_e ocs_ramd_mode_select(ocs_io_t *io, uint8_t cdb[]);
215 static ocs_ramd_hndlr_rtn_e ocs_ramd_request_sense(ocs_io_t *io, uint8_t cdb[]);
216 
217 static int32_t ocs_ramd_abort_node_tasks(ocs_node_t *nodes, ocs_io_t *io);
218 static int32_t ocs_ramd_abort_node_tasks_lun(ocs_node_t *node, uint32_t lun, ocs_io_t *io);
219 
220 static uint32_t scsi_validate_format_unit(ocs_io_t *io);
221 static int32_t scsi_format_unit_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg);
222 static uint32_t scsi_validate_mode_select(ocs_io_t *io);
223 static int32_t scsi_mode_select_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg);
225 static void ocs_scsi_ramd_free(ocs_t *ocs, ocs_ramdisc_t ***rdisc, uint32_t *rdisc_count);
226 
227 static ocs_ramd_hndlr_rtn_e ocs_ramd_read_common(ocs_io_t *io, uint64_t lba, uint32_t block_count, uint8_t rdprotect,
228  uint32_t app_tag, uint32_t ref_tag);
229 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_common(ocs_io_t *io, uint64_t lba, uint32_t block_count, uint8_t wrprotect,
230  uint32_t app_tag, uint32_t ref_tag);
231 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same_common(ocs_io_t *io, uint64_t lba, uint32_t block_count,
232  uint8_t wrprotect, uint8_t pbdata, uint8_t lbdata,
233  uint32_t app_tag, uint32_t ref_tag);
234 static ocs_ramd_hndlr_rtn_e ocs_ramd_write_long_common(ocs_io_t *io, uint64_t lba, uint16_t xfer_len,
235  uint8_t cor_dis, uint8_t wr_uncor, uint8_t pblock);
236 static ocs_ramd_hndlr_rtn_e ocs_ramd_read_long_common(ocs_io_t *io, uint64_t lba, uint16_t xfer_len,
237  uint8_t pblock, uint8_t corrct);
238 static int32_t ocs_ramd_io_error(ocs_io_t *io, ocs_scsi_io_status_e scsi_status);
239 static int32_t ocs_ramd_abort_tgt_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg);
240 static int32_t ocs_mgmt_read_block(ocs_t *ocs, char *, void *, uint32_t, void *, uint32_t);
241 
242 /**
243  * @brief A structure used to map a handler function to a SCSI sommand.
244  */
245 typedef struct {
246  uint8_t check_lun;
247  uint8_t data_dir;
248  uint32_t scsi_cmd;
249  ocs_ramd_hndlr_rtn_e (*handler)(ocs_io_t *io, uint8_t cdb[]);
251 
252 #define DATA_DIR_NONE 0x00
253 #define DATA_DIR_READ 0x01
254 #define DATA_DIR_WRITE 0x02
255 #define DATA_DIR_RDWR 0x03
257 
264  /* WRITE 32 is a Variable Length CDB */
275  /* READ 32 is a Variable Length CDB */
277  /* READ CAPACITY 16 is a SERVICE ACTION */
288  /* VERIFY and WRITE_AND_VERIFY commands intentionally omitted */
290 };
291 
295  };
296 
299  };
300 
306 };
307 
308 /* Management API */
309 void ocs_mgmt_ramd_list(ocs_textbuf_t *textbuf, void *object);
310 void ocs_mgmt_ramd_get_all(ocs_textbuf_t *textbuf, void *object);
311 int ocs_mgmt_ramd_get(ocs_textbuf_t *textbuf, char *parent, char *name, void *object);
312 int ocs_mgmt_ramd_exec(char *parent, char *action, void *arg_in, uint32_t arg_in_length,
313  void *arg_out, uint32_t arg_out_length, void *object);
316  .get_handler = ocs_mgmt_ramd_get,
317  .get_all_handler = ocs_mgmt_ramd_get_all,
318  .exec_handler = ocs_mgmt_ramd_exec,
319 };
320 
321 /**
322  * @brief Ignore abort event.
323  *
324  * @param evt Abort event to be ignored.
325  * @param func The calling function name.
326  * @param io Pointer to the IO context that received the abort
327  * event.
328  * @param tmfio Pointer to the TMF IO context (if ABTS).
329  *
330  * @return None.
331  */
332 static void
333 ocs_ignore_abort_evt(ocs_sm_event_t evt, const char *func,
334  ocs_io_t *io, ocs_io_t *tmfio)
335 {
336  io_printf(io, "%s: ignore evt: %-20s\n", func, ocs_sm_event_name(evt));
337 
338  if (tmfio) {
339  /* Acknowledge the abort event */
340  ocs_sm_post_event(&tmfio->tgt_io.sm, OCS_EVT_IO_ABORT_IGNORED, NULL);
341  }
342 }
343 
344 /**
345  * @ingroup scsi_api_target
346  * @brief Driver-wide initialization for the target-server.
347  *
348  * @par Description
349  * Called by the OS initialization prior to the PCI device discovery.
350  *
351  * @return Returns 0 on success, or a negative error code value on failure.
352  */
353 int32_t
355 {
356  uint32_t i;
357  scsi_cmd_table_t *pcmd;
358  uint32_t ramd_threading = 0;
359  uint32_t thread_cmds = 0;
360  char buf[32];
361 
362  ocs_lock_init(NULL, &ramd_lock, "RAM disc alloc lock");
363 
364  /* Build scsi cmd lookup */
365  ocs_memset(scsi_cmd_lookup, 0, sizeof(scsi_cmd_lookup));
366  for (i = 0, pcmd = scsi_cmd_table; i < ARRAY_SIZE(scsi_cmd_table); i++, pcmd++) {
367  scsi_cmd_lookup[pcmd->scsi_cmd] = pcmd;
368  }
369 
370  if (ocs_get_property("ramd_threading", buf, sizeof(buf)) == 0) {
371  ramd_threading = ocs_strtoul(buf, 0, 0);
372  ocs_log_debug(NULL, "ramd_threading: %d\n", ramd_threading);
373  }
374 
375  if (ocs_get_property("thread_cmds", buf, sizeof(buf)) == 0) {
376  thread_cmds = ocs_strtoul(buf, 0, 0);
377  ocs_log_debug(NULL, "thread_cmds: %d\n", thread_cmds);
378  }
379 
380  if (thread_cmds && !ramd_threading) {
381  ocs_log_test(NULL, "Can't thread_cmds without enabling ramd_threading\n");
382  return -1;
383  }
384 
385  /* Start a bunch of server threads */
386  ocs_ramd_start_threads(ramd_threading, thread_cmds);
387 
388  return 0;
389 }
390 
391 /**
392  * @ingroup scsi_api_target
393  * @brief Driver-wide clean-up for the target-server.
394  *
395  * @par Description
396  * Called by the OS driver-wide exit/clean-up.
397  *
398  * @return Returns 0 on success, or a negative error code value on failure.
399  */
400 int32_t
402 {
404  return 0;
405 }
406 
407 /**
408  * @brief Set SCST IO state
409  *
410  * @param io pointer to SCSI IO object
411  * @param state to be set
412  *
413  * @return TGT iostate
414  */
415 uint32_t ocs_scsi_tgt_io_state(ocs_io_t *io)
416 {
417  return 0;
418 }
419 
420 /**
421  * @brief Initialize SCSI IO.
422  *
423  * @par Description
424  * Initialize SCSI IO, this function is called once per IO during IO pool
425  * allocation so that the target server may initialize any of its own private
426  * data.
427  *
428  * @param io Pointer to SCSI IO object.
429  *
430  * @return returns 0 for success, a negative error code value for failure.
431  */
432 int32_t
433 ocs_scsi_tgt_io_init(ocs_io_t *io)
434 {
435  ocs_lock_init(io->ocs, &io->tgt_io.sm_lock, "iosmlock:%p", io);
436  return 0;
437 }
438 
439 /**
440  * @brief Un-initialize SCSI IO.
441  *
442  * @par Description
443  * Un-initialize target server private data in a SCSI io object.
444  *
445  * @param io Pointer to SCSI IO object.
446  *
447  * @return Returns 0 on success, or a negative error code value on failure.
448  */
449 int32_t
450 ocs_scsi_tgt_io_exit(ocs_io_t *io)
451 {
452  ocs_lock_free(&io->tgt_io.sm_lock);
453  return 0;
454 }
455 
456 /**
457  * @brief Given an IO and a LUN number, return the ramdisc to which the IO is directed.
458  *
459  * @par Description
460  * From the IO, get the ocs, then the target private area for the ocs.
461  * Check if a global RAMD (one RAMD for all SLI Ports) is used. If
462  * so, then find the RAMD in the target private area. If not, then go from
463  * IO to node to SLI Port to find the SLI Port's list of ramdiscs.
464  *
465  * @param io Pointer to the IO context.
466  * @param lun LUN for the command.
467  *
468  * @return Returns a pointer to the ramdisc, or NULL if @c lun is not valid.
469  */
471 ocs_ramd_lookup(ocs_io_t *io, uint32_t lun)
472 {
473  ocs_t *ocs = io->ocs;
474  ocs_ramdisc_t *rval = NULL;
475 
476  if (ocs->tgt_ocs.use_global_ramd) {
477  if (lun < ocs->tgt_ocs.rdisc_count) {
478  rval = ocs->tgt_ocs.rdisc[lun];
479  }
480  } else {
481  ocs_node_t *node = io->node;
482 
483  if (node != NULL) {
484  ocs_sport_t *sport = node->sport;
485  if (sport != NULL) {
486 
487  if (lun < sport->tgt_sport.rdisc_count) {
488  rval = sport->tgt_sport.rdisc[lun];
489  }
490  }
491  }
492  }
493  return rval;
494 }
495 
496 /**
497  * @brief Given an TGT number and a LUN number, return the corresponding ramdisc
498  *
499  *@par Description
500  * This function is used by the mgmt interface to access ramdisc data out-of-band
501  * rather than through a SCSI IO. The TGT number is assumed to correspond to
502  * a sport on the first domain. TGT 0 is domain[1]/sport[1], TGT 1 is
503  * domain[1]/sport[2], etc.
504  * @n @n
505  * If the global RAMD is being used then the TGT number must be 0.
506  * In this case the ramdisc for LUN n is at ocs->tgt_ocs.rdisc[n].
507  *
508  * If per-port RAMD is being used then the TGT number is used to
509  * find the correct sport, then LUN n is at sport->tgt_sport.rdisc[n].
510  *
511  * @param ocs Pointer to the ocs.
512  * @param tgt TGT number, 0..n.
513  * @param lun LUN number, 0..n.
514  *
515  * @return Returns a pointer to a ramdisc structure on success, or
516  * NULL on failure.
517  */
519 ocs_ramd_lookup_by_tgt(ocs_t *ocs, uint32_t tgt, uint32_t lun)
520 {
521  ocs_ramdisc_t *rval = NULL;
522 
523  if (ocs->tgt_ocs.use_global_ramd) {
524  if (tgt == 0) {
525  if (lun < ocs->tgt_ocs.rdisc_count) {
526  rval = ocs->tgt_ocs.rdisc[lun];
527  }
528  }
529  } else {
530  ocs_domain_t *domain = ocs_domain_get_instance(ocs, 1);
531  if (domain != NULL) {
532  ocs_sport_t *sport = ocs_sport_get_instance(domain, tgt+1);
533  if (sport != NULL) {
534  if (lun < sport->tgt_sport.rdisc_count) {
535  rval = sport->tgt_sport.rdisc[lun];
536  }
537  }
538  }
539  }
540  return rval;
541 }
542 
543 /**
544  * @brief Create a RAMD (target) based on the property parameter values.
545  *
546  * @param ocs Pointer to the ocs.
547  * @param rdisc Pointer to the ramdisc to allocate.
548  * @param rdisc_count Pointer to the number of ramdiscs (LUNs) allocated.
549  *
550  * @return Returns 0 on success, or a negative error code value on failure.
551  */
552 static int32_t
553 ocs_scsi_ramd_create(ocs_t *ocs, ocs_ramdisc_t ***rdisc, uint32_t *rdisc_count)
554 {
555  char prop_buf[32];
556  uint32_t num_luns = 1;
557  uint32_t ramdisc_blocksize = 512;
558  uint64_t ramdisc_size = 50*1024*1024;
559  uint64_t ramdisc_blockcount = 0;
560  uint32_t max_sge_size;
561  uint32_t i;
562  int32_t rc = 0;
563  uint8_t p_type = 0;
564  uint8_t prot_en = 0;
565  uint8_t dif_separate = 0;
566  uint8_t external_dif = 0;
567  char t10_vendor_id[8] = { 0 };
568  uint32_t dif_capable;
569 
570  // Obtain device's DIF capability
571  dif_capable = ocs_scsi_get_property(ocs, OCS_SCSI_DIF_CAPABLE);
572 
573  if (ocs_get_property("ramdisc_size", prop_buf, sizeof(prop_buf)) == 0) {
574  char *p;
575  ramdisc_size = ocs_strtoul(prop_buf, &p, 0);
576  switch(*p) {
577  case 'g': case 'G':
578  ramdisc_size *= 1024;
579  /* fallthrough */
580  case 'm': case 'M':
581  ramdisc_size *= 1024;
582  /* fallthrough */
583  case 'k': case 'K':
584  ramdisc_size *= 1024;
585  break;
586  }
587  if (ramdisc_size < OCS_RAMD_MIN_SIZE) {
588  ocs_log_warn(ocs, "Ramdisc size %s (%" PRIu64 ") too small. Min size %u.\n",
589  prop_buf, ramdisc_size, OCS_RAMD_MIN_SIZE);
590  return -1;
591  }
592  }
593  if (ocs_get_property("ramdisc_blocksize", prop_buf, sizeof(prop_buf)) == 0) {
594  ramdisc_blocksize = ocs_strtoul(prop_buf, 0, 0);
595  }
596  if (ocs_get_property("num_luns", prop_buf, sizeof(prop_buf)) == 0) {
597  num_luns = ocs_strtoul(prop_buf, 0, 0);
598  }
599  if (ocs_get_property("p_type", prop_buf, sizeof(prop_buf)) == 0) {
600  p_type = ocs_strtoul(prop_buf, 0, 0);
601  if (p_type != 0) {
602  // Enable only if the device is capable of DIF
603  if (dif_capable) {
604  prot_en = 1;
605  } else {
606  ocs_log_warn(ocs, "WARNING: RAMD: Cannot initialize with DIF, port is not DIF capable\n");
607  prot_en = 0;
608  p_type = 0;
609  }
610  }
611  }
612  /* User provided "external_dif" parameter will implicitly enables DIF processing.
613  * So it is necessary to verify the SLI port is capable of supporting DIF.
614  */
615  if (ocs_get_property("external_dif", prop_buf, sizeof(prop_buf)) == 0) {
616  if (dif_capable) {
617  external_dif = ocs_strlen(prop_buf) > 0;
618  if (external_dif) {
619  if (ocs_strlen(prop_buf) > sizeof(t10_vendor_id)) {
620  ocs_log_err(ocs,"External dif vendor ID size exceeded the MAX size. Max size is : %lu\n",
621  sizeof(t10_vendor_id));
622  }
623 
624  ocs_strncpy(t10_vendor_id, prop_buf, sizeof(t10_vendor_id));
625  prot_en = 1;
626  if (p_type == 0) {
627  p_type = 1;
628  }
629  }
630  } else {
631  ocs_log_err(ocs,"Device does not support DIF, ignoring user's external dif request.\n");
632  }
633  }
634 
635  if (ocs_get_property("dif_separate", prop_buf, sizeof(prop_buf)) == 0) {
636  dif_separate = ocs_strtoul(prop_buf, 0, 0);
637  }
638 
639  ramdisc_blockcount = ramdisc_size / ramdisc_blocksize;
640  ocs_log_debug(ocs, "Ramdisc: num_luns:%d blocksize:%d blockcount:%" PRIu64 " (%" PRIu64 " mb)\n",
641  num_luns, ramdisc_blocksize, ramdisc_blockcount, (ramdisc_blocksize*ramdisc_blockcount) / (1024*1024));
642 
643  *rdisc = ocs_malloc(ocs, sizeof(ocs_ramdisc_t*) * num_luns, OCS_M_NOWAIT | OCS_M_ZERO);
644  ocs_assert(*rdisc, -1);
645  *rdisc_count = num_luns;
646 
647  max_sge_size = ocs->tgt_ocs.max_sge;
648  if (max_sge_size > OCS_MAX_DMA_ALLOC)
649  max_sge_size = OCS_MAX_DMA_ALLOC;
650 
651  for (i = 0; i < num_luns; i ++) {
652  if (i) {
653  /**
654  * In case we need to allocate multiple ramdiscs in huge sizes, this entire loop
655  * might take more than '21' secs to execute. As a result, 'rcu_sched' can detect
656  * a stall and send an NMI. To avoid such scenarios, yield the CPU.
657  */
658  ocs_thread_yield(NULL);
659  }
660 
661  (*rdisc)[i] = (ocs_ramdisc_t*) ocs_malloc(ocs, sizeof(ocs_ramdisc_t), OCS_M_NOWAIT | OCS_M_ZERO);
662  if ((*rdisc)[i] == NULL) {
663  ocs_log_err(ocs, "Error: ocs_malloc rdisc[%d] failed\n", i);
664 
665  /* If we failed to allocate part of the LUNs we need to tear down
666  * the entire target. When we return failure the caller will assume the
667  * target wasn't created.
668  */
669 
670  ocs_scsi_ramd_free(ocs, rdisc, rdisc_count);
671  return -1;
672  }
673  rc = ocs_new_ramdisc(ocs, (*rdisc)[i], ramdisc_blocksize,
674  max_sge_size/ramdisc_blocksize, ramdisc_size / ramdisc_blocksize);
675  if (rc) {
676  ocs_log_err(ocs, "Error: ocs_new_ramdisc failed allocating LUN %d\n", i);
677 
678  /* If we failed to allocate part of the LUNs we need to tear down
679  * the entire target. When we return failure the caller will assume the
680  * target wasn't created.
681  */
682 
683  ocs_scsi_ramd_free(ocs, rdisc, rdisc_count);
684 
685  return -1;
686  }
687 
688  (*rdisc)[i]->lun = i;
689 
690  /* Post a Unit Attention saying we were powered on or reset */
691  ocs_ramd_post_ua(ocs, (*rdisc)[i],
694 
695  if (dif_separate) {
696  ocs_log_debug(ocs, "Ramdisc: Enabling DIF separate\n");
697  (*rdisc)[i]->force_dif_separate = TRUE;
698  }
699 
700  if (external_dif) {
701  ocs_log_debug(ocs, "Ramdisc: Enabling proprietary end-to-end protection\n");
702  (*rdisc)[i]->external_dif = TRUE;
703  ocs_strncpy((*rdisc)[i]->t10_vendor_id, t10_vendor_id, sizeof((*rdisc)[i]->t10_vendor_id));
704  }
705 
706  ocs_format_ramdisc(ocs, (*rdisc)[i], ramdisc_blocksize, p_type, prot_en, dif_separate);
707  }
708  return 0;
709 }
710 
711 /**
712  * @brief Free the memory associate with a ramdisc.
713  *
714  * @param ocs Pointer to the ocs.
715  * @param rdisc Pointer to the ramdisc to allocate.
716  * @param rdisc_count Pointer to the number of ramdiscs (LUNs) allocated.
717  */
718 static void
719 ocs_scsi_ramd_free(ocs_t *ocs, ocs_ramdisc_t ***rdisc, uint32_t *rdisc_count)
720 {
721  uint32_t i;
722 
723  if (*rdisc == NULL) {
724  return;
725  }
726 
727  ocs_log_debug(ocs, "freeing ramd, count=%d\n", *rdisc_count);
728  for (i = 0; i < *rdisc_count; i ++) {
729  if ((*rdisc)[i] != NULL) {
730  ocs_del_ramdisc(ocs, (*rdisc)[i]);
731  ocs_free(ocs, (*rdisc)[i], sizeof(ocs_ramdisc_t));
732  }
733  }
734  ocs_free(ocs, *rdisc, sizeof(ocs_ramdisc_t*) * (*rdisc_count));
735 
736  *rdisc = NULL;
737  *rdisc_count = 0;
738 }
739 
740 
741 /**
742  * @ingroup scsi_api_target
743  * @brief Initializes any target fields on the ocs structure.
744  *
745  * @par Description
746  * Called by the OS initialization code when a new device is discovered.
747  *
748  * @param ocs Pointer to the ocs.
749  *
750  * @return Returns 0 on success, or a negative error code value on failure.
751  */
752 int32_t
754 {
755  int32_t rc = 0;
756  uint32_t total_ios;
757  char prop_buf[32];
758 
759  ocs_assert(ocs, -1);
760  api_trace();
761 
762  /* Get the max settings */
763  ocs->tgt_ocs.max_sge = ocs_scsi_get_property(ocs, OCS_SCSI_MAX_SGE);
764  ocs->tgt_ocs.max_sgl = ocs_scsi_get_property(ocs, OCS_SCSI_MAX_SGL);
765 
766  /* initialize IO watermark fields */
767  total_ios = ocs_scsi_get_property(ocs, OCS_SCSI_MAX_IOS);
768  ocs_log_debug(ocs, "total_ios=%d\n", total_ios);
769  ocs->tgt_ocs.watermark_min = (total_ios * OCS_WATERMARK_LOW_PCT) / 100;
770  ocs->tgt_ocs.watermark_max = (total_ios * OCS_WATERMARK_HIGH_PCT) / 100;
771  ocs_atomic_init(&ocs->tgt_ocs.io_high_watermark, ocs->tgt_ocs.watermark_max);
772  ocs_atomic_init(&ocs->tgt_ocs.watermark_hit, 0);
773  ocs_atomic_init(&ocs->tgt_ocs.initiator_count, 0);
774 
775  /* See if we are using the global ramdisk */
776  ocs->tgt_ocs.use_global_ramd = TRUE;
777  if (ocs_get_property("global_ramdisc", prop_buf, sizeof(prop_buf)) == 0) {
778  ocs->tgt_ocs.use_global_ramd = ocs_strtoul(prop_buf, NULL, 0);
779  }
780 
781 #if defined(OCS_INCLUDE_FC)
782  ocs->fc_type = FC_TYPE_FCP;
783 #endif
784 
785  if (ocs->tgt_ocs.use_global_ramd) {
786  /*
787  * initialize RAM disk; will only do this once and all
788  * other PCI functions will use the same RAM Disk
789  */
790  ocs_lock(&ramd_lock);
791  if (ramd_ref_cnt) {
792  ocs_log_debug(ocs, "initialized subsequent RAM disk\n");
793  ocs->tgt_ocs.rdisc = ocs_ramd;
794  ocs->tgt_ocs.rdisc_count = ocs_ramd_count;
795  ramd_ref_cnt++;
796  ocs_unlock(&ramd_lock);
797  return 0;
798  }
799  rc = ocs_scsi_ramd_create(ocs, &ocs->tgt_ocs.rdisc, &ocs->tgt_ocs.rdisc_count);
800 
801  if (rc == 0) {
802  ocs_log_debug(ocs, "initialized first RAM disk count=%d\n", ocs->tgt_ocs.rdisc_count);
803  ocs_ramd = ocs->tgt_ocs.rdisc;
804  ocs_ramd_count = ocs->tgt_ocs.rdisc_count;
805  ramd_ref_cnt++;
806  }
807  ocs_unlock(&ramd_lock);
808  }
809 
810  ocs->tgt_mgmt_functions = &ramd_mgmt_functions;
811 
812  ocs->tgt_ocs.enable_task_set_full = ocs_scsi_get_property(ocs, OCS_SCSI_ENABLE_TASK_SET_FULL);
813  ocs_log_debug(ocs, "Ramdisc: task set full processing is %s\n",
814  ocs->tgt_ocs.enable_task_set_full ? "enabled" : "disabled");
815 
816  ocs_port_rdisc_init(ocs);
817 
818  /* Register a bounce callback even if not needed */
820 
821  return rc;
822 }
823 
824 /**
825  * @ingroup scsi_api_target
826  * @brief Tears down the target members of the ocs structure.
827  *
828  * @par Description
829  * Called by the OS code when the device is removed.
830  *
831  * @param ocs Pointer to the ocs.
832  *
833  * @return Returns 0 on success, or a negative error code value on failure.
834  */
835 int32_t
837 {
838  api_trace();
839 
840  ocs_assert(ocs, -1);
841 
842  if (ocs->tgt_ocs.use_global_ramd && ocs->tgt_ocs.rdisc) {
843  ocs_lock(&ramd_lock);
844  if (--ramd_ref_cnt) {
845  ocs_unlock(&ramd_lock);
846  /* still other devices using RAM disk, just exit */
847  ocs_log_debug(ocs, "decremented ramd ref count %d\n",
848  ramd_ref_cnt);
849  } else {
850  ocs_ramd = NULL;
851  ocs_ramd_count = 0;
852  ocs_unlock(&ramd_lock);
853  ocs_scsi_ramd_free(ocs, &ocs->tgt_ocs.rdisc, &ocs->tgt_ocs.rdisc_count);
854  }
855  } else if (!ocs->tgt_ocs.use_global_ramd) {
856  ocs_port_rdisc_free(ocs);
857  }
858  return 0;
859 }
860 
861 /**
862  * @ingroup scsi_api_target
863  * @brief Accept a new domain notification.
864  *
865  * @par Description
866  * Called by the base driver when a new domain is discovered. A target-server
867  * uses this call to prepare for the new remote node notifications
868  * coming from the ocs_scsi_new_initiator().
869  * The domain context has the ocs_scsi_tgt_domain_t structure, which is
870  * declared by the target-server code and used for target-server private data.<br><br>
871  * @b Note: This function will only be called if the base driver has been enabled for target
872  * capability. For initiator-client back-ends, the ocs_scsi_ini_new_domain()
873  * function is called.
874  *
875  * @param domain Pointer to the domain.
876  *
877  * @return Returns 0 on success, or a negative error code value on failure.
878  */
879 int32_t
880 ocs_scsi_tgt_new_domain(ocs_domain_t *domain)
881 {
882  int32_t rc = 0;
883  api_trace();
884  return rc;
885 }
886 
887 /**
888  * @ingroup scsi_api_target
889  * @brief Accept a domain lost notification.
890  *
891  * @par Description
892  * Called by the base driver when a domain goes away. A target-server
893  * uses this call to clean up all domain-scoped resources.<br><br>
894  * @b Note: For initiator-client back-ends, the ocs_scsi_ini_del_domain()
895  * function is called.
896  *
897  * @param domain Pointer to the domain.
898  *
899  * @return None.
900  */
901 void
902 ocs_scsi_tgt_del_domain(ocs_domain_t *domain)
903 {
904  api_trace();
905  ocs_assert(domain);
906 }
907 
908 /**
909  * @ingroup scsi_api_target
910  * @brief Accept a new SLI Port notification.
911  *
912  * @par Description
913  * Called by the base driver when a new SLI Port (sport) is discovered. A target-server
914  * uses this call to prepare for new remote node notifications
915  * arising from ocs_scsi_new_initiator().<br><br>
916  * @b Note: This function will only be called if the base driver has been enabled for
917  * target capability. For initiator-client backends, the ocs_scsi_ini_new_sport()
918  * function is called.
919  *
920  * @param sport Pointer to the sport.
921  *
922  * @return Returns 0 on success, or a negative error code value on failure.
923  */
924 int32_t ocs_scsi_tgt_new_sport(ocs_sport_t *sport)
925 {
926  ocs_t *ocs = sport->ocs;
927  int32_t rc = 0;
928 
929  ocs_log_debug(ocs, "targ: New SPORT: %s\n", sport->display_name);
930 
931  /* initialize sPort RAM disk if this feature is enabled */
932  if (!ocs->tgt_ocs.use_global_ramd) {
933  ocs_sport_lock(sport);
934  /* If a ramdisc matching the tgt_id is found, then use it, otherwise allocate a
935  * new one.
936  */
937  rc = ocs_port_rdisc_find(sport->ocs, sport->tgt_id, &sport->tgt_sport.rdisc,
938  &sport->tgt_sport.rdisc_count);
939  if (rc < 0) { /* not found */
940  rc = ocs_scsi_ramd_create(ocs, &sport->tgt_sport.rdisc, &sport->tgt_sport.rdisc_count);
941 
942  if (rc == 0) {
943  ocs_log_debug(ocs, "initialized sport RAM disk count=%d\n",
944  sport->tgt_sport.rdisc_count);
945  }
946 
947  rc = ocs_port_rdisc_save(ocs, sport->tgt_id, sport->tgt_sport.rdisc,
948  sport->tgt_sport.rdisc_count);
949  if (rc < 0) {
950  ocs_log_test(ocs, "ocs_port_rdisc_save failed\n");
951  }
952  }
953  ocs_sport_unlock(sport);
954  }
955 
956  return rc;
957 }
958 
959 /**
960  * @ingroup scsi_api_target
961  * @brief Accept a SLI Port gone notification.
962  *
963  * @par Description
964  * Called by the base driver when a sport goes away. A target-server
965  * uses this call to clean up all sport-scoped resources.<br><br>
966  * @b Note: For initiator-client backends, the ocs_scsi_ini_del_sport()
967  * function is called.
968  *
969  * @param sport Pointer to the sport structure.
970  *
971  * @return None.
972  */
973 void
974 ocs_scsi_tgt_del_sport(ocs_sport_t *sport)
975 {
976  ocs_t *ocs = sport->ocs;
977 
978  ocs_assert(ocs);
979 
980  ocs_log_debug(sport->ocs, "Del SPORT: %s\n", sport->display_name);
981 
982 }
983 
984 /**
985  * @ingroup scsi_api_target
986  * @brief Validate a new initiator.
987  *
988  * @par Description
989  * Sent by the base driver to validate a remote initiatiator. The target-server
990  * returns TRUE if this initiator should be accepted.<br><br>
991  * @b Note: This function is only to be called
992  * if the base driver is enabled for target capability.
993  *
994  * @param node Pointer to the remote initiator node to validate.
995  *
996  * @return Returns TRUE if the initiator should be accepted, or FALSE if the
997  * initiator should be rejected.
998  *
999  * @note
1000  */
1001 int32_t
1003 {
1004  return 1;
1005 }
1006 
1007 /**
1008  * @ingroup scsi_api_target
1009  * @brief Receive notification of a new SCSI initiator node.
1010  *
1011  * @par Description
1012  * Sent by the base driver to notify a target-server of the presence of a new
1013  * remote initiator. The target-server may use this call to prepare for
1014  * an inbound IO from this node.
1015  * The ocs_node_t structure has an element of type ocs_scsi_tgt_node_t named
1016  * tgt_node that is declared and used by a target-server for private
1017  * information.
1018  * @n @n @b Note: This function is only called if the base driver is enabled for target capability.
1019  *
1020  * @param node Pointer to a new remote initiator node.
1021  *
1022  * @return Returns 0 on success, or a negative error code value on failure.
1023  *
1024  * @note
1025  */
1026 int32_t
1027 ocs_scsi_new_initiator(ocs_node_t *node)
1028 {
1029  ocs_t *ocs;
1030  int num_luns = 0;
1031  int i;
1032  int32_t initiator_count;
1033  int32_t watermark;
1034 
1035  api_trace();
1036  ocs_assert(node, -1);
1037  ocs = node->ocs;
1038 
1039  /*
1040  * update IO watermark: increment initiator count
1041  */
1042  initiator_count = ocs_atomic_add_return(&ocs->tgt_ocs.initiator_count, 1) + 1;
1043  watermark = OCS_MAX(ocs->tgt_ocs.watermark_min,
1044  ocs->tgt_ocs.watermark_max - initiator_count*OCS_IO_WATERMARK_PER_INITIATOR);
1045  ocs_atomic_set(&ocs->tgt_ocs.io_high_watermark, watermark);
1046 
1047  if (ocs->tgt_ocs.use_global_ramd) {
1048  num_luns = ocs->tgt_ocs.rdisc_count;
1049  } else if (node->sport != NULL) {
1050  num_luns = node->sport->tgt_sport.rdisc_count;
1051  }
1052  if (num_luns == 0) {
1053  ocs_log_warn(ocs, "0 luns found");
1054  return -1;
1055  }
1056 
1057  /* Create Unit Attention queues, one per LUN */
1058  node->tgt_node.ua_queues = ocs_malloc(ocs, num_luns * sizeof(ocs_ramdisc_ua_queue_t),
1059  OCS_M_NOWAIT | OCS_M_ZERO);
1060  if (node->tgt_node.ua_queues == NULL) {
1061  node->tgt_node.num_ua_queues = 0;
1062  } else {
1063  node->tgt_node.num_ua_queues = num_luns;
1064  for (i=0; i<num_luns; i++) {
1065  ocs_lock_init(node->ocs, &node->tgt_node.ua_queues[i].ua_queue_lock, "ua queue lock");
1066  node->tgt_node.ua_queues[i].num_entries = 0;
1067  ocs_list_init(&node->tgt_node.ua_queues[i].entry_list, ocs_ramdisc_ua_t, link);
1068  }
1069  }
1070 
1071  return 0;
1072 }
1073 
1074 /**
1075  * @ingroup scsi_api_target
1076  * @brief Delete a SCSI initiator node.
1077  *
1078  * @par Description
1079  * Sent by the base driver to notify a target-server that a remote initiator is now gone.
1080  * The base driver has terminated all outstanding IOs, and the target-server will
1081  * receive the appropriate completions.
1082  * @n @n @b Note: This function is only called if the base driver is enabled for target capability.
1083  *
1084  * @param node Pointer to the node being deleted.
1085  * @param reason This is the reason that the initiator was removed and
1086  * will be one of the following enumeration types:
1087  * OCS_SCSI_INITIATOR_DELETED or OS_SCSI_INITIATOR_MISSING.
1088  *
1089  * @return Returns OCS_SCSI_CALL_COMPLETE to indicate that all work completed.
1090  */
1091 int32_t
1093 {
1094  ocs_t *ocs;
1095  uint32_t num_luns = 0;
1096  int32_t initiator_count;
1097  int32_t watermark;
1098 
1099  /*
1100  * initiator is missing, return OCS_SCSI_CALL_COMPLETE to let the transport
1101  * dispose of the node.
1102  */
1103  if (reason == OCS_SCSI_INITIATOR_MISSING) {
1104  return OCS_SCSI_CALL_COMPLETE;
1105  }
1106 
1107  ocs_assert(node, -1);
1108  ocs = node->ocs;
1109 
1110  /*
1111  * update IO watermark: decrement initiator count
1112  */
1113  initiator_count = ocs_atomic_sub_return(&ocs->tgt_ocs.initiator_count, 1) - 1;
1114  watermark = OCS_MAX(ocs->tgt_ocs.watermark_min,
1115  ocs->tgt_ocs.watermark_max - initiator_count*OCS_IO_WATERMARK_PER_INITIATOR);
1116  ocs_atomic_set(&ocs->tgt_ocs.io_high_watermark, watermark);
1117 
1118  /* Remove the Unit Attention queues */
1119  if (ocs->tgt_ocs.use_global_ramd) {
1120  num_luns = ocs->tgt_ocs.rdisc_count;
1121  } else if (node->sport != NULL) {
1122  num_luns = node->sport->tgt_sport.rdisc_count;
1123  }
1124 
1125  if (node->tgt_node.ua_queues != NULL) {
1126  uint32_t i;
1127 
1128  for (i=0; i<num_luns; i++) {
1129  ocs_lock_free(&node->tgt_node.ua_queues[i].ua_queue_lock);
1130  }
1131  ocs_free(ocs, node->tgt_node.ua_queues, num_luns * sizeof(ocs_ramdisc_ua_queue_t));
1132  node->tgt_node.ua_queues = NULL;
1133  }
1134  node->tgt_node.num_ua_queues = 0;
1135 
1136  return OCS_SCSI_CALL_COMPLETE;
1137 }
1138 
1139 /**
1140  * @brief Perform ramd IO free.
1141  *
1142  * @par Description
1143  * Frees an ocs_ramd-managed IO object.
1144  *
1145  * @param io Pointer to the IO context.
1146  *
1147  * @return None.
1148  */
1149 static void
1150 ocs_ramd_io_free(ocs_io_t *io)
1151 {
1152  ocs_assert(!io->tgt_io.tmfio);
1153  ocs_dma_free(io->ocs, &io->tgt_io.payload);
1154  /* kill the state machine */
1155  ocs_sm_disable(&io->tgt_io.sm);
1156  /* Call down to free base context resources, but only if the IO is still active */
1157  if (ocs_io_busy(io)) {
1158  io->tgt_io.thr = NULL;
1160  }
1161 }
1162 
1163 /**
1164  * @brief Defines the standard sense response bits per the SCSI <i>SPC specification</i>.
1165  *
1166  */
1167 typedef struct {
1168  uint8_t response_code:7,
1169  :1;
1170  uint8_t :8;
1171  uint8_t sense_key:4,
1172  :1,
1173  ili:1,
1174  eom:1,
1175  filemark:1;
1176  uint8_t information[4];
1178  uint8_t command_specific_information[4];
1179  uint8_t asc;
1180  uint8_t ascq;
1181  uint8_t fru_code;
1182  uint8_t sense_key_specific_hi:7,
1183  sksv:1;
1184  uint8_t sense_key_specific_lo[2];
1186 
1187 /**
1188  * @ingroup scsi_api_target
1189  * @brief Receive an FCP SCSI command.
1190  *
1191  * @par Description
1192  * Called by the base driver when a new SCSI command has been received. The
1193  * target-server processes the command, and issues data phase and/or response phase
1194  * requests to the base driver.
1195  * The IO context (ocs_io_t) structure has an element of type ocs_scsi_tgt_io_t named
1196  * tgt_io that is declared and used by a target-server for private information.
1197  *
1198  * @param io Pointer to the IO context.
1199  * @param lun LUN for this IO.
1200  * @param cdb Pointer to the SCSI CDB.
1201  * @param cdb_len Length of the CDB, in bytes.
1202  * @param flags Command flags.
1203  *
1204  * @return Returns 0 on success, or a negative error code value on failure.
1205  */
1206 int32_t
1207 _ocs_scsi_recv_cmd(ocs_io_t *io, uint32_t lun, uint8_t *cdb, uint32_t cdb_len, uint32_t flags)
1208 {
1209  return ocs_scsi_recv_cmd_common(io, lun, cdb, cdb_len, flags, FALSE, NULL, 0);
1210 }
1211 
1212 /**
1213  * @ingroup scsi_api_target
1214  * @brief Receive an FCP SCSI command with the first burst data.
1215  *
1216  * @par Description
1217  * Receive a new FCP SCSI command from the base driver with the first burst data.
1218  *
1219  * @param io Pointer to the IO context.
1220  * @param lun LUN for this IO.
1221  * @param cdb Pointer to the SCSI CDB.
1222  * @param cdb_len Length of CDB, in bytes.
1223  * @param flags Command flags.
1224  * @param first_burst_buffers First burst buffers.
1225  * @param first_burst_bytes The number of bytes received in the first burst.
1226  *
1227  * @return Returns 0 on success, or a negative error code value on failure.
1228  */
1229 int32_t
1230 ocs_scsi_recv_cmd_first_burst(ocs_io_t *io, uint32_t lun, uint8_t *cdb, uint32_t cdb_len, uint32_t flags,
1231  ocs_dma_t first_burst_buffers[], uint32_t first_burst_bytes)
1232 {
1233  int32_t rc = -1;
1234 
1235  if (cdb != NULL) {
1236  rc = ocs_scsi_recv_cmd_common(io, lun, cdb, cdb_len, flags, TRUE, first_burst_buffers, first_burst_bytes);
1237  } else {
1238  /* Post buffers to the IO state machine */
1239  if (flags & OCS_SCSI_FIRST_BURST_ERR) {
1240  rc = ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_IO_FIRST_BURST_ERR, NULL);
1241  } else if (flags & OCS_SCSI_FIRST_BURST_ABORTED) {
1242  rc = ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_IO_FIRST_BURST_ABORTED, NULL);
1243  } else {
1244  io->tgt_io.first_burst_bytes = first_burst_bytes;
1245  io->tgt_io.first_burst_buffers = first_burst_buffers;
1246  rc = ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_IO_FIRST_BURST, NULL);
1247  }
1248  }
1249 
1250  return rc;
1251 }
1252 
1253 /**
1254  * @brief Return address of data and DIF data given a block in the target
1255  *
1256  * @par Description
1257  * The virtual addresses of the data block and DIF block are returned for a given
1258  * lba in the target.
1259  *
1260  * @param io Pointer to the IO context.
1261  * @param blocknumber The SCSI logical block address.
1262  * @param addrlen Array of address length pairs.
1263  * @param max_addrlen Maximum number of addrlen pairs to return.
1264  * @param dif_vaddr Pointer to location to store virtual address of DIF block.
1265  *
1266  * @return Returns number of addrlen pairs returned, or a negative value for error.
1267  */
1268 int32_t
1269 ocs_scsi_get_block_vaddr(ocs_io_t *io, uint64_t blocknumber, ocs_scsi_vaddr_len_t addrlen[],
1270  uint32_t max_addrlen, void **dif_vaddr)
1271 {
1272  int32_t rc = -1;
1273 
1274  if (max_addrlen >= 1) {
1275  addrlen[0].vaddr = (void*) ocs_ramdisc_lba_address(io->tgt_io.ramdisc, blocknumber);
1276  addrlen[0].length = io->tgt_io.ramdisc->data_block_size;
1277  *dif_vaddr = (void*) ocs_ramdisc_dif_address(io->tgt_io.ramdisc, blocknumber);
1278  rc = 1;
1279  }
1280 
1281  return rc;
1282 }
1283 
1284 /**
1285  * @brief Common code used by ocs_scsi_recv_cmd() and ocs_scsi_recv_cmd_first_burst().
1286  *
1287  * @par Description
1288  * Receive a new FCP SCSI command from the base driver, possibly with the first burst data.
1289  *
1290  * @param io Pointer to the IO context.
1291  * @param lun LUN for this IO.
1292  * @param cdb Pointer to the SCSI CDB.
1293  * @param cdb_len Length of the CDB, in bytes.
1294  * @param flags Command flags.
1295  * @param has_first_burst TRUE if the command has first burst data.
1296  * @param first_burst_buffers First burst buffer.
1297  * @param first_burst_bytes The number of bytes received in the first burst.
1298  *
1299  * @return Returns 0 on success, or a negative error code value on failure.
1300  */
1301 int32_t
1302 ocs_scsi_recv_cmd_common(ocs_io_t *io, uint32_t lun, uint8_t *cdb, uint32_t cdb_len, uint32_t flags,
1303  uint8_t has_first_burst,
1304  ocs_dma_t first_burst_buffers[], uint32_t first_burst_bytes)
1305 {
1306  int32_t rc = 0;
1307  uint32_t i;
1308  ocs_t *ocs = io->ocs;
1309  scsi_cmd_table_t *pcmd;
1310  int32_t active_io_count;
1311 
1312  api_trace();
1313 
1314  ocs_assert(cdb, -1);
1315 
1316  if (ENABLE_CMD_TRACE) {
1317  io_printf(io, "cdb: ");
1318  for (i = 0; i < cdb_len; i ++)
1319  ocs_log_debug(NULL, "%02X ", cdb[i]);
1320  ocs_log_debug(NULL, "\n");
1321  }
1322 
1323 
1324  /* Save calling arguments */
1325  io->tgt_io.flags = flags;
1326  io->tgt_io.first_burst_buffers = first_burst_buffers;
1327  io->tgt_io.first_burst_bytes = first_burst_bytes;
1328  io->tgt_io.tmfio = NULL;
1329 
1330  /* Get a copy of the tgt domain context */
1331  io->tgt_io.lun = lun;
1332  io->tgt_io.sm.app = io;
1333 
1334  /* Clear block IO state */
1335  io->tgt_io.io_block_count = 0;
1336  io->tgt_io.acc_block_count = 0;
1337  io->tgt_io.cur_lba = 0;
1338  io->tgt_io.cur_block_count = 0;
1339 
1340  io->tgt_io.sgl_phys = 0;
1341 
1342  io->tgt_io.ramdisc = ocs_ramd_lookup(io, lun);
1343 
1344  /* Set flag indicating whether the LUN is defined for this target */
1345  io->tgt_io.lun_is_valid = (io->tgt_io.ramdisc != NULL);
1346  active_io_count = ACTIVE_IO_COUNT(ocs);
1347 
1348  /* set target timeout */
1349  io->timeout = ocs->target_io_timer_sec;
1350 
1351  /* set treceive wqe timeout */
1352  io->trecv_wqe_timer = ocs->tgt_wqe_timer;
1353 
1354  /* Take lock */
1355  ocs_lock(&io->tgt_io.sm_lock);
1356 
1357  /* Check for task set full */
1358  if (ocs->tgt_ocs.enable_task_set_full &&
1359  (active_io_count >= ocs_atomic_read(&ocs->tgt_ocs.io_high_watermark))) {
1360  ocs_atomic_add_return(&ocs->tgt_ocs.watermark_hit, 1);
1361  /* If this is a first burst command, then defer sending task set full */
1362  if (has_first_burst) {
1363  io->tgt_io.send_task_set_full = TRUE;
1365  } else {
1367  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
1368  } else {
1369  ocs_ramd_io_free(io);
1370  }
1371  }
1372  ocs_unlock(&io->tgt_io.sm_lock);
1373  return -1;
1374  }
1375 
1376  /* Find a matching command */
1377  pcmd = NULL;
1378  if (cdb[0] == SCSI_SERVICE_ACTION_IN16) {
1379  uint32_t svc_action = cdb[1] & 0x1F;
1380 
1381  for (i = 0; i < ARRAY_SIZE(scsi_cmd_table_svc_in_action16); i ++) {
1382  if (scsi_cmd_table_svc_in_action16[i].scsi_cmd == svc_action) {
1383  pcmd = &scsi_cmd_table_svc_in_action16[i];
1384  break;
1385  }
1386  }
1387  } else if (cdb[0] == SCSI_SERVICE_ACTION_OUT16) {
1388  uint32_t svc_action = cdb[1] & 0x1F;
1389 
1390  for (i = 0; i < ARRAY_SIZE(scsi_cmd_table_svc_out_action16); i ++) {
1391  if (scsi_cmd_table_svc_out_action16[i].scsi_cmd == svc_action) {
1392  pcmd = &scsi_cmd_table_svc_out_action16[i];
1393  break;
1394  }
1395  }
1396  } else if (cdb[0] == SCSI_VARIABLE_LEN_CDB) {
1397  uint16_t svc_action = (cdb[8] << 8) |
1398  (cdb[9] << 0);
1399  for (i = 0; i < ARRAY_SIZE(scsi_cmd_table_variable_len_cdb); i ++) {
1400  if (scsi_cmd_table_variable_len_cdb[i].scsi_cmd == svc_action) {
1401  pcmd = &scsi_cmd_table_variable_len_cdb[i];
1402  break;
1403  }
1404  }
1405 
1406  } else {
1407  pcmd = scsi_cmd_lookup[cdb[0]];
1408  }
1409 
1410  /* Save cdb, pointer to command */
1411  io->tgt_io.pcmd = pcmd;
1412  io->tgt_io.cdb_len = cdb_len;
1413  ocs_memcpy(io->tgt_io.cdb, cdb, MIN(cdb_len, ARRAY_SIZE(io->tgt_io.cdb)));
1414 
1415  /* Note whether first burst */
1416  io->tgt_io.has_first_burst = has_first_burst;
1417 
1418  if (ocs->err_injection != NO_ERR_INJECT) {
1419  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_init_err_injection, NULL);
1420  } else {
1421  /* Start the state machine */
1422  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_init, NULL);
1423  }
1424 
1425  ocs_unlock(&io->tgt_io.sm_lock);
1426 
1427  return rc;
1428 }
1429 
1430 /**
1431  * @ingroup io_sm
1432  * @brief State: Initial IO state with error injection enabled.
1433  *
1434  * @par Description
1435  * The initial IO state processes error injection options for the received command.
1436  *
1437  * @param ctx Remote node sm context.
1438  * @param evt Event to process.
1439  * @param arg Per event optional argument.
1440  *
1441  * @return Returns NULL.
1442  */
1443 static void *
1444 __ocs_ramd_io_init_err_injection(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
1445 {
1446  ocs_io_t *io = ctx->app;
1447  ocs_t *ocs = io->ocs;
1448  scsi_cmd_table_t *pcmd = io->tgt_io.pcmd;
1449  int32_t rc;
1450 
1451  switch (evt) {
1452  case OCS_EVT_ENTER: {
1453  switch (ocs->err_injection) {
1454  case INJECT_DROP_CMD:
1455  if (ocs->cmd_err_inject != 0 && ocs->cmd_err_inject == pcmd->scsi_cmd) {
1456  io_printf(io, "Err-injection test: drop command\n");
1457  return NULL;
1458  }
1459  break;
1460  case INJECT_FREE_DROPPED:
1461  if (ocs->cmd_err_inject != 0 && ocs->cmd_err_inject == pcmd->scsi_cmd) {
1462  io_printf(io, "Err-injection test: free dropped command\n");
1463  ocs_ramd_io_free(io);
1464  return NULL;
1465  }
1466  break;
1467  case INJECT_DELAY_CMD:
1468  if (ocs->cmd_err_inject != 0 && ocs->cmd_err_inject == pcmd->scsi_cmd) {
1469  io_printf(io, "Err-injection test: delay command\n");
1470  ocs_msleep(ocs->delay_value_msec);
1471  }
1472  break;
1473  case INJECT_DROP_DATA:
1474  /* drop_data is handled in __ocs_ramd_io_wait_first_burst */
1475  case INJECT_DROP_RESP:
1476  /* drop_resp is handled in dataphase states */
1477  default:
1478  break;
1479  }
1480  /* Start the state machine */
1481  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_init, NULL);
1482  break;
1483  }
1484 
1485  case OCS_EVT_ABORT_IO:
1487  /* Abort the I/O in progress */
1488  io_printf(io, "%-20s evt=%s\n", __func__, ocs_sm_event_name(evt));
1489 
1490  /* for abort_io event, will send completion event after abort
1491  * for abort_io_no_resp event, no such completion will be generated
1492  */
1493  if (evt == OCS_EVT_ABORT_IO) {
1494  ocs_assert(arg, NULL);
1495  io->tgt_io.tmfio = (ocs_io_t *)arg;
1496  } else {
1497  io->tgt_io.tmfio = NULL;
1498  }
1499 
1500  /* transition to state where we wait for completions
1501  * we would only be in this state if the command was dropped,
1502  * we won't be getting an IO cmpl
1503  */
1505 
1506  rc = ocs_scsi_tgt_abort_io(io, false, ocs_ramd_abort_tgt_cb, NULL);
1507  if (rc != 0) {
1508  io_printf(io, "Failed to abort IO\n");
1509  }
1510  break;
1511 
1512  default:
1513  __ocs_ramd_io_common(__func__, ctx, evt, arg);
1514  break;
1515  }
1516  return NULL;
1517 }
1518 
1519 /**
1520  * @brief Function to call the command handler for a CDB.
1521  *
1522  * @par Description
1523  * After a command and its first burst data is received, this function is called
1524  * to call the actual command handler function.
1525  *
1526  * @param io Pointer to the SCSI IO object.
1527  *
1528  * @return The command handler status.
1529  */
1530 static ocs_ramd_hndlr_rtn_e
1532 {
1533  ocs_sm_ctx_t *ctx = &io->tgt_io.sm;
1534  uint8_t *cdb = io->tgt_io.cdb;
1535  scsi_cmd_table_t *pcmd = io->tgt_io.pcmd;
1537 
1539 
1540  rc = pcmd->handler(io, cdb);
1541 
1542  switch(rc) {
1546  } else {
1547  ocs_ramd_io_free(io);
1548  }
1549  break;
1552  break;
1555  break;
1558  break;
1561  break;
1563  ocs_ramd_io_free(io);
1564  break;
1565 
1566  default:
1567  break;
1568  }
1569 
1570  return rc;
1571 }
1572 
1573 /**
1574  * @ingroup io_sm
1575  * @brief State: Initial IO state.
1576  *
1577  * @par Description
1578  * The initial IO state invokes a SCSI command processing function, which may be a
1579  * data phase or a response for the received command.
1580  *
1581  * @param ctx Remote node sm context.
1582  * @param evt Event to process.
1583  * @param arg Per event optional argument.
1584  *
1585  * @return Returns NULL.
1586  */
1587 static void *
1588 __ocs_ramd_io_init(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
1589 {
1590  ocs_io_t *io = ctx->app;
1591  ocs_t *ocs = io->ocs;
1592 
1593  io_sm_trace();
1594  io->tgt_io.current_state = __OCS_RAMD_IO_INIT;
1595 
1596  switch(evt) {
1597  case OCS_EVT_ENTER: {
1598  scsi_cmd_table_t *pcmd;
1599  ocs_node_t *node = io->node;
1600  uint8_t asc;
1601  uint8_t ascq;
1602  uint32_t lun = io->tgt_io.lun;
1603  uint8_t direction = 0;
1604 
1605  /*
1606  * If the command has first burst which has not been received,
1607  * then wait for the data before calling the command handler.
1608  */
1609  if (io->tgt_io.has_first_burst &&
1610  (io->tgt_io.first_burst_bytes == 0 ||
1611  io->tgt_io.first_burst_buffers == NULL)) {
1613  break;
1614  }
1615 
1616  pcmd = io->tgt_io.pcmd;
1617 
1618  /* Check to see whether we found a supported command. If not send Check Condition. */
1619  if (pcmd == NULL) {
1620  io_printf(io, "No cmd handler\n");
1622  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
1623  } else {
1624  ocs_ramd_io_free(io);
1625  }
1626  break;
1627  }
1628 
1629  /* Check data direction validity, return a chack condition on failure */
1630  direction = ((io->tgt_io.flags & OCS_SCSI_CMD_DIR_IN) ? DATA_DIR_WRITE : 0) |
1631  ((io->tgt_io.flags & OCS_SCSI_CMD_DIR_OUT) ? DATA_DIR_READ : 0);
1632 
1633  if ((io->exp_xfer_len > 0) && ((pcmd->data_dir != direction) || (direction == DATA_DIR_NONE))) {
1636  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
1637  } else {
1638  ocs_ramd_io_free(io);
1639  }
1640 
1641  break;
1642  }
1643 
1644  /* Check for valid lun */
1645  if (pcmd->check_lun && !io->tgt_io.lun_is_valid) {
1646  io_printf(io, "lun %d out of range\n", io->tgt_io.lun);
1648  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
1649  } else {
1650  ocs_ramd_io_free(io);
1651  }
1652  break;
1653  }
1654 
1655  /* Check for Unit Attention */
1656  if (ocs_ramd_get_ua(ocs, node, io->tgt_io.lun, &asc, &ascq)) {
1657  switch (io->tgt_io.cdb[0]) {
1658  case SCSI_INQUIRY:
1659  /*
1660  * Allow the command
1661  * Don't clear any unit attentions
1662  */
1663  break;
1664  case SCSI_REQUEST_SENSE:
1665  /*
1666  * Report the Unit Attention
1667  * Clear the Unit Attention
1668  */
1669  ocs_ramd_clear_ua(ocs, node, lun, asc, ascq);
1671  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
1672  } else {
1673  ocs_ramd_io_free(io);
1674  }
1675  return NULL;
1676  case SCSI_REPORT_LUNS:
1677  /*
1678  * Allow the command
1679  * Clear any REPORTED LUNS DATA HAS CHANGED unit attentions
1680  */
1683  break;
1684  default:
1685  /*
1686  * If there's a current Unit Attention return that instead
1687  * of processing the command.
1688  */
1689  ocs_ramd_clear_ua(ocs, node, lun, asc, ascq);
1691  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
1692  } else {
1693  ocs_ramd_io_free(io);
1694  }
1695  return NULL;
1696  }
1697  }
1699  break;
1700  }
1701 
1702  default:
1703  __ocs_ramd_io_common(__func__, ctx, evt, arg);
1704  break;
1705  }
1706 
1707  return NULL;
1708 
1709 }
1710 
1711 /**
1712  * @ingroup io_sm
1713  * @brief State: Waiting for first burst data.
1714  *
1715  * @par Description
1716  * Waits for the first burst data on an IO.
1717  *
1718  * @param ctx Remote node sm context.
1719  * @param evt Event to process.
1720  * @param arg Per event optional argument.
1721  *
1722  * @return Returns NULL.
1723  */
1724 static void *
1725 __ocs_ramd_io_wait_first_burst(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
1726 {
1727  ocs_io_t *io = ctx->app;
1728  ocs_t *ocs = io->ocs;
1729 
1730  io_sm_trace();
1731  io->tgt_io.current_state = __OCS_RAMD_IO_WAIT_FIRST_BURST;
1732 
1733  switch(evt) {
1735  /*
1736  * If the command has first burst which has not been received,
1737  * then wait for the data before calling the command handler.
1738  */
1739  if (io->tgt_io.has_first_burst &&
1740  (io->tgt_io.first_burst_bytes == 0 ||
1741  io->tgt_io.first_burst_buffers == NULL)) {
1742  /* Wait for the first burst data */
1743  } else {
1744  if (ocs->err_injection == INJECT_DROP_DATA) {
1745  io_printf(io, "Err-injection test: First burst data dropped\n");
1746  break;
1747  }
1748  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_init, NULL);
1749  }
1750  break;
1751 
1753  /* first burst has encountered an error before receiving all of it,
1754  send generic check condition */
1755  io_printf(io, "first burst/auto-xfr-rdy error encountered\n");
1757  break;
1758 
1760  /* first burst has been aborted before receiving all of it, free io */
1761  io_printf(io, "first burst/auto-xfr-rdy aborted encountered\n");
1762  ocs_ramd_io_free(io);
1763  break;
1764 
1765  case OCS_EVT_ABORT_IO:
1766  case OCS_EVT_ABORT_IO_NO_RESP: {
1767  int32_t rc;
1768 
1769  /* Abort the I/O in progress */
1770  io_printf(io, "evt=%s\n", ocs_sm_event_name(evt));
1771 
1772  /* for abort_io event, will send completion event after abort
1773  * for abort_io_no_resp event, no such completion will be generated
1774  */
1775  if (evt == OCS_EVT_ABORT_IO) {
1776  ocs_assert(arg, NULL);
1777  io->tgt_io.tmfio = (ocs_io_t *)arg;
1778  } else {
1779  io->tgt_io.tmfio = NULL;
1780  }
1781 
1782  /* we haven't issued a command to the chip, so we won't be expecting
1783  * an IO completion (only abort completion)
1784  */
1786  rc = ocs_scsi_tgt_abort_io(io, false, ocs_ramd_abort_tgt_cb, NULL);
1787  if (rc != 0) {
1788  io_printf(io, "Failed to abort IO\n");
1789  }
1790  break;
1791  }
1792  default:
1793  __ocs_ramd_io_common(__func__, ctx, evt, arg);
1794  break;
1795  }
1796 
1797  return NULL;
1798 }
1799 
1800 /**
1801  * @brief Command abort callback.
1802  *
1803  * @par Description
1804  * This function is called with a command abort request to the base driver SCSI API completes.
1805  *
1806  * @param io Pointer to the IO context.
1807  * @param scsi_status Completion status.
1808  * @param flags Flags.
1809  * @param arg Application-specified argument.
1810  *
1811  * @return Returns 0 on success, or a negative error code value on failure.
1812  */
1813 static int32_t
1814 ocs_ramd_abort_tgt_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
1815 {
1816  ocs_lock(&io->tgt_io.sm_lock);
1817  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_ABORT_CMPL, NULL);
1818  ocs_unlock(&io->tgt_io.sm_lock);
1819  return 0;
1820 }
1821 
1822 static int32_t
1823 ocs_ramd_io_error(ocs_io_t *io, ocs_scsi_io_status_e scsi_status)
1824 {
1825  uint8_t asc;
1826  uint8_t ascq;
1827  uint8_t check_cond = TRUE;
1828 
1829  switch (scsi_status) {
1830  case OCS_SCSI_STATUS_NO_IO:
1835  /*
1836  * For these status codes, no use sending a check condition,
1837  * just free the IO.
1838  */
1839  io_printf(io, "status=%d, freeing io\n", scsi_status);
1840  check_cond = FALSE;
1841  break;
1843  asc = SNSCODE_DIF_ERR;
1844  ascq = 0x01;
1845  break;
1847  asc = SNSCODE_DIF_ERR;
1848  ascq = 0x02;
1849  break;
1851  asc = SNSCODE_DIF_ERR;
1852  ascq = 0x03;
1853  break;
1855  asc = SNSCODE_DIF_ERR;
1856  ascq = 0x01;
1857  break;
1859  asc = SNSCODE_ABORTED_CMD;
1860  ascq = 0x05;
1861  break;
1862  default:
1863  asc = SNSCODE_OFF_ERR;
1864  ascq = 0x00;
1865  break;
1866  }
1867 
1868  if (check_cond) {
1870  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
1871  } else {
1872  ocs_ramd_io_free(io);
1873  }
1874  } else {
1875  ocs_ramd_io_free(io);
1876  }
1877  return 0;
1878 }
1879 
1880 /**
1881  * @ingroup io_sm
1882  * @brief State: Wait for the data phase to complete.
1883  *
1884  * @par Description
1885  * Wait for data phase to complete. If done, send a response.
1886  *
1887  * @param ctx Remote node sm context.
1888  * @param evt Event to process.
1889  * @param arg Per event optional argument.
1890  *
1891  * @return Returns NULL.
1892  */
1893 static void *
1894 __ocs_ramd_io_wait_dataphase_compl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
1895 {
1896  ocs_io_t *io = ctx->app;
1897  ocs_t *ocs = io->ocs;
1898 
1899  io_sm_trace();
1900  io->tgt_io.current_state = __OCS_RAMD_IO_WAIT_DATAPHASE_COMPL;
1901 
1902  switch(evt) {
1903  case OCS_EVT_IO_CMPL:
1904  if (ocs->err_injection == INJECT_DROP_RESP) {
1905  io_printf(io, "Err-injection test: drop response lun x%x\n", io->tgt_io.lun);
1906  break;
1907  }
1910  } else {
1911  ocs_ramd_io_free(io);
1912  }
1913  break;
1914 
1915  case OCS_EVT_IO_CMPL_ERRORS: {
1916  ocs_scsi_io_status_e scsi_status;
1917 
1918  scsi_status = *((ocs_scsi_io_status_e*) arg);
1919  ocs_ramd_io_error(io, scsi_status);
1920  break;
1921  }
1922 
1923  case OCS_EVT_RESP_CMPL:
1924  ocs_ramd_io_free(io);
1925  break;
1926 
1927  default:
1928  __ocs_ramd_io_common(__func__, ctx, evt, arg);
1929  break;
1930  }
1931 
1932  return NULL;
1933 }
1934 
1935 /**
1936  * @ingroup io_sm
1937  * @brief State: Wait for the data phase to complete, then validate the received data.
1938  *
1939  * @par Description
1940  * Wait for data phase to complete. If done, send a response.
1941  *
1942  * @param ctx Remote node sm context.
1943  * @param evt Event to process.
1944  * @param arg Per event optional argument.
1945  *
1946  * @return Returns NULL.
1947  */
1948 static void *
1950 {
1951  int32_t (*validate)(ocs_io_t *io);
1952  ocs_io_t *io = ctx->app;
1953  ocs_t *ocs = io->ocs;
1954  uint32_t result;
1956 
1957  io_sm_trace();
1958  io->tgt_io.current_state = __OCS_RAMD_IO_WAIT_AND_VALIDATE_DATAPHASE;
1959 
1960  switch(evt) {
1961  case OCS_EVT_IO_CMPL:
1962  if (ocs->err_injection == INJECT_DROP_RESP) {
1963  io_printf(io, "Err-injection test: drop response lun x%x\n", io->tgt_io.lun);
1964  break;
1965  }
1966 
1967  result = 0;
1968  if (arg != NULL) {
1969  validate = arg;
1970  result = validate(io);
1971  }
1972 
1973  if (result == 0) {
1974  rc = ocs_ramd_good_status(io);
1975  } else {
1976  rc = ocs_ramd_check_cond(io, ((result & 0xff0000) >> 16), ((result & 0xff00) >> 8),
1977  result & 0xff);
1978  }
1979 
1980  if (rc == SCSI_HANDLER_RESP_STARTED) {
1982  } else {
1983  ocs_ramd_io_free(io);
1984  }
1985  break;
1986 
1987  case OCS_EVT_IO_CMPL_ERRORS: {
1988  ocs_scsi_io_status_e scsi_status;
1989 
1990  scsi_status = *((ocs_scsi_io_status_e*) arg);
1991  ocs_ramd_io_error(io, scsi_status);
1992  break;
1993  }
1994 
1995  default:
1996  __ocs_ramd_io_common(__func__, ctx, evt, arg);
1997  break;
1998  }
1999 
2000  return NULL;
2001 }
2002 
2003 /**
2004  * @ingroup io_sm
2005  * @brief State: Wait for the data phase to complete following a WRITE SAME command.
2006  *
2007  * @par Description
2008  * Wait for the data phase to complete. If done, complete the WRITE SAME command.
2009  *
2010  * @param ctx Remote node sm context.
2011  * @param evt Event to process.
2012  * @param arg Per event optional argument.
2013  *
2014  * @return Returns NULL.
2015  */
2016 static void *
2018 {
2019  ocs_io_t *io = ctx->app;
2020  ocs_t *ocs = io->ocs;
2022 
2023  io_sm_trace();
2024  io->tgt_io.current_state = __OCS_RAMD_IO_WAIT_AND_COMPLETE_WRITESAME_DATAPHASE;
2025 
2026  switch(evt) {
2027  case OCS_EVT_IO_CMPL:
2028  if (ocs->err_injection == INJECT_DROP_RESP) {
2029  io_printf(io, "Err-injection test: drop response lun x%x\n", io->tgt_io.lun);
2030  break;
2031  }
2032 
2033  /* At this point we've completed the data phase for the WRITE SAME command
2034  * so we've transfered the one block to memory. Now we need to complete the
2035  * command by copying that block to other blocks, possibly with modifications
2036  * along the way
2037  */
2038 
2040 
2041 
2042  if (rc == SCSI_HANDLER_RESP_STARTED) {
2044  } else {
2045  ocs_ramd_io_free(io);
2046  }
2047  break;
2048 
2049  case OCS_EVT_IO_CMPL_ERRORS: {
2050  ocs_scsi_io_status_e scsi_status;
2051 
2052  scsi_status = *((ocs_scsi_io_status_e*) arg);
2053  ocs_ramd_io_error(io, scsi_status);
2054  break;
2055  }
2056 
2057  default:
2058  __ocs_ramd_io_common(__func__, ctx, evt, arg);
2059  break;
2060  }
2061 
2062  return NULL;
2063 }
2064 
2065 /**
2066  * @ingroup io_sm
2067  * @brief State: Wait on first burst then send response.
2068  *
2069  * @par Description
2070  * Wait for first burst to complete, then send a SCSI response.
2071  *
2072  * @param ctx Remote node sm context.
2073  * @param evt Event to process.
2074  * @param arg Per event optional argument.
2075  *
2076  * @return Returns NULL.
2077  */
2078 static void *
2080 {
2081  ocs_io_t *io;
2082  ocs_ramd_hndlr_rtn_e rc = 0;
2083 
2084  ocs_assert(ctx, NULL);
2085  ocs_assert(ctx->app, NULL);
2086  io = ctx->app;
2087  ocs_assert(io->node, NULL);
2088  ocs_assert(io->ocs, NULL);
2089 
2090  io_sm_trace();
2091  io->tgt_io.current_state = __OCS_RAMD_IO_WAIT_FIRST_BURST_SEND_RESP;
2092 
2093  switch(evt) {
2094  case OCS_EVT_ENTER:
2095  ocs_assert(io->tgt_io.send_task_set_full || io->tgt_io.send_check_cond, NULL);
2096  break;
2097 
2099  if (io->tgt_io.send_task_set_full) {
2101  } else if (io->tgt_io.send_check_cond) {
2102  rc = ocs_ramd_check_cond(io, io->tgt_io.send_sense_key,
2103  io->tgt_io.send_asc,
2104  io->tgt_io.send_ascq);
2105  }
2106  if (rc == SCSI_HANDLER_RESP_STARTED) {
2107  ocs_sm_transition(&io->tgt_io.sm, __ocs_ramd_io_wait_resp_compl, NULL);
2108  } else {
2109  ocs_ramd_io_free(io);
2110  }
2111  break;
2113  /* first burst has encountered an error before receiving all of it,
2114  send generic check condition */
2115  io_printf(io, "first burst/auto-xfr-rdy error encountered\n");
2117  break;
2118 
2120  /* first burst has been aborted before receiving all of it, free io */
2121  io_printf(io, "first burst/auto-xfr-rdy aborted encountered\n");
2122  ocs_ramd_io_free(io);
2123  break;
2124 
2125  case OCS_EVT_ABORT_IO:
2126  case OCS_EVT_ABORT_IO_NO_RESP: {
2127  int32_t rc;
2128 
2129  /* Abort the I/O in progress */
2130  io_printf(io, "evt=%s\n", ocs_sm_event_name(evt));
2131 
2132  /* for abort_io event, will send completion event after abort
2133  * for abort_io_no_resp event, no such completion will be generated
2134  */
2135  if (evt == OCS_EVT_ABORT_IO) {
2136  ocs_assert(arg, NULL);
2137  io->tgt_io.tmfio = (ocs_io_t *)arg;
2138  } else {
2139  io->tgt_io.tmfio = NULL;
2140  }
2141 
2142  /* we haven't issued a command to the chip, so we won't be expecting
2143  * an IO completion (only abort completion)
2144  */
2146  rc = ocs_scsi_tgt_abort_io(io, false, ocs_ramd_abort_tgt_cb, NULL);
2147  if (rc != 0) {
2148  io_printf(io, "Failed to abort IO\n");
2149  }
2150  break;
2151  }
2152  default:
2153  __ocs_ramd_io_common(__func__, ctx, evt, arg);
2154  break;
2155  }
2156 
2157  return NULL;
2158 }
2159 
2160 /**
2161  * @ingroup io_sm
2162  * @brief State: Wait for the response to complete.
2163  *
2164  * @par Description
2165  * Wait for a SCSI response phase to complete. Upon completion, free the IO context.
2166  *
2167  * @param ctx Remote node sm context.
2168  * @param evt Event to process.
2169  * @param arg Per event optional argument.
2170  *
2171  * @return Returns NULL.
2172  */
2173 static void *
2174 __ocs_ramd_io_wait_resp_compl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
2175 {
2176  ocs_io_t *io;
2177 
2178  ocs_assert(ctx, NULL);
2179  ocs_assert(ctx->app, NULL);
2180  io = ctx->app;
2181  ocs_assert(io->node, NULL);
2182  ocs_assert(io->ocs, NULL);
2183 
2184  io_sm_trace();
2185  io->tgt_io.current_state = __OCS_RAMD_IO_WAIT_RESP_COMPL;
2186 
2187  switch(evt) {
2188  case OCS_EVT_RESP_CMPL:
2189  ocs_ramd_io_free(io);
2190  break;
2191  default:
2192  __ocs_ramd_io_common(__func__, ctx, evt, arg);
2193  break;
2194  }
2195 
2196  return NULL;
2197 }
2198 
2199 /**
2200  * @ingroup io_sm
2201  * @brief State: Wait for the TMF response to complete.
2202  *
2203  * @par Description
2204  * Wait for a TMF response phase to complete. Upon completion,
2205  * free the IO context.
2206  *
2207  * @param ctx Remote node sm context.
2208  * @param evt Event to process.
2209  * @param arg Per event optional argument.
2210  *
2211  * @return Returns NULL.
2212  */
2213 static void *
2214 __ocs_ramd_tmf_wait_resp_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
2215 {
2216  ocs_io_t *tmfio;
2217 
2218  ocs_assert(ctx, NULL);
2219  ocs_assert(ctx->app, NULL);
2220  tmfio = ctx->app;
2221  ocs_assert(tmfio->node, NULL);
2222  ocs_assert(tmfio->ocs, NULL);
2223 
2224  tmfio_sm_trace();
2225  tmfio->tgt_io.current_state = __OCS_RAMD_TMF_WAIT_RESP_CMPL;
2226 
2227  switch(evt) {
2228  case OCS_EVT_RESP_CMPL:
2229  ocs_ramd_io_free(tmfio);
2230  break;
2231  case OCS_EVT_ABORT_IO:
2232  case OCS_EVT_ABORT_IO_NO_RESP: {
2233  ocs_ignore_abort_evt(evt, __func__, tmfio, (ocs_io_t *)arg);
2234  break;
2235  }
2236  default:
2237  __ocs_ramd_io_common(__func__, ctx, evt, arg);
2238  break;
2239  }
2240 
2241  return NULL;
2242 }
2243 
2244 /**
2245  * @brief Abort response callback.
2246  *
2247  * @par Description
2248  * When an IO abort operation has completed, this callback is invoked.
2249  *
2250  * @param io Pointer to the context.
2251  * @param scsi_status Completion status.
2252  * @param flags Flags.
2253  * @param arg Application-specified argument.
2254  *
2255  * @return Returns 0 on success, or a negative error code value on failure.
2256  */
2257 static int32_t
2258 ocs_ramd_tgt_abort_rsp_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
2259 {
2260  if (scsi_status != OCS_SCSI_STATUS_GOOD) {
2261  io_printf(io, "abort failed, status=%d\n", scsi_status);
2262  }
2263  ocs_lock(&io->tgt_io.sm_lock);
2264  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_RESP_CMPL, NULL);
2265  ocs_unlock(&io->tgt_io.sm_lock);
2266  return 0;
2267 }
2268 
2269 /**
2270  * @brief State: Aborting IO.
2271  *
2272  * @par Description
2273  * IO abort has been started. Wait for IO or abort to complete.
2274  *
2275  * @param ctx Remote node sm context.
2276  * @param evt Event to process.
2277  * @param arg Per event optional argument.
2278  *
2279  * @return Returns NULL.
2280  */
2281 static void *
2282 __ocs_ramd_io_aborting(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
2283 {
2284  ocs_io_t *io; /* 'io' here is IO being aborted */
2285 
2286  ocs_assert(ctx, NULL);
2287  ocs_assert(ctx->app, NULL);
2288  io = ctx->app;
2289  ocs_assert(io->node, NULL);
2290  ocs_assert(io->ocs, NULL);
2291 
2292  io_sm_trace();
2293  io->tgt_io.current_state = __OCS_RAMD_IO_ABORTING;
2294 
2295  switch(evt) {
2296  case OCS_EVT_IO_CMPL:
2298  case OCS_EVT_RESP_CMPL:
2299  /* IO has completed first. Transition to wait for abort to complete */
2300  io_printf(io, "IO cmpl evt=%s\n", ocs_sm_event_name(evt));
2302  break;
2303 
2304  case OCS_EVT_ABORT_CMPL: {
2305  /* Abort has completed first. Transition to wait for abort to complete */
2306  io_printf(io, "IO abort cmpl evt=%s\n", ocs_sm_event_name(evt));
2308  break;
2309  }
2310  case OCS_EVT_ABORT_IO:
2311  case OCS_EVT_ABORT_IO_NO_RESP: {
2312  ocs_ignore_abort_evt(evt, __func__, io, (ocs_io_t *)arg);
2313  break;
2314  }
2315  default:
2316  __ocs_ramd_io_common(__func__, ctx, evt, arg);
2317  break;
2318  }
2319 
2320  return NULL;
2321 }
2322 
2323 /**
2324  * @brief State: Cleanup aborted IO.
2325  *
2326  * @par Description
2327  * Cleanup IO that has been aborted.
2328  *
2329  * @param io Aborted IO.
2330  *
2331  * @return None.
2332  */
2333 static void
2335 {
2336  ocs_io_t *tmfio = io->tgt_io.tmfio;
2337 
2338  if (tmfio) {
2339  /* post event to TMF IO to indicate that this IO has been aborted */
2340  ocs_sm_post_event(&tmfio->tgt_io.sm, OCS_EVT_IO_ABORTED_BY_TMF, NULL);
2341  io->tgt_io.tmfio = NULL;
2342  }
2343 
2344  /* Free the aborted IO */
2345  ocs_ramd_io_free(io);
2346 }
2347 
2348 /**
2349  * @brief State: Aborting IO.
2350  *
2351  * @par Description
2352  * IO abort has been started and IO has completed. Wait for the
2353  * abort to complete and then free IO.
2354  *
2355  * @param ctx Remote node sm context.
2356  * @param evt Event to process.
2357  * @param arg Per event optional argument.
2358  *
2359  * @return Returns NULL.
2360  */
2361 
2362 static void *
2363 __ocs_ramd_io_aborting_wait_abort_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
2364 {
2365  ocs_io_t *io; /* 'io' here is IO being aborted */
2366 
2367  ocs_assert(ctx, NULL);
2368  ocs_assert(ctx->app, NULL);
2369  io = ctx->app;
2370  ocs_assert(io->node, NULL);
2371  ocs_assert(io->ocs, NULL);
2372 
2373  io_sm_trace();
2374  io->tgt_io.current_state = __OCS_RAMD_IO_ABORTING_WAIT_ABORT_CMPL;
2375 
2376  switch(evt) {
2377  case OCS_EVT_ABORT_CMPL: {
2378  io_printf(io, "IO abort cmpl evt=%s\n", ocs_sm_event_name(evt));
2380  break;
2381  }
2382  case OCS_EVT_ABORT_IO:
2383  case OCS_EVT_ABORT_IO_NO_RESP: {
2384  ocs_ignore_abort_evt(evt, __func__, io, (ocs_io_t *)arg);
2385  break;
2386  }
2387  default:
2388  __ocs_ramd_io_common(__func__, ctx, evt, arg);
2389  break;
2390  }
2391 
2392  return NULL;
2393 }
2394 
2395 /**
2396  * @brief State: Aborting IO.
2397  *
2398  * @par Description
2399  * IO abort has been started and Abort has completed. Wait for
2400  * the IO to complete and then free IO.
2401  *
2402  * @param ctx Remote node sm context.
2403  * @param evt Event to process.
2404  * @param arg Per event optional argument.
2405  *
2406  * @return Returns NULL.
2407  */
2408 static void *
2409 __ocs_ramd_io_aborting_wait_io_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
2410 {
2411  ocs_io_t *io; /* 'io' here is IO being aborted */
2412 
2413  ocs_assert(ctx, NULL);
2414  ocs_assert(ctx->app, NULL);
2415  io = ctx->app;
2416  ocs_assert(io->node, NULL);
2417  ocs_assert(io->ocs, NULL);
2418 
2419  io_sm_trace();
2420  io->tgt_io.current_state = __OCS_RAMD_IO_ABORTING_WAIT_IO_CMPL;
2421 
2422  switch(evt) {
2423  case OCS_EVT_IO_CMPL:
2425  case OCS_EVT_RESP_CMPL:
2426  io_printf(io, "IO cmpl evt=%s\n", ocs_sm_event_name(evt));
2428  break;
2429  case OCS_EVT_ABORT_IO:
2430  case OCS_EVT_ABORT_IO_NO_RESP: {
2431  ocs_ignore_abort_evt(evt, __func__, io, (ocs_io_t *)arg);
2432  break;
2433  }
2434  default:
2435  __ocs_ramd_io_common(__func__, ctx, evt, arg);
2436  break;
2437  }
2438 
2439  return NULL;
2440 }
2441 
2442 /**
2443  * @ingroup io_sm
2444  * @brief State: Common IO event handler.
2445  *
2446  * @par Description
2447  * Handles events common to all states.
2448  *
2449  * @param funcname Calling function name string.
2450  * @param ctx Remote node sm context.
2451  * @param evt Event to process.
2452  * @param arg Per event optional argument.
2453  *
2454  * @return Returns NULL.
2455  */
2456 
2457 static void *
2458 __ocs_ramd_io_common(const char *funcname, ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
2459 {
2460  ocs_io_t *io = ctx->app;
2461  int32_t rc;
2462 
2463  switch(evt) {
2464  case OCS_EVT_ENTER:
2465  case OCS_EVT_REENTER:
2466  case OCS_EVT_EXIT:
2467  break;
2468  case OCS_EVT_SHUTDOWN:
2469  break;
2470  case OCS_EVT_ABORT_IO:
2472  /* Abort the I/O in progress */
2473  io_printf(io, "%-20s evt=%s\n", funcname, ocs_sm_event_name(evt));
2474 
2475  /* for abort_io event, will send completion event after abort
2476  * for abort_io_no_resp event, no such completion will be generated
2477  */
2478  if (evt == OCS_EVT_ABORT_IO) {
2479  ocs_assert(arg, NULL);
2480  io->tgt_io.tmfio = (ocs_io_t *)arg;
2481  } else {
2482  io->tgt_io.tmfio = NULL;
2483  }
2484 
2485  /* transition to state where we wait for completions */
2487  rc = ocs_scsi_tgt_abort_io(io, false, ocs_ramd_abort_tgt_cb, NULL);
2488  if (rc != 0) {
2489  io_printf(io, "Failed to abort IO\n");
2490  }
2491  break;
2492 
2496  io_printf(io, "%-20s ignoring evt %-20s\n", funcname, ocs_sm_event_name(evt));
2497  break;
2498  default:
2499  io_printf(io, "%-20s %-20s not handled\n", funcname, ocs_sm_event_name(evt));
2500  break;
2501  }
2502  return NULL;
2503 }
2504 
2505 /**
2506  * @ingroup scsi_cmd
2507  * @brief Send a good SCSI status.
2508  *
2509  * @par Description
2510  * A good SCSI status is sent to the initiator associated with the IO.
2511  *
2512  * @param io Pointer to the IO context.
2513  *
2514  * @return Returns SCSI_HANDLER_RESP_STARTED on success, or
2515  * SCSI_HANDLER_NOT_STARTED on error.
2516  */
2517 
2520 {
2522 
2523  /* Send the response */
2524  if (ocs_scsi_send_resp(io, 0, NULL, scsi_io_cb, NULL) == 0) {
2526  } else {
2528  }
2529 
2530  return rc;
2531 }
2532 
2533 /**
2534  * @ingroup scsi_cmd
2535  * @brief Send a SCSI check condition.
2536  *
2537  * @par Description
2538  * A SCSI check condition response is sent to the initiator associated with the
2539  * IO.
2540  *
2541  * @param io Pointer to the IO context.
2542  * @param sense_key SCSI response sense key.
2543  * @param asc SCSI response ASC value.
2544  * @param ascq SCSI response ASCA value.
2545  *
2546  * @return Returns SCSI_HANDLER_RESP_STARTED on success, or
2547  * SCSI_HANDLER_NOT_STARTED on error.
2548  */
2549 
2551 ocs_ramd_check_cond(ocs_io_t *io, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2552 {
2554  ocs_scsi_cmd_resp_t rsp;
2555  fixed_sense_data_t sense;
2556 
2557  ocs_memset(&rsp, 0, sizeof(rsp));
2558  ocs_memset(&sense, 0, sizeof(sense));
2559 
2560  rsp.scsi_status = 2;
2561  rsp.sense_data = (uint8_t*) &sense;
2562  rsp.sense_data_length = sizeof(sense);
2563 
2564 #if defined(OCS_INCLUDE_FC)
2565  rsp.residual = io->exp_xfer_len - io->transferred;
2566 #endif
2567 
2568  /* populate sense data with a check condition */
2569  sense.response_code = 0x70;
2570  sense.sense_key = sense_key; /* sense key */
2571  sense.asc = asc; /* logical unit not ready */
2572  sense.ascq = ascq; /* cause not reportable */
2573  sense.additional_sense_length = sizeof (sense) - 8;
2574 
2575  /* Send the response */
2576  if (ocs_scsi_send_resp(io, 0, &rsp, scsi_io_cb, NULL) == 0) {
2578  } else {
2580  }
2581 
2582  return rc;
2583 }
2584 
2585 /**
2586  * @ingroup scsi_cmd
2587  * @brief Send a SCSI task set full or busy status.
2588  *
2589  * @par Description
2590  * A SCSI task set full or busy response is sent, depending on whether
2591  * another IO is already active on the LUN.
2592  *
2593  * @param io Pointer to the IO context.
2594  *
2595  * @return Returns SCSI_HANDLER_RESP_STARTED on success or
2596  * SCSI_HANDLER_NOT_STARTED on error.
2597  */
2600 {
2601  int32_t rc;
2602  ocs_scsi_cmd_resp_t rsp;
2603  ocs_io_t *ioloop;
2604  ocs_io_t *ionext;
2605  ocs_t *ocs = io->ocs;
2606 
2607  ocs_memset(&rsp, 0, sizeof(rsp));
2608 
2609  /*
2610  * If there is another command for the LUN, then send task set full,
2611  * if this is the first one, then send the busy status.
2612  */
2613  rsp.scsi_status = SCSI_STATUS_BUSY; /* Busy */
2614  ocs_lock(&io->node->active_ios_lock);
2615  if (!ocs_list_empty(&io->node->active_ios)) {
2616  ocs_list_foreach_safe(&io->node->active_ios, ioloop, ionext) {
2617  if (ioloop != io &&
2618  ioloop->tgt_io.lun == io->tgt_io.lun) {
2619  rsp.scsi_status = SCSI_STATUS_TASK_SET_FULL; /* Task set full */
2620  break;
2621  }
2622  }
2623  }
2624  ocs_unlock(&io->node->active_ios_lock);
2625 
2626  /* Log a message here indicating a busy or task set full state */
2627  if (OCS_LOG_ENABLE_Q_FULL_BUSY_MSG(ocs)) {
2628  /* Log Task Set Full */
2630  /* Task Set Full Message */
2631  ocs_log_info(ocs, "OCS RAMD TASK SET FULL. Tasks >= %d\n", ocs_atomic_read(&ocs->tgt_ocs.io_high_watermark));
2632  }
2633  else if (rsp.scsi_status == SCSI_STATUS_BUSY) {
2634  /* Log Busy Message */
2635  ocs_log_info(ocs, "OCS RAMD SCSI BUSY\n");
2636  }
2637  }
2638 
2639  /* Send the repsponse on low latency queue, using WQ CLASS 1 */
2640  if (ocs_scsi_send_resp(io, OCS_SCSI_LOW_LATENCY | OCS_SCSI_WQ_CLASS(1), &rsp, scsi_io_cb, NULL) == 0) {
2642  } else {
2644  }
2645  return rc;
2646 }
2647 
2648 /**
2649  * @ingroup scsi_api_target
2650  * @brief Receive a TMF command IO.
2651  *
2652  * @par Description
2653  * Called by the base driver when a SCSI TMF command has been received. The
2654  * target-server processes the command, aborting commands as needed, and posts
2655  * a response using ocs_scsi_send_resp().
2656  * The IO context (ocs_io_t) structure has an element of type ocs_scsi_tgt_io_t, named
2657  * tgt_io that is declared and used by a target-server for private information.
2658  * @n @n @b Notes:
2659  * -If the target-server walks the nodes active_ios linked list, and starts IO
2660  * abort processing, the code <i>must not</i> abort the IO passed into the
2661  * ocs_scsi_recv_tmf() command.
2662  * - This io_to_abort is guaranteed not to be freed during
2663  * this function's scope. Since ocs_scsi_tgt_abort_io() takes a
2664  * reference on the io_to_abort, if ocs_scsi_tgt_abort_io() is
2665  * called within this function's scope, another reference is not
2666  * needed. If ocs_scsi_tgt_abort_io() isn't called in this
2667  * context and the io_to_abort pointer needs to be used beyond
2668  * this function's scope, use ocs_ref_get() to take another
2669  * reference.
2670  *
2671  * @param tmfio Pointer to the IO context.
2672  * @param lun Logical unit value.
2673  * @param cmd Command request.
2674  * @param io_to_abort Pointer to the IO object to abort for
2675  * TASK_ABORT (NULL for all other TMF).
2676  *
2677  * @param flags Flags.
2678  *
2679  * @return Returns 0 on success, or a negative error code value on failure.
2680  */
2681 int32_t
2682 ocs_scsi_recv_tmf(ocs_io_t *tmfio, uint32_t lun, ocs_scsi_tmf_cmd_e cmd, ocs_io_t *io_to_abort, uint32_t flags)
2683 {
2684  ocs_node_t *node;
2685  ocs_t *ocs;
2686  ocs_sport_t *sport;
2688  int32_t rc;
2689 
2690  ocs_assert(tmfio, -1);
2691  ocs_assert(tmfio->node, -1);
2692  ocs_assert(tmfio->ocs, -1);
2693 
2694  node = tmfio->node;
2695  ocs = tmfio->ocs;
2696  sport = node->sport;
2697 
2698  tmfio->tgt_io.lun = lun;
2699 
2700  api_trace();
2701 
2702  /* No watermark checks for task management */
2703 
2704  tmfio->tgt_io.ramdisc = ocs_ramd_lookup(tmfio, lun);
2705  if(tmfio->tgt_io.ramdisc == NULL) {
2706  io_printf(tmfio, "Invalid lun specified, cmd=%d, lun=%d\n", cmd, lun);
2708  NULL, scsi_io_cb, NULL);
2709  if (rc == 0) {
2710  tmfio->tgt_io.sm.app = tmfio;
2711  ocs_sm_transition(&tmfio->tgt_io.sm, __ocs_ramd_tmf_wait_resp_cmpl, NULL);
2712  }
2713  return rc;
2714  }
2715 
2716  switch(cmd) {
2720  io_printf(tmfio, "unsupported TMF=%d\n", cmd);
2721  break;
2722 
2723  /*
2724  * Post ABORT_IO event to the IO, the IO state machine will call ocs_scsi_send_tmf_resp()
2725  * when complete, so we return now.
2726  */
2727  case OCS_SCSI_TMF_ABORT_TASK: {
2728  /* save the task management IO object in the IO that is being aborted */
2729  io_printf(tmfio, "abort task: aborting " IOFMT "\n", IOFMT_ARGS(io_to_abort));
2730 
2731  /*
2732  * Move tmfio to wait for IO abort where response will be sent, then
2733  * send ABORT event to io_to_abort state machine
2734  */
2735  tmfio->tgt_io.sm.app = tmfio;
2736  ocs_sm_transition(&tmfio->tgt_io.sm, __ocs_ramd_abts_wait_io_abort_resp, NULL);
2737 
2738  /*
2739  * Taking a reference on io_to_abort is not needed since the ABORT_IO
2740  * event immediately results in a call to ocs_scsi_tgt_abort_io()
2741  */
2742  if (ocs_sm_post_event(&io_to_abort->tgt_io.sm, OCS_EVT_ABORT_IO, tmfio)) {
2743  ocs_assert(FALSE, -1);
2744  }
2745  return 0;
2746  }
2747 
2749  /* Abort all tasks for this node */
2750  ocs_log_debug(ocs, "[%s] abort_task_set\n", node->display_name);
2751  ocs_ramd_abort_node_tasks_lun(node, tmfio->tgt_io.lun, tmfio);
2752  break;
2753  }
2755  ocs_node_t *n;
2756  ocs_node_t *np;
2757 
2758  ocs_log_debug(ocs, "[%s] clear_task_set\n", node->display_name);
2759 
2760  /* Abort all I/Os */
2761  ocs_sport_lock(sport);
2762  ocs_list_foreach_safe(&sport->node_list, n, np) {
2763  ocs_ramd_abort_node_tasks_lun(n, tmfio->tgt_io.lun, tmfio);
2764  }
2765  ocs_sport_unlock(sport);
2766  break;
2767  }
2769  ocs_node_t *n;
2770  ocs_node_t *np;
2771 
2772  ocs_log_debug(ocs, "[%s] logical unit reset, lun x%x\n", node->display_name, tmfio->tgt_io.lun);
2773 
2774  /* Abort all I/Os */
2775  ocs_sport_lock(sport);
2776  ocs_list_foreach_safe(&sport->node_list, n, np) {
2777  ocs_ramd_abort_node_tasks_lun(n, tmfio->tgt_io.lun, tmfio);
2778  }
2779  ocs_sport_unlock(sport);
2780 
2781  ocs_ramd_post_ua(ocs, tmfio->tgt_io.ramdisc,
2784  NULL);
2785  break;
2786  }
2787 
2789  ocs_node_t *n;
2790  ocs_node_t *np;
2791  uint32_t lun;
2792  uint32_t lun_count;
2793 
2794  /* get lun count */
2795  if (ocs->tgt_ocs.use_global_ramd) {
2796  lun_count = ocs->tgt_ocs.rdisc_count;
2797  } else {
2798  lun_count = sport->tgt_sport.rdisc_count;
2799  }
2800 
2801  /* perform lun reset on each LUN */
2802  ocs_log_debug(ocs, "[%s] target reset: lun count=%d\n",
2803  node->display_name, lun_count);
2804 
2805  /* abort IOs on all nodes on all LUNs */
2806  ocs_sport_lock(sport);
2807  ocs_list_foreach_safe(&sport->node_list, n, np) {
2808  ocs_ramd_abort_node_tasks(n, tmfio);
2809  }
2810  ocs_sport_unlock(sport);
2811 
2812  /* establish UA for all LUNs */
2813  for (lun = 0; lun < lun_count; lun++) {
2814  ocs_ramd_post_ua(ocs, tmfio->tgt_io.ramdisc,
2817  NULL);
2818  }
2819  break;
2820  }
2821 
2822  }
2823 
2824  rc = ocs_scsi_send_tmf_resp(tmfio, rspcode, NULL, scsi_io_cb, NULL);
2825  if (rc == 0) {
2826  tmfio->tgt_io.sm.app = tmfio;
2827  ocs_sm_transition(&tmfio->tgt_io.sm, __ocs_ramd_tmf_wait_resp_cmpl, NULL);
2828  }
2829  return rc;
2830 }
2831 
2832 /**
2833  * @brief Response phase callback.
2834  *
2835  * @par Description
2836  * This function is called when the response phase for an IO completes.
2837  *
2838  * @param io Pointer to the IO structure.
2839  * @param scsi_status Completion status.
2840  * @param flags Flags.
2841  * @param arg Application-specified argument.
2842  *
2843  * @return Returns 0 on success, or a negative error code value on failure.
2844  */
2845 int32_t
2846 _scsi_io_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
2847 {
2848  api_trace();
2849 
2850  ocs_assert(io, -1);
2851 
2852  /* for debugging state machines, send RESP_CMPL event instead of directly freeing */
2853  ocs_lock(&io->tgt_io.sm_lock);
2854  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_RESP_CMPL, NULL);
2855  ocs_unlock(&io->tgt_io.sm_lock);
2856 
2857  return 0;
2858 }
2859 
2860 /**
2861  * @brief Command data phase callback
2862  *
2863  * @par Description
2864  * This function is called when the SCSI command data phase has completed.
2865  *
2866  * @param io Pointer to the IO structure.
2867  * @param scsi_status Completion status.
2868  * @param flags Flags.
2869  * @param arg Application-specified argument.
2870  *
2871  * @return Returns 0 on success, or a negative error code value on failure.
2872  */
2873 
2874 int32_t
2875 _scsi_dataphase_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
2876 {
2877  api_trace();
2878 
2879  ocs_assert(io, -1);
2880  ocs_assert(io->ocs, -1);
2881 
2882  ocs_lock(&io->tgt_io.sm_lock);
2883 
2884 #if ENABLE_BUFFER_DUMP==1
2885 #if defined(OCS_INCLUDE_DEBUG)
2886  /* Dump buffers if enabled for write commands */
2887  if (io->tgt_io.is_read == FALSE) {
2888  ocs_t *ocs = io->ocs;
2889  ocs_ramdisc_sgl_t sgl[64];
2890  uint32_t max_sgl = (ocs->tgt_ocs.max_sgl < ARRAY_SIZE(sgl) ? ocs->tgt_ocs.max_sgl : ARRAY_SIZE(sgl));
2891  uint32_t blk_count = io->tgt_io.cur_block_count;
2892  uint32_t sgl_count;
2893  uint64_t lba;
2894  uint32_t sgl_flags = 0;
2895 
2896  if (io->tgt_io.sgl_phys) {
2897  sgl_flags |= SGL_REQ_FLAGS_PHYS;
2898  }
2899 
2900  lba = io->tgt_io.io_lba + io->tgt_io.acc_block_count;
2901  sgl_count = ocs_ramdisc_sgl(ocs, io->tgt_io.ramdisc, sgl, max_sgl, ocs->tgt_ocs.max_sge,
2902  lba, &blk_count, sgl_flags, TRUE);
2903  }
2904 #endif
2905 #endif
2906 
2907  switch (scsi_status) {
2908  case OCS_SCSI_STATUS_GOOD:
2909  {
2910  int32_t rc;
2911  io->tgt_io.acc_block_count += io->tgt_io.cur_block_count;
2912 
2913  if (io->tgt_io.acc_block_count < io->tgt_io.io_block_count) {
2914  io->tgt_io.dif_info.ref_tag += io->tgt_io.cur_block_count;
2915 
2916  rc = ocs_ramd_data_phase_start(io);
2917  /* Errors here are not expected.
2918  * If there is no error, code will fall through to bottom
2919  */
2920  if (rc != 0) {
2921  const char *fn = strrchr(__FILE__, '/'); \
2922  ocs_log_err(io->ocs, "%s(%d) assertion (rc == 0) failed\n", (fn ? fn + 1 : __FILE__), __LINE__);
2923  ocs_unlock(&io->tgt_io.sm_lock);
2924  return rc;
2925  }
2926  } else if (flags & OCS_SCSI_IO_CMPL) {
2927 #if ENABLE_DIF_VERIFICATION == 1
2928  if (!io->tgt_io.is_read) {
2929  if (ocs_ramdisc_dif_verify(io->ocs, io->tgt_io.ramdisc, io->tgt_io.io_lba,
2930  io->tgt_io.io_block_count) != 0) {
2931  ocs_log_test(io->ocs, "Write DIF IO at LBA 0x%"
2932  PRIx64 " for %d blocks with bad data in RAM\n",
2933  io->tgt_io.io_lba, io->tgt_io.io_block_count);
2934  }
2935  }
2936 #endif
2937  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_IO_CMPL, NULL);
2938  }
2939  else if (flags & OCS_SCSI_IO_CMPL_RSP_SENT) {
2940  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_RESP_CMPL, NULL);
2941  }
2942  break;
2943  }
2944  default:
2945  io_printf(io, "dataphase error %d\n", scsi_status);
2946  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_IO_CMPL_ERRORS, &scsi_status);
2947  break;
2948  }
2949 
2950  ocs_unlock(&io->tgt_io.sm_lock);
2951  return 0;
2952 }
2953 
2954 /**
2955  * @ingroup scsi_cmd
2956  * @brief Process the SCSI WRITE (6) command.
2957  *
2958  * @par Description
2959  * The SCSI WRITE (6) command is processed.
2960  *
2961  * @param io Pointer to the IO context.
2962  * @param cdb Pointer to the SCSI CDB.
2963  *
2964  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
2965  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
2966  */
2967 static ocs_ramd_hndlr_rtn_e
2968 ocs_ramd_write6(ocs_io_t *io, uint8_t cdb[]) {
2969  uint64_t lba;
2970  uint32_t block_count;
2971  uint8_t wrprotect;
2972 
2973  /* Validate the CDB */
2974  ocs_assert(cdb[0] == SCSI_WRITE6, -1);
2975 
2976  /* Extract LBA */
2977  lba = ((cdb[1] & 0x1f) << 16) |
2978  (cdb[2] << 8) |
2979  (cdb[3] << 0);
2980 
2981  /* Extract Block Count */
2982  block_count = cdb[4];
2983 
2984  /* SBC-3 states that a transfer length of 0
2985  * specifies that 256 logical blocks shall be written.
2986  */
2987  if (block_count == 0){
2988  block_count = 256;
2989  }
2990 
2991  /* WRITE6 has no way to specify WRPROTECT - and in fact WRITE6
2992  * is deprecated in SBC-3 so there's really no definition that
2993  * covers what should be done here.
2994  * If we have protection enabled we'll set WRPROTECT to 3 which
2995  * is passthrough with no checking. If protection is not enabled
2996  * we'll set it to 0.
2997  */
2998  if (io->tgt_io.ramdisc->prot_en == 0) {
2999  wrprotect = 0;
3000  } else {
3001  wrprotect = 3;
3002  }
3003 
3004  /* Call common function to do the I/O */
3005  return ocs_ramd_write_common(io, lba, block_count, wrprotect, INVALID_APP_TAG, lba&0xffffffff);
3006 }
3007 
3008 /**
3009  * @ingroup scsi_cmd
3010  * @brief Process the SCSI WRITE (10) command.
3011  *
3012  * @par Description
3013  * The SCSI WRITE (10) command is processed.
3014  *
3015  * @param io Pointer to the IO context.
3016  * @param cdb Pointer to the SCSI CDB.
3017  *
3018  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3019  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3020  */
3021 static ocs_ramd_hndlr_rtn_e
3022 ocs_ramd_write10(ocs_io_t *io, uint8_t cdb[]) {
3023  uint64_t lba;
3024  uint32_t block_count;
3025  uint8_t wrprotect;
3026 
3027  ocs_assert((cdb[0] == SCSI_WRITE10) || (cdb[0] == SCSI_WRITE_AND_VERIFY10), -1);
3028 
3029  /* Extract LBA */
3030  lba = ((uint32_t)(cdb[2] << 24)) |
3031  ((uint32_t)(cdb[3] << 16)) |
3032  ((uint32_t)(cdb[4] << 8)) |
3033  ((uint32_t)(cdb[5] << 0));
3034 
3035  /* Extract Block Count */
3036  block_count =
3037  (cdb[7] << 8) |
3038  (cdb[8] << 0);
3039 
3040  /* Extract WRPROTECT */
3041  wrprotect = ((cdb[1] & 0xe0) >> 5);
3042 
3043  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (wrprotect != 0)) {
3044  /* Write10 not supported with prot type 2 and non-zero wrprotect */
3046  }
3047 
3048  /* Call common function to do the I/O */
3049  return ocs_ramd_write_common(io, lba, block_count, wrprotect, INVALID_APP_TAG, lba&0xffffffff);
3050 }
3051 
3052 /**
3053  * @ingroup scsi_cmd
3054  * @brief Process the SCSI WRITE (12) command.
3055  *
3056  * @par Description
3057  * The SCSI WRITE (12) command is processed.
3058  *
3059  * @param io Pointer to the IO context.
3060  * @param cdb Pointer to the SCSI CDB.
3061  *
3062  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3063  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3064  */
3065 static ocs_ramd_hndlr_rtn_e
3066 ocs_ramd_write12(ocs_io_t *io, uint8_t cdb[]) {
3067  uint64_t lba;
3068  uint32_t block_count;
3069  uint8_t wrprotect;
3070 
3071  ocs_assert((cdb[0] == SCSI_WRITE12) || (cdb[0] == SCSI_WRITE_AND_VERIFY12), -1);
3072 
3073  /* Extract LBA */
3074  lba = ((uint32_t)(cdb[2] << 24)) |
3075  ((uint32_t)(cdb[3] << 16)) |
3076  ((uint32_t)(cdb[4] << 8)) |
3077  ((uint32_t)(cdb[5] << 0));
3078 
3079  /* Extract Block Count */
3080  block_count =
3081  ((uint32_t)(cdb[6] << 24)) |
3082  ((uint32_t)(cdb[7] << 16)) |
3083  ((uint32_t)(cdb[8] << 8)) |
3084  ((uint32_t)(cdb[9] << 0));
3085 
3086  /* Extract WRPROTECT */
3087  wrprotect = ((cdb[1] & 0xe0) >> 5);
3088 
3089  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (wrprotect != 0)) {
3090  /* Write12 not supported with prot type 2 and non-zero wrprotect */
3092  }
3093 
3094  /* Call common function to do the I/O */
3095  return ocs_ramd_write_common(io, lba, block_count, wrprotect, INVALID_APP_TAG, lba&0xffffffff);
3096 }
3097 
3098 /**
3099  * @ingroup scsi_cmd
3100  * @brief Process the SCSI WRITE (16) command.
3101  *
3102  * @par Description
3103  * The SCSI WRITE (16) command is processed.
3104  *
3105  * @param io Pointer to the IO context.
3106  * @param cdb Pointer to the SCSI CDB.
3107  *
3108  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3109  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3110  */
3111 static ocs_ramd_hndlr_rtn_e
3112 ocs_ramd_write16(ocs_io_t *io, uint8_t cdb[]) {
3113  uint64_t lba;
3114  uint32_t block_count;
3115  uint8_t wrprotect;
3116 
3117  ocs_assert((cdb[0] == SCSI_WRITE16) || (cdb[0] == SCSI_WRITE_AND_VERIFY16), -1);
3118 
3119  /* Extract LBA */
3120  lba = ((uint64_t)cdb[2] << 56) |
3121  ((uint64_t)cdb[3] << 48) |
3122  ((uint64_t)cdb[4] << 40) |
3123  ((uint64_t)cdb[5] << 32) |
3124  ((uint64_t)cdb[6] << 24) |
3125  ((uint64_t)cdb[7] << 16) |
3126  ((uint64_t)cdb[8] << 8) |
3127  ((uint64_t)cdb[9] << 0);
3128 
3129  /* Extract Block Count */
3130  block_count =
3131  (cdb[10] << 24) |
3132  (cdb[11] << 16) |
3133  (cdb[12] << 8) |
3134  (cdb[13] << 0);
3135 
3136  /* Extract WRPROTECT */
3137  wrprotect = ((cdb[1] & 0xe0) >> 5);
3138 
3139  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (wrprotect != 0)) {
3140  /* Write16 not supported with prot type 2 and non-zero wrprotect */
3142  }
3143 
3144  /* Call common function to do the I/O */
3145  return ocs_ramd_write_common(io, lba, block_count, wrprotect, INVALID_APP_TAG, lba&0xffffffff);
3146 }
3147 
3148 /**
3149  * @ingroup scsi_cmd
3150  * @brief Process the SCSI WRITE (32) command.
3151  *
3152  * @par Description
3153  * The SCSI WRITE (32) command is processed.
3154  *
3155  * @param io Pointer to the IO context.
3156  * @param cdb Pointer to the SCSI CDB.
3157  *
3158  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3159  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3160  */
3161 static ocs_ramd_hndlr_rtn_e
3162 ocs_ramd_write32(ocs_io_t *io, uint8_t cdb[]) {
3163  uint64_t lba;
3164  uint32_t block_count;
3165  uint8_t wrprotect;
3166  uint32_t ref_tag;
3167  uint32_t app_tag;
3168 
3169  /* Validate the CDB */
3170  ocs_assert(cdb[0] == SCSI_VARIABLE_LEN_CDB, -1);
3172  (cdb[9] == SCSI_VAR_CMD_SVC_ACT_WRITE_VERIFY32), -1);
3173 
3174 
3175  /* Extract LBA */
3176  lba = ((uint64_t)cdb[12] << 56) |
3177  ((uint64_t)cdb[13] << 48) |
3178  ((uint64_t)cdb[14] << 40) |
3179  ((uint64_t)cdb[15] << 32) |
3180  ((uint64_t)cdb[16] << 24) |
3181  ((uint64_t)cdb[17] << 16) |
3182  ((uint64_t)cdb[18] << 8) |
3183  ((uint64_t)cdb[19] << 0);
3184 
3185  /* Extract Block Count */
3186  block_count =
3187  (cdb[28] << 24) |
3188  (cdb[29] << 16) |
3189  (cdb[30] << 8) |
3190  (cdb[31] << 0);
3191 
3192  /* Extract WRPROTECT */
3193  wrprotect = ((cdb[10] & 0xe0) >> 5);
3194 
3195  /* Extract Ref Tag */
3196  ref_tag = (cdb[20] << 24) |
3197  (cdb[21] << 16) |
3198  (cdb[22] << 8) |
3199  (cdb[23] << 0);
3200 
3201  /* Extract App Tag */
3202  app_tag = (cdb[24] << 8) |
3203  (cdb[25] << 0);
3204 
3205  if (io->tgt_io.ramdisc->prot_en == 0) {
3206  if (wrprotect != 0) {
3207  /* SBC 4.21.2.2: If type 0 protection is enabled and the wrprotect field is
3208  * non-zero ... ILLEGAL REQUEST / INVALID FIELD IN CDB
3209  */
3211  } else {
3212  /* If type 0 protection is enabled and the wrprotect field is set to a zero
3213  * value ... ILLEGAL REQUEST / INVALID COMMAND OPERATION CODE
3214  */
3216  }
3217  }
3218 
3219  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type != 2)) {
3220  /* SBC 4.21.2.3: If type 1 protection is enabled, then the following medium commands
3221  * are invalid ... ILLEGAL_REQUEST / INVALID_COMMAND_OPERATION_CODE
3222  */
3224  }
3225 
3226  /* Call common function to do the I/O */
3227  return ocs_ramd_write_common(io, lba, block_count, wrprotect, app_tag, ref_tag);
3228 }
3229 
3230 /**
3231  * @ingroup scsi_cmd
3232  * @brief Process the SCSI WRITE LONG (10) command.
3233  *
3234  * @par Description
3235  * The SCSI WRITE LONG (10) command is processed.
3236  *
3237  * @param io Pointer to the IO context.
3238  * @param cdb Pointer to the SCSI CDB.
3239  *
3240  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3241  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3242  */
3243 static ocs_ramd_hndlr_rtn_e
3244 ocs_ramd_write_long10(ocs_io_t *io, uint8_t cdb[])
3245 {
3246  uint64_t lba;
3247  uint16_t xfer_len;
3248  uint8_t cor_dis, wr_uncor, pblock;
3249 
3250  /* Validate the CDB */
3251  ocs_assert((cdb[0] == SCSI_WRITE_LONG10), -1);
3252 
3253  cor_dis = (cdb[1] >> 7) & 1;
3254  wr_uncor = (cdb[1] >> 6) & 1;
3255  pblock = (cdb[1] >> 5) & 1;
3256 
3257  /* Extract LBA */
3258  lba = ((uint32_t)(cdb[2] << 24)) |
3259  ((uint32_t)(cdb[3] << 16)) |
3260  ((uint32_t)(cdb[4] << 8)) |
3261  ((uint32_t)(cdb[5] << 0));
3262 
3263  xfer_len = (cdb[7] << 8) |
3264  (cdb[8] << 0);
3265 
3266  /* As per SBC-3 a transfer length of 0 means no data transfer
3267  * so just return good status now. unless the wr_uncor is set.
3268  */
3269  if ((xfer_len == 0) && (!wr_uncor)) {
3270  return(ocs_ramd_good_status(io));
3271  }
3272 
3273  /* Call common function to do the I/O */
3274  return ocs_ramd_write_long_common(io, lba, xfer_len, cor_dis, wr_uncor, pblock);
3275 }
3276 
3277 /**
3278  * @ingroup scsi_cmd
3279  * @brief Process the SCSI WRITE LONG (16) command.
3280  *
3281  * @par Description
3282  * The SCSI WRITE LONG (16) command is processed.
3283  *
3284  * @param io Pointer to the IO context.
3285  * @param cdb Pointer to the SCSI CDB.
3286  *
3287  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3288  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3289  */
3290 static ocs_ramd_hndlr_rtn_e
3291 ocs_ramd_write_long16(ocs_io_t *io, uint8_t cdb[])
3292 {
3293  uint64_t lba;
3294  uint16_t xfer_len;
3295  uint8_t cor_dis, wr_uncor, pblock;
3296 
3297  /* Validate the CDB */
3298  ocs_assert(cdb[0] == SCSI_SERVICE_ACTION_OUT16, -1);
3299  ocs_assert(((cdb[1] & 0x1F) == SCSI_SVC_ACTION16_READ_LONG), -1);
3300 
3301  cor_dis = (cdb[1] >> 7) & 1;
3302  wr_uncor = (cdb[1] >> 6) & 1;
3303  pblock = (cdb[1] >> 5) & 1;
3304 
3305  /* Extract LBA */
3306  lba = ((uint64_t)cdb[2] << 56) |
3307  ((uint64_t)cdb[3] << 48) |
3308  ((uint64_t)cdb[4] << 40) |
3309  ((uint64_t)cdb[5] << 32) |
3310  ((uint64_t)cdb[6] << 24) |
3311  ((uint64_t)cdb[7] << 16) |
3312  ((uint64_t)cdb[8] << 8) |
3313  ((uint64_t)cdb[9] << 0);
3314 
3315  xfer_len = (cdb[12] << 8) |
3316  (cdb[13] << 0);
3317 
3318  /* As per SBC-3 a transfer length of 0 means no data transfer
3319  * so just return good status now. unless the wr_uncor is set.
3320  */
3321  if ((xfer_len == 0) && (!wr_uncor)) {
3322  return(ocs_ramd_good_status(io));
3323  }
3324 
3325  /* Call common function to do the I/O */
3326  return ocs_ramd_write_long_common(io, lba, xfer_len, cor_dis, wr_uncor, pblock);
3327 }
3328 
3329 /**
3330  * @ingroup scsi_cmd
3331  * @brief Process the SCSI WRITE SAME (10) command.
3332  *
3333  * @par Description
3334  * The SCSI WRITE SAME (10) command is processed.
3335  *
3336  * @param io Pointer to the IO context.
3337  * @param cdb Pointer to the SCSI CDB.
3338  *
3339  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3340  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3341  */
3342 static ocs_ramd_hndlr_rtn_e
3343 ocs_ramd_write_same10(ocs_io_t *io, uint8_t cdb[])
3344 {
3345  uint64_t lba;
3346  uint16_t num_blocks;
3347  uint8_t wrprotect;
3348  uint8_t pbdata;
3349  uint8_t lbdata;
3350 
3351  /* Validate the CDB */
3352  ocs_assert((cdb[0] == SCSI_WRITE_SAME10), -1);
3353 
3354  wrprotect = (cdb[1] & 0xe0) >> 5;
3355  pbdata = (cdb[1] & 0x04) >> 2;
3356  lbdata = (cdb[1] & 0x02) >> 1;
3357 
3358  /* Extract LBA */
3359  lba = ((uint32_t)(cdb[2] << 24)) |
3360  ((uint32_t)(cdb[3] << 16)) |
3361  ((uint32_t)(cdb[4] << 8)) |
3362  ((uint32_t)(cdb[5] << 0));
3363 
3364  num_blocks = (cdb[7] << 8) |
3365  (cdb[8] << 0);
3366 
3367  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (wrprotect != 0)) {
3368  /* WRITE SAME(10) not supported with prot type 2 and non-zero wrprotect */
3370  }
3371 
3372  /* Call common function to do the I/O */
3373  return ocs_ramd_write_same_common(io, lba, num_blocks, wrprotect, pbdata, lbdata, INVALID_APP_TAG, lba & 0xffffffff);
3374 }
3375 
3376 /**
3377  * @ingroup scsi_cmd
3378  * @brief Process the SCSI WRITE SAME (16) command
3379  *
3380  * @par Description
3381  * The SCSI WRITE SAME (16) command is processed.
3382  *
3383  * @param io Pointer to the IO context.
3384  * @param cdb Pointer to the SCSI CDB.
3385  *
3386  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3387  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3388  */
3389 static ocs_ramd_hndlr_rtn_e
3390 ocs_ramd_write_same16(ocs_io_t *io, uint8_t cdb[])
3391 {
3392  uint64_t lba;
3393  uint16_t num_blocks;
3394  uint8_t wrprotect;
3395  uint8_t pbdata;
3396  uint8_t lbdata;
3397 
3398  /* Validate the CDB */
3399  ocs_assert((cdb[0] == SCSI_WRITE_SAME16), -1);
3400 
3401  wrprotect = (cdb[1] & 0xe0) >> 5;
3402  pbdata = (cdb[1] & 0x04) >> 2;
3403  lbdata = (cdb[1] & 0x02) >> 1;
3404 
3405  /* Extract LBA */
3406  lba = ((uint64_t)cdb[2] << 56) |
3407  ((uint64_t)cdb[3] << 48) |
3408  ((uint64_t)cdb[4] << 40) |
3409  ((uint64_t)cdb[5] << 32) |
3410  ((uint64_t)cdb[6] << 24) |
3411  ((uint64_t)cdb[7] << 16) |
3412  ((uint64_t)cdb[8] << 8) |
3413  ((uint64_t)cdb[9] << 0);
3414 
3415  num_blocks = ((uint64_t)cdb[10] << 24) |
3416  ((uint64_t)cdb[11] << 16) |
3417  ((uint64_t)cdb[12] << 8) |
3418  ((uint64_t)cdb[13] << 0);
3419 
3420  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (wrprotect != 0)) {
3421  /* WRITE SAME(16) not supported with prot type 2 and non-zero wrprotect */
3423  }
3424 
3425  /* Call common function to do the I/O */
3426  return ocs_ramd_write_same_common(io, lba, num_blocks, wrprotect, pbdata, lbdata, INVALID_APP_TAG, lba & 0xffffffff);
3427 }
3428 
3429 /**
3430  * @ingroup scsi_cmd
3431  * @brief Process the SCSI WRITE SAME (32) command
3432  *
3433  * @par Description
3434  * The SCSI WRITE SAME (32) command is processed.
3435  *
3436  * @param io Pointer to the IO context.
3437  * @param cdb Pointer to the SCSI CDB.
3438  *
3439  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3440  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3441  */
3442 static ocs_ramd_hndlr_rtn_e
3443 ocs_ramd_write_same32(ocs_io_t *io, uint8_t cdb[])
3444 {
3445  uint64_t lba;
3446  uint16_t num_blocks;
3447  uint8_t wrprotect;
3448  uint8_t pbdata;
3449  uint8_t lbdata;
3450  uint32_t expected_ref_tag;
3451  uint16_t expected_app_tag;
3452 
3453  /* Validate the CDB */
3454  ocs_assert(cdb[0] == SCSI_VARIABLE_LEN_CDB, -1);
3456 
3457  wrprotect = (cdb[10] & 0xe0) >> 5;
3458  pbdata = (cdb[10] & 0x04) >> 2;
3459  lbdata = (cdb[10] & 0x02) >> 1;
3460 
3461  /* Extract LBA */
3462  lba = ((uint64_t)cdb[12] << 56) |
3463  ((uint64_t)cdb[13] << 48) |
3464  ((uint64_t)cdb[14] << 40) |
3465  ((uint64_t)cdb[15] << 32) |
3466  ((uint64_t)cdb[16] << 24) |
3467  ((uint64_t)cdb[17] << 16) |
3468  ((uint64_t)cdb[18] << 8) |
3469  ((uint64_t)cdb[19] << 0);
3470 
3471  expected_ref_tag = ((uint64_t)cdb[20] << 24) |
3472  ((uint64_t)cdb[21] << 16) |
3473  ((uint64_t)cdb[22] << 8) |
3474  ((uint64_t)cdb[23] << 0);
3475 
3476  expected_app_tag = ((uint64_t)cdb[24] << 8) |
3477  ((uint64_t)cdb[25] << 0);
3478 
3479  num_blocks = ((uint64_t)cdb[28] << 24) |
3480  ((uint64_t)cdb[29] << 16) |
3481  ((uint64_t)cdb[30] << 8) |
3482  ((uint64_t)cdb[31] << 0);
3483 
3484  if (io->tgt_io.ramdisc->prot_en == 0) {
3485  if (wrprotect != 0) {
3486  /* SBC 4.21.2.2: If type 0 protection is enabled and the wrprotect field is
3487  * non-zero ... ILLEGAL REQUEST / INVALID FIELD IN CDB
3488  */
3490  } else {
3491  /* If type 0 protection is enabled and the wrprotect field is set to a zero
3492  * value ... ILLEGAL REQUEST / INVALID COMMAND OPERATION CODE
3493  */
3495  }
3496  }
3497 
3498  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type != 2)) {
3499  /* SBC 4.21.2.3: If type 1 protection is enabled, then the following medum commands
3500  * are invalid ... ILLEGAL_REQUEST / INVALID_COMMAND_OPERATION_CODE
3501  */
3503  }
3504 
3505  /* Call common function to do the I/O */
3506  return ocs_ramd_write_same_common(io, lba, num_blocks, wrprotect, pbdata, lbdata,
3507  expected_app_tag, expected_ref_tag);
3508 }
3509 
3510 /**
3511  * @ingroup scsi_cmd
3512  * @brief Process first burst data into the IO DMA memory.
3513  *
3514  * @param io Pointer to the IO context.
3515  * @param bytes_left The number of first burst bytes received.
3516  *
3517  * @return Returns 0 on success, or a negative error code value on failure.
3518  */
3519 static int32_t
3520 ocs_ramd_first_burst_local(ocs_io_t *io, uint32_t bytes_left)
3521 {
3522  uint32_t offset = 0;
3523  uint32_t buf_index = 0;
3524  uint32_t buffer_size;
3525 
3526  while (bytes_left > 0) {
3527  buffer_size = (bytes_left > io->tgt_io.first_burst_buffers[buf_index].size ?
3528  io->tgt_io.first_burst_buffers[buf_index].size : bytes_left);
3529  /* copy the data to the allocated buffer */
3530  ocs_memcpy((uint8_t *)io->tgt_io.payload.virt + offset,
3531  io->tgt_io.first_burst_buffers[buf_index].virt,
3532  buffer_size);
3533  buf_index++;
3534  bytes_left -= buffer_size;
3535  offset += buffer_size;
3536  }
3537  return 0;
3538 }
3539 
3540 /**
3541  * @ingroup scsi_cmd
3542  * @brief Process the first burst data without any DIF information.
3543  *
3544  * @param io Pointer to the IO context.
3545  * @param ramdisc Pointer to the ramdisc structure.
3546  *
3547  * @return Returns 0 on success, or a negative error code value on failure.
3548  */
3549 static int32_t
3551 {
3552  uint32_t blocksize = ramdisc->phys_block_size;
3553  uint32_t buf_index = 0;
3554  uint64_t lba = io->tgt_io.io_lba;
3555  uint32_t bytes_left;
3556 
3557  uint8_t *src;
3558  uint32_t src_remaining;
3559  uint8_t *dst;
3560  uint32_t dst_remaining;
3561  uint32_t n;
3562 
3563  /*
3564  * Since the ramdisk does not handle partial data blocks,
3565  * truncate the immediate data to a block boundary.
3566  */
3567  bytes_left = (io->tgt_io.first_burst_bytes - (io->tgt_io.first_burst_bytes % blocksize));
3568 
3569  src = io->tgt_io.first_burst_buffers[buf_index].virt;
3570  src_remaining = io->tgt_io.first_burst_buffers[buf_index++].size;
3571 
3572 #if defined(OCS_INCLUDE_FC)
3573  if (io->ocs->esoc) {
3574  uint64_t lba_offset;
3575  /* adjust for HP offset */
3576  lba_offset = (lba % 32) * blocksize;
3577  src += lba_offset;
3578  }
3579 #endif
3580 
3581  dst = ocs_ramdisc_lba_address(ramdisc, lba++);
3582  dst_remaining = blocksize;
3583 
3584  while (bytes_left > 0) {
3585 
3586  n = MIN(bytes_left, MIN(dst_remaining, src_remaining));
3587 
3588  ocs_memcpy(dst, src, n);
3589  src += n;
3590  src_remaining -= n;
3591  dst += n;
3592  dst_remaining -= n;
3593  bytes_left -= n;
3594 
3595  if (src_remaining == 0) {
3596  src = io->tgt_io.first_burst_buffers[buf_index].virt;
3597  src_remaining = io->tgt_io.first_burst_buffers[buf_index++].size;
3598  }
3599  if (dst_remaining == 0) {
3600  dst = ocs_ramdisc_lba_address(ramdisc, lba++);
3601  dst_remaining = blocksize;
3602  }
3603  }
3604 
3605  io->tgt_io.acc_block_count += io->tgt_io.first_burst_bytes / ramdisc->phys_block_size;
3606  return 0;
3607 }
3608 
3609 /**
3610  * @ingroup scsi_cmd
3611  * @brief Set the Disable App and reference fields based on the protection type.
3612  *
3613  * @param ocs Pointer to the ocs structure.
3614  * @param ramdisc Pointer to the ramdisc structure.
3615  * @param dif_info Pointer to the DIF information structure for an IO.
3616  */
3617 static void
3619  ocs_ramdisc_t *ramdisc,
3620  ocs_scsi_dif_info_t *dif_info)
3621 {
3622  if (ramdisc->prot_en) {
3623  if (ocs->ctrlmask & OCS_CTRLMASK_TGT_ALWAYS_VERIFY_DIF) {
3624  dif_info->disable_app_ffff = 0;
3625  dif_info->disable_app_ref_ffff = 0;
3626  } else {
3627  switch(ramdisc->p_type) {
3628  case 0:
3629  dif_info->disable_app_ffff = 0;
3630  dif_info->disable_app_ref_ffff = 0;
3631  break;
3632  case 1:
3633  dif_info->disable_app_ffff = 1;
3634  dif_info->disable_app_ref_ffff = 0;
3635  break;
3636  case 2:
3637  dif_info->disable_app_ffff = 1;
3638  dif_info->disable_app_ref_ffff = 0;
3639  break;
3640  case 3:
3641  dif_info->disable_app_ffff = 0;
3642  dif_info->disable_app_ref_ffff = 1;
3643  break;
3644  default:
3645  dif_info->disable_app_ffff = 0;
3646  dif_info->disable_app_ref_ffff = 0;
3647  break;
3648  }
3649  }
3650  }
3651 }
3652 
3653 
3654 /**
3655  * @ingroup scsi_cmd
3656  * @brief Process the first burst data by adding/verifying/stripping DIF information.
3657  *
3658  * @param io Pointer to the IO context.
3659  * @param ramdisc Pointer to the ramdisc structure.
3660  * @param data_blk_size Size of the block without any DIF.
3661  *
3662  * @return Returns 0 on success, or a negative error code value on failure.
3663  */
3664 static int32_t
3666  ocs_ramdisc_t *ramdisc,
3667  uint32_t data_blk_size)
3668 {
3669  ocs_scsi_dif_info_t *dif_info = &(io->tgt_io.dif_info);
3670  uint32_t blocksize;
3671  uint32_t bytes_left;
3672  uint32_t buf_index = 0;
3673  uint32_t blk_count = 0;
3674  uint64_t lba = io->tgt_io.io_lba;
3675  uint16_t crc;
3676  dif_t difdata;
3677  uint16_t recv_crc;
3678  uint16_t recv_apptag;
3679  uint32_t recv_reftag;
3680 
3681  uint8_t *src;
3682  uint32_t src_remaining;
3683  uint8_t *dst;
3684  uint32_t dst_remaining;
3685  uint32_t n;
3686 
3687  if (dif_info->dif_oper == OCS_SCSI_DIF_OPER_DISABLED) {
3688  blocksize = ramdisc->phys_block_size;
3689  } else {
3690  blocksize = ocs_scsi_dif_wire_blocksize(dif_info, !io->tgt_io.is_read);
3691  }
3692 
3693  /*
3694  * Since the adapter does not handle partial data blocks and DIF,
3695  * truncate the immediate data to a block boundary.
3696  */
3697  bytes_left = (io->tgt_io.first_burst_bytes -
3698  (io->tgt_io.first_burst_bytes % blocksize));
3699 
3700  /* Update io->transferred */
3702 
3703  /*
3704  * First copy the data block into the ramdisc while calculating the
3705  * block guard (CRC). Once the data is copied, we will verify the T10_PI
3706  * data if required, insert or strip as required. If a mis-compare
3707  * occurs then this routine will send the appropriate response.
3708  */
3709  src = io->tgt_io.first_burst_buffers[buf_index].virt;
3710  src_remaining = io->tgt_io.first_burst_buffers[buf_index++].size;
3711 
3712 #if defined(OCS_INCLUDE_FC)
3713  if (io->ocs->esoc) {
3714  uint64_t lba_offset;
3715  /* adjust for HP offset */
3716  lba_offset = (lba % 32) * blocksize;
3717  src += lba_offset;
3718 
3719  }
3720 #endif
3721 
3722  while (bytes_left > 0) {
3723 
3724  dst = ocs_ramdisc_lba_address(ramdisc, lba);
3725  dst_remaining = data_blk_size;
3726  crc = 0;
3727 
3728  /* Copy one block */
3729  while (dst_remaining > 0) {
3730  n = MIN(bytes_left, MIN(dst_remaining, src_remaining));
3731  ocs_memcpy(dst, src, n);
3732  crc = ocs_scsi_dif_calc_crc(dst, n, crc);
3733  src += n;
3734  src_remaining -= n;
3735  dst += n;
3736  dst_remaining -= n;
3737  bytes_left -= n;
3738  if (src_remaining == 0) {
3739  src = io->tgt_io.first_burst_buffers[buf_index].virt;
3740  src_remaining = io->tgt_io.first_burst_buffers[buf_index++].size;
3741  }
3742  }
3743 
3744  /*
3745  * If we are verifying or stripping, we have to grab the T10 PI
3746  * data from the first burst stream.
3747  */
3748  if (dif_info->dif_oper == OCS_SCSI_DIF_OPER_PASS_THRU ||
3749  dif_info->dif_oper == OCS_SCSI_DIF_OPER_STRIP) {
3750  dst = (uint8_t*) &difdata;
3751  dst_remaining = sizeof(difdata);
3752  while (dst_remaining > 0) {
3753  n = MIN(bytes_left, MIN(dst_remaining, src_remaining));
3754  ocs_memcpy(dst, src, n);
3755  src += n;
3756  src_remaining -= n;
3757  dst += n;
3758  dst_remaining -= n;
3759  bytes_left -= n;
3760  if (src_remaining == 0) {
3761  src = io->tgt_io.first_burst_buffers[buf_index].virt;
3762  src_remaining = io->tgt_io.first_burst_buffers[buf_index++].size;
3763  }
3764  }
3765 
3766  /* verify the CRC */
3767  recv_crc = ocs_be16toh(difdata.crc);
3768 
3769  if (dif_info->check_guard && crc != recv_crc) {
3770  io_printf(io, "%s: miscompare (CRC) received 0x%x but "
3771  "expected 0x%x\n",
3772  __func__,
3773  recv_crc,
3774  crc);
3775  return ocs_ramd_check_cond(io, SNS_ILLEGAL_REQ, 0x10, 1);
3776  }
3777 
3778  /* verify the app tag */
3779  recv_apptag = ocs_be16toh(difdata.app_tag);
3780  if (dif_info->check_app_tag &&
3781  dif_info->app_tag != recv_apptag) {
3782  io_printf(io, "%s: miscompare (app_tag) received 0x%x but "
3783  "expected 0x%x\n",
3784  __func__,
3785  recv_apptag,
3786  dif_info->app_tag);
3787  return ocs_ramd_check_cond(io, SNS_ILLEGAL_REQ, 0x10, 2);
3788  }
3789 
3790  /* verify the ref tag */
3791  recv_reftag = ocs_be32toh(difdata.ref_tag);
3792  if (dif_info->check_ref_tag &&
3793  dif_info->ref_tag + blk_count != recv_reftag) {
3794  io_printf(io, "%s: miscompare (ref_tag) received 0x%x but "
3795  "expected 0x%x\n",
3796  __func__,
3797  recv_reftag,
3798  dif_info->ref_tag + blk_count);
3799  ocs_ramd_check_cond(io, SNS_ILLEGAL_REQ, 0x10, 3);
3800  return -1;
3801  }
3802  }
3803 
3804  /* build the T10 PI data for the insert case */
3805  if (dif_info->dif_oper == OCS_SCSI_DIF_OPER_INSERT) {
3806  uint32_t blkCount = dif_info->ref_tag + blk_count;
3807 
3808  difdata.crc = ocs_htobe16(crc);
3809  difdata.app_tag = ocs_htobe16(dif_info->app_tag);
3810  difdata.ref_tag = ocs_htobe32(blkCount);
3811  }
3812 
3813  /* Copy the DIF data to the ramdisc */
3814  if (dif_info->dif_oper == OCS_SCSI_DIF_OPER_PASS_THRU ||
3815  dif_info->dif_oper == OCS_SCSI_DIF_OPER_INSERT) {
3816  dif_t *dif = ocs_ramdisc_dif_address(ramdisc, lba);
3817 
3818  if (dif != NULL) {
3819  ocs_memcpy(dif, &difdata, sizeof(*dif));
3820  }
3821  }
3822  blk_count++;
3823  lba++;
3824  }
3825  io->tgt_io.acc_block_count = blk_count;
3826  return 0;
3827 }
3828 static ocs_ramd_hndlr_rtn_e
3829 ocs_ramd_init_write_dif(ocs_io_t *io, ocs_scsi_dif_info_t *dif_info, uint8_t wrprotect, uint32_t app_tag, uint32_t ref_tag)
3830 {
3831  int32_t rc;
3832  int app_tag_valid = app_tag != INVALID_APP_TAG;
3833 
3834  if (io->tgt_io.ramdisc->prot_en) {
3835  rc = ocs_scsi_dif_set_blocksize(dif_info, io->tgt_io.ramdisc->data_block_size);
3836  /* This should never happen, consider it more of an assertion */
3837  if (rc) {
3838  ocs_log_err(io->ocs, "Unsupported write DIF block size: %d\n",
3839  io->tgt_io.ramdisc->data_block_size);
3841  }
3842  }
3843 
3844  dif_info->ref_tag = ref_tag;
3845 
3846  /* SBC3 Table 101, note h:
3847  *
3848  * For wrprotect = 0
3849  * If the ATO bit is set to one in the Control mode page (see @e SPC-4)
3850  * then the device server shall write FFFFh into each LOGICAL BLOCK
3851  * APPLICATION TAG field.
3852  *
3853  * This only applies to WRPROTECT 0, but that's handled later in
3854  * ocs_scsi_convert_dif_info because it only uses this app_tag
3855  * if we're doing an INSERT operation.
3856  */
3857 
3858  dif_info->app_tag = (app_tag_valid ? app_tag : (wrprotect == 0 ? 0xffff : RAMD_APP_TAG));
3859  dif_info->dif_separate = io->tgt_io.ramdisc->dif_separate;
3860 
3861  /* Set the AT and ATRT bits */
3862  ocs_ramd_set_dif_prot_type_fields(io->ocs, io->tgt_io.ramdisc, dif_info);
3863 
3864  /* Set DIF parameters based on wrprotect
3865  * SBC says that if RWWP in Mode Page 0a is 0 (which it is) then we MAY
3866  * check the application tag. Further, except for a WRITE32, we would check
3867  * the application tag against a list tags specified on mode page 0a subpage 02,
3868  * the APPLICATION TAG MODE PAGE. Since we don't yet support that mode page the
3869  * code currently chooses not to check the app tag except on WRITE32 where the
3870  * expected tag is in the CDB. A future enhancement might be to implement that
3871  * mode page and enable checking here.
3872  */
3873 
3874  switch (wrprotect) {
3875  case 0:
3876  /* If protection is enabled insert DIF info */
3877  if (io->tgt_io.ramdisc->prot_en) {
3878  dif_info->check_app_tag = 0;
3879  dif_info->check_guard = 0;
3880  dif_info->check_ref_tag = 0;
3881  dif_info->dif_oper = OCS_SCSI_DIF_OPER_INSERT;
3882  } else {
3884  }
3885 
3886  break;
3887  case 1:
3888  /* If protection is enabled check all guard and ref */
3889  if (io->tgt_io.ramdisc->prot_en) {
3890  dif_info->check_app_tag = 0;
3891  dif_info->check_guard = 1;
3892  dif_info->check_ref_tag = 1;
3894  } else {
3895  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
3897  }
3898  break;
3899 
3900  case 2:
3901  /* If protection is enabled check only ref */
3902  if (io->tgt_io.ramdisc->prot_en) {
3903  dif_info->check_app_tag = 0;
3904  dif_info->check_guard = 0;
3905  dif_info->check_ref_tag = 1;
3907  } else {
3908  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
3910  }
3911  break;
3912  case 3:
3913  /* If protection is enabled don't check any fields */
3914  if (io->tgt_io.ramdisc->prot_en) {
3915  dif_info->check_app_tag = 0;
3916  dif_info->check_guard = 0;
3917  dif_info->check_ref_tag = 0;
3919  } else {
3920  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
3922  }
3923  break;
3924  case 4:
3925  /* If protection is enabled check only the guard field */
3926  if (io->tgt_io.ramdisc->prot_en) {
3927  dif_info->check_app_tag = 0;
3928  dif_info->check_guard = 1;
3929  dif_info->check_ref_tag = 0;
3931  } else {
3932  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
3934  }
3935  break;
3936  case 5:
3937  /* If protection is enabled check guard and ref */
3938  if (io->tgt_io.ramdisc->prot_en) {
3939  dif_info->check_app_tag = 0;
3940  dif_info->check_guard = 1;
3941  dif_info->check_ref_tag = 1;
3943  } else {
3944  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
3946  }
3947  break;
3948  default:
3949  /* Unsupported
3950  * Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB
3951  */
3953  }
3954  return 0;
3955 }
3956 
3957 /**
3958  * @ingroup scsi_cmd
3959  * @brief Common code for all SCSI WRITE commands.
3960  *
3961  * @par Description
3962  * The SCSI WRITE command is processed.
3963  *
3964  * @param io Pointer to the IO context.
3965  * @param lba Starting LBA for the transfer.
3966  * @param block_count Number of blocks to transfer.
3967  * @param wrprotect Value of the WRPROTECT field in the CDB.
3968  * @param ref_tag Value of the REFERENCE TAG to write for the first block.
3969  * @param app_tag Value of the APPLICATION TAG to write.
3970  *
3971  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
3972  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
3973  */
3974 static ocs_ramd_hndlr_rtn_e
3975 ocs_ramd_write_common(ocs_io_t *io, uint64_t lba, uint32_t block_count, uint8_t wrprotect,
3976  uint32_t app_tag, uint32_t ref_tag)
3977 {
3979 //TODO: use scsi property "max number of SGE's per SGL"
3980  ocs_scsi_dif_info_t *dif_info;
3981 
3982  /* As per SBC-3 a transfer length of 0 means no data transfer
3983  * so just return good status now.
3984  */
3985  if (block_count == 0) {
3986  return ocs_ramd_good_status(io);
3987  } else if (io->exp_xfer_len == 0) {
3988  /* mis-match between the FCP_DL and Block count in scsi CDB */
3990  }
3991 
3992  dif_info = &(io->tgt_io.dif_info);
3993 
3994  /* If protection type is 0 (ramdisc->prot_en == 0) then wrprotect must
3995  * be 0. Non-zero rdprotect should return INVALID FIELD IN CDB
3996  */
3997  if ((io->tgt_io.ramdisc->prot_en == 0) && (wrprotect != 0)) {
3998  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4000  }
4001 
4002  /* to put in the disk block, which is the ramdisc block_size */
4003  io->tgt_io.is_read = FALSE;
4004  io->tgt_io.io_lba = lba;
4005  io->tgt_io.io_block_count = block_count;
4006  io->tgt_io.acc_block_count = 0;
4007  io->tgt_io.cur_lba = 0;
4008  io->tgt_io.cur_block_count = 0;
4009 
4010  cmd_trace(io, "lba x%" PRIx64 " count x%x\n", lba, block_count);
4011 
4012  ocs_memset((void *)dif_info, 0, sizeof(ocs_scsi_dif_info_t));
4013 
4014  if ((rc = ocs_ramd_init_write_dif(io, dif_info, wrprotect, app_tag, ref_tag))) {
4015  return rc;
4016  }
4017  /* save LBA for DIF error recovery */
4018  dif_info->lba = lba;
4019 
4020  /* Process the first burst data */
4021  if (io->tgt_io.first_burst_bytes) {
4022  if (dif_info->dif_oper == OCS_SCSI_DIF_OPER_DISABLED) {
4023  rc = ocs_ramd_first_burst_no_dif(io, io->tgt_io.ramdisc);
4024  } else {
4025  rc = ocs_ramd_first_burst_with_dif(io, io->tgt_io.ramdisc, io->tgt_io.ramdisc->data_block_size);
4026  }
4027 
4028  if (rc != 0) {
4029  return SCSI_HANDLER_NOT_STARTED;
4030  }
4031 
4032  /* Check to see if the IO is complete */
4033  if (io->tgt_io.acc_block_count == io->tgt_io.io_block_count) {
4034  return ocs_ramd_good_status(io);
4035  }
4036  }
4037 
4038  if (ocs_ramd_data_phase_start(io) == 0) {
4040  } else {
4042  }
4043 
4044  return rc;
4045 }
4046 
4047 /**
4048  * @ingroup scsi_cmd
4049  * @brief Common code for all SCSI WRITE LONG commands.
4050  *
4051  * @par Description
4052  * The SCSI WRITE LONG command is processed.
4053  *
4054  * @param io Pointer to the IO context.
4055  * @param lba Starting LBA for the transfer.
4056  * @param xfer_len Transfer length, in bytes.
4057  * @param cor_dis Correction disabled bit from the CDB.
4058  * @param wr_uncor Write uncorrectable bit from the CDB.
4059  * @param pblock Physical block bit from the CDB.
4060  *
4061  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4062  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4063  */
4064 static ocs_ramd_hndlr_rtn_e
4065 ocs_ramd_write_long_common(ocs_io_t *io, uint64_t lba, uint16_t xfer_len,
4066  uint8_t cor_dis, uint8_t wr_uncor, uint8_t pblock)
4067 {
4069  ocs_scsi_dif_info_t *dif_info;
4070 
4071  if (pblock == 1) {
4073  }
4074 
4075  if (wr_uncor == 1 || cor_dis == 1) {
4076  /* just send response */
4077  return ocs_ramd_good_status(io);
4078  }
4079 
4080  /* to put in the disk block, which is the ramdisc block_size */
4081  io->tgt_io.is_read = FALSE;
4082  io->tgt_io.io_lba = lba;
4083  io->tgt_io.io_block_count = xfer_len / io->tgt_io.ramdisc->data_block_size;
4084 
4085  cmd_trace(io, "lba x%" PRIx64 "\n", lba);
4086 
4087  dif_info = &(io->tgt_io.dif_info);
4088  memset((void *)dif_info, 0, sizeof(ocs_scsi_dif_info_t));
4090 
4091  /* Process the first burst data */
4092  if (io->tgt_io.first_burst_bytes) {
4093  if (ocs_ramd_first_burst_no_dif(io, io->tgt_io.ramdisc) != 0) {
4094  return SCSI_HANDLER_NOT_STARTED;
4095  }
4096 
4097  /* Check to see if the IO is complete */
4098  if (io->tgt_io.acc_block_count == io->tgt_io.io_block_count) {
4099  return ocs_ramd_good_status(io);
4100  }
4101  }
4102 
4103  io->tgt_io.sgl_phys = 1;
4104 
4105  if (ocs_ramd_data_phase_start(io) == 0) {
4107  } else {
4109  }
4110 
4111  return rc;
4112 }
4113 
4114 
4115 /**
4116  * @ingroup scsi_cmd
4117  * @brief Common code for all SCSI WRITE SAME commands.
4118  *
4119  * @par Description
4120  * The SCSI WRITE SAME command is processed.
4121  *
4122  * @param io Pointer to the IO context.
4123  * @param lba Starting LBA for the transfer.
4124  * @param block_count Number of blocks to be written to the media.
4125  * @param wrprotect WRPROTECT value from the CDB.
4126  * @param pbdata PBDATA bit from the CDB.
4127  * @param lbdata LBDATA bit from the CDB.
4128  * @param ref_tag Value of the REFERENCE TAG to write for the first block.
4129  * @param app_tag Value of the APPLICATION TAG to write.
4130  *
4131  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4132  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4133  */
4134 static ocs_ramd_hndlr_rtn_e
4135 ocs_ramd_write_same_common(ocs_io_t *io, uint64_t lba, uint32_t block_count,
4136  uint8_t wrprotect, uint8_t pbdata, uint8_t lbdata,
4137  uint32_t app_tag, uint32_t ref_tag)
4138 {
4140  ocs_scsi_dif_info_t *dif_info;
4141 
4142  api_trace();
4143 
4144  /* If protection type is 0 (ramdisc->prot_en == 0) then wrprotect must
4145  * be 0. Non-zero rdprotect should return INVALID FIELD IN CDB
4146  */
4147  if ((io->tgt_io.ramdisc->prot_en == 0) && (wrprotect != 0)) {
4148  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4150  }
4151 
4152  /* Save wrprotect, pbdata, lbdata, and block_count in the io. We'll need them later */
4153  io->tgt_io.wrprotect = wrprotect;
4154  io->tgt_io.pbdata = pbdata;
4155  io->tgt_io.lbdata = lbdata;
4156  io->tgt_io.num_blocks = block_count;
4157 
4158  /* Validate the CDB parameters */
4159  if ((pbdata == 1) && (lbdata == 1)) {
4161  }
4162 
4163  /* Make sure lba plus block_count doesn't fall off the end of the disk */
4164  if ((lba + block_count) > io->tgt_io.ramdisc->block_count) {
4166  }
4167 //TODO: use normal write path(factoring is needed)
4168 
4169  /* The following is similar to (and copied from) ocs_ramd_write_common.
4170  * The difference is that we want to do a single block of I/O regardless
4171  * of the block count. Then after that block of I/O has completed we'll
4172  * copy the data, possibly with modifications, into other blocks.
4173  */
4174 
4175  dif_info = &(io->tgt_io.dif_info);
4176 
4177  /* Change block_count to 1. From here on down block_count is the number of blocks on the wire */
4178  block_count = 1;
4179 
4180  /* to put in the disk block, which is the ramdisc block_size */
4181  io->tgt_io.is_read = FALSE;
4182  io->tgt_io.io_lba = lba;
4183  io->tgt_io.io_block_count = block_count;
4184 
4185  cmd_trace(io, "lba x%" PRIx64 " count x%x\n", lba, block_count);
4186 
4187  memset((void *)dif_info, 0, sizeof(ocs_scsi_dif_info_t));
4188 
4189  if ((rc = ocs_ramd_init_write_dif(io, dif_info, wrprotect, app_tag, ref_tag))) {
4190  return rc;
4191  }
4192 
4193  /* Process the first burst data */
4194  if (io->tgt_io.first_burst_bytes) {
4195  if (dif_info->dif_oper == OCS_SCSI_DIF_OPER_DISABLED) {
4196  rc = ocs_ramd_first_burst_no_dif(io, io->tgt_io.ramdisc);
4197  } else {
4198  rc = ocs_ramd_first_burst_with_dif(io, io->tgt_io.ramdisc, io->tgt_io.ramdisc->data_block_size);
4199  }
4200 
4201  if (rc != 0) {
4202  return SCSI_HANDLER_NOT_STARTED;
4203  }
4204 
4205  /* Check to see if the IO is complete */
4206  if (io->tgt_io.acc_block_count == io->tgt_io.io_block_count) {
4207  return ocs_ramd_complete_write_same(io);
4208  }
4209  }
4210 
4211  /* We don't want auto response because we need time to process the
4212  * command after the data phase completes.
4213  */
4214  io->tgt_io.supress_auto_response = 1;
4215 
4216  if (ocs_ramd_data_phase_start(io) == 0) {
4218  } else {
4220  }
4221 
4222 
4223 
4224  return rc;
4225 }
4226 
4227 /**
4228  * @ingroup scsi_cmd
4229  * @brief Command completion for the SCSI WRITE SAME command.
4230  *
4231  * @par Description
4232  * This function is called when the data phase for the WRITE SAME command has
4233  * completed. At this point, a single block has been transfered to the target
4234  * and written to the ramdisc. The remaining actions involve copying that
4235  * block, possibly with modifications, to other blocks on the ramdisc.<br><br>
4236  * @b Note: This function must call either ocs_ramd_check_cond or ocs_ramd_status_good.
4237  *
4238  * @param io Pointer to the IO context.
4239  *
4240  * @return Returns SCSI_HANDLER_NOT_STARTED if an error occurs, or
4241  * SCSI_HANDLER_RESP_STARTED if a response (either STATUS GOOD or
4242  * CHECK CONDITION) has been sent.
4243  */
4244 static ocs_ramd_hndlr_rtn_e
4246 {
4247  uint64_t start_lba = io->tgt_io.io_lba;
4248  uint32_t block_count = io->tgt_io.num_blocks;
4249  uint32_t i;
4250  uint8_t *src;
4251  uint8_t *dest;
4252  uint32_t ref_tag;
4253  uint64_t phys_addr;
4254  dif_t *src_dif;
4255  dif_t *dst_dif;
4256 
4257  api_trace();
4258 
4259  /* If the block count is zero that means "the rest of the disc" */
4260  if (block_count == 0) {
4261  block_count = io->tgt_io.ramdisc->block_count - start_lba;
4262  }
4263 
4264  src = ocs_ramdisc_lba_address(io->tgt_io.ramdisc, start_lba);
4265 
4266  /* Handle the three possible values for pbdata and lbdata (they can't both be 1) */
4267  if ((io->tgt_io.lbdata == 0) && (io->tgt_io.pbdata == 0)) {
4268  if (io->tgt_io.ramdisc->prot_en == 0) {
4269  /* Protection type is 0. Just copy the one block to all the others */
4270  for (i=1; i<block_count; i++) {
4271  dest = ocs_ramdisc_lba_address(io->tgt_io.ramdisc, start_lba + i);
4272  ocs_memcpy(dest, src, io->tgt_io.ramdisc->phys_block_size);
4273  }
4274  } else {
4275  src_dif = ocs_ramdisc_dif_address(io->tgt_io.ramdisc, start_lba);
4276  ref_tag = ocs_be32toh(src_dif->ref_tag);
4277  src_dif->crc = ocs_htobe16(ocs_scsi_dif_calc_crc(src, io->tgt_io.ramdisc->data_block_size, 0));
4278 
4279  /* Start with block 1 */
4280  for (i=1; i<block_count; i++) {
4281  dest = ocs_ramdisc_lba_address(io->tgt_io.ramdisc, start_lba + i);
4282  ocs_memcpy(dest, src, io->tgt_io.ramdisc->phys_block_size);
4283 
4284  dst_dif = ocs_ramdisc_dif_address(io->tgt_io.ramdisc, start_lba + i);
4285 
4286  dst_dif->crc = src_dif->crc;
4287  dst_dif->app_tag = src_dif->app_tag;
4288  dst_dif->ref_tag = ocs_htobe32(ref_tag + i);
4289  }
4290  }
4291 
4292  } else if ((io->tgt_io.lbdata == 0) && (io->tgt_io.pbdata == 1)) {
4293  /* Copy the block to all the others, replacing the first 8
4294  * bytes of each block with the physical sector address.
4295  * Since we don't have cylinders, heads, and sectors this
4296  * will be the same as the LBA
4297  */
4298  for (i=0; i<block_count; i++) {
4299  dest = ocs_ramdisc_lba_address(io->tgt_io.ramdisc, start_lba + i);
4300  ocs_memcpy(dest, src, io->tgt_io.ramdisc->phys_block_size);
4301  phys_addr = ocs_htobe64(start_lba + i);
4302  ocs_memcpy(dest, &phys_addr, 8);
4303  }
4304 
4305  } else if ((io->tgt_io.lbdata == 1) && (io->tgt_io.pbdata == 0)) {
4306  /* Copy the block to all the others, replacing the first 4 bytes
4307  * with the LBA of the block
4308  */
4309  for (i=0; i<block_count; i++) {
4310  dest = ocs_ramdisc_lba_address(io->tgt_io.ramdisc, start_lba + i);
4311  ocs_memcpy(dest, src, io->tgt_io.ramdisc->phys_block_size);
4312  ref_tag = ocs_htobe32(start_lba + i);
4313  ocs_memcpy(dest, &ref_tag, 4);
4314  }
4315 
4316  } else {
4318  }
4319 
4320 
4321  return ocs_ramd_good_status(io);
4322 
4323 }
4324 
4325 /**
4326  * @ingroup scsi_cmd
4327  * @brief Process the SCSI READ (6) command.
4328  *
4329  * @par Description
4330  * The SCSI READ (6) command is processed.
4331  *
4332  * @param io Pointer to the IO context.
4333  * @param cdb Pointer to the SCSI CDB.
4334  *
4335  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4336  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4337  */
4338 static ocs_ramd_hndlr_rtn_e
4339 ocs_ramd_read6(ocs_io_t *io, uint8_t cdb[])
4340 {
4341  uint64_t lba;
4342  uint32_t block_count;
4343  uint8_t rdprotect;
4344 
4345  ocs_assert(cdb[0] == SCSI_READ6, -1);
4346 
4347  lba = ((cdb[1] & 0x1f) << 16) |
4348  (cdb[2] << 8) |
4349  (cdb[3] << 0);
4350  block_count = cdb[4];
4351 
4352  /* SBC-3 states that a transfer length of 0
4353  * specifies that 256 logical blocks shall be read.
4354  */
4355  if (block_count == 0){
4356  block_count = 256;
4357  }
4358 
4359 
4360  /* BZ 208104: As per SBC-3 rev 22, Table 50, the protection information is never
4361  * transmitted on the wire, hence always set rdprotect to 0 . The following revisions
4362  * of SBC-3 completed deprecated this command.
4363  */
4364  rdprotect = 0;
4365 
4366  return ocs_ramd_read_common(io, lba, block_count, rdprotect, INVALID_APP_TAG, lba & 0xffffffff);
4367 }
4368 
4369 /**
4370  * @ingroup scsi_cmd
4371  * @brief Process the SCSI READ (10) command.
4372  *
4373  * @par Description
4374  * The SCSI READ (10) command is processed.
4375  *
4376  * @param io Pointer to the IO context.
4377  * @param cdb Pointer to the SCSI CDB.
4378  *
4379  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4380  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4381  */
4382 static ocs_ramd_hndlr_rtn_e
4383 ocs_ramd_read10(ocs_io_t *io, uint8_t cdb[])
4384 {
4385  uint64_t lba;
4386  uint32_t block_count;
4387  uint8_t rdprotect;
4388 
4389  ocs_assert(cdb[0] == SCSI_READ10, -1);
4390 
4391  lba = ((uint32_t)(cdb[2] << 24)) |
4392  ((uint32_t)(cdb[3] << 16)) |
4393  ((uint32_t)(cdb[4] << 8)) |
4394  ((uint32_t)(cdb[5] << 0));
4395  block_count =
4396  (cdb[7] << 8) |
4397  (cdb[8] << 0);
4398  rdprotect = ((cdb[1] & 0xe0) >> 5);
4399 
4400  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (rdprotect != 0)) {
4401  /* Read10 not supported with prot type 2 and non-zero rdprotect */
4403  }
4404 
4405  return ocs_ramd_read_common(io, lba, block_count, rdprotect, INVALID_APP_TAG, lba & 0xffffffff);
4406 }
4407 
4408 /**
4409  * @ingroup scsi_cmd
4410  * @brief Process the SCSI READ (12) command.
4411  *
4412  * @par Description
4413  * The SCSI READ (12) command is processed.
4414  *
4415  * @param io Pointer to the IO context.
4416  * @param cdb Pointer to the SCSI CDB.
4417  *
4418  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4419  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4420  */
4421 static ocs_ramd_hndlr_rtn_e
4422 ocs_ramd_read12(ocs_io_t *io, uint8_t cdb[])
4423 {
4424  uint64_t lba;
4425  uint32_t block_count;
4426  uint8_t rdprotect;
4427 
4428  ocs_assert(cdb[0] == SCSI_READ12, -1);
4429 
4430  lba = (cdb[2] << 24) |
4431  (cdb[3] << 16) |
4432  (cdb[4] << 8) |
4433  (cdb[5] << 0);
4434  block_count = ((uint32_t)(cdb[6] << 24)) |
4435  ((uint32_t)(cdb[7] << 16)) |
4436  ((uint32_t)(cdb[8] << 8)) |
4437  ((uint32_t)(cdb[9] << 0));
4438  rdprotect = ((cdb[1] & 0xe0) >> 5);
4439 
4440  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (rdprotect != 0)) {
4441  /* Read12 not supported with prot type 2 and non-zero rdprotect */
4443  }
4444 
4445  return ocs_ramd_read_common(io, lba, block_count, rdprotect, INVALID_APP_TAG, lba & 0xffffffff);
4446 }
4447 
4448 /**
4449  * @ingroup scsi_cmd
4450  * @brief Process the SCSI READ (16) command.
4451  *
4452  * @par Description
4453  * The SCSI READ (16) command is processed.
4454  *
4455  * @param io Pointer to the IO context.
4456  * @param cdb Pointer to the SCSI CDB.
4457  *
4458  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4459  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4460  */
4461 
4462 static ocs_ramd_hndlr_rtn_e ocs_ramd_read16(ocs_io_t *io, uint8_t cdb[])
4463 {
4464  uint64_t lba;
4465  uint32_t block_count;
4466  uint8_t rdprotect;
4467 
4468  ocs_assert(cdb[0] == SCSI_READ16, -1);
4469 
4470  lba = ((uint64_t)cdb[2] << 56) |
4471  ((uint64_t)cdb[3] << 48) |
4472  ((uint64_t)cdb[4] << 40) |
4473  ((uint64_t)cdb[5] << 32) |
4474  ((uint64_t)cdb[6] << 24) |
4475  ((uint64_t)cdb[7] << 16) |
4476  ((uint64_t)cdb[8] << 8) |
4477  ((uint64_t)cdb[9] << 0);
4478  block_count = (cdb[10] << 24) |
4479  (cdb[11] << 16) |
4480  (cdb[12] << 8) |
4481  (cdb[13] << 0);
4482  rdprotect = ((cdb[1] & 0xe0) >> 5);
4483 
4484  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type == 2) && (rdprotect != 0)) {
4485  /* Read16 not supported with prot type 2 and non-zero rdprotect */
4487  }
4488 
4489  return ocs_ramd_read_common(io, lba, block_count, rdprotect, INVALID_APP_TAG, lba & 0xffffffff);
4490 }
4491 
4492 /**
4493  * @ingroup scsi_cmd
4494  * @brief Process the SCSI READ (32) command.
4495  *
4496  * @par Description
4497  * The SCSI READ (32) command is processed.
4498  *
4499  * @param io Pointer to the IO context.
4500  * @param cdb Pointer to the SCSI CDB.
4501  *
4502  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4503  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4504  */
4505 static ocs_ramd_hndlr_rtn_e
4506 ocs_ramd_read32(ocs_io_t *io, uint8_t cdb[])
4507 {
4508  uint64_t lba;
4509  uint32_t block_count;
4510  uint8_t rdprotect;
4511  uint32_t ref_tag;
4512  uint32_t app_tag;
4513 
4514  ocs_assert(cdb[0] == SCSI_VARIABLE_LEN_CDB, -1);
4515  ocs_assert(cdb[9] == SCSI_VAR_CMD_SVC_ACT_READ32, -1);
4516 
4517  lba = ((uint64_t)cdb[12] << 56) |
4518  ((uint64_t)cdb[13] << 48) |
4519  ((uint64_t)cdb[14] << 40) |
4520  ((uint64_t)cdb[15] << 32) |
4521  ((uint64_t)cdb[16] << 24) |
4522  ((uint64_t)cdb[17] << 16) |
4523  ((uint64_t)cdb[18] << 8) |
4524  ((uint64_t)cdb[19] << 0);
4525  block_count = (cdb[28] << 24) |
4526  (cdb[29] << 16) |
4527  (cdb[30] << 8) |
4528  (cdb[31] << 0);
4529  rdprotect = ((cdb[10] & 0xe0) >> 5);
4530  ref_tag = (cdb[20] << 24) |
4531  (cdb[21] << 16) |
4532  (cdb[22] << 8) |
4533  (cdb[23] << 0);
4534  app_tag = (cdb[24] << 8) |
4535  (cdb[25] << 0);
4536 
4537  if (io->tgt_io.ramdisc->prot_en == 0) {
4538  if (rdprotect != 0) {
4539  /* SBC 4.21.2.2: If type 0 protection is enabled and the rdprotect field is */
4540  /* non-zero ... ILLEGAL REQUEST / INVALID FIELD IN CDB */
4542  } else {
4543  /* If type 0 protection is enabled and the rdprotect field is set to a zero
4544  * value ... ILLEGAL REQUEST / INVALID COMMAND OPERATION CODE
4545  */
4547  }
4548  }
4549 
4550  if ((io->tgt_io.ramdisc->prot_en == 1) && (io->tgt_io.ramdisc->p_type != 2)) {
4551  /* SBC 4.21.2.3: If type 1 protection is enabled, then the following medum commands
4552  * are invalid ... ILLEGAL_REQUEST / INVALID_COMMAND_OPERATION_CODE
4553  */
4555  }
4556 
4557  return ocs_ramd_read_common(io, lba, block_count, rdprotect, app_tag, ref_tag);
4558 }
4559 
4560 /**
4561  * @ingroup scsi_cmd
4562  * @brief Process the SCSI READ LONG (10) command.
4563  *
4564  * @par Description
4565  * The SCSI READ LONG (10) command is processed.
4566  *
4567  * @param io Pointer to the IO context.
4568  * @param cdb Pointer to the SCSI CDB.
4569  *
4570  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4571  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4572  */
4573 static ocs_ramd_hndlr_rtn_e
4574 ocs_ramd_read_long10(ocs_io_t *io, uint8_t cdb[])
4575 {
4576  uint64_t lba;
4577  uint16_t xfer_len;
4578  uint8_t pblock, corrct;
4579 
4580  ocs_assert(cdb[0] == SCSI_READ_LONG10, -1);
4581 
4582  pblock = (cdb[1] >> 2) & 1;
4583  corrct = (cdb[1] >> 1) & 1;
4584 
4585  lba = ((uint32_t)(cdb[2] << 24)) |
4586  ((uint32_t)(cdb[3] << 16)) |
4587  ((uint32_t)(cdb[4] << 8)) |
4588  ((uint32_t)(cdb[5] << 0));
4589 
4590  xfer_len =
4591  (cdb[7] << 8) |
4592  (cdb[8] << 0);
4593 
4594  /* As per SBC-3 a transfer length of 0 means no data transfer
4595  * so just return good status now.
4596  */
4597  if (xfer_len == 0) {
4598  return(ocs_ramd_good_status(io));
4599  }
4600 
4601  return ocs_ramd_read_long_common(io, lba, xfer_len, pblock, corrct);
4602 }
4603 
4604 /**
4605  * @ingroup scsi_cmd
4606  * @brief Process the SCSI READ LONG (16) command.
4607  *
4608  * @par Description
4609  * The SCSI READ LONG (16) command is processed.
4610  *
4611  * @param io Pointer to the IO context.
4612  * @param cdb Pointer to the SCSI CDB.
4613  *
4614  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4615  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4616  */
4617 static ocs_ramd_hndlr_rtn_e
4618 ocs_ramd_read_long16(ocs_io_t *io, uint8_t cdb[])
4619 {
4620  uint64_t lba;
4621  uint16_t xfer_len;
4622  uint8_t pblock, corrct;
4623 
4624  ocs_assert(cdb[0] == SCSI_SERVICE_ACTION_IN16, -1);
4625  ocs_assert(((cdb[1] & 0x1F) == SCSI_SVC_ACTION16_READ_LONG), -1);
4626 
4627  lba = ((uint64_t)cdb[2] << 56) |
4628  ((uint64_t)cdb[3] << 48) |
4629  ((uint64_t)cdb[4] << 40) |
4630  ((uint64_t)cdb[5] << 32) |
4631  ((uint64_t)cdb[6] << 24) |
4632  ((uint64_t)cdb[7] << 16) |
4633  ((uint64_t)cdb[8] << 8) |
4634  ((uint64_t)cdb[9] << 0);
4635 
4636  xfer_len =
4637  (cdb[12] << 8) |
4638  (cdb[13] << 0);
4639 
4640  /* As per SBC-3 a transfer length of 0 means no data transfer
4641  * so just return good status now.
4642  */
4643  if (xfer_len == 0) {
4644  return(ocs_ramd_good_status(io));
4645  }
4646 
4647  pblock = (cdb[14] >> 1) & 1;
4648  corrct = cdb[14] & 1;
4649 
4650  return ocs_ramd_read_long_common(io, lba, xfer_len, pblock, corrct);
4651 }
4652 
4653 /**
4654  * @ingroup scsi_cmd
4655  * @brief Common code to start a SCSI data phase once the IO has been set up.
4656  *
4657  * @param io Pointer to the IO context.
4658  *
4659  * @return Returns 0 on success, or a negative error code value on failure.
4660  */
4661 static int32_t
4663 {
4664  ocs_t *ocs = io->ocs;
4665  uint32_t blocksize;
4666  ocs_scsi_dif_info_t *dif_info = &io->tgt_io.dif_info;
4667  uint32_t rc;
4668  uint64_t lba;
4669  uint32_t max_sgl;
4670  uint32_t flags = 0;
4671  uint32_t sgl_flags = 0;
4672  uint64_t xfer_len;
4673  int sgl_status;
4674 
4675  max_sgl = MIN(ocs->tgt_ocs.max_sgl, io->sgl_allocated);
4676 
4677  if (dif_info->dif_oper == OCS_SCSI_DIF_OPER_DISABLED) {
4678  dif_info = NULL;
4679  blocksize = io->tgt_io.ramdisc->phys_block_size;
4680  } else {
4681  blocksize = ocs_scsi_dif_wire_blocksize(dif_info, !io->tgt_io.is_read);
4682  }
4683 
4684  if (io->tgt_io.sgl_phys) {
4685  sgl_flags |= SGL_REQ_FLAGS_PHYS;
4686  }
4687 
4688  lba = io->tgt_io.io_lba + io->tgt_io.acc_block_count;
4689 
4690  /*
4691  * Calculate the remaining blocks in the IO. This is passed to the SGL
4692  * building function to indicate how many blocks are requested.
4693  *
4694  * We need to adjust the value to not exceed the max transfer size
4695  * requested by the transport for this node.
4696  */
4697  io->tgt_io.cur_block_count = io->tgt_io.io_block_count - io->tgt_io.acc_block_count;
4698  if (!io->tgt_io.is_read &&
4699  (io->tgt_io.cur_block_count * blocksize) > io->node->max_wr_xfer_size) {
4700  io->tgt_io.cur_block_count = io->node->max_wr_xfer_size / blocksize;
4701  }
4702 
4703  /* build an SGL */
4704  sgl_status = ocs_ramdisc_sgl(io->ocs, io->tgt_io.ramdisc, (ocs_ramdisc_sgl_t*) io->sgl, max_sgl,
4705  ocs->tgt_ocs.max_sge,
4706  lba, &io->tgt_io.cur_block_count, sgl_flags, ENABLE_BUFFER_DUMP);
4707  if (sgl_status < 0) {
4708  ocs_log_err(io->ocs, "Failed building SGLs\n");
4709  return sgl_status;
4710  }
4711  io->sgl_count = sgl_status;
4712 
4713  if (dif_info != NULL) {
4714  /*
4715  * Note: This calculation assumes that the values returned by
4716  * ocs_ramdisc_sgl() are block aligned.
4717  */
4718 //TODO: need something here
4719  }
4720 
4721  if (ocs->err_injection != INJECT_DROP_RESP &&
4722  (io->tgt_io.io_block_count == (io->tgt_io.acc_block_count + io->tgt_io.cur_block_count))) {
4723  flags |= OCS_SCSI_LAST_DATAPHASE;
4724  }
4725 
4726  if (io->tgt_io.supress_auto_response) {
4727  flags |= OCS_SCSI_NO_AUTO_RESPONSE;
4728  }
4729 
4730  xfer_len = (io->tgt_io.cur_block_count * (uint64_t)blocksize);
4731 
4732  if (io->tgt_io.is_read) {
4733 #if ENABLE_DIF_VERIFICATION == 1
4734  if (ocs_ramdisc_dif_verify(ocs, io->tgt_io.ramdisc, lba, io->tgt_io.cur_block_count) != 0) {
4735  ocs_log_test(io->ocs, "Read DIF IO at LBA 0x%" PRIx64
4736  " for %d blocks with bad data in RAM\n",
4737  lba, io->tgt_io.cur_block_count);
4738  }
4739 #endif
4740  rc = ocs_scsi_send_rd_data(io, flags, dif_info, io->sgl, io->sgl_count,
4741  xfer_len, scsi_dataphase_cb, NULL);
4742  } else {
4743  rc = ocs_scsi_recv_wr_data(io, flags, dif_info, io->sgl, io->sgl_count,
4744  xfer_len, scsi_dataphase_cb, NULL);
4745  }
4746 
4747  return rc;
4748 }
4749 
4750 /**
4751  * @ingroup scsi_cmd
4752  * @brief Common code used for all SCSI READ commands.
4753  *
4754  * @par Description
4755  * The SCSI READ command is processed.
4756  *
4757  * @param io Pointer to the IO context.
4758  * @param lba Starting LBA for the transfer.
4759  * @param block_count Number of blocks to transfer.
4760  * @param rdprotect Value of the RDPROTECT field in the CDB.
4761  * @param app_tag Value of the APPLICATION TAG to expect.
4762  * @param ref_tag Value of the REFERENCE TAG to expect for the first block.
4763  *
4764  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4765  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4766  */
4767 static ocs_ramd_hndlr_rtn_e
4768 ocs_ramd_read_common(ocs_io_t *io, uint64_t lba, uint32_t block_count, uint8_t rdprotect,
4769  uint32_t app_tag, uint32_t ref_tag)
4770 {
4771  int32_t rc;
4772  ocs_scsi_dif_info_t *dif_info;
4773  int app_tag_valid = (app_tag != INVALID_APP_TAG);
4774 
4775  /* As per SBC-3 a transfer length of 0 means no data transfer
4776  * so just return good status now.
4777  */
4778  if (block_count == 0) {
4779  return(ocs_ramd_good_status(io));
4780  } else if (io->exp_xfer_len == 0) {
4781  /* mis-match between the FCP_DL and Block count in scsi CDB */
4783  }
4784 
4785  dif_info = &(io->tgt_io.dif_info);
4786 
4787  /* If protection type is 0 (ramdisc->prot_en == 0) then rdprotect must
4788  * be 0. Non-zero rdprotect should return INVALID FIELD IN CDB
4789  */
4790  if ((io->tgt_io.ramdisc->prot_en == 0) && (rdprotect != 0)) {
4791  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4793  }
4794 
4795  io->tgt_io.io_lba = lba;
4796  io->tgt_io.io_block_count = block_count;
4797  io->tgt_io.acc_block_count = 0;
4798  io->tgt_io.cur_lba = 0;
4799  io->tgt_io.cur_block_count = 0;
4800 
4801  io->tgt_io.is_read = TRUE;
4802 
4803  cmd_trace(io, "lba x%" PRIx64 " count x%x io_len x%x\n", lba, block_count,
4804  io->tgt_io.io_block_count * io->tgt_io.ramdisc->phys_block_size);
4805 
4806  ocs_memset(dif_info, 0, sizeof(ocs_scsi_dif_info_t));
4807 
4808  dif_info->ref_tag = ref_tag;
4809  dif_info->app_tag = (app_tag == INVALID_APP_TAG) ? RAMD_APP_TAG : app_tag;
4810 
4811  dif_info->dif_separate = io->tgt_io.ramdisc->dif_separate;
4812 
4813  /* Set the AT and ATRT bits */
4814  ocs_ramd_set_dif_prot_type_fields(io->ocs, io->tgt_io.ramdisc, dif_info);
4815 
4816  /* save LBA for DIF error recovery */
4817  dif_info->lba = lba;
4818 
4819  /* Unsupported DIF blocksize */
4820  rc = ocs_scsi_dif_set_blocksize(dif_info, io->tgt_io.ramdisc->data_block_size);
4821  if (rc) {
4822  ocs_log_test(io->ocs, "Unsupported DIF read block size: %d\n",
4823  io->tgt_io.ramdisc->data_block_size);
4825  }
4826 
4827  switch (rdprotect) {
4828  case 0:
4829  /* If protection is enabled check all three fields, strip the PI info */
4830  if (io->tgt_io.ramdisc->prot_en) {
4831  dif_info->check_app_tag = app_tag_valid;
4832  dif_info->check_guard = 1;
4833  dif_info->check_ref_tag = 1;
4834  dif_info->dif_oper = OCS_SCSI_DIF_OPER_STRIP;
4835  } else {
4837  }
4838 
4839  break;
4840  case 1:
4841  case 5:
4842  /* Codes 001b and 101b are identical on read
4843  * If protection is enabled check all three fields, don't strip
4844  */
4845  if (io->tgt_io.ramdisc->prot_en) {
4846  dif_info->check_app_tag = app_tag_valid;
4847  dif_info->check_guard = 1;
4848  dif_info->check_ref_tag = 1;
4850  } else {
4851  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4853  }
4854  break;
4855  case 2:
4856  /* If protection is enabled check only app and ref, don't strip */
4857  if (io->tgt_io.ramdisc->prot_en) {
4858  dif_info->check_app_tag = app_tag_valid;
4859  dif_info->check_guard = 0;
4860  dif_info->check_ref_tag = 1;
4862  } else {
4863  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4865  }
4866  break;
4867  case 3:
4868  /* If protection is enabled don't check any fields, don't strip */
4869  if (io->tgt_io.ramdisc->prot_en) {
4870  dif_info->check_app_tag = 0;
4871  dif_info->check_guard = 0;
4872  dif_info->check_ref_tag = 0;
4874  } else {
4875  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4877  }
4878  break;
4879  case 4:
4880  /* If protection is enabled check only the guard field, don't strip */
4881  if (io->tgt_io.ramdisc->prot_en) {
4882  dif_info->check_app_tag = 0;
4883  dif_info->check_guard = 1;
4884  dif_info->check_ref_tag = 0;
4886  } else {
4887  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4889  }
4890  break;
4891  default:
4892  /* Unsupported */
4893  /* Check condition, ILLEGAL REQUEST, INVALID FIELD IN CDB */
4895  }
4896 
4897  rc = ocs_ramd_data_phase_start(io);
4898  if (rc == -1) {
4900  }
4901 
4902  if (rc == 0) {
4904  }
4905 
4906  return SCSI_HANDLER_NOT_STARTED;
4907 }
4908 
4909 /**
4910  * @ingroup scsi_cmd
4911  * @brief Common code used for all SCSI READ commands.
4912  *
4913  * @par Description
4914  * The SCSI READ command is processed.
4915  *
4916  * @param io Pointer to the IO context.
4917  * @param lba Starting LBA for the transfer.
4918  * @param xfer_len Transfer length, in bytes.
4919  * @param pblock Physical block bit from the CDB.
4920  * @param corrct Correction bit from the CDB.
4921  *
4922  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
4923  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
4924  */
4925 static ocs_ramd_hndlr_rtn_e
4926 ocs_ramd_read_long_common(ocs_io_t *io, uint64_t lba, uint16_t xfer_len, uint8_t pblock, uint8_t corrct)
4927 {
4928  int32_t rc;
4929  ocs_scsi_dif_info_t *dif_info;
4930 
4931  io->tgt_io.is_read = TRUE;
4932  io->tgt_io.io_lba = lba;
4933  io->tgt_io.io_block_count = xfer_len / io->tgt_io.ramdisc->phys_block_size;
4934 
4935  cmd_trace(io, "lba x%" PRIx64 " io_len x%x\n", lba, io->tgt_io.io_block_count *
4936  io->tgt_io.ramdisc->phys_block_size);
4937 
4938  dif_info = &(io->tgt_io.dif_info);
4939  ocs_memset((void *)dif_info, 0, sizeof(ocs_scsi_dif_info_t));
4941 
4942  io->tgt_io.sgl_phys = 1;
4943 
4944  rc = ocs_ramd_data_phase_start(io);
4945  if (rc == -1) {
4946  /* Attempt to access off the end of the device */
4948  }
4949 
4950  if (rc == 0) {
4952  }
4953 
4954  return SCSI_HANDLER_NOT_STARTED;
4955 }
4956 
4957 /**
4958  * @ingroup scsi_cmd
4959  * @brief Validate a FORMAT UNIT parameter list.
4960  *
4961  * @par Description
4962  * Called after a FORMAT UNIT data out phase has completed. This function
4963  * validates the contents of the parameter list. If the parameter list is
4964  * valid, the function takes the appropriate action and returns 0. If the parameter
4965  * list is not valid, the function returns a sense key, code, and qualifier to be returned
4966  * to the initiator in a CHECK CONDITION.
4967  *
4968  * @param io Pointer to the IO context.
4969  *
4970  * @return Returns 0 for success; or a non-zero sense key, code, and qualifier for failure.
4971  */
4972 
4973 static uint32_t
4975  uint8_t fmtpinfo;
4976  uint8_t protection_field_usage;
4977  uint8_t *payload;
4978  uint8_t p_type = 0;
4979  uint8_t prot_en = 0;
4980  uint8_t dif_separate;
4981 
4982  api_trace();
4983 
4984  /*payload_length = io->tgt_io.mem_io_len; */
4985 
4986  fmtpinfo = io->tgt_io.fmtpinfo;
4987 
4988  payload = io->tgt_io.payload.virt;
4989 
4990  protection_field_usage = payload[0] & 0x07;
4991 
4992  dif_separate = (payload[1] & 1) != 0;
4993 
4994  /*if (payload_length == 4) {
4995  * protection_interval_exponent = 0;
4996  *} else {
4997  * protection_interval_exponent = payload[3] & 0x0f;
4998  *}
4999  */
5000 
5001  /* Validate the long list of SPT / PROTECT / FMTPINFO / PROTECTION FIELD USAGE
5002  * combinations per table 34 in SBC
5003  */
5004 
5005  if (protection_field_usage != 0) {
5006  /* protection field usage must be 0 for the types we support (0, 1, and 2) */
5008  }
5009 
5010  switch (fmtpinfo) {
5011  case 0:
5012  /* Type 0 protection */
5013  p_type = 0;
5014  prot_en = 0;
5015  break;
5016  case 1:
5017  /* fmtpinfo 1 is illegal */
5019  break;
5020  case 2:
5021  /* Type 1 protection */
5022  p_type = 1;
5023  prot_en = 1;
5024  break;
5025  case 3:
5026  /* Type 2 protection */
5027  p_type = 2;
5028  prot_en = 1;
5029  break;
5030  default:
5031  /* This shouldn't happen - FMTPINFO is 2 bits */
5032  ocs_assert(0, -1);
5033  }
5034 
5035  /* If protection is enabled make sure the block size is one that's supported
5036  * in the DISEED SGE
5037  */
5038  if (prot_en) {
5039  /* Validate the requested block size. Values supported by SLI4 in the DISEED SGE are
5040  * 512, 520, 1024, 2048, and 4096
5041  */
5042  int formatted_block_size;
5043 
5044  /* If pending_block_size is zero then this command is not changing the block size, so
5045  * the block size after formatting will be ramdisc->block_size (including DIF data).
5046  * In this case the size of the user data block will be ramdisc->block_size - 8 if
5047  * protection is already enabled, or ramdisc->block_size if protection is disabled.
5048  *
5049  * If pending_block_size is non-zero then this command is changing the block size, so
5050  * the block size after formatting will be ramdisc->pending_block_size (NOT including DIF data).
5051  * In this case the size of the user data block will be ramdisc->pending_block_size.
5052  */
5053 
5054  if (io->tgt_io.ramdisc->pending_block_size == 0) {
5055  if (io->tgt_io.ramdisc->prot_en) {
5056  formatted_block_size = io->tgt_io.ramdisc->phys_block_size - 8;
5057  } else {
5058  formatted_block_size = io->tgt_io.ramdisc->phys_block_size;
5059  }
5060  } else {
5061  formatted_block_size = io->tgt_io.ramdisc->pending_block_size;
5062  }
5063 
5064  switch(formatted_block_size) {
5065  case 512:
5066  case 520:
5067  case 1024:
5068  case 2048:
5069  case 4096:
5070  case 4104:
5071  break;
5072  default:
5073  ocs_log_test(io->ocs, "Failing format with protection enabled and block size of %d\n", formatted_block_size);
5075  }
5076  }
5077 
5078 
5079  /* Ready to format the unit */
5080 
5081  if (ocs_format_ramdisc(io->ocs, io->tgt_io.ramdisc, io->tgt_io.ramdisc->pending_block_size, p_type,
5082  prot_en, dif_separate)) {
5083 
5084  /*
5085  * Note: According to SBC 4.10 the UA condition for CAPACITY DATA HAS CHANGED
5086  * should be established for all initiators except the one that generated the
5087  * command that caused the condition.
5088  */
5089 
5090  ocs_ramd_post_ua(io->ocs, io->tgt_io.ramdisc,
5092  io->node);
5093  }
5094 
5095 
5096 
5097  return 0;
5098 }
5099 
5100 /**
5101  * @brief Command data phase callback for the FORMAT UNIT command.
5102  *
5103  * @par Description
5104  * This function is called with the SCSI command data phase has completed following
5105  * a FORMAT UNIT command.
5106  *
5107  * @param io Pointer to the IO structure.
5108  * @param scsi_status Completion status.
5109  * @param flags Flags.
5110  * @param arg Application-specified argument.
5111  *
5112  * @return Returns 0 on success, or a negative error code value on failure.
5113  */
5114 static int32_t
5115 scsi_format_unit_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
5116 {
5117 
5118  api_trace();
5119 
5120  ocs_assert(io, -1);
5121  ocs_assert(io->ocs, -1);
5122 
5123  ocs_lock(&io->tgt_io.sm_lock);
5124  if (scsi_status == OCS_SCSI_STATUS_GOOD) {
5125  if (flags & OCS_SCSI_IO_CMPL) {
5127  }
5128  else if (flags & OCS_SCSI_IO_CMPL_RSP_SENT) {
5129  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_RESP_CMPL, NULL);
5130  }
5131  }
5132  else {
5133  ocs_log_test(io->ocs, "dataphase completed with errors %d\n", scsi_status);
5134  ocs_unlock(&io->tgt_io.sm_lock);
5135  return -scsi_status;
5136  }
5137 
5138  ocs_unlock(&io->tgt_io.sm_lock);
5139  return 0;
5140 }
5141 
5142 /**
5143  * @ingroup scsi_cmd
5144  * @brief Process the SCSI FORMAT UNIT command.
5145  *
5146  * @par Description
5147  * The SCSI FORMAT UNIT command is processed.
5148  *
5149  * @param io Pointer to the IO context.
5150  * @param cdb Pointer to the SCSI CDB.
5151  *
5152  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
5153  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
5154  */
5155 static ocs_ramd_hndlr_rtn_e
5156 ocs_ramd_format_unit(ocs_io_t *io, uint8_t cdb[])
5157 {
5158  uint8_t fmtpinfo;
5159  uint8_t longlist;
5160  uint8_t fmtdata;
5161  /*uint8_t cmplst; */
5162  /*uint8_t defect_list_format; */
5163  uint8_t parameter_list_header_length;
5164  uint32_t rc;
5165  ocs_scsi_sgl_t sgl;
5166 
5167  api_trace();
5168 
5169  fmtpinfo = (cdb[1] & 0xc0) >> 6;
5170  longlist = (cdb[1] & 0x20) >> 5;
5171  fmtdata = (cdb[1] & 0x10) >> 4;
5172  /*cmplst = (cdb[1] & 0x80) >> 3; */
5173  /*defect_list_format = (cdb[1] & 0x07); */
5174 
5175  if ((fmtpinfo != 0) && (fmtdata == 0)) {
5176  /* fmtdata indicates that format data will be sent
5177  * If the protection information is non-zero then format data is required.
5178  * It is an error to have non-zero protection information without format data
5179  */
5181  }
5182 
5183  /* If LUN is not DIF capable, fail (SBC 3r30, Table 34, note f) */
5184  if (fmtpinfo && !io->tgt_io.ramdisc->prot_capable) {
5186  }
5187 
5188  /* Save fmtpinfo - it will be used later when we validate the parameter list */
5189  io->tgt_io.fmtpinfo = fmtpinfo;
5190 
5191  if (fmtdata) {
5192  /* We need to initiate a DATA OUT phase to get the parameter list */
5193  if (longlist) {
5194  parameter_list_header_length = 8;
5195  } else {
5196  parameter_list_header_length = 4;
5197  }
5198 
5199  rc = ocs_dma_alloc(io->ocs, &io->tgt_io.payload, parameter_list_header_length, 16);
5200  if (rc) {
5201  ocs_log_err(io->ocs, "ocs_scsi_dma_alloc failed\n");
5202  return SCSI_HANDLER_NOT_STARTED;
5203  }
5204  ocs_assert(io->tgt_io.payload.virt, -1);
5205 
5206  /*
5207  * If we have already received the data in the first burst, then
5208  * just copy the data in to payload buffer and advance the
5209  * io state machine.
5210  */
5211  if (io->tgt_io.first_burst_bytes >= parameter_list_header_length) {
5212  ocs_ramd_first_burst_local(io, parameter_list_header_length);
5215  /* We should be in the response wait phase already,
5216  * so return that status again...
5217  */
5219  }
5220 
5221  /* Build an SGL that points to our buffer */
5222  ocs_memset(&sgl, 0, sizeof(ocs_scsi_sgl_t));
5223  sgl.addr = io->tgt_io.payload.phys;
5224  sgl.len = parameter_list_header_length;
5225 
5226  rc = ocs_scsi_recv_wr_data(io, 0, NULL,
5227  &sgl, 1, parameter_list_header_length, scsi_format_unit_cb, NULL);
5228 
5229  if (rc == 0) {
5231  } else {
5232  ocs_log_test(io->ocs, "Failed to start data phase, rc is %d\n", rc);
5233  return SCSI_HANDLER_NOT_STARTED;
5234  }
5235 
5236  } else {
5237  /* No FMTDATA bit, so no dataout phase required
5238  * This can only happen if fmtpinfo is also 0, indicating p_type is 0 (No protection)
5239  */
5240 
5241  rc = ocs_format_ramdisc(io->ocs, io->tgt_io.ramdisc, io->tgt_io.ramdisc->pending_block_size,
5242  0, 0, FALSE);
5243  if (rc) {
5244  /* Post UA */
5245  ocs_ramd_post_ua(io->ocs, io->tgt_io.ramdisc,
5248  }
5249 
5250  return ocs_ramd_good_status(io);
5251  }
5252 
5253  /* Should not be reached */
5254  return SCSI_HANDLER_NOT_STARTED;
5255 
5256 }
5257 
5258 /**
5259  * @brief A structure used to create the READ CAPACITY (10) parameter data.
5260  */
5261 typedef struct {
5262  uint32_t returned_lba;
5263  uint32_t lba_length;
5265 
5266 /**
5267  * @ingroup scsi_cmd
5268  * @brief Process the SCSI READ CAPACITY (10) command.
5269  *
5270  * @par Description
5271  * The SCSI READ CAPACITY (10) command is processed.
5272  *
5273  * @param io Pointer to the IO context.
5274  * @param cdb Pointer to the SCSI CDB.
5275  *
5276  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
5277  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
5278  */
5279 static ocs_ramd_hndlr_rtn_e
5280 ocs_ramd_read_capacity10(ocs_io_t *io, uint8_t cdb[])
5281 {
5282  int32_t rc;
5283  ocs_ramdisc_sgl_t sgl[1];
5284  uint32_t lba;
5285  uint32_t pmi;
5286  uint32_t xfer_len;
5287  uint32_t block_size;
5289 
5290  lba = (cdb[2] << 24) |
5291  (cdb[3] << 16) |
5292  (cdb[4] << 8) |
5293  (cdb[5] << 0);
5294  pmi = (cdb[8] & 1) != 0;
5295 
5296  cmd_trace(io, "lba x%x cpmi x%x\n", lba, pmi);
5297 
5298  rc = ocs_dma_alloc(io->ocs, &io->tgt_io.payload, 256, 16);
5299  if (rc) {
5300  ocs_log_err(io->ocs, "ocs_dma_alloc failed\n");
5301  return SCSI_HANDLER_NOT_STARTED;
5302  }
5303  ocs_assert(io->tgt_io.payload.virt, -1);
5304 
5305  param = io->tgt_io.payload.virt;
5306  memset(param, 0, sizeof(*param));
5307 
5308  /* we ignore pmi in this implementation */
5309  param->returned_lba = ocs_htobe32(io->tgt_io.ramdisc->block_count - 1);
5310 
5311  /* We want to return the size of the user data area of the block. If protection
5312  * is enabled then this is 8 bytes less than the actual block size.
5313  */
5314  block_size = io->tgt_io.ramdisc->phys_block_size;
5315  if (io->tgt_io.ramdisc->prot_en) {
5316  block_size -= 8;
5317  }
5318  param->lba_length = ocs_htobe32(block_size);
5319 
5320  xfer_len = sizeof (*param);
5321  sgl[0].addr = io->tgt_io.payload.phys;
5322  sgl[0].len = xfer_len;
5323 
5324  io->tgt_io.is_read = TRUE;
5325 
5326  /* Send read data */
5328  xfer_len, scsi_dataphase_cb, NULL);
5329  if (rc == 0)
5331 
5332  return SCSI_HANDLER_NOT_STARTED;
5333 }
5334 
5335 /**
5336  * @brief A structure used to create READ CAPACITY (16) parameter data.
5337  */
5338 typedef struct {
5339  uint64_t returned_lba;
5340  uint32_t lba_length;
5341  uint8_t prot_en : 1,
5342  p_type : 3,
5343  rsvd1 : 4;
5344  uint8_t lba_per_phys_blk:4,
5345  p_i_exponent:4;
5346  uint8_t lowest_aligned_lba_msb:6,
5347  lbprz:1,
5348  lbpme:1;
5350  uint8_t reserved[16];
5352 
5353 /**
5354  * @ingroup scsi_cmd
5355  * @brief Process the SCSI READ CAPACITY (16) command.
5356  *
5357  * @par Description
5358  * The SCSI READ CAPACITY (16) command is processed.
5359  *
5360  * @param io Pointer to the IO context.
5361  * @param cdb Pointer to the SCSI CDB.
5362  *
5363  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
5364  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
5365  */
5366 static ocs_ramd_hndlr_rtn_e
5367 ocs_ramd_read_capacity16(ocs_io_t *io, uint8_t cdb[])
5368 {
5369  int32_t rc;
5370  ocs_ramdisc_sgl_t sgl[1];
5371  uint64_t lba;
5372  uint32_t allocation_len;
5373  uint32_t pmi;
5374  uint32_t block_size;
5375  uint32_t xfer_len;
5377 
5378  lba = (((uint64_t)cdb[2]) << 56) |
5379  (((uint64_t)cdb[3]) << 48) |
5380  (((uint64_t)cdb[4]) << 40) |
5381  (((uint64_t)cdb[5]) << 32) |
5382  (((uint64_t)cdb[6]) << 24) |
5383  (((uint64_t)cdb[7]) << 16) |
5384  (((uint64_t)cdb[8]) << 8) |
5385  (((uint64_t)cdb[9]) << 0);
5386  allocation_len =
5387  (cdb[10] << 24) |
5388  (cdb[11] << 16) |
5389  (cdb[12] << 8) |
5390  (cdb[13] << 0);
5391 
5392  /* As per SPC-4 an allocation length of 0 means no data transfer
5393  * so just return good status now.
5394  */
5395  if (allocation_len == 0) {
5396  return(ocs_ramd_good_status(io));
5397  }
5398 
5399  pmi = (cdb[15] & 1) != 0;
5400 
5401  cmd_trace(io, "lba %" PRId64 " len %d pmi %d\n", lba, allocation_len, pmi);
5402  rc = ocs_dma_alloc(io->ocs, &io->tgt_io.payload, allocation_len, 16);
5403  if (rc) {
5404  ocs_log_err(NULL, "ocs_dma_alloc failed\n");
5405  return SCSI_HANDLER_NOT_STARTED;
5406  }
5407  ocs_assert(io->tgt_io.payload.virt, -1);
5408 
5409  param = io->tgt_io.payload.virt;
5410  memset(param, 0, sizeof(*param));
5411 
5412  /* we ignore pmi in this implementation */
5413  param->returned_lba = ocs_htobe64(io->tgt_io.ramdisc->block_count - 1);
5414 
5415  /* We want to return the size of the user data area of the block. If protection
5416  * is enabled then this is 8 bytes less than the actual block size.
5417  */
5418  block_size = io->tgt_io.ramdisc->phys_block_size;
5419  if (io->tgt_io.ramdisc->prot_en) {
5420  block_size -= 8;
5421  }
5422  param->lba_length = ocs_htobe32(block_size);
5423 
5424  if (io->tgt_io.ramdisc->prot_en == 0) {
5425  /* Protection off */
5426  param->prot_en = 0;
5427  param->p_type = 0;
5428  } else {
5429  param->prot_en = 1;
5430  param->p_type = io->tgt_io.ramdisc->p_type - 1;
5431  }
5432 
5433  param->p_i_exponent = 0;
5434 
5435 
5436  xfer_len = sizeof (*param);
5437  sgl[0].addr = io->tgt_io.payload.phys;
5438  sgl[0].len = xfer_len;
5439 
5440  io->tgt_io.is_read = TRUE;
5441 
5442  /* Send read data */
5444  xfer_len, scsi_dataphase_cb, NULL);
5445  if (rc == 0)
5447 
5448  return SCSI_HANDLER_NOT_STARTED;
5449 }
5450 
5451 #define INQ_VPD_SUPPORTED_PAGE_00 0x00
5452 #define INQ_VPD_UNIT_SERIAL_NO_PAGE_80 0x80
5453 #define INQ_VPD_DEVICE_ID_PAGE_83 0x83
5454 #define INQ_VPD_EXT_INQ_DATA_PAGE_86 0x86
5455 #define INQ_VPD_BLOCKS_LIMIT_PAGE_B0 0xB0
5456 #define INQ_VPD_BLOCK_DEV_CHARS_PAGE_B1 0xB1
5457 
5458 /**
5459  * VPD pages supported by the ramd driver
5460  * Adding a new VPD page must be inserted in correct
5461  * numerical oreder.
5462  */
5470 };
5471 
5472 /**
5473  * @brief A structure used to create the standard inquiry data.
5474  */
5475 typedef struct {
5476  uint8_t peripheral_device_type:5,
5477  peripheral_qualifier:3;
5478  uint8_t :7,
5479  rmb:1;
5480  uint8_t version;
5481  uint8_t response_data_format:4,
5482  hisup:1,
5483  norm_aca:1,
5484  :2;
5485  uint8_t additional_len;
5486  uint8_t protect:1,
5487  :2,
5488  pc:1,
5489  tpgs:2,
5490  acc:1,
5491  sccs:1;
5492  uint8_t addr16:1,
5493  :3,
5494  multip:1,
5495  vs:1,
5496  enc_serv:1,
5497  :1;
5498  uint8_t vs7:1,
5499  cmd_que:1,
5500  :2,
5501  sync:1,
5502  wbus16:1,
5503  :2;
5504  uint8_t t10_vendor_id[8];
5505  uint8_t product_id[16];
5506  uint8_t product_revision_level[4];
5507  uint8_t vendor_specific[20];
5508  uint8_t ius:1,
5509  qas:1,
5510  clocking:2,
5511  :4;
5512  uint8_t rsvd57;
5513  uint8_t version_descriptor_1[2];
5514  uint8_t version_descriptor_2[2];
5515  uint8_t version_descriptor_3[2];
5516  uint8_t version_descriptor_4[2];
5517  uint8_t version_descriptor_5[2];
5518  uint8_t version_descriptor_6[2];
5519  uint8_t version_descriptor_7[2];
5520  uint8_t version_descriptor_8[2];
5521  uint8_t rsvd74[22];
5522  uint8_t vendor_specific_parameters[0];
5524 
5525 /**
5526  * @brief Vendor specific inquiry data for External DIF
5527  */
5528 typedef struct {
5529  uint8_t rsvd96[72];
5532  uint8_t ato:1,
5533  ref_chk:1,
5534  app_chk:1,
5535  grd_chk:1,
5536  spt:3,
5537  protect:1;
5538  uint8_t rsvd171;
5540 
5541 /**
5542  * @brief A structure used to create the inquiry page 00 data.
5543  */
5544 typedef struct {
5545  uint8_t peripheral_device_type:5,
5546  peripheral_qualifier:3;
5547  uint8_t page_code;
5548  uint8_t :8;
5549  uint8_t page_length;
5550  uint8_t page_codes[0];
5552 
5553 /**
5554  * @brief A structure used to create the inquiry page 80 data.
5555  */
5556 typedef struct {
5557  uint8_t peripheral_device_type:5,
5558  peripheral_qualifier:3;
5559  uint8_t page_code;
5560  uint8_t :8;
5561  uint8_t page_length;
5562  uint8_t product_serial_number[24];
5564 
5565 /**
5566  * @brief A structure used to create the inquiry page 83 data.
5567  */
5568 typedef struct {
5569  uint8_t peripheral_device_type:5,
5570  peripheral_qualifier:3;
5571  uint8_t page_code;
5572  uint16_t page_length;
5574 
5575 /**
5576  * @brief A structure used to create the device ID data for inquiry page 83.
5577  */
5578 typedef struct {
5579  uint8_t code_set:4,
5580  protocol_identifier:4;
5581  uint8_t designator_type:4,
5582  association:2,
5583  :1,
5584  piv:1;
5585  uint8_t :8;
5588 
5589 /**
5590  * @brief The Designator used by inquiry page 83. This is an EUI-64
5591  * based 12-byte identifier.
5592  */
5593 typedef struct {
5594  uint8_t company_id[3];
5595  uint8_t vendor_specific_extension_identifier[5];
5596  uint32_t directory_id;
5598 
5599 /**
5600  * @brief A structure used to create the inquiry page 86 data.
5601  */
5602 typedef struct {
5603  uint8_t peripheral_device_type:5,
5604  peripheral_qualifier:3;
5605  uint8_t page_code;
5606  uint16_t page_length;
5607  uint8_t ref_chk:1,
5608  app_chk:1,
5609  grd_chk:1,
5610  spt:3,
5611  active_microcode:2;
5612  uint8_t simpsup:1,
5613  ordsup:1,
5614  headsup:1,
5615  prior_sup:1,
5616  group_sup:1,
5617  uask_sup:1,
5618  :2;
5619  uint8_t v_sup:1,
5620  nv_sup:1,
5621  crd_sup:1,
5622  wu_sup:1,
5623  :4;
5624  uint8_t liuclr:1,
5625  :3,
5626  p_i_i_sup:1,
5627  :3;
5628  uint8_t cbcs:1,
5629  :3,
5630  r_sup:1,
5631  :3;
5632  uint8_t multi_io_nexus_microcode_download:4,
5633  :4;
5634  uint8_t rsvd[54];
5636 
5637 /**
5638  * @brief A structure used to create the inquiry page B0 data.
5639  */
5640 typedef struct {
5641  uint8_t peripheral_device_type:5,
5642  peripheral_qualifier:3;
5643  uint8_t page_code;
5644  uint16_t page_length;
5645  uint8_t wsnz:1,
5646  :7;
5647  uint8_t maximum_compare_and_write_length;
5655  uint32_t unmap_granularity_alignment:31,
5656  ugavalid:1;
5659  uint8_t rsvd2[20];
5661 
5662 /**
5663  * @brief A structure used to create the inquiry page B1 data.
5664  */
5665 typedef struct {
5666  uint8_t peripheral_device_type:5,
5667  peripheral_qualifier:3;
5668  uint8_t page_code;
5669  uint16_t page_length;
5671  uint8_t rsvd1;
5672  uint8_t nominal_form_factor:4,
5673  :4;
5674  uint8_t rsvd2[56];
5676 
5677 
5678 /**
5679  * @ingroup scsi_cmd
5680  * @brief Process the SCSI INQUIRY command.
5681  *
5682  * @par Description
5683  * The SCSI INQUIRY command is processed.
5684  *
5685  * @param io Pointer to the IO context.
5686  * @param cdb Pointer to the SCSI CDB.
5687  *
5688  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
5689  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
5690  */
5691 static ocs_ramd_hndlr_rtn_e
5692 ocs_ramd_inquiry(ocs_io_t *io, uint8_t cdb[])
5693 {
5694  int32_t rc;
5695  ocs_scsi_sgl_t sgl[1];
5696  uint32_t evpd;
5697  uint32_t page_code;
5698  uint32_t allocation_len;
5699  uint32_t xfer_len = 0;
5700  ocs_t *ocs = io->ocs;
5701 
5702  cmd_trace(io, "");
5703  evpd = cdb[1] & 1;
5704  page_code = cdb[2];
5705  allocation_len = (cdb[3] << 8) | (cdb[4]);
5706 
5707  /* As per SPC-4 an allocation length of 0 means no data transfer
5708  * so just return good status now.
5709  */
5710  if (allocation_len == 0) {
5711  return(ocs_ramd_good_status(io));
5712  }
5713 
5714  rc = ocs_dma_alloc(io->ocs, &io->tgt_io.payload, 256, 16);
5715  if (rc) {
5716  ocs_log_err(NULL, "ocs__dma_alloc failed\n");
5717  return SCSI_HANDLER_NOT_STARTED;
5718  }
5719  ocs_assert(io->tgt_io.payload.virt, -1);
5720 
5721  /* Check for an invalid LUN. SPC-5 6.5.1:
5722  * In response to an INQUIRY command received by an incorrect logical
5723  * unit, the SCSI target device shall return the INQUIRY data with the
5724  * peripheral qualifier set to the value defined in 6.5.2.
5725  *
5726  * SPC-5 Table 138:
5727  * Qualifier 011b is "An addressed logical unit is not accessible to
5728  * the task router (see SAM-5) contained in the SCSI target port that
5729  * received this INQUIRY command. If the task router sets the PERIPHERAL
5730  * QUALIFIER field to 011b, the task router shall set the PERIPHERAL
5731  * DEVICE TYPE field to 1Fh.
5732  *
5733  * Peripheral Device Type 1fh is "Unknown or no device type"
5734  *
5735  */
5736 
5737  if (!io->tgt_io.lun_is_valid) {
5738  if (evpd == 0) {
5739  scsi_std_inquiry_t *inq;
5740 
5741  inq = io->tgt_io.payload.virt;
5742  memset(inq, 0, sizeof(*inq));
5743 
5744  inq->peripheral_qualifier = 0x3;
5745  inq->peripheral_device_type = 0x1f;
5746 
5747  xfer_len = offsetof(scsi_std_inquiry_t, vendor_specific);
5748  } else {
5750  }
5751 
5752  } else {
5753 
5754  if (evpd == 0) {
5755  scsi_std_inquiry_t *inq;
5756 
5757  inq = io->tgt_io.payload.virt;
5758  memset(inq, 0, sizeof(*inq));
5759 
5760  inq->additional_len = 0x1f;
5761  inq->peripheral_device_type = 0; /* SBC-3 */
5762  inq->version = 3; /* SPC-3 */
5763  inq->hisup = 1;
5764  inq->response_data_format = 2;
5765  inq->protect = io->tgt_io.ramdisc->prot_capable;
5766  inq->cmd_que = 1;
5767  ocs_memcpy(inq->t10_vendor_id, "EMULEX ", 8);
5768  ocs_memcpy(inq->product_id, "ocs-ramd ", 16);
5769  ocs_memcpy(inq->product_revision_level, "0000", 4);
5770 
5771  /* compute the number of bytes to transfer */
5772  xfer_len = offsetof(scsi_std_inquiry_t, vendor_specific);
5773 
5774  if (io->tgt_io.ramdisc->external_dif) {
5776 
5777  ocs_memset(inqextdif, 0, sizeof(scsi_extdif_inquiry_t));
5778  ocs_memset(inq->t10_vendor_id, 0, sizeof(inq->t10_vendor_id));
5779 
5780  /* Proprietary End-to-end protection */
5781  inq->additional_len = 0xa7;
5782  inq->protect = 0;
5783  ocs_strncpy(inq->t10_vendor_id, io->tgt_io.ramdisc->t10_vendor_id,
5784  MIN(sizeof(inq->t10_vendor_id), ocs_strlen(io->tgt_io.ramdisc->t10_vendor_id)));
5785 
5786  inqextdif->protection_information_length = 2;
5787  inqextdif->data_format_version = 1;
5788 
5789  inqextdif->protect = io->tgt_io.ramdisc->prot_capable;
5790  /* inqextdif->spt is 0x0 meaning Type 1 */
5791  inqextdif->grd_chk = 1;
5792  inqextdif->app_chk = 0;
5793  inqextdif->ref_chk = 1;
5794  /* inqextdif->ato is 0x0 meaning app tag is modified */
5795 
5796  xfer_len = sizeof(scsi_std_inquiry_t) + sizeof(scsi_extdif_inquiry_t);
5797  }
5798  } else {
5799  switch(page_code) {
5800  case 0x00: {
5802  uint8_t *p;
5803  uint8_t i;
5804 
5805  inq = io->tgt_io.payload.virt;
5806  memset(inq, 0, sizeof(*inq));
5807 
5808  p = inq->page_codes;
5809  for (i = 0; i < sizeof(supportedVPDPageList); i++) {
5810  *p++ = supportedVPDPageList[i];
5811  }
5812  xfer_len = sizeof(*inq) + (p - inq->page_codes);
5813  inq->page_length = xfer_len - 4;
5814  break;
5815  }
5816  case 0x80: {
5818  uint8_t *pserial;
5819  uint32_t len;
5820  uint64_t serial_id, serial_len;
5821  uint8_t serial_str[16];
5822 
5823  inq = io->tgt_io.payload.virt;
5824  memset(inq, 0, sizeof(*inq));
5825 
5826  inq->page_code = page_code;
5827  inq->page_length = sizeof(*inq) - 4;
5828 
5829  /* Fetch the serial number; it's a VPD segment, so it has a one byte
5830  * length followed by the length number of bytes
5831  */
5833  if (pserial) {
5834  len = *pserial ++;
5835  if (len > sizeof(inq->product_serial_number))
5836  len = sizeof(inq->product_serial_number);
5837 
5838  ocs_memcpy(inq->product_serial_number, pserial, len);
5839 
5840  /* BZ 202760: Make the serial# unique for each lun on a given HBA */
5841  serial_id = io->tgt_io.lun;
5842 
5843  /* Place the sport index in the upper byte of the lower 32 bits */
5844  if (!ocs->tgt_ocs.use_global_ramd) {
5845  if (io->node && io->node->sport) {
5846  serial_id |= (io->node->sport->instance_index << 24);
5847  }
5848  }
5849 
5850  /* Place the OCS instance index in the upper 32 bits */
5851  serial_id |= ((uint64_t)ocs->instance_index << 32);
5852 
5853  /* Stringify the serial_id */
5854  ocs_snprintf(serial_str, sizeof(serial_str), "%" PRIX64, serial_id);
5855  serial_len = ocs_strlen(serial_str) + 1;
5856 
5857  /* Append the serial_str at the end of VPD SN */
5858  if (len > (sizeof(inq->product_serial_number) - serial_len)) {
5859  len = (sizeof(inq->product_serial_number) - serial_len);
5860  }
5861 
5862  ocs_memcpy(&inq->product_serial_number[len], serial_str, serial_len);
5863  }
5864 
5865  /* compute the number of bytes to transfer */
5866  xfer_len = sizeof(*inq);
5867 
5868  break;
5869  }
5870  case 0x83: {
5871  uint8_t *wwnn;
5872  scsi_device_identification_vpd_page_t *inq = io->tgt_io.payload.virt;
5874  scsi_device_designator_t *designator = (scsi_device_designator_t*) (&pd[1]);
5875  uint32_t directory_id;
5876 
5877  ocs_memset(inq, 0, sizeof(*inq));
5878  ocs_memset(pd, 0, sizeof(*pd));
5879  ocs_memset(designator, 0, sizeof(*designator));
5880 
5881  /*
5882  * The SCSI device designator has to uniquely identify a LUN (ocs_ramdisc_t instance).
5883  * If use_global_ramd is true then there is one ramd made up of 1 or more LUNs. This
5884  * ramd is shared between all target ports.
5885  * If use_global_ramd is false then each sport has its own ramd, again made of 1 or more
5886  * LUNs.
5887  *
5888  * The first 3 bytes of the designator are always the IEEE Company ID (00 00 C9)
5889  * Bytes 3-7 will be the WWNN of the port associated with the ramd. In the case of
5890  * a global ramdisc this is always the WWNN of the first port (OCS instance 0).
5891  * For per-sport ramdiscs this is the WWNN from the OCS instance to which the sport
5892  * belongs.
5893  * Bytes 8-11 identify the LUN within the ramdisc. For a per-sport ramdisc the
5894  * index of the sport is placed in the upper byte.
5895  *
5896  * Byte Contents
5897  * 0-2 Company OUI
5898  * 3-7 WWNN
5899  * 8 sport index
5900  * 9-11 LUN
5901  */
5902  directory_id = io->tgt_io.lun;
5903  if (!ocs->tgt_ocs.use_global_ramd) {
5904  ocs_node_t *node = io->node;
5905  if (node != NULL) {
5906  ocs_sport_t *sport = node->sport;
5907  if (sport != NULL) {
5908  directory_id |= (sport->instance_index << 24);
5909  }
5910  }
5912  } else {
5913  ocs_t *first_ocs;
5914 
5915  first_ocs = ocs_get_instance(0);
5916  /* Added a check to verify that there is a first_ocs port,
5917  * else grab the current port instance wwnn
5918  */
5919  if (first_ocs != NULL) {
5920  wwnn = ocs_scsi_get_property_ptr(first_ocs, OCS_SCSI_WWNN);
5921  } else {
5923  }
5924  }
5925 
5926  inq->page_code = page_code;
5927  pd->code_set = 1; /* Code set 1 == Binary */
5928  pd->designator_type = 2; /* Designator Type 2 == EUI-64 */
5929 
5930  ocs_memcpy(designator->vendor_specific_extension_identifier, &(wwnn[3]), 5);
5931  designator->company_id[2] = 0xc9;
5932  designator->directory_id = ocs_htobe32(directory_id);
5933 
5934  pd->designator_length = sizeof(*designator);
5935  inq->page_length = ocs_htobe16(sizeof(*pd) + sizeof(*designator));
5936 
5937  xfer_len = sizeof(*inq) + sizeof(*pd) + sizeof(*designator);
5938  break;
5939  }
5940 
5941  case 0x86: {
5942  scsi_extended_inquiry_data_vpd_page_t *inq = io->tgt_io.payload.virt;
5943 
5944  ocs_memset(inq, 0, sizeof(*inq));
5945 
5946  inq->page_code = page_code;
5947  inq->page_length = ocs_htobe16(sizeof(*inq) - 4);
5948  if (io->tgt_io.ramdisc->external_dif) {
5949  inq->spt = 1;
5950  inq->grd_chk = 1;
5951  inq->app_chk = 1;
5952  inq->ref_chk = 1;
5953  }
5954 
5955  xfer_len = sizeof(*inq);
5956 
5957  break;
5958  }
5959 
5960  case 0xB0: {
5961  scsi_block_limits_vpd_page_t *inq = io->tgt_io.payload.virt;
5962 
5963  ocs_memset(inq, 0, sizeof(*inq));
5964 
5965  inq->page_code = page_code;
5966  inq->page_length = ocs_htobe16(sizeof(*inq) - 4);
5967 
5968  inq->wsnz = 0;
5969  inq->maximum_compare_and_write_length = 0; /* Not supported */
5970  inq->optimal_transfer_length_granularity = 0; /* Not reported */
5971  inq->maximum_transfer_length = 0; /* Not reported */
5972  inq->optimal_transfer_length = 0; /* Not reported */
5974  inq->maximum_unmap_lba_count = 0; /* Not supported */
5975  inq->maximum_unmap_block_descriptor_count = 0; /* Not supported */
5976  inq->optimal_unmap_granularity = 0; /* Not specified */
5977  inq->ugavalid = 0;
5978  inq->unmap_granularity_alignment = 0;
5981 
5982  xfer_len = sizeof(*inq);
5983 
5984  break;
5985  }
5986 
5987  case 0xB1: {
5988  scsi_block_device_characteristics_vpd_page_t *inq = io->tgt_io.payload.virt;
5989 
5990  ocs_memset(inq, 0, sizeof(*inq));
5991 
5992  inq->page_code = page_code;
5993  inq->page_length = ocs_htobe16(sizeof(*inq) - 4);
5994  inq->medium_rotation_rate = ocs_htobe16(1); /* Non-rotating medium */
5995  inq->nominal_form_factor = 0; /* Nominal form factor is not reported */
5996 
5997  xfer_len = sizeof(*inq);
5998 
5999  break;
6000  }
6001 
6002  default:
6003  return SCSI_CMD_NOT_SUPPORTED;
6004  }
6005  }
6006  }
6007 
6008  if (xfer_len > allocation_len) {
6009  xfer_len = allocation_len;
6010  }
6011 
6012  sgl[0].addr = io->tgt_io.payload.phys;
6013  sgl[0].len = xfer_len;
6014 
6015  /* Send read data */
6016  io->tgt_io.is_read = TRUE;
6017 
6019  sgl, 1, xfer_len, scsi_dataphase_cb, NULL);
6020  if (rc == 0) {
6022  }
6023 
6024  return SCSI_HANDLER_NOT_STARTED;
6025 }
6026 
6027 /**
6028  * @ingroup scsi_cmd
6029  * @brief Process the SCSI REQUEST SENSE command.
6030  *
6031  * @par Description
6032  * The SCSI REQUEST SENSE command is processed.
6033  *
6034  * @param io Pointer to the IO context.
6035  * @param cdb Pointer to the SCSI CDB.
6036  *
6037  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
6038  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
6039  */
6040 static ocs_ramd_hndlr_rtn_e
6041 ocs_ramd_request_sense(ocs_io_t *io, uint8_t cdb[])
6042 {
6043  uint8_t allocation_len;
6044  static char sense_data[] = {
6045  0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a,
6046  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6047  0x00, 0x00,
6048  };
6049  int sense_data_len = 18;
6050  int32_t rc;
6051  ocs_scsi_sgl_t sgl[1];
6052 
6053  rc = ocs_dma_alloc(io->ocs, &io->tgt_io.payload, 256, 16);
6054  if (rc) {
6055  ocs_log_err(NULL, "ocs_scsi_dma_alloc failed\n");
6056  return SCSI_HANDLER_NOT_STARTED;
6057  }
6058  ocs_assert(io->tgt_io.payload.virt, -1);
6059 
6060  allocation_len = cdb[4];
6061 
6062  /* As per SPC-4 an allocation length of 0 means no data transfer
6063  * so just return good status now.
6064  */
6065  if (allocation_len == 0) {
6066  return(ocs_ramd_good_status(io));
6067  }
6068 
6069  if (allocation_len < sense_data_len) {
6070  sense_data_len = allocation_len;
6071  }
6072 
6073  ocs_memcpy(io->tgt_io.payload.virt, sense_data, sense_data_len);
6074 
6075  sgl[0].addr = io->tgt_io.payload.phys;
6076  sgl[0].len = sense_data_len;
6077 
6078  io->tgt_io.is_read = TRUE;
6079 
6080  /* Send read data */
6081  rc = ocs_scsi_send_rd_data(io, 0, NULL, sgl, 1, sgl[0].len, scsi_dataphase_cb, NULL);
6082 
6083  if (rc == 0)
6085  return SCSI_HANDLER_NOT_STARTED;
6086 }
6087 
6088 /**
6089  * @ingroup scsi_cmd
6090  * @brief Process the SCSI TEST UNIT READY command
6091  *
6092  * @par Description
6093  * The SCSI TEST UNIT READY command is processed.
6094  *
6095  * @param io Pointer to the IO context.
6096  * @param cdb Pointer to the SCSI CDB.
6097  *
6098  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
6099  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
6100  */
6101 static ocs_ramd_hndlr_rtn_e
6102 ocs_ramd_test_unit_ready(ocs_io_t *io, uint8_t cdb[])
6103 {
6104  cmd_trace(io, "");
6105 
6106  ocs_assert(cdb[0] == SCSI_TEST_UNIT_READY, -1);
6107 
6108  return ocs_ramd_good_status(io);
6109 }
6110 
6111 
6112 /**
6113  * @ingroup scsi_cmd
6114  * @brief Process the SCSI RESERVE or RELEASE command
6115  *
6116  * @par Description
6117  * The SCSI RESERVE or RELEASE command is processed.
6118  * This is only a stub however and always returns a
6119  * good status with no actual reservation being made.
6120  *
6121  * @param io Pointer to the IO context.
6122  * @param cdb Pointer to the SCSI CDB.
6123  *
6124  * @return Returns the handler status for the command. SCSI_CMD_NOT_SUPPORTED
6125  * is an error and the IO should be freed.
6126  */
6127 static ocs_ramd_hndlr_rtn_e
6128 ocs_ramd_reserve_release(ocs_io_t *io, uint8_t cdb[])
6129 {
6130  char prop_buf[32];
6131  uint32_t stub_res6_rel6 = 0;
6132  cmd_trace(io, "");
6133 
6135 
6136  if (ocs_get_property("stub_res6_rel6", prop_buf, sizeof(prop_buf)) == 0) {
6137  stub_res6_rel6 = ocs_strtoul(prop_buf, 0, 0);
6138  }
6139 
6140  if (stub_res6_rel6 == TRUE) {
6141  return ocs_ramd_good_status(io);
6142  }
6143 
6144  return SCSI_CMD_NOT_SUPPORTED;
6145 }
6146 
6147 /**
6148  * @ingroup scsi_cmd
6149  * @brief Process the SCSI REPORT LUNS command.
6150  *
6151  * @par Description
6152  * The SCSI REPORT LUNS command is processed.
6153  *
6154  * @param io Pointer to the IO context.
6155  * @param cdb Pointer to the SCSI CDB.
6156  *
6157  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
6158  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
6159  */
6160 static ocs_ramd_hndlr_rtn_e
6161 ocs_ramd_report_luns(ocs_io_t *io, uint8_t cdb[])
6162 {
6163  int32_t rc;
6164  uint32_t i;
6165  ocs_scsi_sgl_t sgl[1];
6166 
6167  /*uint32_t select_report; */
6168  uint32_t allocation_len;
6169 
6170  uint32_t *param;
6171  uint32_t lun_count = 0;
6172  uint32_t xfer_len = 0;
6173 
6174 
6175  /* grab the lun count based on whether we have a global ramdisc */
6176  if (io->ocs->tgt_ocs.use_global_ramd) {
6177  lun_count = io->ocs->tgt_ocs.rdisc_count;
6178  } else if (io->node != NULL && io->node->sport != NULL) {
6179  lun_count = io->node->sport->tgt_sport.rdisc_count;
6180  }
6181 
6182  /*select_report = cdb[2]; // Unused. We return all luns. */
6183 
6184  allocation_len = (cdb[6] << 24) |
6185  (cdb[7] << 16) |
6186  (cdb[8] << 8) |
6187  (cdb[9] << 0);
6188 
6189  /* As per SPC-4 an allocation length of 0 means no data transfer
6190  * so just return good status now.
6191  */
6192  if (allocation_len == 0) {
6193  return(ocs_ramd_good_status(io));
6194  }
6195 
6196  cmd_trace(io, "len x%x\n", allocation_len);
6197 
6198  rc = ocs_dma_alloc(io->ocs, &io->tgt_io.payload, allocation_len, 16);
6199  if (rc) {
6200  ocs_log_err(NULL, "ocs_dma_alloc failed\n");
6201  return SCSI_HANDLER_NOT_STARTED;
6202  }
6203  ocs_assert(io->tgt_io.payload.virt, -1);
6204 
6205  param = io->tgt_io.payload.virt;
6206 
6207  *param++ = ocs_htobe32(lun_count*8);
6208  *param++ = 0;
6209 
6210  /*
6211  * Adjust the lun count if the allocated length does not allow the
6212  * entire list to be returned. This prevents us from writing past the
6213  * allocated memory.
6214  */
6215  if ((allocation_len - 8) / 8 < lun_count) {
6216  lun_count = (allocation_len - 8) / 8;
6217  }
6218 
6219  /* If there are a small number of LUNs, use this format */
6220  if (lun_count <= 256) {
6221  for (i = 0; i < lun_count; i ++) {
6222  uint8_t *p = (uint8_t*) param;
6223  ocs_memset(p, 0, 8);
6224  p[1] = i;
6225  param += 2;
6226  }
6227  }
6228 
6229  /*
6230  * For more than 256 LUNs, use the Flat Space LUN structure
6231  * See SCSI SAM spec, section 4.7 (This is table 12 in SAM-5)
6232  * Format:
6233  * byte 0: bits 7-6 01b (Address Method)
6234  * byte 0: bits 5-0 MSB of LUN
6235  * byte 1: LSB of LUN
6236  */
6237  else {
6238  for (i = 0; i < lun_count; i ++) {
6239  uint8_t *p = (uint8_t*) param;
6240  ocs_memset(p, 0, 8);
6241  p[0] = 0x01 << 6;
6242  p[0] |= (i & 0x3F00) >> 8;
6243  p[1] = i & 0xFF;
6244  param += 2;
6245  }
6246  }
6247 
6248  xfer_len = (param - ((uint32_t*)io->tgt_io.payload.virt))*sizeof(*param);
6249 
6250  sgl[0].addr = io->tgt_io.payload.phys;
6251  sgl[0].len = xfer_len;
6252 
6253  io->tgt_io.is_read = TRUE;
6254 
6255  /* Send read data */
6257  sgl, 1, xfer_len, scsi_dataphase_cb, NULL);
6258  if (rc == 0)
6260 
6261 
6262  return SCSI_HANDLER_NOT_STARTED;
6263 
6264 }
6265 
6266 /**
6267  * @ingroup scsi_cmd
6268  * @brief Validate a MODE SELECT parameter list.
6269  *
6270  * @par Description
6271  * Called after a MODE SELECT data out phase has completed. This function
6272  * validates the contents of the parameter list. If the parameter list is
6273  * valid, it takes the appropriate action and returns 0. If the parameter
6274  * list is not valid, the function returns a sense key, code, and qualifier to be returned
6275  * to the initiator in a CHECK CONDITION.
6276  *
6277  * @param io Pointer to the IO context.
6278  *
6279  * @return Returns 0 on success; or a non-zero sense key, code, and qualifier on failure.
6280  */
6281 static uint32_t
6283  /*uint8_t payload_length; */
6284  uint8_t *payload;
6285  uint32_t lun;
6286  ocs_ramdisc_t *ramdisc;
6287  /*uint16_t mode_data_length; */
6288  uint16_t block_descriptor_length;
6289  uint8_t longlba;
6290  uint32_t block_size = 0;
6291 
6292  api_trace();
6293 
6294  /*payload_length = io->tgt_io.mem_io_len; */
6295  payload = io->tgt_io.payload.virt;
6296 
6297  lun = io->tgt_io.lun;
6298  ramdisc = ocs_ramd_lookup(io, lun);
6299 
6300  if (io->tgt_io.mode_select_opcode == SCSI_MODE_SELECT6) {
6301  /*mode_data_length = payload[0]; */
6302  block_descriptor_length = payload[3];
6303  longlba = 0;
6304  payload += 4;
6305  } else {
6306  /*mode_data_length = (payload[0] << 8) | payload[1]; */
6307  block_descriptor_length = (payload[6] << 8) | payload[7];
6308  longlba = payload[4] & 0x01;
6309  payload += 8;
6310  }
6311 
6312  /*
6313  * Per the spec we can have block descriptor entries that are either
6314  * 8 or 16 bytes long, depending on the value of the LONGLBA flag,
6315  * and we can have more than one of them. In practice we expect to
6316  * get exactly one 8-byte block descriptor.
6317  *
6318  * We'll support the following cases:
6319  * LONGLBA LENGTH Contents
6320  * 0 0 No block descriptors
6321  * 0 8 One 8-byte block descriptor
6322  * 0 >8 Use the first 8-byte block descriptor, ignore the rest
6323  * 1 0 No block descriptors
6324  * 1 16 One 16-byte block descriptor
6325  * 1 >16 Use the first 16-byte block descriptor, ignore the rest
6326  *
6327  */
6328 
6329  if (block_descriptor_length != 0) {
6330  if (longlba) {
6331  /* Get one 16-byte block descriptor */
6332  block_size = (payload[12] << 24) | (payload[13] << 16)
6333  | (payload[14] << 8) | payload[15];
6334  } else {
6335  /* Get one 8-byte block descriptor */
6336  block_size = (payload[5] << 16) | (payload[6] << 8) | payload[7];
6337  }
6338 
6339 
6340 
6341  ocs_set_ramdisc_blocksize(io->ocs, ramdisc, block_size);
6342  }
6343 
6344  payload += block_descriptor_length;
6345 
6346  /* This is the end of block descriptor processing. Remaining code, TBD,
6347  * will do mode page processing.
6348  */
6349 
6350 
6351 
6352  return 0;
6353 }
6354 
6355 /**
6356  * @ingroup scsi_cmd
6357  * @brief Command data phase callback for the MODE SELECT command.
6358  *
6359  * @par Description
6360  * This function is called when the SCSI command data phase has completed following a MODE SELECT
6361  * command.
6362  *
6363  * @param io Pointer to the IO structure.
6364  * @param scsi_status Completion status.
6365  * @param flags Flags.
6366  * @param arg Application-specified argument.
6367  *
6368  * @return Returns 0 on success, or a negative error code value on failure.
6369  */
6370 static int32_t
6371 scsi_mode_select_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
6372 {
6373 
6374  api_trace();
6375 
6376  ocs_assert(io, -1);
6377  ocs_assert(io->ocs, -1);
6378  ocs_lock(&io->tgt_io.sm_lock);
6379  if (scsi_status == OCS_SCSI_STATUS_GOOD) {
6380  if (flags & OCS_SCSI_IO_CMPL) {
6382  }
6383  else if (flags & OCS_SCSI_IO_CMPL_RSP_SENT) {
6384  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_RESP_CMPL, NULL);
6385  }
6386  }
6387  else {
6388  ocs_log_test(io->ocs, "dataphase completed with errors %d\n", scsi_status);
6389  ocs_unlock(&io->tgt_io.sm_lock);
6390  return -scsi_status;
6391  }
6392  ocs_unlock(&io->tgt_io.sm_lock);
6393 
6394  return 0;
6395 }
6396 
6397 /**
6398  * @ingroup scsi_cmd
6399  * @brief Process the SCSI MODE SELECT command.
6400  *
6401  * @par Description
6402  * The SCSI MODE SELECT command is processed.
6403  *
6404  * @param io Pointer to the IO context.
6405  * @param cdb Pointer to the SCSI CDB.
6406  *
6407  * @return Returns the handler status for the command. SCSI_HANDLER_NOT_STARTED
6408  * and SCSI_CMD_NOT_SUPPORTED are errors and the IO should be freed.
6409  */
6410 static ocs_ramd_hndlr_rtn_e
6411 ocs_ramd_mode_select(ocs_io_t *io, uint8_t cdb[])
6412 {
6413  uint16_t parameter_list_length;
6414  uint32_t rc;
6415  ocs_scsi_sgl_t sgl;
6416 
6417  io->tgt_io.mode_select_opcode = cdb[0];
6418 
6419  if (cdb[0] == SCSI_MODE_SELECT6) {
6420  parameter_list_length = cdb[4];
6421  } else {
6422  parameter_list_length = (cdb[7] << 8) | cdb[8];
6423  }
6424 
6425  /* As per SPC-4 a parameter List length of 0 means no data transfer
6426  * so just return good status now.
6427  */
6428  if (parameter_list_length == 0) {
6429  return(ocs_ramd_good_status(io));
6430  }
6431 
6432  /* We need to initiate a DATA OUT phase to get the parameter list */
6433 
6434  /* Build an SGL that points to our buffer */
6435  rc = ocs_dma_alloc(io->ocs, &io->tgt_io.payload, parameter_list_length, 16);
6436  if (rc) {
6437  ocs_log_err(io->ocs, "ocs_scsi_dma_alloc failed\n");
6438  return SCSI_HANDLER_NOT_STARTED;
6439  }
6440  ocs_assert(io->tgt_io.payload.virt, -1);
6441 
6442  ocs_memset(&sgl, 0, sizeof(ocs_scsi_sgl_t));
6443  sgl.addr = io->tgt_io.payload.phys;
6444  sgl.len = parameter_list_length;
6445 
6446  /*
6447  * If we have already received the data in the first burst, then
6448  * just copy the data in to payload buffer and advance the
6449  * io state machine.
6450  */
6451  if (io->tgt_io.first_burst_bytes >= parameter_list_length) {
6452  ocs_ramd_first_burst_local(io, parameter_list_length);
6455  /* We should be in the response wait phase already,
6456  * so return that status again...
6457  */
6459  }
6460 
6461  rc = ocs_scsi_recv_wr_data(io, 0, NULL,
6462  &sgl, 1, parameter_list_length, scsi_mode_select_cb, NULL);
6463 
6464  if (rc == 0) {
6466  } else {
6467  ocs_log_test(io->ocs, "Failed to start data phase, rc is %d\n", rc);
6468  return SCSI_HANDLER_NOT_STARTED;
6469  }
6470 
6471 }
6472 
6473 /**
6474  * @ingroup scsi_cmd
6475  * @brief Abort all tasks associated with a node.
6476  *
6477  * @par Description
6478  * All the IOs associated with the @c node are aborted.
6479  *
6480  * @param node Pointer to the remote node.
6481  * @param myio Pointer to the IO requesting the abort.
6482  *
6483  * @return Returns 0 on success, or a negative error code value on failure.
6484  */
6485 static int32_t
6486 ocs_ramd_abort_node_tasks(ocs_node_t *node, ocs_io_t *myio)
6487 {
6488  ocs_io_t *io;
6489  ocs_io_t *ionext;
6490  ocs_list_t tmf_abort_list;
6491 
6492  /* Add IOs to be aborted to this list and trigger state machine outside active_ios lock*/
6493  ocs_list_init(&tmf_abort_list, ocs_io_t, tmf_abort_link);
6494 
6495  ocs_lock(&node->active_ios_lock);
6496  if (!ocs_list_empty(&node->active_ios)) {
6497  ocs_log_debug(node->ocs, "[%s] aborting IO's\n", node->display_name);
6498  ocs_list_foreach_safe(&node->active_ios, io, ionext) {
6499  if (io == myio) {
6500  continue;
6501  }
6502  if (!io->tmf_abort && ocs_ref_get_unless_zero(&io->ref)) {
6503  /* Mark the IO to indicate tmf_abort in progress */
6504  io->tmf_abort = TRUE;
6505  ocs_list_add_tail(&tmf_abort_list, io);
6506  }
6507  }
6508  }
6509  ocs_unlock(&node->active_ios_lock);
6510 
6511  ocs_list_foreach_safe(&tmf_abort_list, io, ionext) {
6512  ocs_list_remove(&tmf_abort_list, io);
6513  if (myio != NULL) {
6514  io_printf(myio, "aborting " IOFMT "\n", IOFMT_ARGS(io));
6515  } else {
6516  io_printf(io, "getting aborted\n");
6517  }
6518  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_ABORT_IO_NO_RESP, NULL);
6519  io->tmf_abort = FALSE;
6520  ocs_ref_put(&io->ref);
6521  }
6522  return 0;
6523 }
6524 
6525 /**
6526  * @ingroup scsi_cmd
6527  * @brief Abort tasks associated with a given node and LUN.
6528  *
6529  * @par Description
6530  * The tasks associated with a @c node and @c lun are aborted.
6531  *
6532  * @param node Pointer to the node.
6533  * @param lun LUN value.
6534  * @param myio Pointer to the IO requesting the abort.
6535  *
6536  * @return Returns 0 on success, or a negative error code value on failure.
6537  */
6538 static int32_t
6539 ocs_ramd_abort_node_tasks_lun(ocs_node_t *node, uint32_t lun, ocs_io_t *myio)
6540 {
6541  ocs_io_t *io;
6542  ocs_io_t *ionext;
6543  ocs_list_t tmf_abort_list;
6544 
6545  /* Add IOs to be aborted to this list and trigger state machine outside active_ios lock*/
6546  ocs_list_init(&tmf_abort_list, ocs_io_t, tmf_abort_link);
6547 
6548  ocs_lock(&node->active_ios_lock);
6549  if (!ocs_list_empty(&node->active_ios)) {
6550 
6551  ocs_log_debug(node->ocs, "[%s] aborting IO's for lun x%x\n", node->display_name, lun);
6552 
6553  ocs_list_foreach_safe(&node->active_ios, io, ionext) {
6554  if (io == myio) {
6555  continue;
6556  }
6557 
6558  if ( (io->tgt_io.lun == lun) && !io->tmf_abort && ocs_ref_get_unless_zero(&io->ref)) {
6559  /* Mark the IO to indicate tmf_abort in progress */
6560  io->tmf_abort = TRUE;
6561  ocs_list_add_tail(&tmf_abort_list, io);
6562  }
6563  }
6564  }
6565  ocs_unlock(&node->active_ios_lock);
6566 
6567  ocs_list_foreach_safe(&tmf_abort_list, io, ionext) {
6568  ocs_list_remove(&tmf_abort_list, io);
6569 
6570  io_printf(myio, "aborting " IOFMT "\n", IOFMT_ARGS(io));
6571  ocs_sm_post_event(&io->tgt_io.sm, OCS_EVT_ABORT_IO_NO_RESP, NULL);
6572  io->tmf_abort = FALSE;
6573  ocs_ref_put(&io->ref);
6574  }
6575 
6576  return 0;
6577 }
6578 
6579 void
6581 {
6582  int num_luns = 0;
6583 
6584  switch(type) {
6585  case OCS_SCSI_DDUMP_DEVICE: {
6586  int32_t i;
6587  ocs_t *ocs = obj;
6588  ocs_scsi_tgt_t *tgt_ocs = &ocs->tgt_ocs;
6589  ocs_ddump_value(textbuf, "max_sge", "%u", tgt_ocs->max_sge);
6590  ocs_ddump_value(textbuf, "max_sgl", "%u", tgt_ocs->max_sgl);
6591  ocs_ddump_value(textbuf, "ios_in_use", "%d", ACTIVE_IO_COUNT(ocs));
6592  ocs_ddump_value(textbuf, "io_high_watermark", "%d", ocs_atomic_read(&tgt_ocs->io_high_watermark));
6593  ocs_ddump_value(textbuf, "initiator_count", "%d", ocs_atomic_read(&tgt_ocs->initiator_count));
6594  ocs_ddump_value(textbuf, "watermark_max", "%d", tgt_ocs->watermark_max);
6595  ocs_ddump_value(textbuf, "watermark_min", "%d", tgt_ocs->watermark_min);
6596  ocs_ddump_value(textbuf, "watermark_hit", "%d", ocs_atomic_read(&tgt_ocs->watermark_hit));
6597  ocs_ddump_value(textbuf, "rdisc_count", "%d", tgt_ocs->rdisc_count);
6598 
6599  /* grab the lun count based on whether we have a global ramdisc */
6600  if (ocs->tgt_ocs.use_global_ramd) {
6601  num_luns = ocs->tgt_ocs.rdisc_count;
6602 
6603  for (i = 0; i < num_luns; i ++) {
6604  ocs_ddump_ramdisc(textbuf, ocs->tgt_ocs.rdisc[i]);
6605  }
6606  }
6607  ocs_ramd_thread_ddump(textbuf);
6608  break;
6609  }
6610  case OCS_SCSI_DDUMP_DOMAIN: {
6611 // ocs_domain_t *domain = obj;
6612  break;
6613  }
6614  case OCS_SCSI_DDUMP_SPORT: {
6615 // ocs_sport_t *sport = obj;
6616  break;
6617  }
6618  case OCS_SCSI_DDUMP_NODE: {
6619 // ocs_node_t *node = obj;
6620  break;
6621  }
6622  case OCS_SCSI_DDUMP_IO: {
6623  ocs_io_t *io = obj;
6624 
6625  ocs_ddump_value(textbuf, "current_state", "%s", ocs_ramd_state_names[io->tgt_io.current_state]);
6626  ocs_ddump_value(textbuf, "flags", "%#x", io->tgt_io.flags);
6627  ocs_ddump_value(textbuf, "lba", "%" PRIx64 , io->tgt_io.io_lba);
6628  ocs_ddump_value(textbuf, "command", "%d", io->tgt_io.cdb[0]);
6629  ocs_ddump_value(textbuf, "has_first_burst", "%d", io->tgt_io.has_first_burst);
6630  ocs_ddump_value(textbuf, "first_burst_bytes", "%d", io->tgt_io.first_burst_bytes);
6631  ocs_ddump_value(textbuf, "lun", "%d", io->tgt_io.lun);
6632  ocs_ddump_value(textbuf, "lun_is_valid", "%d", io->tgt_io.lun_is_valid);
6633  ocs_ddump_value(textbuf, "is_read", "%d", io->tgt_io.is_read);
6634  ocs_ddump_value(textbuf, "io_block_count", "%d", io->tgt_io.io_block_count);
6635  ocs_ddump_value(textbuf, "acc_block_count", "%d", io->tgt_io.acc_block_count);
6636  ocs_ddump_value(textbuf, "cur_lba", "%" PRIx64 , io->tgt_io.cur_lba);
6637  ocs_ddump_value(textbuf, "cur_block_count", "%d", io->tgt_io.cur_block_count);
6638 
6639  break;
6640  }
6641  default: {
6642  break;
6643  }
6644  }
6645 }
6646 
6647 /**
6648  * @ingroup io_sm
6649  * @brief State: ABTS: Wait for the IO to be aborted, and send response.
6650  *
6651  * @par Description
6652  * ABTS is waiting for the IO to be aborted. Once aborted, the
6653  * response (BA_ACC) is sent.
6654  *
6655  * @param ctx Remote node sm context.
6656  * @param evt Event to process.
6657  * @param arg Per event optional argument.
6658  *
6659  * @return Returns NULL.
6660  */
6661 static void *
6662 __ocs_ramd_abts_wait_io_abort_resp(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
6663 {
6664  ocs_io_t *tmfio;
6665 
6666  ocs_assert(ctx, NULL);
6667  ocs_assert(ctx->app, NULL);
6668  tmfio = ctx->app;
6669  ocs_assert(tmfio->node, NULL);
6670  ocs_assert(tmfio->ocs, NULL);
6671 
6672  tmfio_sm_trace();
6673  tmfio->tgt_io.current_state = __OCS_RAMD_ABTS_WAIT_IO_ABORT_RESP;
6674 
6675  switch(evt) {
6678  /* send BA_ACC */
6681  == 0) {
6682  /* transition to state to wait for BA_ACC to complete */
6684  } else {
6685  ocs_ramd_io_free(tmfio);
6686  }
6687  break;
6688  case OCS_EVT_ABORT_IO:
6689  case OCS_EVT_ABORT_IO_NO_RESP: {
6690  /* cannot abort an ABORT_XRI */
6691  ocs_ignore_abort_evt(evt, __func__, tmfio, (ocs_io_t *)arg);
6692  break;
6693  }
6694  default:
6695  __ocs_ramd_io_common(__func__, ctx, evt, arg);
6696  break;
6697  }
6698 
6699  return NULL;
6700 }
6701 
6702 /**
6703  * @ingroup io_sm
6704  * @brief State: ABTS: Wait for the BA_ACC to complete.
6705  *
6706  * @par Description
6707  * The IO has been aborted and a BA_ACC sent. Wait for the BA_ACC to
6708  * complete, and then free the ABTS IO.
6709  *
6710  * @param ctx Remote node sm context.
6711  * @param evt Event to process.
6712  * @param arg Per event optional argument.
6713  *
6714  * @return Returns NULL.
6715  */
6716 static void *
6717 __ocs_ramd_wait_abts_acc_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
6718 {
6719  ocs_io_t *tmfio;
6720 
6721  ocs_assert(ctx, NULL);
6722  ocs_assert(ctx->app, NULL);
6723  tmfio = ctx->app;
6724  ocs_assert(tmfio->node, NULL);
6725  ocs_assert(tmfio->ocs, NULL);
6726 
6727  tmfio_sm_trace();
6728  tmfio->tgt_io.current_state = __OCS_RAMD_WAIT_ABTS_ACC_CMPL;
6729 
6730  switch(evt) {
6731  case OCS_EVT_RESP_CMPL:
6732  ocs_ramd_io_free(tmfio);
6733  break;
6734  case OCS_EVT_ABORT_IO:
6735  case OCS_EVT_ABORT_IO_NO_RESP: {
6736  ocs_ignore_abort_evt(evt, __func__, tmfio, (ocs_io_t *)arg);
6737  break;
6738  }
6739  default:
6740  __ocs_ramd_io_common(__func__, ctx, evt, arg);
6741  break;
6742  }
6743 
6744  return NULL;
6745 }
6746 
6747 /**
6748  * @ingroup ramdisc
6749  * @brief Post a Unit Attention condition to a node/LUN (I_T nexus).
6750  *
6751  * @param node Pointer to a node structure.
6752  * @param lun LUN number.
6753  * @param asc Additional Sense Code.
6754  * @param ascq Additional Sense Code Qualifier.
6755  *
6756  * @return None.
6757  */
6758 static void post_one_ua(ocs_node_t *node, uint32_t lun, uint8_t asc, uint8_t ascq) {
6759  ocs_ramdisc_ua_queue_t *ua_queue;
6760  ocs_ramdisc_ua_t *ua_entry;
6761  ocs_ramdisc_ua_t *next;
6762  ocs_t *ocs = node->ocs;
6763 
6764  /* If the node is not an initiator it won't have any UA queues */
6765  if (lun >= node->tgt_node.num_ua_queues) {
6766  return;
6767  }
6768 
6769  /* Post the UA to the UA queue for the correct LUN */
6770  ua_queue = &node->tgt_node.ua_queues[lun];
6771 
6772 
6773 
6774  /*
6775  * SAM 5.14: the device server should clear pending unit attention conditions
6776  * that have the same additional sense code (i.e., the device server should not
6777  * add the same unit attention condition twice)
6778  */
6779  ocs_lock(&ua_queue->ua_queue_lock);
6780  ocs_list_foreach_safe(&ua_queue->entry_list, ua_entry, next) {
6781  if (ua_entry->asc == asc) {
6782  ocs_log_debug(ocs, "Removing old entry for asc 0x%02x for node %d lun %d\n",
6783  asc, node->instance_index, lun);
6784  ocs_list_remove(&ua_queue->entry_list, ua_entry);
6785 
6786  }
6787  }
6788  ocs_unlock(&ua_queue->ua_queue_lock);
6789 
6790  ua_entry = ocs_malloc(ocs, sizeof(*ua_entry), OCS_M_NOWAIT | OCS_M_ZERO);
6791  if (ua_entry != NULL) {
6792  ua_entry->asc = asc;
6793  ua_entry->ascq = ascq;
6794  ocs_list_add_tail(&ua_queue->entry_list, ua_entry);
6795  ocs_log_debug(ocs, "Posted UA %02x/%02x to node %d lun %d\n", asc, ascq, node->instance_index, lun);
6796  } else {
6797  ocs_log_err(ocs, "UA entry allocation failed for node %d, UA is dropped\n",
6798  node->instance_index);
6799  }
6800 }
6801 
6802 /**
6803  * @ingroup ramdisc
6804  * @brief Post a Unit Attention condition to a ramdisc.
6805  *
6806  * @par Description
6807  * Posts a Unit Attention condition to a ramdisc. If use_global_ramd is true,
6808  * then there is only one ramdisc and all nodes are attached to it. In this
6809  * case, this function posts the UA to all nodes. If use_global_ramd is false,
6810  * then there is a ramdisc per sport. In this case, this function posts the UA to
6811  * all nodes on that sport.
6812  * @n @n
6813  * If @c except_node points to a node structure, then that node does not get the UA
6814  * posted. This is used when the ramdisc has been formatted so that the UA can
6815  * be posted to all nodes other than the one that requested the format. See <i>SBC 4.10</i>.
6816  *
6817  * @param ocs Pointer to the ocs structure.
6818  * @param ramdisc Pointer to the ramdisc.
6819  * @param asc Additional Sense Code.
6820  * @param ascq Additional Sense Code Qualifier.
6821  * @param except_node Node which should @e not get the UA posted. May be NULL.
6822  *
6823  * @return None.
6824  */
6825 void
6826 ocs_ramd_post_ua(ocs_t *ocs, ocs_ramdisc_t *ramdisc, uint8_t asc, uint8_t ascq, ocs_node_t *except_node)
6827 {
6828  ocs_domain_t *domain;
6829  ocs_sport_t *sport;
6830  ocs_node_t *node;
6831 
6832 
6833  if (ocs->tgt_ocs.use_global_ramd) {
6834  /* Post the UA to all nodes on all sports */
6835  ocs_device_lock(ocs);
6836  ocs_list_foreach(&ocs->domain_list, domain) {
6837  ocs_domain_lock(domain);
6838  ocs_list_foreach(&domain->sport_list, sport) {
6839  ocs_sport_lock(sport);
6840  ocs_list_foreach(&sport->node_list, node) {
6841  if (node != except_node) {
6842  post_one_ua(node, ramdisc->lun, asc, ascq);
6843  }
6844  }
6845  ocs_sport_unlock(sport);
6846  }
6847  ocs_domain_unlock(domain);
6848  }
6849  ocs_device_unlock(ocs);
6850  } else {
6851  /* Post the UA to all nodes on the sport that this ramdisc is associated with */
6852  uint32_t i;
6853 
6854  ocs_device_lock(ocs);
6855  ocs_list_foreach(&ocs->domain_list, domain) {
6856  ocs_domain_lock(domain);
6857  ocs_list_foreach(&domain->sport_list, sport) {
6858  for (i=0;i<sport->tgt_sport.rdisc_count;i++) {
6859  if (sport->tgt_sport.rdisc[i] == ramdisc) {
6860  ocs_sport_lock(sport);
6861  ocs_list_foreach(&sport->node_list, node) {
6862  if (node != except_node) {
6863  post_one_ua(node, ramdisc->lun, asc, ascq);
6864  }
6865  }
6866  ocs_sport_unlock(sport);
6867  }
6868  }
6869  }
6870  ocs_domain_unlock(domain);
6871  }
6872  ocs_device_unlock(ocs);
6873  }
6874 }
6875 
6876 /**
6877  * @ingroup ramdisc
6878  * @brief Get a pending Unit Attention, if any.
6879  *
6880  * @par Description
6881  * Given a node and a LUN, check for a pending Unit Attention condition.
6882  * If there is a pending Unit Attention, the function fills in the
6883  * ASC and ASCQ for the UA and returns non-zero. If there are no pending
6884  * UA conditions, the function returns 0. If there is more than one
6885  * UA condition pending, the function returns the first one found.
6886  * @n @n
6887  * This function is on the performance path. It is called for every command
6888  * received from the initiator, including I/Os.
6889  *
6890  * @param ocs Pointer to the ocs structure.
6891  * @param node Pointer to the node structure.
6892  * @param lun Logical Unit Number.
6893  * @param asc Pointer to the location to store the Additional Sense Code.
6894  * @param ascq Pointer to the location to store the Additional Sense Code Qualifier.
6895  *
6896  * @return Returns 1 if a Unit Attention exists, or 0 if not.
6897  */
6898 int32_t
6899 ocs_ramd_get_ua(ocs_t *ocs, ocs_node_t *node, uint32_t lun, uint8_t *asc, uint8_t *ascq)
6900 {
6901  ocs_ramdisc_ua_queue_t *ua_queue;
6902  ocs_ramdisc_ua_t *ua_entry;
6903 
6904  /* If the node is not an initiator it won't have any UA queues */
6905  if (lun >= node->tgt_node.num_ua_queues) {
6906  return 0;
6907  }
6908 
6909  /* Locate the UA queue for this LUN */
6910  ua_queue = &node->tgt_node.ua_queues[lun];
6911 
6912  ocs_lock(&ua_queue->ua_queue_lock);
6913  if (ocs_list_empty(&ua_queue->entry_list)) {
6914  ocs_unlock(&ua_queue->ua_queue_lock);
6915  return 0;
6916  }
6917 
6918  ua_entry = ocs_list_get_head(&ua_queue->entry_list);
6919  ocs_unlock(&ua_queue->ua_queue_lock);
6920 
6921  *asc = ua_entry->asc;
6922  *ascq = ua_entry->ascq;
6923 
6924  return 1;
6925 
6926 }
6927 
6928 /**
6929  * @ingroup ramdisc
6930  * @brief Clear a Unit Attention condition.
6931  *
6932  * @par Description
6933  * Given a node, LUN, ASC, and ASCQ, this function clears a Unit Attention
6934  * condition from that node/LUN combination for the given ASC and ASCQ.
6935  *
6936  * @param ocs Pointer to the ocs structure.
6937  * @param node Pointer to the node structure.
6938  * @param lun Logical Unit Number.
6939  * @param asc Additional Sense Code.
6940  * @param ascq Additional Sense Code Qualifier.
6941  *
6942  * @return None.
6943  */
6944 void
6945 ocs_ramd_clear_ua(ocs_t *ocs, ocs_node_t *node, uint32_t lun, uint8_t asc, uint8_t ascq)
6946 {
6947  ocs_ramdisc_ua_queue_t *ua_queue;
6948  ocs_ramdisc_ua_t *ua_entry;
6949  ocs_ramdisc_ua_t *next;
6950 
6951  /* If the node is not an initiator it won't have any UA queues */
6952  if (lun >= node->tgt_node.num_ua_queues) {
6953  return;
6954  }
6955 
6956  /* Locate the UA queue for this LUN */
6957  ua_queue = &node->tgt_node.ua_queues[lun];
6958 
6959  ocs_lock(&ua_queue->ua_queue_lock);
6960  ocs_list_foreach_safe(&ua_queue->entry_list, ua_entry, next) {
6961  if ((ua_entry->asc == asc) && (ua_entry->ascq == ascq)) {
6962  ocs_list_remove(&ua_queue->entry_list, ua_entry);
6963  ocs_log_debug(ocs, "Removed UA %02x/%02x from node %d lun %d\n",
6964  asc, ascq, node->instance_index, lun);
6965  break;
6966  }
6967  }
6968  ocs_unlock(&ua_queue->ua_queue_lock);
6969 
6970 }
6971 
6972 void
6973 ocs_mgmt_ramd_list(ocs_textbuf_t *textbuf, void *object)
6974 {
6975  ocs_mgmt_start_unnumbered_section(textbuf, "tgt");
6976 
6977  ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "ramdisc_size");
6978  ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "ramdisc_blocksize");
6979  ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "num_luns");
6980  ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "p_type");
6981  ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "dif_separate");
6982  ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_EX, "read_block");
6983 
6984  ocs_mgmt_end_unnumbered_section(textbuf, "tgt");
6985 }
6986 
6987 int
6988 ocs_mgmt_ramd_get(ocs_textbuf_t *textbuf, char *parent, char *name, void *object)
6989 {
6990  int retval = -1;
6991  char qualifier[80];
6992  char prop_buf[32];
6993 
6994  ocs_mgmt_start_unnumbered_section(textbuf, "tgt");
6995 
6996  snprintf(qualifier, sizeof(qualifier), "%s/tgt", parent);
6997 
6998  /* If it doesn't start with my qualifier I don't know what to do with it */
6999  if (ocs_strncmp(name, qualifier, strlen(qualifier)) == 0) {
7000  char *unqualified_name = name + strlen(qualifier) +1;
7001 
7002  /* See if it's a value I can supply */
7003  if (ocs_strcmp(unqualified_name, "ramdisc_size") == 0) {
7004  ocs_get_property("ramdisc_size", prop_buf, sizeof(prop_buf));
7005  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "ramdisc_size", prop_buf);
7006  retval = 0;
7007  } else if (ocs_strcmp(unqualified_name, "ramdisc_blocksize") == 0) {
7008  ocs_get_property("ramdisc_blocksize", prop_buf, sizeof(prop_buf));
7009  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "ramdisc_blocksize", prop_buf);
7010  retval = 0;
7011  } else if (ocs_strcmp(unqualified_name, "num_luns") == 0) {
7012  ocs_get_property("num_luns", prop_buf, sizeof(prop_buf));
7013  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "num_luns", prop_buf);
7014  retval = 0;
7015  } else if (ocs_strcmp(unqualified_name, "dif_separate") == 0) {
7016  ocs_get_property("dif_separate", prop_buf, sizeof(prop_buf));
7017  ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "dif_separate", ocs_strtoul(prop_buf, NULL, 0));
7018  retval = 0;
7019  } else if (ocs_strcmp(unqualified_name, "p_type") == 0) {
7020  ocs_get_property("p_type", prop_buf, sizeof(prop_buf));
7021  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "p_type", prop_buf);
7022  retval = 0;
7023  }
7024  }
7025 
7026  ocs_mgmt_end_unnumbered_section(textbuf, "tgt");
7027 
7028  return retval;
7029 }
7030 
7031 void
7032 ocs_mgmt_ramd_get_all(ocs_textbuf_t *textbuf, void *object)
7033 {
7034  char prop_buf[32];
7035 
7036  ocs_mgmt_start_unnumbered_section(textbuf, "tgt");
7037 
7038  ocs_get_property("ramdisc_size", prop_buf, sizeof(prop_buf));
7039  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "ramdisc_size", prop_buf);
7040  ocs_get_property("ramdisc_blocksize", prop_buf, sizeof(prop_buf));
7041  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "ramdisc_blocksize", prop_buf);
7042  ocs_get_property("num_luns", prop_buf, sizeof(prop_buf));
7043  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "num_luns", prop_buf);
7044  ocs_get_property("p_type", prop_buf, sizeof(prop_buf));
7045  ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "p_type", prop_buf);
7046  ocs_get_property("dif_separate", prop_buf, sizeof(prop_buf));
7047  ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "dif_separate", ocs_strtoul(prop_buf, NULL, 0));
7048  ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_EX, "read_block");
7049 
7050  ocs_mgmt_end_unnumbered_section(textbuf, "tgt");
7051 }
7052 
7053 int
7054 ocs_mgmt_ramd_exec(char *parent, char *action_name, void *arg_in, uint32_t arg_in_length,
7055  void *arg_out, uint32_t arg_out_length, void *object)
7056 {
7057  char qualifier[80];
7058  int retval = -1;
7059  ocs_scsi_tgt_t *tgt_ocs = (ocs_scsi_tgt_t *)object;
7060  ocs_t *ocs;
7061 
7062  ocs = container_of(tgt_ocs, ocs_t, tgt_ocs);
7063 
7064  ocs_snprintf(qualifier, sizeof(qualifier), "%s/tgt", parent);
7065 
7066  /* If it doesn't start with my qualifier I don't know what to do with it */
7067  if (ocs_strncmp(action_name, qualifier, strlen(qualifier)) == 0) {
7068  char *unqualified_name = action_name + strlen(qualifier) +1;
7069 
7070  /* See if it's a value I can supply */
7071  if (ocs_strcmp(unqualified_name, "read_block") == 0) {
7072  retval = ocs_mgmt_read_block(ocs, unqualified_name, arg_in, arg_in_length,
7073  arg_out, arg_out_length);
7074  }
7075  }
7076 
7077  return retval;
7078 }
7079 
7080 static int32_t
7081 ocs_mgmt_read_block(ocs_t *ocs, char *name, void *arg_in, uint32_t arg_in_length, void *arg_out, uint32_t arg_out_length)
7082 {
7083  uint32_t tgt;
7084  uint32_t lun;
7085  uint32_t lba;
7086  char input_buffer[80];
7087  ocs_ramdisc_t *ramdisc;
7088  uint8_t *block;
7089  uint32_t length;
7090 
7091  if ((arg_in == NULL) ||
7092  (arg_in_length == 0) ||
7093  (arg_out == NULL) ||
7094  (arg_out_length == 0)) {
7095  return -1;
7096  }
7097 
7098  if (arg_in_length > sizeof(input_buffer)) {
7099  arg_in_length = sizeof(input_buffer);
7100  }
7101 
7102  if (ocs_copy_from_user(input_buffer, arg_in, arg_in_length)) {
7103  ocs_log_test(ocs, "Failed to copy addr from user\n");
7104  return -EFAULT;
7105  }
7106 
7107  if (ocs_sscanf(input_buffer, "%d:%d:%d", &tgt, &lun, &lba) != 3) {
7108  return -EFAULT;
7109  }
7110 
7111  ramdisc = ocs_ramd_lookup_by_tgt(ocs, tgt, lun);
7112  if (ramdisc == NULL) {
7113  return -EFAULT;
7114  }
7115 
7116  block = ocs_ramdisc_lba_address(ramdisc, lba);
7117 
7118  length = arg_out_length;
7119 
7120  if (ocs_copy_to_user(arg_out, block, length)) {
7121  ocs_log_test(ocs, "Failed to copy buffer to user\n");
7122  return -EFAULT;
7123  }
7124 
7125 
7126  return 0;
7127 }
7128 
7129 /**
7130  * @brief Initialize per port ramdisc structure.
7131  *
7132  * @par Description
7133  * The per port ramdisc persistence data structure is initialized.
7134  *
7135  * @param ocs pointer to OCS instance
7136  *
7137  * @return Returns 0 on success, or a negative error code value on failure.
7138  */
7139 int32_t
7141 {
7142  ocs_list_init(&ocs->tgt_ocs.port_rdisc_head, ocs_port_rdisc_t, link);
7143  return 0;
7144 }
7145 
7146 /**
7147  * @brief Free per port ramdisc structure
7148  *
7149  * @par Description
7150  * The per port ramdisc persistence data structure is freed along with the ramdisc
7151  * objects
7152  *
7153  * @param ocs Pointer to OCS instance.
7154  *
7155  * @return None.
7156  */
7157 void
7159 {
7160  ocs_port_rdisc_t *prd;
7161  ocs_port_rdisc_t *next;
7162 
7163  ocs_list_foreach_safe(&ocs->tgt_ocs.port_rdisc_head, prd, next) {
7164  ocs_list_remove(&ocs->tgt_ocs.port_rdisc_head, prd);
7165 
7166  ocs_scsi_ramd_free(ocs, &prd->rdisc, &prd->rdisc_count);
7167 
7168  ocs_free(ocs, prd, sizeof(*prd));
7169  }
7170 }
7171 
7172 /**
7173  * @brief Find a per port ramdisc instance
7174  *
7175  * @par Description
7176  * A per port ramdisc instance is searched for given its target id (tgt_id),
7177  * if found, the rdisc/rdisc_count arguments are updated with the persisted
7178  * ramdisc information.
7179  *
7180  * @param ocs Pointer to OCS structure.
7181  * @param tgt_id Target id.
7182  * @param rdisc Pointer to ramdiscs.
7183  * @param rdisc_count Count of ramdiscs.
7184  *
7185  * @return Returns 0 on success, or a negative error code value on failure.
7186  */
7187 int32_t
7188 ocs_port_rdisc_find(ocs_t *ocs, uint32_t tgt_id, ocs_ramdisc_t ***rdisc, uint32_t *rdisc_count)
7189 {
7190  ocs_port_rdisc_t *prd;
7191  int32_t rc = -1;
7192 
7193  ocs_list_foreach(&ocs->tgt_ocs.port_rdisc_head, prd) {
7194  if (prd->tgt_id == tgt_id) {
7195  *rdisc = prd->rdisc;
7196  *rdisc_count = prd->rdisc_count;
7197  rc = 0;
7198  break;
7199  }
7200  }
7201 
7202  return rc;
7203 }
7204 
7205 /**
7206  * @brief Save a per port ramdisc.
7207  *
7208  * @par Description
7209  * The ramdisc instance given by rdisc/rdisc_count is saved in the per port
7210  * ramdisc persist structure.
7211  *
7212  * @param ocs Pointer to OCS structure.
7213  * @param tgt_id Target id for this ramdisc.
7214  * @param rdisc Pointer to ramdiscs.
7215  * @param rdisc_count Count of ramdiscs.
7216  *
7217  * @return Returns 0 for success, or a negative error code value on failure.
7218  */
7219 int32_t
7220 ocs_port_rdisc_save(ocs_t *ocs, uint32_t tgt_id, ocs_ramdisc_t **rdisc, uint32_t rdisc_count)
7221 {
7222  ocs_port_rdisc_t *port_rdisc;
7223 
7224  port_rdisc = ocs_malloc(ocs, sizeof(*port_rdisc), OCS_M_ZERO|OCS_M_NOWAIT);
7225  if (port_rdisc == NULL) {
7226  ocs_log_err(NULL, "ocs_malloc failed\n");
7227  return -1;
7228  }
7229  port_rdisc->tgt_id = tgt_id;
7230  port_rdisc->rdisc = rdisc;
7231  port_rdisc->rdisc_count = rdisc_count;
7232  ocs_list_add_tail(&ocs->tgt_ocs.port_rdisc_head, port_rdisc);
7233  return 0;
7234 }
7235 
7236 #if defined(OCS_INCLUDE_FC)
7237 /**
7238  * @brief Cancel HAL IOs
7239  */
7240 void ocs_scsi_tgt_cancel_io(ocs_t *ocs)
7241 {
7242  /* Flush the WCQE's before cleaning-up the io_inuse list */
7243  ocs_hal_flush(&ocs->hal);
7244  ocs_hal_io_cancel(&ocs->hal);
7245 }
7246 
7247 /*
7248  * @brief Wait for all sessions are completely deteled.
7249  */
7250 void ocs_scsi_tgt_sess_del_wait(ocs_sport_t *sport)
7251 {
7252  /* Nothing to do in RAMD backend, as session deletion
7253  * will be serialized.
7254  */
7255 }
7256 
7257 /*
7258  * @brief Count number of initiators logged in to sport
7259  * to use this count reference to wait till
7260  * all the sessions are shutdown.
7261  */
7262 void ocs_scsi_tgt_count_sessions(ocs_t *ocs, uint64_t wwpn)
7263 {
7264 
7265  /*
7266  * Nothing to do in RAMD backend, as session deletion
7267  * will be serialized.
7268  */
7269 }
7270 
7271 /*
7272  * @Brief Bring target port online when target is enabled
7273  */
7274 int32_t ocs_scsi_tgt_port_online(ocs_t *ocs)
7275 {
7276  int32_t rc = 0;
7277 
7278  if (ocs->holdoff_link_online == 0) {
7279  ocs_log_debug(ocs, "Restore port ONLINE\n");
7280  rc = ocs_xport_control(ocs->xport, OCS_XPORT_PORT_ONLINE);
7281  if (rc)
7282  ocs_log_err(ocs, "Failed to bring port online\n");
7283  }
7284  return rc;
7285 }
7286 #endif
int32_t ocs_scsi_tgt_port_online(ocs_t *ocs)
#define OCS_WATERMARK_HIGH_PCT
static int32_t ocs_ramd_data_phase_start(ocs_io_t *io)
Common code to start a SCSI data phase once the IO has been set up.
Definition: ocs_ramd.c:4662
uint32_t uint64_t lba
Definition: ocs_scsi.h:233
ocs_sport_t * ocs_sport_get_instance(ocs_domain_t *domain, uint32_t index)
Return a SLI port object, given an instance index.
Definition: ocs_sport.c:336
uint32_t check_guard
Definition: ocs_scsi.h:233
ocs_mgmt_functions_t ramd_mgmt_functions
Definition: ocs_ramd.c:314
int32_t ocs_hal_flush(ocs_hal_t *hal)
Definition: ocs_hal.c:11164
#define OCS_SCSI_FIRST_BURST_ERR
Definition: ocs_scsi.h:54
#define SCSI_ASC(x)
Definition: scsi_asc_ascq.h:48
#define tmfio_sm_trace()
Definition: ocs_ramd.c:87
uint32_t disable_app_ffff
Definition: ocs_scsi.h:233
static ocs_ramd_hndlr_rtn_e ocs_ramd_complete_write_same(ocs_io_t *io)
Command completion for the SCSI WRITE SAME command.
Definition: ocs_ramd.c:4245
uint8_t scsi_status
Definition: ocs_scsi.h:92
A structure used to map a handler function to a SCSI sommand.
Definition: ocs_ramd.c:245
int32_t ocs_new_ramdisc(ocs_t *ocs, ocs_ramdisc_t *ramdisc, uint32_t block_size, uint32_t blocks_per_segment, uint64_t block_count)
Create a new ramdisc instance (LUN).
Definition: ocs_ramdisc.c:72
#define INQ_VPD_BLOCKS_LIMIT_PAGE_B0
Definition: ocs_ramd.c:5455
#define SCSI_STATUS_BUSY
Definition: scsi_cmds.h:267
static int32_t ocs_io_busy(ocs_io_t *io)
Test if IO object is busy.
Definition: ocs_io.h:191
A structure used to create the inquiry page 83 data.
Definition: ocs_ramd.c:5568
target private ocs structure
Definition: ocs_scsi_tgt.h:51
ocs_scsi_tmf_cmd_e
Definition: ocs_scsi.h:138
Defines the standard sense response bits per the SCSI SPC specification.
Definition: ocs_ramd.c:1167
uint8_t peripheral_device_type
Definition: ocs_ramd.c:5476
#define SCSI_ASC_CAPACITY_DATA_HAS_CHANGED
ocs_scsi_tmf_resp_e
Definition: ocs_scsi.h:150
int32_t ocs_scsi_tgt_io_exit(ocs_io_t *io)
Un-initialize SCSI IO.
Definition: ocs_ramd.c:450
int32_t ocs_scsi_recv_tmf(ocs_io_t *tmfio, uint32_t lun, ocs_scsi_tmf_cmd_e cmd, ocs_io_t *abortio, uint32_t flags)
Receive a TMF command IO.
Definition: ocs_ramd.c:2682
int32_t ocs_scsi_tgt_del_device(ocs_t *ocs)
Tears down the target members of the ocs structure.
Definition: ocs_ramd.c:836
#define SNSCODE_INV_PRM
Definition: ocs_ramd.h:59
ocs_list_t ocs_list_link_t
Definition: ocs_list.h:84
uint8_t uint8_t additional_len
Definition: ocs_ramd.c:5481
int32_t ocs_scsi_tgt_new_device(ocs_t *ocs)
Initializes any target fields on the ocs structure.
Definition: ocs_ramd.c:753
int32_t ocs_scsi_tgt_driver_init(void)
Driver-wide initialization for the target-server.
Definition: ocs_ramd.c:354
static void * __ocs_ramd_io_wait_and_complete_writesame_dataphase(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Wait for the data phase to complete following a WRITE SAME command.
Definition: ocs_ramd.c:2017
int32_t ocs_port_rdisc_init(ocs_t *ocs)
Initialize per port ramdisc structure.
Definition: ocs_ramd.c:7140
#define SCSI_RELEASE6
Definition: scsi_cmds.h:79
A structure used to create the inquiry page B0 data.
Definition: ocs_ramd.c:5640
void ocs_mgmt_ramd_list(ocs_textbuf_t *textbuf, void *object)
Definition: ocs_ramd.c:6973
uint8_t * ocs_ramdisc_lba_address(ocs_ramdisc_t *ramdisc, uint64_t lba)
Return the address of a ramdisc logical block.
Definition: ocs_ramdisc.c:577
A structure used to create the inquiry page 80 data.
Definition: ocs_ramd.c:5556
int32_t ocs_scsi_tgt_io_init(ocs_io_t *io)
Initialize SCSI IO.
Definition: ocs_ramd.c:433
void ocs_ramd_tgt_bounce(void(*fctn)(void *arg), void *arg, uint32_t s_id, uint32_t d_id, uint32_t ox_id)
#define DATA_DIR_WRITE
Definition: ocs_ramd.c:254
uint16_t optimal_transfer_length_granularity
Definition: ocs_ramd.c:5648
uint32_t ocs_scsi_tgt_io_state(ocs_io_t *io)
Set SCST IO state.
Definition: ocs_ramd.c:415
static void ocs_scsi_ramd_free(ocs_t *ocs, ocs_ramdisc_t ***rdisc, uint32_t *rdisc_count)
Free the memory associate with a ramdisc.
Definition: ocs_ramd.c:719
#define SNSCODE_OFF_ERR
Definition: ocs_ramd.h:61
#define SCSI_INQUIRY
Definition: scsi_cmds.h:48
uint8_t product_id[16]
Definition: ocs_ramd.c:5505
static int32_t ocs_list_empty(ocs_list_t *list)
Test if a list is empty.
Definition: ocs_list.h:125
T10 DIF information passed to the transport.
Definition: ocs_scsi.h:229
ocs_domain_t * ocs_domain_get_instance(ocs_t *ocs, uint32_t index)
Return a pointer to the domain, given the instance index.
Definition: ocs_domain.c:414
#define SCSI_ASC_POWER_ON_RESET_OR_BUS_DEVICE_RESET_OCCURRED
uintptr_t addr
Definition: ocs_scsi.h:219
static int32_t ocs_ramd_abort_node_tasks(ocs_node_t *nodes, ocs_io_t *io)
Abort all tasks associated with a node.
Definition: ocs_ramd.c:6486
#define SNSCODE_ABORTED_CMD
Definition: ocs_ramd.h:60
static int32_t ocs_ramd_abort_tgt_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
Command abort callback.
Definition: ocs_ramd.c:1814
static ocs_ramd_hndlr_rtn_e ocs_ramd_read12(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ (12) command.
Definition: ocs_ramd.c:4422
static void * __ocs_ramd_io_wait_first_burst_send_resp(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Wait on first burst then send response.
Definition: ocs_ramd.c:2079
uint32_t phys_block_size
Definition: ocs_ramdisc.h:91
#define ACTIVE_IO_COUNT(ocs)
Definition: ocs_ramd.c:135
static void * __ocs_ramd_io_init(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Initial IO state.
Definition: ocs_ramd.c:1588
int32_t watermark_max
Definition: ocs_scsi_tgt.h:66
#define SNSCODEQUAL_INV_CMDIU
Definition: ocs_ramd.h:65
ocs_scsi_ddump_type_e
Definition: ocs_scsi.h:366
int32_t _scsi_io_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
Response phase callback.
Definition: ocs_ramd.c:2846
void ocs_scsi_tgt_count_sessions(ocs_t *ocs, uint64_t wwpn)
int32_t ocs_scsi_tgt_driver_exit(void)
Driver-wide clean-up for the target-server.
Definition: ocs_ramd.c:401
A structure used to create the inquiry page 00 data.
Definition: ocs_ramd.c:5544
int32_t ocs_ramdisc_sgl(ocs_t *ocs, ocs_ramdisc_t *ramdisc, ocs_ramdisc_sgl_t *sgl, uint32_t max_sgl, uint32_t max_sge_size, uint32_t lba, uint32_t *block_count, uint32_t req_flags, uint8_t dump_buffer)
Build an SGL list for blocks in a ramdisc.
Definition: ocs_ramdisc.c:218
#define SCSI_VAR_CMD_SVC_ACT_WRITE_SAME32
Definition: scsi_cmds.h:158
static void * __ocs_ramd_io_wait_first_burst(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Waiting for first burst data.
Definition: ocs_ramd.c:1725
static void * ocs_list_get_head(ocs_list_t *list)
Return the first item in the list.
Definition: ocs_list.h:292
static ocs_lock_t ramd_lock
Definition: ocs_ramd.c:59
static ocs_ramd_hndlr_rtn_e ocs_ramd_write32(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE (32) command.
Definition: ocs_ramd.c:3162
static ocs_ramd_hndlr_rtn_e ocs_ramd_report_luns(ocs_io_t *io, uint8_t cdb[])
Process the SCSI REPORT LUNS command.
Definition: ocs_ramd.c:6161
#define SCSI_RESERVE6
Definition: scsi_cmds.h:86
int32_t ocs_scsi_tgt_new_sport(ocs_sport_t *sport)
Accept a new SLI Port notification.
Definition: ocs_ramd.c:924
uint32_t sense_data_length
Definition: ocs_scsi.h:97
#define SNS_UNIT_ATTENTION
Definition: ocs_ramd.h:50
static int32_t ocs_ramd_first_burst_no_dif(ocs_io_t *io, ocs_ramdisc_t *ramdisc)
Process the first burst data without any DIF information.
Definition: ocs_ramd.c:3550
static void * __ocs_ramd_io_aborting_wait_io_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Aborting IO.
Definition: ocs_ramd.c:2409
ocs_ramdisc_ua_t is a collection of UNIT ATTENTION conditions
Definition: ocs_scsi_tgt.h:112
#define INQ_VPD_BLOCK_DEV_CHARS_PAGE_B1
Definition: ocs_ramd.c:5456
void ocs_ramd_clear_ua(ocs_t *ocs, ocs_node_t *node, uint32_t lun, uint8_t asc, uint8_t ascq)
Clear a Unit Attention condition.
Definition: ocs_ramd.c:6945
static void ocs_ramd_set_dif_prot_type_fields(ocs_t *ocs, ocs_ramdisc_t *ramdisc, ocs_scsi_dif_info_t *dif_info)
Set the Disable App and reference fields based on the protection type.
Definition: ocs_ramd.c:3618
#define SNSCODE_LBA_OOR
Definition: ocs_ramd.h:57
A structure used to create READ CAPACITY (16) parameter data.
Definition: ocs_ramd.c:5338
#define SCSI_MODE_SELECT10
Definition: scsi_cmds.h:53
uint8_t vendor_specific_extension_identifier[5]
Definition: ocs_ramd.c:5595
static ocs_ramd_hndlr_rtn_e ocs_ramd_call_cmd_handler(ocs_io_t *io)
Function to call the command handler for a CDB.
Definition: ocs_ramd.c:1531
uint32_t dif_separate
Definition: ocs_scsi.h:233
#define MGMT_MODE_EX
Definition: ocs_mgmt.h:49
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_long10(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE LONG (10) command.
Definition: ocs_ramd.c:3244
#define FC_TYPE_FCP
Definition: ocs_fcp.h:56
void ocs_scsi_tgt_del_domain(ocs_domain_t *domain)
Accept a domain lost notification.
Definition: ocs_ramd.c:902
static int32_t ocs_scsi_ramd_create(ocs_t *ocs, ocs_ramdisc_t ***rdisc, uint32_t *rdisc_count)
Create a RAMD (target) based on the property parameter values.
Definition: ocs_ramd.c:553
static void ocs_device_lock(ocs_t *ocs)
Definition: ocs_drv_fc.h:218
int32_t _ocs_scsi_recv_cmd(ocs_io_t *io, uint32_t lun, uint8_t *cdb, uint32_t cdb_len, uint32_t flags)
Receive an FCP SCSI command.
Definition: ocs_ramd.c:1207
#define OCS_SCSI_DIF_OPER_PASS_THRU
Definition: ocs_scsi.h:198
static void ocs_ramd_io_free(ocs_io_t *io)
Perform ramd IO free.
Definition: ocs_ramd.c:1150
const char * name
Definition: ocs_ddump.h:54
#define SCSI_FORMAT_UNIT
Definition: scsi_cmds.h:47
static uint32_t thread_cmds
uint8_t response_data_format
Definition: ocs_ramd.c:5481
uint32_t rdisc_count
Definition: ocs_scsi_tgt.h:71
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_long16(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE LONG (16) command.
Definition: ocs_ramd.c:3291
#define OCS_SCSI_LOW_LATENCY
Definition: ocs_scsi.h:60
uint32_t ref_tag
Definition: ocs_ramdisc.h:54
uint32_t maximum_write_same_length_hi
Definition: ocs_ramd.c:5657
generic scatter-gather list structure
Definition: ocs_scsi.h:218
void ocs_set_ramdisc_blocksize(ocs_t *ocs, ocs_ramdisc_t *ramdisc, uint32_t block_size)
Set the ramdisc block size.
Definition: ocs_ramdisc.c:537
static void * __ocs_ramd_io_wait_resp_compl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Wait for the response to complete.
Definition: ocs_ramd.c:2174
static int32_t ocs_ramd_abort_node_tasks_lun(ocs_node_t *node, uint32_t lun, ocs_io_t *io)
Abort tasks associated with a given node and LUN.
Definition: ocs_ramd.c:6539
int32_t _scsi_dataphase_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
Command data phase callback.
Definition: ocs_ramd.c:2875
static ocs_ramd_hndlr_rtn_e ocs_ramd_inquiry(ocs_io_t *io, uint8_t cdb[])
Process the SCSI INQUIRY command.
Definition: ocs_ramd.c:5692
uint8_t uint8_t uint8_t t10_vendor_id[8]
Definition: ocs_ramd.c:5492
void ocs_mgmt_start_unnumbered_section(ocs_textbuf_t *textbuf, const char *name)
Generate the beginning of an unnumbered section in a management XML document.
#define OCS_WATERMARK_LOW_PCT
static int32_t ocs_ramd_first_burst_with_dif(ocs_io_t *io, ocs_ramdisc_t *ramdisc, uint32_t data_blk_size)
Process the first burst data by adding/verifying/stripping DIF information.
Definition: ocs_ramd.c:3665
int32_t ocs_scsi_del_initiator(ocs_node_t *node, ocs_scsi_del_initiator_reason_e reason)
Delete a SCSI initiator node.
Definition: ocs_ramd.c:1092
ramdisc instance structure
Definition: ocs_ramdisc.h:88
#define api_trace(...)
Definition: ocs_ramd.c:122
A structure that contains the status and extended status fields.
Definition: ocs_ramd.c:53
#define SCSI_WRITE_SAME16
Definition: scsi_cmds.h:112
int const char const char void ocs_mgmt_emit_boolean(ocs_textbuf_t *textbuf, int access, const char *name, const int value)
Generate a property with a boolean value in a management XML document.
A structure used to create the device ID data for inquiry page 83.
Definition: ocs_ramd.c:5578
int32_t ocs_ramd_get_ua(ocs_t *ocs, ocs_node_t *node, uint32_t lun, uint8_t *asc, uint8_t *ascq)
Get a pending Unit Attention, if any.
Definition: ocs_ramd.c:6899
#define OCS_SCSI_CMD_DIR_OUT
Definition: ocs_scsi.h:48
static void * __ocs_ramd_io_aborting_wait_abort_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Aborting IO.
Definition: ocs_ramd.c:2363
static void ocs_domain_unlock(ocs_domain_t *domain)
Definition: ocs_domain.h:70
static ocs_ramd_hndlr_rtn_e ocs_ramd_test_unit_ready(ocs_io_t *io, uint8_t cdb[])
Process the SCSI TEST UNIT READY command.
Definition: ocs_ramd.c:6102
uint32_t max_sgl
Definition: ocs_scsi_tgt.h:53
int32_t ocs_del_ramdisc(ocs_t *ocs, ocs_ramdisc_t *ramdisc)
Delete a ramdisc instance.
Definition: ocs_ramdisc.c:151
#define OCS_SCSI_NO_AUTO_RESPONSE
Definition: ocs_scsi.h:59
The Designator used by inquiry page 83. This is an EUI-64 based 12-byte identifier.
Definition: ocs_ramd.c:5593
ocs_scsi_del_initiator_reason_e
Definition: ocs_scsi.h:284
static ocs_ramd_hndlr_rtn_e ocs_ramd_read10(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ (10) command.
Definition: ocs_ramd.c:4383
#define SCSI_ASCQ(x)
Definition: scsi_asc_ascq.h:49
static void * ocs_list_remove(ocs_list_t *list, void *item)
Remove an item from the list.
Definition: ocs_list.h:385
uint32_t rdisc_count
Definition: ocs_ramd.c:68
#define SNSCODE_DIF_ERR
Definition: ocs_ramd.h:55
#define SCSI_VARIABLE_LEN_CDB
Definition: scsi_cmds.h:102
static void * __ocs_ramd_io_aborting(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Aborting IO.
Definition: ocs_ramd.c:2282
uint8_t uint8_t rsvd57
Definition: ocs_ramd.c:5508
static ocs_ramd_hndlr_rtn_e ocs_ramd_read_capacity16(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ CAPACITY (16) command.
Definition: ocs_ramd.c:5367
uint8_t protection_information_length
Definition: ocs_ramd.c:5530
int32_t ocs_scsi_recv_wr_data(ocs_io_t *io, uint32_t flags, ocs_scsi_dif_info_t *dif_info, ocs_scsi_sgl_t *sgl, uint32_t sgl_count, uint64_t wire_len, ocs_scsi_io_cb_t cb, void *arg)
Definition: ocs_scsi.c:1639
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same10(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE SAME (10) command.
Definition: ocs_ramd.c:3343
void ocs_scsi_tgt_del_sport(ocs_sport_t *sport)
Accept a SLI Port gone notification.
Definition: ocs_ramd.c:974
uint32_t check_app_tag
Definition: ocs_scsi.h:233
#define SGL_REQ_FLAGS_PHYS
Definition: ocs_ramdisc.h:129
const char * ocs_sm_event_name(ocs_sm_event_t evt)
Definition: ocs_sm.c:92
static scsi_cmd_table_t * scsi_cmd_lookup[256]
Definition: ocs_ramd.c:256
int32_t ocs_scsi_recv_cmd_common(ocs_io_t *io, uint32_t lun, uint8_t *cdb, uint32_t cdb_len, uint32_t flags, uint8_t has_first_burst, ocs_dma_t first_burst_buffers[], uint32_t first_burst_bytes)
Common code used by ocs_scsi_recv_cmd() and ocs_scsi_recv_cmd_first_burst().
Definition: ocs_ramd.c:1302
uint8_t vendor_specific_parameters[0]
Definition: ocs_ramd.c:5522
int32_t ocs_xport_control(ocs_xport_t *xport, ocs_xport_ctrl_e cmd,...)
Perform transport attach function.
Definition: ocs_xport.c:866
SCSI command response.
Definition: ocs_scsi.h:91
uint32_t maximum_write_same_length_lo
Definition: ocs_ramd.c:5658
#define ocs_list_init(head, type, link)
Definition: ocs_list.h:113
ocs_ramd_hndlr_rtn_e ocs_ramd_mode_sense10(ocs_io_t *io, uint8_t cdb[])
Process SCSI Mode Sense 10 command.
Definition: mode_sense.c:396
#define SCSI_VAR_CMD_SVC_ACT_WRITE32
Definition: scsi_cmds.h:156
uint32_t tgt_id
Definition: ocs_ramd.c:66
uint16_t crc
Definition: ocs_ramdisc.h:52
void ocs_ramd_stop_threads(void)
static ocs_ramd_hndlr_rtn_e ocs_ramd_format_unit(ocs_io_t *io, uint8_t cdb[])
Process the SCSI FORMAT UNIT command.
Definition: ocs_ramd.c:5156
#define scsi_pack_result(key, code, qualifier)
Definition: ocs_ramd.h:68
int32_t ocs_scsi_send_rd_data(ocs_io_t *io, uint32_t flags, ocs_scsi_dif_info_t *dif_info, ocs_scsi_sgl_t *sgl, uint32_t sgl_count, uint64_t wire_len, ocs_scsi_io_cb_t cb, void *arg)
Definition: ocs_scsi.c:1629
ocs_ramd_hndlr_rtn_e(* handler)(ocs_io_t *io, uint8_t cdb[])
Definition: ocs_ramd.c:249
int32_t ocs_port_rdisc_save(ocs_t *ocs, uint32_t tgt_id, ocs_ramdisc_t **rdisc, uint32_t rdisc_count)
Save a per port ramdisc.
Definition: ocs_ramd.c:7220
#define OCS_SCSI_IO_CMPL_RSP_SENT
Definition: ocs_scsi.h:134
#define SCSI_SERVICE_ACTION_OUT16
Definition: scsi_cmds.h:94
void ocs_ramd_thread_ddump(ocs_textbuf_t *textbuf)
#define DATA_DIR_READ
Definition: ocs_ramd.c:253
int32_t ocs_scsi_get_block_vaddr(ocs_io_t *io, uint64_t blocknumber, ocs_scsi_vaddr_len_t addrlen[], uint32_t max_addrlen, void **dif_vaddr)
Return address of data and DIF data given a block in the target.
Definition: ocs_ramd.c:1269
#define SCSI_SERVICE_ACTION_IN16
Definition: scsi_cmds.h:93
static void * __ocs_ramd_io_common(const char *funcname, ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Common IO event handler.
Definition: ocs_ramd.c:2458
#define INQ_VPD_UNIT_SERIAL_NO_PAGE_80
Definition: ocs_ramd.c:5452
static ocs_ramd_hndlr_rtn_e ocs_ramd_read_capacity10(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ CAPACITY (10) command.
Definition: ocs_ramd.c:5280
#define SCSI_WRITE_AND_VERIFY16
Definition: scsi_cmds.h:108
void ocs_scsi_update_first_burst_transferred(ocs_io_t *io, uint32_t transferred)
Update transferred count.
Definition: ocs_scsi.c:3006
uint8_t prot_en
Definition: ocs_ramdisc.h:99
uint32_t scsi_cmd
Definition: ocs_ramd.c:248
uint8_t data_format_version
Definition: ocs_ramd.c:5531
#define INQ_VPD_SUPPORTED_PAGE_00
Definition: ocs_ramd.c:5451
A structure used to create the standard inquiry data.
Definition: ocs_ramd.c:5475
void ocs_scsi_register_bounce(ocs_t *ocs, void(*fctn)(void(*fctn)(void *arg), void *arg, uint32_t s_id, uint32_t d_id, uint32_t ox_id))
Register bounce callback for multi-threading.
Definition: ocs_scsi.c:3023
#define SCSI_READ16
Definition: scsi_cmds.h:72
void ocs_ramd_post_ua(ocs_t *ocs, ocs_ramdisc_t *ramdisc, uint8_t asc, uint8_t ascq, ocs_node_t *except_node)
Post a Unit Attention condition to a ramdisc.
Definition: ocs_ramd.c:6826
uint32_t check_ref_tag
Definition: ocs_scsi.h:233
ocs_ramd_hndlr_rtn_e ocs_ramd_mode_sense6(ocs_io_t *io, uint8_t cdb[])
Process SCSI Mode Sense 6 command.
Definition: mode_sense.c:379
#define ocs_list_foreach_safe(list, item, nxt)
Iterate a linked list safely.
Definition: ocs_list.h:446
void ocs_mgmt_end_unnumbered_section(ocs_textbuf_t *textbuf, const char *name)
Generate the end of a section in a management XML document.
uint32_t maximum_prefetch_xdread_xdwrite_transfer_length
Definition: ocs_ramd.c:5651
static void * __ocs_ramd_tmf_wait_resp_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Wait for the TMF response to complete.
Definition: ocs_ramd.c:2214
static void ocs_device_unlock(ocs_t *ocs)
Definition: ocs_drv_fc.h:223
generic ramdisc scatter-gather list
Definition: ocs_ramdisc.h:119
#define SCSI_WRITE_SAME10
Definition: scsi_cmds.h:111
#define io_printf(io, fmt,...)
Definition: ocs_ramd.c:95
#define OCS_SCSI_LAST_DATAPHASE
Definition: ocs_scsi.h:58
ocs_ramdisc_t * ocs_ramd_lookup_by_tgt(ocs_t *ocs, uint32_t tgt, uint32_t lun)
Given an TGT number and a LUN number, return the corresponding ramdisc.
Definition: ocs_ramd.c:519
static ocs_ramd_hndlr_rtn_e ocs_ramd_read6(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ (6) command.
Definition: ocs_ramd.c:4339
int32_t ocs_ramd_start_threads(uint32_t threading_mode, uint32_t arg_thread_cmds)
#define SCSI_ASC_LOGICAL_UNIT_NOT_SUPPORTED
static scsi_cmd_table_t scsi_cmd_table_variable_len_cdb[]
Definition: ocs_ramd.c:301
#define OCS_SCSI_IO_CMPL
Definition: ocs_scsi.h:133
#define INQ_VPD_DEVICE_ID_PAGE_83
Definition: ocs_ramd.c:5453
static ocs_ramd_hndlr_rtn_e ocs_ramd_read_common(ocs_io_t *io, uint64_t lba, uint32_t block_count, uint8_t rdprotect, uint32_t app_tag, uint32_t ref_tag)
Common code used for all SCSI READ commands.
Definition: ocs_ramd.c:4768
#define OCS_SCSI_FIRST_BURST_ABORTED
Definition: ocs_scsi.h:55
#define SCSI_VAR_CMD_SVC_ACT_WRITE_VERIFY32
Definition: scsi_cmds.h:157
#define SCSI_READ_LONG10
Definition: scsi_cmds.h:69
static ocs_ramd_hndlr_rtn_e ocs_ramd_read_long10(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ LONG (10) command.
Definition: ocs_ramd.c:4574
#define INQ_VPD_EXT_INQ_DATA_PAGE_86
Definition: ocs_ramd.c:5454
int32_t ocs_scsi_tgt_new_domain(ocs_domain_t *domain)
Accept a new domain notification.
Definition: ocs_ramd.c:880
#define SCSI_REQUEST_SENSE
Definition: scsi_cmds.h:84
static ocs_ramd_hndlr_rtn_e ocs_ramd_read16(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ (16) command.
Definition: ocs_ramd.c:4462
#define SNSCODE_BAD_CMD
Definition: ocs_ramd.h:56
#define OCS_SCSI_WQ_CLASS(x)
Definition: ocs_scsi.h:73
#define SCSI_MODE_SENSE10
Definition: scsi_cmds.h:55
int32_t ocs_scsi_new_initiator(ocs_node_t *node)
Receive notification of a new SCSI initiator node.
Definition: ocs_ramd.c:1027
ocs_ramd_hndlr_rtn_e
Definition: ocs_ramd.h:71
static int32_t scsi_format_unit_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
Command data phase callback for the FORMAT UNIT command.
Definition: ocs_ramd.c:5115
dif_t * ocs_ramdisc_dif_address(ocs_ramdisc_t *ramdisc, uint64_t lba)
Return the address of the DIF block if protection is enabled.
Definition: ocs_ramdisc.c:605
static void ocs_ignore_abort_evt(ocs_sm_event_t evt, const char *func, ocs_io_t *io, ocs_io_t *tmfio)
Ignore abort event.
Definition: ocs_ramd.c:333
static void * __ocs_ramd_io_wait_dataphase_compl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Wait for the data phase to complete.
Definition: ocs_ramd.c:1894
#define OCS_LOG_ENABLE_Q_FULL_BUSY_MSG(ocs)
Definition: ocs_debug.h:75
uint32_t disable_app_ref_ffff
Definition: ocs_scsi.h:233
uint8_t peripheral_qualifier
Definition: ocs_ramd.c:5476
#define SNSCODE_INV_CMDIU
Definition: ocs_ramd.h:54
#define SCSI_TEST_UNIT_READY
Definition: scsi_cmds.h:100
static ocs_ramd_hndlr_rtn_e ocs_ramd_mode_select(ocs_io_t *io, uint8_t cdb[])
Process the SCSI MODE SELECT command.
Definition: ocs_ramd.c:6411
void ocs_sm_transition(ocs_sm_ctx_t *ctx, ocs_sm_function_t state, void *data)
Transition to a new state.
Definition: ocs_sm.c:72
static ocs_ramd_hndlr_rtn_e ocs_ramd_read_long_common(ocs_io_t *io, uint64_t lba, uint16_t xfer_len, uint8_t pblock, uint8_t corrct)
Common code used for all SCSI READ commands.
Definition: ocs_ramd.c:4926
#define SCSI_WRITE_LONG10
Definition: scsi_cmds.h:110
uint8_t check_lun
Definition: ocs_ramd.c:246
ocs_ramdisc_t * ocs_ramd_lookup(ocs_io_t *io, uint32_t lun)
Given an IO and a LUN number, return the ramdisc to which the IO is directed.
Definition: ocs_ramd.c:471
uint32_t lun
Definition: ocs_ramdisc.h:89
void ocs_mgmt_ramd_get_all(ocs_textbuf_t *textbuf, void *object)
Definition: ocs_ramd.c:7032
ocs_t * ocs_get_instance(uint32_t index)
return pointer to ocs structure given instance index
#define SCSI_WRITE_AND_VERIFY10
Definition: scsi_cmds.h:106
static void ocs_sport_lock(ocs_sport_t *sport)
Definition: ocs_sport.h:67
#define SCSI_ASC_REPORTED_LUNS_DATA_HAS_CHANGED
#define OCS_RAMD_MIN_SIZE
Definition: ocs_ramd.c:102
#define ENABLE_CMD_TRACE
Definition: ocs_ramd.c:104
uint8_t * sense_data
Definition: ocs_scsi.h:96
int32_t scsi_io_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
ocs_mgmt_get_list_handler get_list_handler
Definition: ocs_mgmt.h:83
#define OCS_CTRLMASK_TGT_ALWAYS_VERIFY_DIF
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_long_common(ocs_io_t *io, uint64_t lba, uint16_t xfer_len, uint8_t cor_dis, uint8_t wr_uncor, uint8_t pblock)
Common code for all SCSI WRITE LONG commands.
Definition: ocs_ramd.c:4065
#define io_sm_trace()
Definition: ocs_ramd.c:79
uint32_t ext_status
Definition: ocs_ramd.c:55
#define SCSI_STATUS_TASK_SET_FULL
Definition: scsi_cmds.h:269
#define SCSI_MODE_SENSE6
Definition: scsi_cmds.h:56
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same32(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE SAME (32) command.
Definition: ocs_ramd.c:3443
#define SCSI_SVC_ACTION16_WRITE_LONG
Definition: scsi_cmds.h:167
#define MGMT_MODE_RD
Definition: ocs_mgmt.h:47
void ocs_scsi_tgt_cancel_io(ocs_t *ocs)
#define SCSI_MODE_SELECT6
Definition: scsi_cmds.h:54
#define SCSI_SVC_ACTION16_READ_LONG
Definition: scsi_cmds.h:162
int ocs_sm_post_event(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *data)
Post an event to a context.
Definition: ocs_sm.c:58
static ocs_ramdisc_t ** ocs_ramd
Definition: ocs_ramd.c:60
static void ocs_list_add_tail(ocs_list_t *list, void *item)
Add an item to the tail of the list.
Definition: ocs_list.h:263
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same16(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE SAME (16) command.
Definition: ocs_ramd.c:3390
static void * __ocs_ramd_io_wait_and_validate_dataphase(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Wait for the data phase to complete, then validate the received data.
Definition: ocs_ramd.c:1949
ocs_ramd_hndlr_rtn_e ocs_ramd_check_cond(ocs_io_t *io, uint8_t sense_key, uint8_t asc, uint8_t ascq)
Send a SCSI check condition.
Definition: ocs_ramd.c:2551
uint8_t data_dir
Definition: ocs_ramd.c:247
A structure used to create the READ CAPACITY (10) parameter data.
Definition: ocs_ramd.c:5261
#define SCSI_WRITE_AND_VERIFY12
Definition: scsi_cmds.h:107
void ocs_scsi_tgt_ddump(ocs_textbuf_t *textbuf, ocs_scsi_ddump_type_e type, void *obj)
Definition: ocs_ramd.c:6580
#define SCSI_REPORT_LUNS
Definition: scsi_cmds.h:80
#define SCSI_WRITE10
Definition: scsi_cmds.h:113
#define SCSI_READ12
Definition: scsi_cmds.h:71
uint32_t status
Definition: ocs_ramd.c:54
uint32_t unmap_granularity_alignment
Definition: ocs_ramd.c:5655
static ocs_ramd_hndlr_rtn_e ocs_ramd_read_long16(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ LONG (16) command.
Definition: ocs_ramd.c:4618
ocs_ramd_state_t
Definition: ocs_ramd.c:138
#define DATA_DIR_NONE
Definition: ocs_ramd.c:252
ocs_list_link_t link
Definition: ocs_ramd.c:65
static void post_one_ua(ocs_node_t *node, uint32_t lun, uint8_t asc, uint8_t ascq)
Post a Unit Attention condition to a node/LUN (I_T nexus).
Definition: ocs_ramd.c:6758
uint8_t response_code
Definition: ocs_ramd.c:1168
static int ramd_ref_cnt
Definition: ocs_ramd.c:62
#define SCSI_VAR_CMD_SVC_ACT_READ32
Definition: scsi_cmds.h:154
#define cmd_trace(io, fmt,...)
Definition: ocs_ramd.c:120
#define OCS_SCSI_CMD_DIR_IN
Definition: ocs_scsi.h:47
int32_t ocs_scsi_dif_wire_blocksize(ocs_scsi_dif_info_t *dif_info, int wiretomem)
Return wire block size given SCSI DIF API.
Definition: ocs_dif.c:237
static ocs_ramd_hndlr_rtn_e ocs_ramd_write16(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE (16) command.
Definition: ocs_ramd.c:3112
uint32_t maximum_unmap_block_descriptor_count
Definition: ocs_ramd.c:5653
static void ocs_domain_lock(ocs_domain_t *domain)
Definition: ocs_domain.h:63
size_t len
Definition: ocs_scsi.h:221
static int32_t ocs_ramd_io_error(ocs_io_t *io, ocs_scsi_io_status_e scsi_status)
Definition: ocs_ramd.c:1823
int32_t ocs_scsi_tgt_abort_io(ocs_io_t *io, bool send_abts, ocs_scsi_io_cb_t cb, void *arg)
Abort a target IO.
Definition: ocs_scsi.c:1989
ocs_ramdisc_t ** rdisc
Definition: ocs_ramd.c:67
static ocs_ramd_hndlr_rtn_e ocs_ramd_reserve_release(ocs_io_t *io, uint8_t cdb[])
Process the SCSI RESERVE or RELEASE command.
Definition: ocs_ramd.c:6128
#define OCS_SCSI_DIF_OPER_INSERT
Definition: ocs_scsi.h:200
static int32_t ocs_ramd_tgt_abort_rsp_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
Abort response callback.
Definition: ocs_ramd.c:2258
#define SCSI_WRITE6
Definition: scsi_cmds.h:116
#define SCSI_SVC_ACTION16_READ_CAPACITY
Definition: scsi_cmds.h:161
#define IOFMT_ARGS(io)
Definition: ocs_ramd.c:77
uint32_t max_sge
Definition: ocs_scsi_tgt.h:52
int32_t residual
Definition: ocs_scsi.h:98
static int32_t ocs_ramd_first_burst_local(ocs_io_t *io, uint32_t bytes_left)
Process first burst data into the IO DMA memory.
Definition: ocs_ramd.c:3520
int32_t ocs_scsi_send_resp(ocs_io_t *io, uint32_t flags, ocs_scsi_cmd_resp_t *rsp, ocs_scsi_io_cb_t cb, void *arg)
Send response data.
Definition: ocs_scsi.c:1688
void ocs_port_rdisc_free(ocs_t *ocs)
Free per port ramdisc structure.
Definition: ocs_ramd.c:7158
#define OCS_IO_WATERMARK_PER_INITIATOR
ocs_atomic_t watermark_hit
Definition: ocs_scsi_tgt.h:64
void ocs_scsi_tgt_sess_del_wait(ocs_sport_t *sport)
int32_t ocs_scsi_validate_initiator(ocs_node_t *node)
Validate a new initiator.
Definition: ocs_ramd.c:1002
uint8_t supportedVPDPageList[]
Definition: ocs_ramd.c:5463
static void ocs_ramd_abort_cleanup(ocs_io_t *io)
State: Cleanup aborted IO.
Definition: ocs_ramd.c:2334
#define RAMD_APP_TAG
Definition: ocs_ramd.c:73
#define SCSI_WRITE12
Definition: scsi_cmds.h:114
int32_t ocs_get_property(const char *prop_name, char *buffer, uint32_t buffer_len)
get driver wide property value
uint32_t app_tag
Definition: ocs_scsi.h:233
#define INVALID_APP_TAG
Definition: ocs_ramd.c:72
Vendor specific inquiry data for External DIF.
Definition: ocs_ramd.c:5528
int32_t ocs_format_ramdisc(ocs_t *ocs, ocs_ramdisc_t *ramdisc, uint32_t block_size, uint8_t p_type, uint8_t prot_en, uint8_t dif_separate)
Reformat an existing ramdisc, possibly the changing block size and/or protection. ...
Definition: ocs_ramdisc.c:359
int ocs_mgmt_ramd_get(ocs_textbuf_t *textbuf, char *parent, char *name, void *object)
Definition: ocs_ramd.c:6988
static void * __ocs_ramd_io_init_err_injection(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: Initial IO state with error injection enabled.
Definition: ocs_ramd.c:1444
uint8_t uint8_t maximum_compare_and_write_length
Definition: ocs_ramd.c:5645
ocs_ramdisc_ua_t represents a single UNIT ATTENTION condition
Definition: ocs_scsi_tgt.h:99
#define ocs_assert(cond,...)
Definition: ocs_debug.h:176
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_common(ocs_io_t *io, uint64_t lba, uint32_t block_count, uint8_t wrprotect, uint32_t app_tag, uint32_t ref_tag)
Common code for all SCSI WRITE commands.
Definition: ocs_ramd.c:3975
int32_t ocs_port_rdisc_find(ocs_t *ocs, uint32_t tgt_id, ocs_ramdisc_t ***rdisc, uint32_t *rdisc_count)
Find a per port ramdisc instance.
Definition: ocs_ramd.c:7188
A structure used to create the inquiry page B1 data.
Definition: ocs_ramd.c:5665
#define SNS_ILLEGAL_REQ
Definition: ocs_ramd.h:49
static scsi_cmd_table_t scsi_cmd_table_svc_in_action16[]
Definition: ocs_ramd.c:292
ocs_atomic_t io_high_watermark
Definition: ocs_scsi_tgt.h:63
ocs_atomic_t initiator_count
Definition: ocs_scsi_tgt.h:61
static int32_t scsi_mode_select_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
Command data phase callback for the MODE SELECT command.
Definition: ocs_ramd.c:6371
uint16_t app_tag
Definition: ocs_ramdisc.h:53
static const char * ocs_ramd_state_names[]
Definition: ocs_ramd.c:155
#define container_of(p, stype, field)
Definition: ocs_ramd.c:129
int ocs_mgmt_ramd_exec(char *parent, char *action, void *arg_in, uint32_t arg_in_length, void *arg_out, uint32_t arg_out_length, void *object)
Definition: ocs_ramd.c:7054
uint32_t ref_tag
Definition: ocs_scsi.h:232
ocs_sm_event_t
Definition: ocs_sm.h:60
uint16_t ocs_scsi_dif_calc_crc(const uint8_t *buffer, uint32_t size, uint16_t crc)
Calculate the T10 PI CRC guard value for a block.
Definition: ocs_dif.c:54
#define OCS_SCSI_CALL_COMPLETE
Definition: ocs_scsi.h:249
uint8_t p_type
Definition: ocs_ramdisc.h:98
void ocs_mgmt_emit_string(ocs_textbuf_t *textbuf, int access, const char *name, const char *value)
Generate a property with a string value in a management XML document.
int32_t ocs_scsi_send_tmf_resp(ocs_io_t *io, ocs_scsi_tmf_resp_e rspcode, uint8_t addl_rsp_info[3], ocs_scsi_io_cb_t cb, void *arg)
Send TMF response data.
Definition: ocs_scsi.c:1815
#define ocs_list_foreach(list, item)
Iterate a linked list.
Definition: ocs_list.h:428
ocs_scsi_io_status_e
Definition: ocs_scsi.h:105
static void * __ocs_ramd_wait_abts_acc_cmpl(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: ABTS: Wait for the BA_ACC to complete.
Definition: ocs_ramd.c:6717
int32_t ocs_hal_io_cancel(ocs_hal_t *hal)
Definition: ocs_hal.c:11262
int32_t ocs_scsi_recv_cmd_first_burst(ocs_io_t *io, uint32_t lun, uint8_t *cdb, uint32_t cdb_len, uint32_t flags, ocs_dma_t first_burst_buffers[], uint32_t first_burst_bytes)
Receive an FCP SCSI command with the first burst data.
Definition: ocs_ramd.c:1230
static int32_t ocs_mgmt_read_block(ocs_t *ocs, char *, void *, uint32_t, void *, uint32_t)
Definition: ocs_ramd.c:7081
static void * __ocs_ramd_abts_wait_io_abort_resp(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
State: ABTS: Wait for the IO to be aborted, and send response.
Definition: ocs_ramd.c:6662
static ocs_ramd_hndlr_rtn_e ocs_ramd_write_same_common(ocs_io_t *io, uint64_t lba, uint32_t block_count, uint8_t wrprotect, uint8_t pbdata, uint8_t lbdata, uint32_t app_tag, uint32_t ref_tag)
Common code for all SCSI WRITE SAME commands.
Definition: ocs_ramd.c:4135
int32_t ocs_scsi_dif_set_blocksize(ocs_scsi_dif_info_t *dif_info, uint32_t blocksize)
Set SCSI API DIF blocksize.
Definition: ocs_dif.c:145
static uint32_t ocs_ramd_count
Definition: ocs_ramd.c:61
#define SNSCODE_INV_CDB
Definition: ocs_ramd.h:58
#define ENABLE_BUFFER_DUMP
Definition: ocs_ramd.c:106
static ocs_ramd_hndlr_rtn_e ocs_ramd_read32(ocs_io_t *io, uint8_t cdb[])
Process the SCSI READ (32) command.
Definition: ocs_ramd.c:4506
ocs_ramd_hndlr_rtn_e ocs_ramd_task_set_full_or_busy(ocs_io_t *io)
Send a SCSI task set full or busy status.
Definition: ocs_ramd.c:2599
void ocs_ddump_ramdisc(ocs_textbuf_t *textbuf, ocs_ramdisc_t *ramdisc)
Definition: ocs_ramdisc.c:759
int32_t scsi_dataphase_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
static uint32_t scsi_validate_mode_select(ocs_io_t *io)
Validate a MODE SELECT parameter list.
Definition: ocs_ramd.c:6282
static scsi_cmd_table_t scsi_cmd_table_svc_out_action16[]
Definition: ocs_ramd.c:297
void * ocs_scsi_get_property_ptr(ocs_t *ocs, ocs_scsi_property_e prop)
Return a property pointer.
Definition: ocs_scsi.c:2871
void ocs_sm_disable(ocs_sm_ctx_t *ctx)
Disable further state machine processing.
Definition: ocs_sm.c:87
ocs_ramd_hndlr_rtn_e ocs_ramd_good_status(ocs_io_t *io)
Send a good SCSI status.
Definition: ocs_ramd.c:2519
static ocs_ramd_hndlr_rtn_e ocs_ramd_request_sense(ocs_io_t *io, uint8_t cdb[])
Process the SCSI REQUEST SENSE command.
Definition: ocs_ramd.c:6041
static uint32_t scsi_validate_format_unit(ocs_io_t *io)
Validate a FORMAT UNIT parameter list.
Definition: ocs_ramd.c:4974
static ocs_ramd_hndlr_rtn_e ocs_ramd_write6(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE (6) command.
Definition: ocs_ramd.c:2968
ocs_scsi_dif_oper_e dif_oper
Definition: ocs_scsi.h:230
static void ocs_sport_unlock(ocs_sport_t *sport)
Definition: ocs_sport.h:74
static scsi_cmd_table_t scsi_cmd_table[]
Definition: ocs_ramd.c:258
#define SNS_ABORTED_CMD
Definition: ocs_ramd.h:51
static ocs_ramd_hndlr_rtn_e ocs_ramd_write12(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE (12) command.
Definition: ocs_ramd.c:3066
uint8_t additional_sense_length
Definition: ocs_ramd.c:1177
static ocs_ramd_hndlr_rtn_e ocs_ramd_write10(ocs_io_t *io, uint8_t cdb[])
Process the SCSI WRITE (10) command.
Definition: ocs_ramd.c:3022
#define SCSI_WRITE16
Definition: scsi_cmds.h:115
#define SCSI_READ10
Definition: scsi_cmds.h:70
uint8_t uint8_t cmd_que
Definition: ocs_ramd.c:5492
void ocs_mgmt_emit_property_name(ocs_textbuf_t *textbuf, int access, const char *name)
Generate a property, with no value, in a management XML document.
uint32_t ocs_scsi_get_property(ocs_t *ocs, ocs_scsi_property_e prop)
Return SCSI API integer valued property.
Definition: ocs_scsi.c:2799
uint8_t product_revision_level[4]
Definition: ocs_ramd.c:5506
void ocs_ddump_value(ocs_textbuf_t *textbuf, const char *name, const char *fmt,...)
Generate driver dump data for a given value.
Definition: ocs_ddumplib.c:126
static ocs_ramd_hndlr_rtn_e ocs_ramd_init_write_dif(ocs_io_t *io, ocs_scsi_dif_info_t *dif_info, uint8_t wrprotect, uint32_t app_tag, uint32_t ref_tag)
Definition: ocs_ramd.c:3829
void ocs_scsi_io_complete(ocs_io_t *io)
Notify the base driver that the IO is complete.
Definition: ocs_scsi.c:2158
#define OCS_SCSI_DIF_OPER_STRIP
Definition: ocs_scsi.h:199
#define SCSI_READ_CAPACITY10
Definition: scsi_cmds.h:65
#define IOFMT
Definition: ocs_ramd.c:75
#define SCSI_READ6
Definition: scsi_cmds.h:73
A structure used to create the inquiry page 86 data.
Definition: ocs_ramd.c:5602
int32_t watermark_min
Definition: ocs_scsi_tgt.h:65