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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1723 /* Used to translate SCSI's cmd to SCST's cmd */
1724 struct gendisk *rq_disk;
1727 /* Corresponding real SCSI device, could be NULL for virtual devices */
1728 struct scsi_device *scsi_dev;
1730 /* Pointer to lists of commands with the lock */
1731 struct scst_cmd_lists *p_cmd_lists;
1733 /* Lists of commands with lock, if dedicated threads are used */
1734 struct scst_cmd_lists cmd_lists;
1736 /* Per-device dedicated IO context */
1737 struct io_context *dev_io_ctx;
1739 /* How many cmds alive on this dev */
1740 atomic_t dev_cmd_count;
1742 /* How many write cmds alive on this dev. Temporary, ToDo */
1743 atomic_t write_cmd_count;
1745 spinlock_t dev_lock; /* device lock */
1748 * How many times device was blocked for new cmds execution.
1749 * Protected by dev_lock
1754 * How many there are "on_dev" commands, i.e. ones those are being
1755 * executed by the underlying SCSI/virtual device.
1757 atomic_t on_dev_count;
1759 struct list_head blocked_cmd_list; /* protected by dev_lock */
1761 /* Used to wait for requested amount of "on_dev" commands */
1762 wait_queue_head_t on_dev_waitQ;
1764 /* A list entry used during TM, protected by scst_mutex */
1765 struct list_head tm_dev_list_entry;
1767 /* Virtual device internal ID */
1770 /* Pointer to virtual device name, for convenience only */
1773 /* List entry in global devices list */
1774 struct list_head dev_list_entry;
1777 * List of tgt_dev's, one per session, protected by scst_mutex or
1778 * dev_lock for reads and both for writes
1780 struct list_head dev_tgt_dev_list;
1782 /* List of acg_dev's, one per acg, protected by scst_mutex */
1783 struct list_head dev_acg_dev_list;
1785 /* List of dedicated threads, protected by scst_mutex */
1786 struct list_head threads_list;
1791 /* Set if tgt_kobj was initialized */
1792 unsigned int dev_kobj_initialized:1;
1794 struct kobject dev_kobj; /* kobject for this struct */
1795 struct kobject *dev_exp_kobj; /* exported groups */
1797 /* Export number in the dev's sysfs list. Protected by scst_mutex */
1798 int dev_exported_lun_num;
1802 * Used to store threads local tgt_dev specific data
1804 struct scst_thr_data_hdr {
1805 /* List entry in tgt_dev->thr_data_list */
1806 struct list_head thr_data_list_entry;
1807 struct task_struct *owner_thr; /* the owner thread */
1809 /* Function that will be called on the tgt_dev destruction */
1810 void (*free_fn) (struct scst_thr_data_hdr *data);
1814 * Used to store per-session specific device information
1816 struct scst_tgt_dev {
1817 /* List entry in sess->sess_tgt_dev_list_hash */
1818 struct list_head sess_tgt_dev_list_entry;
1820 struct scst_device *dev; /* to save extra dereferences */
1821 uint64_t lun; /* to save extra dereferences */
1824 struct sgv_pool *pool;
1827 unsigned long tgt_dev_flags; /* tgt_dev's async flags */
1829 /* Used for storage of dev handler private stuff */
1832 /* How many cmds alive on this dev in this session */
1833 atomic_t tgt_dev_cmd_count;
1836 * Used to execute cmd's in order of arrival, honoring SCSI task
1839 * Protected by sn_lock, except expected_sn, which is protected by
1840 * itself. Curr_sn must have the same size as expected_sn to
1841 * overflow simultaneously.
1845 unsigned long expected_sn;
1846 unsigned long curr_sn;
1848 struct list_head deferred_cmd_list;
1849 struct list_head skipped_sn_list;
1852 * Set if the prev cmd was ORDERED. Size must allow unprotected
1853 * modifications independant to the neighbour fields.
1855 unsigned long prev_cmd_ordered;
1857 int num_free_sn_slots; /* if it's <0, then all slots are busy */
1858 atomic_t *cur_sn_slot;
1859 atomic_t sn_slots[15];
1861 /* List of scst_thr_data_hdr and lock */
1862 spinlock_t thr_data_lock;
1863 struct list_head thr_data_list;
1865 /* Per-(device, session) dedicated IO context */
1866 struct io_context *tgt_dev_io_ctx;
1868 spinlock_t tgt_dev_lock; /* per-session device lock */
1870 /* List of UA's for this device, protected by tgt_dev_lock */
1871 struct list_head UA_list;
1873 struct scst_session *sess; /* corresponding session */
1874 struct scst_acg_dev *acg_dev; /* corresponding acg_dev */
1876 /* List entry in dev->dev_tgt_dev_list */
1877 struct list_head dev_tgt_dev_list_entry;
1879 /* Internal tmp list entry */
1880 struct list_head extra_tgt_dev_list_entry;
1882 /* Set if INQUIRY DATA HAS CHANGED UA is needed */
1883 unsigned int inq_changed_ua_needed:1;
1886 * Stored Unit Attention sense and its length for possible
1887 * subsequent REQUEST SENSE. Both protected by tgt_dev_lock.
1889 unsigned short tgt_dev_valid_sense_len;
1890 uint8_t tgt_dev_sense[SCST_SENSE_BUFFERSIZE];
1892 #ifdef CONFIG_SCST_MEASURE_LATENCY
1894 * Must be the last to allow to work with drivers who don't know
1895 * about this config time option.
1897 * Protected by sess->lat_lock.
1899 uint64_t scst_time, tgt_time, dev_time;
1900 unsigned int processed_cmds;
1901 struct scst_ext_latency_stat dev_latency_stat[SCST_LATENCY_STATS_NUM];
1906 * Used to store ACG-specific device information, like LUN
1908 struct scst_acg_dev {
1909 struct scst_device *dev; /* corresponding device */
1911 uint64_t lun; /* device's LUN in this acg */
1913 /* If set, the corresponding LU is read only */
1914 unsigned int rd_only:1;
1916 /* Set if acg_dev_kobj was initialized */
1917 unsigned int acg_dev_kobj_initialized:1;
1919 struct scst_acg *acg; /* parent acg */
1921 /* List entry in dev->dev_acg_dev_list */
1922 struct list_head dev_acg_dev_list_entry;
1924 /* List entry in acg->acg_dev_list */
1925 struct list_head acg_dev_list_entry;
1927 /* kobject for this structure */
1928 struct kobject acg_dev_kobj;
1932 * ACG - access control group. Used to store group related
1933 * control information.
1936 /* List of acg_dev's in this acg, protected by scst_mutex */
1937 struct list_head acg_dev_list;
1939 /* List of attached sessions, protected by scst_mutex */
1940 struct list_head acg_sess_list;
1942 /* List of attached acn's, protected by scst_mutex */
1943 struct list_head acn_list;
1945 /* List entry in scst_acg_list */
1946 struct list_head scst_acg_list_entry;
1948 /* Name of this acg */
1949 const char *acg_name;
1951 #ifdef CONFIG_SCST_PROC
1952 /* The pointer to the /proc directory entry */
1953 struct proc_dir_entry *acg_proc_root;
1958 * ACN - access control name. Used to store names, by which
1959 * incoming sessions will be assigned to appropriate ACG.
1962 /* Initiator's name */
1964 /* List entry in acg->acn_list */
1965 struct list_head acn_list_entry;
1969 * Used to store per-session UNIT ATTENTIONs
1971 struct scst_tgt_dev_UA {
1972 /* List entry in tgt_dev->UA_list */
1973 struct list_head UA_list_entry;
1975 /* Set if UA is global for session */
1976 unsigned short global_UA:1;
1978 /* Unit Attention valid sense len */
1979 unsigned short UA_valid_sense_len;
1980 /* Unit Attention sense buf */
1981 uint8_t UA_sense_buffer[SCST_SENSE_BUFFERSIZE];
1984 /* Used to deliver AENs */
1986 int event_fn; /* AEN fn */
1988 struct scst_session *sess; /* corresponding session */
1989 uint64_t lun; /* corresponding LUN in SCSI form */
1995 uint8_t aen_sense[SCST_STANDARD_SENSE_LEN];
1999 /* Keeps status of AEN's delivery to remote initiator */
2000 int delivery_status;
2003 #ifndef smp_mb__after_set_bit
2004 /* There is no smp_mb__after_set_bit() in the kernel */
2005 #define smp_mb__after_set_bit() smp_mb()
2009 * Registers target template
2010 * Returns 0 on success or appropriate error code otherwise
2012 int __scst_register_target_template(struct scst_tgt_template *vtt,
2013 const char *version);
2014 static inline int scst_register_target_template(struct scst_tgt_template *vtt)
2016 return __scst_register_target_template(vtt, SCST_INTERFACE_VERSION);
2020 * Unregisters target template
2022 void scst_unregister_target_template(struct scst_tgt_template *vtt);
2025 * Registers and returns target adapter
2026 * Returns new target structure on success or NULL otherwise.
2028 * If parameter "target_name" isn't NULL, then security group with name
2029 * "Default_##target_name", if created, will be used as the default
2030 * instead of "Default" one for all initiators not assigned to any other group.
2032 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
2033 const char *target_name);
2036 * Unregisters target adapter
2038 void scst_unregister(struct scst_tgt *tgt);
2041 * Registers and returns a session
2043 * Returns new session on success or NULL otherwise
2047 * atomic - true, if the function called in the atomic context. If false,
2048 * this function will block until the session registration is completed.
2049 * initiator_name - remote initiator's name, any NULL-terminated string,
2050 * e.g. iSCSI name, which used as the key to found appropriate access
2051 * control group. Could be NULL, then the default target's LUNs are used.
2052 * data - any target driver supplied data
2053 * result_fn - pointer to the function that will be
2054 * asynchronously called when session initialization finishes.
2055 * Can be NULL. Parameters:
2057 * - data - target driver supplied to scst_register_session() data
2058 * - result - session initialization result, 0 on success or
2059 * appropriate error code otherwise
2061 * Note: A session creation and initialization is a complex task,
2062 * which requires sleeping state, so it can't be fully done
2063 * in interrupt context. Therefore the "bottom half" of it, if
2064 * scst_register_session() is called from atomic context, will be
2065 * done in SCST thread context. In this case scst_register_session()
2066 * will return not completely initialized session, but the target
2067 * driver can supply commands to this session via scst_rx_cmd().
2068 * Those commands processing will be delayed inside SCST until
2069 * the session initialization is finished, then their processing
2070 * will be restarted. The target driver will be notified about
2071 * finish of the session initialization by function result_fn().
2072 * On success the target driver could do nothing, but if the
2073 * initialization fails, the target driver must ensure that
2074 * no more new commands being sent or will be sent to SCST after
2075 * result_fn() returns. All already sent to SCST commands for
2076 * failed session will be returned in xmit_response() with BUSY status.
2077 * In case of failure the driver shall call scst_unregister_session()
2078 * inside result_fn(), it will NOT be called automatically.
2080 struct scst_session *scst_register_session(struct scst_tgt *tgt, int atomic,
2081 const char *initiator_name, void *data,
2082 void (*result_fn) (struct scst_session *sess, void *data, int result));
2085 * Unregisters a session.
2087 * sess - session to be unregistered
2088 * wait - if true, instructs to wait until all commands, which
2089 * currently is being executed and belonged to the session, finished.
2090 * Otherwise, target driver should be prepared to receive
2091 * xmit_response() for the session's command after
2092 * scst_unregister_session() returns.
2093 * unreg_done_fn - pointer to the function that will be
2094 * asynchronously called when the last session's command finishes and
2095 * the session is about to be completely freed. Can be NULL.
2101 * - All outstanding commands will be finished regularly. After
2102 * scst_unregister_session() returned no new commands must be sent to
2103 * SCST via scst_rx_cmd().
2105 * - The caller must ensure that no scst_rx_cmd() or scst_rx_mgmt_fn_*() is
2106 * called in paralell with scst_unregister_session().
2108 * - Can be called before result_fn() of scst_register_session() called,
2109 * i.e. during the session registration/initialization.
2111 * - It is highly recommended to call scst_unregister_session() as soon as it
2112 * gets clear that session will be unregistered and not to wait until all
2113 * related commands finished. This function provides the wait functionality,
2114 * but it also starts recovering stuck commands, if there are any.
2115 * Otherwise, your target driver could wait for those commands forever.
2117 void scst_unregister_session(struct scst_session *sess, int wait,
2118 void (*unreg_done_fn) (struct scst_session *sess));
2121 * Registers dev handler driver
2122 * Returns 0 on success or appropriate error code otherwise
2124 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
2125 const char *version);
2126 static inline int scst_register_dev_driver(struct scst_dev_type *dev_type)
2128 return __scst_register_dev_driver(dev_type, SCST_INTERFACE_VERSION);
2132 * Unregisters dev handler driver
2134 void scst_unregister_dev_driver(struct scst_dev_type *dev_type);
2137 * Registers dev handler driver for virtual devices (eg VDISK)
2138 * Returns 0 on success or appropriate error code otherwise
2140 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
2141 const char *version);
2142 static inline int scst_register_virtual_dev_driver(
2143 struct scst_dev_type *dev_type)
2145 return __scst_register_virtual_dev_driver(dev_type,
2146 SCST_INTERFACE_VERSION);
2150 * Unregisters dev handler driver for virtual devices
2152 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type);
2155 * Creates and sends new command to SCST.
2156 * Must not be called in parallel with scst_unregister_session() for the
2157 * same sess. Returns the command on success or NULL otherwise
2159 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
2160 const uint8_t *lun, int lun_len, const uint8_t *cdb,
2161 int cdb_len, int atomic);
2164 * Notifies SCST that the driver finished its part of the command
2165 * initialization, and the command is ready for execution.
2166 * The second argument sets preferred command execition context.
2167 * See SCST_CONTEXT_* constants for details.
2171 * If cmd->set_sn_on_restart_cmd not set, this function, as well as
2172 * scst_cmd_init_stage1_done() and scst_restart_cmd(), must not be
2173 * called simultaneously for the same session (more precisely,
2174 * for the same session/LUN, i.e. tgt_dev), i.e. they must be
2175 * somehow externally serialized. This is needed to have lock free fast path in
2176 * scst_cmd_set_sn(). For majority of targets those functions are naturally
2177 * serialized by the single source of commands. Only iSCSI immediate commands
2178 * with multiple connections per session seems to be an exception. For it, some
2179 * mutex/lock shall be used for the serialization.
2181 void scst_cmd_init_done(struct scst_cmd *cmd,
2182 enum scst_exec_context pref_context);
2185 * Notifies SCST that the driver finished the first stage of the command
2186 * initialization, and the command is ready for execution, but after
2187 * SCST done the command's preprocessing preprocessing_done() function
2188 * should be called. The second argument sets preferred command execition
2189 * context. See SCST_CONTEXT_* constants for details.
2191 * See also scst_cmd_init_done() comment for the serialization requirements.
2193 static inline void scst_cmd_init_stage1_done(struct scst_cmd *cmd,
2194 enum scst_exec_context pref_context, int set_sn)
2196 cmd->preprocessing_only = 1;
2197 cmd->set_sn_on_restart_cmd = !set_sn;
2198 scst_cmd_init_done(cmd, pref_context);
2202 * Notifies SCST that the driver finished its part of the command's
2203 * preprocessing and it is ready for further processing.
2204 * The second argument sets data receiving completion status
2205 * (see SCST_PREPROCESS_STATUS_* constants for details)
2206 * The third argument sets preferred command execition context
2207 * (see SCST_CONTEXT_* constants for details).
2209 * See also scst_cmd_init_done() comment for the serialization requirements.
2211 void scst_restart_cmd(struct scst_cmd *cmd, int status,
2212 enum scst_exec_context pref_context);
2215 * Notifies SCST that the driver received all the necessary data
2216 * and the command is ready for further processing.
2217 * The second argument sets data receiving completion status
2218 * (see SCST_RX_STATUS_* constants for details)
2219 * The third argument sets preferred command execition context
2220 * (see SCST_CONTEXT_* constants for details)
2222 void scst_rx_data(struct scst_cmd *cmd, int status,
2223 enum scst_exec_context pref_context);
2226 * Notifies SCST that the driver sent the response and the command
2227 * can be freed now. Don't forget to set the delivery status, if it
2228 * isn't success, using scst_set_delivery_status() before calling
2229 * this function. The third argument sets preferred command execition
2230 * context (see SCST_CONTEXT_* constants for details)
2232 void scst_tgt_cmd_done(struct scst_cmd *cmd,
2233 enum scst_exec_context pref_context);
2236 * Creates new management command sends it for execution.
2237 * Must not be called in parallel with scst_unregister_session() for the
2238 * same sess. Returns 0 for success, error code otherwise.
2240 int scst_rx_mgmt_fn(struct scst_session *sess,
2241 const struct scst_rx_mgmt_params *params);
2244 * Creates new management command using tag and sends it for execution.
2245 * Can be used for SCST_ABORT_TASK only.
2246 * Must not be called in parallel with scst_unregister_session() for the
2247 * same sess. Returns 0 for success, error code otherwise.
2249 * Obsolete in favor of scst_rx_mgmt_fn()
2251 static inline int scst_rx_mgmt_fn_tag(struct scst_session *sess, int fn,
2252 uint64_t tag, int atomic, void *tgt_priv)
2254 struct scst_rx_mgmt_params params;
2256 BUG_ON(fn != SCST_ABORT_TASK);
2258 memset(¶ms, 0, sizeof(params));
2262 params.atomic = atomic;
2263 params.tgt_priv = tgt_priv;
2264 return scst_rx_mgmt_fn(sess, ¶ms);
2268 * Creates new management command using LUN and sends it for execution.
2269 * Currently can be used for any fn, except SCST_ABORT_TASK.
2270 * Must not be called in parallel with scst_unregister_session() for the
2271 * same sess. Returns 0 for success, error code otherwise.
2273 * Obsolete in favor of scst_rx_mgmt_fn()
2275 static inline int scst_rx_mgmt_fn_lun(struct scst_session *sess, int fn,
2276 const uint8_t *lun, int lun_len, int atomic, void *tgt_priv)
2278 struct scst_rx_mgmt_params params;
2280 BUG_ON(fn == SCST_ABORT_TASK);
2282 memset(¶ms, 0, sizeof(params));
2285 params.lun_len = lun_len;
2287 params.atomic = atomic;
2288 params.tgt_priv = tgt_priv;
2289 return scst_rx_mgmt_fn(sess, ¶ms);
2293 * Provides various info about command's CDB.
2295 * Returns: 0 on success, <0 if command is unknown, >0 if command is invalid.
2297 int scst_get_cdb_info(struct scst_cmd *cmd);
2300 * Set error SCSI status in the command and prepares it for returning it
2302 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status);
2305 * Set error in the command and fill the sense buffer
2307 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq);
2310 * Sets BUSY or TASK QUEUE FULL status
2312 void scst_set_busy(struct scst_cmd *cmd);
2315 * Check if sense in the sense buffer, if any, in the correct format. If not,
2316 * convert it to the correct format.
2318 void scst_check_convert_sense(struct scst_cmd *cmd);
2321 * Sets initial Unit Attention for sess, replacing default scst_sense_reset_UA
2323 void scst_set_initial_UA(struct scst_session *sess, int key, int asc, int ascq);
2326 * Notifies SCST core that dev changed its capacity
2328 void scst_capacity_data_changed(struct scst_device *dev);
2331 * Finds a command based on the supplied tag comparing it with one
2332 * that previously set by scst_cmd_set_tag().
2333 * Returns the command on success or NULL otherwise
2335 struct scst_cmd *scst_find_cmd_by_tag(struct scst_session *sess, uint64_t tag);
2338 * Finds a command based on user supplied data and comparision
2339 * callback function, that should return true, if the command is found.
2340 * Returns the command on success or NULL otherwise
2342 struct scst_cmd *scst_find_cmd(struct scst_session *sess, void *data,
2343 int (*cmp_fn) (struct scst_cmd *cmd,
2347 * Translates SCST's data direction to DMA one from backend storage
2350 enum dma_data_direction scst_to_dma_dir(int scst_dir);
2353 * Translates SCST data direction to DMA data direction from the perspective
2354 * of the target device.
2356 enum dma_data_direction scst_to_tgt_dma_dir(int scst_dir);
2359 * Returns true, if cmd's CDB is locally handled by SCST and 0 otherwise.
2360 * Dev handlers parse() and dev_done() not called for such commands.
2362 static inline bool scst_is_cmd_local(struct scst_cmd *cmd)
2364 return (cmd->op_flags & SCST_LOCAL_CMD) != 0;
2367 /* Returns true, if cmd can deliver UA */
2368 static inline bool scst_is_ua_command(struct scst_cmd *cmd)
2370 return (cmd->op_flags & SCST_SKIP_UA) == 0;
2374 * Registers a virtual device.
2376 * dev_type - the device's device handler
2377 * dev_name - the new device name, NULL-terminated string. Must be uniq
2378 * among all virtual devices in the system. The value isn't
2379 * copied, only the reference is stored, so the value must
2380 * remain valid during the device lifetime.
2381 * Returns assinged to the device ID on success, or negative value otherwise
2383 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
2384 const char *dev_name);
2387 * Unegisters a virtual device.
2389 * id - the device's ID, returned by the registration function
2391 void scst_unregister_virtual_device(int id);
2394 * Get/Set functions for tgt's sg_tablesize
2396 static inline int scst_tgt_get_sg_tablesize(struct scst_tgt *tgt)
2398 return tgt->sg_tablesize;
2401 static inline void scst_tgt_set_sg_tablesize(struct scst_tgt *tgt, int val)
2403 tgt->sg_tablesize = val;
2407 * Get/Set functions for tgt's target private data
2409 static inline void *scst_tgt_get_tgt_priv(struct scst_tgt *tgt)
2411 return tgt->tgt_priv;
2414 static inline void scst_tgt_set_tgt_priv(struct scst_tgt *tgt, void *val)
2416 tgt->tgt_priv = val;
2420 * Get/Set functions for session's target private data
2422 static inline void *scst_sess_get_tgt_priv(struct scst_session *sess)
2424 return sess->tgt_priv;
2427 static inline void scst_sess_set_tgt_priv(struct scst_session *sess,
2430 sess->tgt_priv = val;
2434 * Returns TRUE if cmd is being executed in atomic context.
2436 * Note: checkpatch will complain on the use of in_atomic() below. You can
2437 * safely ignore this warning since in_atomic() is used here only for debugging
2440 static inline int scst_cmd_atomic(struct scst_cmd *cmd)
2442 int res = cmd->atomic;
2443 #ifdef CONFIG_SCST_EXTRACHECKS
2444 if (unlikely((in_atomic() || in_interrupt() || irqs_disabled()) &&
2446 printk(KERN_ERR "ERROR: atomic context and non-atomic cmd\n");
2455 static inline enum scst_exec_context __scst_estimate_context(bool direct)
2458 return SCST_CONTEXT_TASKLET;
2459 else if (irqs_disabled())
2460 return SCST_CONTEXT_THREAD;
2462 return direct ? SCST_CONTEXT_DIRECT :
2463 SCST_CONTEXT_DIRECT_ATOMIC;
2466 static inline enum scst_exec_context scst_estimate_context(void)
2468 return __scst_estimate_context(0);
2471 static inline enum scst_exec_context scst_estimate_context_direct(void)
2473 return __scst_estimate_context(1);
2476 /* Returns cmd's CDB */
2477 static inline const uint8_t *scst_cmd_get_cdb(struct scst_cmd *cmd)
2482 /* Returns cmd's CDB length */
2483 static inline int scst_cmd_get_cdb_len(struct scst_cmd *cmd)
2485 return cmd->cdb_len;
2488 /* Returns cmd's extended CDB */
2489 static inline const uint8_t *scst_cmd_get_ext_cdb(struct scst_cmd *cmd)
2491 return cmd->ext_cdb;
2494 /* Returns cmd's extended CDB length */
2495 static inline int scst_cmd_get_ext_cdb_len(struct scst_cmd *cmd)
2497 return cmd->ext_cdb_len;
2500 /* Sets cmd's extended CDB and its length */
2501 static inline void scst_cmd_set_ext_cdb(struct scst_cmd *cmd,
2502 uint8_t *ext_cdb, unsigned int ext_cdb_len)
2504 cmd->ext_cdb = ext_cdb;
2505 cmd->ext_cdb_len = ext_cdb_len;
2508 /* Returns cmd's session */
2509 static inline struct scst_session *scst_cmd_get_session(struct scst_cmd *cmd)
2514 /* Returns cmd's response data length */
2515 static inline int scst_cmd_get_resp_data_len(struct scst_cmd *cmd)
2517 return cmd->resp_data_len;
2520 /* Returns if status should be sent for cmd */
2521 static inline int scst_cmd_get_is_send_status(struct scst_cmd *cmd)
2523 return cmd->is_send_status;
2527 * Returns pointer to cmd's SG data buffer.
2529 * Usage of this function is not recommended, use scst_get_buf_*()
2530 * family of functions instead.
2532 static inline struct scatterlist *scst_cmd_get_sg(struct scst_cmd *cmd)
2538 * Returns cmd's sg_cnt.
2540 * Usage of this function is not recommended, use scst_get_buf_*()
2541 * family of functions instead.
2543 static inline int scst_cmd_get_sg_cnt(struct scst_cmd *cmd)
2549 * Returns cmd's data buffer length.
2551 * In case if you need to iterate over data in the buffer, usage of
2552 * this function is not recommended, use scst_get_buf_*()
2553 * family of functions instead.
2555 static inline unsigned int scst_cmd_get_bufflen(struct scst_cmd *cmd)
2557 return cmd->bufflen;
2561 * Returns pointer to cmd's bidirectional in (WRITE) SG data buffer.
2563 * Usage of this function is not recommended, use scst_get_in_buf_*()
2564 * family of functions instead.
2566 static inline struct scatterlist *scst_cmd_get_in_sg(struct scst_cmd *cmd)
2572 * Returns cmd's bidirectional in (WRITE) sg_cnt.
2574 * Usage of this function is not recommended, use scst_get_in_buf_*()
2575 * family of functions instead.
2577 static inline int scst_cmd_get_in_sg_cnt(struct scst_cmd *cmd)
2579 return cmd->in_sg_cnt;
2583 * Returns cmd's bidirectional in (WRITE) data buffer length.
2585 * In case if you need to iterate over data in the buffer, usage of
2586 * this function is not recommended, use scst_get_in_buf_*()
2587 * family of functions instead.
2589 static inline unsigned int scst_cmd_get_in_bufflen(struct scst_cmd *cmd)
2591 return cmd->in_bufflen;
2594 /* Returns pointer to cmd's target's SG data buffer */
2595 static inline struct scatterlist *scst_cmd_get_tgt_sg(struct scst_cmd *cmd)
2600 /* Returns cmd's target's sg_cnt */
2601 static inline int scst_cmd_get_tgt_sg_cnt(struct scst_cmd *cmd)
2603 return cmd->tgt_sg_cnt;
2606 /* Sets cmd's target's SG data buffer */
2607 static inline void scst_cmd_set_tgt_sg(struct scst_cmd *cmd,
2608 struct scatterlist *sg, int sg_cnt)
2611 cmd->tgt_sg_cnt = sg_cnt;
2612 cmd->tgt_data_buf_alloced = 1;
2615 /* Returns pointer to cmd's target's IN SG data buffer */
2616 static inline struct scatterlist *scst_cmd_get_in_tgt_sg(struct scst_cmd *cmd)
2618 return cmd->tgt_in_sg;
2621 /* Returns cmd's target's IN sg_cnt */
2622 static inline int scst_cmd_get_tgt_in_sg_cnt(struct scst_cmd *cmd)
2624 return cmd->tgt_in_sg_cnt;
2627 /* Sets cmd's target's IN SG data buffer */
2628 static inline void scst_cmd_set_tgt_in_sg(struct scst_cmd *cmd,
2629 struct scatterlist *sg, int sg_cnt)
2631 WARN_ON(!cmd->tgt_data_buf_alloced);
2633 cmd->tgt_in_sg = sg;
2634 cmd->tgt_in_sg_cnt = sg_cnt;
2637 /* Returns cmd's data direction */
2638 static inline scst_data_direction scst_cmd_get_data_direction(
2639 struct scst_cmd *cmd)
2641 return cmd->data_direction;
2644 /* Returns cmd's status byte from host device */
2645 static inline uint8_t scst_cmd_get_status(struct scst_cmd *cmd)
2650 /* Returns cmd's status from host adapter itself */
2651 static inline uint8_t scst_cmd_get_msg_status(struct scst_cmd *cmd)
2653 return cmd->msg_status;
2656 /* Returns cmd's status set by low-level driver to indicate its status */
2657 static inline uint8_t scst_cmd_get_host_status(struct scst_cmd *cmd)
2659 return cmd->host_status;
2662 /* Returns cmd's status set by SCSI mid-level */
2663 static inline uint8_t scst_cmd_get_driver_status(struct scst_cmd *cmd)
2665 return cmd->driver_status;
2668 /* Returns pointer to cmd's sense buffer */
2669 static inline uint8_t *scst_cmd_get_sense_buffer(struct scst_cmd *cmd)
2674 /* Returns cmd's valid sense length */
2675 static inline int scst_cmd_get_sense_buffer_len(struct scst_cmd *cmd)
2677 return cmd->sense_valid_len;
2681 * Get/Set functions for cmd's target SN
2683 static inline uint64_t scst_cmd_get_tag(struct scst_cmd *cmd)
2688 static inline void scst_cmd_set_tag(struct scst_cmd *cmd, uint64_t tag)
2694 * Get/Set functions for cmd's target private data.
2695 * Variant with *_lock must be used if target driver uses
2696 * scst_find_cmd() to avoid race with it, except inside scst_find_cmd()'s
2697 * callback, where lock is already taken.
2699 static inline void *scst_cmd_get_tgt_priv(struct scst_cmd *cmd)
2701 return cmd->tgt_priv;
2704 static inline void scst_cmd_set_tgt_priv(struct scst_cmd *cmd, void *val)
2706 cmd->tgt_priv = val;
2709 void *scst_cmd_get_tgt_priv_lock(struct scst_cmd *cmd);
2710 void scst_cmd_set_tgt_priv_lock(struct scst_cmd *cmd, void *val);
2713 * Get/Set functions for tgt_need_alloc_data_buf flag
2715 static inline int scst_cmd_get_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2717 return cmd->tgt_need_alloc_data_buf;
2720 static inline void scst_cmd_set_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2722 cmd->tgt_need_alloc_data_buf = 1;
2726 * Get/Set functions for tgt_data_buf_alloced flag
2728 static inline int scst_cmd_get_tgt_data_buff_alloced(struct scst_cmd *cmd)
2730 return cmd->tgt_data_buf_alloced;
2733 static inline void scst_cmd_set_tgt_data_buff_alloced(struct scst_cmd *cmd)
2735 cmd->tgt_data_buf_alloced = 1;
2739 * Get/Set functions for dh_data_buf_alloced flag
2741 static inline int scst_cmd_get_dh_data_buff_alloced(struct scst_cmd *cmd)
2743 return cmd->dh_data_buf_alloced;
2746 static inline void scst_cmd_set_dh_data_buff_alloced(struct scst_cmd *cmd)
2748 cmd->dh_data_buf_alloced = 1;
2752 * Get/Set functions for no_sgv flag
2754 static inline int scst_cmd_get_no_sgv(struct scst_cmd *cmd)
2759 static inline void scst_cmd_set_no_sgv(struct scst_cmd *cmd)
2765 * Get/Set functions for tgt_sn
2767 static inline int scst_cmd_get_tgt_sn(struct scst_cmd *cmd)
2769 BUG_ON(!cmd->tgt_sn_set);
2773 static inline void scst_cmd_set_tgt_sn(struct scst_cmd *cmd, uint32_t tgt_sn)
2775 cmd->tgt_sn_set = 1;
2776 cmd->tgt_sn = tgt_sn;
2780 * Returns 1 if the cmd was aborted, so its status is invalid and no
2781 * reply shall be sent to the remote initiator. A target driver should
2782 * only clear internal resources, associated with cmd.
2784 static inline int scst_cmd_aborted(struct scst_cmd *cmd)
2786 return test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags) &&
2787 !test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
2790 /* Returns sense data format for cmd's dev */
2791 static inline bool scst_get_cmd_dev_d_sense(struct scst_cmd *cmd)
2793 return (cmd->dev != NULL) ? cmd->dev->d_sense : 0;
2797 * Get/Set functions for expected data direction, transfer length
2798 * and its validity flag
2800 static inline int scst_cmd_is_expected_set(struct scst_cmd *cmd)
2802 return cmd->expected_values_set;
2805 static inline scst_data_direction scst_cmd_get_expected_data_direction(
2806 struct scst_cmd *cmd)
2808 return cmd->expected_data_direction;
2811 static inline int scst_cmd_get_expected_transfer_len(
2812 struct scst_cmd *cmd)
2814 return cmd->expected_transfer_len;
2817 static inline int scst_cmd_get_expected_in_transfer_len(
2818 struct scst_cmd *cmd)
2820 return cmd->expected_in_transfer_len;
2823 static inline void scst_cmd_set_expected(struct scst_cmd *cmd,
2824 scst_data_direction expected_data_direction,
2825 int expected_transfer_len)
2827 cmd->expected_data_direction = expected_data_direction;
2828 cmd->expected_transfer_len = expected_transfer_len;
2829 cmd->expected_values_set = 1;
2832 static inline void scst_cmd_set_expected_in_transfer_len(struct scst_cmd *cmd,
2833 int expected_in_transfer_len)
2835 WARN_ON(!cmd->expected_values_set);
2836 cmd->expected_in_transfer_len = expected_in_transfer_len;
2840 * Get/clear functions for cmd's may_need_dma_sync
2842 static inline int scst_get_may_need_dma_sync(struct scst_cmd *cmd)
2844 return cmd->may_need_dma_sync;
2847 static inline void scst_clear_may_need_dma_sync(struct scst_cmd *cmd)
2849 cmd->may_need_dma_sync = 0;
2853 * Get/set functions for cmd's delivery_status. It is one of
2854 * SCST_CMD_DELIVERY_* constants. It specifies the status of the
2855 * command's delivery to initiator.
2857 static inline int scst_get_delivery_status(struct scst_cmd *cmd)
2859 return cmd->delivery_status;
2862 static inline void scst_set_delivery_status(struct scst_cmd *cmd,
2863 int delivery_status)
2865 cmd->delivery_status = delivery_status;
2869 * Get/Set function for mgmt cmd's target private data
2871 static inline void *scst_mgmt_cmd_get_tgt_priv(struct scst_mgmt_cmd *mcmd)
2873 return mcmd->tgt_priv;
2876 static inline void scst_mgmt_cmd_set_tgt_priv(struct scst_mgmt_cmd *mcmd,
2879 mcmd->tgt_priv = val;
2882 /* Returns mgmt cmd's completition status (SCST_MGMT_STATUS_* constants) */
2883 static inline int scst_mgmt_cmd_get_status(struct scst_mgmt_cmd *mcmd)
2885 return mcmd->status;
2888 /* Returns mgmt cmd's TM fn */
2889 static inline int scst_mgmt_cmd_get_fn(struct scst_mgmt_cmd *mcmd)
2895 * Called by dev handler's task_mgmt_fn() to notify SCST core that mcmd
2896 * is going to complete asynchronously.
2898 void scst_prepare_async_mcmd(struct scst_mgmt_cmd *mcmd);
2901 * Called by dev handler to notify SCST core that async. mcmd is completed
2902 * with status "status".
2904 void scst_async_mcmd_completed(struct scst_mgmt_cmd *mcmd, int status);
2906 /* Returns AEN's fn */
2907 static inline int scst_aen_get_event_fn(struct scst_aen *aen)
2909 return aen->event_fn;
2912 /* Returns AEN's session */
2913 static inline struct scst_session *scst_aen_get_sess(struct scst_aen *aen)
2918 /* Returns AEN's LUN */
2919 static inline uint64_t scst_aen_get_lun(struct scst_aen *aen)
2924 /* Returns SCSI AEN's sense */
2925 static inline const uint8_t *scst_aen_get_sense(struct scst_aen *aen)
2927 return aen->aen_sense;
2930 /* Returns SCSI AEN's sense length */
2931 static inline int scst_aen_get_sense_len(struct scst_aen *aen)
2933 return aen->aen_sense_len;
2937 * Get/set functions for AEN's delivery_status. It is one of
2938 * SCST_AEN_RES_* constants. It specifies the status of the
2939 * command's delivery to initiator.
2941 static inline int scst_get_aen_delivery_status(struct scst_aen *aen)
2943 return aen->delivery_status;
2946 static inline void scst_set_aen_delivery_status(struct scst_aen *aen,
2949 aen->delivery_status = status;
2953 * Notifies SCST that the driver has sent the AEN and it
2954 * can be freed now. Don't forget to set the delivery status, if it
2955 * isn't success, using scst_set_aen_delivery_status() before calling
2958 void scst_aen_done(struct scst_aen *aen);
2960 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2963 * The macro's sg_page(), sg_virt(), sg_init_table(), sg_assign_page() and
2964 * sg_set_page() have been introduced in the 2.6.24 kernel. The definitions
2965 * below are backports of the 2.6.24 macro's for older kernels. There is one
2966 * exception however: when compiling SCST on a system with a pre-2.6.24 kernel
2967 * (e.g. RHEL 5.x) where the OFED kernel headers have been installed, do not
2968 * define the backported macro's because OFED has already defined these.
2971 #ifndef __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__
2973 static inline struct page *sg_page(struct scatterlist *sg)
2978 static inline void *sg_virt(struct scatterlist *sg)
2980 return page_address(sg_page(sg)) + sg->offset;
2983 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
2985 memset(sgl, 0, sizeof(*sgl) * nents);
2988 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
2993 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
2994 unsigned int len, unsigned int offset)
2996 sg_assign_page(sg, page);
2997 sg->offset = offset;
3001 static inline struct scatterlist *sg_next(struct scatterlist *sg)
3007 #endif /* __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__ */
3009 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
3011 static inline void sg_clear(struct scatterlist *sg)
3013 memset(sg, 0, sizeof(*sg));
3014 #ifdef CONFIG_DEBUG_SG
3015 sg->sg_magic = SG_MAGIC;
3019 enum scst_sg_copy_dir {
3020 SCST_SG_COPY_FROM_TARGET,
3021 SCST_SG_COPY_TO_TARGET
3025 * Copies data between cmd->tgt_sg and cmd->sg in direction defined by
3026 * copy_dir parameter.
3028 void scst_copy_sg(struct scst_cmd *cmd, enum scst_sg_copy_dir copy_dir);
3031 * Functions for access to the commands data (SG) buffer,
3032 * including HIGHMEM environment. Should be used instead of direct
3033 * access. Returns the mapped buffer length for success, 0 for EOD,
3034 * negative error code otherwise.
3036 * "Buf" argument returns the mapped buffer
3038 * The "put" function unmaps the buffer.
3040 static inline int __scst_get_buf(struct scst_cmd *cmd, struct scatterlist *sg,
3041 int sg_cnt, uint8_t **buf)
3044 int i = cmd->get_sg_buf_entry_num;
3048 if ((i >= sg_cnt) || unlikely(sg == NULL))
3051 *buf = page_address(sg_page(&sg[i]));
3052 *buf += sg[i].offset;
3055 cmd->get_sg_buf_entry_num++;
3061 static inline int scst_get_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3063 cmd->get_sg_buf_entry_num = 0;
3064 cmd->may_need_dma_sync = 1;
3065 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3068 static inline int scst_get_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3070 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3073 static inline void scst_put_buf(struct scst_cmd *cmd, void *buf)
3078 static inline int scst_get_in_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3080 cmd->get_sg_buf_entry_num = 0;
3081 cmd->may_need_dma_sync = 1;
3082 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3085 static inline int scst_get_in_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3087 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3090 static inline void scst_put_in_buf(struct scst_cmd *cmd, void *buf)
3096 * Returns approximate higher rounded buffers count that
3097 * scst_get_buf_[first|next]() return.
3099 static inline int scst_get_buf_count(struct scst_cmd *cmd)
3101 return (cmd->sg_cnt == 0) ? 1 : cmd->sg_cnt;
3105 * Returns approximate higher rounded buffers count that
3106 * scst_get_in_buf_[first|next]() return.
3108 static inline int scst_get_in_buf_count(struct scst_cmd *cmd)
3110 return (cmd->in_sg_cnt == 0) ? 1 : cmd->in_sg_cnt;
3113 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23) && !defined(BACKPORT_LINUX_WORKQUEUE_TO_2_6_19)
3114 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
3115 static inline int cancel_delayed_work_sync(struct delayed_work *work)
3117 static inline int cancel_delayed_work_sync(struct work_struct *work)
3122 res = cancel_delayed_work(work);
3123 flush_scheduled_work();
3129 * Suspends and resumes any activity.
3130 * Function scst_suspend_activity() doesn't return 0, until there are any
3131 * active commands (state after SCST_CMD_STATE_INIT). If "interruptible"
3132 * is true, it returns after SCST_SUSPENDING_TIMEOUT or if it was interrupted
3133 * by a signal with the coresponding error status < 0. If "interruptible"
3134 * is false, it will wait virtually forever.
3136 * New arriving commands stay in that state until scst_resume_activity()
3139 int scst_suspend_activity(bool interruptible);
3140 void scst_resume_activity(void);
3143 * Main SCST commands processing routing. Must be used only by dev handlers.
3144 * Argument atomic is true if function called in atomic context.
3146 void scst_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3149 * SCST commands processing routine, which should be called by dev handler
3150 * after its parse() callback returned SCST_CMD_STATE_STOP. Arguments
3151 * the same as for scst_process_active_cmd().
3153 void scst_post_parse_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3156 * Checks if command can be executed (reservations, etc.) or there are local
3157 * events, like pending UAs. Returns < 0 if command must be aborted, > 0 if
3158 * there is an event and command should be immediately completed, or 0
3161 * !! Dev handlers implementing exec() callback must call this function there !!
3162 * !! just before the actual command's execution !!
3164 int scst_check_local_events(struct scst_cmd *cmd);
3167 * Returns the next state of the SCSI target state machine in case if command's
3168 * completed abnormally.
3170 int scst_get_cmd_abnormal_done_state(const struct scst_cmd *cmd);
3173 * Sets state of the SCSI target state machine in case if command's completed
3176 void scst_set_cmd_abnormal_done_state(struct scst_cmd *cmd);
3178 struct scst_trace_log {
3183 #ifdef CONFIG_SCST_PROC
3186 * Returns target driver's root entry in SCST's /proc hierarchy.
3187 * The driver can create own files/directories here, which should
3188 * be deleted in the driver's release().
3190 struct proc_dir_entry *scst_proc_get_tgt_root(
3191 struct scst_tgt_template *vtt);
3194 * Returns device handler's root entry in SCST's /proc hierarchy.
3195 * The driver can create own files/directories here, which should
3196 * be deleted in the driver's detach()/release().
3198 struct proc_dir_entry *scst_proc_get_dev_type_root(
3199 struct scst_dev_type *dtt);
3202 ** Two library functions and the structure to help the drivers
3203 ** that use scst_debug.* facilities manage "trace_level" /proc entry.
3204 ** The functions service "standard" log levels and allow to work
3205 ** with driver specific levels, which should be passed inside as
3206 ** NULL-terminated array of struct scst_trace_log's, where:
3207 ** - val - the level's numeric value
3208 ** - token - its string representation
3211 int scst_proc_log_entry_read(struct seq_file *seq, unsigned long log_level,
3212 const struct scst_trace_log *tbl);
3213 int scst_proc_log_entry_write(struct file *file, const char __user *buf,
3214 unsigned long length, unsigned long *log_level,
3215 unsigned long default_level, const struct scst_trace_log *tbl);
3218 * helper data structure and function to create proc entry.
3220 struct scst_proc_data {
3221 const struct file_operations seq_op;
3222 int (*show)(struct seq_file *, void *);
3226 int scst_single_seq_open(struct inode *inode, struct file *file);
3228 struct proc_dir_entry *scst_create_proc_entry(struct proc_dir_entry *root,
3229 const char *name, struct scst_proc_data *pdata);
3231 #define SCST_DEF_RW_SEQ_OP(x) \
3233 .owner = THIS_MODULE, \
3234 .open = scst_single_seq_open,\
3237 .llseek = seq_lseek, \
3238 .release = single_release, \
3241 #else /* CONFIG_SCST_PROC */
3244 * Returns target driver's root sysfs kobject.
3245 * The driver can create own files/directories/links here.
3247 static inline struct kobject *scst_sysfs_get_tgtt_kobj(
3248 struct scst_tgt_template *tgtt)
3250 return &tgtt->tgtt_kobj;
3254 * Returns target's root sysfs kobject.
3255 * The driver can create own files/directories/links here.
3257 static inline struct kobject *scst_sysfs_get_tgt_kobj(
3258 struct scst_tgt *tgt)
3260 return &tgt->tgt_kobj;
3264 * Returns device handler's root sysfs kobject.
3265 * The driver can create own files/directories/links here.
3267 static inline struct kobject *scst_sysfs_get_devt_kobj(
3268 struct scst_dev_type *devt)
3270 return &devt->devt_kobj;
3274 * Returns device's root sysfs kobject.
3275 * The driver can create own files/directories/links here.
3277 static inline struct kobject *scst_sysfs_get_dev_kobj(
3278 struct scst_device *dev)
3280 return &dev->dev_kobj;
3283 #endif /* CONFIG_SCST_PROC */
3285 /* Returns target name */
3286 static inline const char *scst_get_tgt_name(const struct scst_tgt *tgt)
3288 return tgt->tgt_name;
3292 * Adds and deletes (stops) num of global SCST's threads. Returns 0 on
3293 * success, error code otherwise.
3295 int scst_add_global_threads(int num);
3296 void scst_del_global_threads(int num);
3298 int scst_alloc_sense(struct scst_cmd *cmd, int atomic);
3299 int scst_alloc_set_sense(struct scst_cmd *cmd, int atomic,
3300 const uint8_t *sense, unsigned int len);
3303 * Sets the corresponding field in the sense buffer taking sense type
3304 * into account. Returns resulting sense length.
3306 int scst_set_sense(uint8_t *buffer, int len, bool d_sense,
3307 int key, int asc, int ascq);
3310 * Returns true if the sense is valid and carrying a Unit Attention or
3313 bool scst_is_ua_sense(const uint8_t *sense, int len);
3316 * Returnes true if sense matches to (key, asc, ascq) and false otherwise.
3317 * Valid_mask is one or several SCST_SENSE_*_VALID constants setting valid
3318 * (key, asc, ascq) values.
3320 bool scst_analyze_sense(const uint8_t *sense, int len,
3321 unsigned int valid_mask, int key, int asc, int ascq);
3324 * Returnes a pseudo-random number for debugging purposes. Available only in
3327 unsigned long scst_random(void);
3330 * Sets response data length for cmd and truncates its SG vector accordingly.
3331 * The cmd->resp_data_len must not be set directly, it must be set only
3332 * using this function. Value of resp_data_len must be <= cmd->bufflen.
3334 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len);
3337 * Get/put global ref counter that prevents from entering into suspended
3338 * activities stage, so protects from any global management operations.
3340 void scst_get(void);
3341 void scst_put(void);
3346 void scst_cmd_get(struct scst_cmd *cmd);
3347 void scst_cmd_put(struct scst_cmd *cmd);
3350 * Allocates and returns pointer to SG vector with data size "size".
3351 * In *count returned the count of entries in the vector.
3352 * Returns NULL for failure.
3354 struct scatterlist *scst_alloc(int size, gfp_t gfp_mask, int *count);
3356 /* Frees SG vector returned by scst_alloc() */
3357 void scst_free(struct scatterlist *sg, int count);
3360 * Adds local to the current thread data to tgt_dev
3361 * (they will be local for the tgt_dev and current thread).
3363 void scst_add_thr_data(struct scst_tgt_dev *tgt_dev,
3364 struct scst_thr_data_hdr *data,
3365 void (*free_fn) (struct scst_thr_data_hdr *data));
3367 /* Deletes all local to threads data from tgt_dev */
3368 void scst_del_all_thr_data(struct scst_tgt_dev *tgt_dev);
3370 /* Deletes all local to threads data from all tgt_dev's of the dev */
3371 void scst_dev_del_all_thr_data(struct scst_device *dev);
3373 /* Finds local to the thread data. Returns NULL, if they not found. */
3374 struct scst_thr_data_hdr *__scst_find_thr_data(struct scst_tgt_dev *tgt_dev,
3375 struct task_struct *tsk);
3377 /* Finds local to the current thread data. Returns NULL, if they not found. */
3378 static inline struct scst_thr_data_hdr *scst_find_thr_data(
3379 struct scst_tgt_dev *tgt_dev)
3381 return __scst_find_thr_data(tgt_dev, current);
3384 static inline void scst_thr_data_get(struct scst_thr_data_hdr *data)
3386 atomic_inc(&data->ref);
3389 static inline void scst_thr_data_put(struct scst_thr_data_hdr *data)
3391 if (atomic_dec_and_test(&data->ref))
3392 data->free_fn(data);
3396 ** Generic parse() support routines.
3397 ** Done via pointer on functions to avoid unneeded dereferences on
3401 /* Calculates and returns block shift for the given sector size */
3402 int scst_calc_block_shift(int sector_size);
3404 /* Generic parse() for SBC (disk) devices */
3405 int scst_sbc_generic_parse(struct scst_cmd *cmd,
3406 int (*get_block_shift)(struct scst_cmd *cmd));
3408 /* Generic parse() for MMC (cdrom) devices */
3409 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
3410 int (*get_block_shift)(struct scst_cmd *cmd));
3412 /* Generic parse() for MO disk devices */
3413 int scst_modisk_generic_parse(struct scst_cmd *cmd,
3414 int (*get_block_shift)(struct scst_cmd *cmd));
3416 /* Generic parse() for tape devices */
3417 int scst_tape_generic_parse(struct scst_cmd *cmd,
3418 int (*get_block_size)(struct scst_cmd *cmd));
3420 /* Generic parse() for changer devices */
3421 int scst_changer_generic_parse(struct scst_cmd *cmd,
3422 int (*nothing)(struct scst_cmd *cmd));
3424 /* Generic parse() for "processor" devices */
3425 int scst_processor_generic_parse(struct scst_cmd *cmd,
3426 int (*nothing)(struct scst_cmd *cmd));
3428 /* Generic parse() for RAID devices */
3429 int scst_raid_generic_parse(struct scst_cmd *cmd,
3430 int (*nothing)(struct scst_cmd *cmd));
3433 ** Generic dev_done() support routines.
3434 ** Done via pointer on functions to avoid unneeded dereferences on
3438 /* Generic dev_done() for block devices */
3439 int scst_block_generic_dev_done(struct scst_cmd *cmd,
3440 void (*set_block_shift)(struct scst_cmd *cmd, int block_shift));
3442 /* Generic dev_done() for tape devices */
3443 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
3444 void (*set_block_size)(struct scst_cmd *cmd, int block_size));
3447 * Issues a MODE SENSE for control mode page data and sets the corresponding
3448 * dev's parameter from it. Returns 0 on success and not 0 otherwise.
3450 int scst_obtain_device_parameters(struct scst_device *dev);
3453 * Has to be put here open coded, because Linux doesn't have equivalent, which
3454 * allows exclusive wake ups of threads in LIFO order. We need it to let (yet)
3455 * unneeded threads sleep and not pollute CPU cache by their stacks.
3457 static inline void add_wait_queue_exclusive_head(wait_queue_head_t *q,
3460 unsigned long flags;
3462 wait->flags |= WQ_FLAG_EXCLUSIVE;
3463 spin_lock_irqsave(&q->lock, flags);
3464 __add_wait_queue(q, wait);
3465 spin_unlock_irqrestore(&q->lock, flags);
3468 #endif /* __SCST_H */