4 * Copyright (C) 2004 - 2009 Vladislav Bolkhovitin <vst@vlnb.net>
5 * Copyright (C) 2004 - 2005 Leonid Stoljar
6 * Copyright (C) 2007 - 2009 ID7 Ltd.
8 * Main SCSI target mid-level include file.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation, version 2
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
24 #include <linux/types.h>
25 #include <linux/version.h>
26 #include <linux/blkdev.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
30 #define CONFIG_SCST_PROC
32 #ifdef CONFIG_SCST_PROC
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi.h>
42 #include <scst_const.h>
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
45 #ifndef RHEL_RELEASE_CODE
55 * Version numbers, the same as for the kernel.
57 * Changing it don't forget to change SCST_FIO_REV in scst_vdisk.c
58 * and FIO_REV in usr/fileio/common.h as well.
60 #define SCST_VERSION(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + d)
61 #define SCST_VERSION_CODE SCST_VERSION(2, 0, 0, 0)
62 #ifdef CONFIG_SCST_PROC
63 #define SCST_VERSION_STRING_SUFFIX "-procfs"
65 #define SCST_VERSION_STRING_SUFFIX
67 #define SCST_VERSION_STRING "2.0.0-pre1" SCST_VERSION_STRING_SUFFIX
68 #define SCST_INTERFACE_VERSION \
69 SCST_VERSION_STRING "$Revision$" SCST_CONST_VERSION
71 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
72 #define COMPLETION_INITIALIZER_ONSTACK(work) \
73 ({ init_completion(&work); work; })
76 * Lockdep needs to run a non-constant initializer for on-stack
77 * completions - so we use the _ONSTACK() variant for those that
78 * are on the kernel stack:
81 # define DECLARE_COMPLETION_ONSTACK(work) \
82 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
84 # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
88 * list_is_last - tests whether @list is the last entry in list @head
89 * @list: the entry to test
90 * @head: the head of the list
92 static inline int list_is_last(const struct list_head *list,
93 const struct list_head *head)
95 return list->next == head;
100 #define SCST_LOCAL_NAME "scst_lcl_drvr"
102 /*************************************************************
103 ** States of command processing state machine. At first,
104 ** "active" states, then - "passive" ones. This is to have
105 ** more efficient generated code of the corresponding
106 ** "switch" statements.
107 *************************************************************/
109 /* Internal parsing */
110 #define SCST_CMD_STATE_PRE_PARSE 0
112 /* Dev handler's parse() is going to be called */
113 #define SCST_CMD_STATE_DEV_PARSE 1
115 /* Allocation of the cmd's data buffer */
116 #define SCST_CMD_STATE_PREPARE_SPACE 2
118 /* Target driver's rdy_to_xfer() is going to be called */
119 #define SCST_CMD_STATE_RDY_TO_XFER 3
121 /* Target driver's pre_exec() is going to be called */
122 #define SCST_CMD_STATE_TGT_PRE_EXEC 4
124 /* Cmd is going to be sent for execution */
125 #define SCST_CMD_STATE_SEND_FOR_EXEC 5
127 /* Cmd is being checked if it should be executed locally */
128 #define SCST_CMD_STATE_LOCAL_EXEC 6
130 /* Cmd is ready for execution */
131 #define SCST_CMD_STATE_REAL_EXEC 7
133 /* Internal post-exec checks */
134 #define SCST_CMD_STATE_PRE_DEV_DONE 8
136 /* Internal MODE SELECT pages related checks */
137 #define SCST_CMD_STATE_MODE_SELECT_CHECKS 9
139 /* Dev handler's dev_done() is going to be called */
140 #define SCST_CMD_STATE_DEV_DONE 10
142 /* Target driver's xmit_response() is going to be called */
143 #define SCST_CMD_STATE_PRE_XMIT_RESP 11
145 /* Target driver's xmit_response() is going to be called */
146 #define SCST_CMD_STATE_XMIT_RESP 12
149 #define SCST_CMD_STATE_FINISHED 13
151 /* Internal cmd finished */
152 #define SCST_CMD_STATE_FINISHED_INTERNAL 14
154 #define SCST_CMD_STATE_LAST_ACTIVE (SCST_CMD_STATE_FINISHED_INTERNAL+100)
156 /* A cmd is created, but scst_cmd_init_done() not called */
157 #define SCST_CMD_STATE_INIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+1)
159 /* LUN translation (cmd->tgt_dev assignment) */
160 #define SCST_CMD_STATE_INIT (SCST_CMD_STATE_LAST_ACTIVE+2)
162 /* Allocation of the cmd's data buffer */
163 #define SCST_CMD_STATE_PREPROCESS_DONE (SCST_CMD_STATE_LAST_ACTIVE+3)
165 /* Waiting for data from the initiator (until scst_rx_data() called) */
166 #define SCST_CMD_STATE_DATA_WAIT (SCST_CMD_STATE_LAST_ACTIVE+4)
168 /* Waiting for CDB's execution finish */
169 #define SCST_CMD_STATE_REAL_EXECUTING (SCST_CMD_STATE_LAST_ACTIVE+5)
171 /* Waiting for response's transmission finish */
172 #define SCST_CMD_STATE_XMIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+6)
174 /*************************************************************
175 * Can be retuned instead of cmd's state by dev handlers'
176 * functions, if the command's state should be set by default
177 *************************************************************/
178 #define SCST_CMD_STATE_DEFAULT 500
180 /*************************************************************
181 * Can be retuned instead of cmd's state by dev handlers'
182 * functions, if it is impossible to complete requested
183 * task in atomic context. The cmd will be restarted in thread
185 *************************************************************/
186 #define SCST_CMD_STATE_NEED_THREAD_CTX 1000
188 /*************************************************************
189 * Can be retuned instead of cmd's state by dev handlers'
190 * parse function, if the cmd processing should be stopped
191 * for now. The cmd will be restarted by dev handlers itself.
192 *************************************************************/
193 #define SCST_CMD_STATE_STOP 1001
195 /*************************************************************
196 ** States of mgmt command processing state machine
197 *************************************************************/
199 /* LUN translation (mcmd->tgt_dev assignment) */
200 #define SCST_MCMD_STATE_INIT 0
202 /* Mgmt cmd is ready for processing */
203 #define SCST_MCMD_STATE_READY 1
205 /* Mgmt cmd is being executing */
206 #define SCST_MCMD_STATE_EXECUTING 2
208 /* Post check when affected commands done */
209 #define SCST_MCMD_STATE_POST_AFFECTED_CMDS_DONE 3
211 /* Target driver's task_mgmt_fn_done() is going to be called */
212 #define SCST_MCMD_STATE_DONE 4
214 /* The mcmd finished */
215 #define SCST_MCMD_STATE_FINISHED 5
217 /*************************************************************
218 ** Constants for "atomic" parameter of SCST's functions
219 *************************************************************/
220 #define SCST_NON_ATOMIC 0
221 #define SCST_ATOMIC 1
223 /*************************************************************
224 ** Values for pref_context parameter of scst_cmd_init_done(),
225 ** scst_rx_data(), scst_restart_cmd(), scst_tgt_cmd_done()
226 ** and scst_cmd_done()
227 *************************************************************/
229 enum scst_exec_context {
231 * Direct cmd's processing (i.e. regular function calls in the current
232 * context) sleeping is not allowed
234 SCST_CONTEXT_DIRECT_ATOMIC,
237 * Direct cmd's processing (i.e. regular function calls in the current
238 * context), sleeping is allowed, no restrictions
242 /* Tasklet or thread context required for cmd's processing */
243 SCST_CONTEXT_TASKLET,
245 /* Thread context required for cmd's processing */
249 * Context is the same as it was in previous call of the corresponding
250 * callback. For example, if dev handler's exec() does sync. data
251 * reading this value should be used for scst_cmd_done(). The same is
252 * true if scst_tgt_cmd_done() called directly from target driver's
253 * xmit_response(). Not allowed in scst_cmd_init_done() and
254 * scst_cmd_init_stage1_done().
259 /*************************************************************
260 ** Values for status parameter of scst_rx_data()
261 *************************************************************/
264 #define SCST_RX_STATUS_SUCCESS 0
267 * Data receiving finished with error, so set the sense and
268 * finish the command, including xmit_response() call
270 #define SCST_RX_STATUS_ERROR 1
273 * Data receiving finished with error and the sense is set,
274 * so finish the command, including xmit_response() call
276 #define SCST_RX_STATUS_ERROR_SENSE_SET 2
279 * Data receiving finished with fatal error, so finish the command,
280 * but don't call xmit_response()
282 #define SCST_RX_STATUS_ERROR_FATAL 3
284 /*************************************************************
285 ** Values for status parameter of scst_restart_cmd()
286 *************************************************************/
289 #define SCST_PREPROCESS_STATUS_SUCCESS 0
292 * Command's processing finished with error, so set the sense and
293 * finish the command, including xmit_response() call
295 #define SCST_PREPROCESS_STATUS_ERROR 1
298 * Command's processing finished with error and the sense is set,
299 * so finish the command, including xmit_response() call
301 #define SCST_PREPROCESS_STATUS_ERROR_SENSE_SET 2
304 * Command's processing finished with fatal error, so finish the command,
305 * but don't call xmit_response()
307 #define SCST_PREPROCESS_STATUS_ERROR_FATAL 3
309 /* Thread context requested */
310 #define SCST_PREPROCESS_STATUS_NEED_THREAD 4
312 /*************************************************************
313 ** Values for AEN functions
314 *************************************************************/
317 * SCSI Asynchronous Event. Parameter contains SCSI sense
318 * (Unit Attention). AENs generated only for 2 the following UAs:
319 * CAPACITY DATA HAS CHANGED and REPORTED LUNS DATA HAS CHANGED.
320 * Other UAs reported regularly as CHECK CONDITION status,
321 * because it doesn't look safe to report them using AENs, since
322 * reporting using AENs opens delivery race windows even in case of
325 #define SCST_AEN_SCSI 0
327 /*************************************************************
328 ** Allowed return/status codes for report_aen() callback and
329 ** scst_set_aen_delivery_status() function
330 *************************************************************/
333 #define SCST_AEN_RES_SUCCESS 0
336 #define SCST_AEN_RES_NOT_SUPPORTED -1
339 #define SCST_AEN_RES_FAILED -2
341 /*************************************************************
342 ** Allowed return codes for xmit_response(), rdy_to_xfer()
343 *************************************************************/
346 #define SCST_TGT_RES_SUCCESS 0
348 /* Internal device queue is full, retry again later */
349 #define SCST_TGT_RES_QUEUE_FULL -1
352 * It is impossible to complete requested task in atomic context.
353 * The cmd will be restarted in thread context.
355 #define SCST_TGT_RES_NEED_THREAD_CTX -2
358 * Fatal error, if returned by xmit_response() the cmd will
359 * be destroyed, if by any other function, xmit_response()
360 * will be called with HARDWARE ERROR sense data
362 #define SCST_TGT_RES_FATAL_ERROR -3
364 /*************************************************************
365 ** Allowed return codes for dev handler's exec()
366 *************************************************************/
368 /* The cmd is done, go to other ones */
369 #define SCST_EXEC_COMPLETED 0
371 /* The cmd should be sent to SCSI mid-level */
372 #define SCST_EXEC_NOT_COMPLETED 1
375 * Thread context is required to execute the command.
376 * Exec() will be called again in the thread context.
378 #define SCST_EXEC_NEED_THREAD 2
381 * Set if cmd is finished and there is status/sense to be sent.
382 * The status should be not sent (i.e. the flag not set) if the
383 * possibility to perform a command in "chunks" (i.e. with multiple
384 * xmit_response()/rdy_to_xfer()) is used (not implemented yet).
385 * Obsolete, use scst_cmd_get_is_send_status() instead.
387 #define SCST_TSC_FLAG_STATUS 0x2
389 /*************************************************************
390 ** Additional return code for dev handler's task_mgmt_fn()
391 *************************************************************/
393 /* Regular standard actions for the command should be done */
394 #define SCST_DEV_TM_NOT_COMPLETED 1
396 /*************************************************************
397 ** Session initialization phases
398 *************************************************************/
400 /* Set if session is being initialized */
401 #define SCST_SESS_IPH_INITING 0
403 /* Set if the session is successfully initialized */
404 #define SCST_SESS_IPH_SUCCESS 1
406 /* Set if the session initialization failed */
407 #define SCST_SESS_IPH_FAILED 2
409 /* Set if session is initialized and ready */
410 #define SCST_SESS_IPH_READY 3
412 /*************************************************************
413 ** Session shutdown phases
414 *************************************************************/
416 /* Set if session is initialized and ready */
417 #define SCST_SESS_SPH_READY 0
419 /* Set if session is shutting down */
420 #define SCST_SESS_SPH_SHUTDOWN 1
422 /*************************************************************
423 ** Session's async (atomic) flags
424 *************************************************************/
426 /* Set if the sess's hw pending work is scheduled */
427 #define SCST_SESS_HW_PENDING_WORK_SCHEDULED 0
429 /*************************************************************
430 ** Cmd's async (atomic) flags
431 *************************************************************/
433 /* Set if the cmd is aborted and ABORTED sense will be sent as the result */
434 #define SCST_CMD_ABORTED 0
436 /* Set if the cmd is aborted by other initiator */
437 #define SCST_CMD_ABORTED_OTHER 1
439 /* Set if the cmd is aborted and counted in cmd_done_wait_count */
440 #define SCST_CMD_DONE_COUNTED 2
442 /* Set if no response should be sent to the target about this cmd */
443 #define SCST_CMD_NO_RESP 3
445 /* Set if the cmd is dead and can be destroyed at any time */
446 #define SCST_CMD_CAN_BE_DESTROYED 4
448 /*************************************************************
449 ** Tgt_dev's async. flags (tgt_dev_flags)
450 *************************************************************/
452 /* Set if tgt_dev has Unit Attention sense */
453 #define SCST_TGT_DEV_UA_PENDING 0
455 /* Set if tgt_dev is RESERVED by another session */
456 #define SCST_TGT_DEV_RESERVED 1
458 /* Set if the corresponding context is atomic */
459 #define SCST_TGT_DEV_AFTER_INIT_WR_ATOMIC 5
460 #define SCST_TGT_DEV_AFTER_INIT_OTH_ATOMIC 6
461 #define SCST_TGT_DEV_AFTER_RESTART_WR_ATOMIC 7
462 #define SCST_TGT_DEV_AFTER_RESTART_OTH_ATOMIC 8
463 #define SCST_TGT_DEV_AFTER_RX_DATA_ATOMIC 9
464 #define SCST_TGT_DEV_AFTER_EXEC_ATOMIC 10
466 #define SCST_TGT_DEV_CLUST_POOL 11
468 #ifdef CONFIG_SCST_PROC
470 /*************************************************************
471 ** Name of the entry in /proc
472 *************************************************************/
473 #define SCST_PROC_ENTRY_NAME "scsi_tgt"
477 /*************************************************************
478 ** Activities suspending timeout
479 *************************************************************/
480 #define SCST_SUSPENDING_TIMEOUT (90 * HZ)
482 /*************************************************************
483 ** Kernel cache creation helper
484 *************************************************************/
486 #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
487 sizeof(struct __struct), __alignof__(struct __struct),\
488 (__flags), NULL, NULL)
491 /*************************************************************
492 ** Vlaid_mask constants for scst_analyze_sense()
493 *************************************************************/
495 #define SCST_SENSE_KEY_VALID 1
496 #define SCST_SENSE_ASC_VALID 2
497 #define SCST_SENSE_ASCQ_VALID 4
499 #define SCST_SENSE_ASCx_VALID (SCST_SENSE_ASC_VALID | \
500 SCST_SENSE_ASCQ_VALID)
502 #define SCST_SENSE_ALL_VALID (SCST_SENSE_KEY_VALID | \
503 SCST_SENSE_ASC_VALID | \
504 SCST_SENSE_ASCQ_VALID)
506 /*************************************************************
508 *************************************************************/
513 struct scst_mgmt_cmd;
516 struct scst_dev_type;
523 * SCST uses 64-bit numbers to represent LUN's internally. The value
524 * NO_SUCH_LUN is guaranteed to be different of every valid LUN.
526 #define NO_SUCH_LUN ((uint64_t)-1)
528 typedef enum dma_data_direction scst_data_direction;
530 enum scst_cdb_flags {
531 /* SCST_TRANSFER_LEN_TYPE_FIXED must be equiv 1 (FIXED_BIT in cdb) */
532 SCST_TRANSFER_LEN_TYPE_FIXED = 0x001,
533 SCST_SMALL_TIMEOUT = 0x002,
534 SCST_LONG_TIMEOUT = 0x004,
535 SCST_UNKNOWN_LENGTH = 0x008,
536 SCST_INFO_NOT_FOUND = 0x010, /* must be single bit */
537 SCST_VERIFY_BYTCHK_MISMATCH_ALLOWED = 0x020,
538 SCST_IMPLICIT_HQ = 0x040,
539 SCST_SKIP_UA = 0x080,
540 SCST_WRITE_MEDIUM = 0x100,
541 SCST_LOCAL_CMD = 0x200,
542 SCST_LOCAL_EXEC_NEEDED = 0x400,
546 * Scsi_Target_Template: defines what functions a target driver will
547 * have to provide in order to work with the target mid-level.
548 * MUST HAVEs define functions that are expected to be in order to work.
549 * OPTIONAL says that there is a choice.
551 * Also, pay attention to the fact that a command is BLOCKING or NON-BLOCKING.
552 * NON-BLOCKING means that a function returns immediately and will not wait
553 * for actual data transfer to finish. Blocking in such command could have
554 * negative impact on overall system performance. If blocking is necessary,
555 * it is worth to consider creating dedicated thread(s) in target driver, to
556 * which the commands would be passed and which would perform blocking
557 * operations instead of SCST.
559 * If the function allowed to sleep or not is determined by its last
560 * argument, which is true, if sleeping is not allowed. In this case,
561 * if the function requires sleeping, it can return
562 * SCST_TGT_RES_NEED_THREAD_CTX, and it will be recalled in the thread context,
563 * where sleeping is allowed.
565 struct scst_tgt_template {
569 * SG tablesize allows to check whether scatter/gather can be used
575 * True, if this target adapter uses unchecked DMA onto an ISA bus.
577 unsigned unchecked_isa_dma:1;
580 * True, if this target adapter can benefit from using SG-vector
581 * clustering (i.e. smaller number of segments).
583 unsigned use_clustering:1;
586 * True, if this target adapter doesn't support SG-vector clustering
588 unsigned no_clustering:1;
591 * True, if corresponding function supports execution in
592 * the atomic (non-sleeping) context
594 unsigned xmit_response_atomic:1;
595 unsigned rdy_to_xfer_atomic:1;
597 #ifdef CONFIG_SCST_PROC
598 /* True, if the template doesn't need the entry in /proc */
599 unsigned no_proc_entry:1;
603 * The maximum time in seconds cmd can stay inside the target
604 * hardware, i.e. after rdy_to_xfer() and xmit_response(), before
605 * on_hw_pending_cmd_timeout() will be called, if defined.
607 * In the current implementation a cmd will be aborted in time t
608 * max_hw_pending_time <= t < 2*max_hw_pending_time.
610 int max_hw_pending_time;
613 * This function is equivalent to the SCSI
614 * queuecommand. The target should transmit the response
615 * buffer and the status in the scst_cmd struct.
616 * The expectation is that this executing this command is NON-BLOCKING.
618 * After the response is actually transmitted, the target
619 * should call the scst_tgt_cmd_done() function of the
620 * mid-level, which will allow it to free up the command.
621 * Returns one of the SCST_TGT_RES_* constants.
623 * Pay attention to "atomic" attribute of the cmd, which can be get
624 * by scst_cmd_atomic(): it is true if the function called in the
625 * atomic (non-sleeping) context.
629 int (*xmit_response) (struct scst_cmd *cmd);
632 * This function informs the driver that data
633 * buffer corresponding to the said command have now been
634 * allocated and it is OK to receive data for this command.
635 * This function is necessary because a SCSI target does not
636 * have any control over the commands it receives. Most lower
637 * level protocols have a corresponding function which informs
638 * the initiator that buffers have been allocated e.g., XFER_
639 * RDY in Fibre Channel. After the data is actually received
640 * the low-level driver needs to call scst_rx_data() in order to
641 * continue processing this command.
642 * Returns one of the SCST_TGT_RES_* constants.
643 * This command is expected to be NON-BLOCKING.
645 * Pay attention to "atomic" attribute of the cmd, which can be get
646 * by scst_cmd_atomic(): it is true if the function called in the
647 * atomic (non-sleeping) context.
651 int (*rdy_to_xfer) (struct scst_cmd *cmd);
654 * Called if cmd stays inside the target hardware, i.e. after
655 * rdy_to_xfer() and xmit_response(), more than max_hw_pending_time
656 * time. The target driver supposed to cleanup this command and
657 * resume cmd's processing.
661 void (*on_hw_pending_cmd_timeout) (struct scst_cmd *cmd);
664 * Called to notify the driver that the command is about to be freed.
665 * Necessary, because for aborted commands xmit_response() could not
666 * be called. Could be called on IRQ context.
670 void (*on_free_cmd) (struct scst_cmd *cmd);
673 * This function allows target driver to handle data buffer
674 * allocations on its own.
676 * Target driver doesn't have to always allocate buffer in this
677 * function, but if it decide to do it, it must check that
678 * scst_cmd_get_data_buff_alloced() returns 0, otherwise to avoid
679 * double buffer allocation and memory leaks alloc_data_buf() shall
682 * Shall return 0 in case of success or < 0 (preferrably -ENOMEM)
683 * in case of error, or > 0 if the regular SCST allocation should be
684 * done. In case of returning successfully,
685 * scst_cmd->tgt_data_buf_alloced will be set by SCST.
687 * It is possible that both target driver and dev handler request own
688 * memory allocation. In this case, data will be memcpy() between
689 * buffers, where necessary.
691 * If allocation in atomic context - cf. scst_cmd_atomic() - is not
692 * desired or fails and consequently < 0 is returned, this function
693 * will be re-called in thread context.
695 * Please note that the driver will have to handle itself all relevant
696 * details such as scatterlist setup, highmem, freeing the allocated
701 int (*alloc_data_buf) (struct scst_cmd *cmd);
704 * This function informs the driver that data
705 * buffer corresponding to the said command have now been
706 * allocated and other preprocessing tasks have been done.
707 * A target driver could need to do some actions at this stage.
708 * After the target driver done the needed actions, it shall call
709 * scst_restart_cmd() in order to continue processing this command.
711 * Called only if the cmd is queued using scst_cmd_init_stage1_done()
712 * instead of scst_cmd_init_done().
714 * Returns void, the result is expected to be returned using
715 * scst_restart_cmd().
717 * This command is expected to be NON-BLOCKING.
719 * Pay attention to "atomic" attribute of the cmd, which can be get
720 * by scst_cmd_atomic(): it is true if the function called in the
721 * atomic (non-sleeping) context.
725 void (*preprocessing_done) (struct scst_cmd *cmd);
728 * This function informs the driver that the said command is about
731 * Returns one of the SCST_PREPROCESS_* constants.
733 * This command is expected to be NON-BLOCKING.
735 * Pay attention to "atomic" attribute of the cmd, which can be get
736 * by scst_cmd_atomic(): it is true if the function called in the
737 * atomic (non-sleeping) context.
741 int (*pre_exec) (struct scst_cmd *cmd);
744 * This function informs the driver that all affected by the
745 * corresponding task management function commands have beed completed.
746 * No return value expected.
748 * This function is expected to be NON-BLOCKING.
750 * Called without any locks held from a thread context.
754 void (*task_mgmt_affected_cmds_done) (struct scst_mgmt_cmd *mgmt_cmd);
757 * This function informs the driver that the corresponding task
758 * management function has been completed, i.e. all the corresponding
759 * commands completed and freed. No return value expected.
761 * This function is expected to be NON-BLOCKING.
763 * Called without any locks held from a thread context.
765 * MUST HAVE if the target supports task management.
767 void (*task_mgmt_fn_done) (struct scst_mgmt_cmd *mgmt_cmd);
770 * This function should detect the target adapters that
771 * are present in the system. The function should return a value
772 * >= 0 to signify the number of detected target adapters.
773 * A negative value should be returned whenever there is
778 int (*detect) (struct scst_tgt_template *tgt_template);
781 * This function should free up the resources allocated to the device.
782 * The function should return 0 to indicate successful release
783 * or a negative value if there are some issues with the release.
784 * In the current version the return value is ignored.
788 int (*release) (struct scst_tgt *tgt);
791 * This function is used for Asynchronous Event Notifications.
793 * Returns one of the SCST_AEN_RES_* constants.
794 * After AEN is sent, target driver must call scst_aen_done() and,
795 * optionally, scst_set_aen_delivery_status().
797 * This command is expected to be NON-BLOCKING, but can sleep.
799 * MUST HAVE, if low-level protocol supports AENs.
801 int (*report_aen) (struct scst_aen *aen);
803 #ifdef CONFIG_SCST_PROC
805 * Those functions can be used to export the driver's statistics and
806 * other infos to the world outside the kernel as well as to get some
807 * management commands from it.
811 int (*read_proc) (struct seq_file *seq, struct scst_tgt *tgt);
812 int (*write_proc) (char *buffer, char **start, off_t offset,
813 int length, int *eof, struct scst_tgt *tgt);
817 * This function allows to enable or disable particular target.
818 * A disabled target doesn't receive and process any SCSI commands.
820 * SHOULD HAVE to avoid race when there are connected initiators,
821 * while target not yet completed the initial configuration. In this
822 * case the too early connected initiators would see not those devices,
823 * which they intended to see.
825 ssize_t (*enable_tgt) (struct scst_tgt *tgt, const char *buffer,
829 * This function shows if particular target is enabled or not.
831 * SHOULD HAVE, see above why.
833 bool (*is_tgt_enabled) (struct scst_tgt *tgt);
836 * Name of the template. Must be unique to identify
837 * the template. MUST HAVE
839 const char name[SCST_MAX_NAME];
842 * Number of additional threads to the pool of dedicated threads.
843 * Used if xmit_response() or rdy_to_xfer() is blocking.
844 * It is the target driver's duty to ensure that not more, than that
845 * number of threads, are blocked in those functions at any time.
849 /* Optional default log flags */
850 const unsigned long default_trace_flags;
852 /* Optional pointer to trace flags */
853 unsigned long *trace_flags;
855 /* Optional local trace table */
856 struct scst_trace_log *trace_tbl;
858 /* Optional local trace table help string */
859 const char *trace_tbl_help;
861 #ifndef CONFIG_SCST_PROC
862 /* Optional sysfs attributes */
863 const struct attribute **tgtt_attrs;
865 /* Optional sysfs target attributes */
866 const struct attribute **tgt_attrs;
869 /** Private, must be inited to 0 by memset() **/
871 /* List of targets per template, protected by scst_mutex */
872 struct list_head tgt_list;
874 /* List entry of global templates list */
875 struct list_head scst_template_list_entry;
877 #ifdef CONFIG_SCST_PROC
878 /* The pointer to the /proc directory entry */
879 struct proc_dir_entry *proc_tgt_root;
882 /* Set if tgtt_kobj was initialized */
883 unsigned int tgtt_kobj_initialized:1;
885 struct kobject tgtt_kobj; /* kobject for this struct */
887 struct completion tgtt_kobj_release_cmpl;
889 #ifdef CONFIG_SCST_PROC
890 /* Device number in /proc */
895 struct scst_dev_type {
896 /* SCSI type of the supported device. MUST HAVE */
900 * True, if corresponding function supports execution in
901 * the atomic (non-sleeping) context
903 unsigned parse_atomic:1;
904 unsigned exec_atomic:1;
905 unsigned dev_done_atomic:1;
907 #ifdef CONFIG_SCST_PROC
908 /* Set, if no /proc files should be automatically created by SCST */
913 * Should be set, if exec() is synchronous. This is a hint to SCST core
914 * to optimize commands order management.
916 unsigned exec_sync:1;
919 * Called to parse CDB from the cmd and initialize
920 * cmd->bufflen and cmd->data_direction (both - REQUIRED).
921 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
922 * if the next default state should be used, or
923 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
924 * context, but requires sleeping, or SCST_CMD_STATE_STOP if the
925 * command should not be further processed for now. In the
926 * SCST_CMD_STATE_NEED_THREAD_CTX case the function
927 * will be recalled in the thread context, where sleeping is allowed.
929 * Pay attention to "atomic" attribute of the cmd, which can be get
930 * by scst_cmd_atomic(): it is true if the function called in the
931 * atomic (non-sleeping) context.
935 int (*parse) (struct scst_cmd *cmd);
938 * Called to execute CDB. Useful, for instance, to implement
939 * data caching. The result of CDB execution is reported via
940 * cmd->scst_cmd_done() callback.
942 * - SCST_EXEC_COMPLETED - the cmd is done, go to other ones
943 * - SCST_EXEC_NEED_THREAD - thread context is required to execute
944 * the command. Exec() will be called again in the thread context.
945 * - SCST_EXEC_NOT_COMPLETED - the cmd should be sent to SCSI
948 * Pay attention to "atomic" attribute of the cmd, which can be get
949 * by scst_cmd_atomic(): it is true if the function called in the
950 * atomic (non-sleeping) context.
952 * If this function provides sync execution, you should set
953 * exec_sync flag and consider to setup dedicated threads by
954 * setting threads_num > 0.
956 * !! If this function is implemented, scst_check_local_events() !!
957 * !! shall be called inside it just before the actual command's !!
960 * OPTIONAL, if not set, the commands will be sent directly to SCSI
963 int (*exec) (struct scst_cmd *cmd);
966 * Called to notify dev handler about the result of cmd execution
967 * and perform some post processing. Cmd's fields is_send_status and
968 * resp_data_len should be set by this function, but SCST offers good
970 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
971 * if the next default state should be used, or
972 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
973 * context, but requires sleeping. In the last case, the function
974 * will be recalled in the thread context, where sleeping is allowed.
976 * Pay attention to "atomic" attribute of the cmd, which can be get
977 * by scst_cmd_atomic(): it is true if the function called in the
978 * atomic (non-sleeping) context.
980 int (*dev_done) (struct scst_cmd *cmd);
983 * Called to notify dev hander that the command is about to be freed.
984 * Could be called on IRQ context.
986 void (*on_free_cmd) (struct scst_cmd *cmd);
989 * Called to execute a task management command.
991 * - SCST_MGMT_STATUS_SUCCESS - the command is done with success,
992 * no firther actions required
993 * - The SCST_MGMT_STATUS_* error code if the command is failed and
994 * no further actions required
995 * - SCST_DEV_TM_NOT_COMPLETED - regular standard actions for the
996 * command should be done
998 * Called without any locks held from a thread context.
1000 int (*task_mgmt_fn) (struct scst_mgmt_cmd *mgmt_cmd,
1001 struct scst_tgt_dev *tgt_dev);
1004 * Called when new device is attaching to the dev handler
1005 * Returns 0 on success, error code otherwise.
1007 int (*attach) (struct scst_device *dev);
1009 /* Called when new device is detaching from the dev handler */
1010 void (*detach) (struct scst_device *dev);
1013 * Called when new tgt_dev (session) is attaching to the dev handler.
1014 * Returns 0 on success, error code otherwise.
1016 int (*attach_tgt) (struct scst_tgt_dev *tgt_dev);
1018 /* Called when tgt_dev (session) is detaching from the dev handler */
1019 void (*detach_tgt) (struct scst_tgt_dev *tgt_dev);
1021 #ifdef CONFIG_SCST_PROC
1023 * Those functions can be used to export the handler's statistics and
1024 * other infos to the world outside the kernel as well as to get some
1025 * management commands from it.
1029 int (*read_proc) (struct seq_file *seq, struct scst_dev_type *dev_type);
1030 int (*write_proc) (char *buffer, char **start, off_t offset,
1031 int length, int *eof, struct scst_dev_type *dev_type);
1035 * Name of the dev handler. Must be unique. MUST HAVE.
1037 * It's SCST_MAX_NAME + few more bytes to match scst_user expectations.
1039 char name[SCST_MAX_NAME + 10];
1042 * Number of dedicated threads. If 0 - no dedicated threads will
1043 * be created, if <0 - creation of dedicated threads is prohibited.
1047 /* Optional default log flags */
1048 const unsigned long default_trace_flags;
1050 /* Optional pointer to trace flags */
1051 unsigned long *trace_flags;
1053 /* Optional local trace table */
1054 struct scst_trace_log *trace_tbl;
1056 /* Optional local trace table help string */
1057 const char *trace_tbl_help;
1059 #ifndef CONFIG_SCST_PROC
1060 /* Optional sysfs attributes */
1061 const struct attribute **devt_attrs;
1063 /* Optional sysfs device attributes */
1064 const struct attribute **dev_attrs;
1067 /* Pointer to dev handler's private data */
1070 /* Pointer to parent dev type in the sysfs hierarchy */
1071 struct scst_dev_type *parent;
1073 struct module *module;
1075 /** Private, must be inited to 0 by memset() **/
1077 /* list entry in scst_dev_type_list */
1078 struct list_head dev_type_list_entry;
1080 #ifdef CONFIG_SCST_PROC
1081 /* The pointer to the /proc directory entry */
1082 struct proc_dir_entry *proc_dev_type_root;
1085 unsigned int devt_kobj_initialized:1;
1087 struct kobject devt_kobj; /* main handlers/driver */
1089 /* To wait until devt_kobj released */
1090 struct completion devt_kobj_release_compl;
1094 /* List of remote sessions per target, protected by scst_mutex */
1095 struct list_head sess_list;
1097 /* List entry of targets per template (tgts_list) */
1098 struct list_head tgt_list_entry;
1100 struct scst_tgt_template *tgtt; /* corresponding target template */
1102 struct scst_acg *default_acg; /* The default acg for this target. */
1105 * Maximum SG table size. Needed here, since different cards on the
1106 * same target template can have different SG table limitations.
1110 /* Used for storage of target driver private stuff */
1114 * The following fields used to store and retry cmds if target's
1115 * internal queue is full, so the target is unable to accept
1116 * the cmd returning QUEUE FULL.
1117 * They protected by tgt_lock, where necessary.
1119 bool retry_timer_active;
1120 struct timer_list retry_timer;
1121 atomic_t finished_cmds;
1123 spinlock_t tgt_lock;
1124 struct list_head retry_cmd_list;
1126 /* Used to wait until session finished to unregister */
1127 wait_queue_head_t unreg_waitQ;
1129 #ifdef CONFIG_SCST_PROC
1130 /* Device number in /proc */
1134 /* Name of the target */
1137 #ifdef CONFIG_SCST_PROC
1138 /* Name on the default security group ("Default_target_name") */
1139 char *default_group_name;
1142 /* Set if tgt_kobj was initialized */
1143 unsigned int tgt_kobj_initialized:1;
1145 struct kobject tgt_kobj; /* main targets/target kobject */
1146 struct kobject *tgt_sess_kobj; /* target/sessions/ */
1147 struct kobject *tgt_luns_kobj; /* target/luns/ */
1148 struct kobject *tgt_ini_grp_kobj; /* target/ini_groups/ */
1151 /* Hash size and hash fn for hash based lun translation */
1152 #define TGT_DEV_HASH_SHIFT 5
1153 #define TGT_DEV_HASH_SIZE (1 << TGT_DEV_HASH_SHIFT)
1154 #define HASH_VAL(_val) (_val & (TGT_DEV_HASH_SIZE - 1))
1156 #ifdef CONFIG_SCST_MEASURE_LATENCY
1158 /* Defines extended latency statistics */
1159 struct scst_ext_latency_stat {
1160 uint64_t scst_time_rd, tgt_time_rd, dev_time_rd;
1161 unsigned int processed_cmds_rd;
1162 uint64_t min_scst_time_rd, min_tgt_time_rd, min_dev_time_rd;
1163 uint64_t max_scst_time_rd, max_tgt_time_rd, max_dev_time_rd;
1165 uint64_t scst_time_wr, tgt_time_wr, dev_time_wr;
1166 unsigned int processed_cmds_wr;
1167 uint64_t min_scst_time_wr, min_tgt_time_wr, min_dev_time_wr;
1168 uint64_t max_scst_time_wr, max_tgt_time_wr, max_dev_time_wr;
1171 #define SCST_IO_SIZE_THRESHOLD_SMALL (8*1024)
1172 #define SCST_IO_SIZE_THRESHOLD_MEDIUM (32*1024)
1173 #define SCST_IO_SIZE_THRESHOLD_LARGE (128*1024)
1174 #define SCST_IO_SIZE_THRESHOLD_VERY_LARGE (512*1024)
1176 #define SCST_LATENCY_STAT_INDEX_SMALL 0
1177 #define SCST_LATENCY_STAT_INDEX_MEDIUM 1
1178 #define SCST_LATENCY_STAT_INDEX_LARGE 2
1179 #define SCST_LATENCY_STAT_INDEX_VERY_LARGE 3
1180 #define SCST_LATENCY_STAT_INDEX_OTHER 4
1181 #define SCST_LATENCY_STATS_NUM (SCST_LATENCY_STAT_INDEX_OTHER + 1)
1183 #endif /* CONFIG_SCST_MEASURE_LATENCY */
1185 struct scst_session {
1187 * Initialization phase, one of SCST_SESS_IPH_* constants, protected by
1192 struct scst_tgt *tgt; /* corresponding target */
1194 /* Used for storage of target driver private stuff */
1197 unsigned long sess_aflags; /* session's async flags */
1200 * Hash list of tgt_dev's for this session, protected by scst_mutex
1201 * and suspended activity
1203 struct list_head sess_tgt_dev_list_hash[TGT_DEV_HASH_SIZE];
1206 * List of cmds in this session. Used to find a cmd in the
1207 * session. Protected by sess_list_lock.
1209 struct list_head search_cmd_list;
1211 spinlock_t sess_list_lock; /* protects search_cmd_list, etc */
1214 * List of cmds in this in the state after PRE_XMIT_RESP. All the cmds
1215 * moved here from search_cmd_list. Needed for hw_pending_work.
1216 * Protected by sess_list_lock.
1218 struct list_head after_pre_xmit_cmd_list;
1220 atomic_t refcnt; /* get/put counter */
1223 * Alive commands for this session. ToDo: make it part of the common
1226 atomic_t sess_cmd_count;
1228 /* Access control for this session and list entry there */
1229 struct scst_acg *acg;
1231 /* List entry for the sessions list inside ACG */
1232 struct list_head acg_sess_list_entry;
1234 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
1235 struct delayed_work hw_pending_work;
1237 struct work_struct hw_pending_work;
1240 /* Name of attached initiator */
1241 const char *initiator_name;
1243 /* List entry of sessions per target */
1244 struct list_head sess_list_entry;
1246 /* List entry for the list that keeps session, waiting for the init */
1247 struct list_head sess_init_list_entry;
1250 * List entry for the list that keeps session, waiting for the shutdown
1252 struct list_head sess_shut_list_entry;
1255 * Lists of deferred during session initialization commands.
1256 * Protected by sess_list_lock.
1258 struct list_head init_deferred_cmd_list;
1259 struct list_head init_deferred_mcmd_list;
1262 * Shutdown phase, one of SCST_SESS_SPH_* constants, unprotected.
1263 * Async. relating to init_phase, must be a separate variable, because
1264 * session could be unregistered before async. registration is finished.
1266 unsigned long shut_phase;
1268 /* Used if scst_unregister_session() called in wait mode */
1269 struct completion *shutdown_compl;
1271 /* Set if sess_kobj was initialized */
1272 unsigned int sess_kobj_initialized:1;
1274 struct kobject sess_kobj; /* kobject for this struct */
1277 * Functions and data for user callbacks from scst_register_session()
1278 * and scst_unregister_session()
1280 void *reg_sess_data;
1281 void (*init_result_fn) (struct scst_session *sess, void *data,
1283 void (*unreg_done_fn) (struct scst_session *sess);
1285 #ifdef CONFIG_SCST_MEASURE_LATENCY
1287 * Must be the last to allow to work with drivers who don't know
1288 * about this config time option.
1290 spinlock_t lat_lock;
1291 uint64_t scst_time, tgt_time, dev_time;
1292 unsigned int processed_cmds;
1293 uint64_t min_scst_time, min_tgt_time, min_dev_time;
1294 uint64_t max_scst_time, max_tgt_time, max_dev_time;
1295 struct scst_ext_latency_stat sess_latency_stat[SCST_LATENCY_STATS_NUM];
1299 struct scst_cmd_lists {
1300 spinlock_t cmd_list_lock;
1301 struct list_head active_cmd_list;
1302 wait_queue_head_t cmd_list_waitQ;
1303 struct list_head lists_list_entry;
1307 /* List entry for below *_cmd_lists */
1308 struct list_head cmd_list_entry;
1310 /* Pointer to lists of commands with the lock */
1311 struct scst_cmd_lists *cmd_lists;
1315 struct scst_session *sess; /* corresponding session */
1317 /* Cmd state, one of SCST_CMD_STATE_* constants */
1320 /*************************************************************
1322 *************************************************************/
1325 * Set if expected_sn should be incremented, i.e. cmd was sent
1328 unsigned int sent_for_exec:1;
1330 /* Set if the cmd's action is completed */
1331 unsigned int completed:1;
1333 /* Set if we should ignore Unit Attention in scst_check_sense() */
1334 unsigned int ua_ignore:1;
1336 /* Set if cmd is being processed in atomic context */
1337 unsigned int atomic:1;
1339 /* Set if this command was sent in double UA possible state */
1340 unsigned int double_ua_possible:1;
1342 /* Set if this command contains status */
1343 unsigned int is_send_status:1;
1345 /* Set if cmd is being retried */
1346 unsigned int retry:1;
1348 /* Set if cmd is internally generated */
1349 unsigned int internal:1;
1351 /* Set if the device was blocked by scst_inc_on_dev_cmd() (for debug) */
1352 unsigned int inc_blocking:1;
1354 /* Set if the device should be unblocked after cmd's finish */
1355 unsigned int needs_unblocking:1;
1357 /* Set if scst_dec_on_dev_cmd() call is needed on the cmd's finish */
1358 unsigned int dec_on_dev_needed:1;
1360 /* Set if cmd is queued as hw pending */
1361 unsigned int cmd_hw_pending:1;
1364 * Set if the target driver wants to alloc data buffers on its own.
1365 * In this case alloc_data_buf() must be provided in the target driver
1368 unsigned int tgt_need_alloc_data_buf:1;
1371 * Set by SCST if the custom data buffer allocation by the target driver
1374 unsigned int tgt_data_buf_alloced:1;
1376 /* Set if custom data buffer allocated by dev handler */
1377 unsigned int dh_data_buf_alloced:1;
1379 /* Set if the target driver called scst_set_expected() */
1380 unsigned int expected_values_set:1;
1383 * Set if the SG buffer was modified by scst_set_resp_data_len()
1385 unsigned int sg_buff_modified:1;
1388 * Set if scst_cmd_init_stage1_done() called and the target
1389 * want that preprocessing_done() will be called
1391 unsigned int preprocessing_only:1;
1393 /* Set if cmd's SN was set */
1394 unsigned int sn_set:1;
1396 /* Set if hq_cmd_count was incremented */
1397 unsigned int hq_cmd_inced:1;
1400 * Set if scst_cmd_init_stage1_done() called and the target wants
1401 * that the SN for the cmd won't be assigned until scst_restart_cmd()
1403 unsigned int set_sn_on_restart_cmd:1;
1405 /* Set if the cmd's must not use sgv cache for data buffer */
1406 unsigned int no_sgv:1;
1409 * Set if target driver may need to call dma_sync_sg() or similar
1410 * function before transferring cmd' data to the target device
1413 unsigned int may_need_dma_sync:1;
1415 /* Set if the cmd was done or aborted out of its SN */
1416 unsigned int out_of_sn:1;
1418 /* Set if increment expected_sn in cmd->scst_cmd_done() */
1419 unsigned int inc_expected_sn_on_done:1;
1421 /* Set if tgt_sn field is valid */
1422 unsigned int tgt_sn_set:1;
1424 /* Set if cmd is done */
1425 unsigned int done:1;
1427 /* Set if cmd is finished */
1428 unsigned int finished:1;
1431 * Set if the cmd was delayed by task management debugging code.
1432 * Used only if CONFIG_SCST_DEBUG_TM is on.
1434 unsigned int tm_dbg_delayed:1;
1437 * Set if the cmd must be ignored by task management debugging code.
1438 * Used only if CONFIG_SCST_DEBUG_TM is on.
1440 unsigned int tm_dbg_immut:1;
1442 /**************************************************************/
1444 unsigned long cmd_flags; /* cmd's async flags */
1446 /* Keeps status of cmd's status/data delivery to remote initiator */
1447 int delivery_status;
1449 struct scst_tgt_template *tgtt; /* to save extra dereferences */
1450 struct scst_tgt *tgt; /* to save extra dereferences */
1451 struct scst_device *dev; /* to save extra dereferences */
1453 struct scst_tgt_dev *tgt_dev; /* corresponding device for this cmd */
1455 uint64_t lun; /* LUN for this cmd */
1457 unsigned long start_time;
1459 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1460 struct scsi_request *scsi_req; /* SCSI request */
1463 /* List entry for tgt_dev's SN related lists */
1464 struct list_head sn_cmd_list_entry;
1466 /* Cmd's serial number, used to execute cmd's in order of arrival */
1469 /* The corresponding sn_slot in tgt_dev->sn_slots */
1472 /* List entry for sess's search_cmd_list and after_pre_xmit_cmd_list */
1473 struct list_head sess_cmd_list_entry;
1476 * Used to found the cmd by scst_find_cmd_by_tag(). Set by the
1477 * target driver on the cmd's initialization time
1481 uint32_t tgt_sn; /* SN set by target driver (for TM purposes) */
1483 /* CDB and its len */
1484 uint8_t cdb[SCST_MAX_CDB_SIZE];
1485 short cdb_len; /* it might be -1 */
1486 unsigned short ext_cdb_len;
1489 enum scst_cdb_flags op_flags;
1490 const char *op_name;
1492 enum scst_cmd_queue_type queue_type;
1494 int timeout; /* CDB execution timeout in seconds */
1495 int retries; /* Amount of retries that will be done by SCSI mid-level */
1497 /* SCSI data direction, one of SCST_DATA_* constants */
1498 scst_data_direction data_direction;
1500 /* Remote initiator supplied values, if any */
1501 scst_data_direction expected_data_direction;
1502 int expected_transfer_len;
1503 int expected_in_transfer_len; /* for bidi writes */
1506 * Cmd data length. Could be different from bufflen for commands like
1507 * VERIFY, which transfer different amount of data (if any), than
1512 /* Completition routine */
1513 void (*scst_cmd_done) (struct scst_cmd *cmd, int next_state,
1514 enum scst_exec_context pref_context);
1516 struct sgv_pool_obj *sgv; /* sgv object */
1517 int bufflen; /* cmd buffer length */
1518 struct scatterlist *sg; /* cmd data buffer SG vector */
1519 int sg_cnt; /* SG segments count */
1522 * Response data length in data buffer. This field must not be set
1523 * directly, use scst_set_resp_data_len() for that
1527 /* scst_get_sg_buf_[first,next]() support */
1528 int get_sg_buf_entry_num;
1530 /* Bidirectional transfers support */
1531 int in_bufflen; /* WRITE buffer length */
1532 struct sgv_pool_obj *in_sgv; /* WRITE sgv object */
1533 struct scatterlist *in_sg; /* WRITE data buffer SG vector */
1534 int in_sg_cnt; /* WRITE SG segments count */
1537 * Used if both target driver and dev handler request own memory
1538 * allocation. In other cases, both are equal to sg and sg_cnt
1541 * If target driver requests own memory allocations, it MUST use
1542 * functions scst_cmd_get_tgt_sg*() to get sg and sg_cnt! Otherwise,
1543 * it may use functions scst_cmd_get_sg*().
1545 struct scatterlist *tgt_sg;
1547 struct scatterlist *tgt_in_sg; /* bidirectional */
1548 int tgt_in_sg_cnt; /* bidirectional */
1551 * The status fields in case of errors must be set using
1552 * scst_set_cmd_error_status()!
1554 uint8_t status; /* status byte from target device */
1555 uint8_t msg_status; /* return status from host adapter itself */
1556 uint8_t host_status; /* set by low-level driver to indicate status */
1557 uint8_t driver_status; /* set by mid-level */
1559 uint8_t *sense; /* pointer to sense buffer */
1560 unsigned short sense_valid_len; /* length of valid sense data */
1561 unsigned short sense_buflen; /* length of the sense buffer, if any */
1563 /* Start time when cmd was sent to rdy_to_xfer() or xmit_response() */
1564 unsigned long hw_pending_start;
1566 /* Used for storage of target driver private stuff */
1569 /* Used for storage of dev handler private stuff */
1573 * Used to restore the SG vector if it was modified by
1574 * scst_set_resp_data_len()
1576 int orig_sg_cnt, orig_sg_entry, orig_entry_len;
1578 /* Used to retry commands in case of double UA */
1579 int dbl_ua_orig_resp_data_len, dbl_ua_orig_data_direction;
1581 /* List corresponding mgmt cmd, if any, protected by sess_list_lock */
1582 struct list_head mgmt_cmd_list;
1584 /* List entry for dev's blocked_cmd_list */
1585 struct list_head blocked_cmd_list_entry;
1587 struct scst_cmd *orig_cmd; /* Used to issue REQUEST SENSE */
1589 #ifdef CONFIG_SCST_MEASURE_LATENCY
1591 * Must be the last to allow to work with drivers who don't know
1592 * about this config time option.
1594 uint64_t start, curr_start, parse_time, alloc_buf_time;
1595 uint64_t restart_waiting_time, rdy_to_xfer_time;
1596 uint64_t pre_exec_time, exec_time, dev_done_time;
1597 uint64_t xmit_time, tgt_on_free_time, dev_on_free_time;
1601 struct scst_rx_mgmt_params {
1609 unsigned char tag_set;
1610 unsigned char lun_set;
1611 unsigned char cmd_sn_set;
1614 struct scst_mgmt_cmd_stub {
1615 struct scst_mgmt_cmd *mcmd;
1617 /* List entry in cmd->mgmt_cmd_list */
1618 struct list_head cmd_mgmt_cmd_list_entry;
1621 struct scst_mgmt_cmd {
1622 /* List entry for *_mgmt_cmd_list */
1623 struct list_head mgmt_cmd_list_entry;
1625 struct scst_session *sess;
1627 /* Mgmt cmd state, one of SCST_MCMD_STATE_* constants */
1632 unsigned int completed:1; /* set, if the mcmd is completed */
1633 /* Set if device(s) should be unblocked after mcmd's finish */
1634 unsigned int needs_unblocking:1;
1635 unsigned int lun_set:1; /* set, if lun field is valid */
1636 unsigned int cmd_sn_set:1; /* set, if cmd_sn field is valid */
1637 /* set, if scst_mgmt_affected_cmds_done was called */
1638 unsigned int affected_cmds_done_called:1;
1641 * Number of commands to finish before sending response,
1642 * protected by scst_mcmd_lock
1644 int cmd_finish_wait_count;
1647 * Number of commands to complete (done) before resetting reservation,
1648 * protected by scst_mcmd_lock
1650 int cmd_done_wait_count;
1652 /* Number of completed commands, protected by scst_mcmd_lock */
1653 int completed_cmd_count;
1655 uint64_t lun; /* LUN for this mgmt cmd */
1656 /* or (and for iSCSI) */
1657 uint64_t tag; /* tag of the corresponding cmd */
1659 uint32_t cmd_sn; /* affected command's highest SN */
1661 /* corresponding cmd (to be aborted, found by tag) */
1662 struct scst_cmd *cmd_to_abort;
1664 /* corresponding device for this mgmt cmd (found by lun) */
1665 struct scst_tgt_dev *mcmd_tgt_dev;
1667 /* completition status, one of the SCST_MGMT_STATUS_* constants */
1670 /* Used for storage of target driver private stuff */
1674 struct scst_device {
1675 struct scst_dev_type *handler; /* corresponding dev handler */
1677 struct scst_mem_lim dev_mem_lim;
1679 unsigned short type; /* SCSI type of the device */
1681 /*************************************************************
1682 ** Dev's flags. Updates serialized by dev_lock or suspended
1684 *************************************************************/
1686 /* Set if dev is RESERVED */
1687 unsigned short dev_reserved:1;
1689 /* Set if double reset UA is possible */
1690 unsigned short dev_double_ua_possible:1;
1692 /* If set, dev is read only */
1693 unsigned short rd_only:1;
1695 /**************************************************************/
1697 /*************************************************************
1698 ** Dev's control mode page related values. Updates serialized
1699 ** by scst_block_dev(). It's long to not interfere with the
1701 *************************************************************/
1703 unsigned long queue_alg:4;
1704 unsigned long tst:3;
1705 unsigned long tas:1;
1706 unsigned long swp:1;
1707 unsigned long d_sense:1;
1710 * Set if device implements own ordered commands management. If not set
1711 * and queue_alg is SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER,
1712 * expected_sn will be incremented only after commands finished.
1714 unsigned long has_own_order_mgmt:1;
1716 /**************************************************************/
1718 /* Used for storage of dev handler private stuff */
1721 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1722 /* Used to translate SCSI's cmd to SCST's cmd */
1723 struct gendisk *rq_disk;
1726 /* Corresponding real SCSI device, could be NULL for virtual devices */
1727 struct scsi_device *scsi_dev;
1729 /* Pointer to lists of commands with the lock */
1730 struct scst_cmd_lists *p_cmd_lists;
1732 /* Lists of commands with lock, if dedicated threads are used */
1733 struct scst_cmd_lists cmd_lists;
1735 /* Per-device dedicated IO context */
1736 struct io_context *dev_io_ctx;
1738 /* How many cmds alive on this dev */
1739 atomic_t dev_cmd_count;
1741 /* How many write cmds alive on this dev. Temporary, ToDo */
1742 atomic_t write_cmd_count;
1744 spinlock_t dev_lock; /* device lock */
1747 * How many times device was blocked for new cmds execution.
1748 * Protected by dev_lock
1753 * How many there are "on_dev" commands, i.e. ones those are being
1754 * executed by the underlying SCSI/virtual device.
1756 atomic_t on_dev_count;
1758 struct list_head blocked_cmd_list; /* protected by dev_lock */
1760 /* Used to wait for requested amount of "on_dev" commands */
1761 wait_queue_head_t on_dev_waitQ;
1763 /* A list entry used during TM, protected by scst_mutex */
1764 struct list_head tm_dev_list_entry;
1766 /* Virtual device internal ID */
1769 /* Pointer to virtual device name, for convenience only */
1772 /* List entry in global devices list */
1773 struct list_head dev_list_entry;
1776 * List of tgt_dev's, one per session, protected by scst_mutex or
1777 * dev_lock for reads and both for writes
1779 struct list_head dev_tgt_dev_list;
1781 /* List of acg_dev's, one per acg, protected by scst_mutex */
1782 struct list_head dev_acg_dev_list;
1784 /* List of dedicated threads, protected by scst_mutex */
1785 struct list_head threads_list;
1790 /* Set if tgt_kobj was initialized */
1791 unsigned int dev_kobj_initialized:1;
1793 struct kobject dev_kobj; /* kobject for this struct */
1794 struct kobject *dev_exp_kobj; /* exported groups */
1796 /* Export number in the dev's sysfs list. Protected by scst_mutex */
1797 int dev_exported_lun_num;
1801 * Used to store threads local tgt_dev specific data
1803 struct scst_thr_data_hdr {
1804 /* List entry in tgt_dev->thr_data_list */
1805 struct list_head thr_data_list_entry;
1806 struct task_struct *owner_thr; /* the owner thread */
1808 /* Function that will be called on the tgt_dev destruction */
1809 void (*free_fn) (struct scst_thr_data_hdr *data);
1813 * Used to store per-session specific device information
1815 struct scst_tgt_dev {
1816 /* List entry in sess->sess_tgt_dev_list_hash */
1817 struct list_head sess_tgt_dev_list_entry;
1819 struct scst_device *dev; /* to save extra dereferences */
1820 uint64_t lun; /* to save extra dereferences */
1823 struct sgv_pool *pool;
1826 unsigned long tgt_dev_flags; /* tgt_dev's async flags */
1828 /* Used for storage of dev handler private stuff */
1831 /* How many cmds alive on this dev in this session */
1832 atomic_t tgt_dev_cmd_count;
1835 * Used to execute cmd's in order of arrival, honoring SCSI task
1838 * Protected by sn_lock, except expected_sn, which is protected by
1839 * itself. Curr_sn must have the same size as expected_sn to
1840 * overflow simultaneously.
1844 unsigned long expected_sn;
1845 unsigned long curr_sn;
1847 struct list_head deferred_cmd_list;
1848 struct list_head skipped_sn_list;
1851 * Set if the prev cmd was ORDERED. Size must allow unprotected
1852 * modifications independant to the neighbour fields.
1854 unsigned long prev_cmd_ordered;
1856 int num_free_sn_slots; /* if it's <0, then all slots are busy */
1857 atomic_t *cur_sn_slot;
1858 atomic_t sn_slots[15];
1860 /* List of scst_thr_data_hdr and lock */
1861 spinlock_t thr_data_lock;
1862 struct list_head thr_data_list;
1864 /* Per-(device, session) dedicated IO context */
1865 struct io_context *tgt_dev_io_ctx;
1867 spinlock_t tgt_dev_lock; /* per-session device lock */
1869 /* List of UA's for this device, protected by tgt_dev_lock */
1870 struct list_head UA_list;
1872 struct scst_session *sess; /* corresponding session */
1873 struct scst_acg_dev *acg_dev; /* corresponding acg_dev */
1875 /* List entry in dev->dev_tgt_dev_list */
1876 struct list_head dev_tgt_dev_list_entry;
1878 /* Internal tmp list entry */
1879 struct list_head extra_tgt_dev_list_entry;
1881 /* Set if INQUIRY DATA HAS CHANGED UA is needed */
1882 unsigned int inq_changed_ua_needed:1;
1885 * Stored Unit Attention sense and its length for possible
1886 * subsequent REQUEST SENSE. Both protected by tgt_dev_lock.
1888 unsigned short tgt_dev_valid_sense_len;
1889 uint8_t tgt_dev_sense[SCST_SENSE_BUFFERSIZE];
1891 #ifdef CONFIG_SCST_MEASURE_LATENCY
1893 * Must be the last to allow to work with drivers who don't know
1894 * about this config time option.
1896 * Protected by sess->lat_lock.
1898 uint64_t scst_time, tgt_time, dev_time;
1899 unsigned int processed_cmds;
1900 struct scst_ext_latency_stat dev_latency_stat[SCST_LATENCY_STATS_NUM];
1905 * Used to store ACG-specific device information, like LUN
1907 struct scst_acg_dev {
1908 struct scst_device *dev; /* corresponding device */
1910 uint64_t lun; /* device's LUN in this acg */
1912 /* If set, the corresponding LU is read only */
1913 unsigned int rd_only:1;
1915 /* Set if acg_dev_kobj was initialized */
1916 unsigned int acg_dev_kobj_initialized:1;
1918 struct scst_acg *acg; /* parent acg */
1920 /* List entry in dev->dev_acg_dev_list */
1921 struct list_head dev_acg_dev_list_entry;
1923 /* List entry in acg->acg_dev_list */
1924 struct list_head acg_dev_list_entry;
1926 /* kobject for this structure */
1927 struct kobject acg_dev_kobj;
1931 * ACG - access control group. Used to store group related
1932 * control information.
1935 /* List of acg_dev's in this acg, protected by scst_mutex */
1936 struct list_head acg_dev_list;
1938 /* List of attached sessions, protected by scst_mutex */
1939 struct list_head acg_sess_list;
1941 /* List of attached acn's, protected by scst_mutex */
1942 struct list_head acn_list;
1944 /* List entry in scst_acg_list */
1945 struct list_head scst_acg_list_entry;
1947 /* Name of this acg */
1948 const char *acg_name;
1950 #ifdef CONFIG_SCST_PROC
1951 /* The pointer to the /proc directory entry */
1952 struct proc_dir_entry *acg_proc_root;
1957 * ACN - access control name. Used to store names, by which
1958 * incoming sessions will be assigned to appropriate ACG.
1961 /* Initiator's name */
1963 /* List entry in acg->acn_list */
1964 struct list_head acn_list_entry;
1968 * Used to store per-session UNIT ATTENTIONs
1970 struct scst_tgt_dev_UA {
1971 /* List entry in tgt_dev->UA_list */
1972 struct list_head UA_list_entry;
1974 /* Set if UA is global for session */
1975 unsigned short global_UA:1;
1977 /* Unit Attention valid sense len */
1978 unsigned short UA_valid_sense_len;
1979 /* Unit Attention sense buf */
1980 uint8_t UA_sense_buffer[SCST_SENSE_BUFFERSIZE];
1983 /* Used to deliver AENs */
1985 int event_fn; /* AEN fn */
1987 struct scst_session *sess; /* corresponding session */
1988 uint64_t lun; /* corresponding LUN in SCSI form */
1994 uint8_t aen_sense[SCST_STANDARD_SENSE_LEN];
1998 /* Keeps status of AEN's delivery to remote initiator */
1999 int delivery_status;
2002 #ifndef smp_mb__after_set_bit
2003 /* There is no smp_mb__after_set_bit() in the kernel */
2004 #define smp_mb__after_set_bit() smp_mb()
2008 * Registers target template
2009 * Returns 0 on success or appropriate error code otherwise
2011 int __scst_register_target_template(struct scst_tgt_template *vtt,
2012 const char *version);
2013 static inline int scst_register_target_template(struct scst_tgt_template *vtt)
2015 return __scst_register_target_template(vtt, SCST_INTERFACE_VERSION);
2019 * Unregisters target template
2021 void scst_unregister_target_template(struct scst_tgt_template *vtt);
2024 * Registers and returns target adapter
2025 * Returns new target structure on success or NULL otherwise.
2027 * If parameter "target_name" isn't NULL, then security group with name
2028 * "Default_##target_name", if created, will be used as the default
2029 * instead of "Default" one for all initiators not assigned to any other group.
2031 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
2032 const char *target_name);
2035 * Unregisters target adapter
2037 void scst_unregister(struct scst_tgt *tgt);
2040 * Registers and returns a session
2042 * Returns new session on success or NULL otherwise
2046 * atomic - true, if the function called in the atomic context. If false,
2047 * this function will block until the session registration is completed.
2048 * initiator_name - remote initiator's name, any NULL-terminated string,
2049 * e.g. iSCSI name, which used as the key to found appropriate access
2050 * control group. Could be NULL, then the default target's LUNs are used.
2051 * data - any target driver supplied data
2052 * result_fn - pointer to the function that will be
2053 * asynchronously called when session initialization finishes.
2054 * Can be NULL. Parameters:
2056 * - data - target driver supplied to scst_register_session() data
2057 * - result - session initialization result, 0 on success or
2058 * appropriate error code otherwise
2060 * Note: A session creation and initialization is a complex task,
2061 * which requires sleeping state, so it can't be fully done
2062 * in interrupt context. Therefore the "bottom half" of it, if
2063 * scst_register_session() is called from atomic context, will be
2064 * done in SCST thread context. In this case scst_register_session()
2065 * will return not completely initialized session, but the target
2066 * driver can supply commands to this session via scst_rx_cmd().
2067 * Those commands processing will be delayed inside SCST until
2068 * the session initialization is finished, then their processing
2069 * will be restarted. The target driver will be notified about
2070 * finish of the session initialization by function result_fn().
2071 * On success the target driver could do nothing, but if the
2072 * initialization fails, the target driver must ensure that
2073 * no more new commands being sent or will be sent to SCST after
2074 * result_fn() returns. All already sent to SCST commands for
2075 * failed session will be returned in xmit_response() with BUSY status.
2076 * In case of failure the driver shall call scst_unregister_session()
2077 * inside result_fn(), it will NOT be called automatically.
2079 struct scst_session *scst_register_session(struct scst_tgt *tgt, int atomic,
2080 const char *initiator_name, void *data,
2081 void (*result_fn) (struct scst_session *sess, void *data, int result));
2084 * Unregisters a session.
2086 * sess - session to be unregistered
2087 * wait - if true, instructs to wait until all commands, which
2088 * currently is being executed and belonged to the session, finished.
2089 * Otherwise, target driver should be prepared to receive
2090 * xmit_response() for the session's command after
2091 * scst_unregister_session() returns.
2092 * unreg_done_fn - pointer to the function that will be
2093 * asynchronously called when the last session's command finishes and
2094 * the session is about to be completely freed. Can be NULL.
2100 * - All outstanding commands will be finished regularly. After
2101 * scst_unregister_session() returned no new commands must be sent to
2102 * SCST via scst_rx_cmd().
2104 * - The caller must ensure that no scst_rx_cmd() or scst_rx_mgmt_fn_*() is
2105 * called in paralell with scst_unregister_session().
2107 * - Can be called before result_fn() of scst_register_session() called,
2108 * i.e. during the session registration/initialization.
2110 * - It is highly recommended to call scst_unregister_session() as soon as it
2111 * gets clear that session will be unregistered and not to wait until all
2112 * related commands finished. This function provides the wait functionality,
2113 * but it also starts recovering stuck commands, if there are any.
2114 * Otherwise, your target driver could wait for those commands forever.
2116 void scst_unregister_session(struct scst_session *sess, int wait,
2117 void (*unreg_done_fn) (struct scst_session *sess));
2120 * Registers dev handler driver
2121 * Returns 0 on success or appropriate error code otherwise
2123 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
2124 const char *version);
2125 static inline int scst_register_dev_driver(struct scst_dev_type *dev_type)
2127 return __scst_register_dev_driver(dev_type, SCST_INTERFACE_VERSION);
2131 * Unregisters dev handler driver
2133 void scst_unregister_dev_driver(struct scst_dev_type *dev_type);
2136 * Registers dev handler driver for virtual devices (eg VDISK)
2137 * Returns 0 on success or appropriate error code otherwise
2139 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
2140 const char *version);
2141 static inline int scst_register_virtual_dev_driver(
2142 struct scst_dev_type *dev_type)
2144 return __scst_register_virtual_dev_driver(dev_type,
2145 SCST_INTERFACE_VERSION);
2149 * Unregisters dev handler driver for virtual devices
2151 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type);
2154 * Creates and sends new command to SCST.
2155 * Must not be called in parallel with scst_unregister_session() for the
2156 * same sess. Returns the command on success or NULL otherwise
2158 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
2159 const uint8_t *lun, int lun_len, const uint8_t *cdb,
2160 int cdb_len, int atomic);
2163 * Notifies SCST that the driver finished its part of the command
2164 * initialization, and the command is ready for execution.
2165 * The second argument sets preferred command execition context.
2166 * See SCST_CONTEXT_* constants for details.
2170 * If cmd->set_sn_on_restart_cmd not set, this function, as well as
2171 * scst_cmd_init_stage1_done() and scst_restart_cmd(), must not be
2172 * called simultaneously for the same session (more precisely,
2173 * for the same session/LUN, i.e. tgt_dev), i.e. they must be
2174 * somehow externally serialized. This is needed to have lock free fast path in
2175 * scst_cmd_set_sn(). For majority of targets those functions are naturally
2176 * serialized by the single source of commands. Only iSCSI immediate commands
2177 * with multiple connections per session seems to be an exception. For it, some
2178 * mutex/lock shall be used for the serialization.
2180 void scst_cmd_init_done(struct scst_cmd *cmd,
2181 enum scst_exec_context pref_context);
2184 * Notifies SCST that the driver finished the first stage of the command
2185 * initialization, and the command is ready for execution, but after
2186 * SCST done the command's preprocessing preprocessing_done() function
2187 * should be called. The second argument sets preferred command execition
2188 * context. See SCST_CONTEXT_* constants for details.
2190 * See also scst_cmd_init_done() comment for the serialization requirements.
2192 static inline void scst_cmd_init_stage1_done(struct scst_cmd *cmd,
2193 enum scst_exec_context pref_context, int set_sn)
2195 cmd->preprocessing_only = 1;
2196 cmd->set_sn_on_restart_cmd = !set_sn;
2197 scst_cmd_init_done(cmd, pref_context);
2201 * Notifies SCST that the driver finished its part of the command's
2202 * preprocessing and it is ready for further processing.
2203 * The second argument sets data receiving completion status
2204 * (see SCST_PREPROCESS_STATUS_* constants for details)
2205 * The third argument sets preferred command execition context
2206 * (see SCST_CONTEXT_* constants for details).
2208 * See also scst_cmd_init_done() comment for the serialization requirements.
2210 void scst_restart_cmd(struct scst_cmd *cmd, int status,
2211 enum scst_exec_context pref_context);
2214 * Notifies SCST that the driver received all the necessary data
2215 * and the command is ready for further processing.
2216 * The second argument sets data receiving completion status
2217 * (see SCST_RX_STATUS_* constants for details)
2218 * The third argument sets preferred command execition context
2219 * (see SCST_CONTEXT_* constants for details)
2221 void scst_rx_data(struct scst_cmd *cmd, int status,
2222 enum scst_exec_context pref_context);
2225 * Notifies SCST that the driver sent the response and the command
2226 * can be freed now. Don't forget to set the delivery status, if it
2227 * isn't success, using scst_set_delivery_status() before calling
2228 * this function. The third argument sets preferred command execition
2229 * context (see SCST_CONTEXT_* constants for details)
2231 void scst_tgt_cmd_done(struct scst_cmd *cmd,
2232 enum scst_exec_context pref_context);
2235 * Creates new management command sends it for execution.
2236 * Must not be called in parallel with scst_unregister_session() for the
2237 * same sess. Returns 0 for success, error code otherwise.
2239 int scst_rx_mgmt_fn(struct scst_session *sess,
2240 const struct scst_rx_mgmt_params *params);
2243 * Creates new management command using tag and sends it for execution.
2244 * Can be used for SCST_ABORT_TASK only.
2245 * Must not be called in parallel with scst_unregister_session() for the
2246 * same sess. Returns 0 for success, error code otherwise.
2248 * Obsolete in favor of scst_rx_mgmt_fn()
2250 static inline int scst_rx_mgmt_fn_tag(struct scst_session *sess, int fn,
2251 uint64_t tag, int atomic, void *tgt_priv)
2253 struct scst_rx_mgmt_params params;
2255 BUG_ON(fn != SCST_ABORT_TASK);
2257 memset(¶ms, 0, sizeof(params));
2261 params.atomic = atomic;
2262 params.tgt_priv = tgt_priv;
2263 return scst_rx_mgmt_fn(sess, ¶ms);
2267 * Creates new management command using LUN and sends it for execution.
2268 * Currently can be used for any fn, except SCST_ABORT_TASK.
2269 * Must not be called in parallel with scst_unregister_session() for the
2270 * same sess. Returns 0 for success, error code otherwise.
2272 * Obsolete in favor of scst_rx_mgmt_fn()
2274 static inline int scst_rx_mgmt_fn_lun(struct scst_session *sess, int fn,
2275 const uint8_t *lun, int lun_len, int atomic, void *tgt_priv)
2277 struct scst_rx_mgmt_params params;
2279 BUG_ON(fn == SCST_ABORT_TASK);
2281 memset(¶ms, 0, sizeof(params));
2284 params.lun_len = lun_len;
2286 params.atomic = atomic;
2287 params.tgt_priv = tgt_priv;
2288 return scst_rx_mgmt_fn(sess, ¶ms);
2292 * Provides various info about command's CDB.
2294 * Returns: 0 on success, <0 if command is unknown, >0 if command is invalid.
2296 int scst_get_cdb_info(struct scst_cmd *cmd);
2299 * Set error SCSI status in the command and prepares it for returning it
2301 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status);
2304 * Set error in the command and fill the sense buffer
2306 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq);
2309 * Sets BUSY or TASK QUEUE FULL status
2311 void scst_set_busy(struct scst_cmd *cmd);
2314 * Check if sense in the sense buffer, if any, in the correct format. If not,
2315 * convert it to the correct format.
2317 void scst_check_convert_sense(struct scst_cmd *cmd);
2320 * Sets initial Unit Attention for sess, replacing default scst_sense_reset_UA
2322 void scst_set_initial_UA(struct scst_session *sess, int key, int asc, int ascq);
2325 * Notifies SCST core that dev changed its capacity
2327 void scst_capacity_data_changed(struct scst_device *dev);
2330 * Finds a command based on the supplied tag comparing it with one
2331 * that previously set by scst_cmd_set_tag().
2332 * Returns the command on success or NULL otherwise
2334 struct scst_cmd *scst_find_cmd_by_tag(struct scst_session *sess, uint64_t tag);
2337 * Finds a command based on user supplied data and comparision
2338 * callback function, that should return true, if the command is found.
2339 * Returns the command on success or NULL otherwise
2341 struct scst_cmd *scst_find_cmd(struct scst_session *sess, void *data,
2342 int (*cmp_fn) (struct scst_cmd *cmd,
2346 * Translates SCST's data direction to DMA one from backend storage
2349 enum dma_data_direction scst_to_dma_dir(int scst_dir);
2352 * Translates SCST data direction to DMA data direction from the perspective
2353 * of the target device.
2355 enum dma_data_direction scst_to_tgt_dma_dir(int scst_dir);
2358 * Returns true, if cmd's CDB is locally handled by SCST and 0 otherwise.
2359 * Dev handlers parse() and dev_done() not called for such commands.
2361 static inline bool scst_is_cmd_local(struct scst_cmd *cmd)
2363 return (cmd->op_flags & SCST_LOCAL_CMD) != 0;
2366 /* Returns true, if cmd can deliver UA */
2367 static inline bool scst_is_ua_command(struct scst_cmd *cmd)
2369 return (cmd->op_flags & SCST_SKIP_UA) == 0;
2373 * Registers a virtual device.
2375 * dev_type - the device's device handler
2376 * dev_name - the new device name, NULL-terminated string. Must be uniq
2377 * among all virtual devices in the system. The value isn't
2378 * copied, only the reference is stored, so the value must
2379 * remain valid during the device lifetime.
2380 * Returns assinged to the device ID on success, or negative value otherwise
2382 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
2383 const char *dev_name);
2386 * Unegisters a virtual device.
2388 * id - the device's ID, returned by the registration function
2390 void scst_unregister_virtual_device(int id);
2393 * Get/Set functions for tgt's sg_tablesize
2395 static inline int scst_tgt_get_sg_tablesize(struct scst_tgt *tgt)
2397 return tgt->sg_tablesize;
2400 static inline void scst_tgt_set_sg_tablesize(struct scst_tgt *tgt, int val)
2402 tgt->sg_tablesize = val;
2406 * Get/Set functions for tgt's target private data
2408 static inline void *scst_tgt_get_tgt_priv(struct scst_tgt *tgt)
2410 return tgt->tgt_priv;
2413 static inline void scst_tgt_set_tgt_priv(struct scst_tgt *tgt, void *val)
2415 tgt->tgt_priv = val;
2419 * Get/Set functions for session's target private data
2421 static inline void *scst_sess_get_tgt_priv(struct scst_session *sess)
2423 return sess->tgt_priv;
2426 static inline void scst_sess_set_tgt_priv(struct scst_session *sess,
2429 sess->tgt_priv = val;
2433 * Returns TRUE if cmd is being executed in atomic context.
2435 * Note: checkpatch will complain on the use of in_atomic() below. You can
2436 * safely ignore this warning since in_atomic() is used here only for debugging
2439 static inline int scst_cmd_atomic(struct scst_cmd *cmd)
2441 int res = cmd->atomic;
2442 #ifdef CONFIG_SCST_EXTRACHECKS
2443 if (unlikely((in_atomic() || in_interrupt() || irqs_disabled()) &&
2445 printk(KERN_ERR "ERROR: atomic context and non-atomic cmd\n");
2454 static inline enum scst_exec_context __scst_estimate_context(bool direct)
2457 return SCST_CONTEXT_TASKLET;
2458 else if (irqs_disabled())
2459 return SCST_CONTEXT_THREAD;
2461 return direct ? SCST_CONTEXT_DIRECT :
2462 SCST_CONTEXT_DIRECT_ATOMIC;
2465 static inline enum scst_exec_context scst_estimate_context(void)
2467 return __scst_estimate_context(0);
2470 static inline enum scst_exec_context scst_estimate_context_direct(void)
2472 return __scst_estimate_context(1);
2475 /* Returns cmd's CDB */
2476 static inline const uint8_t *scst_cmd_get_cdb(struct scst_cmd *cmd)
2481 /* Returns cmd's CDB length */
2482 static inline int scst_cmd_get_cdb_len(struct scst_cmd *cmd)
2484 return cmd->cdb_len;
2487 /* Returns cmd's extended CDB */
2488 static inline const uint8_t *scst_cmd_get_ext_cdb(struct scst_cmd *cmd)
2490 return cmd->ext_cdb;
2493 /* Returns cmd's extended CDB length */
2494 static inline int scst_cmd_get_ext_cdb_len(struct scst_cmd *cmd)
2496 return cmd->ext_cdb_len;
2499 /* Sets cmd's extended CDB and its length */
2500 static inline void scst_cmd_set_ext_cdb(struct scst_cmd *cmd,
2501 uint8_t *ext_cdb, unsigned int ext_cdb_len)
2503 cmd->ext_cdb = ext_cdb;
2504 cmd->ext_cdb_len = ext_cdb_len;
2507 /* Returns cmd's session */
2508 static inline struct scst_session *scst_cmd_get_session(struct scst_cmd *cmd)
2513 /* Returns cmd's response data length */
2514 static inline int scst_cmd_get_resp_data_len(struct scst_cmd *cmd)
2516 return cmd->resp_data_len;
2519 /* Returns if status should be sent for cmd */
2520 static inline int scst_cmd_get_is_send_status(struct scst_cmd *cmd)
2522 return cmd->is_send_status;
2526 * Returns pointer to cmd's SG data buffer.
2528 * Usage of this function is not recommended, use scst_get_buf_*()
2529 * family of functions instead.
2531 static inline struct scatterlist *scst_cmd_get_sg(struct scst_cmd *cmd)
2537 * Returns cmd's sg_cnt.
2539 * Usage of this function is not recommended, use scst_get_buf_*()
2540 * family of functions instead.
2542 static inline int scst_cmd_get_sg_cnt(struct scst_cmd *cmd)
2548 * Returns cmd's data buffer length.
2550 * In case if you need to iterate over data in the buffer, usage of
2551 * this function is not recommended, use scst_get_buf_*()
2552 * family of functions instead.
2554 static inline unsigned int scst_cmd_get_bufflen(struct scst_cmd *cmd)
2556 return cmd->bufflen;
2560 * Returns pointer to cmd's bidirectional in (WRITE) SG data buffer.
2562 * Usage of this function is not recommended, use scst_get_in_buf_*()
2563 * family of functions instead.
2565 static inline struct scatterlist *scst_cmd_get_in_sg(struct scst_cmd *cmd)
2571 * Returns cmd's bidirectional in (WRITE) sg_cnt.
2573 * Usage of this function is not recommended, use scst_get_in_buf_*()
2574 * family of functions instead.
2576 static inline int scst_cmd_get_in_sg_cnt(struct scst_cmd *cmd)
2578 return cmd->in_sg_cnt;
2582 * Returns cmd's bidirectional in (WRITE) data buffer length.
2584 * In case if you need to iterate over data in the buffer, usage of
2585 * this function is not recommended, use scst_get_in_buf_*()
2586 * family of functions instead.
2588 static inline unsigned int scst_cmd_get_in_bufflen(struct scst_cmd *cmd)
2590 return cmd->in_bufflen;
2593 /* Returns pointer to cmd's target's SG data buffer */
2594 static inline struct scatterlist *scst_cmd_get_tgt_sg(struct scst_cmd *cmd)
2599 /* Returns cmd's target's sg_cnt */
2600 static inline int scst_cmd_get_tgt_sg_cnt(struct scst_cmd *cmd)
2602 return cmd->tgt_sg_cnt;
2605 /* Sets cmd's target's SG data buffer */
2606 static inline void scst_cmd_set_tgt_sg(struct scst_cmd *cmd,
2607 struct scatterlist *sg, int sg_cnt)
2610 cmd->tgt_sg_cnt = sg_cnt;
2611 cmd->tgt_data_buf_alloced = 1;
2614 /* Returns pointer to cmd's target's IN SG data buffer */
2615 static inline struct scatterlist *scst_cmd_get_in_tgt_sg(struct scst_cmd *cmd)
2617 return cmd->tgt_in_sg;
2620 /* Returns cmd's target's IN sg_cnt */
2621 static inline int scst_cmd_get_tgt_in_sg_cnt(struct scst_cmd *cmd)
2623 return cmd->tgt_in_sg_cnt;
2626 /* Sets cmd's target's IN SG data buffer */
2627 static inline void scst_cmd_set_tgt_in_sg(struct scst_cmd *cmd,
2628 struct scatterlist *sg, int sg_cnt)
2630 WARN_ON(!cmd->tgt_data_buf_alloced);
2632 cmd->tgt_in_sg = sg;
2633 cmd->tgt_in_sg_cnt = sg_cnt;
2636 /* Returns cmd's data direction */
2637 static inline scst_data_direction scst_cmd_get_data_direction(
2638 struct scst_cmd *cmd)
2640 return cmd->data_direction;
2643 /* Returns cmd's status byte from host device */
2644 static inline uint8_t scst_cmd_get_status(struct scst_cmd *cmd)
2649 /* Returns cmd's status from host adapter itself */
2650 static inline uint8_t scst_cmd_get_msg_status(struct scst_cmd *cmd)
2652 return cmd->msg_status;
2655 /* Returns cmd's status set by low-level driver to indicate its status */
2656 static inline uint8_t scst_cmd_get_host_status(struct scst_cmd *cmd)
2658 return cmd->host_status;
2661 /* Returns cmd's status set by SCSI mid-level */
2662 static inline uint8_t scst_cmd_get_driver_status(struct scst_cmd *cmd)
2664 return cmd->driver_status;
2667 /* Returns pointer to cmd's sense buffer */
2668 static inline uint8_t *scst_cmd_get_sense_buffer(struct scst_cmd *cmd)
2673 /* Returns cmd's valid sense length */
2674 static inline int scst_cmd_get_sense_buffer_len(struct scst_cmd *cmd)
2676 return cmd->sense_valid_len;
2680 * Get/Set functions for cmd's target SN
2682 static inline uint64_t scst_cmd_get_tag(struct scst_cmd *cmd)
2687 static inline void scst_cmd_set_tag(struct scst_cmd *cmd, uint64_t tag)
2693 * Get/Set functions for cmd's target private data.
2694 * Variant with *_lock must be used if target driver uses
2695 * scst_find_cmd() to avoid race with it, except inside scst_find_cmd()'s
2696 * callback, where lock is already taken.
2698 static inline void *scst_cmd_get_tgt_priv(struct scst_cmd *cmd)
2700 return cmd->tgt_priv;
2703 static inline void scst_cmd_set_tgt_priv(struct scst_cmd *cmd, void *val)
2705 cmd->tgt_priv = val;
2708 void *scst_cmd_get_tgt_priv_lock(struct scst_cmd *cmd);
2709 void scst_cmd_set_tgt_priv_lock(struct scst_cmd *cmd, void *val);
2712 * Get/Set functions for tgt_need_alloc_data_buf flag
2714 static inline int scst_cmd_get_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2716 return cmd->tgt_need_alloc_data_buf;
2719 static inline void scst_cmd_set_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2721 cmd->tgt_need_alloc_data_buf = 1;
2725 * Get/Set functions for tgt_data_buf_alloced flag
2727 static inline int scst_cmd_get_tgt_data_buff_alloced(struct scst_cmd *cmd)
2729 return cmd->tgt_data_buf_alloced;
2732 static inline void scst_cmd_set_tgt_data_buff_alloced(struct scst_cmd *cmd)
2734 cmd->tgt_data_buf_alloced = 1;
2738 * Get/Set functions for dh_data_buf_alloced flag
2740 static inline int scst_cmd_get_dh_data_buff_alloced(struct scst_cmd *cmd)
2742 return cmd->dh_data_buf_alloced;
2745 static inline void scst_cmd_set_dh_data_buff_alloced(struct scst_cmd *cmd)
2747 cmd->dh_data_buf_alloced = 1;
2751 * Get/Set functions for no_sgv flag
2753 static inline int scst_cmd_get_no_sgv(struct scst_cmd *cmd)
2758 static inline void scst_cmd_set_no_sgv(struct scst_cmd *cmd)
2764 * Get/Set functions for tgt_sn
2766 static inline int scst_cmd_get_tgt_sn(struct scst_cmd *cmd)
2768 BUG_ON(!cmd->tgt_sn_set);
2772 static inline void scst_cmd_set_tgt_sn(struct scst_cmd *cmd, uint32_t tgt_sn)
2774 cmd->tgt_sn_set = 1;
2775 cmd->tgt_sn = tgt_sn;
2779 * Returns 1 if the cmd was aborted, so its status is invalid and no
2780 * reply shall be sent to the remote initiator. A target driver should
2781 * only clear internal resources, associated with cmd.
2783 static inline int scst_cmd_aborted(struct scst_cmd *cmd)
2785 return test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags) &&
2786 !test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
2789 /* Returns sense data format for cmd's dev */
2790 static inline bool scst_get_cmd_dev_d_sense(struct scst_cmd *cmd)
2792 return (cmd->dev != NULL) ? cmd->dev->d_sense : 0;
2796 * Get/Set functions for expected data direction, transfer length
2797 * and its validity flag
2799 static inline int scst_cmd_is_expected_set(struct scst_cmd *cmd)
2801 return cmd->expected_values_set;
2804 static inline scst_data_direction scst_cmd_get_expected_data_direction(
2805 struct scst_cmd *cmd)
2807 return cmd->expected_data_direction;
2810 static inline int scst_cmd_get_expected_transfer_len(
2811 struct scst_cmd *cmd)
2813 return cmd->expected_transfer_len;
2816 static inline int scst_cmd_get_expected_in_transfer_len(
2817 struct scst_cmd *cmd)
2819 return cmd->expected_in_transfer_len;
2822 static inline void scst_cmd_set_expected(struct scst_cmd *cmd,
2823 scst_data_direction expected_data_direction,
2824 int expected_transfer_len)
2826 cmd->expected_data_direction = expected_data_direction;
2827 cmd->expected_transfer_len = expected_transfer_len;
2828 cmd->expected_values_set = 1;
2831 static inline void scst_cmd_set_expected_in_transfer_len(struct scst_cmd *cmd,
2832 int expected_in_transfer_len)
2834 WARN_ON(!cmd->expected_values_set);
2835 cmd->expected_in_transfer_len = expected_in_transfer_len;
2839 * Get/clear functions for cmd's may_need_dma_sync
2841 static inline int scst_get_may_need_dma_sync(struct scst_cmd *cmd)
2843 return cmd->may_need_dma_sync;
2846 static inline void scst_clear_may_need_dma_sync(struct scst_cmd *cmd)
2848 cmd->may_need_dma_sync = 0;
2852 * Get/set functions for cmd's delivery_status. It is one of
2853 * SCST_CMD_DELIVERY_* constants. It specifies the status of the
2854 * command's delivery to initiator.
2856 static inline int scst_get_delivery_status(struct scst_cmd *cmd)
2858 return cmd->delivery_status;
2861 static inline void scst_set_delivery_status(struct scst_cmd *cmd,
2862 int delivery_status)
2864 cmd->delivery_status = delivery_status;
2868 * Get/Set function for mgmt cmd's target private data
2870 static inline void *scst_mgmt_cmd_get_tgt_priv(struct scst_mgmt_cmd *mcmd)
2872 return mcmd->tgt_priv;
2875 static inline void scst_mgmt_cmd_set_tgt_priv(struct scst_mgmt_cmd *mcmd,
2878 mcmd->tgt_priv = val;
2881 /* Returns mgmt cmd's completition status (SCST_MGMT_STATUS_* constants) */
2882 static inline int scst_mgmt_cmd_get_status(struct scst_mgmt_cmd *mcmd)
2884 return mcmd->status;
2887 /* Returns mgmt cmd's TM fn */
2888 static inline int scst_mgmt_cmd_get_fn(struct scst_mgmt_cmd *mcmd)
2894 * Called by dev handler's task_mgmt_fn() to notify SCST core that mcmd
2895 * is going to complete asynchronously.
2897 void scst_prepare_async_mcmd(struct scst_mgmt_cmd *mcmd);
2900 * Called by dev handler to notify SCST core that async. mcmd is completed
2901 * with status "status".
2903 void scst_async_mcmd_completed(struct scst_mgmt_cmd *mcmd, int status);
2905 /* Returns AEN's fn */
2906 static inline int scst_aen_get_event_fn(struct scst_aen *aen)
2908 return aen->event_fn;
2911 /* Returns AEN's session */
2912 static inline struct scst_session *scst_aen_get_sess(struct scst_aen *aen)
2917 /* Returns AEN's LUN */
2918 static inline uint64_t scst_aen_get_lun(struct scst_aen *aen)
2923 /* Returns SCSI AEN's sense */
2924 static inline const uint8_t *scst_aen_get_sense(struct scst_aen *aen)
2926 return aen->aen_sense;
2929 /* Returns SCSI AEN's sense length */
2930 static inline int scst_aen_get_sense_len(struct scst_aen *aen)
2932 return aen->aen_sense_len;
2936 * Get/set functions for AEN's delivery_status. It is one of
2937 * SCST_AEN_RES_* constants. It specifies the status of the
2938 * command's delivery to initiator.
2940 static inline int scst_get_aen_delivery_status(struct scst_aen *aen)
2942 return aen->delivery_status;
2945 static inline void scst_set_aen_delivery_status(struct scst_aen *aen,
2948 aen->delivery_status = status;
2952 * Notifies SCST that the driver has sent the AEN and it
2953 * can be freed now. Don't forget to set the delivery status, if it
2954 * isn't success, using scst_set_aen_delivery_status() before calling
2957 void scst_aen_done(struct scst_aen *aen);
2959 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2962 * The macro's sg_page(), sg_virt(), sg_init_table(), sg_assign_page() and
2963 * sg_set_page() have been introduced in the 2.6.24 kernel. The definitions
2964 * below are backports of the 2.6.24 macro's for older kernels. There is one
2965 * exception however: when compiling SCST on a system with a pre-2.6.24 kernel
2966 * (e.g. RHEL 5.x) where the OFED kernel headers have been installed, do not
2967 * define the backported macro's because OFED has already defined these.
2970 #ifndef __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__
2972 static inline struct page *sg_page(struct scatterlist *sg)
2977 static inline void *sg_virt(struct scatterlist *sg)
2979 return page_address(sg_page(sg)) + sg->offset;
2982 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
2984 memset(sgl, 0, sizeof(*sgl) * nents);
2987 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
2992 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
2993 unsigned int len, unsigned int offset)
2995 sg_assign_page(sg, page);
2996 sg->offset = offset;
3000 static inline struct scatterlist *sg_next(struct scatterlist *sg)
3006 #endif /* __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__ */
3008 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
3010 static inline void sg_clear(struct scatterlist *sg)
3012 memset(sg, 0, sizeof(*sg));
3013 #ifdef CONFIG_DEBUG_SG
3014 sg->sg_magic = SG_MAGIC;
3018 enum scst_sg_copy_dir {
3019 SCST_SG_COPY_FROM_TARGET,
3020 SCST_SG_COPY_TO_TARGET
3024 * Copies data between cmd->tgt_sg and cmd->sg in direction defined by
3025 * copy_dir parameter.
3027 void scst_copy_sg(struct scst_cmd *cmd, enum scst_sg_copy_dir copy_dir);
3030 * Functions for access to the commands data (SG) buffer,
3031 * including HIGHMEM environment. Should be used instead of direct
3032 * access. Returns the mapped buffer length for success, 0 for EOD,
3033 * negative error code otherwise.
3035 * "Buf" argument returns the mapped buffer
3037 * The "put" function unmaps the buffer.
3039 static inline int __scst_get_buf(struct scst_cmd *cmd, struct scatterlist *sg,
3040 int sg_cnt, uint8_t **buf)
3043 int i = cmd->get_sg_buf_entry_num;
3047 if ((i >= sg_cnt) || unlikely(sg == NULL))
3050 *buf = page_address(sg_page(&sg[i]));
3051 *buf += sg[i].offset;
3054 cmd->get_sg_buf_entry_num++;
3060 static inline int scst_get_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3062 cmd->get_sg_buf_entry_num = 0;
3063 cmd->may_need_dma_sync = 1;
3064 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3067 static inline int scst_get_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3069 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3072 static inline void scst_put_buf(struct scst_cmd *cmd, void *buf)
3077 static inline int scst_get_in_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3079 cmd->get_sg_buf_entry_num = 0;
3080 cmd->may_need_dma_sync = 1;
3081 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3084 static inline int scst_get_in_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3086 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3089 static inline void scst_put_in_buf(struct scst_cmd *cmd, void *buf)
3095 * Returns approximate higher rounded buffers count that
3096 * scst_get_buf_[first|next]() return.
3098 static inline int scst_get_buf_count(struct scst_cmd *cmd)
3100 return (cmd->sg_cnt == 0) ? 1 : cmd->sg_cnt;
3104 * Returns approximate higher rounded buffers count that
3105 * scst_get_in_buf_[first|next]() return.
3107 static inline int scst_get_in_buf_count(struct scst_cmd *cmd)
3109 return (cmd->in_sg_cnt == 0) ? 1 : cmd->in_sg_cnt;
3112 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23) && !defined(BACKPORT_LINUX_WORKQUEUE_TO_2_6_19)
3113 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
3114 static inline int cancel_delayed_work_sync(struct delayed_work *work)
3116 static inline int cancel_delayed_work_sync(struct work_struct *work)
3121 res = cancel_delayed_work(work);
3122 flush_scheduled_work();
3128 * Suspends and resumes any activity.
3129 * Function scst_suspend_activity() doesn't return 0, until there are any
3130 * active commands (state after SCST_CMD_STATE_INIT). If "interruptible"
3131 * is true, it returns after SCST_SUSPENDING_TIMEOUT or if it was interrupted
3132 * by a signal with the coresponding error status < 0. If "interruptible"
3133 * is false, it will wait virtually forever.
3135 * New arriving commands stay in that state until scst_resume_activity()
3138 int scst_suspend_activity(bool interruptible);
3139 void scst_resume_activity(void);
3142 * Main SCST commands processing routing. Must be used only by dev handlers.
3143 * Argument atomic is true if function called in atomic context.
3145 void scst_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3148 * SCST commands processing routine, which should be called by dev handler
3149 * after its parse() callback returned SCST_CMD_STATE_STOP. Arguments
3150 * the same as for scst_process_active_cmd().
3152 void scst_post_parse_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3155 * Checks if command can be executed (reservations, etc.) or there are local
3156 * events, like pending UAs. Returns < 0 if command must be aborted, > 0 if
3157 * there is an event and command should be immediately completed, or 0
3160 * !! Dev handlers implementing exec() callback must call this function there !!
3161 * !! just before the actual command's execution !!
3163 int scst_check_local_events(struct scst_cmd *cmd);
3166 * Returns the next state of the SCSI target state machine in case if command's
3167 * completed abnormally.
3169 int scst_get_cmd_abnormal_done_state(const struct scst_cmd *cmd);
3172 * Sets state of the SCSI target state machine in case if command's completed
3175 void scst_set_cmd_abnormal_done_state(struct scst_cmd *cmd);
3177 struct scst_trace_log {
3182 #ifdef CONFIG_SCST_PROC
3185 * Returns target driver's root entry in SCST's /proc hierarchy.
3186 * The driver can create own files/directories here, which should
3187 * be deleted in the driver's release().
3189 struct proc_dir_entry *scst_proc_get_tgt_root(
3190 struct scst_tgt_template *vtt);
3193 * Returns device handler's root entry in SCST's /proc hierarchy.
3194 * The driver can create own files/directories here, which should
3195 * be deleted in the driver's detach()/release().
3197 struct proc_dir_entry *scst_proc_get_dev_type_root(
3198 struct scst_dev_type *dtt);
3201 ** Two library functions and the structure to help the drivers
3202 ** that use scst_debug.* facilities manage "trace_level" /proc entry.
3203 ** The functions service "standard" log levels and allow to work
3204 ** with driver specific levels, which should be passed inside as
3205 ** NULL-terminated array of struct scst_trace_log's, where:
3206 ** - val - the level's numeric value
3207 ** - token - its string representation
3210 int scst_proc_log_entry_read(struct seq_file *seq, unsigned long log_level,
3211 const struct scst_trace_log *tbl);
3212 int scst_proc_log_entry_write(struct file *file, const char __user *buf,
3213 unsigned long length, unsigned long *log_level,
3214 unsigned long default_level, const struct scst_trace_log *tbl);
3217 * helper data structure and function to create proc entry.
3219 struct scst_proc_data {
3220 const struct file_operations seq_op;
3221 int (*show)(struct seq_file *, void *);
3225 int scst_single_seq_open(struct inode *inode, struct file *file);
3227 struct proc_dir_entry *scst_create_proc_entry(struct proc_dir_entry *root,
3228 const char *name, struct scst_proc_data *pdata);
3230 #define SCST_DEF_RW_SEQ_OP(x) \
3232 .owner = THIS_MODULE, \
3233 .open = scst_single_seq_open,\
3236 .llseek = seq_lseek, \
3237 .release = single_release, \
3240 #else /* CONFIG_SCST_PROC */
3243 * Returns target driver's root sysfs kobject.
3244 * The driver can create own files/directories/links here.
3246 static inline struct kobject *scst_sysfs_get_tgtt_kobj(
3247 struct scst_tgt_template *tgtt)
3249 return &tgtt->tgtt_kobj;
3253 * Returns target's root sysfs kobject.
3254 * The driver can create own files/directories/links here.
3256 static inline struct kobject *scst_sysfs_get_tgt_kobj(
3257 struct scst_tgt *tgt)
3259 return &tgt->tgt_kobj;
3263 * Returns device handler's root sysfs kobject.
3264 * The driver can create own files/directories/links here.
3266 static inline struct kobject *scst_sysfs_get_devt_kobj(
3267 struct scst_dev_type *devt)
3269 return &devt->devt_kobj;
3273 * Returns device's root sysfs kobject.
3274 * The driver can create own files/directories/links here.
3276 static inline struct kobject *scst_sysfs_get_dev_kobj(
3277 struct scst_device *dev)
3279 return &dev->dev_kobj;
3282 #endif /* CONFIG_SCST_PROC */
3284 /* Returns target name */
3285 static inline const char *scst_get_tgt_name(const struct scst_tgt *tgt)
3287 return tgt->tgt_name;
3291 * Adds and deletes (stops) num of global SCST's threads. Returns 0 on
3292 * success, error code otherwise.
3294 int scst_add_global_threads(int num);
3295 void scst_del_global_threads(int num);
3297 int scst_alloc_sense(struct scst_cmd *cmd, int atomic);
3298 int scst_alloc_set_sense(struct scst_cmd *cmd, int atomic,
3299 const uint8_t *sense, unsigned int len);
3302 * Sets the corresponding field in the sense buffer taking sense type
3303 * into account. Returns resulting sense length.
3305 int scst_set_sense(uint8_t *buffer, int len, bool d_sense,
3306 int key, int asc, int ascq);
3309 * Returns true if the sense is valid and carrying a Unit Attention or
3312 bool scst_is_ua_sense(const uint8_t *sense, int len);
3315 * Returnes true if sense matches to (key, asc, ascq) and false otherwise.
3316 * Valid_mask is one or several SCST_SENSE_*_VALID constants setting valid
3317 * (key, asc, ascq) values.
3319 bool scst_analyze_sense(const uint8_t *sense, int len,
3320 unsigned int valid_mask, int key, int asc, int ascq);
3323 * Returnes a pseudo-random number for debugging purposes. Available only in
3326 unsigned long scst_random(void);
3329 * Sets response data length for cmd and truncates its SG vector accordingly.
3330 * The cmd->resp_data_len must not be set directly, it must be set only
3331 * using this function. Value of resp_data_len must be <= cmd->bufflen.
3333 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len);
3336 * Get/put global ref counter that prevents from entering into suspended
3337 * activities stage, so protects from any global management operations.
3339 void scst_get(void);
3340 void scst_put(void);
3345 void scst_cmd_get(struct scst_cmd *cmd);
3346 void scst_cmd_put(struct scst_cmd *cmd);
3349 * Allocates and returns pointer to SG vector with data size "size".
3350 * In *count returned the count of entries in the vector.
3351 * Returns NULL for failure.
3353 struct scatterlist *scst_alloc(int size, gfp_t gfp_mask, int *count);
3355 /* Frees SG vector returned by scst_alloc() */
3356 void scst_free(struct scatterlist *sg, int count);
3359 * Adds local to the current thread data to tgt_dev
3360 * (they will be local for the tgt_dev and current thread).
3362 void scst_add_thr_data(struct scst_tgt_dev *tgt_dev,
3363 struct scst_thr_data_hdr *data,
3364 void (*free_fn) (struct scst_thr_data_hdr *data));
3366 /* Deletes all local to threads data from tgt_dev */
3367 void scst_del_all_thr_data(struct scst_tgt_dev *tgt_dev);
3369 /* Deletes all local to threads data from all tgt_dev's of the dev */
3370 void scst_dev_del_all_thr_data(struct scst_device *dev);
3372 /* Finds local to the thread data. Returns NULL, if they not found. */
3373 struct scst_thr_data_hdr *__scst_find_thr_data(struct scst_tgt_dev *tgt_dev,
3374 struct task_struct *tsk);
3376 /* Finds local to the current thread data. Returns NULL, if they not found. */
3377 static inline struct scst_thr_data_hdr *scst_find_thr_data(
3378 struct scst_tgt_dev *tgt_dev)
3380 return __scst_find_thr_data(tgt_dev, current);
3383 static inline void scst_thr_data_get(struct scst_thr_data_hdr *data)
3385 atomic_inc(&data->ref);
3388 static inline void scst_thr_data_put(struct scst_thr_data_hdr *data)
3390 if (atomic_dec_and_test(&data->ref))
3391 data->free_fn(data);
3395 ** Generic parse() support routines.
3396 ** Done via pointer on functions to avoid unneeded dereferences on
3400 /* Calculates and returns block shift for the given sector size */
3401 int scst_calc_block_shift(int sector_size);
3403 /* Generic parse() for SBC (disk) devices */
3404 int scst_sbc_generic_parse(struct scst_cmd *cmd,
3405 int (*get_block_shift)(struct scst_cmd *cmd));
3407 /* Generic parse() for MMC (cdrom) devices */
3408 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
3409 int (*get_block_shift)(struct scst_cmd *cmd));
3411 /* Generic parse() for MO disk devices */
3412 int scst_modisk_generic_parse(struct scst_cmd *cmd,
3413 int (*get_block_shift)(struct scst_cmd *cmd));
3415 /* Generic parse() for tape devices */
3416 int scst_tape_generic_parse(struct scst_cmd *cmd,
3417 int (*get_block_size)(struct scst_cmd *cmd));
3419 /* Generic parse() for changer devices */
3420 int scst_changer_generic_parse(struct scst_cmd *cmd,
3421 int (*nothing)(struct scst_cmd *cmd));
3423 /* Generic parse() for "processor" devices */
3424 int scst_processor_generic_parse(struct scst_cmd *cmd,
3425 int (*nothing)(struct scst_cmd *cmd));
3427 /* Generic parse() for RAID devices */
3428 int scst_raid_generic_parse(struct scst_cmd *cmd,
3429 int (*nothing)(struct scst_cmd *cmd));
3432 ** Generic dev_done() support routines.
3433 ** Done via pointer on functions to avoid unneeded dereferences on
3437 /* Generic dev_done() for block devices */
3438 int scst_block_generic_dev_done(struct scst_cmd *cmd,
3439 void (*set_block_shift)(struct scst_cmd *cmd, int block_shift));
3441 /* Generic dev_done() for tape devices */
3442 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
3443 void (*set_block_size)(struct scst_cmd *cmd, int block_size));
3446 * Issues a MODE SENSE for control mode page data and sets the corresponding
3447 * dev's parameter from it. Returns 0 on success and not 0 otherwise.
3449 int scst_obtain_device_parameters(struct scst_device *dev);
3452 * Has to be put here open coded, because Linux doesn't have equivalent, which
3453 * allows exclusive wake ups of threads in LIFO order. We need it to let (yet)
3454 * unneeded threads sleep and not pollute CPU cache by their stacks.
3456 static inline void add_wait_queue_exclusive_head(wait_queue_head_t *q,
3459 unsigned long flags;
3461 wait->flags |= WQ_FLAG_EXCLUSIVE;
3462 spin_lock_irqsave(&q->lock, flags);
3463 __add_wait_queue(q, wait);
3464 spin_unlock_irqrestore(&q->lock, flags);
3467 #endif /* __SCST_H */