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
72 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
73 #define COMPLETION_INITIALIZER_ONSTACK(work) \
74 ({ init_completion(&work); work; })
77 * Lockdep needs to run a non-constant initializer for on-stack
78 * completions - so we use the _ONSTACK() variant for those that
79 * are on the kernel stack:
82 # define DECLARE_COMPLETION_ONSTACK(work) \
83 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
85 # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
89 * list_is_last - tests whether @list is the last entry in list @head
90 * @list: the entry to test
91 * @head: the head of the list
93 static inline int list_is_last(const struct list_head *list,
94 const struct list_head *head)
96 return list->next == head;
101 #define SCST_LOCAL_NAME "scst_lcl_drvr"
103 /*************************************************************
104 ** States of command processing state machine. At first,
105 ** "active" states, then - "passive" ones. This is to have
106 ** more efficient generated code of the corresponding
107 ** "switch" statements.
108 *************************************************************/
110 /* Internal parsing */
111 #define SCST_CMD_STATE_PRE_PARSE 0
113 /* Dev handler's parse() is going to be called */
114 #define SCST_CMD_STATE_DEV_PARSE 1
116 /* Allocation of the cmd's data buffer */
117 #define SCST_CMD_STATE_PREPARE_SPACE 2
119 /* Target driver's rdy_to_xfer() is going to be called */
120 #define SCST_CMD_STATE_RDY_TO_XFER 3
122 /* Target driver's pre_exec() is going to be called */
123 #define SCST_CMD_STATE_TGT_PRE_EXEC 4
125 /* Cmd is going to be sent for execution */
126 #define SCST_CMD_STATE_SEND_FOR_EXEC 5
128 /* Cmd is being checked if it should be executed locally */
129 #define SCST_CMD_STATE_LOCAL_EXEC 6
131 /* Cmd is ready for execution */
132 #define SCST_CMD_STATE_REAL_EXEC 7
134 /* Internal post-exec checks */
135 #define SCST_CMD_STATE_PRE_DEV_DONE 8
137 /* Internal MODE SELECT pages related checks */
138 #define SCST_CMD_STATE_MODE_SELECT_CHECKS 9
140 /* Dev handler's dev_done() is going to be called */
141 #define SCST_CMD_STATE_DEV_DONE 10
143 /* Target driver's xmit_response() is going to be called */
144 #define SCST_CMD_STATE_PRE_XMIT_RESP 11
146 /* Target driver's xmit_response() is going to be called */
147 #define SCST_CMD_STATE_XMIT_RESP 12
150 #define SCST_CMD_STATE_FINISHED 13
152 /* Internal cmd finished */
153 #define SCST_CMD_STATE_FINISHED_INTERNAL 14
155 #define SCST_CMD_STATE_LAST_ACTIVE (SCST_CMD_STATE_FINISHED_INTERNAL+100)
157 /* A cmd is created, but scst_cmd_init_done() not called */
158 #define SCST_CMD_STATE_INIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+1)
160 /* LUN translation (cmd->tgt_dev assignment) */
161 #define SCST_CMD_STATE_INIT (SCST_CMD_STATE_LAST_ACTIVE+2)
163 /* Allocation of the cmd's data buffer */
164 #define SCST_CMD_STATE_PREPROCESS_DONE (SCST_CMD_STATE_LAST_ACTIVE+3)
166 /* Waiting for data from the initiator (until scst_rx_data() called) */
167 #define SCST_CMD_STATE_DATA_WAIT (SCST_CMD_STATE_LAST_ACTIVE+4)
169 /* Waiting for CDB's execution finish */
170 #define SCST_CMD_STATE_REAL_EXECUTING (SCST_CMD_STATE_LAST_ACTIVE+5)
172 /* Waiting for response's transmission finish */
173 #define SCST_CMD_STATE_XMIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+6)
175 /*************************************************************
176 * Can be retuned instead of cmd's state by dev handlers'
177 * functions, if the command's state should be set by default
178 *************************************************************/
179 #define SCST_CMD_STATE_DEFAULT 500
181 /*************************************************************
182 * Can be retuned instead of cmd's state by dev handlers'
183 * functions, if it is impossible to complete requested
184 * task in atomic context. The cmd will be restarted in thread
186 *************************************************************/
187 #define SCST_CMD_STATE_NEED_THREAD_CTX 1000
189 /*************************************************************
190 * Can be retuned instead of cmd's state by dev handlers'
191 * parse function, if the cmd processing should be stopped
192 * for now. The cmd will be restarted by dev handlers itself.
193 *************************************************************/
194 #define SCST_CMD_STATE_STOP 1001
196 /*************************************************************
197 ** States of mgmt command processing state machine
198 *************************************************************/
200 /* LUN translation (mcmd->tgt_dev assignment) */
201 #define SCST_MCMD_STATE_INIT 0
203 /* Mgmt cmd is ready for processing */
204 #define SCST_MCMD_STATE_READY 1
206 /* Mgmt cmd is being executing */
207 #define SCST_MCMD_STATE_EXECUTING 2
209 /* Post check when affected commands done */
210 #define SCST_MCMD_STATE_POST_AFFECTED_CMDS_DONE 3
212 /* Target driver's task_mgmt_fn_done() is going to be called */
213 #define SCST_MCMD_STATE_DONE 4
215 /* The mcmd finished */
216 #define SCST_MCMD_STATE_FINISHED 5
218 /*************************************************************
219 ** Constants for "atomic" parameter of SCST's functions
220 *************************************************************/
221 #define SCST_NON_ATOMIC 0
222 #define SCST_ATOMIC 1
224 /*************************************************************
225 ** Values for pref_context parameter of scst_cmd_init_done(),
226 ** scst_rx_data(), scst_restart_cmd(), scst_tgt_cmd_done()
227 ** and scst_cmd_done()
228 *************************************************************/
230 enum scst_exec_context {
232 * Direct cmd's processing (i.e. regular function calls in the current
233 * context) sleeping is not allowed
235 SCST_CONTEXT_DIRECT_ATOMIC,
238 * Direct cmd's processing (i.e. regular function calls in the current
239 * context), sleeping is allowed, no restrictions
243 /* Tasklet or thread context required for cmd's processing */
244 SCST_CONTEXT_TASKLET,
246 /* Thread context required for cmd's processing */
250 * Context is the same as it was in previous call of the corresponding
251 * callback. For example, if dev handler's exec() does sync. data
252 * reading this value should be used for scst_cmd_done(). The same is
253 * true if scst_tgt_cmd_done() called directly from target driver's
254 * xmit_response(). Not allowed in scst_cmd_init_done() and
255 * scst_cmd_init_stage1_done().
260 /*************************************************************
261 ** Values for status parameter of scst_rx_data()
262 *************************************************************/
265 #define SCST_RX_STATUS_SUCCESS 0
268 * Data receiving finished with error, so set the sense and
269 * finish the command, including xmit_response() call
271 #define SCST_RX_STATUS_ERROR 1
274 * Data receiving finished with error and the sense is set,
275 * so finish the command, including xmit_response() call
277 #define SCST_RX_STATUS_ERROR_SENSE_SET 2
280 * Data receiving finished with fatal error, so finish the command,
281 * but don't call xmit_response()
283 #define SCST_RX_STATUS_ERROR_FATAL 3
285 /*************************************************************
286 ** Values for status parameter of scst_restart_cmd()
287 *************************************************************/
290 #define SCST_PREPROCESS_STATUS_SUCCESS 0
293 * Command's processing finished with error, so set the sense and
294 * finish the command, including xmit_response() call
296 #define SCST_PREPROCESS_STATUS_ERROR 1
299 * Command's processing finished with error and the sense is set,
300 * so finish the command, including xmit_response() call
302 #define SCST_PREPROCESS_STATUS_ERROR_SENSE_SET 2
305 * Command's processing finished with fatal error, so finish the command,
306 * but don't call xmit_response()
308 #define SCST_PREPROCESS_STATUS_ERROR_FATAL 3
310 /* Thread context requested */
311 #define SCST_PREPROCESS_STATUS_NEED_THREAD 4
313 /*************************************************************
314 ** Values for AEN functions
315 *************************************************************/
318 * SCSI Asynchronous Event. Parameter contains SCSI sense
319 * (Unit Attention). AENs generated only for 2 the following UAs:
320 * CAPACITY DATA HAS CHANGED and REPORTED LUNS DATA HAS CHANGED.
321 * Other UAs reported regularly as CHECK CONDITION status,
322 * because it doesn't look safe to report them using AENs, since
323 * reporting using AENs opens delivery race windows even in case of
326 #define SCST_AEN_SCSI 0
328 /*************************************************************
329 ** Allowed return/status codes for report_aen() callback and
330 ** scst_set_aen_delivery_status() function
331 *************************************************************/
334 #define SCST_AEN_RES_SUCCESS 0
337 #define SCST_AEN_RES_NOT_SUPPORTED -1
340 #define SCST_AEN_RES_FAILED -2
342 /*************************************************************
343 ** Allowed return codes for xmit_response(), rdy_to_xfer()
344 *************************************************************/
347 #define SCST_TGT_RES_SUCCESS 0
349 /* Internal device queue is full, retry again later */
350 #define SCST_TGT_RES_QUEUE_FULL -1
353 * It is impossible to complete requested task in atomic context.
354 * The cmd will be restarted in thread context.
356 #define SCST_TGT_RES_NEED_THREAD_CTX -2
359 * Fatal error, if returned by xmit_response() the cmd will
360 * be destroyed, if by any other function, xmit_response()
361 * will be called with HARDWARE ERROR sense data
363 #define SCST_TGT_RES_FATAL_ERROR -3
365 /*************************************************************
366 ** Allowed return codes for dev handler's exec()
367 *************************************************************/
369 /* The cmd is done, go to other ones */
370 #define SCST_EXEC_COMPLETED 0
372 /* The cmd should be sent to SCSI mid-level */
373 #define SCST_EXEC_NOT_COMPLETED 1
376 * Thread context is required to execute the command.
377 * Exec() will be called again in the thread context.
379 #define SCST_EXEC_NEED_THREAD 2
382 * Set if cmd is finished and there is status/sense to be sent.
383 * The status should be not sent (i.e. the flag not set) if the
384 * possibility to perform a command in "chunks" (i.e. with multiple
385 * xmit_response()/rdy_to_xfer()) is used (not implemented yet).
386 * Obsolete, use scst_cmd_get_is_send_status() instead.
388 #define SCST_TSC_FLAG_STATUS 0x2
390 /*************************************************************
391 ** Additional return code for dev handler's task_mgmt_fn()
392 *************************************************************/
394 /* Regular standard actions for the command should be done */
395 #define SCST_DEV_TM_NOT_COMPLETED 1
397 /*************************************************************
398 ** Session initialization phases
399 *************************************************************/
401 /* Set if session is being initialized */
402 #define SCST_SESS_IPH_INITING 0
404 /* Set if the session is successfully initialized */
405 #define SCST_SESS_IPH_SUCCESS 1
407 /* Set if the session initialization failed */
408 #define SCST_SESS_IPH_FAILED 2
410 /* Set if session is initialized and ready */
411 #define SCST_SESS_IPH_READY 3
413 /*************************************************************
414 ** Session shutdown phases
415 *************************************************************/
417 /* Set if session is initialized and ready */
418 #define SCST_SESS_SPH_READY 0
420 /* Set if session is shutting down */
421 #define SCST_SESS_SPH_SHUTDOWN 1
423 /*************************************************************
424 ** Session's async (atomic) flags
425 *************************************************************/
427 /* Set if the sess's hw pending work is scheduled */
428 #define SCST_SESS_HW_PENDING_WORK_SCHEDULED 0
430 /*************************************************************
431 ** Cmd's async (atomic) flags
432 *************************************************************/
434 /* Set if the cmd is aborted and ABORTED sense will be sent as the result */
435 #define SCST_CMD_ABORTED 0
437 /* Set if the cmd is aborted by other initiator */
438 #define SCST_CMD_ABORTED_OTHER 1
440 /* Set if the cmd is aborted and counted in cmd_done_wait_count */
441 #define SCST_CMD_DONE_COUNTED 2
443 /* Set if no response should be sent to the target about this cmd */
444 #define SCST_CMD_NO_RESP 3
446 /* Set if the cmd is dead and can be destroyed at any time */
447 #define SCST_CMD_CAN_BE_DESTROYED 4
449 /*************************************************************
450 ** Tgt_dev's async. flags (tgt_dev_flags)
451 *************************************************************/
453 /* Set if tgt_dev has Unit Attention sense */
454 #define SCST_TGT_DEV_UA_PENDING 0
456 /* Set if tgt_dev is RESERVED by another session */
457 #define SCST_TGT_DEV_RESERVED 1
459 /* Set if the corresponding context is atomic */
460 #define SCST_TGT_DEV_AFTER_INIT_WR_ATOMIC 5
461 #define SCST_TGT_DEV_AFTER_INIT_OTH_ATOMIC 6
462 #define SCST_TGT_DEV_AFTER_RESTART_WR_ATOMIC 7
463 #define SCST_TGT_DEV_AFTER_RESTART_OTH_ATOMIC 8
464 #define SCST_TGT_DEV_AFTER_RX_DATA_ATOMIC 9
465 #define SCST_TGT_DEV_AFTER_EXEC_ATOMIC 10
467 #define SCST_TGT_DEV_CLUST_POOL 11
469 #ifdef CONFIG_SCST_PROC
471 /*************************************************************
472 ** Name of the entry in /proc
473 *************************************************************/
474 #define SCST_PROC_ENTRY_NAME "scsi_tgt"
478 /*************************************************************
479 ** Activities suspending timeout
480 *************************************************************/
481 #define SCST_SUSPENDING_TIMEOUT (90 * HZ)
483 /*************************************************************
484 ** Kernel cache creation helper
485 *************************************************************/
487 #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
488 sizeof(struct __struct), __alignof__(struct __struct),\
489 (__flags), NULL, NULL)
492 /*************************************************************
493 ** Vlaid_mask constants for scst_analyze_sense()
494 *************************************************************/
496 #define SCST_SENSE_KEY_VALID 1
497 #define SCST_SENSE_ASC_VALID 2
498 #define SCST_SENSE_ASCQ_VALID 4
500 #define SCST_SENSE_ASCx_VALID (SCST_SENSE_ASC_VALID | \
501 SCST_SENSE_ASCQ_VALID)
503 #define SCST_SENSE_ALL_VALID (SCST_SENSE_KEY_VALID | \
504 SCST_SENSE_ASC_VALID | \
505 SCST_SENSE_ASCQ_VALID)
507 /*************************************************************
509 *************************************************************/
514 struct scst_mgmt_cmd;
517 struct scst_dev_type;
524 * SCST uses 64-bit numbers to represent LUN's internally. The value
525 * NO_SUCH_LUN is guaranteed to be different of every valid LUN.
527 #define NO_SUCH_LUN ((uint64_t)-1)
529 typedef enum dma_data_direction scst_data_direction;
531 enum scst_cdb_flags {
532 /* SCST_TRANSFER_LEN_TYPE_FIXED must be equiv 1 (FIXED_BIT in cdb) */
533 SCST_TRANSFER_LEN_TYPE_FIXED = 0x001,
534 SCST_SMALL_TIMEOUT = 0x002,
535 SCST_LONG_TIMEOUT = 0x004,
536 SCST_UNKNOWN_LENGTH = 0x008,
537 SCST_INFO_NOT_FOUND = 0x010, /* must be single bit */
538 SCST_VERIFY_BYTCHK_MISMATCH_ALLOWED = 0x020,
539 SCST_IMPLICIT_HQ = 0x040,
540 SCST_SKIP_UA = 0x080,
541 SCST_WRITE_MEDIUM = 0x100,
542 SCST_LOCAL_CMD = 0x200,
543 SCST_LOCAL_EXEC_NEEDED = 0x400,
547 * Scsi_Target_Template: defines what functions a target driver will
548 * have to provide in order to work with the target mid-level.
549 * MUST HAVEs define functions that are expected to be in order to work.
550 * OPTIONAL says that there is a choice.
552 * Also, pay attention to the fact that a command is BLOCKING or NON-BLOCKING.
553 * NON-BLOCKING means that a function returns immediately and will not wait
554 * for actual data transfer to finish. Blocking in such command could have
555 * negative impact on overall system performance. If blocking is necessary,
556 * it is worth to consider creating dedicated thread(s) in target driver, to
557 * which the commands would be passed and which would perform blocking
558 * operations instead of SCST.
560 * If the function allowed to sleep or not is determined by its last
561 * argument, which is true, if sleeping is not allowed. In this case,
562 * if the function requires sleeping, it can return
563 * SCST_TGT_RES_NEED_THREAD_CTX, and it will be recalled in the thread context,
564 * where sleeping is allowed.
566 struct scst_tgt_template {
570 * SG tablesize allows to check whether scatter/gather can be used
576 * True, if this target adapter uses unchecked DMA onto an ISA bus.
578 unsigned unchecked_isa_dma:1;
581 * True, if this target adapter can benefit from using SG-vector
582 * clustering (i.e. smaller number of segments).
584 unsigned use_clustering:1;
587 * True, if this target adapter doesn't support SG-vector clustering
589 unsigned no_clustering:1;
592 * True, if corresponding function supports execution in
593 * the atomic (non-sleeping) context
595 unsigned xmit_response_atomic:1;
596 unsigned rdy_to_xfer_atomic:1;
598 #ifdef CONFIG_SCST_PROC
599 /* True, if the template doesn't need the entry in /proc */
600 unsigned no_proc_entry:1;
604 * The maximum time in seconds cmd can stay inside the target
605 * hardware, i.e. after rdy_to_xfer() and xmit_response(), before
606 * on_hw_pending_cmd_timeout() will be called, if defined.
608 * In the current implementation a cmd will be aborted in time t
609 * max_hw_pending_time <= t < 2*max_hw_pending_time.
611 int max_hw_pending_time;
614 * This function is equivalent to the SCSI
615 * queuecommand. The target should transmit the response
616 * buffer and the status in the scst_cmd struct.
617 * The expectation is that this executing this command is NON-BLOCKING.
619 * After the response is actually transmitted, the target
620 * should call the scst_tgt_cmd_done() function of the
621 * mid-level, which will allow it to free up the command.
622 * Returns one of the SCST_TGT_RES_* constants.
624 * Pay attention to "atomic" attribute of the cmd, which can be get
625 * by scst_cmd_atomic(): it is true if the function called in the
626 * atomic (non-sleeping) context.
630 int (*xmit_response) (struct scst_cmd *cmd);
633 * This function informs the driver that data
634 * buffer corresponding to the said command have now been
635 * allocated and it is OK to receive data for this command.
636 * This function is necessary because a SCSI target does not
637 * have any control over the commands it receives. Most lower
638 * level protocols have a corresponding function which informs
639 * the initiator that buffers have been allocated e.g., XFER_
640 * RDY in Fibre Channel. After the data is actually received
641 * the low-level driver needs to call scst_rx_data() in order to
642 * continue processing this command.
643 * Returns one of the SCST_TGT_RES_* constants.
644 * This command is expected to be NON-BLOCKING.
646 * Pay attention to "atomic" attribute of the cmd, which can be get
647 * by scst_cmd_atomic(): it is true if the function called in the
648 * atomic (non-sleeping) context.
652 int (*rdy_to_xfer) (struct scst_cmd *cmd);
655 * Called if cmd stays inside the target hardware, i.e. after
656 * rdy_to_xfer() and xmit_response(), more than max_hw_pending_time
657 * time. The target driver supposed to cleanup this command and
658 * resume cmd's processing.
662 void (*on_hw_pending_cmd_timeout) (struct scst_cmd *cmd);
665 * Called to notify the driver that the command is about to be freed.
666 * Necessary, because for aborted commands xmit_response() could not
667 * be called. Could be called on IRQ context.
671 void (*on_free_cmd) (struct scst_cmd *cmd);
674 * This function allows target driver to handle data buffer
675 * allocations on its own.
677 * Target driver doesn't have to always allocate buffer in this
678 * function, but if it decide to do it, it must check that
679 * scst_cmd_get_data_buff_alloced() returns 0, otherwise to avoid
680 * double buffer allocation and memory leaks alloc_data_buf() shall
683 * Shall return 0 in case of success or < 0 (preferrably -ENOMEM)
684 * in case of error, or > 0 if the regular SCST allocation should be
685 * done. In case of returning successfully,
686 * scst_cmd->tgt_data_buf_alloced will be set by SCST.
688 * It is possible that both target driver and dev handler request own
689 * memory allocation. In this case, data will be memcpy() between
690 * buffers, where necessary.
692 * If allocation in atomic context - cf. scst_cmd_atomic() - is not
693 * desired or fails and consequently < 0 is returned, this function
694 * will be re-called in thread context.
696 * Please note that the driver will have to handle itself all relevant
697 * details such as scatterlist setup, highmem, freeing the allocated
702 int (*alloc_data_buf) (struct scst_cmd *cmd);
705 * This function informs the driver that data
706 * buffer corresponding to the said command have now been
707 * allocated and other preprocessing tasks have been done.
708 * A target driver could need to do some actions at this stage.
709 * After the target driver done the needed actions, it shall call
710 * scst_restart_cmd() in order to continue processing this command.
712 * Called only if the cmd is queued using scst_cmd_init_stage1_done()
713 * instead of scst_cmd_init_done().
715 * Returns void, the result is expected to be returned using
716 * scst_restart_cmd().
718 * This command is expected to be NON-BLOCKING.
720 * Pay attention to "atomic" attribute of the cmd, which can be get
721 * by scst_cmd_atomic(): it is true if the function called in the
722 * atomic (non-sleeping) context.
726 void (*preprocessing_done) (struct scst_cmd *cmd);
729 * This function informs the driver that the said command is about
732 * Returns one of the SCST_PREPROCESS_* constants.
734 * This command is expected to be NON-BLOCKING.
736 * Pay attention to "atomic" attribute of the cmd, which can be get
737 * by scst_cmd_atomic(): it is true if the function called in the
738 * atomic (non-sleeping) context.
742 int (*pre_exec) (struct scst_cmd *cmd);
745 * This function informs the driver that all affected by the
746 * corresponding task management function commands have beed completed.
747 * No return value expected.
749 * This function is expected to be NON-BLOCKING.
751 * Called without any locks held from a thread context.
755 void (*task_mgmt_affected_cmds_done) (struct scst_mgmt_cmd *mgmt_cmd);
758 * This function informs the driver that the corresponding task
759 * management function has been completed, i.e. all the corresponding
760 * commands completed and freed. No return value expected.
762 * This function is expected to be NON-BLOCKING.
764 * Called without any locks held from a thread context.
766 * MUST HAVE if the target supports task management.
768 void (*task_mgmt_fn_done) (struct scst_mgmt_cmd *mgmt_cmd);
771 * This function should detect the target adapters that
772 * are present in the system. The function should return a value
773 * >= 0 to signify the number of detected target adapters.
774 * A negative value should be returned whenever there is
779 int (*detect) (struct scst_tgt_template *tgt_template);
782 * This function should free up the resources allocated to the device.
783 * The function should return 0 to indicate successful release
784 * or a negative value if there are some issues with the release.
785 * In the current version the return value is ignored.
789 int (*release) (struct scst_tgt *tgt);
792 * This function is used for Asynchronous Event Notifications.
794 * Returns one of the SCST_AEN_RES_* constants.
795 * After AEN is sent, target driver must call scst_aen_done() and,
796 * optionally, scst_set_aen_delivery_status().
798 * This command is expected to be NON-BLOCKING, but can sleep.
800 * MUST HAVE, if low-level protocol supports AENs.
802 int (*report_aen) (struct scst_aen *aen);
804 #ifdef CONFIG_SCST_PROC
806 * Those functions can be used to export the driver's statistics and
807 * other infos to the world outside the kernel as well as to get some
808 * management commands from it.
812 int (*read_proc) (struct seq_file *seq, struct scst_tgt *tgt);
813 int (*write_proc) (char *buffer, char **start, off_t offset,
814 int length, int *eof, struct scst_tgt *tgt);
818 * This function allows to enable or disable particular target.
819 * A disabled target doesn't receive and process any SCSI commands.
821 * SHOULD HAVE to avoid race when there are connected initiators,
822 * while target not yet completed the initial configuration. In this
823 * case the too early connected initiators would see not those devices,
824 * which they intended to see.
826 ssize_t (*enable_tgt) (struct scst_tgt *tgt, const char *buffer,
830 * This function shows if particular target is enabled or not.
832 * SHOULD HAVE, see above why.
834 bool (*is_tgt_enabled) (struct scst_tgt *tgt);
837 * Name of the template. Must be unique to identify
838 * the template. MUST HAVE
840 const char name[SCST_MAX_NAME];
843 * Number of additional threads to the pool of dedicated threads.
844 * Used if xmit_response() or rdy_to_xfer() is blocking.
845 * It is the target driver's duty to ensure that not more, than that
846 * number of threads, are blocked in those functions at any time.
850 /* Optional default log flags */
851 const unsigned long default_trace_flags;
853 /* Optional pointer to trace flags */
854 unsigned long *trace_flags;
856 /* Optional local trace table */
857 struct scst_trace_log *trace_tbl;
859 /* Optional local trace table help string */
860 const char *trace_tbl_help;
862 #ifndef CONFIG_SCST_PROC
863 /* Optional sysfs attributes */
864 const struct attribute **tgtt_attrs;
866 /* Optional sysfs target attributes */
867 const struct attribute **tgt_attrs;
870 /** Private, must be inited to 0 by memset() **/
872 /* List of targets per template, protected by scst_mutex */
873 struct list_head tgt_list;
875 /* List entry of global templates list */
876 struct list_head scst_template_list_entry;
878 #ifdef CONFIG_SCST_PROC
879 /* The pointer to the /proc directory entry */
880 struct proc_dir_entry *proc_tgt_root;
883 /* Set if tgtt_kobj was initialized */
884 unsigned int tgtt_kobj_initialized:1;
886 struct kobject tgtt_kobj; /* kobject for this struct */
888 struct completion tgtt_kobj_release_cmpl;
890 #ifdef CONFIG_SCST_PROC
891 /* Device number in /proc */
896 struct scst_dev_type {
897 /* SCSI type of the supported device. MUST HAVE */
901 * True, if corresponding function supports execution in
902 * the atomic (non-sleeping) context
904 unsigned parse_atomic:1;
905 unsigned exec_atomic:1;
906 unsigned dev_done_atomic:1;
908 #ifdef CONFIG_SCST_PROC
909 /* Set, if no /proc files should be automatically created by SCST */
914 * Should be set, if exec() is synchronous. This is a hint to SCST core
915 * to optimize commands order management.
917 unsigned exec_sync:1;
920 * Called to parse CDB from the cmd and initialize
921 * cmd->bufflen and cmd->data_direction (both - REQUIRED).
922 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
923 * if the next default state should be used, or
924 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
925 * context, but requires sleeping, or SCST_CMD_STATE_STOP if the
926 * command should not be further processed for now. In the
927 * SCST_CMD_STATE_NEED_THREAD_CTX case the function
928 * will be recalled in the thread context, where sleeping is allowed.
930 * Pay attention to "atomic" attribute of the cmd, which can be get
931 * by scst_cmd_atomic(): it is true if the function called in the
932 * atomic (non-sleeping) context.
936 int (*parse) (struct scst_cmd *cmd);
939 * Called to execute CDB. Useful, for instance, to implement
940 * data caching. The result of CDB execution is reported via
941 * cmd->scst_cmd_done() callback.
943 * - SCST_EXEC_COMPLETED - the cmd is done, go to other ones
944 * - SCST_EXEC_NEED_THREAD - thread context is required to execute
945 * the command. Exec() will be called again in the thread context.
946 * - SCST_EXEC_NOT_COMPLETED - the cmd should be sent to SCSI
949 * Pay attention to "atomic" attribute of the cmd, which can be get
950 * by scst_cmd_atomic(): it is true if the function called in the
951 * atomic (non-sleeping) context.
953 * If this function provides sync execution, you should set
954 * exec_sync flag and consider to setup dedicated threads by
955 * setting threads_num > 0.
957 * !! If this function is implemented, scst_check_local_events() !!
958 * !! shall be called inside it just before the actual command's !!
961 * OPTIONAL, if not set, the commands will be sent directly to SCSI
964 int (*exec) (struct scst_cmd *cmd);
967 * Called to notify dev handler about the result of cmd execution
968 * and perform some post processing. Cmd's fields is_send_status and
969 * resp_data_len should be set by this function, but SCST offers good
971 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
972 * if the next default state should be used, or
973 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
974 * context, but requires sleeping. In the last case, the function
975 * will be recalled in the thread context, where sleeping is allowed.
977 * Pay attention to "atomic" attribute of the cmd, which can be get
978 * by scst_cmd_atomic(): it is true if the function called in the
979 * atomic (non-sleeping) context.
981 int (*dev_done) (struct scst_cmd *cmd);
984 * Called to notify dev hander that the command is about to be freed.
985 * Could be called on IRQ context.
987 void (*on_free_cmd) (struct scst_cmd *cmd);
990 * Called to execute a task management command.
992 * - SCST_MGMT_STATUS_SUCCESS - the command is done with success,
993 * no firther actions required
994 * - The SCST_MGMT_STATUS_* error code if the command is failed and
995 * no further actions required
996 * - SCST_DEV_TM_NOT_COMPLETED - regular standard actions for the
997 * command should be done
999 * Called without any locks held from a thread context.
1001 int (*task_mgmt_fn) (struct scst_mgmt_cmd *mgmt_cmd,
1002 struct scst_tgt_dev *tgt_dev);
1005 * Called when new device is attaching to the dev handler
1006 * Returns 0 on success, error code otherwise.
1008 int (*attach) (struct scst_device *dev);
1010 /* Called when new device is detaching from the dev handler */
1011 void (*detach) (struct scst_device *dev);
1014 * Called when new tgt_dev (session) is attaching to the dev handler.
1015 * Returns 0 on success, error code otherwise.
1017 int (*attach_tgt) (struct scst_tgt_dev *tgt_dev);
1019 /* Called when tgt_dev (session) is detaching from the dev handler */
1020 void (*detach_tgt) (struct scst_tgt_dev *tgt_dev);
1022 #ifdef CONFIG_SCST_PROC
1024 * Those functions can be used to export the handler's statistics and
1025 * other infos to the world outside the kernel as well as to get some
1026 * management commands from it.
1030 int (*read_proc) (struct seq_file *seq, struct scst_dev_type *dev_type);
1031 int (*write_proc) (char *buffer, char **start, off_t offset,
1032 int length, int *eof, struct scst_dev_type *dev_type);
1036 * Name of the dev handler. Must be unique. MUST HAVE.
1038 * It's SCST_MAX_NAME + few more bytes to match scst_user expectations.
1040 char name[SCST_MAX_NAME + 10];
1043 * Number of dedicated threads. If 0 - no dedicated threads will
1044 * be created, if <0 - creation of dedicated threads is prohibited.
1048 /* Optional default log flags */
1049 const unsigned long default_trace_flags;
1051 /* Optional pointer to trace flags */
1052 unsigned long *trace_flags;
1054 /* Optional local trace table */
1055 struct scst_trace_log *trace_tbl;
1057 /* Optional local trace table help string */
1058 const char *trace_tbl_help;
1060 #ifndef CONFIG_SCST_PROC
1061 /* Optional sysfs attributes */
1062 const struct attribute **devt_attrs;
1064 /* Optional sysfs device attributes */
1065 const struct attribute **dev_attrs;
1068 /* Pointer to dev handler's private data */
1071 /* Pointer to parent dev type in the sysfs hierarchy */
1072 struct scst_dev_type *parent;
1074 struct module *module;
1076 /** Private, must be inited to 0 by memset() **/
1078 /* list entry in scst_dev_type_list */
1079 struct list_head dev_type_list_entry;
1081 #ifdef CONFIG_SCST_PROC
1082 /* The pointer to the /proc directory entry */
1083 struct proc_dir_entry *proc_dev_type_root;
1086 unsigned int devt_kobj_initialized:1;
1088 struct kobject devt_kobj; /* main handlers/driver */
1090 /* To wait until devt_kobj released */
1091 struct completion devt_kobj_release_compl;
1095 /* List of remote sessions per target, protected by scst_mutex */
1096 struct list_head sess_list;
1098 /* List entry of targets per template (tgts_list) */
1099 struct list_head tgt_list_entry;
1101 struct scst_tgt_template *tgtt; /* corresponding target template */
1103 struct scst_acg *default_acg; /* The default acg for this target. */
1106 * Maximum SG table size. Needed here, since different cards on the
1107 * same target template can have different SG table limitations.
1111 /* Used for storage of target driver private stuff */
1115 * The following fields used to store and retry cmds if target's
1116 * internal queue is full, so the target is unable to accept
1117 * the cmd returning QUEUE FULL.
1118 * They protected by tgt_lock, where necessary.
1120 bool retry_timer_active;
1121 struct timer_list retry_timer;
1122 atomic_t finished_cmds;
1124 spinlock_t tgt_lock;
1125 struct list_head retry_cmd_list;
1127 /* Used to wait until session finished to unregister */
1128 wait_queue_head_t unreg_waitQ;
1130 #ifdef CONFIG_SCST_PROC
1131 /* Device number in /proc */
1135 /* Name of the target */
1138 #ifdef CONFIG_SCST_PROC
1139 /* Name on the default security group ("Default_target_name") */
1140 char *default_group_name;
1143 /* Set if tgt_kobj was initialized */
1144 unsigned int tgt_kobj_initialized:1;
1146 struct kobject tgt_kobj; /* main targets/target kobject */
1147 struct kobject *tgt_sess_kobj; /* target/sessions/ */
1148 struct kobject *tgt_luns_kobj; /* target/luns/ */
1149 struct kobject *tgt_ini_grp_kobj; /* target/ini_groups/ */
1152 /* Hash size and hash fn for hash based lun translation */
1153 #define TGT_DEV_HASH_SHIFT 5
1154 #define TGT_DEV_HASH_SIZE (1 << TGT_DEV_HASH_SHIFT)
1155 #define HASH_VAL(_val) (_val & (TGT_DEV_HASH_SIZE - 1))
1157 #ifdef CONFIG_SCST_MEASURE_LATENCY
1159 /* Defines extended latency statistics */
1160 struct scst_ext_latency_stat {
1161 uint64_t scst_time_rd, tgt_time_rd, dev_time_rd;
1162 unsigned int processed_cmds_rd;
1163 uint64_t min_scst_time_rd, min_tgt_time_rd, min_dev_time_rd;
1164 uint64_t max_scst_time_rd, max_tgt_time_rd, max_dev_time_rd;
1166 uint64_t scst_time_wr, tgt_time_wr, dev_time_wr;
1167 unsigned int processed_cmds_wr;
1168 uint64_t min_scst_time_wr, min_tgt_time_wr, min_dev_time_wr;
1169 uint64_t max_scst_time_wr, max_tgt_time_wr, max_dev_time_wr;
1172 #define SCST_IO_SIZE_THRESHOLD_SMALL (8*1024)
1173 #define SCST_IO_SIZE_THRESHOLD_MEDIUM (32*1024)
1174 #define SCST_IO_SIZE_THRESHOLD_LARGE (128*1024)
1175 #define SCST_IO_SIZE_THRESHOLD_VERY_LARGE (512*1024)
1177 #define SCST_LATENCY_STAT_INDEX_SMALL 0
1178 #define SCST_LATENCY_STAT_INDEX_MEDIUM 1
1179 #define SCST_LATENCY_STAT_INDEX_LARGE 2
1180 #define SCST_LATENCY_STAT_INDEX_VERY_LARGE 3
1181 #define SCST_LATENCY_STAT_INDEX_OTHER 4
1182 #define SCST_LATENCY_STATS_NUM (SCST_LATENCY_STAT_INDEX_OTHER + 1)
1184 #endif /* CONFIG_SCST_MEASURE_LATENCY */
1186 struct scst_session {
1188 * Initialization phase, one of SCST_SESS_IPH_* constants, protected by
1193 struct scst_tgt *tgt; /* corresponding target */
1195 /* Used for storage of target driver private stuff */
1198 unsigned long sess_aflags; /* session's async flags */
1201 * Hash list of tgt_dev's for this session, protected by scst_mutex
1202 * and suspended activity
1204 struct list_head sess_tgt_dev_list_hash[TGT_DEV_HASH_SIZE];
1207 * List of cmds in this session. Used to find a cmd in the
1208 * session. Protected by sess_list_lock.
1210 struct list_head search_cmd_list;
1212 spinlock_t sess_list_lock; /* protects search_cmd_list, etc */
1215 * List of cmds in this in the state after PRE_XMIT_RESP. All the cmds
1216 * moved here from search_cmd_list. Needed for hw_pending_work.
1217 * Protected by sess_list_lock.
1219 struct list_head after_pre_xmit_cmd_list;
1221 atomic_t refcnt; /* get/put counter */
1224 * Alive commands for this session. ToDo: make it part of the common
1227 atomic_t sess_cmd_count;
1229 /* Access control for this session and list entry there */
1230 struct scst_acg *acg;
1232 /* List entry for the sessions list inside ACG */
1233 struct list_head acg_sess_list_entry;
1235 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
1236 struct delayed_work hw_pending_work;
1238 struct work_struct hw_pending_work;
1241 /* Name of attached initiator */
1242 const char *initiator_name;
1244 /* List entry of sessions per target */
1245 struct list_head sess_list_entry;
1247 /* List entry for the list that keeps session, waiting for the init */
1248 struct list_head sess_init_list_entry;
1251 * List entry for the list that keeps session, waiting for the shutdown
1253 struct list_head sess_shut_list_entry;
1256 * Lists of deferred during session initialization commands.
1257 * Protected by sess_list_lock.
1259 struct list_head init_deferred_cmd_list;
1260 struct list_head init_deferred_mcmd_list;
1263 * Shutdown phase, one of SCST_SESS_SPH_* constants, unprotected.
1264 * Async. relating to init_phase, must be a separate variable, because
1265 * session could be unregistered before async. registration is finished.
1267 unsigned long shut_phase;
1269 /* Used if scst_unregister_session() called in wait mode */
1270 struct completion *shutdown_compl;
1272 /* Set if sess_kobj was initialized */
1273 unsigned int sess_kobj_initialized:1;
1275 struct kobject sess_kobj; /* kobject for this struct */
1278 * Functions and data for user callbacks from scst_register_session()
1279 * and scst_unregister_session()
1281 void *reg_sess_data;
1282 void (*init_result_fn) (struct scst_session *sess, void *data,
1284 void (*unreg_done_fn) (struct scst_session *sess);
1286 #ifdef CONFIG_SCST_MEASURE_LATENCY
1288 * Must be the last to allow to work with drivers who don't know
1289 * about this config time option.
1291 spinlock_t lat_lock;
1292 uint64_t scst_time, tgt_time, dev_time;
1293 unsigned int processed_cmds;
1294 uint64_t min_scst_time, min_tgt_time, min_dev_time;
1295 uint64_t max_scst_time, max_tgt_time, max_dev_time;
1296 struct scst_ext_latency_stat sess_latency_stat[SCST_LATENCY_STATS_NUM];
1300 struct scst_cmd_lists {
1301 spinlock_t cmd_list_lock;
1302 struct list_head active_cmd_list;
1303 wait_queue_head_t cmd_list_waitQ;
1304 struct list_head lists_list_entry;
1308 /* List entry for below *_cmd_lists */
1309 struct list_head cmd_list_entry;
1311 /* Pointer to lists of commands with the lock */
1312 struct scst_cmd_lists *cmd_lists;
1316 struct scst_session *sess; /* corresponding session */
1318 /* Cmd state, one of SCST_CMD_STATE_* constants */
1321 /*************************************************************
1323 *************************************************************/
1326 * Set if expected_sn should be incremented, i.e. cmd was sent
1329 unsigned int sent_for_exec:1;
1331 /* Set if the cmd's action is completed */
1332 unsigned int completed:1;
1334 /* Set if we should ignore Unit Attention in scst_check_sense() */
1335 unsigned int ua_ignore:1;
1337 /* Set if cmd is being processed in atomic context */
1338 unsigned int atomic:1;
1340 /* Set if this command was sent in double UA possible state */
1341 unsigned int double_ua_possible:1;
1343 /* Set if this command contains status */
1344 unsigned int is_send_status:1;
1346 /* Set if cmd is being retried */
1347 unsigned int retry:1;
1349 /* Set if cmd is internally generated */
1350 unsigned int internal:1;
1352 /* Set if the device was blocked by scst_inc_on_dev_cmd() (for debug) */
1353 unsigned int inc_blocking:1;
1355 /* Set if the device should be unblocked after cmd's finish */
1356 unsigned int needs_unblocking:1;
1358 /* Set if scst_dec_on_dev_cmd() call is needed on the cmd's finish */
1359 unsigned int dec_on_dev_needed:1;
1361 /* Set if cmd is queued as hw pending */
1362 unsigned int cmd_hw_pending:1;
1365 * Set if the target driver wants to alloc data buffers on its own.
1366 * In this case alloc_data_buf() must be provided in the target driver
1369 unsigned int tgt_need_alloc_data_buf:1;
1372 * Set by SCST if the custom data buffer allocation by the target driver
1375 unsigned int tgt_data_buf_alloced:1;
1377 /* Set if custom data buffer allocated by dev handler */
1378 unsigned int dh_data_buf_alloced:1;
1380 /* Set if the target driver called scst_set_expected() */
1381 unsigned int expected_values_set:1;
1384 * Set if the SG buffer was modified by scst_set_resp_data_len()
1386 unsigned int sg_buff_modified:1;
1389 * Set if scst_cmd_init_stage1_done() called and the target
1390 * want that preprocessing_done() will be called
1392 unsigned int preprocessing_only:1;
1394 /* Set if cmd's SN was set */
1395 unsigned int sn_set:1;
1397 /* Set if hq_cmd_count was incremented */
1398 unsigned int hq_cmd_inced:1;
1401 * Set if scst_cmd_init_stage1_done() called and the target wants
1402 * that the SN for the cmd won't be assigned until scst_restart_cmd()
1404 unsigned int set_sn_on_restart_cmd:1;
1406 /* Set if the cmd's must not use sgv cache for data buffer */
1407 unsigned int no_sgv:1;
1410 * Set if target driver may need to call dma_sync_sg() or similar
1411 * function before transferring cmd' data to the target device
1414 unsigned int may_need_dma_sync:1;
1416 /* Set if the cmd was done or aborted out of its SN */
1417 unsigned int out_of_sn:1;
1419 /* Set if increment expected_sn in cmd->scst_cmd_done() */
1420 unsigned int inc_expected_sn_on_done:1;
1422 /* Set if tgt_sn field is valid */
1423 unsigned int tgt_sn_set:1;
1425 /* Set if cmd is done */
1426 unsigned int done:1;
1428 /* Set if cmd is finished */
1429 unsigned int finished:1;
1432 * Set if the cmd was delayed by task management debugging code.
1433 * Used only if CONFIG_SCST_DEBUG_TM is on.
1435 unsigned int tm_dbg_delayed:1;
1438 * Set if the cmd must be ignored by task management debugging code.
1439 * Used only if CONFIG_SCST_DEBUG_TM is on.
1441 unsigned int tm_dbg_immut:1;
1443 /**************************************************************/
1445 unsigned long cmd_flags; /* cmd's async flags */
1447 /* Keeps status of cmd's status/data delivery to remote initiator */
1448 int delivery_status;
1450 struct scst_tgt_template *tgtt; /* to save extra dereferences */
1451 struct scst_tgt *tgt; /* to save extra dereferences */
1452 struct scst_device *dev; /* to save extra dereferences */
1454 struct scst_tgt_dev *tgt_dev; /* corresponding device for this cmd */
1456 uint64_t lun; /* LUN for this cmd */
1458 unsigned long start_time;
1460 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1461 struct scsi_request *scsi_req; /* SCSI request */
1464 /* List entry for tgt_dev's SN related lists */
1465 struct list_head sn_cmd_list_entry;
1467 /* Cmd's serial number, used to execute cmd's in order of arrival */
1470 /* The corresponding sn_slot in tgt_dev->sn_slots */
1473 /* List entry for sess's search_cmd_list and after_pre_xmit_cmd_list */
1474 struct list_head sess_cmd_list_entry;
1477 * Used to found the cmd by scst_find_cmd_by_tag(). Set by the
1478 * target driver on the cmd's initialization time
1482 uint32_t tgt_sn; /* SN set by target driver (for TM purposes) */
1484 /* CDB and its len */
1485 uint8_t cdb[SCST_MAX_CDB_SIZE];
1486 short cdb_len; /* it might be -1 */
1487 unsigned short ext_cdb_len;
1490 enum scst_cdb_flags op_flags;
1491 const char *op_name;
1493 enum scst_cmd_queue_type queue_type;
1495 int timeout; /* CDB execution timeout in seconds */
1496 int retries; /* Amount of retries that will be done by SCSI mid-level */
1498 /* SCSI data direction, one of SCST_DATA_* constants */
1499 scst_data_direction data_direction;
1501 /* Remote initiator supplied values, if any */
1502 scst_data_direction expected_data_direction;
1503 int expected_transfer_len;
1504 int expected_in_transfer_len; /* for bidi writes */
1507 * Cmd data length. Could be different from bufflen for commands like
1508 * VERIFY, which transfer different amount of data (if any), than
1513 /* Completition routine */
1514 void (*scst_cmd_done) (struct scst_cmd *cmd, int next_state,
1515 enum scst_exec_context pref_context);
1517 struct sgv_pool_obj *sgv; /* sgv object */
1518 int bufflen; /* cmd buffer length */
1519 struct scatterlist *sg; /* cmd data buffer SG vector */
1520 int sg_cnt; /* SG segments count */
1523 * Response data length in data buffer. This field must not be set
1524 * directly, use scst_set_resp_data_len() for that
1528 /* scst_get_sg_buf_[first,next]() support */
1529 int get_sg_buf_entry_num;
1531 /* Bidirectional transfers support */
1532 int in_bufflen; /* WRITE buffer length */
1533 struct sgv_pool_obj *in_sgv; /* WRITE sgv object */
1534 struct scatterlist *in_sg; /* WRITE data buffer SG vector */
1535 int in_sg_cnt; /* WRITE SG segments count */
1538 * Used if both target driver and dev handler request own memory
1539 * allocation. In other cases, both are equal to sg and sg_cnt
1542 * If target driver requests own memory allocations, it MUST use
1543 * functions scst_cmd_get_tgt_sg*() to get sg and sg_cnt! Otherwise,
1544 * it may use functions scst_cmd_get_sg*().
1546 struct scatterlist *tgt_sg;
1548 struct scatterlist *tgt_in_sg; /* bidirectional */
1549 int tgt_in_sg_cnt; /* bidirectional */
1552 * The status fields in case of errors must be set using
1553 * scst_set_cmd_error_status()!
1555 uint8_t status; /* status byte from target device */
1556 uint8_t msg_status; /* return status from host adapter itself */
1557 uint8_t host_status; /* set by low-level driver to indicate status */
1558 uint8_t driver_status; /* set by mid-level */
1560 uint8_t *sense; /* pointer to sense buffer */
1561 unsigned short sense_valid_len; /* length of valid sense data */
1562 unsigned short sense_buflen; /* length of the sense buffer, if any */
1564 /* Start time when cmd was sent to rdy_to_xfer() or xmit_response() */
1565 unsigned long hw_pending_start;
1567 /* Used for storage of target driver private stuff */
1570 /* Used for storage of dev handler private stuff */
1574 * Used to restore the SG vector if it was modified by
1575 * scst_set_resp_data_len()
1577 int orig_sg_cnt, orig_sg_entry, orig_entry_len;
1579 /* Used to retry commands in case of double UA */
1580 int dbl_ua_orig_resp_data_len, dbl_ua_orig_data_direction;
1582 /* List corresponding mgmt cmd, if any, protected by sess_list_lock */
1583 struct list_head mgmt_cmd_list;
1585 /* List entry for dev's blocked_cmd_list */
1586 struct list_head blocked_cmd_list_entry;
1588 struct scst_cmd *orig_cmd; /* Used to issue REQUEST SENSE */
1590 #ifdef CONFIG_SCST_MEASURE_LATENCY
1592 * Must be the last to allow to work with drivers who don't know
1593 * about this config time option.
1595 uint64_t start, curr_start, parse_time, alloc_buf_time;
1596 uint64_t restart_waiting_time, rdy_to_xfer_time;
1597 uint64_t pre_exec_time, exec_time, dev_done_time;
1598 uint64_t xmit_time, tgt_on_free_time, dev_on_free_time;
1602 struct scst_rx_mgmt_params {
1610 unsigned char tag_set;
1611 unsigned char lun_set;
1612 unsigned char cmd_sn_set;
1615 struct scst_mgmt_cmd_stub {
1616 struct scst_mgmt_cmd *mcmd;
1618 /* List entry in cmd->mgmt_cmd_list */
1619 struct list_head cmd_mgmt_cmd_list_entry;
1622 struct scst_mgmt_cmd {
1623 /* List entry for *_mgmt_cmd_list */
1624 struct list_head mgmt_cmd_list_entry;
1626 struct scst_session *sess;
1628 /* Mgmt cmd state, one of SCST_MCMD_STATE_* constants */
1633 unsigned int completed:1; /* set, if the mcmd is completed */
1634 /* Set if device(s) should be unblocked after mcmd's finish */
1635 unsigned int needs_unblocking:1;
1636 unsigned int lun_set:1; /* set, if lun field is valid */
1637 unsigned int cmd_sn_set:1; /* set, if cmd_sn field is valid */
1638 /* set, if scst_mgmt_affected_cmds_done was called */
1639 unsigned int affected_cmds_done_called:1;
1642 * Number of commands to finish before sending response,
1643 * protected by scst_mcmd_lock
1645 int cmd_finish_wait_count;
1648 * Number of commands to complete (done) before resetting reservation,
1649 * protected by scst_mcmd_lock
1651 int cmd_done_wait_count;
1653 /* Number of completed commands, protected by scst_mcmd_lock */
1654 int completed_cmd_count;
1656 uint64_t lun; /* LUN for this mgmt cmd */
1657 /* or (and for iSCSI) */
1658 uint64_t tag; /* tag of the corresponding cmd */
1660 uint32_t cmd_sn; /* affected command's highest SN */
1662 /* corresponding cmd (to be aborted, found by tag) */
1663 struct scst_cmd *cmd_to_abort;
1665 /* corresponding device for this mgmt cmd (found by lun) */
1666 struct scst_tgt_dev *mcmd_tgt_dev;
1668 /* completition status, one of the SCST_MGMT_STATUS_* constants */
1671 /* Used for storage of target driver private stuff */
1675 struct scst_device {
1676 struct scst_dev_type *handler; /* corresponding dev handler */
1678 struct scst_mem_lim dev_mem_lim;
1680 unsigned short type; /* SCSI type of the device */
1682 /*************************************************************
1683 ** Dev's flags. Updates serialized by dev_lock or suspended
1685 *************************************************************/
1687 /* Set if dev is RESERVED */
1688 unsigned short dev_reserved:1;
1690 /* Set if double reset UA is possible */
1691 unsigned short dev_double_ua_possible:1;
1693 /* If set, dev is read only */
1694 unsigned short rd_only:1;
1696 /**************************************************************/
1698 /*************************************************************
1699 ** Dev's control mode page related values. Updates serialized
1700 ** by scst_block_dev(). It's long to not interfere with the
1702 *************************************************************/
1704 unsigned long queue_alg:4;
1705 unsigned long tst:3;
1706 unsigned long tas:1;
1707 unsigned long swp:1;
1708 unsigned long d_sense:1;
1711 * Set if device implements own ordered commands management. If not set
1712 * and queue_alg is SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER,
1713 * expected_sn will be incremented only after commands finished.
1715 unsigned long has_own_order_mgmt:1;
1717 /**************************************************************/
1719 /* Used for storage of dev handler private stuff */
1722 /* Used to translate SCSI's cmd to SCST's cmd */
1723 struct gendisk *rq_disk;
1725 /* Corresponding real SCSI device, could be NULL for virtual devices */
1726 struct scsi_device *scsi_dev;
1728 /* Pointer to lists of commands with the lock */
1729 struct scst_cmd_lists *p_cmd_lists;
1731 /* Lists of commands with lock, if dedicated threads are used */
1732 struct scst_cmd_lists cmd_lists;
1734 /* Per-device dedicated IO context */
1735 struct io_context *dev_io_ctx;
1737 /* How many cmds alive on this dev */
1738 atomic_t dev_cmd_count;
1740 /* How many write cmds alive on this dev. Temporary, ToDo */
1741 atomic_t write_cmd_count;
1743 spinlock_t dev_lock; /* device lock */
1746 * How many times device was blocked for new cmds execution.
1747 * Protected by dev_lock
1752 * How many there are "on_dev" commands, i.e. ones those are being
1753 * executed by the underlying SCSI/virtual device.
1755 atomic_t on_dev_count;
1757 struct list_head blocked_cmd_list; /* protected by dev_lock */
1759 /* Used to wait for requested amount of "on_dev" commands */
1760 wait_queue_head_t on_dev_waitQ;
1762 /* A list entry used during TM, protected by scst_mutex */
1763 struct list_head tm_dev_list_entry;
1765 /* Virtual device internal ID */
1768 /* Pointer to virtual device name, for convenience only */
1771 /* List entry in global devices list */
1772 struct list_head dev_list_entry;
1775 * List of tgt_dev's, one per session, protected by scst_mutex or
1776 * dev_lock for reads and both for writes
1778 struct list_head dev_tgt_dev_list;
1780 /* List of acg_dev's, one per acg, protected by scst_mutex */
1781 struct list_head dev_acg_dev_list;
1783 /* List of dedicated threads, protected by scst_mutex */
1784 struct list_head threads_list;
1789 /* Set if tgt_kobj was initialized */
1790 unsigned int dev_kobj_initialized:1;
1792 struct kobject dev_kobj; /* kobject for this struct */
1793 struct kobject *dev_exp_kobj; /* exported groups */
1795 /* Export number in the dev's sysfs list. Protected by scst_mutex */
1796 int dev_exported_lun_num;
1800 * Used to store threads local tgt_dev specific data
1802 struct scst_thr_data_hdr {
1803 /* List entry in tgt_dev->thr_data_list */
1804 struct list_head thr_data_list_entry;
1805 struct task_struct *owner_thr; /* the owner thread */
1807 /* Function that will be called on the tgt_dev destruction */
1808 void (*free_fn) (struct scst_thr_data_hdr *data);
1812 * Used to store per-session specific device information
1814 struct scst_tgt_dev {
1815 /* List entry in sess->sess_tgt_dev_list_hash */
1816 struct list_head sess_tgt_dev_list_entry;
1818 struct scst_device *dev; /* to save extra dereferences */
1819 uint64_t lun; /* to save extra dereferences */
1822 struct sgv_pool *pool;
1825 unsigned long tgt_dev_flags; /* tgt_dev's async flags */
1827 /* Used for storage of dev handler private stuff */
1830 /* How many cmds alive on this dev in this session */
1831 atomic_t tgt_dev_cmd_count;
1834 * Used to execute cmd's in order of arrival, honoring SCSI task
1837 * Protected by sn_lock, except expected_sn, which is protected by
1838 * itself. Curr_sn must have the same size as expected_sn to
1839 * overflow simultaneously.
1843 unsigned long expected_sn;
1844 unsigned long curr_sn;
1846 struct list_head deferred_cmd_list;
1847 struct list_head skipped_sn_list;
1850 * Set if the prev cmd was ORDERED. Size must allow unprotected
1851 * modifications independant to the neighbour fields.
1853 unsigned long prev_cmd_ordered;
1855 int num_free_sn_slots; /* if it's <0, then all slots are busy */
1856 atomic_t *cur_sn_slot;
1857 atomic_t sn_slots[15];
1859 /* List of scst_thr_data_hdr and lock */
1860 spinlock_t thr_data_lock;
1861 struct list_head thr_data_list;
1863 /* Per-(device, session) dedicated IO context */
1864 struct io_context *tgt_dev_io_ctx;
1866 spinlock_t tgt_dev_lock; /* per-session device lock */
1868 /* List of UA's for this device, protected by tgt_dev_lock */
1869 struct list_head UA_list;
1871 struct scst_session *sess; /* corresponding session */
1872 struct scst_acg_dev *acg_dev; /* corresponding acg_dev */
1874 /* List entry in dev->dev_tgt_dev_list */
1875 struct list_head dev_tgt_dev_list_entry;
1877 /* Internal tmp list entry */
1878 struct list_head extra_tgt_dev_list_entry;
1880 /* Set if INQUIRY DATA HAS CHANGED UA is needed */
1881 unsigned int inq_changed_ua_needed:1;
1884 * Stored Unit Attention sense and its length for possible
1885 * subsequent REQUEST SENSE. Both protected by tgt_dev_lock.
1887 unsigned short tgt_dev_valid_sense_len;
1888 uint8_t tgt_dev_sense[SCST_SENSE_BUFFERSIZE];
1890 #ifdef CONFIG_SCST_MEASURE_LATENCY
1892 * Must be the last to allow to work with drivers who don't know
1893 * about this config time option.
1895 * Protected by sess->lat_lock.
1897 uint64_t scst_time, tgt_time, dev_time;
1898 unsigned int processed_cmds;
1899 struct scst_ext_latency_stat dev_latency_stat[SCST_LATENCY_STATS_NUM];
1904 * Used to store ACG-specific device information, like LUN
1906 struct scst_acg_dev {
1907 struct scst_device *dev; /* corresponding device */
1909 uint64_t lun; /* device's LUN in this acg */
1911 /* If set, the corresponding LU is read only */
1912 unsigned int rd_only:1;
1914 /* Set if acg_dev_kobj was initialized */
1915 unsigned int acg_dev_kobj_initialized:1;
1917 struct scst_acg *acg; /* parent acg */
1919 /* List entry in dev->dev_acg_dev_list */
1920 struct list_head dev_acg_dev_list_entry;
1922 /* List entry in acg->acg_dev_list */
1923 struct list_head acg_dev_list_entry;
1925 /* kobject for this structure */
1926 struct kobject acg_dev_kobj;
1930 * ACG - access control group. Used to store group related
1931 * control information.
1934 /* List of acg_dev's in this acg, protected by scst_mutex */
1935 struct list_head acg_dev_list;
1937 /* List of attached sessions, protected by scst_mutex */
1938 struct list_head acg_sess_list;
1940 /* List of attached acn's, protected by scst_mutex */
1941 struct list_head acn_list;
1943 /* List entry in scst_acg_list */
1944 struct list_head scst_acg_list_entry;
1946 /* Name of this acg */
1947 const char *acg_name;
1949 #ifdef CONFIG_SCST_PROC
1950 /* The pointer to the /proc directory entry */
1951 struct proc_dir_entry *acg_proc_root;
1956 * ACN - access control name. Used to store names, by which
1957 * incoming sessions will be assigned to appropriate ACG.
1960 /* Initiator's name */
1962 /* List entry in acg->acn_list */
1963 struct list_head acn_list_entry;
1967 * Used to store per-session UNIT ATTENTIONs
1969 struct scst_tgt_dev_UA {
1970 /* List entry in tgt_dev->UA_list */
1971 struct list_head UA_list_entry;
1973 /* Set if UA is global for session */
1974 unsigned short global_UA:1;
1976 /* Unit Attention valid sense len */
1977 unsigned short UA_valid_sense_len;
1978 /* Unit Attention sense buf */
1979 uint8_t UA_sense_buffer[SCST_SENSE_BUFFERSIZE];
1982 /* Used to deliver AENs */
1984 int event_fn; /* AEN fn */
1986 struct scst_session *sess; /* corresponding session */
1987 uint64_t lun; /* corresponding LUN in SCSI form */
1993 uint8_t aen_sense[SCST_STANDARD_SENSE_LEN];
1997 /* Keeps status of AEN's delivery to remote initiator */
1998 int delivery_status;
2001 #ifndef smp_mb__after_set_bit
2002 /* There is no smp_mb__after_set_bit() in the kernel */
2003 #define smp_mb__after_set_bit() smp_mb()
2007 * Registers target template
2008 * Returns 0 on success or appropriate error code otherwise
2010 int __scst_register_target_template(struct scst_tgt_template *vtt,
2011 const char *version);
2012 static inline int scst_register_target_template(struct scst_tgt_template *vtt)
2014 return __scst_register_target_template(vtt, SCST_INTERFACE_VERSION);
2018 * Unregisters target template
2020 void scst_unregister_target_template(struct scst_tgt_template *vtt);
2023 * Registers and returns target adapter
2024 * Returns new target structure on success or NULL otherwise.
2026 * If parameter "target_name" isn't NULL, then security group with name
2027 * "Default_##target_name", if created, will be used as the default
2028 * instead of "Default" one for all initiators not assigned to any other group.
2030 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
2031 const char *target_name);
2034 * Unregisters target adapter
2036 void scst_unregister(struct scst_tgt *tgt);
2039 * Registers and returns a session
2041 * Returns new session on success or NULL otherwise
2045 * atomic - true, if the function called in the atomic context. If false,
2046 * this function will block until the session registration is completed.
2047 * initiator_name - remote initiator's name, any NULL-terminated string,
2048 * e.g. iSCSI name, which used as the key to found appropriate access
2049 * control group. Could be NULL, then the default target's LUNs are used.
2050 * data - any target driver supplied data
2051 * result_fn - pointer to the function that will be
2052 * asynchronously called when session initialization finishes.
2053 * Can be NULL. Parameters:
2055 * - data - target driver supplied to scst_register_session() data
2056 * - result - session initialization result, 0 on success or
2057 * appropriate error code otherwise
2059 * Note: A session creation and initialization is a complex task,
2060 * which requires sleeping state, so it can't be fully done
2061 * in interrupt context. Therefore the "bottom half" of it, if
2062 * scst_register_session() is called from atomic context, will be
2063 * done in SCST thread context. In this case scst_register_session()
2064 * will return not completely initialized session, but the target
2065 * driver can supply commands to this session via scst_rx_cmd().
2066 * Those commands processing will be delayed inside SCST until
2067 * the session initialization is finished, then their processing
2068 * will be restarted. The target driver will be notified about
2069 * finish of the session initialization by function result_fn().
2070 * On success the target driver could do nothing, but if the
2071 * initialization fails, the target driver must ensure that
2072 * no more new commands being sent or will be sent to SCST after
2073 * result_fn() returns. All already sent to SCST commands for
2074 * failed session will be returned in xmit_response() with BUSY status.
2075 * In case of failure the driver shall call scst_unregister_session()
2076 * inside result_fn(), it will NOT be called automatically.
2078 struct scst_session *scst_register_session(struct scst_tgt *tgt, int atomic,
2079 const char *initiator_name, void *data,
2080 void (*result_fn) (struct scst_session *sess, void *data, int result));
2083 * Unregisters a session.
2085 * sess - session to be unregistered
2086 * wait - if true, instructs to wait until all commands, which
2087 * currently is being executed and belonged to the session, finished.
2088 * Otherwise, target driver should be prepared to receive
2089 * xmit_response() for the session's command after
2090 * scst_unregister_session() returns.
2091 * unreg_done_fn - pointer to the function that will be
2092 * asynchronously called when the last session's command finishes and
2093 * the session is about to be completely freed. Can be NULL.
2099 * - All outstanding commands will be finished regularly. After
2100 * scst_unregister_session() returned no new commands must be sent to
2101 * SCST via scst_rx_cmd().
2103 * - The caller must ensure that no scst_rx_cmd() or scst_rx_mgmt_fn_*() is
2104 * called in paralell with scst_unregister_session().
2106 * - Can be called before result_fn() of scst_register_session() called,
2107 * i.e. during the session registration/initialization.
2109 * - It is highly recommended to call scst_unregister_session() as soon as it
2110 * gets clear that session will be unregistered and not to wait until all
2111 * related commands finished. This function provides the wait functionality,
2112 * but it also starts recovering stuck commands, if there are any.
2113 * Otherwise, your target driver could wait for those commands forever.
2115 void scst_unregister_session(struct scst_session *sess, int wait,
2116 void (*unreg_done_fn) (struct scst_session *sess));
2119 * Registers dev handler driver
2120 * Returns 0 on success or appropriate error code otherwise
2122 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
2123 const char *version);
2124 static inline int scst_register_dev_driver(struct scst_dev_type *dev_type)
2126 return __scst_register_dev_driver(dev_type, SCST_INTERFACE_VERSION);
2130 * Unregisters dev handler driver
2132 void scst_unregister_dev_driver(struct scst_dev_type *dev_type);
2135 * Registers dev handler driver for virtual devices (eg VDISK)
2136 * Returns 0 on success or appropriate error code otherwise
2138 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
2139 const char *version);
2140 static inline int scst_register_virtual_dev_driver(
2141 struct scst_dev_type *dev_type)
2143 return __scst_register_virtual_dev_driver(dev_type,
2144 SCST_INTERFACE_VERSION);
2148 * Unregisters dev handler driver for virtual devices
2150 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type);
2153 * Creates and sends new command to SCST.
2154 * Must not be called in parallel with scst_unregister_session() for the
2155 * same sess. Returns the command on success or NULL otherwise
2157 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
2158 const uint8_t *lun, int lun_len, const uint8_t *cdb,
2159 int cdb_len, int atomic);
2162 * Notifies SCST that the driver finished its part of the command
2163 * initialization, and the command is ready for execution.
2164 * The second argument sets preferred command execition context.
2165 * See SCST_CONTEXT_* constants for details.
2169 * If cmd->set_sn_on_restart_cmd not set, this function, as well as
2170 * scst_cmd_init_stage1_done() and scst_restart_cmd(), must not be
2171 * called simultaneously for the same session (more precisely,
2172 * for the same session/LUN, i.e. tgt_dev), i.e. they must be
2173 * somehow externally serialized. This is needed to have lock free fast path in
2174 * scst_cmd_set_sn(). For majority of targets those functions are naturally
2175 * serialized by the single source of commands. Only iSCSI immediate commands
2176 * with multiple connections per session seems to be an exception. For it, some
2177 * mutex/lock shall be used for the serialization.
2179 void scst_cmd_init_done(struct scst_cmd *cmd,
2180 enum scst_exec_context pref_context);
2183 * Notifies SCST that the driver finished the first stage of the command
2184 * initialization, and the command is ready for execution, but after
2185 * SCST done the command's preprocessing preprocessing_done() function
2186 * should be called. The second argument sets preferred command execition
2187 * context. See SCST_CONTEXT_* constants for details.
2189 * See also scst_cmd_init_done() comment for the serialization requirements.
2191 static inline void scst_cmd_init_stage1_done(struct scst_cmd *cmd,
2192 enum scst_exec_context pref_context, int set_sn)
2194 cmd->preprocessing_only = 1;
2195 cmd->set_sn_on_restart_cmd = !set_sn;
2196 scst_cmd_init_done(cmd, pref_context);
2200 * Notifies SCST that the driver finished its part of the command's
2201 * preprocessing and it is ready for further processing.
2202 * The second argument sets data receiving completion status
2203 * (see SCST_PREPROCESS_STATUS_* constants for details)
2204 * The third argument sets preferred command execition context
2205 * (see SCST_CONTEXT_* constants for details).
2207 * See also scst_cmd_init_done() comment for the serialization requirements.
2209 void scst_restart_cmd(struct scst_cmd *cmd, int status,
2210 enum scst_exec_context pref_context);
2213 * Notifies SCST that the driver received all the necessary data
2214 * and the command is ready for further processing.
2215 * The second argument sets data receiving completion status
2216 * (see SCST_RX_STATUS_* constants for details)
2217 * The third argument sets preferred command execition context
2218 * (see SCST_CONTEXT_* constants for details)
2220 void scst_rx_data(struct scst_cmd *cmd, int status,
2221 enum scst_exec_context pref_context);
2224 * Notifies SCST that the driver sent the response and the command
2225 * can be freed now. Don't forget to set the delivery status, if it
2226 * isn't success, using scst_set_delivery_status() before calling
2227 * this function. The third argument sets preferred command execition
2228 * context (see SCST_CONTEXT_* constants for details)
2230 void scst_tgt_cmd_done(struct scst_cmd *cmd,
2231 enum scst_exec_context pref_context);
2234 * Creates new management command sends it for execution.
2235 * Must not be called in parallel with scst_unregister_session() for the
2236 * same sess. Returns 0 for success, error code otherwise.
2238 int scst_rx_mgmt_fn(struct scst_session *sess,
2239 const struct scst_rx_mgmt_params *params);
2242 * Creates new management command using tag and sends it for execution.
2243 * Can be used for SCST_ABORT_TASK only.
2244 * Must not be called in parallel with scst_unregister_session() for the
2245 * same sess. Returns 0 for success, error code otherwise.
2247 * Obsolete in favor of scst_rx_mgmt_fn()
2249 static inline int scst_rx_mgmt_fn_tag(struct scst_session *sess, int fn,
2250 uint64_t tag, int atomic, void *tgt_priv)
2252 struct scst_rx_mgmt_params params;
2254 BUG_ON(fn != SCST_ABORT_TASK);
2256 memset(¶ms, 0, sizeof(params));
2260 params.atomic = atomic;
2261 params.tgt_priv = tgt_priv;
2262 return scst_rx_mgmt_fn(sess, ¶ms);
2266 * Creates new management command using LUN and sends it for execution.
2267 * Currently can be used for any fn, except SCST_ABORT_TASK.
2268 * Must not be called in parallel with scst_unregister_session() for the
2269 * same sess. Returns 0 for success, error code otherwise.
2271 * Obsolete in favor of scst_rx_mgmt_fn()
2273 static inline int scst_rx_mgmt_fn_lun(struct scst_session *sess, int fn,
2274 const uint8_t *lun, int lun_len, int atomic, void *tgt_priv)
2276 struct scst_rx_mgmt_params params;
2278 BUG_ON(fn == SCST_ABORT_TASK);
2280 memset(¶ms, 0, sizeof(params));
2283 params.lun_len = lun_len;
2285 params.atomic = atomic;
2286 params.tgt_priv = tgt_priv;
2287 return scst_rx_mgmt_fn(sess, ¶ms);
2291 * Provides various info about command's CDB.
2293 * Returns: 0 on success, <0 if command is unknown, >0 if command is invalid.
2295 int scst_get_cdb_info(struct scst_cmd *cmd);
2298 * Set error SCSI status in the command and prepares it for returning it
2300 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status);
2303 * Set error in the command and fill the sense buffer
2305 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq);
2308 * Sets BUSY or TASK QUEUE FULL status
2310 void scst_set_busy(struct scst_cmd *cmd);
2313 * Check if sense in the sense buffer, if any, in the correct format. If not,
2314 * convert it to the correct format.
2316 void scst_check_convert_sense(struct scst_cmd *cmd);
2319 * Sets initial Unit Attention for sess, replacing default scst_sense_reset_UA
2321 void scst_set_initial_UA(struct scst_session *sess, int key, int asc, int ascq);
2324 * Notifies SCST core that dev changed its capacity
2326 void scst_capacity_data_changed(struct scst_device *dev);
2329 * Finds a command based on the supplied tag comparing it with one
2330 * that previously set by scst_cmd_set_tag().
2331 * Returns the command on success or NULL otherwise
2333 struct scst_cmd *scst_find_cmd_by_tag(struct scst_session *sess, uint64_t tag);
2336 * Finds a command based on user supplied data and comparision
2337 * callback function, that should return true, if the command is found.
2338 * Returns the command on success or NULL otherwise
2340 struct scst_cmd *scst_find_cmd(struct scst_session *sess, void *data,
2341 int (*cmp_fn) (struct scst_cmd *cmd,
2345 * Translates SCST's data direction to DMA one from backend storage
2348 enum dma_data_direction scst_to_dma_dir(int scst_dir);
2351 * Translates SCST data direction to DMA data direction from the perspective
2352 * of the target device.
2354 enum dma_data_direction scst_to_tgt_dma_dir(int scst_dir);
2357 * Returns true, if cmd's CDB is locally handled by SCST and 0 otherwise.
2358 * Dev handlers parse() and dev_done() not called for such commands.
2360 static inline bool scst_is_cmd_local(struct scst_cmd *cmd)
2362 return (cmd->op_flags & SCST_LOCAL_CMD) != 0;
2365 /* Returns true, if cmd can deliver UA */
2366 static inline bool scst_is_ua_command(struct scst_cmd *cmd)
2368 return (cmd->op_flags & SCST_SKIP_UA) == 0;
2372 * Registers a virtual device.
2374 * dev_type - the device's device handler
2375 * dev_name - the new device name, NULL-terminated string. Must be uniq
2376 * among all virtual devices in the system. The value isn't
2377 * copied, only the reference is stored, so the value must
2378 * remain valid during the device lifetime.
2379 * Returns assinged to the device ID on success, or negative value otherwise
2381 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
2382 const char *dev_name);
2385 * Unegisters a virtual device.
2387 * id - the device's ID, returned by the registration function
2389 void scst_unregister_virtual_device(int id);
2392 * Get/Set functions for tgt's sg_tablesize
2394 static inline int scst_tgt_get_sg_tablesize(struct scst_tgt *tgt)
2396 return tgt->sg_tablesize;
2399 static inline void scst_tgt_set_sg_tablesize(struct scst_tgt *tgt, int val)
2401 tgt->sg_tablesize = val;
2405 * Get/Set functions for tgt's target private data
2407 static inline void *scst_tgt_get_tgt_priv(struct scst_tgt *tgt)
2409 return tgt->tgt_priv;
2412 static inline void scst_tgt_set_tgt_priv(struct scst_tgt *tgt, void *val)
2414 tgt->tgt_priv = val;
2418 * Get/Set functions for session's target private data
2420 static inline void *scst_sess_get_tgt_priv(struct scst_session *sess)
2422 return sess->tgt_priv;
2425 static inline void scst_sess_set_tgt_priv(struct scst_session *sess,
2428 sess->tgt_priv = val;
2432 * Returns TRUE if cmd is being executed in atomic context.
2434 * Note: checkpatch will complain on the use of in_atomic() below. You can
2435 * safely ignore this warning since in_atomic() is used here only for debugging
2438 static inline int scst_cmd_atomic(struct scst_cmd *cmd)
2440 int res = cmd->atomic;
2441 #ifdef CONFIG_SCST_EXTRACHECKS
2442 if (unlikely((in_atomic() || in_interrupt() || irqs_disabled()) &&
2444 printk(KERN_ERR "ERROR: atomic context and non-atomic cmd\n");
2453 static inline enum scst_exec_context __scst_estimate_context(bool direct)
2456 return SCST_CONTEXT_TASKLET;
2457 else if (irqs_disabled())
2458 return SCST_CONTEXT_THREAD;
2460 return direct ? SCST_CONTEXT_DIRECT :
2461 SCST_CONTEXT_DIRECT_ATOMIC;
2464 static inline enum scst_exec_context scst_estimate_context(void)
2466 return __scst_estimate_context(0);
2469 static inline enum scst_exec_context scst_estimate_context_direct(void)
2471 return __scst_estimate_context(1);
2474 /* Returns cmd's CDB */
2475 static inline const uint8_t *scst_cmd_get_cdb(struct scst_cmd *cmd)
2480 /* Returns cmd's CDB length */
2481 static inline int scst_cmd_get_cdb_len(struct scst_cmd *cmd)
2483 return cmd->cdb_len;
2486 /* Returns cmd's extended CDB */
2487 static inline const uint8_t *scst_cmd_get_ext_cdb(struct scst_cmd *cmd)
2489 return cmd->ext_cdb;
2492 /* Returns cmd's extended CDB length */
2493 static inline int scst_cmd_get_ext_cdb_len(struct scst_cmd *cmd)
2495 return cmd->ext_cdb_len;
2498 /* Sets cmd's extended CDB and its length */
2499 static inline void scst_cmd_set_ext_cdb(struct scst_cmd *cmd,
2500 uint8_t *ext_cdb, unsigned int ext_cdb_len)
2502 cmd->ext_cdb = ext_cdb;
2503 cmd->ext_cdb_len = ext_cdb_len;
2506 /* Returns cmd's session */
2507 static inline struct scst_session *scst_cmd_get_session(struct scst_cmd *cmd)
2512 /* Returns cmd's response data length */
2513 static inline int scst_cmd_get_resp_data_len(struct scst_cmd *cmd)
2515 return cmd->resp_data_len;
2518 /* Returns if status should be sent for cmd */
2519 static inline int scst_cmd_get_is_send_status(struct scst_cmd *cmd)
2521 return cmd->is_send_status;
2525 * Returns pointer to cmd's SG data buffer.
2527 * Usage of this function is not recommended, use scst_get_buf_*()
2528 * family of functions instead.
2530 static inline struct scatterlist *scst_cmd_get_sg(struct scst_cmd *cmd)
2536 * Returns cmd's sg_cnt.
2538 * Usage of this function is not recommended, use scst_get_buf_*()
2539 * family of functions instead.
2541 static inline int scst_cmd_get_sg_cnt(struct scst_cmd *cmd)
2547 * Returns cmd's data buffer length.
2549 * In case if you need to iterate over data in the buffer, usage of
2550 * this function is not recommended, use scst_get_buf_*()
2551 * family of functions instead.
2553 static inline unsigned int scst_cmd_get_bufflen(struct scst_cmd *cmd)
2555 return cmd->bufflen;
2559 * Returns pointer to cmd's bidirectional in (WRITE) SG data buffer.
2561 * Usage of this function is not recommended, use scst_get_in_buf_*()
2562 * family of functions instead.
2564 static inline struct scatterlist *scst_cmd_get_in_sg(struct scst_cmd *cmd)
2570 * Returns cmd's bidirectional in (WRITE) sg_cnt.
2572 * Usage of this function is not recommended, use scst_get_in_buf_*()
2573 * family of functions instead.
2575 static inline int scst_cmd_get_in_sg_cnt(struct scst_cmd *cmd)
2577 return cmd->in_sg_cnt;
2581 * Returns cmd's bidirectional in (WRITE) data buffer length.
2583 * In case if you need to iterate over data in the buffer, usage of
2584 * this function is not recommended, use scst_get_in_buf_*()
2585 * family of functions instead.
2587 static inline unsigned int scst_cmd_get_in_bufflen(struct scst_cmd *cmd)
2589 return cmd->in_bufflen;
2592 /* Returns pointer to cmd's target's SG data buffer */
2593 static inline struct scatterlist *scst_cmd_get_tgt_sg(struct scst_cmd *cmd)
2598 /* Returns cmd's target's sg_cnt */
2599 static inline int scst_cmd_get_tgt_sg_cnt(struct scst_cmd *cmd)
2601 return cmd->tgt_sg_cnt;
2604 /* Sets cmd's target's SG data buffer */
2605 static inline void scst_cmd_set_tgt_sg(struct scst_cmd *cmd,
2606 struct scatterlist *sg, int sg_cnt)
2609 cmd->tgt_sg_cnt = sg_cnt;
2610 cmd->tgt_data_buf_alloced = 1;
2613 /* Returns pointer to cmd's target's IN SG data buffer */
2614 static inline struct scatterlist *scst_cmd_get_in_tgt_sg(struct scst_cmd *cmd)
2616 return cmd->tgt_in_sg;
2619 /* Returns cmd's target's IN sg_cnt */
2620 static inline int scst_cmd_get_tgt_in_sg_cnt(struct scst_cmd *cmd)
2622 return cmd->tgt_in_sg_cnt;
2625 /* Sets cmd's target's IN SG data buffer */
2626 static inline void scst_cmd_set_tgt_in_sg(struct scst_cmd *cmd,
2627 struct scatterlist *sg, int sg_cnt)
2629 WARN_ON(!cmd->tgt_data_buf_alloced);
2631 cmd->tgt_in_sg = sg;
2632 cmd->tgt_in_sg_cnt = sg_cnt;
2635 /* Returns cmd's data direction */
2636 static inline scst_data_direction scst_cmd_get_data_direction(
2637 struct scst_cmd *cmd)
2639 return cmd->data_direction;
2642 /* Returns cmd's status byte from host device */
2643 static inline uint8_t scst_cmd_get_status(struct scst_cmd *cmd)
2648 /* Returns cmd's status from host adapter itself */
2649 static inline uint8_t scst_cmd_get_msg_status(struct scst_cmd *cmd)
2651 return cmd->msg_status;
2654 /* Returns cmd's status set by low-level driver to indicate its status */
2655 static inline uint8_t scst_cmd_get_host_status(struct scst_cmd *cmd)
2657 return cmd->host_status;
2660 /* Returns cmd's status set by SCSI mid-level */
2661 static inline uint8_t scst_cmd_get_driver_status(struct scst_cmd *cmd)
2663 return cmd->driver_status;
2666 /* Returns pointer to cmd's sense buffer */
2667 static inline uint8_t *scst_cmd_get_sense_buffer(struct scst_cmd *cmd)
2672 /* Returns cmd's valid sense length */
2673 static inline int scst_cmd_get_sense_buffer_len(struct scst_cmd *cmd)
2675 return cmd->sense_valid_len;
2679 * Get/Set functions for cmd's target SN
2681 static inline uint64_t scst_cmd_get_tag(struct scst_cmd *cmd)
2686 static inline void scst_cmd_set_tag(struct scst_cmd *cmd, uint64_t tag)
2692 * Get/Set functions for cmd's target private data.
2693 * Variant with *_lock must be used if target driver uses
2694 * scst_find_cmd() to avoid race with it, except inside scst_find_cmd()'s
2695 * callback, where lock is already taken.
2697 static inline void *scst_cmd_get_tgt_priv(struct scst_cmd *cmd)
2699 return cmd->tgt_priv;
2702 static inline void scst_cmd_set_tgt_priv(struct scst_cmd *cmd, void *val)
2704 cmd->tgt_priv = val;
2707 void *scst_cmd_get_tgt_priv_lock(struct scst_cmd *cmd);
2708 void scst_cmd_set_tgt_priv_lock(struct scst_cmd *cmd, void *val);
2711 * Get/Set functions for tgt_need_alloc_data_buf flag
2713 static inline int scst_cmd_get_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2715 return cmd->tgt_need_alloc_data_buf;
2718 static inline void scst_cmd_set_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2720 cmd->tgt_need_alloc_data_buf = 1;
2724 * Get/Set functions for tgt_data_buf_alloced flag
2726 static inline int scst_cmd_get_tgt_data_buff_alloced(struct scst_cmd *cmd)
2728 return cmd->tgt_data_buf_alloced;
2731 static inline void scst_cmd_set_tgt_data_buff_alloced(struct scst_cmd *cmd)
2733 cmd->tgt_data_buf_alloced = 1;
2737 * Get/Set functions for dh_data_buf_alloced flag
2739 static inline int scst_cmd_get_dh_data_buff_alloced(struct scst_cmd *cmd)
2741 return cmd->dh_data_buf_alloced;
2744 static inline void scst_cmd_set_dh_data_buff_alloced(struct scst_cmd *cmd)
2746 cmd->dh_data_buf_alloced = 1;
2750 * Get/Set functions for no_sgv flag
2752 static inline int scst_cmd_get_no_sgv(struct scst_cmd *cmd)
2757 static inline void scst_cmd_set_no_sgv(struct scst_cmd *cmd)
2763 * Get/Set functions for tgt_sn
2765 static inline int scst_cmd_get_tgt_sn(struct scst_cmd *cmd)
2767 BUG_ON(!cmd->tgt_sn_set);
2771 static inline void scst_cmd_set_tgt_sn(struct scst_cmd *cmd, uint32_t tgt_sn)
2773 cmd->tgt_sn_set = 1;
2774 cmd->tgt_sn = tgt_sn;
2778 * Returns 1 if the cmd was aborted, so its status is invalid and no
2779 * reply shall be sent to the remote initiator. A target driver should
2780 * only clear internal resources, associated with cmd.
2782 static inline int scst_cmd_aborted(struct scst_cmd *cmd)
2784 return test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags) &&
2785 !test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
2788 /* Returns sense data format for cmd's dev */
2789 static inline bool scst_get_cmd_dev_d_sense(struct scst_cmd *cmd)
2791 return (cmd->dev != NULL) ? cmd->dev->d_sense : 0;
2795 * Get/Set functions for expected data direction, transfer length
2796 * and its validity flag
2798 static inline int scst_cmd_is_expected_set(struct scst_cmd *cmd)
2800 return cmd->expected_values_set;
2803 static inline scst_data_direction scst_cmd_get_expected_data_direction(
2804 struct scst_cmd *cmd)
2806 return cmd->expected_data_direction;
2809 static inline int scst_cmd_get_expected_transfer_len(
2810 struct scst_cmd *cmd)
2812 return cmd->expected_transfer_len;
2815 static inline int scst_cmd_get_expected_in_transfer_len(
2816 struct scst_cmd *cmd)
2818 return cmd->expected_in_transfer_len;
2821 static inline void scst_cmd_set_expected(struct scst_cmd *cmd,
2822 scst_data_direction expected_data_direction,
2823 int expected_transfer_len)
2825 cmd->expected_data_direction = expected_data_direction;
2826 cmd->expected_transfer_len = expected_transfer_len;
2827 cmd->expected_values_set = 1;
2830 static inline void scst_cmd_set_expected_in_transfer_len(struct scst_cmd *cmd,
2831 int expected_in_transfer_len)
2833 WARN_ON(!cmd->expected_values_set);
2834 cmd->expected_in_transfer_len = expected_in_transfer_len;
2838 * Get/clear functions for cmd's may_need_dma_sync
2840 static inline int scst_get_may_need_dma_sync(struct scst_cmd *cmd)
2842 return cmd->may_need_dma_sync;
2845 static inline void scst_clear_may_need_dma_sync(struct scst_cmd *cmd)
2847 cmd->may_need_dma_sync = 0;
2851 * Get/set functions for cmd's delivery_status. It is one of
2852 * SCST_CMD_DELIVERY_* constants. It specifies the status of the
2853 * command's delivery to initiator.
2855 static inline int scst_get_delivery_status(struct scst_cmd *cmd)
2857 return cmd->delivery_status;
2860 static inline void scst_set_delivery_status(struct scst_cmd *cmd,
2861 int delivery_status)
2863 cmd->delivery_status = delivery_status;
2867 * Get/Set function for mgmt cmd's target private data
2869 static inline void *scst_mgmt_cmd_get_tgt_priv(struct scst_mgmt_cmd *mcmd)
2871 return mcmd->tgt_priv;
2874 static inline void scst_mgmt_cmd_set_tgt_priv(struct scst_mgmt_cmd *mcmd,
2877 mcmd->tgt_priv = val;
2880 /* Returns mgmt cmd's completition status (SCST_MGMT_STATUS_* constants) */
2881 static inline int scst_mgmt_cmd_get_status(struct scst_mgmt_cmd *mcmd)
2883 return mcmd->status;
2886 /* Returns mgmt cmd's TM fn */
2887 static inline int scst_mgmt_cmd_get_fn(struct scst_mgmt_cmd *mcmd)
2893 * Called by dev handler's task_mgmt_fn() to notify SCST core that mcmd
2894 * is going to complete asynchronously.
2896 void scst_prepare_async_mcmd(struct scst_mgmt_cmd *mcmd);
2899 * Called by dev handler to notify SCST core that async. mcmd is completed
2900 * with status "status".
2902 void scst_async_mcmd_completed(struct scst_mgmt_cmd *mcmd, int status);
2904 /* Returns AEN's fn */
2905 static inline int scst_aen_get_event_fn(struct scst_aen *aen)
2907 return aen->event_fn;
2910 /* Returns AEN's session */
2911 static inline struct scst_session *scst_aen_get_sess(struct scst_aen *aen)
2916 /* Returns AEN's LUN */
2917 static inline uint64_t scst_aen_get_lun(struct scst_aen *aen)
2922 /* Returns SCSI AEN's sense */
2923 static inline const uint8_t *scst_aen_get_sense(struct scst_aen *aen)
2925 return aen->aen_sense;
2928 /* Returns SCSI AEN's sense length */
2929 static inline int scst_aen_get_sense_len(struct scst_aen *aen)
2931 return aen->aen_sense_len;
2935 * Get/set functions for AEN's delivery_status. It is one of
2936 * SCST_AEN_RES_* constants. It specifies the status of the
2937 * command's delivery to initiator.
2939 static inline int scst_get_aen_delivery_status(struct scst_aen *aen)
2941 return aen->delivery_status;
2944 static inline void scst_set_aen_delivery_status(struct scst_aen *aen,
2947 aen->delivery_status = status;
2951 * Notifies SCST that the driver has sent the AEN and it
2952 * can be freed now. Don't forget to set the delivery status, if it
2953 * isn't success, using scst_set_aen_delivery_status() before calling
2956 void scst_aen_done(struct scst_aen *aen);
2958 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2961 * The macro's sg_page(), sg_virt(), sg_init_table(), sg_assign_page() and
2962 * sg_set_page() have been introduced in the 2.6.24 kernel. The definitions
2963 * below are backports of the 2.6.24 macro's for older kernels. There is one
2964 * exception however: when compiling SCST on a system with a pre-2.6.24 kernel
2965 * (e.g. RHEL 5.x) where the OFED kernel headers have been installed, do not
2966 * define the backported macro's because OFED has already defined these.
2969 #ifndef __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__
2971 static inline struct page *sg_page(struct scatterlist *sg)
2976 static inline void *sg_virt(struct scatterlist *sg)
2978 return page_address(sg_page(sg)) + sg->offset;
2981 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
2983 memset(sgl, 0, sizeof(*sgl) * nents);
2986 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
2991 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
2992 unsigned int len, unsigned int offset)
2994 sg_assign_page(sg, page);
2995 sg->offset = offset;
2999 static inline struct scatterlist *sg_next(struct scatterlist *sg)
3005 #endif /* __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__ */
3007 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
3009 static inline void sg_clear(struct scatterlist *sg)
3011 memset(sg, 0, sizeof(*sg));
3012 #ifdef CONFIG_DEBUG_SG
3013 sg->sg_magic = SG_MAGIC;
3018 * Functions for access to the commands data (SG) buffer,
3019 * including HIGHMEM environment. Should be used instead of direct
3020 * access. Returns the mapped buffer length for success, 0 for EOD,
3021 * negative error code otherwise.
3023 * "Buf" argument returns the mapped buffer
3025 * The "put" function unmaps the buffer.
3027 static inline int __scst_get_buf(struct scst_cmd *cmd, struct scatterlist *sg,
3028 int sg_cnt, uint8_t **buf)
3031 int i = cmd->get_sg_buf_entry_num;
3035 if ((i >= sg_cnt) || unlikely(sg == NULL))
3038 *buf = page_address(sg_page(&sg[i]));
3039 *buf += sg[i].offset;
3042 cmd->get_sg_buf_entry_num++;
3048 static inline int scst_get_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3050 cmd->get_sg_buf_entry_num = 0;
3051 cmd->may_need_dma_sync = 1;
3052 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3055 static inline int scst_get_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3057 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3060 static inline void scst_put_buf(struct scst_cmd *cmd, void *buf)
3065 static inline int scst_get_in_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3067 cmd->get_sg_buf_entry_num = 0;
3068 cmd->may_need_dma_sync = 1;
3069 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3072 static inline int scst_get_in_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3074 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3077 static inline void scst_put_in_buf(struct scst_cmd *cmd, void *buf)
3083 * Returns approximate higher rounded buffers count that
3084 * scst_get_buf_[first|next]() return.
3086 static inline int scst_get_buf_count(struct scst_cmd *cmd)
3088 return (cmd->sg_cnt == 0) ? 1 : cmd->sg_cnt;
3092 * Returns approximate higher rounded buffers count that
3093 * scst_get_in_buf_[first|next]() return.
3095 static inline int scst_get_in_buf_count(struct scst_cmd *cmd)
3097 return (cmd->in_sg_cnt == 0) ? 1 : cmd->in_sg_cnt;
3100 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23) && !defined(BACKPORT_LINUX_WORKQUEUE_TO_2_6_19)
3101 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
3102 static inline int cancel_delayed_work_sync(struct delayed_work *work)
3104 static inline int cancel_delayed_work_sync(struct work_struct *work)
3109 res = cancel_delayed_work(work);
3110 flush_scheduled_work();
3116 * Suspends and resumes any activity.
3117 * Function scst_suspend_activity() doesn't return 0, until there are any
3118 * active commands (state after SCST_CMD_STATE_INIT). If "interruptible"
3119 * is true, it returns after SCST_SUSPENDING_TIMEOUT or if it was interrupted
3120 * by a signal with the coresponding error status < 0. If "interruptible"
3121 * is false, it will wait virtually forever.
3123 * New arriving commands stay in that state until scst_resume_activity()
3126 int scst_suspend_activity(bool interruptible);
3127 void scst_resume_activity(void);
3130 * Main SCST commands processing routing. Must be used only by dev handlers.
3131 * Argument atomic is true if function called in atomic context.
3133 void scst_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3136 * SCST commands processing routine, which should be called by dev handler
3137 * after its parse() callback returned SCST_CMD_STATE_STOP. Arguments
3138 * the same as for scst_process_active_cmd().
3140 void scst_post_parse_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3143 * Checks if command can be executed (reservations, etc.) or there are local
3144 * events, like pending UAs. Returns < 0 if command must be aborted, > 0 if
3145 * there is an event and command should be immediately completed, or 0
3148 * !! Dev handlers implementing exec() callback must call this function there !!
3149 * !! just before the actual command's execution !!
3151 int scst_check_local_events(struct scst_cmd *cmd);
3154 * Returns the next state of the SCSI target state machine in case if command's
3155 * completed abnormally.
3157 int scst_get_cmd_abnormal_done_state(const struct scst_cmd *cmd);
3160 * Sets state of the SCSI target state machine in case if command's completed
3163 void scst_set_cmd_abnormal_done_state(struct scst_cmd *cmd);
3165 struct scst_trace_log {
3170 #ifdef CONFIG_SCST_PROC
3173 * Returns target driver's root entry in SCST's /proc hierarchy.
3174 * The driver can create own files/directories here, which should
3175 * be deleted in the driver's release().
3177 struct proc_dir_entry *scst_proc_get_tgt_root(
3178 struct scst_tgt_template *vtt);
3181 * Returns device handler's root entry in SCST's /proc hierarchy.
3182 * The driver can create own files/directories here, which should
3183 * be deleted in the driver's detach()/release().
3185 struct proc_dir_entry *scst_proc_get_dev_type_root(
3186 struct scst_dev_type *dtt);
3189 ** Two library functions and the structure to help the drivers
3190 ** that use scst_debug.* facilities manage "trace_level" /proc entry.
3191 ** The functions service "standard" log levels and allow to work
3192 ** with driver specific levels, which should be passed inside as
3193 ** NULL-terminated array of struct scst_trace_log's, where:
3194 ** - val - the level's numeric value
3195 ** - token - its string representation
3198 int scst_proc_log_entry_read(struct seq_file *seq, unsigned long log_level,
3199 const struct scst_trace_log *tbl);
3200 int scst_proc_log_entry_write(struct file *file, const char __user *buf,
3201 unsigned long length, unsigned long *log_level,
3202 unsigned long default_level, const struct scst_trace_log *tbl);
3205 * helper data structure and function to create proc entry.
3207 struct scst_proc_data {
3208 const struct file_operations seq_op;
3209 int (*show)(struct seq_file *, void *);
3213 int scst_single_seq_open(struct inode *inode, struct file *file);
3215 struct proc_dir_entry *scst_create_proc_entry(struct proc_dir_entry *root,
3216 const char *name, struct scst_proc_data *pdata);
3218 #define SCST_DEF_RW_SEQ_OP(x) \
3220 .owner = THIS_MODULE, \
3221 .open = scst_single_seq_open,\
3224 .llseek = seq_lseek, \
3225 .release = single_release, \
3228 #else /* CONFIG_SCST_PROC */
3231 * Returns target driver's root sysfs kobject.
3232 * The driver can create own files/directories/links here.
3234 static inline struct kobject *scst_sysfs_get_tgtt_kobj(
3235 struct scst_tgt_template *tgtt)
3237 return &tgtt->tgtt_kobj;
3241 * Returns target's root sysfs kobject.
3242 * The driver can create own files/directories/links here.
3244 static inline struct kobject *scst_sysfs_get_tgt_kobj(
3245 struct scst_tgt *tgt)
3247 return &tgt->tgt_kobj;
3251 * Returns device handler's root sysfs kobject.
3252 * The driver can create own files/directories/links here.
3254 static inline struct kobject *scst_sysfs_get_devt_kobj(
3255 struct scst_dev_type *devt)
3257 return &devt->devt_kobj;
3261 * Returns device's root sysfs kobject.
3262 * The driver can create own files/directories/links here.
3264 static inline struct kobject *scst_sysfs_get_dev_kobj(
3265 struct scst_device *dev)
3267 return &dev->dev_kobj;
3270 #endif /* CONFIG_SCST_PROC */
3272 /* Returns target name */
3273 static inline const char *scst_get_tgt_name(const struct scst_tgt *tgt)
3275 return tgt->tgt_name;
3279 * Adds and deletes (stops) num of global SCST's threads. Returns 0 on
3280 * success, error code otherwise.
3282 int scst_add_global_threads(int num);
3283 void scst_del_global_threads(int num);
3285 int scst_alloc_sense(struct scst_cmd *cmd, int atomic);
3286 int scst_alloc_set_sense(struct scst_cmd *cmd, int atomic,
3287 const uint8_t *sense, unsigned int len);
3290 * Sets the corresponding field in the sense buffer taking sense type
3291 * into account. Returns resulting sense length.
3293 int scst_set_sense(uint8_t *buffer, int len, bool d_sense,
3294 int key, int asc, int ascq);
3297 * Returns true if the sense is valid and carrying a Unit Attention or
3300 bool scst_is_ua_sense(const uint8_t *sense, int len);
3303 * Returnes true if sense matches to (key, asc, ascq) and false otherwise.
3304 * Valid_mask is one or several SCST_SENSE_*_VALID constants setting valid
3305 * (key, asc, ascq) values.
3307 bool scst_analyze_sense(const uint8_t *sense, int len,
3308 unsigned int valid_mask, int key, int asc, int ascq);
3311 * Returnes a pseudo-random number for debugging purposes. Available only in
3314 unsigned long scst_random(void);
3317 * Sets response data length for cmd and truncates its SG vector accordingly.
3318 * The cmd->resp_data_len must not be set directly, it must be set only
3319 * using this function. Value of resp_data_len must be <= cmd->bufflen.
3321 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len);
3324 * Get/put global ref counter that prevents from entering into suspended
3325 * activities stage, so protects from any global management operations.
3327 void scst_get(void);
3328 void scst_put(void);
3333 void scst_cmd_get(struct scst_cmd *cmd);
3334 void scst_cmd_put(struct scst_cmd *cmd);
3337 * Allocates and returns pointer to SG vector with data size "size".
3338 * In *count returned the count of entries in the vector.
3339 * Returns NULL for failure.
3341 struct scatterlist *scst_alloc(int size, gfp_t gfp_mask, int *count);
3343 /* Frees SG vector returned by scst_alloc() */
3344 void scst_free(struct scatterlist *sg, int count);
3347 * Adds local to the current thread data to tgt_dev
3348 * (they will be local for the tgt_dev and current thread).
3350 void scst_add_thr_data(struct scst_tgt_dev *tgt_dev,
3351 struct scst_thr_data_hdr *data,
3352 void (*free_fn) (struct scst_thr_data_hdr *data));
3354 /* Deletes all local to threads data from tgt_dev */
3355 void scst_del_all_thr_data(struct scst_tgt_dev *tgt_dev);
3357 /* Deletes all local to threads data from all tgt_dev's of the dev */
3358 void scst_dev_del_all_thr_data(struct scst_device *dev);
3360 /* Finds local to the thread data. Returns NULL, if they not found. */
3361 struct scst_thr_data_hdr *__scst_find_thr_data(struct scst_tgt_dev *tgt_dev,
3362 struct task_struct *tsk);
3364 /* Finds local to the current thread data. Returns NULL, if they not found. */
3365 static inline struct scst_thr_data_hdr *scst_find_thr_data(
3366 struct scst_tgt_dev *tgt_dev)
3368 return __scst_find_thr_data(tgt_dev, current);
3371 static inline void scst_thr_data_get(struct scst_thr_data_hdr *data)
3373 atomic_inc(&data->ref);
3376 static inline void scst_thr_data_put(struct scst_thr_data_hdr *data)
3378 if (atomic_dec_and_test(&data->ref))
3379 data->free_fn(data);
3383 ** Generic parse() support routines.
3384 ** Done via pointer on functions to avoid unneeded dereferences on
3388 /* Calculates and returns block shift for the given sector size */
3389 int scst_calc_block_shift(int sector_size);
3391 /* Generic parse() for SBC (disk) devices */
3392 int scst_sbc_generic_parse(struct scst_cmd *cmd,
3393 int (*get_block_shift)(struct scst_cmd *cmd));
3395 /* Generic parse() for MMC (cdrom) devices */
3396 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
3397 int (*get_block_shift)(struct scst_cmd *cmd));
3399 /* Generic parse() for MO disk devices */
3400 int scst_modisk_generic_parse(struct scst_cmd *cmd,
3401 int (*get_block_shift)(struct scst_cmd *cmd));
3403 /* Generic parse() for tape devices */
3404 int scst_tape_generic_parse(struct scst_cmd *cmd,
3405 int (*get_block_size)(struct scst_cmd *cmd));
3407 /* Generic parse() for changer devices */
3408 int scst_changer_generic_parse(struct scst_cmd *cmd,
3409 int (*nothing)(struct scst_cmd *cmd));
3411 /* Generic parse() for "processor" devices */
3412 int scst_processor_generic_parse(struct scst_cmd *cmd,
3413 int (*nothing)(struct scst_cmd *cmd));
3415 /* Generic parse() for RAID devices */
3416 int scst_raid_generic_parse(struct scst_cmd *cmd,
3417 int (*nothing)(struct scst_cmd *cmd));
3420 ** Generic dev_done() support routines.
3421 ** Done via pointer on functions to avoid unneeded dereferences on
3425 /* Generic dev_done() for block devices */
3426 int scst_block_generic_dev_done(struct scst_cmd *cmd,
3427 void (*set_block_shift)(struct scst_cmd *cmd, int block_shift));
3429 /* Generic dev_done() for tape devices */
3430 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
3431 void (*set_block_size)(struct scst_cmd *cmd, int block_size));
3434 * Issues a MODE SENSE for control mode page data and sets the corresponding
3435 * dev's parameter from it. Returns 0 on success and not 0 otherwise.
3437 int scst_obtain_device_parameters(struct scst_device *dev);
3440 * Has to be put here open coded, because Linux doesn't have equivalent, which
3441 * allows exclusive wake ups of threads in LIFO order. We need it to let (yet)
3442 * unneeded threads sleep and not pollute CPU cache by their stacks.
3444 static inline void add_wait_queue_exclusive_head(wait_queue_head_t *q,
3447 unsigned long flags;
3449 wait->flags |= WQ_FLAG_EXCLUSIVE;
3450 spin_lock_irqsave(&q->lock, flags);
3451 __add_wait_queue(q, wait);
3452 spin_unlock_irqrestore(&q->lock, flags);
3455 #endif /* __SCST_H */