4 * Copyright (C) 2004 - 2009 Vladislav Bolkhovitin <vst@vlnb.net>
5 * Copyright (C) 2004 - 2005 Leonid Stoljar
6 * Copyright (C) 2007 - 2009 ID7 Ltd.
8 * Main SCSI target mid-level include file.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation, version 2
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
24 #include <linux/types.h>
25 #include <linux/version.h>
26 #include <linux/blkdev.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
30 #define CONFIG_SCST_PROC
32 #ifdef CONFIG_SCST_PROC
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi.h>
42 #include <scst_const.h>
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
45 #ifndef RHEL_RELEASE_CODE
55 * Version numbers, the same as for the kernel.
57 * Changing it don't forget to change SCST_FIO_REV in scst_vdisk.c
58 * and FIO_REV in usr/fileio/common.h as well.
60 #define SCST_VERSION(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + d)
61 #define SCST_VERSION_CODE SCST_VERSION(2, 0, 0, 0)
62 #ifdef CONFIG_SCST_PROC
63 #define SCST_VERSION_STRING_SUFFIX "-procfs"
65 #define SCST_VERSION_STRING_SUFFIX
67 #define SCST_VERSION_STRING "2.0.0-pre1" SCST_VERSION_STRING_SUFFIX
68 #define SCST_INTERFACE_VERSION \
69 SCST_VERSION_STRING "$Revision$" SCST_CONST_VERSION
71 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
72 #define COMPLETION_INITIALIZER_ONSTACK(work) \
73 ({ init_completion(&work); work; })
76 * Lockdep needs to run a non-constant initializer for on-stack
77 * completions - so we use the _ONSTACK() variant for those that
78 * are on the kernel stack:
81 # define DECLARE_COMPLETION_ONSTACK(work) \
82 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
84 # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
88 * list_is_last - tests whether @list is the last entry in list @head
89 * @list: the entry to test
90 * @head: the head of the list
92 static inline int list_is_last(const struct list_head *list,
93 const struct list_head *head)
95 return list->next == head;
100 #define SCST_LOCAL_NAME "scst_lcl_drvr"
102 /*************************************************************
103 ** States of command processing state machine. At first,
104 ** "active" states, then - "passive" ones. This is to have
105 ** more efficient generated code of the corresponding
106 ** "switch" statements.
107 *************************************************************/
109 /* Internal parsing */
110 #define SCST_CMD_STATE_PRE_PARSE 0
112 /* Dev handler's parse() is going to be called */
113 #define SCST_CMD_STATE_DEV_PARSE 1
115 /* Allocation of the cmd's data buffer */
116 #define SCST_CMD_STATE_PREPARE_SPACE 2
118 /* Target driver's rdy_to_xfer() is going to be called */
119 #define SCST_CMD_STATE_RDY_TO_XFER 3
121 /* Target driver's pre_exec() is going to be called */
122 #define SCST_CMD_STATE_TGT_PRE_EXEC 4
124 /* Cmd is going to be sent for execution */
125 #define SCST_CMD_STATE_SEND_FOR_EXEC 5
127 /* Cmd is being checked if it should be executed locally */
128 #define SCST_CMD_STATE_LOCAL_EXEC 6
130 /* Cmd is ready for execution */
131 #define SCST_CMD_STATE_REAL_EXEC 7
133 /* Internal post-exec checks */
134 #define SCST_CMD_STATE_PRE_DEV_DONE 8
136 /* Internal MODE SELECT pages related checks */
137 #define SCST_CMD_STATE_MODE_SELECT_CHECKS 9
139 /* Dev handler's dev_done() is going to be called */
140 #define SCST_CMD_STATE_DEV_DONE 10
142 /* Target driver's xmit_response() is going to be called */
143 #define SCST_CMD_STATE_PRE_XMIT_RESP 11
145 /* Target driver's xmit_response() is going to be called */
146 #define SCST_CMD_STATE_XMIT_RESP 12
149 #define SCST_CMD_STATE_FINISHED 13
151 /* Internal cmd finished */
152 #define SCST_CMD_STATE_FINISHED_INTERNAL 14
154 #define SCST_CMD_STATE_LAST_ACTIVE (SCST_CMD_STATE_FINISHED_INTERNAL+100)
156 /* A cmd is created, but scst_cmd_init_done() not called */
157 #define SCST_CMD_STATE_INIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+1)
159 /* LUN translation (cmd->tgt_dev assignment) */
160 #define SCST_CMD_STATE_INIT (SCST_CMD_STATE_LAST_ACTIVE+2)
162 /* Allocation of the cmd's data buffer */
163 #define SCST_CMD_STATE_PREPROCESS_DONE (SCST_CMD_STATE_LAST_ACTIVE+3)
165 /* Waiting for data from the initiator (until scst_rx_data() called) */
166 #define SCST_CMD_STATE_DATA_WAIT (SCST_CMD_STATE_LAST_ACTIVE+4)
168 /* Waiting for CDB's execution finish */
169 #define SCST_CMD_STATE_REAL_EXECUTING (SCST_CMD_STATE_LAST_ACTIVE+5)
171 /* Waiting for response's transmission finish */
172 #define SCST_CMD_STATE_XMIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+6)
174 /*************************************************************
175 * Can be retuned instead of cmd's state by dev handlers'
176 * functions, if the command's state should be set by default
177 *************************************************************/
178 #define SCST_CMD_STATE_DEFAULT 500
180 /*************************************************************
181 * Can be retuned instead of cmd's state by dev handlers'
182 * functions, if it is impossible to complete requested
183 * task in atomic context. The cmd will be restarted in thread
185 *************************************************************/
186 #define SCST_CMD_STATE_NEED_THREAD_CTX 1000
188 /*************************************************************
189 * Can be retuned instead of cmd's state by dev handlers'
190 * parse function, if the cmd processing should be stopped
191 * for now. The cmd will be restarted by dev handlers itself.
192 *************************************************************/
193 #define SCST_CMD_STATE_STOP 1001
195 /*************************************************************
196 ** States of mgmt command processing state machine
197 *************************************************************/
199 /* LUN translation (mcmd->tgt_dev assignment) */
200 #define SCST_MCMD_STATE_INIT 0
202 /* Mgmt cmd is ready for processing */
203 #define SCST_MCMD_STATE_READY 1
205 /* Mgmt cmd is being executing */
206 #define SCST_MCMD_STATE_EXECUTING 2
208 /* Post check when affected commands done */
209 #define SCST_MCMD_STATE_POST_AFFECTED_CMDS_DONE 3
211 /* Target driver's task_mgmt_fn_done() is going to be called */
212 #define SCST_MCMD_STATE_DONE 4
214 /* The mcmd finished */
215 #define SCST_MCMD_STATE_FINISHED 5
217 /*************************************************************
218 ** Constants for "atomic" parameter of SCST's functions
219 *************************************************************/
220 #define SCST_NON_ATOMIC 0
221 #define SCST_ATOMIC 1
223 /*************************************************************
224 ** Values for pref_context parameter of scst_cmd_init_done(),
225 ** scst_rx_data(), scst_restart_cmd(), scst_tgt_cmd_done()
226 ** and scst_cmd_done()
227 *************************************************************/
229 enum scst_exec_context {
231 * Direct cmd's processing (i.e. regular function calls in the current
232 * context) sleeping is not allowed
234 SCST_CONTEXT_DIRECT_ATOMIC,
237 * Direct cmd's processing (i.e. regular function calls in the current
238 * context), sleeping is allowed, no restrictions
242 /* Tasklet or thread context required for cmd's processing */
243 SCST_CONTEXT_TASKLET,
245 /* Thread context required for cmd's processing */
249 * Context is the same as it was in previous call of the corresponding
250 * callback. For example, if dev handler's exec() does sync. data
251 * reading this value should be used for scst_cmd_done(). The same is
252 * true if scst_tgt_cmd_done() called directly from target driver's
253 * xmit_response(). Not allowed in scst_cmd_init_done() and
254 * scst_cmd_init_stage1_done().
259 /*************************************************************
260 ** Values for status parameter of scst_rx_data()
261 *************************************************************/
264 #define SCST_RX_STATUS_SUCCESS 0
267 * Data receiving finished with error, so set the sense and
268 * finish the command, including xmit_response() call
270 #define SCST_RX_STATUS_ERROR 1
273 * Data receiving finished with error and the sense is set,
274 * so finish the command, including xmit_response() call
276 #define SCST_RX_STATUS_ERROR_SENSE_SET 2
279 * Data receiving finished with fatal error, so finish the command,
280 * but don't call xmit_response()
282 #define SCST_RX_STATUS_ERROR_FATAL 3
284 /*************************************************************
285 ** Values for status parameter of scst_restart_cmd()
286 *************************************************************/
289 #define SCST_PREPROCESS_STATUS_SUCCESS 0
292 * Command's processing finished with error, so set the sense and
293 * finish the command, including xmit_response() call
295 #define SCST_PREPROCESS_STATUS_ERROR 1
298 * Command's processing finished with error and the sense is set,
299 * so finish the command, including xmit_response() call
301 #define SCST_PREPROCESS_STATUS_ERROR_SENSE_SET 2
304 * Command's processing finished with fatal error, so finish the command,
305 * but don't call xmit_response()
307 #define SCST_PREPROCESS_STATUS_ERROR_FATAL 3
309 /* Thread context requested */
310 #define SCST_PREPROCESS_STATUS_NEED_THREAD 4
312 /*************************************************************
313 ** Values for AEN functions
314 *************************************************************/
317 * SCSI Asynchronous Event. Parameter contains SCSI sense
318 * (Unit Attention). AENs generated only for 2 the following UAs:
319 * CAPACITY DATA HAS CHANGED and REPORTED LUNS DATA HAS CHANGED.
320 * Other UAs reported regularly as CHECK CONDITION status,
321 * because it doesn't look safe to report them using AENs, since
322 * reporting using AENs opens delivery race windows even in case of
325 #define SCST_AEN_SCSI 0
327 /*************************************************************
328 ** Allowed return/status codes for report_aen() callback and
329 ** scst_set_aen_delivery_status() function
330 *************************************************************/
333 #define SCST_AEN_RES_SUCCESS 0
336 #define SCST_AEN_RES_NOT_SUPPORTED -1
339 #define SCST_AEN_RES_FAILED -2
341 /*************************************************************
342 ** Allowed return codes for xmit_response(), rdy_to_xfer()
343 *************************************************************/
346 #define SCST_TGT_RES_SUCCESS 0
348 /* Internal device queue is full, retry again later */
349 #define SCST_TGT_RES_QUEUE_FULL -1
352 * It is impossible to complete requested task in atomic context.
353 * The cmd will be restarted in thread context.
355 #define SCST_TGT_RES_NEED_THREAD_CTX -2
358 * Fatal error, if returned by xmit_response() the cmd will
359 * be destroyed, if by any other function, xmit_response()
360 * will be called with HARDWARE ERROR sense data
362 #define SCST_TGT_RES_FATAL_ERROR -3
364 /*************************************************************
365 ** Allowed return codes for dev handler's exec()
366 *************************************************************/
368 /* The cmd is done, go to other ones */
369 #define SCST_EXEC_COMPLETED 0
371 /* The cmd should be sent to SCSI mid-level */
372 #define SCST_EXEC_NOT_COMPLETED 1
375 * Thread context is required to execute the command.
376 * Exec() will be called again in the thread context.
378 #define SCST_EXEC_NEED_THREAD 2
381 * Set if cmd is finished and there is status/sense to be sent.
382 * The status should be not sent (i.e. the flag not set) if the
383 * possibility to perform a command in "chunks" (i.e. with multiple
384 * xmit_response()/rdy_to_xfer()) is used (not implemented yet).
385 * Obsolete, use scst_cmd_get_is_send_status() instead.
387 #define SCST_TSC_FLAG_STATUS 0x2
389 /*************************************************************
390 ** Additional return code for dev handler's task_mgmt_fn()
391 *************************************************************/
393 /* Regular standard actions for the command should be done */
394 #define SCST_DEV_TM_NOT_COMPLETED 1
396 /*************************************************************
397 ** Session initialization phases
398 *************************************************************/
400 /* Set if session is being initialized */
401 #define SCST_SESS_IPH_INITING 0
403 /* Set if the session is successfully initialized */
404 #define SCST_SESS_IPH_SUCCESS 1
406 /* Set if the session initialization failed */
407 #define SCST_SESS_IPH_FAILED 2
409 /* Set if session is initialized and ready */
410 #define SCST_SESS_IPH_READY 3
412 /*************************************************************
413 ** Session shutdown phases
414 *************************************************************/
416 /* Set if session is initialized and ready */
417 #define SCST_SESS_SPH_READY 0
419 /* Set if session is shutting down */
420 #define SCST_SESS_SPH_SHUTDOWN 1
422 /*************************************************************
423 ** Session's async (atomic) flags
424 *************************************************************/
426 /* Set if the sess's hw pending work is scheduled */
427 #define SCST_SESS_HW_PENDING_WORK_SCHEDULED 0
429 /*************************************************************
430 ** Cmd's async (atomic) flags
431 *************************************************************/
433 /* Set if the cmd is aborted and ABORTED sense will be sent as the result */
434 #define SCST_CMD_ABORTED 0
436 /* Set if the cmd is aborted by other initiator */
437 #define SCST_CMD_ABORTED_OTHER 1
439 /* Set if the cmd is aborted and counted in cmd_done_wait_count */
440 #define SCST_CMD_DONE_COUNTED 2
442 /* Set if no response should be sent to the target about this cmd */
443 #define SCST_CMD_NO_RESP 3
445 /* Set if the cmd is dead and can be destroyed at any time */
446 #define SCST_CMD_CAN_BE_DESTROYED 4
448 /*************************************************************
449 ** Tgt_dev's async. flags (tgt_dev_flags)
450 *************************************************************/
452 /* Set if tgt_dev has Unit Attention sense */
453 #define SCST_TGT_DEV_UA_PENDING 0
455 /* Set if tgt_dev is RESERVED by another session */
456 #define SCST_TGT_DEV_RESERVED 1
458 /* Set if the corresponding context is atomic */
459 #define SCST_TGT_DEV_AFTER_INIT_WR_ATOMIC 5
460 #define SCST_TGT_DEV_AFTER_INIT_OTH_ATOMIC 6
461 #define SCST_TGT_DEV_AFTER_RESTART_WR_ATOMIC 7
462 #define SCST_TGT_DEV_AFTER_RESTART_OTH_ATOMIC 8
463 #define SCST_TGT_DEV_AFTER_RX_DATA_ATOMIC 9
464 #define SCST_TGT_DEV_AFTER_EXEC_ATOMIC 10
466 #define SCST_TGT_DEV_CLUST_POOL 11
468 #ifdef CONFIG_SCST_PROC
470 /*************************************************************
471 ** Name of the entry in /proc
472 *************************************************************/
473 #define SCST_PROC_ENTRY_NAME "scsi_tgt"
477 /*************************************************************
478 ** Activities suspending timeout
479 *************************************************************/
480 #define SCST_SUSPENDING_TIMEOUT (90 * HZ)
482 /*************************************************************
483 ** Kernel cache creation helper
484 *************************************************************/
486 #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
487 sizeof(struct __struct), __alignof__(struct __struct),\
488 (__flags), NULL, NULL)
491 /*************************************************************
492 ** Vlaid_mask constants for scst_analyze_sense()
493 *************************************************************/
495 #define SCST_SENSE_KEY_VALID 1
496 #define SCST_SENSE_ASC_VALID 2
497 #define SCST_SENSE_ASCQ_VALID 4
499 #define SCST_SENSE_ASCx_VALID (SCST_SENSE_ASC_VALID | \
500 SCST_SENSE_ASCQ_VALID)
502 #define SCST_SENSE_ALL_VALID (SCST_SENSE_KEY_VALID | \
503 SCST_SENSE_ASC_VALID | \
504 SCST_SENSE_ASCQ_VALID)
506 /*************************************************************
508 *************************************************************/
513 struct scst_mgmt_cmd;
516 struct scst_dev_type;
523 * SCST uses 64-bit numbers to represent LUN's internally. The value
524 * NO_SUCH_LUN is guaranteed to be different of every valid LUN.
526 #define NO_SUCH_LUN ((uint64_t)-1)
528 typedef enum dma_data_direction scst_data_direction;
530 enum scst_cdb_flags {
531 /* SCST_TRANSFER_LEN_TYPE_FIXED must be equiv 1 (FIXED_BIT in cdb) */
532 SCST_TRANSFER_LEN_TYPE_FIXED = 0x001,
533 SCST_SMALL_TIMEOUT = 0x002,
534 SCST_LONG_TIMEOUT = 0x004,
535 SCST_UNKNOWN_LENGTH = 0x008,
536 SCST_INFO_NOT_FOUND = 0x010, /* must be single bit */
537 SCST_VERIFY_BYTCHK_MISMATCH_ALLOWED = 0x020,
538 SCST_IMPLICIT_HQ = 0x040,
539 SCST_SKIP_UA = 0x080,
540 SCST_WRITE_MEDIUM = 0x100,
541 SCST_LOCAL_CMD = 0x200,
542 SCST_FULLY_LOCAL_CMD = 0x400,
546 * Scsi_Target_Template: defines what functions a target driver will
547 * have to provide in order to work with the target mid-level.
548 * MUST HAVEs define functions that are expected to be in order to work.
549 * OPTIONAL says that there is a choice.
551 * Also, pay attention to the fact that a command is BLOCKING or NON-BLOCKING.
552 * NON-BLOCKING means that a function returns immediately and will not wait
553 * for actual data transfer to finish. Blocking in such command could have
554 * negative impact on overall system performance. If blocking is necessary,
555 * it is worth to consider creating dedicated thread(s) in target driver, to
556 * which the commands would be passed and which would perform blocking
557 * operations instead of SCST.
559 * If the function allowed to sleep or not is determined by its last
560 * argument, which is true, if sleeping is not allowed. In this case,
561 * if the function requires sleeping, it can return
562 * SCST_TGT_RES_NEED_THREAD_CTX, and it will be recalled in the thread context,
563 * where sleeping is allowed.
565 struct scst_tgt_template {
569 * SG tablesize allows to check whether scatter/gather can be used
575 * True, if this target adapter uses unchecked DMA onto an ISA bus.
577 unsigned unchecked_isa_dma:1;
580 * True, if this target adapter can benefit from using SG-vector
581 * clustering (i.e. smaller number of segments).
583 unsigned use_clustering:1;
586 * True, if this target adapter doesn't support SG-vector clustering
588 unsigned no_clustering:1;
591 * True, if corresponding function supports execution in
592 * the atomic (non-sleeping) context
594 unsigned xmit_response_atomic:1;
595 unsigned rdy_to_xfer_atomic:1;
597 #ifdef CONFIG_SCST_PROC
598 /* True, if the template doesn't need the entry in /proc */
599 unsigned no_proc_entry:1;
603 * The maximum time in seconds cmd can stay inside the target
604 * hardware, i.e. after rdy_to_xfer() and xmit_response(), before
605 * on_hw_pending_cmd_timeout() will be called, if defined.
607 * In the current implementation a cmd will be aborted in time t
608 * max_hw_pending_time <= t < 2*max_hw_pending_time.
610 int max_hw_pending_time;
613 * This function is equivalent to the SCSI
614 * queuecommand. The target should transmit the response
615 * buffer and the status in the scst_cmd struct.
616 * The expectation is that this executing this command is NON-BLOCKING.
618 * After the response is actually transmitted, the target
619 * should call the scst_tgt_cmd_done() function of the
620 * mid-level, which will allow it to free up the command.
621 * Returns one of the SCST_TGT_RES_* constants.
623 * Pay attention to "atomic" attribute of the cmd, which can be get
624 * by scst_cmd_atomic(): it is true if the function called in the
625 * atomic (non-sleeping) context.
629 int (*xmit_response) (struct scst_cmd *cmd);
632 * This function informs the driver that data
633 * buffer corresponding to the said command have now been
634 * allocated and it is OK to receive data for this command.
635 * This function is necessary because a SCSI target does not
636 * have any control over the commands it receives. Most lower
637 * level protocols have a corresponding function which informs
638 * the initiator that buffers have been allocated e.g., XFER_
639 * RDY in Fibre Channel. After the data is actually received
640 * the low-level driver needs to call scst_rx_data() in order to
641 * continue processing this command.
642 * Returns one of the SCST_TGT_RES_* constants.
643 * This command is expected to be NON-BLOCKING.
645 * Pay attention to "atomic" attribute of the cmd, which can be get
646 * by scst_cmd_atomic(): it is true if the function called in the
647 * atomic (non-sleeping) context.
651 int (*rdy_to_xfer) (struct scst_cmd *cmd);
654 * Called if cmd stays inside the target hardware, i.e. after
655 * rdy_to_xfer() and xmit_response(), more than max_hw_pending_time
656 * time. The target driver supposed to cleanup this command and
657 * resume cmd's processing.
661 void (*on_hw_pending_cmd_timeout) (struct scst_cmd *cmd);
664 * Called to notify the driver that the command is about to be freed.
665 * Necessary, because for aborted commands xmit_response() could not
666 * be called. Could be called on IRQ context.
670 void (*on_free_cmd) (struct scst_cmd *cmd);
673 * This function allows target driver to handle data buffer
674 * allocations on its own.
676 * Target driver doesn't have to always allocate buffer in this
677 * function, but if it decide to do it, it must check that
678 * scst_cmd_get_data_buff_alloced() returns 0, otherwise to avoid
679 * double buffer allocation and memory leaks alloc_data_buf() shall
682 * Shall return 0 in case of success or < 0 (preferrably -ENOMEM)
683 * in case of error, or > 0 if the regular SCST allocation should be
684 * done. In case of returning successfully,
685 * scst_cmd->tgt_data_buf_alloced will be set by SCST.
687 * It is possible that both target driver and dev handler request own
688 * memory allocation. In this case, data will be memcpy() between
689 * buffers, where necessary.
691 * If allocation in atomic context - cf. scst_cmd_atomic() - is not
692 * desired or fails and consequently < 0 is returned, this function
693 * will be re-called in thread context.
695 * Please note that the driver will have to handle itself all relevant
696 * details such as scatterlist setup, highmem, freeing the allocated
701 int (*alloc_data_buf) (struct scst_cmd *cmd);
704 * This function informs the driver that data
705 * buffer corresponding to the said command have now been
706 * allocated and other preprocessing tasks have been done.
707 * A target driver could need to do some actions at this stage.
708 * After the target driver done the needed actions, it shall call
709 * scst_restart_cmd() in order to continue processing this command.
711 * Called only if the cmd is queued using scst_cmd_init_stage1_done()
712 * instead of scst_cmd_init_done().
714 * Returns void, the result is expected to be returned using
715 * scst_restart_cmd().
717 * This command is expected to be NON-BLOCKING.
719 * Pay attention to "atomic" attribute of the cmd, which can be get
720 * by scst_cmd_atomic(): it is true if the function called in the
721 * atomic (non-sleeping) context.
725 void (*preprocessing_done) (struct scst_cmd *cmd);
728 * This function informs the driver that the said command is about
731 * Returns one of the SCST_PREPROCESS_* constants.
733 * This command is expected to be NON-BLOCKING.
735 * Pay attention to "atomic" attribute of the cmd, which can be get
736 * by scst_cmd_atomic(): it is true if the function called in the
737 * atomic (non-sleeping) context.
741 int (*pre_exec) (struct scst_cmd *cmd);
744 * This function informs the driver that all affected by the
745 * corresponding task management function commands have beed completed.
746 * No return value expected.
748 * This function is expected to be NON-BLOCKING.
750 * Called without any locks held from a thread context.
754 void (*task_mgmt_affected_cmds_done) (struct scst_mgmt_cmd *mgmt_cmd);
757 * This function informs the driver that the corresponding task
758 * management function has been completed, i.e. all the corresponding
759 * commands completed and freed. No return value expected.
761 * This function is expected to be NON-BLOCKING.
763 * Called without any locks held from a thread context.
765 * MUST HAVE if the target supports task management.
767 void (*task_mgmt_fn_done) (struct scst_mgmt_cmd *mgmt_cmd);
770 * This function should detect the target adapters that
771 * are present in the system. The function should return a value
772 * >= 0 to signify the number of detected target adapters.
773 * A negative value should be returned whenever there is
778 int (*detect) (struct scst_tgt_template *tgt_template);
781 * This function should free up the resources allocated to the device.
782 * The function should return 0 to indicate successful release
783 * or a negative value if there are some issues with the release.
784 * In the current version the return value is ignored.
788 int (*release) (struct scst_tgt *tgt);
791 * This function is used for Asynchronous Event Notifications.
793 * Returns one of the SCST_AEN_RES_* constants.
794 * After AEN is sent, target driver must call scst_aen_done() and,
795 * optionally, scst_set_aen_delivery_status().
797 * This command is expected to be NON-BLOCKING, but can sleep.
799 * MUST HAVE, if low-level protocol supports AENs.
801 int (*report_aen) (struct scst_aen *aen);
803 #ifdef CONFIG_SCST_PROC
805 * Those functions can be used to export the driver's statistics and
806 * other infos to the world outside the kernel as well as to get some
807 * management commands from it.
811 int (*read_proc) (struct seq_file *seq, struct scst_tgt *tgt);
812 int (*write_proc) (char *buffer, char **start, off_t offset,
813 int length, int *eof, struct scst_tgt *tgt);
817 * This function allows to enable or disable particular target.
818 * A disabled target doesn't receive and process any SCSI commands.
820 * SHOULD HAVE to avoid race when there are connected initiators,
821 * while target not yet completed the initial configuration. In this
822 * case the too early connected initiators would see not those devices,
823 * which they intended to see.
825 ssize_t (*enable_tgt) (struct scst_tgt *tgt, const char *buffer,
829 * This function shows if particular target is enabled or not.
831 * SHOULD HAVE, see above why.
833 bool (*is_tgt_enabled) (struct scst_tgt *tgt);
836 * Name of the template. Must be unique to identify
837 * the template. MUST HAVE
839 const char name[SCST_MAX_NAME];
842 * Number of additional threads to the pool of dedicated threads.
843 * Used if xmit_response() or rdy_to_xfer() is blocking.
844 * It is the target driver's duty to ensure that not more, than that
845 * number of threads, are blocked in those functions at any time.
849 /* Optional default log flags */
850 const unsigned long default_trace_flags;
852 /* Optional pointer to trace flags */
853 unsigned long *trace_flags;
855 /* Optional local trace table */
856 struct scst_trace_log *trace_tbl;
858 /* Optional local trace table help string */
859 const char *trace_tbl_help;
861 #ifndef CONFIG_SCST_PROC
862 /* Optional sysfs attributes */
863 const struct attribute **tgtt_attrs;
865 /* Optional sysfs target attributes */
866 const struct attribute **tgt_attrs;
868 /* Optional sysfs session attributes */
869 const struct attribute **sess_attrs;
872 /** Private, must be inited to 0 by memset() **/
874 /* List of targets per template, protected by scst_mutex */
875 struct list_head tgt_list;
877 /* List entry of global templates list */
878 struct list_head scst_template_list_entry;
880 #ifdef CONFIG_SCST_PROC
881 /* The pointer to the /proc directory entry */
882 struct proc_dir_entry *proc_tgt_root;
885 /* Set if tgtt_kobj was initialized */
886 unsigned int tgtt_kobj_initialized:1;
888 struct kobject tgtt_kobj; /* kobject for this struct */
890 struct completion tgtt_kobj_release_cmpl;
892 #ifdef CONFIG_SCST_PROC
893 /* Device number in /proc */
898 struct scst_dev_type {
899 /* SCSI type of the supported device. MUST HAVE */
903 * True, if corresponding function supports execution in
904 * the atomic (non-sleeping) context
906 unsigned parse_atomic:1;
907 unsigned exec_atomic:1;
908 unsigned dev_done_atomic:1;
910 #ifdef CONFIG_SCST_PROC
911 /* Set, if no /proc files should be automatically created by SCST */
916 * Should be set, if exec() is synchronous. This is a hint to SCST core
917 * to optimize commands order management.
919 unsigned exec_sync:1;
922 * Called to parse CDB from the cmd and initialize
923 * cmd->bufflen and cmd->data_direction (both - REQUIRED).
924 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
925 * if the next default state should be used, or
926 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
927 * context, but requires sleeping, or SCST_CMD_STATE_STOP if the
928 * command should not be further processed for now. In the
929 * SCST_CMD_STATE_NEED_THREAD_CTX case the function
930 * will be recalled in the thread context, where sleeping is allowed.
932 * Pay attention to "atomic" attribute of the cmd, which can be get
933 * by scst_cmd_atomic(): it is true if the function called in the
934 * atomic (non-sleeping) context.
938 int (*parse) (struct scst_cmd *cmd);
941 * Called to execute CDB. Useful, for instance, to implement
942 * data caching. The result of CDB execution is reported via
943 * cmd->scst_cmd_done() callback.
945 * - SCST_EXEC_COMPLETED - the cmd is done, go to other ones
946 * - SCST_EXEC_NEED_THREAD - thread context is required to execute
947 * the command. Exec() will be called again in the thread context.
948 * - SCST_EXEC_NOT_COMPLETED - the cmd should be sent to SCSI
951 * Pay attention to "atomic" attribute of the cmd, which can be get
952 * by scst_cmd_atomic(): it is true if the function called in the
953 * atomic (non-sleeping) context.
955 * If this function provides sync execution, you should set
956 * exec_sync flag and consider to setup dedicated threads by
957 * setting threads_num > 0.
959 * !! If this function is implemented, scst_check_local_events() !!
960 * !! shall be called inside it just before the actual command's !!
963 * OPTIONAL, if not set, the commands will be sent directly to SCSI
966 int (*exec) (struct scst_cmd *cmd);
969 * Called to notify dev handler about the result of cmd execution
970 * and perform some post processing. Cmd's fields is_send_status and
971 * resp_data_len should be set by this function, but SCST offers good
973 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
974 * if the next default state should be used, or
975 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
976 * context, but requires sleeping. In the last case, the function
977 * will be recalled in the thread context, where sleeping is allowed.
979 * Pay attention to "atomic" attribute of the cmd, which can be get
980 * by scst_cmd_atomic(): it is true if the function called in the
981 * atomic (non-sleeping) context.
983 int (*dev_done) (struct scst_cmd *cmd);
986 * Called to notify dev hander that the command is about to be freed.
987 * Could be called on IRQ context.
989 void (*on_free_cmd) (struct scst_cmd *cmd);
992 * Called to execute a task management command.
994 * - SCST_MGMT_STATUS_SUCCESS - the command is done with success,
995 * no firther actions required
996 * - The SCST_MGMT_STATUS_* error code if the command is failed and
997 * no further actions required
998 * - SCST_DEV_TM_NOT_COMPLETED - regular standard actions for the
999 * command should be done
1001 * Called without any locks held from a thread context.
1003 int (*task_mgmt_fn) (struct scst_mgmt_cmd *mgmt_cmd,
1004 struct scst_tgt_dev *tgt_dev);
1007 * Called when new device is attaching to the dev handler
1008 * Returns 0 on success, error code otherwise.
1010 int (*attach) (struct scst_device *dev);
1012 /* Called when new device is detaching from the dev handler */
1013 void (*detach) (struct scst_device *dev);
1016 * Called when new tgt_dev (session) is attaching to the dev handler.
1017 * Returns 0 on success, error code otherwise.
1019 int (*attach_tgt) (struct scst_tgt_dev *tgt_dev);
1021 /* Called when tgt_dev (session) is detaching from the dev handler */
1022 void (*detach_tgt) (struct scst_tgt_dev *tgt_dev);
1024 #ifdef CONFIG_SCST_PROC
1026 * Those functions can be used to export the handler's statistics and
1027 * other infos to the world outside the kernel as well as to get some
1028 * management commands from it.
1032 int (*read_proc) (struct seq_file *seq, struct scst_dev_type *dev_type);
1033 int (*write_proc) (char *buffer, char **start, off_t offset,
1034 int length, int *eof, struct scst_dev_type *dev_type);
1038 * Name of the dev handler. Must be unique. MUST HAVE.
1040 * It's SCST_MAX_NAME + few more bytes to match scst_user expectations.
1042 char name[SCST_MAX_NAME + 10];
1045 * Number of dedicated threads. If 0 - no dedicated threads will
1046 * be created, if <0 - creation of dedicated threads is prohibited.
1050 /* Optional default log flags */
1051 const unsigned long default_trace_flags;
1053 /* Optional pointer to trace flags */
1054 unsigned long *trace_flags;
1056 /* Optional local trace table */
1057 struct scst_trace_log *trace_tbl;
1059 /* Optional local trace table help string */
1060 const char *trace_tbl_help;
1062 #ifndef CONFIG_SCST_PROC
1063 /* Optional sysfs attributes */
1064 const struct attribute **devt_attrs;
1066 /* Optional sysfs device attributes */
1067 const struct attribute **dev_attrs;
1070 /* Pointer to dev handler's private data */
1073 /* Pointer to parent dev type in the sysfs hierarchy */
1074 struct scst_dev_type *parent;
1076 struct module *module;
1078 /** Private, must be inited to 0 by memset() **/
1080 /* list entry in scst_dev_type_list */
1081 struct list_head dev_type_list_entry;
1083 #ifdef CONFIG_SCST_PROC
1084 /* The pointer to the /proc directory entry */
1085 struct proc_dir_entry *proc_dev_type_root;
1088 unsigned int devt_kobj_initialized:1;
1090 struct kobject devt_kobj; /* main handlers/driver */
1092 /* To wait until devt_kobj released */
1093 struct completion devt_kobj_release_compl;
1097 /* List of remote sessions per target, protected by scst_mutex */
1098 struct list_head sess_list;
1100 /* List entry of targets per template (tgts_list) */
1101 struct list_head tgt_list_entry;
1103 struct scst_tgt_template *tgtt; /* corresponding target template */
1105 struct scst_acg *default_acg; /* The default acg for this target. */
1108 * Maximum SG table size. Needed here, since different cards on the
1109 * same target template can have different SG table limitations.
1113 /* Used for storage of target driver private stuff */
1117 * The following fields used to store and retry cmds if target's
1118 * internal queue is full, so the target is unable to accept
1119 * the cmd returning QUEUE FULL.
1120 * They protected by tgt_lock, where necessary.
1122 bool retry_timer_active;
1123 struct timer_list retry_timer;
1124 atomic_t finished_cmds;
1126 spinlock_t tgt_lock;
1127 struct list_head retry_cmd_list;
1129 /* Used to wait until session finished to unregister */
1130 wait_queue_head_t unreg_waitQ;
1132 #ifdef CONFIG_SCST_PROC
1133 /* Device number in /proc */
1137 /* Name of the target */
1140 #ifdef CONFIG_SCST_PROC
1141 /* Name on the default security group ("Default_target_name") */
1142 char *default_group_name;
1145 /* Set if tgt_kobj was initialized */
1146 unsigned int tgt_kobj_initialized:1;
1149 * Used to protect sysfs attributes to be called after this
1150 * object was unregistered.
1152 struct rw_semaphore tgt_attr_rwsem;
1154 struct kobject tgt_kobj; /* main targets/target kobject */
1155 struct kobject *tgt_sess_kobj; /* target/sessions/ */
1156 struct kobject *tgt_luns_kobj; /* target/luns/ */
1157 struct kobject *tgt_ini_grp_kobj; /* target/ini_groups/ */
1160 /* Hash size and hash fn for hash based lun translation */
1161 #define TGT_DEV_HASH_SHIFT 5
1162 #define TGT_DEV_HASH_SIZE (1 << TGT_DEV_HASH_SHIFT)
1163 #define HASH_VAL(_val) (_val & (TGT_DEV_HASH_SIZE - 1))
1165 #ifdef CONFIG_SCST_MEASURE_LATENCY
1167 /* Defines extended latency statistics */
1168 struct scst_ext_latency_stat {
1169 uint64_t scst_time_rd, tgt_time_rd, dev_time_rd;
1170 unsigned int processed_cmds_rd;
1171 uint64_t min_scst_time_rd, min_tgt_time_rd, min_dev_time_rd;
1172 uint64_t max_scst_time_rd, max_tgt_time_rd, max_dev_time_rd;
1174 uint64_t scst_time_wr, tgt_time_wr, dev_time_wr;
1175 unsigned int processed_cmds_wr;
1176 uint64_t min_scst_time_wr, min_tgt_time_wr, min_dev_time_wr;
1177 uint64_t max_scst_time_wr, max_tgt_time_wr, max_dev_time_wr;
1180 #define SCST_IO_SIZE_THRESHOLD_SMALL (8*1024)
1181 #define SCST_IO_SIZE_THRESHOLD_MEDIUM (32*1024)
1182 #define SCST_IO_SIZE_THRESHOLD_LARGE (128*1024)
1183 #define SCST_IO_SIZE_THRESHOLD_VERY_LARGE (512*1024)
1185 #define SCST_LATENCY_STAT_INDEX_SMALL 0
1186 #define SCST_LATENCY_STAT_INDEX_MEDIUM 1
1187 #define SCST_LATENCY_STAT_INDEX_LARGE 2
1188 #define SCST_LATENCY_STAT_INDEX_VERY_LARGE 3
1189 #define SCST_LATENCY_STAT_INDEX_OTHER 4
1190 #define SCST_LATENCY_STATS_NUM (SCST_LATENCY_STAT_INDEX_OTHER + 1)
1192 #endif /* CONFIG_SCST_MEASURE_LATENCY */
1194 struct scst_session {
1196 * Initialization phase, one of SCST_SESS_IPH_* constants, protected by
1201 struct scst_tgt *tgt; /* corresponding target */
1203 /* Used for storage of target driver private stuff */
1206 unsigned long sess_aflags; /* session's async flags */
1209 * Hash list of tgt_dev's for this session, protected by scst_mutex
1210 * and suspended activity
1212 struct list_head sess_tgt_dev_list_hash[TGT_DEV_HASH_SIZE];
1215 * List of cmds in this session. Used to find a cmd in the
1216 * session. Protected by sess_list_lock.
1218 struct list_head search_cmd_list;
1220 spinlock_t sess_list_lock; /* protects search_cmd_list, etc */
1223 * List of cmds in this in the state after PRE_XMIT_RESP. All the cmds
1224 * moved here from search_cmd_list. Needed for hw_pending_work.
1225 * Protected by sess_list_lock.
1227 struct list_head after_pre_xmit_cmd_list;
1229 atomic_t refcnt; /* get/put counter */
1232 * Alive commands for this session. ToDo: make it part of the common
1235 atomic_t sess_cmd_count;
1237 /* Access control for this session and list entry there */
1238 struct scst_acg *acg;
1240 /* List entry for the sessions list inside ACG */
1241 struct list_head acg_sess_list_entry;
1243 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
1244 struct delayed_work hw_pending_work;
1246 struct work_struct hw_pending_work;
1249 /* Name of attached initiator */
1250 const char *initiator_name;
1252 /* List entry of sessions per target */
1253 struct list_head sess_list_entry;
1255 /* List entry for the list that keeps session, waiting for the init */
1256 struct list_head sess_init_list_entry;
1259 * List entry for the list that keeps session, waiting for the shutdown
1261 struct list_head sess_shut_list_entry;
1264 * Lists of deferred during session initialization commands.
1265 * Protected by sess_list_lock.
1267 struct list_head init_deferred_cmd_list;
1268 struct list_head init_deferred_mcmd_list;
1271 * Shutdown phase, one of SCST_SESS_SPH_* constants, unprotected.
1272 * Async. relating to init_phase, must be a separate variable, because
1273 * session could be unregistered before async. registration is finished.
1275 unsigned long shut_phase;
1277 /* Used if scst_unregister_session() called in wait mode */
1278 struct completion *shutdown_compl;
1280 /* Set if sess_kobj was initialized */
1281 unsigned int sess_kobj_initialized:1;
1284 * Used to protect sysfs attributes to be called after this
1285 * object was unregistered.
1287 struct rw_semaphore sess_attr_rwsem;
1289 struct kobject sess_kobj; /* kobject for this struct */
1292 * Functions and data for user callbacks from scst_register_session()
1293 * and scst_unregister_session()
1295 void *reg_sess_data;
1296 void (*init_result_fn) (struct scst_session *sess, void *data,
1298 void (*unreg_done_fn) (struct scst_session *sess);
1300 #ifdef CONFIG_SCST_MEASURE_LATENCY
1302 * Must be the last to allow to work with drivers who don't know
1303 * about this config time option.
1305 spinlock_t lat_lock;
1306 uint64_t scst_time, tgt_time, dev_time;
1307 unsigned int processed_cmds;
1308 uint64_t min_scst_time, min_tgt_time, min_dev_time;
1309 uint64_t max_scst_time, max_tgt_time, max_dev_time;
1310 struct scst_ext_latency_stat sess_latency_stat[SCST_LATENCY_STATS_NUM];
1314 struct scst_cmd_lists {
1315 spinlock_t cmd_list_lock;
1316 struct list_head active_cmd_list;
1317 wait_queue_head_t cmd_list_waitQ;
1318 struct list_head lists_list_entry;
1322 /* List entry for below *_cmd_lists */
1323 struct list_head cmd_list_entry;
1325 /* Pointer to lists of commands with the lock */
1326 struct scst_cmd_lists *cmd_lists;
1330 struct scst_session *sess; /* corresponding session */
1332 /* Cmd state, one of SCST_CMD_STATE_* constants */
1335 /*************************************************************
1337 *************************************************************/
1340 * Set if expected_sn should be incremented, i.e. cmd was sent
1343 unsigned int sent_for_exec:1;
1345 /* Set if the cmd's action is completed */
1346 unsigned int completed:1;
1348 /* Set if we should ignore Unit Attention in scst_check_sense() */
1349 unsigned int ua_ignore:1;
1351 /* Set if cmd is being processed in atomic context */
1352 unsigned int atomic:1;
1354 /* Set if this command was sent in double UA possible state */
1355 unsigned int double_ua_possible:1;
1357 /* Set if this command contains status */
1358 unsigned int is_send_status:1;
1360 /* Set if cmd is being retried */
1361 unsigned int retry:1;
1363 /* Set if cmd is internally generated */
1364 unsigned int internal:1;
1366 /* Set if the device was blocked by scst_inc_on_dev_cmd() (for debug) */
1367 unsigned int inc_blocking:1;
1369 /* Set if the device should be unblocked after cmd's finish */
1370 unsigned int needs_unblocking:1;
1372 /* Set if scst_dec_on_dev_cmd() call is needed on the cmd's finish */
1373 unsigned int dec_on_dev_needed:1;
1375 /* Set if cmd is queued as hw pending */
1376 unsigned int cmd_hw_pending:1;
1379 * Set if the target driver wants to alloc data buffers on its own.
1380 * In this case alloc_data_buf() must be provided in the target driver
1383 unsigned int tgt_need_alloc_data_buf:1;
1386 * Set by SCST if the custom data buffer allocation by the target driver
1389 unsigned int tgt_data_buf_alloced:1;
1391 /* Set if custom data buffer allocated by dev handler */
1392 unsigned int dh_data_buf_alloced:1;
1394 /* Set if the target driver called scst_set_expected() */
1395 unsigned int expected_values_set:1;
1398 * Set if the SG buffer was modified by scst_set_resp_data_len()
1400 unsigned int sg_buff_modified:1;
1403 * Set if scst_cmd_init_stage1_done() called and the target
1404 * want that preprocessing_done() will be called
1406 unsigned int preprocessing_only:1;
1408 /* Set if cmd's SN was set */
1409 unsigned int sn_set:1;
1411 /* Set if hq_cmd_count was incremented */
1412 unsigned int hq_cmd_inced:1;
1415 * Set if scst_cmd_init_stage1_done() called and the target wants
1416 * that the SN for the cmd won't be assigned until scst_restart_cmd()
1418 unsigned int set_sn_on_restart_cmd:1;
1420 /* Set if the cmd's must not use sgv cache for data buffer */
1421 unsigned int no_sgv:1;
1424 * Set if target driver may need to call dma_sync_sg() or similar
1425 * function before transferring cmd' data to the target device
1428 unsigned int may_need_dma_sync:1;
1430 /* Set if the cmd was done or aborted out of its SN */
1431 unsigned int out_of_sn:1;
1433 /* Set if increment expected_sn in cmd->scst_cmd_done() */
1434 unsigned int inc_expected_sn_on_done:1;
1436 /* Set if tgt_sn field is valid */
1437 unsigned int tgt_sn_set:1;
1439 /* Set if cmd is done */
1440 unsigned int done:1;
1442 /* Set if cmd is finished */
1443 unsigned int finished:1;
1446 * Set if the cmd was delayed by task management debugging code.
1447 * Used only if CONFIG_SCST_DEBUG_TM is on.
1449 unsigned int tm_dbg_delayed:1;
1452 * Set if the cmd must be ignored by task management debugging code.
1453 * Used only if CONFIG_SCST_DEBUG_TM is on.
1455 unsigned int tm_dbg_immut:1;
1457 /**************************************************************/
1459 unsigned long cmd_flags; /* cmd's async flags */
1461 /* Keeps status of cmd's status/data delivery to remote initiator */
1462 int delivery_status;
1464 struct scst_tgt_template *tgtt; /* to save extra dereferences */
1465 struct scst_tgt *tgt; /* to save extra dereferences */
1466 struct scst_device *dev; /* to save extra dereferences */
1468 struct scst_tgt_dev *tgt_dev; /* corresponding device for this cmd */
1470 uint64_t lun; /* LUN for this cmd */
1472 unsigned long start_time;
1474 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1475 struct scsi_request *scsi_req; /* SCSI request */
1478 /* List entry for tgt_dev's SN related lists */
1479 struct list_head sn_cmd_list_entry;
1481 /* Cmd's serial number, used to execute cmd's in order of arrival */
1484 /* The corresponding sn_slot in tgt_dev->sn_slots */
1487 /* List entry for sess's search_cmd_list and after_pre_xmit_cmd_list */
1488 struct list_head sess_cmd_list_entry;
1491 * Used to found the cmd by scst_find_cmd_by_tag(). Set by the
1492 * target driver on the cmd's initialization time
1496 uint32_t tgt_sn; /* SN set by target driver (for TM purposes) */
1498 /* CDB and its len */
1499 uint8_t cdb[SCST_MAX_CDB_SIZE];
1500 short cdb_len; /* it might be -1 */
1501 unsigned short ext_cdb_len;
1504 enum scst_cdb_flags op_flags;
1505 const char *op_name;
1507 enum scst_cmd_queue_type queue_type;
1509 int timeout; /* CDB execution timeout in seconds */
1510 int retries; /* Amount of retries that will be done by SCSI mid-level */
1512 /* SCSI data direction, one of SCST_DATA_* constants */
1513 scst_data_direction data_direction;
1515 /* Remote initiator supplied values, if any */
1516 scst_data_direction expected_data_direction;
1517 int expected_transfer_len;
1518 int expected_in_transfer_len; /* for bidi writes */
1521 * Cmd data length. Could be different from bufflen for commands like
1522 * VERIFY, which transfer different amount of data (if any), than
1527 /* Completition routine */
1528 void (*scst_cmd_done) (struct scst_cmd *cmd, int next_state,
1529 enum scst_exec_context pref_context);
1531 struct sgv_pool_obj *sgv; /* sgv object */
1532 int bufflen; /* cmd buffer length */
1533 struct scatterlist *sg; /* cmd data buffer SG vector */
1534 int sg_cnt; /* SG segments count */
1537 * Response data length in data buffer. This field must not be set
1538 * directly, use scst_set_resp_data_len() for that
1542 /* scst_get_sg_buf_[first,next]() support */
1543 int get_sg_buf_entry_num;
1545 /* Bidirectional transfers support */
1546 int in_bufflen; /* WRITE buffer length */
1547 struct sgv_pool_obj *in_sgv; /* WRITE sgv object */
1548 struct scatterlist *in_sg; /* WRITE data buffer SG vector */
1549 int in_sg_cnt; /* WRITE SG segments count */
1552 * Used if both target driver and dev handler request own memory
1553 * allocation. In other cases, both are equal to sg and sg_cnt
1556 * If target driver requests own memory allocations, it MUST use
1557 * functions scst_cmd_get_tgt_sg*() to get sg and sg_cnt! Otherwise,
1558 * it may use functions scst_cmd_get_sg*().
1560 struct scatterlist *tgt_sg;
1562 struct scatterlist *tgt_in_sg; /* bidirectional */
1563 int tgt_in_sg_cnt; /* bidirectional */
1566 * The status fields in case of errors must be set using
1567 * scst_set_cmd_error_status()!
1569 uint8_t status; /* status byte from target device */
1570 uint8_t msg_status; /* return status from host adapter itself */
1571 uint8_t host_status; /* set by low-level driver to indicate status */
1572 uint8_t driver_status; /* set by mid-level */
1574 uint8_t *sense; /* pointer to sense buffer */
1575 unsigned short sense_valid_len; /* length of valid sense data */
1576 unsigned short sense_buflen; /* length of the sense buffer, if any */
1578 /* Start time when cmd was sent to rdy_to_xfer() or xmit_response() */
1579 unsigned long hw_pending_start;
1581 /* Used for storage of target driver private stuff */
1584 /* Used for storage of dev handler private stuff */
1588 * Used to restore the SG vector if it was modified by
1589 * scst_set_resp_data_len()
1591 int orig_sg_cnt, orig_sg_entry, orig_entry_len;
1593 /* Used to retry commands in case of double UA */
1594 int dbl_ua_orig_resp_data_len, dbl_ua_orig_data_direction;
1596 /* List corresponding mgmt cmd, if any, protected by sess_list_lock */
1597 struct list_head mgmt_cmd_list;
1599 /* List entry for dev's blocked_cmd_list */
1600 struct list_head blocked_cmd_list_entry;
1602 struct scst_cmd *orig_cmd; /* Used to issue REQUEST SENSE */
1604 #ifdef CONFIG_SCST_MEASURE_LATENCY
1606 * Must be the last to allow to work with drivers who don't know
1607 * about this config time option.
1609 uint64_t start, curr_start, parse_time, alloc_buf_time;
1610 uint64_t restart_waiting_time, rdy_to_xfer_time;
1611 uint64_t pre_exec_time, exec_time, dev_done_time;
1612 uint64_t xmit_time, tgt_on_free_time, dev_on_free_time;
1616 struct scst_rx_mgmt_params {
1624 unsigned char tag_set;
1625 unsigned char lun_set;
1626 unsigned char cmd_sn_set;
1629 struct scst_mgmt_cmd_stub {
1630 struct scst_mgmt_cmd *mcmd;
1632 /* List entry in cmd->mgmt_cmd_list */
1633 struct list_head cmd_mgmt_cmd_list_entry;
1636 struct scst_mgmt_cmd {
1637 /* List entry for *_mgmt_cmd_list */
1638 struct list_head mgmt_cmd_list_entry;
1640 struct scst_session *sess;
1642 /* Mgmt cmd state, one of SCST_MCMD_STATE_* constants */
1647 unsigned int completed:1; /* set, if the mcmd is completed */
1648 /* Set if device(s) should be unblocked after mcmd's finish */
1649 unsigned int needs_unblocking:1;
1650 unsigned int lun_set:1; /* set, if lun field is valid */
1651 unsigned int cmd_sn_set:1; /* set, if cmd_sn field is valid */
1652 /* set, if scst_mgmt_affected_cmds_done was called */
1653 unsigned int affected_cmds_done_called:1;
1656 * Number of commands to finish before sending response,
1657 * protected by scst_mcmd_lock
1659 int cmd_finish_wait_count;
1662 * Number of commands to complete (done) before resetting reservation,
1663 * protected by scst_mcmd_lock
1665 int cmd_done_wait_count;
1667 /* Number of completed commands, protected by scst_mcmd_lock */
1668 int completed_cmd_count;
1670 uint64_t lun; /* LUN for this mgmt cmd */
1671 /* or (and for iSCSI) */
1672 uint64_t tag; /* tag of the corresponding cmd */
1674 uint32_t cmd_sn; /* affected command's highest SN */
1676 /* corresponding cmd (to be aborted, found by tag) */
1677 struct scst_cmd *cmd_to_abort;
1679 /* corresponding device for this mgmt cmd (found by lun) */
1680 struct scst_tgt_dev *mcmd_tgt_dev;
1682 /* completition status, one of the SCST_MGMT_STATUS_* constants */
1685 /* Used for storage of target driver private stuff */
1689 struct scst_device {
1690 struct scst_dev_type *handler; /* corresponding dev handler */
1692 struct scst_mem_lim dev_mem_lim;
1694 unsigned short type; /* SCSI type of the device */
1696 /*************************************************************
1697 ** Dev's flags. Updates serialized by dev_lock or suspended
1699 *************************************************************/
1701 /* Set if dev is RESERVED */
1702 unsigned short dev_reserved:1;
1704 /* Set if double reset UA is possible */
1705 unsigned short dev_double_ua_possible:1;
1707 /* If set, dev is read only */
1708 unsigned short rd_only:1;
1710 /**************************************************************/
1712 /*************************************************************
1713 ** Dev's control mode page related values. Updates serialized
1714 ** by scst_block_dev(). It's long to not interfere with the
1716 *************************************************************/
1718 unsigned long queue_alg:4;
1719 unsigned long tst:3;
1720 unsigned long tas:1;
1721 unsigned long swp:1;
1722 unsigned long d_sense:1;
1725 * Set if device implements own ordered commands management. If not set
1726 * and queue_alg is SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER,
1727 * expected_sn will be incremented only after commands finished.
1729 unsigned long has_own_order_mgmt:1;
1731 /**************************************************************/
1733 /* Used for storage of dev handler private stuff */
1736 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1737 /* Used to translate SCSI's cmd to SCST's cmd */
1738 struct gendisk *rq_disk;
1741 /* Corresponding real SCSI device, could be NULL for virtual devices */
1742 struct scsi_device *scsi_dev;
1744 /* Pointer to lists of commands with the lock */
1745 struct scst_cmd_lists *p_cmd_lists;
1747 /* Lists of commands with lock, if dedicated threads are used */
1748 struct scst_cmd_lists cmd_lists;
1750 /* Per-device dedicated IO context */
1751 struct io_context *dev_io_ctx;
1753 /* How many cmds alive on this dev */
1754 atomic_t dev_cmd_count;
1756 /* How many write cmds alive on this dev. Temporary, ToDo */
1757 atomic_t write_cmd_count;
1759 spinlock_t dev_lock; /* device lock */
1762 * How many times device was blocked for new cmds execution.
1763 * Protected by dev_lock
1768 * How many there are "on_dev" commands, i.e. ones those are being
1769 * executed by the underlying SCSI/virtual device.
1771 atomic_t on_dev_count;
1773 struct list_head blocked_cmd_list; /* protected by dev_lock */
1775 /* Used to wait for requested amount of "on_dev" commands */
1776 wait_queue_head_t on_dev_waitQ;
1778 /* A list entry used during TM, protected by scst_mutex */
1779 struct list_head tm_dev_list_entry;
1781 /* Virtual device internal ID */
1784 /* Pointer to virtual device name, for convenience only */
1787 /* List entry in global devices list */
1788 struct list_head dev_list_entry;
1791 * List of tgt_dev's, one per session, protected by scst_mutex or
1792 * dev_lock for reads and both for writes
1794 struct list_head dev_tgt_dev_list;
1796 /* List of acg_dev's, one per acg, protected by scst_mutex */
1797 struct list_head dev_acg_dev_list;
1799 /* List of dedicated threads, protected by scst_mutex */
1800 struct list_head threads_list;
1805 /* Set if tgt_kobj was initialized */
1806 unsigned int dev_kobj_initialized:1;
1809 * Used to protect sysfs attributes to be called after this
1810 * object was unregistered.
1812 struct rw_semaphore dev_attr_rwsem;
1814 struct kobject dev_kobj; /* kobject for this struct */
1815 struct kobject *dev_exp_kobj; /* exported groups */
1817 /* Export number in the dev's sysfs list. Protected by scst_mutex */
1818 int dev_exported_lun_num;
1822 * Used to store threads local tgt_dev specific data
1824 struct scst_thr_data_hdr {
1825 /* List entry in tgt_dev->thr_data_list */
1826 struct list_head thr_data_list_entry;
1827 struct task_struct *owner_thr; /* the owner thread */
1829 /* Function that will be called on the tgt_dev destruction */
1830 void (*free_fn) (struct scst_thr_data_hdr *data);
1834 * Used to store per-session specific device information
1836 struct scst_tgt_dev {
1837 /* List entry in sess->sess_tgt_dev_list_hash */
1838 struct list_head sess_tgt_dev_list_entry;
1840 struct scst_device *dev; /* to save extra dereferences */
1841 uint64_t lun; /* to save extra dereferences */
1844 struct sgv_pool *pool;
1847 unsigned long tgt_dev_flags; /* tgt_dev's async flags */
1849 /* Used for storage of dev handler private stuff */
1852 /* How many cmds alive on this dev in this session */
1853 atomic_t tgt_dev_cmd_count;
1856 * Used to execute cmd's in order of arrival, honoring SCSI task
1859 * Protected by sn_lock, except expected_sn, which is protected by
1860 * itself. Curr_sn must have the same size as expected_sn to
1861 * overflow simultaneously.
1865 unsigned long expected_sn;
1866 unsigned long curr_sn;
1868 struct list_head deferred_cmd_list;
1869 struct list_head skipped_sn_list;
1872 * Set if the prev cmd was ORDERED. Size must allow unprotected
1873 * modifications independant to the neighbour fields.
1875 unsigned long prev_cmd_ordered;
1877 int num_free_sn_slots; /* if it's <0, then all slots are busy */
1878 atomic_t *cur_sn_slot;
1879 atomic_t sn_slots[15];
1881 /* List of scst_thr_data_hdr and lock */
1882 spinlock_t thr_data_lock;
1883 struct list_head thr_data_list;
1885 /* Per-(device, session) dedicated IO context */
1886 struct io_context *tgt_dev_io_ctx;
1888 spinlock_t tgt_dev_lock; /* per-session device lock */
1890 /* List of UA's for this device, protected by tgt_dev_lock */
1891 struct list_head UA_list;
1893 struct scst_session *sess; /* corresponding session */
1894 struct scst_acg_dev *acg_dev; /* corresponding acg_dev */
1896 /* List entry in dev->dev_tgt_dev_list */
1897 struct list_head dev_tgt_dev_list_entry;
1899 /* Internal tmp list entry */
1900 struct list_head extra_tgt_dev_list_entry;
1902 /* Set if INQUIRY DATA HAS CHANGED UA is needed */
1903 unsigned int inq_changed_ua_needed:1;
1906 * Stored Unit Attention sense and its length for possible
1907 * subsequent REQUEST SENSE. Both protected by tgt_dev_lock.
1909 unsigned short tgt_dev_valid_sense_len;
1910 uint8_t tgt_dev_sense[SCST_SENSE_BUFFERSIZE];
1912 #ifdef CONFIG_SCST_MEASURE_LATENCY
1914 * Must be the last to allow to work with drivers who don't know
1915 * about this config time option.
1917 * Protected by sess->lat_lock.
1919 uint64_t scst_time, tgt_time, dev_time;
1920 unsigned int processed_cmds;
1921 struct scst_ext_latency_stat dev_latency_stat[SCST_LATENCY_STATS_NUM];
1926 * Used to store ACG-specific device information, like LUN
1928 struct scst_acg_dev {
1929 struct scst_device *dev; /* corresponding device */
1931 uint64_t lun; /* device's LUN in this acg */
1933 /* If set, the corresponding LU is read only */
1934 unsigned int rd_only:1;
1936 /* Set if acg_dev_kobj was initialized */
1937 unsigned int acg_dev_kobj_initialized:1;
1939 struct scst_acg *acg; /* parent acg */
1941 /* List entry in dev->dev_acg_dev_list */
1942 struct list_head dev_acg_dev_list_entry;
1944 /* List entry in acg->acg_dev_list */
1945 struct list_head acg_dev_list_entry;
1947 /* kobject for this structure */
1948 struct kobject acg_dev_kobj;
1952 * ACG - access control group. Used to store group related
1953 * control information.
1956 /* List of acg_dev's in this acg, protected by scst_mutex */
1957 struct list_head acg_dev_list;
1959 /* List of attached sessions, protected by scst_mutex */
1960 struct list_head acg_sess_list;
1962 /* List of attached acn's, protected by scst_mutex */
1963 struct list_head acn_list;
1965 /* List entry in scst_acg_list */
1966 struct list_head scst_acg_list_entry;
1968 /* Name of this acg */
1969 const char *acg_name;
1971 #ifdef CONFIG_SCST_PROC
1972 /* The pointer to the /proc directory entry */
1973 struct proc_dir_entry *acg_proc_root;
1978 * ACN - access control name. Used to store names, by which
1979 * incoming sessions will be assigned to appropriate ACG.
1982 /* Initiator's name */
1984 /* List entry in acg->acn_list */
1985 struct list_head acn_list_entry;
1989 * Used to store per-session UNIT ATTENTIONs
1991 struct scst_tgt_dev_UA {
1992 /* List entry in tgt_dev->UA_list */
1993 struct list_head UA_list_entry;
1995 /* Set if UA is global for session */
1996 unsigned short global_UA:1;
1998 /* Unit Attention valid sense len */
1999 unsigned short UA_valid_sense_len;
2000 /* Unit Attention sense buf */
2001 uint8_t UA_sense_buffer[SCST_SENSE_BUFFERSIZE];
2004 /* Used to deliver AENs */
2006 int event_fn; /* AEN fn */
2008 struct scst_session *sess; /* corresponding session */
2009 uint64_t lun; /* corresponding LUN in SCSI form */
2015 uint8_t aen_sense[SCST_STANDARD_SENSE_LEN];
2019 /* Keeps status of AEN's delivery to remote initiator */
2020 int delivery_status;
2023 #ifndef smp_mb__after_set_bit
2024 /* There is no smp_mb__after_set_bit() in the kernel */
2025 #define smp_mb__after_set_bit() smp_mb()
2029 * Registers target template.
2030 * Returns 0 on success or appropriate error code otherwise.
2032 * Note: *vtt must be static!
2034 int __scst_register_target_template(struct scst_tgt_template *vtt,
2035 const char *version);
2036 static inline int scst_register_target_template(struct scst_tgt_template *vtt)
2038 return __scst_register_target_template(vtt, SCST_INTERFACE_VERSION);
2042 * Unregisters target template
2044 void scst_unregister_target_template(struct scst_tgt_template *vtt);
2047 * Registers and returns target adapter
2048 * Returns new target structure on success or NULL otherwise.
2050 * If parameter "target_name" isn't NULL, then security group with name
2051 * "Default_##target_name", if created, will be used as the default
2052 * instead of "Default" one for all initiators not assigned to any other group.
2054 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
2055 const char *target_name);
2058 * Unregisters target adapter
2060 void scst_unregister(struct scst_tgt *tgt);
2063 * Registers and returns a session
2065 * Returns new session on success or NULL otherwise
2069 * atomic - true, if the function called in the atomic context. If false,
2070 * this function will block until the session registration is completed.
2071 * initiator_name - remote initiator's name, any NULL-terminated string,
2072 * e.g. iSCSI name, which used as the key to found appropriate access
2073 * control group. Could be NULL, then the default target's LUNs are used.
2074 * data - any target driver supplied data
2075 * result_fn - pointer to the function that will be
2076 * asynchronously called when session initialization finishes.
2077 * Can be NULL. Parameters:
2079 * - data - target driver supplied to scst_register_session() data
2080 * - result - session initialization result, 0 on success or
2081 * appropriate error code otherwise
2083 * Note: A session creation and initialization is a complex task,
2084 * which requires sleeping state, so it can't be fully done
2085 * in interrupt context. Therefore the "bottom half" of it, if
2086 * scst_register_session() is called from atomic context, will be
2087 * done in SCST thread context. In this case scst_register_session()
2088 * will return not completely initialized session, but the target
2089 * driver can supply commands to this session via scst_rx_cmd().
2090 * Those commands processing will be delayed inside SCST until
2091 * the session initialization is finished, then their processing
2092 * will be restarted. The target driver will be notified about
2093 * finish of the session initialization by function result_fn().
2094 * On success the target driver could do nothing, but if the
2095 * initialization fails, the target driver must ensure that
2096 * no more new commands being sent or will be sent to SCST after
2097 * result_fn() returns. All already sent to SCST commands for
2098 * failed session will be returned in xmit_response() with BUSY status.
2099 * In case of failure the driver shall call scst_unregister_session()
2100 * inside result_fn(), it will NOT be called automatically.
2102 struct scst_session *scst_register_session(struct scst_tgt *tgt, int atomic,
2103 const char *initiator_name, void *data,
2104 void (*result_fn) (struct scst_session *sess, void *data, int result));
2107 * Unregisters a session.
2109 * sess - session to be unregistered
2110 * wait - if true, instructs to wait until all commands, which
2111 * currently is being executed and belonged to the session, finished.
2112 * Otherwise, target driver should be prepared to receive
2113 * xmit_response() for the session's command after
2114 * scst_unregister_session() returns.
2115 * unreg_done_fn - pointer to the function that will be
2116 * asynchronously called when the last session's command finishes and
2117 * the session is about to be completely freed. Can be NULL.
2123 * - All outstanding commands will be finished regularly. After
2124 * scst_unregister_session() returned no new commands must be sent to
2125 * SCST via scst_rx_cmd().
2127 * - The caller must ensure that no scst_rx_cmd() or scst_rx_mgmt_fn_*() is
2128 * called in paralell with scst_unregister_session().
2130 * - Can be called before result_fn() of scst_register_session() called,
2131 * i.e. during the session registration/initialization.
2133 * - It is highly recommended to call scst_unregister_session() as soon as it
2134 * gets clear that session will be unregistered and not to wait until all
2135 * related commands finished. This function provides the wait functionality,
2136 * but it also starts recovering stuck commands, if there are any.
2137 * Otherwise, your target driver could wait for those commands forever.
2139 void scst_unregister_session(struct scst_session *sess, int wait,
2140 void (*unreg_done_fn) (struct scst_session *sess));
2143 * Registers dev handler driver.
2144 * Returns 0 on success or appropriate error code otherwise.
2146 * Note: *dev_type must be static!
2148 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
2149 const char *version);
2150 static inline int scst_register_dev_driver(struct scst_dev_type *dev_type)
2152 return __scst_register_dev_driver(dev_type, SCST_INTERFACE_VERSION);
2156 * Unregisters dev handler driver
2158 void scst_unregister_dev_driver(struct scst_dev_type *dev_type);
2161 * Registers dev handler driver for virtual devices (eg VDISK).
2162 * Returns 0 on success or appropriate error code otherwise.
2164 * Note: *dev_type must be static!
2166 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
2167 const char *version);
2168 static inline int scst_register_virtual_dev_driver(
2169 struct scst_dev_type *dev_type)
2171 return __scst_register_virtual_dev_driver(dev_type,
2172 SCST_INTERFACE_VERSION);
2176 * Unregisters dev handler driver for virtual devices
2178 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type);
2181 * Creates and sends new command to SCST.
2182 * Must not be called in parallel with scst_unregister_session() for the
2183 * same sess. Returns the command on success or NULL otherwise
2185 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
2186 const uint8_t *lun, int lun_len, const uint8_t *cdb,
2187 int cdb_len, int atomic);
2190 * Notifies SCST that the driver finished its part of the command
2191 * initialization, and the command is ready for execution.
2192 * The second argument sets preferred command execition context.
2193 * See SCST_CONTEXT_* constants for details.
2197 * If cmd->set_sn_on_restart_cmd not set, this function, as well as
2198 * scst_cmd_init_stage1_done() and scst_restart_cmd(), must not be
2199 * called simultaneously for the same session (more precisely,
2200 * for the same session/LUN, i.e. tgt_dev), i.e. they must be
2201 * somehow externally serialized. This is needed to have lock free fast path in
2202 * scst_cmd_set_sn(). For majority of targets those functions are naturally
2203 * serialized by the single source of commands. Only iSCSI immediate commands
2204 * with multiple connections per session seems to be an exception. For it, some
2205 * mutex/lock shall be used for the serialization.
2207 void scst_cmd_init_done(struct scst_cmd *cmd,
2208 enum scst_exec_context pref_context);
2211 * Notifies SCST that the driver finished the first stage of the command
2212 * initialization, and the command is ready for execution, but after
2213 * SCST done the command's preprocessing preprocessing_done() function
2214 * should be called. The second argument sets preferred command execition
2215 * context. See SCST_CONTEXT_* constants for details.
2217 * See also scst_cmd_init_done() comment for the serialization requirements.
2219 static inline void scst_cmd_init_stage1_done(struct scst_cmd *cmd,
2220 enum scst_exec_context pref_context, int set_sn)
2222 cmd->preprocessing_only = 1;
2223 cmd->set_sn_on_restart_cmd = !set_sn;
2224 scst_cmd_init_done(cmd, pref_context);
2228 * Notifies SCST that the driver finished its part of the command's
2229 * preprocessing and it is ready for further processing.
2230 * The second argument sets data receiving completion status
2231 * (see SCST_PREPROCESS_STATUS_* constants for details)
2232 * The third argument sets preferred command execition context
2233 * (see SCST_CONTEXT_* constants for details).
2235 * See also scst_cmd_init_done() comment for the serialization requirements.
2237 void scst_restart_cmd(struct scst_cmd *cmd, int status,
2238 enum scst_exec_context pref_context);
2241 * Notifies SCST that the driver received all the necessary data
2242 * and the command is ready for further processing.
2243 * The second argument sets data receiving completion status
2244 * (see SCST_RX_STATUS_* constants for details)
2245 * The third argument sets preferred command execition context
2246 * (see SCST_CONTEXT_* constants for details)
2248 void scst_rx_data(struct scst_cmd *cmd, int status,
2249 enum scst_exec_context pref_context);
2252 * Notifies SCST that the driver sent the response and the command
2253 * can be freed now. Don't forget to set the delivery status, if it
2254 * isn't success, using scst_set_delivery_status() before calling
2255 * this function. The third argument sets preferred command execition
2256 * context (see SCST_CONTEXT_* constants for details)
2258 void scst_tgt_cmd_done(struct scst_cmd *cmd,
2259 enum scst_exec_context pref_context);
2262 * Creates new management command sends it for execution.
2263 * Must not be called in parallel with scst_unregister_session() for the
2264 * same sess. Returns 0 for success, error code otherwise.
2266 int scst_rx_mgmt_fn(struct scst_session *sess,
2267 const struct scst_rx_mgmt_params *params);
2270 * Creates new management command using tag and sends it for execution.
2271 * Can be used for SCST_ABORT_TASK only.
2272 * Must not be called in parallel with scst_unregister_session() for the
2273 * same sess. Returns 0 for success, error code otherwise.
2275 * Obsolete in favor of scst_rx_mgmt_fn()
2277 static inline int scst_rx_mgmt_fn_tag(struct scst_session *sess, int fn,
2278 uint64_t tag, int atomic, void *tgt_priv)
2280 struct scst_rx_mgmt_params params;
2282 BUG_ON(fn != SCST_ABORT_TASK);
2284 memset(¶ms, 0, sizeof(params));
2288 params.atomic = atomic;
2289 params.tgt_priv = tgt_priv;
2290 return scst_rx_mgmt_fn(sess, ¶ms);
2294 * Creates new management command using LUN and sends it for execution.
2295 * Currently can be used for any fn, except SCST_ABORT_TASK.
2296 * Must not be called in parallel with scst_unregister_session() for the
2297 * same sess. Returns 0 for success, error code otherwise.
2299 * Obsolete in favor of scst_rx_mgmt_fn()
2301 static inline int scst_rx_mgmt_fn_lun(struct scst_session *sess, int fn,
2302 const uint8_t *lun, int lun_len, int atomic, void *tgt_priv)
2304 struct scst_rx_mgmt_params params;
2306 BUG_ON(fn == SCST_ABORT_TASK);
2308 memset(¶ms, 0, sizeof(params));
2311 params.lun_len = lun_len;
2313 params.atomic = atomic;
2314 params.tgt_priv = tgt_priv;
2315 return scst_rx_mgmt_fn(sess, ¶ms);
2319 * Provides various info about command's CDB.
2321 * Returns: 0 on success, <0 if command is unknown, >0 if command is invalid.
2323 int scst_get_cdb_info(struct scst_cmd *cmd);
2326 * Set error SCSI status in the command and prepares it for returning it
2328 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status);
2331 * Set error in the command and fill the sense buffer
2333 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq);
2336 * Sets BUSY or TASK QUEUE FULL status
2338 void scst_set_busy(struct scst_cmd *cmd);
2341 * Check if sense in the sense buffer, if any, in the correct format. If not,
2342 * convert it to the correct format.
2344 void scst_check_convert_sense(struct scst_cmd *cmd);
2347 * Sets initial Unit Attention for sess, replacing default scst_sense_reset_UA
2349 void scst_set_initial_UA(struct scst_session *sess, int key, int asc, int ascq);
2352 * Notifies SCST core that dev changed its capacity
2354 void scst_capacity_data_changed(struct scst_device *dev);
2357 * Finds a command based on the supplied tag comparing it with one
2358 * that previously set by scst_cmd_set_tag().
2359 * Returns the command on success or NULL otherwise
2361 struct scst_cmd *scst_find_cmd_by_tag(struct scst_session *sess, uint64_t tag);
2364 * Finds a command based on user supplied data and comparision
2365 * callback function, that should return true, if the command is found.
2366 * Returns the command on success or NULL otherwise
2368 struct scst_cmd *scst_find_cmd(struct scst_session *sess, void *data,
2369 int (*cmp_fn) (struct scst_cmd *cmd,
2373 * Translates SCST's data direction to DMA one from backend storage
2376 enum dma_data_direction scst_to_dma_dir(int scst_dir);
2379 * Translates SCST data direction to DMA data direction from the perspective
2380 * of the target device.
2382 enum dma_data_direction scst_to_tgt_dma_dir(int scst_dir);
2385 * Returns true, if cmd's CDB is fully locally handled by SCST and false
2386 * otherwise. Dev handlers parse() and dev_done() not called for such commands.
2388 static inline bool scst_is_cmd_fully_local(struct scst_cmd *cmd)
2390 return (cmd->op_flags & SCST_FULLY_LOCAL_CMD) != 0;
2394 * Returns true, if cmd's CDB is locally handled by SCST and
2397 static inline bool scst_is_cmd_local(struct scst_cmd *cmd)
2399 return (cmd->op_flags & SCST_LOCAL_CMD) != 0;
2402 /* Returns true, if cmd can deliver UA */
2403 static inline bool scst_is_ua_command(struct scst_cmd *cmd)
2405 return (cmd->op_flags & SCST_SKIP_UA) == 0;
2409 * Registers a virtual device.
2411 * dev_type - the device's device handler
2412 * dev_name - the new device name, NULL-terminated string. Must be uniq
2413 * among all virtual devices in the system. The value isn't
2414 * copied, only the reference is stored, so the value must
2415 * remain valid during the device lifetime.
2416 * Returns assinged to the device ID on success, or negative value otherwise
2418 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
2419 const char *dev_name);
2422 * Unegisters a virtual device.
2424 * id - the device's ID, returned by the registration function
2426 void scst_unregister_virtual_device(int id);
2429 * Get/Set functions for tgt's sg_tablesize
2431 static inline int scst_tgt_get_sg_tablesize(struct scst_tgt *tgt)
2433 return tgt->sg_tablesize;
2436 static inline void scst_tgt_set_sg_tablesize(struct scst_tgt *tgt, int val)
2438 tgt->sg_tablesize = val;
2442 * Get/Set functions for tgt's target private data
2444 static inline void *scst_tgt_get_tgt_priv(struct scst_tgt *tgt)
2446 return tgt->tgt_priv;
2449 static inline void scst_tgt_set_tgt_priv(struct scst_tgt *tgt, void *val)
2451 tgt->tgt_priv = val;
2455 * Get/Set functions for session's target private data
2457 static inline void *scst_sess_get_tgt_priv(struct scst_session *sess)
2459 return sess->tgt_priv;
2462 static inline void scst_sess_set_tgt_priv(struct scst_session *sess,
2465 sess->tgt_priv = val;
2469 * Returns TRUE if cmd is being executed in atomic context.
2471 * Note: checkpatch will complain on the use of in_atomic() below. You can
2472 * safely ignore this warning since in_atomic() is used here only for debugging
2475 static inline int scst_cmd_atomic(struct scst_cmd *cmd)
2477 int res = cmd->atomic;
2478 #ifdef CONFIG_SCST_EXTRACHECKS
2479 if (unlikely((in_atomic() || in_interrupt() || irqs_disabled()) &&
2481 printk(KERN_ERR "ERROR: atomic context and non-atomic cmd\n");
2490 static inline enum scst_exec_context __scst_estimate_context(bool direct)
2493 return SCST_CONTEXT_TASKLET;
2494 else if (irqs_disabled())
2495 return SCST_CONTEXT_THREAD;
2497 return direct ? SCST_CONTEXT_DIRECT :
2498 SCST_CONTEXT_DIRECT_ATOMIC;
2501 static inline enum scst_exec_context scst_estimate_context(void)
2503 return __scst_estimate_context(0);
2506 static inline enum scst_exec_context scst_estimate_context_direct(void)
2508 return __scst_estimate_context(1);
2511 /* Returns cmd's CDB */
2512 static inline const uint8_t *scst_cmd_get_cdb(struct scst_cmd *cmd)
2517 /* Returns cmd's CDB length */
2518 static inline int scst_cmd_get_cdb_len(struct scst_cmd *cmd)
2520 return cmd->cdb_len;
2523 /* Returns cmd's extended CDB */
2524 static inline const uint8_t *scst_cmd_get_ext_cdb(struct scst_cmd *cmd)
2526 return cmd->ext_cdb;
2529 /* Returns cmd's extended CDB length */
2530 static inline int scst_cmd_get_ext_cdb_len(struct scst_cmd *cmd)
2532 return cmd->ext_cdb_len;
2535 /* Sets cmd's extended CDB and its length */
2536 static inline void scst_cmd_set_ext_cdb(struct scst_cmd *cmd,
2537 uint8_t *ext_cdb, unsigned int ext_cdb_len)
2539 cmd->ext_cdb = ext_cdb;
2540 cmd->ext_cdb_len = ext_cdb_len;
2543 /* Returns cmd's session */
2544 static inline struct scst_session *scst_cmd_get_session(struct scst_cmd *cmd)
2549 /* Returns cmd's response data length */
2550 static inline int scst_cmd_get_resp_data_len(struct scst_cmd *cmd)
2552 return cmd->resp_data_len;
2555 /* Returns if status should be sent for cmd */
2556 static inline int scst_cmd_get_is_send_status(struct scst_cmd *cmd)
2558 return cmd->is_send_status;
2562 * Returns pointer to cmd's SG data buffer.
2564 * Usage of this function is not recommended, use scst_get_buf_*()
2565 * family of functions instead.
2567 static inline struct scatterlist *scst_cmd_get_sg(struct scst_cmd *cmd)
2573 * Returns cmd's sg_cnt.
2575 * Usage of this function is not recommended, use scst_get_buf_*()
2576 * family of functions instead.
2578 static inline int scst_cmd_get_sg_cnt(struct scst_cmd *cmd)
2584 * Returns cmd's data buffer length.
2586 * In case if you need to iterate over data in the buffer, usage of
2587 * this function is not recommended, use scst_get_buf_*()
2588 * family of functions instead.
2590 static inline unsigned int scst_cmd_get_bufflen(struct scst_cmd *cmd)
2592 return cmd->bufflen;
2596 * Returns pointer to cmd's bidirectional in (WRITE) SG data buffer.
2598 * Usage of this function is not recommended, use scst_get_in_buf_*()
2599 * family of functions instead.
2601 static inline struct scatterlist *scst_cmd_get_in_sg(struct scst_cmd *cmd)
2607 * Returns cmd's bidirectional in (WRITE) sg_cnt.
2609 * Usage of this function is not recommended, use scst_get_in_buf_*()
2610 * family of functions instead.
2612 static inline int scst_cmd_get_in_sg_cnt(struct scst_cmd *cmd)
2614 return cmd->in_sg_cnt;
2618 * Returns cmd's bidirectional in (WRITE) data buffer length.
2620 * In case if you need to iterate over data in the buffer, usage of
2621 * this function is not recommended, use scst_get_in_buf_*()
2622 * family of functions instead.
2624 static inline unsigned int scst_cmd_get_in_bufflen(struct scst_cmd *cmd)
2626 return cmd->in_bufflen;
2629 /* Returns pointer to cmd's target's SG data buffer */
2630 static inline struct scatterlist *scst_cmd_get_tgt_sg(struct scst_cmd *cmd)
2635 /* Returns cmd's target's sg_cnt */
2636 static inline int scst_cmd_get_tgt_sg_cnt(struct scst_cmd *cmd)
2638 return cmd->tgt_sg_cnt;
2641 /* Sets cmd's target's SG data buffer */
2642 static inline void scst_cmd_set_tgt_sg(struct scst_cmd *cmd,
2643 struct scatterlist *sg, int sg_cnt)
2646 cmd->tgt_sg_cnt = sg_cnt;
2647 cmd->tgt_data_buf_alloced = 1;
2650 /* Returns pointer to cmd's target's IN SG data buffer */
2651 static inline struct scatterlist *scst_cmd_get_in_tgt_sg(struct scst_cmd *cmd)
2653 return cmd->tgt_in_sg;
2656 /* Returns cmd's target's IN sg_cnt */
2657 static inline int scst_cmd_get_tgt_in_sg_cnt(struct scst_cmd *cmd)
2659 return cmd->tgt_in_sg_cnt;
2662 /* Sets cmd's target's IN SG data buffer */
2663 static inline void scst_cmd_set_tgt_in_sg(struct scst_cmd *cmd,
2664 struct scatterlist *sg, int sg_cnt)
2666 WARN_ON(!cmd->tgt_data_buf_alloced);
2668 cmd->tgt_in_sg = sg;
2669 cmd->tgt_in_sg_cnt = sg_cnt;
2672 /* Returns cmd's data direction */
2673 static inline scst_data_direction scst_cmd_get_data_direction(
2674 struct scst_cmd *cmd)
2676 return cmd->data_direction;
2679 /* Returns cmd's status byte from host device */
2680 static inline uint8_t scst_cmd_get_status(struct scst_cmd *cmd)
2685 /* Returns cmd's status from host adapter itself */
2686 static inline uint8_t scst_cmd_get_msg_status(struct scst_cmd *cmd)
2688 return cmd->msg_status;
2691 /* Returns cmd's status set by low-level driver to indicate its status */
2692 static inline uint8_t scst_cmd_get_host_status(struct scst_cmd *cmd)
2694 return cmd->host_status;
2697 /* Returns cmd's status set by SCSI mid-level */
2698 static inline uint8_t scst_cmd_get_driver_status(struct scst_cmd *cmd)
2700 return cmd->driver_status;
2703 /* Returns pointer to cmd's sense buffer */
2704 static inline uint8_t *scst_cmd_get_sense_buffer(struct scst_cmd *cmd)
2709 /* Returns cmd's valid sense length */
2710 static inline int scst_cmd_get_sense_buffer_len(struct scst_cmd *cmd)
2712 return cmd->sense_valid_len;
2716 * Get/Set functions for cmd's target SN
2718 static inline uint64_t scst_cmd_get_tag(struct scst_cmd *cmd)
2723 static inline void scst_cmd_set_tag(struct scst_cmd *cmd, uint64_t tag)
2729 * Get/Set functions for cmd's target private data.
2730 * Variant with *_lock must be used if target driver uses
2731 * scst_find_cmd() to avoid race with it, except inside scst_find_cmd()'s
2732 * callback, where lock is already taken.
2734 static inline void *scst_cmd_get_tgt_priv(struct scst_cmd *cmd)
2736 return cmd->tgt_priv;
2739 static inline void scst_cmd_set_tgt_priv(struct scst_cmd *cmd, void *val)
2741 cmd->tgt_priv = val;
2744 void *scst_cmd_get_tgt_priv_lock(struct scst_cmd *cmd);
2745 void scst_cmd_set_tgt_priv_lock(struct scst_cmd *cmd, void *val);
2748 * Get/Set functions for tgt_need_alloc_data_buf flag
2750 static inline int scst_cmd_get_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2752 return cmd->tgt_need_alloc_data_buf;
2755 static inline void scst_cmd_set_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2757 cmd->tgt_need_alloc_data_buf = 1;
2761 * Get/Set functions for tgt_data_buf_alloced flag
2763 static inline int scst_cmd_get_tgt_data_buff_alloced(struct scst_cmd *cmd)
2765 return cmd->tgt_data_buf_alloced;
2768 static inline void scst_cmd_set_tgt_data_buff_alloced(struct scst_cmd *cmd)
2770 cmd->tgt_data_buf_alloced = 1;
2774 * Get/Set functions for dh_data_buf_alloced flag
2776 static inline int scst_cmd_get_dh_data_buff_alloced(struct scst_cmd *cmd)
2778 return cmd->dh_data_buf_alloced;
2781 static inline void scst_cmd_set_dh_data_buff_alloced(struct scst_cmd *cmd)
2783 cmd->dh_data_buf_alloced = 1;
2787 * Get/Set functions for no_sgv flag
2789 static inline int scst_cmd_get_no_sgv(struct scst_cmd *cmd)
2794 static inline void scst_cmd_set_no_sgv(struct scst_cmd *cmd)
2800 * Get/Set functions for tgt_sn
2802 static inline int scst_cmd_get_tgt_sn(struct scst_cmd *cmd)
2804 BUG_ON(!cmd->tgt_sn_set);
2808 static inline void scst_cmd_set_tgt_sn(struct scst_cmd *cmd, uint32_t tgt_sn)
2810 cmd->tgt_sn_set = 1;
2811 cmd->tgt_sn = tgt_sn;
2815 * Returns 1 if the cmd was aborted, so its status is invalid and no
2816 * reply shall be sent to the remote initiator. A target driver should
2817 * only clear internal resources, associated with cmd.
2819 static inline int scst_cmd_aborted(struct scst_cmd *cmd)
2821 return test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags) &&
2822 !test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
2825 /* Returns sense data format for cmd's dev */
2826 static inline bool scst_get_cmd_dev_d_sense(struct scst_cmd *cmd)
2828 return (cmd->dev != NULL) ? cmd->dev->d_sense : 0;
2832 * Get/Set functions for expected data direction, transfer length
2833 * and its validity flag
2835 static inline int scst_cmd_is_expected_set(struct scst_cmd *cmd)
2837 return cmd->expected_values_set;
2840 static inline scst_data_direction scst_cmd_get_expected_data_direction(
2841 struct scst_cmd *cmd)
2843 return cmd->expected_data_direction;
2846 static inline int scst_cmd_get_expected_transfer_len(
2847 struct scst_cmd *cmd)
2849 return cmd->expected_transfer_len;
2852 static inline int scst_cmd_get_expected_in_transfer_len(
2853 struct scst_cmd *cmd)
2855 return cmd->expected_in_transfer_len;
2858 static inline void scst_cmd_set_expected(struct scst_cmd *cmd,
2859 scst_data_direction expected_data_direction,
2860 int expected_transfer_len)
2862 cmd->expected_data_direction = expected_data_direction;
2863 cmd->expected_transfer_len = expected_transfer_len;
2864 cmd->expected_values_set = 1;
2867 static inline void scst_cmd_set_expected_in_transfer_len(struct scst_cmd *cmd,
2868 int expected_in_transfer_len)
2870 WARN_ON(!cmd->expected_values_set);
2871 cmd->expected_in_transfer_len = expected_in_transfer_len;
2875 * Get/clear functions for cmd's may_need_dma_sync
2877 static inline int scst_get_may_need_dma_sync(struct scst_cmd *cmd)
2879 return cmd->may_need_dma_sync;
2882 static inline void scst_clear_may_need_dma_sync(struct scst_cmd *cmd)
2884 cmd->may_need_dma_sync = 0;
2888 * Get/set functions for cmd's delivery_status. It is one of
2889 * SCST_CMD_DELIVERY_* constants. It specifies the status of the
2890 * command's delivery to initiator.
2892 static inline int scst_get_delivery_status(struct scst_cmd *cmd)
2894 return cmd->delivery_status;
2897 static inline void scst_set_delivery_status(struct scst_cmd *cmd,
2898 int delivery_status)
2900 cmd->delivery_status = delivery_status;
2904 * Get/Set function for mgmt cmd's target private data
2906 static inline void *scst_mgmt_cmd_get_tgt_priv(struct scst_mgmt_cmd *mcmd)
2908 return mcmd->tgt_priv;
2911 static inline void scst_mgmt_cmd_set_tgt_priv(struct scst_mgmt_cmd *mcmd,
2914 mcmd->tgt_priv = val;
2917 /* Returns mgmt cmd's completition status (SCST_MGMT_STATUS_* constants) */
2918 static inline int scst_mgmt_cmd_get_status(struct scst_mgmt_cmd *mcmd)
2920 return mcmd->status;
2923 /* Returns mgmt cmd's TM fn */
2924 static inline int scst_mgmt_cmd_get_fn(struct scst_mgmt_cmd *mcmd)
2930 * Called by dev handler's task_mgmt_fn() to notify SCST core that mcmd
2931 * is going to complete asynchronously.
2933 void scst_prepare_async_mcmd(struct scst_mgmt_cmd *mcmd);
2936 * Called by dev handler to notify SCST core that async. mcmd is completed
2937 * with status "status".
2939 void scst_async_mcmd_completed(struct scst_mgmt_cmd *mcmd, int status);
2941 /* Returns AEN's fn */
2942 static inline int scst_aen_get_event_fn(struct scst_aen *aen)
2944 return aen->event_fn;
2947 /* Returns AEN's session */
2948 static inline struct scst_session *scst_aen_get_sess(struct scst_aen *aen)
2953 /* Returns AEN's LUN */
2954 static inline uint64_t scst_aen_get_lun(struct scst_aen *aen)
2959 /* Returns SCSI AEN's sense */
2960 static inline const uint8_t *scst_aen_get_sense(struct scst_aen *aen)
2962 return aen->aen_sense;
2965 /* Returns SCSI AEN's sense length */
2966 static inline int scst_aen_get_sense_len(struct scst_aen *aen)
2968 return aen->aen_sense_len;
2972 * Get/set functions for AEN's delivery_status. It is one of
2973 * SCST_AEN_RES_* constants. It specifies the status of the
2974 * command's delivery to initiator.
2976 static inline int scst_get_aen_delivery_status(struct scst_aen *aen)
2978 return aen->delivery_status;
2981 static inline void scst_set_aen_delivery_status(struct scst_aen *aen,
2984 aen->delivery_status = status;
2988 * Notifies SCST that the driver has sent the AEN and it
2989 * can be freed now. Don't forget to set the delivery status, if it
2990 * isn't success, using scst_set_aen_delivery_status() before calling
2993 void scst_aen_done(struct scst_aen *aen);
2995 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2998 * The macro's sg_page(), sg_virt(), sg_init_table(), sg_assign_page() and
2999 * sg_set_page() have been introduced in the 2.6.24 kernel. The definitions
3000 * below are backports of the 2.6.24 macro's for older kernels. There is one
3001 * exception however: when compiling SCST on a system with a pre-2.6.24 kernel
3002 * (e.g. RHEL 5.x) where the OFED kernel headers have been installed, do not
3003 * define the backported macro's because OFED has already defined these.
3006 #ifndef __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__
3008 static inline struct page *sg_page(struct scatterlist *sg)
3013 static inline void *sg_virt(struct scatterlist *sg)
3015 return page_address(sg_page(sg)) + sg->offset;
3018 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
3020 memset(sgl, 0, sizeof(*sgl) * nents);
3023 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
3028 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
3029 unsigned int len, unsigned int offset)
3031 sg_assign_page(sg, page);
3032 sg->offset = offset;
3036 static inline struct scatterlist *sg_next(struct scatterlist *sg)
3042 #endif /* __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__ */
3044 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
3046 static inline void sg_clear(struct scatterlist *sg)
3048 memset(sg, 0, sizeof(*sg));
3049 #ifdef CONFIG_DEBUG_SG
3050 sg->sg_magic = SG_MAGIC;
3054 enum scst_sg_copy_dir {
3055 SCST_SG_COPY_FROM_TARGET,
3056 SCST_SG_COPY_TO_TARGET
3060 * Copies data between cmd->tgt_sg and cmd->sg in direction defined by
3061 * copy_dir parameter.
3063 void scst_copy_sg(struct scst_cmd *cmd, enum scst_sg_copy_dir copy_dir);
3066 * Functions for access to the commands data (SG) buffer,
3067 * including HIGHMEM environment. Should be used instead of direct
3068 * access. Returns the mapped buffer length for success, 0 for EOD,
3069 * negative error code otherwise.
3071 * "Buf" argument returns the mapped buffer
3073 * The "put" function unmaps the buffer.
3075 static inline int __scst_get_buf(struct scst_cmd *cmd, struct scatterlist *sg,
3076 int sg_cnt, uint8_t **buf)
3079 int i = cmd->get_sg_buf_entry_num;
3083 if ((i >= sg_cnt) || unlikely(sg == NULL))
3086 *buf = page_address(sg_page(&sg[i]));
3087 *buf += sg[i].offset;
3090 cmd->get_sg_buf_entry_num++;
3096 static inline int scst_get_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3098 cmd->get_sg_buf_entry_num = 0;
3099 cmd->may_need_dma_sync = 1;
3100 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3103 static inline int scst_get_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3105 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
3108 static inline void scst_put_buf(struct scst_cmd *cmd, void *buf)
3113 static inline int scst_get_in_buf_first(struct scst_cmd *cmd, uint8_t **buf)
3115 cmd->get_sg_buf_entry_num = 0;
3116 cmd->may_need_dma_sync = 1;
3117 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3120 static inline int scst_get_in_buf_next(struct scst_cmd *cmd, uint8_t **buf)
3122 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
3125 static inline void scst_put_in_buf(struct scst_cmd *cmd, void *buf)
3131 * Returns approximate higher rounded buffers count that
3132 * scst_get_buf_[first|next]() return.
3134 static inline int scst_get_buf_count(struct scst_cmd *cmd)
3136 return (cmd->sg_cnt == 0) ? 1 : cmd->sg_cnt;
3140 * Returns approximate higher rounded buffers count that
3141 * scst_get_in_buf_[first|next]() return.
3143 static inline int scst_get_in_buf_count(struct scst_cmd *cmd)
3145 return (cmd->in_sg_cnt == 0) ? 1 : cmd->in_sg_cnt;
3148 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23) && !defined(BACKPORT_LINUX_WORKQUEUE_TO_2_6_19)
3149 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
3150 static inline int cancel_delayed_work_sync(struct delayed_work *work)
3152 static inline int cancel_delayed_work_sync(struct work_struct *work)
3157 res = cancel_delayed_work(work);
3158 flush_scheduled_work();
3164 * Suspends and resumes any activity.
3165 * Function scst_suspend_activity() doesn't return 0, until there are any
3166 * active commands (state after SCST_CMD_STATE_INIT). If "interruptible"
3167 * is true, it returns after SCST_SUSPENDING_TIMEOUT or if it was interrupted
3168 * by a signal with the coresponding error status < 0. If "interruptible"
3169 * is false, it will wait virtually forever.
3171 * New arriving commands stay in that state until scst_resume_activity()
3174 int scst_suspend_activity(bool interruptible);
3175 void scst_resume_activity(void);
3178 * Main SCST commands processing routing. Must be used only by dev handlers.
3179 * Argument atomic is true if function called in atomic context.
3181 void scst_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3184 * SCST commands processing routine, which should be called by dev handler
3185 * after its parse() callback returned SCST_CMD_STATE_STOP. Arguments
3186 * the same as for scst_process_active_cmd().
3188 void scst_post_parse_process_active_cmd(struct scst_cmd *cmd, bool atomic);
3191 * Checks if command can be executed (reservations, etc.) or there are local
3192 * events, like pending UAs. Returns < 0 if command must be aborted, > 0 if
3193 * there is an event and command should be immediately completed, or 0
3196 * !! Dev handlers implementing exec() callback must call this function there !!
3197 * !! just before the actual command's execution !!
3199 int scst_check_local_events(struct scst_cmd *cmd);
3202 * Returns the next state of the SCSI target state machine in case if command's
3203 * completed abnormally.
3205 int scst_get_cmd_abnormal_done_state(const struct scst_cmd *cmd);
3208 * Sets state of the SCSI target state machine in case if command's completed
3211 void scst_set_cmd_abnormal_done_state(struct scst_cmd *cmd);
3213 struct scst_trace_log {
3218 #ifdef CONFIG_SCST_PROC
3221 * Returns target driver's root entry in SCST's /proc hierarchy.
3222 * The driver can create own files/directories here, which should
3223 * be deleted in the driver's release().
3225 struct proc_dir_entry *scst_proc_get_tgt_root(
3226 struct scst_tgt_template *vtt);
3229 * Returns device handler's root entry in SCST's /proc hierarchy.
3230 * The driver can create own files/directories here, which should
3231 * be deleted in the driver's detach()/release().
3233 struct proc_dir_entry *scst_proc_get_dev_type_root(
3234 struct scst_dev_type *dtt);
3237 ** Two library functions and the structure to help the drivers
3238 ** that use scst_debug.* facilities manage "trace_level" /proc entry.
3239 ** The functions service "standard" log levels and allow to work
3240 ** with driver specific levels, which should be passed inside as
3241 ** NULL-terminated array of struct scst_trace_log's, where:
3242 ** - val - the level's numeric value
3243 ** - token - its string representation
3246 int scst_proc_log_entry_read(struct seq_file *seq, unsigned long log_level,
3247 const struct scst_trace_log *tbl);
3248 int scst_proc_log_entry_write(struct file *file, const char __user *buf,
3249 unsigned long length, unsigned long *log_level,
3250 unsigned long default_level, const struct scst_trace_log *tbl);
3253 * helper data structure and function to create proc entry.
3255 struct scst_proc_data {
3256 const struct file_operations seq_op;
3257 int (*show)(struct seq_file *, void *);
3261 int scst_single_seq_open(struct inode *inode, struct file *file);
3263 struct proc_dir_entry *scst_create_proc_entry(struct proc_dir_entry *root,
3264 const char *name, struct scst_proc_data *pdata);
3266 #define SCST_DEF_RW_SEQ_OP(x) \
3268 .owner = THIS_MODULE, \
3269 .open = scst_single_seq_open,\
3272 .llseek = seq_lseek, \
3273 .release = single_release, \
3276 #else /* CONFIG_SCST_PROC */
3278 /* Regular SCST sysfs operations */
3279 extern struct sysfs_ops scst_sysfs_ops;
3282 * Returns target driver's root sysfs kobject.
3283 * The driver can create own files/directories/links here.
3285 static inline struct kobject *scst_sysfs_get_tgtt_kobj(
3286 struct scst_tgt_template *tgtt)
3288 return &tgtt->tgtt_kobj;
3292 * Returns target's root sysfs kobject.
3293 * The driver can create own files/directories/links here.
3295 static inline struct kobject *scst_sysfs_get_tgt_kobj(
3296 struct scst_tgt *tgt)
3298 return &tgt->tgt_kobj;
3302 * Returns device handler's root sysfs kobject.
3303 * The driver can create own files/directories/links here.
3305 static inline struct kobject *scst_sysfs_get_devt_kobj(
3306 struct scst_dev_type *devt)
3308 return &devt->devt_kobj;
3312 * Returns device's root sysfs kobject.
3313 * The driver can create own files/directories/links here.
3315 static inline struct kobject *scst_sysfs_get_dev_kobj(
3316 struct scst_device *dev)
3318 return &dev->dev_kobj;
3322 * Returns session's root sysfs kobject.
3323 * The driver can create own files/directories/links here.
3325 static inline struct kobject *scst_sysfs_get_sess_kobj(
3326 struct scst_session *sess)
3328 return &sess->sess_kobj;
3331 #endif /* CONFIG_SCST_PROC */
3333 /* Returns target name */
3334 static inline const char *scst_get_tgt_name(const struct scst_tgt *tgt)
3336 return tgt->tgt_name;
3340 * Adds and deletes (stops) num of global SCST's threads. Returns 0 on
3341 * success, error code otherwise.
3343 int scst_add_global_threads(int num);
3344 void scst_del_global_threads(int num);
3346 int scst_alloc_sense(struct scst_cmd *cmd, int atomic);
3347 int scst_alloc_set_sense(struct scst_cmd *cmd, int atomic,
3348 const uint8_t *sense, unsigned int len);
3351 * Sets the corresponding field in the sense buffer taking sense type
3352 * into account. Returns resulting sense length.
3354 int scst_set_sense(uint8_t *buffer, int len, bool d_sense,
3355 int key, int asc, int ascq);
3358 * Returns true if the sense is valid and carrying a Unit Attention or
3361 bool scst_is_ua_sense(const uint8_t *sense, int len);
3364 * Returnes true if sense matches to (key, asc, ascq) and false otherwise.
3365 * Valid_mask is one or several SCST_SENSE_*_VALID constants setting valid
3366 * (key, asc, ascq) values.
3368 bool scst_analyze_sense(const uint8_t *sense, int len,
3369 unsigned int valid_mask, int key, int asc, int ascq);
3372 * Returnes a pseudo-random number for debugging purposes. Available only in
3375 unsigned long scst_random(void);
3378 * Sets response data length for cmd and truncates its SG vector accordingly.
3379 * The cmd->resp_data_len must not be set directly, it must be set only
3380 * using this function. Value of resp_data_len must be <= cmd->bufflen.
3382 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len);
3385 * Get/put global ref counter that prevents from entering into suspended
3386 * activities stage, so protects from any global management operations.
3388 void scst_get(void);
3389 void scst_put(void);
3394 void scst_cmd_get(struct scst_cmd *cmd);
3395 void scst_cmd_put(struct scst_cmd *cmd);
3398 * Allocates and returns pointer to SG vector with data size "size".
3399 * In *count returned the count of entries in the vector.
3400 * Returns NULL for failure.
3402 struct scatterlist *scst_alloc(int size, gfp_t gfp_mask, int *count);
3404 /* Frees SG vector returned by scst_alloc() */
3405 void scst_free(struct scatterlist *sg, int count);
3408 * Adds local to the current thread data to tgt_dev
3409 * (they will be local for the tgt_dev and current thread).
3411 void scst_add_thr_data(struct scst_tgt_dev *tgt_dev,
3412 struct scst_thr_data_hdr *data,
3413 void (*free_fn) (struct scst_thr_data_hdr *data));
3415 /* Deletes all local to threads data from tgt_dev */
3416 void scst_del_all_thr_data(struct scst_tgt_dev *tgt_dev);
3418 /* Deletes all local to threads data from all tgt_dev's of the dev */
3419 void scst_dev_del_all_thr_data(struct scst_device *dev);
3421 /* Finds local to the thread data. Returns NULL, if they not found. */
3422 struct scst_thr_data_hdr *__scst_find_thr_data(struct scst_tgt_dev *tgt_dev,
3423 struct task_struct *tsk);
3425 /* Finds local to the current thread data. Returns NULL, if they not found. */
3426 static inline struct scst_thr_data_hdr *scst_find_thr_data(
3427 struct scst_tgt_dev *tgt_dev)
3429 return __scst_find_thr_data(tgt_dev, current);
3432 static inline void scst_thr_data_get(struct scst_thr_data_hdr *data)
3434 atomic_inc(&data->ref);
3437 static inline void scst_thr_data_put(struct scst_thr_data_hdr *data)
3439 if (atomic_dec_and_test(&data->ref))
3440 data->free_fn(data);
3444 ** Generic parse() support routines.
3445 ** Done via pointer on functions to avoid unneeded dereferences on
3449 /* Calculates and returns block shift for the given sector size */
3450 int scst_calc_block_shift(int sector_size);
3452 /* Generic parse() for SBC (disk) devices */
3453 int scst_sbc_generic_parse(struct scst_cmd *cmd,
3454 int (*get_block_shift)(struct scst_cmd *cmd));
3456 /* Generic parse() for MMC (cdrom) devices */
3457 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
3458 int (*get_block_shift)(struct scst_cmd *cmd));
3460 /* Generic parse() for MO disk devices */
3461 int scst_modisk_generic_parse(struct scst_cmd *cmd,
3462 int (*get_block_shift)(struct scst_cmd *cmd));
3464 /* Generic parse() for tape devices */
3465 int scst_tape_generic_parse(struct scst_cmd *cmd,
3466 int (*get_block_size)(struct scst_cmd *cmd));
3468 /* Generic parse() for changer devices */
3469 int scst_changer_generic_parse(struct scst_cmd *cmd,
3470 int (*nothing)(struct scst_cmd *cmd));
3472 /* Generic parse() for "processor" devices */
3473 int scst_processor_generic_parse(struct scst_cmd *cmd,
3474 int (*nothing)(struct scst_cmd *cmd));
3476 /* Generic parse() for RAID devices */
3477 int scst_raid_generic_parse(struct scst_cmd *cmd,
3478 int (*nothing)(struct scst_cmd *cmd));
3481 ** Generic dev_done() support routines.
3482 ** Done via pointer on functions to avoid unneeded dereferences on
3486 /* Generic dev_done() for block devices */
3487 int scst_block_generic_dev_done(struct scst_cmd *cmd,
3488 void (*set_block_shift)(struct scst_cmd *cmd, int block_shift));
3490 /* Generic dev_done() for tape devices */
3491 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
3492 void (*set_block_size)(struct scst_cmd *cmd, int block_size));
3495 * Issues a MODE SENSE for control mode page data and sets the corresponding
3496 * dev's parameter from it. Returns 0 on success and not 0 otherwise.
3498 int scst_obtain_device_parameters(struct scst_device *dev);
3501 * Has to be put here open coded, because Linux doesn't have equivalent, which
3502 * allows exclusive wake ups of threads in LIFO order. We need it to let (yet)
3503 * unneeded threads sleep and not pollute CPU cache by their stacks.
3505 static inline void add_wait_queue_exclusive_head(wait_queue_head_t *q,
3508 unsigned long flags;
3510 wait->flags |= WQ_FLAG_EXCLUSIVE;
3511 spin_lock_irqsave(&q->lock, flags);
3512 __add_wait_queue(q, wait);
3513 spin_unlock_irqrestore(&q->lock, flags);
3516 #endif /* __SCST_H */