4 * Copyright (C) 2004 - 2008 Vladislav Bolkhovitin <vst@vlnb.net>
5 * Copyright (C) 2004 - 2005 Leonid Stoljar
6 * Copyright (C) 2007 - 2008 CMS Distribution Limited
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/proc_fs.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi.h>
35 #include <scst_const.h>
40 * Version numbers, the same as for the kernel.
42 * Changing it don't forget to change SCST_FIO_REV in scst_vdisk.c
43 * and FIO_REV in usr/fileio/common.h as well.
45 #define SCST_VERSION_CODE 0x01000100
46 #define SCST_VERSION(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + d)
47 #define SCST_VERSION_STRING "1.0.1"
48 #define SCST_INTERFACE_VERSION \
49 SCST_VERSION_STRING "$Revision$" SCST_CONST_VERSION
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
52 #ifndef RHEL_RELEASE_CODE
59 #define SCST_LOCAL_NAME "scst_lcl_drvr"
61 /*************************************************************
62 ** States of command processing state machine. At first,
63 ** "active" states, then - "passive" ones. This is to have
64 ** more efficient generated code of the corresponding
65 ** "switch" statements.
66 *************************************************************/
68 /* Internal parsing */
69 #define SCST_CMD_STATE_PRE_PARSE 0
71 /* Dev handler's parse() is going to be called */
72 #define SCST_CMD_STATE_DEV_PARSE 1
74 /* Allocation of the cmd's data buffer */
75 #define SCST_CMD_STATE_PREPARE_SPACE 2
77 /* Target driver's rdy_to_xfer() is going to be called */
78 #define SCST_CMD_STATE_RDY_TO_XFER 3
80 /* Target driver's pre_exec() is going to be called */
81 #define SCST_CMD_STATE_TGT_PRE_EXEC 4
83 /* Cmd is going to be sent for execution */
84 #define SCST_CMD_STATE_SEND_FOR_EXEC 5
86 /* Cmd is being checked if it should be executed locally */
87 #define SCST_CMD_STATE_LOCAL_EXEC 6
89 /* Cmd is ready for execution */
90 #define SCST_CMD_STATE_REAL_EXEC 7
92 /* Internal post-exec checks */
93 #define SCST_CMD_STATE_PRE_DEV_DONE 8
95 /* Internal MODE SELECT pages related checks */
96 #define SCST_CMD_STATE_MODE_SELECT_CHECKS 9
98 /* Dev handler's dev_done() is going to be called */
99 #define SCST_CMD_STATE_DEV_DONE 10
101 /* Target driver's xmit_response() is going to be called */
102 #define SCST_CMD_STATE_PRE_XMIT_RESP 11
104 /* Target driver's xmit_response() is going to be called */
105 #define SCST_CMD_STATE_XMIT_RESP 12
108 #define SCST_CMD_STATE_FINISHED 13
110 /* Internal cmd finished */
111 #define SCST_CMD_STATE_FINISHED_INTERNAL 14
113 #define SCST_CMD_STATE_LAST_ACTIVE (SCST_CMD_STATE_FINISHED_INTERNAL+100)
115 /* A cmd is created, but scst_cmd_init_done() not called */
116 #define SCST_CMD_STATE_INIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+1)
118 /* LUN translation (cmd->tgt_dev assignment) */
119 #define SCST_CMD_STATE_INIT (SCST_CMD_STATE_LAST_ACTIVE+2)
121 /* Allocation of the cmd's data buffer */
122 #define SCST_CMD_STATE_PREPROCESS_DONE (SCST_CMD_STATE_LAST_ACTIVE+3)
124 /* Waiting for data from the initiator (until scst_rx_data() called) */
125 #define SCST_CMD_STATE_DATA_WAIT (SCST_CMD_STATE_LAST_ACTIVE+4)
127 /* Waiting for CDB's execution finish */
128 #define SCST_CMD_STATE_REAL_EXECUTING (SCST_CMD_STATE_LAST_ACTIVE+5)
130 /* Waiting for response's transmission finish */
131 #define SCST_CMD_STATE_XMIT_WAIT (SCST_CMD_STATE_LAST_ACTIVE+6)
133 /*************************************************************
134 * Can be retuned instead of cmd's state by dev handlers'
135 * functions, if the command's state should be set by default
136 *************************************************************/
137 #define SCST_CMD_STATE_DEFAULT 500
139 /*************************************************************
140 * Can be retuned instead of cmd's state by dev handlers'
141 * functions, if it is impossible to complete requested
142 * task in atomic context. The cmd will be restarted in thread
144 *************************************************************/
145 #define SCST_CMD_STATE_NEED_THREAD_CTX 1000
147 /*************************************************************
148 * Can be retuned instead of cmd's state by dev handlers'
149 * parse function, if the cmd processing should be stopped
150 * for now. The cmd will be restarted by dev handlers itself.
151 *************************************************************/
152 #define SCST_CMD_STATE_STOP 1001
154 /*************************************************************
155 ** States of mgmt command processing state machine
156 *************************************************************/
158 /* LUN translation (mcmd->tgt_dev assignment) */
159 #define SCST_MCMD_STATE_INIT 0
161 /* Mgmt cmd is ready for processing */
162 #define SCST_MCMD_STATE_READY 1
164 /* Mgmt cmd is being executing */
165 #define SCST_MCMD_STATE_EXECUTING 2
167 /* Post check when affected commands done */
168 #define SCST_MCMD_STATE_POST_AFFECTED_CMDS_DONE 3
170 /* Target driver's task_mgmt_fn_done() is going to be called */
171 #define SCST_MCMD_STATE_DONE 4
173 /* The mcmd finished */
174 #define SCST_MCMD_STATE_FINISHED 5
176 /*************************************************************
177 ** Constants for "atomic" parameter of SCST's functions
178 *************************************************************/
179 #define SCST_NON_ATOMIC 0
180 #define SCST_ATOMIC 1
182 /*************************************************************
183 ** Values for pref_context parameter of scst_cmd_init_done(),
184 ** scst_rx_data(), scst_restart_cmd(), scst_tgt_cmd_done()
185 ** and scst_cmd_done()
186 *************************************************************/
188 enum scst_exec_context {
190 * Direct cmd's processing (i.e. regular function calls in the current
191 * context) sleeping is not allowed
193 SCST_CONTEXT_DIRECT_ATOMIC,
196 * Direct cmd's processing (i.e. regular function calls in the current
197 * context), sleeping is allowed, no restrictions
201 /* Tasklet or thread context required for cmd's processing */
202 SCST_CONTEXT_TASKLET,
204 /* Thread context required for cmd's processing */
208 * Context is the same as it was in previous call of the corresponding
209 * callback. For example, if dev handler's exec() does sync. data
210 * reading this value should be used for scst_cmd_done(). The same is
211 * true if scst_tgt_cmd_done() called directly from target driver's
212 * xmit_response(). Not allowed in scst_cmd_init_done() and
213 * scst_cmd_init_stage1_done().
218 /*************************************************************
219 ** Values for status parameter of scst_rx_data()
220 *************************************************************/
223 #define SCST_RX_STATUS_SUCCESS 0
226 * Data receiving finished with error, so set the sense and
227 * finish the command, including xmit_response() call
229 #define SCST_RX_STATUS_ERROR 1
232 * Data receiving finished with error and the sense is set,
233 * so finish the command, including xmit_response() call
235 #define SCST_RX_STATUS_ERROR_SENSE_SET 2
238 * Data receiving finished with fatal error, so finish the command,
239 * but don't call xmit_response()
241 #define SCST_RX_STATUS_ERROR_FATAL 3
243 /*************************************************************
244 ** Values for status parameter of scst_restart_cmd()
245 *************************************************************/
248 #define SCST_PREPROCESS_STATUS_SUCCESS 0
251 * Command's processing finished with error, so set the sense and
252 * finish the command, including xmit_response() call
254 #define SCST_PREPROCESS_STATUS_ERROR 1
257 * Command's processing finished with error and the sense is set,
258 * so finish the command, including xmit_response() call
260 #define SCST_PREPROCESS_STATUS_ERROR_SENSE_SET 2
263 * Command's processing finished with fatal error, so finish the command,
264 * but don't call xmit_response()
266 #define SCST_PREPROCESS_STATUS_ERROR_FATAL 3
268 /* Thread context requested */
269 #define SCST_PREPROCESS_STATUS_NEED_THREAD 4
271 /*************************************************************
272 ** Values for AEN functions
273 *************************************************************/
276 * SCSI Asynchronous Event. Parameter contains SCSI sense
277 * (Unit Attention). AENs generated only for 2 the following UAs:
278 * CAPACITY DATA HAS CHANGED and REPORTED LUNS DATA HAS CHANGED.
279 * Other UAs reported regularly as CHECK CONDITION status,
280 * because it doesn't look safe to report them using AENs, since
281 * reporting using AENs opens delivery race windows even in case of
284 #define SCST_AEN_SCSI 0
286 /*************************************************************
287 ** Allowed return/status codes for report_aen() callback and
288 ** scst_set_aen_delivery_status() function
289 *************************************************************/
292 #define SCST_AEN_RES_SUCCESS 0
295 #define SCST_AEN_RES_NOT_SUPPORTED -1
298 #define SCST_AEN_RES_FAILED -2
300 /*************************************************************
301 ** Allowed return codes for xmit_response(), rdy_to_xfer()
302 *************************************************************/
305 #define SCST_TGT_RES_SUCCESS 0
307 /* Internal device queue is full, retry again later */
308 #define SCST_TGT_RES_QUEUE_FULL -1
311 * It is impossible to complete requested task in atomic context.
312 * The cmd will be restarted in thread context.
314 #define SCST_TGT_RES_NEED_THREAD_CTX -2
317 * Fatal error, if returned by xmit_response() the cmd will
318 * be destroyed, if by any other function, xmit_response()
319 * will be called with HARDWARE ERROR sense data
321 #define SCST_TGT_RES_FATAL_ERROR -3
323 /*************************************************************
324 ** Allowed return codes for dev handler's exec()
325 *************************************************************/
327 /* The cmd is done, go to other ones */
328 #define SCST_EXEC_COMPLETED 0
330 /* The cmd should be sent to SCSI mid-level */
331 #define SCST_EXEC_NOT_COMPLETED 1
334 * Thread context is required to execute the command.
335 * Exec() will be called again in the thread context.
337 #define SCST_EXEC_NEED_THREAD 2
340 * Set if cmd is finished and there is status/sense to be sent.
341 * The status should be not sent (i.e. the flag not set) if the
342 * possibility to perform a command in "chunks" (i.e. with multiple
343 * xmit_response()/rdy_to_xfer()) is used (not implemented yet).
344 * Obsolete, use scst_cmd_get_is_send_status() instead.
346 #define SCST_TSC_FLAG_STATUS 0x2
348 /*************************************************************
349 ** Additional return code for dev handler's task_mgmt_fn()
350 *************************************************************/
352 /* Regular standard actions for the command should be done */
353 #define SCST_DEV_TM_NOT_COMPLETED 1
355 /*************************************************************
356 ** Session initialization phases
357 *************************************************************/
359 /* Set if session is being initialized */
360 #define SCST_SESS_IPH_INITING 0
362 /* Set if the session is successfully initialized */
363 #define SCST_SESS_IPH_SUCCESS 1
365 /* Set if the session initialization failed */
366 #define SCST_SESS_IPH_FAILED 2
368 /* Set if session is initialized and ready */
369 #define SCST_SESS_IPH_READY 3
371 /*************************************************************
372 ** Session shutdown phases
373 *************************************************************/
375 /* Set if session is initialized and ready */
376 #define SCST_SESS_SPH_READY 0
378 /* Set if session is shutting down */
379 #define SCST_SESS_SPH_SHUTDOWN 1
381 /*************************************************************
382 ** Cmd's async (atomic) flags
383 *************************************************************/
385 /* Set if the cmd is aborted and ABORTED sense will be sent as the result */
386 #define SCST_CMD_ABORTED 0
388 /* Set if the cmd is aborted by other initiator */
389 #define SCST_CMD_ABORTED_OTHER 1
391 /* Set if the cmd is aborted and counted in cmd_done_wait_count */
392 #define SCST_CMD_DONE_COUNTED 2
394 /* Set if no response should be sent to the target about this cmd */
395 #define SCST_CMD_NO_RESP 3
397 /* Set if the cmd is dead and can be destroyed at any time */
398 #define SCST_CMD_CAN_BE_DESTROYED 4
400 /*************************************************************
401 ** Tgt_dev's async. flags (tgt_dev_flags)
402 *************************************************************/
404 /* Set if tgt_dev has Unit Attention sense */
405 #define SCST_TGT_DEV_UA_PENDING 0
407 /* Set if tgt_dev is RESERVED by another session */
408 #define SCST_TGT_DEV_RESERVED 1
410 /* Set if the corresponding context is atomic */
411 #define SCST_TGT_DEV_AFTER_INIT_WR_ATOMIC 5
412 #define SCST_TGT_DEV_AFTER_INIT_OTH_ATOMIC 6
413 #define SCST_TGT_DEV_AFTER_RESTART_WR_ATOMIC 7
414 #define SCST_TGT_DEV_AFTER_RESTART_OTH_ATOMIC 8
415 #define SCST_TGT_DEV_AFTER_RX_DATA_ATOMIC 9
416 #define SCST_TGT_DEV_AFTER_EXEC_ATOMIC 10
418 #define SCST_TGT_DEV_CLUST_POOL 11
420 /*************************************************************
421 ** Name of the entry in /proc
422 *************************************************************/
423 #define SCST_PROC_ENTRY_NAME "scsi_tgt"
425 /*************************************************************
426 ** Activities suspending timeout
427 *************************************************************/
428 #define SCST_SUSPENDING_TIMEOUT (90 * HZ)
430 /*************************************************************
431 ** Kernel cache creation helper
432 *************************************************************/
434 #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
435 sizeof(struct __struct), __alignof__(struct __struct),\
436 (__flags), NULL, NULL)
439 /*************************************************************
440 ** Vlaid_mask constants for scst_analyze_sense()
441 *************************************************************/
443 #define SCST_SENSE_KEY_VALID 1
444 #define SCST_SENSE_ASC_VALID 2
445 #define SCST_SENSE_ASCQ_VALID 4
447 #define SCST_SENSE_ASCx_VALID (SCST_SENSE_ASC_VALID | \
448 SCST_SENSE_ASCQ_VALID)
450 #define SCST_SENSE_ALL_VALID (SCST_SENSE_KEY_VALID | \
451 SCST_SENSE_ASC_VALID | \
452 SCST_SENSE_ASCQ_VALID)
454 /*************************************************************
456 *************************************************************/
461 struct scst_mgmt_cmd;
464 struct scst_dev_type;
471 * SCST uses 64-bit numbers to represent LUN's internally. The value
472 * NO_SUCH_LUN is guaranteed to be different of every valid LUN.
474 #define NO_SUCH_LUN ((uint64_t)-1)
476 typedef enum dma_data_direction scst_data_direction;
478 enum scst_cdb_flags {
479 /* SCST_TRANSFER_LEN_TYPE_FIXED must be equiv 1 (FIXED_BIT in cdb) */
480 SCST_TRANSFER_LEN_TYPE_FIXED = 0x01,
481 SCST_SMALL_TIMEOUT = 0x02,
482 SCST_LONG_TIMEOUT = 0x04,
483 SCST_UNKNOWN_LENGTH = 0x08,
484 SCST_INFO_INVALID = 0x10,
485 SCST_VERIFY_BYTCHK_MISMATCH_ALLOWED = 0x20,
489 * Scsi_Target_Template: defines what functions a target driver will
490 * have to provide in order to work with the target mid-level.
491 * MUST HAVEs define functions that are expected to be in order to work.
492 * OPTIONAL says that there is a choice.
494 * Also, pay attention to the fact that a command is BLOCKING or NON-BLOCKING.
495 * NON-BLOCKING means that a function returns immediately and will not wait
496 * for actual data transfer to finish. Blocking in such command could have
497 * negative impact on overall system performance. If blocking is necessary,
498 * it is worth to consider creating dedicated thread(s) in target driver, to
499 * which the commands would be passed and which would perform blocking
500 * operations instead of SCST.
502 * If the function allowed to sleep or not is determined by its last
503 * argument, which is true, if sleeping is not allowed. In this case,
504 * if the function requires sleeping, it can return
505 * SCST_TGT_RES_NEED_THREAD_CTX, and it will be recalled in the thread context,
506 * where sleeping is allowed.
508 struct scst_tgt_template {
512 * SG tablesize allows to check whether scatter/gather can be used
518 * True, if this target adapter uses unchecked DMA onto an ISA bus.
520 unsigned unchecked_isa_dma:1;
523 * True, if this target adapter can benefit from using SG-vector
524 * clustering (i.e. smaller number of segments).
526 unsigned use_clustering:1;
529 * True, if this target adapter doesn't support SG-vector clustering
531 unsigned no_clustering:1;
534 * True, if corresponding function supports execution in
535 * the atomic (non-sleeping) context
537 unsigned xmit_response_atomic:1;
538 unsigned rdy_to_xfer_atomic:1;
540 /* True, if the template doesn't need the entry in /proc */
541 unsigned no_proc_entry:1;
544 * This function is equivalent to the SCSI
545 * queuecommand. The target should transmit the response
546 * buffer and the status in the scst_cmd struct.
547 * The expectation is that this executing this command is NON-BLOCKING.
549 * After the response is actually transmitted, the target
550 * should call the scst_tgt_cmd_done() function of the
551 * mid-level, which will allow it to free up the command.
552 * Returns one of the SCST_TGT_RES_* constants.
554 * Pay attention to "atomic" attribute of the cmd, which can be get
555 * by scst_cmd_atomic(): it is true if the function called in the
556 * atomic (non-sleeping) context.
560 int (*xmit_response) (struct scst_cmd *cmd);
563 * This function informs the driver that data
564 * buffer corresponding to the said command have now been
565 * allocated and it is OK to receive data for this command.
566 * This function is necessary because a SCSI target does not
567 * have any control over the commands it receives. Most lower
568 * level protocols have a corresponding function which informs
569 * the initiator that buffers have been allocated e.g., XFER_
570 * RDY in Fibre Channel. After the data is actually received
571 * the low-level driver needs to call scst_rx_data() in order to
572 * continue processing this command.
573 * Returns one of the SCST_TGT_RES_* constants.
574 * This command is expected to be NON-BLOCKING.
576 * Pay attention to "atomic" attribute of the cmd, which can be get
577 * by scst_cmd_atomic(): it is true if the function called in the
578 * atomic (non-sleeping) context.
582 int (*rdy_to_xfer) (struct scst_cmd *cmd);
585 * Called to notify the driver that the command is about to be freed.
586 * Necessary, because for aborted commands xmit_response() could not
587 * be called. Could be called on IRQ context.
591 void (*on_free_cmd) (struct scst_cmd *cmd);
594 * This function allows target driver to handle data buffer
595 * allocations on its own.
597 * Target driver doesn't have to always allocate buffer in this
598 * function, but if it decide to do it, it must check that
599 * scst_cmd_get_data_buff_alloced() returns 0, otherwise to avoid
600 * double buffer allocation and memory leaks alloc_data_buf() shall
603 * Shall return 0 in case of success or < 0 (preferrably -ENOMEM)
604 * in case of error, or > 0 if the regular SCST allocation should be
605 * done. In case of returning successfully,
606 * scst_cmd->tgt_data_buf_alloced will be set by SCST.
608 * It is possible that both target driver and dev handler request own
609 * memory allocation. In this case, data will be memcpy() between
610 * buffers, where necessary.
612 * If allocation in atomic context - cf. scst_cmd_atomic() - is not
613 * desired or fails and consequently < 0 is returned, this function
614 * will be re-called in thread context.
616 * Please note that the driver will have to handle itself all relevant
617 * details such as scatterlist setup, highmem, freeing the allocated
622 int (*alloc_data_buf) (struct scst_cmd *cmd);
625 * This function informs the driver that data
626 * buffer corresponding to the said command have now been
627 * allocated and other preprocessing tasks have been done.
628 * A target driver could need to do some actions at this stage.
629 * After the target driver done the needed actions, it shall call
630 * scst_restart_cmd() in order to continue processing this command.
632 * Called only if the cmd is queued using scst_cmd_init_stage1_done()
633 * instead of scst_cmd_init_done().
635 * Returns void, the result is expected to be returned using
636 * scst_restart_cmd().
638 * This command is expected to be NON-BLOCKING.
640 * Pay attention to "atomic" attribute of the cmd, which can be get
641 * by scst_cmd_atomic(): it is true if the function called in the
642 * atomic (non-sleeping) context.
646 void (*preprocessing_done) (struct scst_cmd *cmd);
649 * This function informs the driver that the said command is about
652 * Returns one of the SCST_PREPROCESS_* constants.
654 * This command is expected to be NON-BLOCKING.
656 * Pay attention to "atomic" attribute of the cmd, which can be get
657 * by scst_cmd_atomic(): it is true if the function called in the
658 * atomic (non-sleeping) context.
662 int (*pre_exec) (struct scst_cmd *cmd);
665 * This function informs the driver that all affected by the
666 * corresponding task management function commands have beed completed.
667 * No return value expected.
669 * This function is expected to be NON-BLOCKING.
671 * Called without any locks held from a thread context.
675 void (*task_mgmt_affected_cmds_done) (struct scst_mgmt_cmd *mgmt_cmd);
678 * This function informs the driver that the corresponding task
679 * management function has been completed, i.e. all the corresponding
680 * commands completed and freed. No return value expected.
682 * This function is expected to be NON-BLOCKING.
684 * Called without any locks held from a thread context.
686 * MUST HAVE if the target supports task management.
688 void (*task_mgmt_fn_done) (struct scst_mgmt_cmd *mgmt_cmd);
691 * This function should detect the target adapters that
692 * are present in the system. The function should return a value
693 * >= 0 to signify the number of detected target adapters.
694 * A negative value should be returned whenever there is
699 int (*detect) (struct scst_tgt_template *tgt_template);
702 * This function should free up the resources allocated to the device.
703 * The function should return 0 to indicate successful release
704 * or a negative value if there are some issues with the release.
705 * In the current version the return value is ignored.
709 int (*release) (struct scst_tgt *tgt);
712 * This function is used for Asynchronous Event Notifications.
714 * Returns one of the SCST_AEN_RES_* constants.
715 * After AEN is sent, target driver must call scst_aen_done() and,
716 * optionally, scst_set_aen_delivery_status().
718 * This command is expected to be NON-BLOCKING, but can sleep.
720 * MUST HAVE, if low-level protocol supports AENs.
722 int (*report_aen) (struct scst_aen *aen);
725 * Those functions can be used to export the driver's statistics and
726 * other infos to the world outside the kernel as well as to get some
727 * management commands from it.
731 int (*read_proc) (struct seq_file *seq, struct scst_tgt *tgt);
732 int (*write_proc) (char *buffer, char **start, off_t offset,
733 int length, int *eof, struct scst_tgt *tgt);
736 * Name of the template. Must be unique to identify
737 * the template. MUST HAVE
739 const char name[SCST_MAX_NAME];
742 * Number of additional threads to the pool of dedicated threads.
743 * Used if xmit_response() or rdy_to_xfer() is blocking.
744 * It is the target driver's duty to ensure that not more, than that
745 * number of threads, are blocked in those functions at any time.
749 /* Private, must be inited to 0 by memset() */
751 /* List of targets per template, protected by scst_mutex */
752 struct list_head tgt_list;
754 /* List entry of global templates list */
755 struct list_head scst_template_list_entry;
757 /* The pointer to the /proc directory entry */
758 struct proc_dir_entry *proc_tgt_root;
760 /* Device number in /proc */
764 struct scst_dev_type {
765 /* SCSI type of the supported device. MUST HAVE */
769 * True, if corresponding function supports execution in
770 * the atomic (non-sleeping) context
772 unsigned parse_atomic:1;
773 unsigned exec_atomic:1;
774 unsigned dev_done_atomic:1;
776 /* Set, if no /proc files should be automatically created by SCST */
780 * Should be set, if exec() is synchronous. This is a hint to SCST core
781 * to optimize commands order management.
783 unsigned exec_sync:1;
786 * Called to parse CDB from the cmd and initialize
787 * cmd->bufflen and cmd->data_direction (both - REQUIRED).
788 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
789 * if the next default state should be used, or
790 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
791 * context, but requires sleeping, or SCST_CMD_STATE_STOP if the
792 * command should not be further processed for now. In the
793 * SCST_CMD_STATE_NEED_THREAD_CTX case the function
794 * will be recalled in the thread context, where sleeping is allowed.
796 * Pay attention to "atomic" attribute of the cmd, which can be get
797 * by scst_cmd_atomic(): it is true if the function called in the
798 * atomic (non-sleeping) context.
802 int (*parse) (struct scst_cmd *cmd);
805 * Called to execute CDB. Useful, for instance, to implement
806 * data caching. The result of CDB execution is reported via
807 * cmd->scst_cmd_done() callback.
809 * - SCST_EXEC_COMPLETED - the cmd is done, go to other ones
810 * - SCST_EXEC_NEED_THREAD - thread context is required to execute
811 * the command. Exec() will be called again in the thread context.
812 * - SCST_EXEC_NOT_COMPLETED - the cmd should be sent to SCSI
815 * Pay attention to "atomic" attribute of the cmd, which can be get
816 * by scst_cmd_atomic(): it is true if the function called in the
817 * atomic (non-sleeping) context.
819 * If this function provides sync execution, you should set
820 * exec_sync flag and consider to setup dedicated threads by
821 * setting threads_num > 0.
823 * !! If this function is implemented, scst_check_local_events() !!
824 * !! shall be called inside it just before the actual command's !!
827 * OPTIONAL, if not set, the commands will be sent directly to SCSI
830 int (*exec) (struct scst_cmd *cmd);
833 * Called to notify dev handler about the result of cmd execution
834 * and perform some post processing. Cmd's fields is_send_status and
835 * resp_data_len should be set by this function, but SCST offers good
837 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
838 * if the next default state should be used, or
839 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
840 * context, but requires sleeping. In the last case, the function
841 * will be recalled in the thread context, where sleeping is allowed.
843 * Pay attention to "atomic" attribute of the cmd, which can be get
844 * by scst_cmd_atomic(): it is true if the function called in the
845 * atomic (non-sleeping) context.
847 int (*dev_done) (struct scst_cmd *cmd);
850 * Called to notify dev hander that the command is about to be freed.
851 * Could be called on IRQ context.
853 void (*on_free_cmd) (struct scst_cmd *cmd);
856 * Called to execute a task management command.
858 * - SCST_MGMT_STATUS_SUCCESS - the command is done with success,
859 * no firther actions required
860 * - The SCST_MGMT_STATUS_* error code if the command is failed and
861 * no further actions required
862 * - SCST_DEV_TM_NOT_COMPLETED - regular standard actions for the
863 * command should be done
865 * Called without any locks held from a thread context.
867 int (*task_mgmt_fn) (struct scst_mgmt_cmd *mgmt_cmd,
868 struct scst_tgt_dev *tgt_dev);
871 * Called when new device is attaching to the dev handler
872 * Returns 0 on success, error code otherwise.
874 int (*attach) (struct scst_device *dev);
876 /* Called when new device is detaching from the dev handler */
877 void (*detach) (struct scst_device *dev);
880 * Called when new tgt_dev (session) is attaching to the dev handler.
881 * Returns 0 on success, error code otherwise.
883 int (*attach_tgt) (struct scst_tgt_dev *tgt_dev);
885 /* Called when tgt_dev (session) is detaching from the dev handler */
886 void (*detach_tgt) (struct scst_tgt_dev *tgt_dev);
889 * Those functions can be used to export the handler's statistics and
890 * other infos to the world outside the kernel as well as to get some
891 * management commands from it.
895 int (*read_proc) (struct seq_file *seq, struct scst_dev_type *dev_type);
896 int (*write_proc) (char *buffer, char **start, off_t offset,
897 int length, int *eof, struct scst_dev_type *dev_type);
900 * Name of the dev handler. Must be unique. MUST HAVE.
902 * It's SCST_MAX_NAME + few more bytes to match scst_user expectations.
904 char name[SCST_MAX_NAME + 10];
907 * Number of dedicated threads. If 0 - no dedicated threads will
908 * be created, if <0 - creation of dedicated threads is prohibited.
912 struct module *module;
916 /* list entry in scst_dev_type_list */
917 struct list_head dev_type_list_entry;
919 /* The pointer to the /proc directory entry */
920 struct proc_dir_entry *proc_dev_type_root;
924 /* List of remote sessions per target, protected by scst_mutex */
925 struct list_head sess_list;
927 /* List entry of targets per template (tgts_list) */
928 struct list_head tgt_list_entry;
930 struct scst_tgt_template *tgtt; /* corresponding target template */
933 * Maximum SG table size. Needed here, since different cards on the
934 * same target template can have different SG table limitations.
938 /* Used for storage of target driver private stuff */
942 * The following fields used to store and retry cmds if
943 * target's internal queue is full, so the target is unable to accept
944 * the cmd returning QUEUE FULL
946 bool retry_timer_active;
947 struct timer_list retry_timer;
948 atomic_t finished_cmds;
949 int retry_cmds; /* protected by tgt_lock */
951 struct list_head retry_cmd_list; /* protected by tgt_lock */
953 /* Used to wait until session finished to unregister */
954 wait_queue_head_t unreg_waitQ;
956 /* Device number in /proc */
959 /* Name on the default security group ("Default_target_name") */
960 char *default_group_name;
963 /* Hash size and hash fn for hash based lun translation */
964 #define TGT_DEV_HASH_SHIFT 5
965 #define TGT_DEV_HASH_SIZE (1<<TGT_DEV_HASH_SHIFT)
966 #define HASH_VAL(_val) (_val & (TGT_DEV_HASH_SIZE - 1))
968 struct scst_session {
970 * Initialization phase, one of SCST_SESS_IPH_* constants, protected by
975 struct scst_tgt *tgt; /* corresponding target */
977 /* Used for storage of target driver private stuff */
981 * Hash list of tgt_dev's for this session, protected by scst_mutex
982 * and suspended activity
984 struct list_head sess_tgt_dev_list_hash[TGT_DEV_HASH_SIZE];
987 * List of cmds in this session. Used to find a cmd in the
988 * session. Protected by sess_list_lock.
990 struct list_head search_cmd_list;
992 atomic_t refcnt; /* get/put counter */
995 * Alive commands for this session. ToDo: make it part of the common
998 atomic_t sess_cmd_count;
1000 spinlock_t sess_list_lock; /* protects search_cmd_list, etc */
1002 /* Access control for this session and list entry there */
1003 struct scst_acg *acg;
1005 /* List entry for the sessions list inside ACG */
1006 struct list_head acg_sess_list_entry;
1008 /* Name of attached initiator */
1009 const char *initiator_name;
1011 /* List entry of sessions per target */
1012 struct list_head sess_list_entry;
1014 /* List entry for the list that keeps session, waiting for the init */
1015 struct list_head sess_init_list_entry;
1018 * List entry for the list that keeps session, waiting for the shutdown
1020 struct list_head sess_shut_list_entry;
1023 * Lists of deffered during session initialization commands.
1024 * Protected by sess_list_lock.
1026 struct list_head init_deferred_cmd_list;
1027 struct list_head init_deferred_mcmd_list;
1030 * Shutdown phase, one of SCST_SESS_SPH_* constants, unprotected.
1031 * Async. relating to init_phase, must be a separate variable, because
1032 * session could be unregistered before async. registration is finished.
1034 unsigned long shut_phase;
1036 /* Used if scst_unregister_session() called in wait mode */
1037 struct completion *shutdown_compl;
1040 * Functions and data for user callbacks from scst_register_session()
1041 * and scst_unregister_session()
1043 void *reg_sess_data;
1044 void (*init_result_fn) (struct scst_session *sess, void *data,
1046 void (*unreg_done_fn) (struct scst_session *sess);
1048 #ifdef CONFIG_SCST_MEASURE_LATENCY /* must be last */
1049 spinlock_t meas_lock;
1050 uint64_t scst_time, processing_time;
1051 unsigned int processed_cmds;
1055 struct scst_cmd_lists {
1056 spinlock_t cmd_list_lock;
1057 struct list_head active_cmd_list;
1058 wait_queue_head_t cmd_list_waitQ;
1059 struct list_head lists_list_entry;
1063 /* List entry for below *_cmd_lists */
1064 struct list_head cmd_list_entry;
1066 /* Pointer to lists of commands with the lock */
1067 struct scst_cmd_lists *cmd_lists;
1071 struct scst_session *sess; /* corresponding session */
1073 /* Cmd state, one of SCST_CMD_STATE_* constants */
1076 /*************************************************************
1078 *************************************************************/
1081 * Set if expected_sn should be incremented, i.e. cmd was sent
1084 unsigned int sent_for_exec:1;
1086 /* Set if the cmd's action is completed */
1087 unsigned int completed:1;
1089 /* Set if we should ignore Unit Attention in scst_check_sense() */
1090 unsigned int ua_ignore:1;
1092 /* Set if cmd is being processed in atomic context */
1093 unsigned int atomic:1;
1095 /* Set if this command was sent in double UA possible state */
1096 unsigned int double_ua_possible:1;
1098 /* Set if this command contains status */
1099 unsigned int is_send_status:1;
1101 /* Set if cmd is being retried */
1102 unsigned int retry:1;
1104 /* Set if cmd is internally generated */
1105 unsigned int internal:1;
1107 /* Set if the device was blocked by scst_inc_on_dev_cmd() (for debug) */
1108 unsigned int inc_blocking:1;
1110 /* Set if the device should be unblocked after cmd's finish */
1111 unsigned int needs_unblocking:1;
1113 /* Set if scst_dec_on_dev_cmd() call is needed on the cmd's finish */
1114 unsigned int dec_on_dev_needed:1;
1117 * Set if the target driver wants to alloc data buffers on its own.
1118 * In this case alloc_data_buf() must be provided in the target driver
1121 unsigned int tgt_need_alloc_data_buf:1;
1124 * Set by SCST if the custom data buffer allocation by the target driver
1127 unsigned int tgt_data_buf_alloced:1;
1129 /* Set if custom data buffer allocated by dev handler */
1130 unsigned int dh_data_buf_alloced:1;
1132 /* Set if the target driver called scst_set_expected() */
1133 unsigned int expected_values_set:1;
1136 * Set if the cmd was delayed by task management debugging code.
1137 * Used only if CONFIG_SCST_DEBUG_TM is on.
1139 unsigned int tm_dbg_delayed:1;
1142 * Set if the cmd must be ignored by task management debugging code.
1143 * Used only if CONFIG_SCST_DEBUG_TM is on.
1145 unsigned int tm_dbg_immut:1;
1148 * Set if the SG buffer was modified by scst_set_resp_data_len()
1150 unsigned int sg_buff_modified:1;
1153 * Set if scst_cmd_init_stage1_done() called and the target
1154 * want that preprocessing_done() will be called
1156 unsigned int preprocessing_only:1;
1158 /* Set if cmd's SN was set */
1159 unsigned int sn_set:1;
1161 /* Set if hq_cmd_count was incremented */
1162 unsigned int hq_cmd_inced:1;
1165 * Set if scst_cmd_init_stage1_done() called and the target wants
1166 * that the SN for the cmd won't be assigned until scst_restart_cmd()
1168 unsigned int set_sn_on_restart_cmd:1;
1170 /* Set if the cmd's must not use sgv cache for data buffer */
1171 unsigned int no_sgv:1;
1174 * Set if target driver may need to call dma_sync_sg() or similar
1175 * function before transferring cmd' data to the target device
1178 unsigned int may_need_dma_sync:1;
1180 /* Set if the cmd was done or aborted out of its SN */
1181 unsigned int out_of_sn:1;
1183 /* Set if increment expected_sn in cmd->scst_cmd_done() */
1184 unsigned int inc_expected_sn_on_done:1;
1186 /* Set if tgt_sn field is valid */
1187 unsigned int tgt_sn_set:1;
1189 /* Set if cmd is done */
1190 unsigned int done:1;
1192 /* Set if cmd is finished */
1193 unsigned int finished:1;
1195 /**************************************************************/
1197 unsigned long cmd_flags; /* cmd's async flags */
1199 /* Keeps status of cmd's status/data delivery to remote initiator */
1200 int delivery_status;
1202 struct scst_tgt_template *tgtt; /* to save extra dereferences */
1203 struct scst_tgt *tgt; /* to save extra dereferences */
1204 struct scst_device *dev; /* to save extra dereferences */
1206 struct scst_tgt_dev *tgt_dev; /* corresponding device for this cmd */
1208 uint64_t lun; /* LUN for this cmd */
1210 unsigned long start_time;
1212 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1213 struct scsi_request *scsi_req; /* SCSI request */
1216 /* List entry for tgt_dev's SN related lists */
1217 struct list_head sn_cmd_list_entry;
1219 /* Cmd's serial number, used to execute cmd's in order of arrival */
1222 /* The corresponding sn_slot in tgt_dev->sn_slots */
1225 /* List entry for session's search_cmd_list */
1226 struct list_head search_cmd_list_entry;
1229 * Used to found the cmd by scst_find_cmd_by_tag(). Set by the
1230 * target driver on the cmd's initialization time
1234 uint32_t tgt_sn; /* SN set by target driver (for TM purposes) */
1236 /* CDB and its len */
1237 uint8_t cdb[SCST_MAX_CDB_SIZE];
1238 unsigned short cdb_len;
1239 unsigned short ext_cdb_len;
1242 enum scst_cdb_flags op_flags;
1243 const char *op_name;
1245 enum scst_cmd_queue_type queue_type;
1247 int timeout; /* CDB execution timeout in seconds */
1248 int retries; /* Amount of retries that will be done by SCSI mid-level */
1250 /* SCSI data direction, one of SCST_DATA_* constants */
1251 scst_data_direction data_direction;
1253 /* Remote initiator supplied values, if any */
1254 scst_data_direction expected_data_direction;
1255 int expected_transfer_len;
1258 * Cmd data length. Could be different from bufflen for commands like
1259 * VERIFY, which transfer different amount of data (if any), than
1264 /* Completition routine */
1265 void (*scst_cmd_done) (struct scst_cmd *cmd, int next_state,
1266 enum scst_exec_context pref_context);
1268 struct sgv_pool_obj *sgv; /* sgv object */
1270 int bufflen; /* cmd buffer length */
1271 struct scatterlist *sg; /* cmd data buffer SG vector */
1272 int sg_cnt; /* SG segments count */
1274 /* scst_get_sg_buf_[first,next]() support */
1275 int get_sg_buf_entry_num;
1278 * Response data length in data buffer. This field must not be set
1279 * directly, use scst_set_resp_data_len() for that
1284 * Used if both target driver and dev handler request own memory
1285 * allocation. In other cases, both are equal to sg and sg_cnt
1288 * If target driver requests own memory allocations, it MUST use
1289 * functions scst_cmd_get_tgt_sg*() to get sg and sg_cnt! Otherwise,
1290 * it may use functions scst_cmd_get_sg*().
1292 struct scatterlist *tgt_sg;
1296 * The status fields in case of errors must be set using
1297 * scst_set_cmd_error_status()!
1299 uint8_t status; /* status byte from target device */
1300 uint8_t msg_status; /* return status from host adapter itself */
1301 uint8_t host_status; /* set by low-level driver to indicate status */
1302 uint8_t driver_status; /* set by mid-level */
1304 uint8_t *sense; /* pointer to sense buffer */
1306 /* Used for storage of target driver private stuff */
1309 /* Used for storage of dev handler private stuff */
1313 * Used to restore the SG vector if it was modified by
1314 * scst_set_resp_data_len()
1316 int orig_sg_cnt, orig_sg_entry, orig_entry_len;
1318 /* Used to retry commands in case of double UA */
1319 int dbl_ua_orig_resp_data_len, dbl_ua_orig_data_direction;
1321 /* List corresponding mgmt cmd, if any, protected by sess_list_lock */
1322 struct list_head mgmt_cmd_list;
1324 /* List entry for dev's blocked_cmd_list */
1325 struct list_head blocked_cmd_list_entry;
1327 struct scst_cmd *orig_cmd; /* Used to issue REQUEST SENSE */
1329 #ifdef CONFIG_SCST_MEASURE_LATENCY /* must be last */
1330 uint64_t start, pre_exec_finish, post_exec_start;
1334 struct scst_rx_mgmt_params {
1342 unsigned char tag_set;
1343 unsigned char lun_set;
1344 unsigned char cmd_sn_set;
1347 struct scst_mgmt_cmd_stub {
1348 struct scst_mgmt_cmd *mcmd;
1350 /* List entry in cmd->mgmt_cmd_list */
1351 struct list_head cmd_mgmt_cmd_list_entry;
1354 struct scst_mgmt_cmd {
1355 /* List entry for *_mgmt_cmd_list */
1356 struct list_head mgmt_cmd_list_entry;
1358 struct scst_session *sess;
1360 /* Mgmt cmd state, one of SCST_MCMD_STATE_* constants */
1365 unsigned int completed:1; /* set, if the mcmd is completed */
1366 /* Set if device(s) should be unblocked after mcmd's finish */
1367 unsigned int needs_unblocking:1;
1368 unsigned int lun_set:1; /* set, if lun field is valid */
1369 unsigned int cmd_sn_set:1; /* set, if cmd_sn field is valid */
1370 /* set, if scst_mgmt_affected_cmds_done was called */
1371 unsigned int affected_cmds_done_called:1;
1374 * Number of commands to finish before sending response,
1375 * protected by scst_mcmd_lock
1377 int cmd_finish_wait_count;
1380 * Number of commands to complete (done) before resetting reservation,
1381 * protected by scst_mcmd_lock
1383 int cmd_done_wait_count;
1385 /* Number of completed commands, protected by scst_mcmd_lock */
1386 int completed_cmd_count;
1388 uint64_t lun; /* LUN for this mgmt cmd */
1389 /* or (and for iSCSI) */
1390 uint64_t tag; /* tag of the corresponding cmd */
1392 uint32_t cmd_sn; /* affected command's highest SN */
1394 /* corresponding cmd (to be aborted, found by tag) */
1395 struct scst_cmd *cmd_to_abort;
1397 /* corresponding device for this mgmt cmd (found by lun) */
1398 struct scst_tgt_dev *mcmd_tgt_dev;
1400 /* completition status, one of the SCST_MGMT_STATUS_* constants */
1403 /* Used for storage of target driver private stuff */
1407 struct scst_device {
1408 struct scst_dev_type *handler; /* corresponding dev handler */
1410 struct scst_mem_lim dev_mem_lim;
1412 unsigned short type; /* SCSI type of the device */
1414 /*************************************************************
1415 ** Dev's flags. Updates serialized by dev_lock or suspended
1417 *************************************************************/
1419 /* Set if dev is RESERVED */
1420 unsigned short dev_reserved:1;
1422 /* Set if double reset UA is possible */
1423 unsigned short dev_double_ua_possible:1;
1425 /**************************************************************/
1427 /*************************************************************
1428 ** Dev's control mode page related values. Updates serialized
1429 ** by scst_block_dev(). It's long to not interfere with the
1431 *************************************************************/
1433 unsigned long queue_alg:4;
1434 unsigned long tst:3;
1435 unsigned long tas:1;
1436 unsigned long swp:1;
1439 * Set if device implements own ordered commands management. If not set
1440 * and queue_alg is SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER,
1441 * expected_sn will be incremented only after commands finished.
1443 unsigned long has_own_order_mgmt:1;
1445 /**************************************************************/
1447 /* Used for storage of dev handler private stuff */
1450 /* Used to translate SCSI's cmd to SCST's cmd */
1451 struct gendisk *rq_disk;
1453 /* Corresponding real SCSI device, could be NULL for virtual devices */
1454 struct scsi_device *scsi_dev;
1456 /* Pointer to lists of commands with the lock */
1457 struct scst_cmd_lists *p_cmd_lists;
1459 /* Lists of commands with lock, if dedicated threads are used */
1460 struct scst_cmd_lists cmd_lists;
1462 /* How many cmds alive on this dev */
1463 atomic_t dev_cmd_count;
1465 /* How many write cmds alive on this dev. Temporary, ToDo */
1466 atomic_t write_cmd_count;
1468 spinlock_t dev_lock; /* device lock */
1471 * How many times device was blocked for new cmds execution.
1472 * Protected by dev_lock
1477 * How many there are "on_dev" commands, i.e. ones those are being
1478 * executed by the underlying SCSI/virtual device.
1480 atomic_t on_dev_count;
1482 struct list_head blocked_cmd_list; /* protected by dev_lock */
1484 /* Used to wait for requested amount of "on_dev" commands */
1485 wait_queue_head_t on_dev_waitQ;
1487 /* A list entry used during TM, protected by scst_mutex */
1488 struct list_head tm_dev_list_entry;
1490 /* Virtual device internal ID */
1493 /* Pointer to virtual device name, for convenience only */
1494 const char *virt_name;
1496 /* List entry in global devices list */
1497 struct list_head dev_list_entry;
1500 * List of tgt_dev's, one per session, protected by scst_mutex or
1501 * dev_lock for reads and both for writes
1503 struct list_head dev_tgt_dev_list;
1505 /* List of acg_dev's, one per acg, protected by scst_mutex */
1506 struct list_head dev_acg_dev_list;
1508 /* List of dedicated threads, protected by scst_mutex */
1509 struct list_head threads_list;
1516 * Used to store threads local tgt_dev specific data
1518 struct scst_thr_data_hdr {
1519 /* List entry in tgt_dev->thr_data_list */
1520 struct list_head thr_data_list_entry;
1521 struct task_struct *owner_thr; /* the owner thread */
1523 /* Function that will be called on the tgt_dev destruction */
1524 void (*free_fn) (struct scst_thr_data_hdr *data);
1528 * Used to store per-session specific device information
1530 struct scst_tgt_dev {
1531 /* List entry in sess->sess_tgt_dev_list_hash */
1532 struct list_head sess_tgt_dev_list_entry;
1534 struct scst_device *dev; /* to save extra dereferences */
1535 uint64_t lun; /* to save extra dereferences */
1538 struct sgv_pool *pool;
1541 unsigned long tgt_dev_flags; /* tgt_dev's async flags */
1543 /* Used for storage of dev handler private stuff */
1546 /* How many cmds alive on this dev in this session */
1547 atomic_t tgt_dev_cmd_count;
1550 * Used to execute cmd's in order of arrival, honoring SCSI task
1553 * Protected by sn_lock, except expected_sn, which is protected by
1554 * itself. Curr_sn must have the same size as expected_sn to
1555 * overflow simultaneously.
1559 unsigned long expected_sn;
1560 unsigned long curr_sn;
1562 struct list_head deferred_cmd_list;
1563 struct list_head skipped_sn_list;
1566 * Set if the prev cmd was ORDERED. Size must allow unprotected
1567 * modifications independant to the neighbour fields.
1569 unsigned long prev_cmd_ordered;
1571 int num_free_sn_slots; /* if it's <0, then all slots are busy */
1572 atomic_t *cur_sn_slot;
1573 atomic_t sn_slots[15];
1575 /* List of scst_thr_data_hdr and lock */
1576 spinlock_t thr_data_lock;
1577 struct list_head thr_data_list;
1579 spinlock_t tgt_dev_lock; /* per-session device lock */
1581 /* List of UA's for this device, protected by tgt_dev_lock */
1582 struct list_head UA_list;
1584 struct scst_session *sess; /* corresponding session */
1585 struct scst_acg_dev *acg_dev; /* corresponding acg_dev */
1587 /* list entry in dev->dev_tgt_dev_list */
1588 struct list_head dev_tgt_dev_list_entry;
1590 /* internal tmp list entry */
1591 struct list_head extra_tgt_dev_list_entry;
1595 * Used to store ACG-specific device information, like LUN
1597 struct scst_acg_dev {
1598 struct scst_device *dev; /* corresponding device */
1599 uint64_t lun; /* device's LUN in this acg */
1600 unsigned int rd_only_flag:1; /* if != 0, then read only */
1601 struct scst_acg *acg; /* parent acg */
1603 /* list entry in dev->dev_acg_dev_list */
1604 struct list_head dev_acg_dev_list_entry;
1606 /* list entry in acg->acg_dev_list */
1607 struct list_head acg_dev_list_entry;
1611 * ACG - access control group. Used to store group related
1612 * control information.
1615 /* List of acg_dev's in this acg, protected by scst_mutex */
1616 struct list_head acg_dev_list;
1618 /* List of attached sessions, protected by scst_mutex */
1619 struct list_head acg_sess_list;
1621 /* List of attached acn's, protected by scst_mutex */
1622 struct list_head acn_list;
1624 /* List entry in scst_acg_list */
1625 struct list_head scst_acg_list_entry;
1627 /* Name of this acg */
1628 const char *acg_name;
1630 /* The pointer to the /proc directory entry */
1631 struct proc_dir_entry *acg_proc_root;
1635 * ACN - access control name. Used to store names, by which
1636 * incoming sessions will be assigned to appropriate ACG.
1639 /* Initiator's name */
1641 /* List entry in acg->acn_list */
1642 struct list_head acn_list_entry;
1646 * Used to store per-session UNIT ATTENTIONs
1648 struct scst_tgt_dev_UA {
1649 /* List entry in tgt_dev->UA_list */
1650 struct list_head UA_list_entry;
1652 /* Set if UA is global for session */
1653 unsigned int global_UA:1;
1655 /* Unit Attention sense */
1656 uint8_t UA_sense_buffer[SCST_SENSE_BUFFERSIZE];
1659 /* Used to deliver AENs */
1661 int event_fn; /* AEN fn */
1663 struct scst_session *sess; /* corresponding session */
1664 uint64_t lun; /* corresponding LUN in SCSI form */
1670 uint8_t aen_sense[SCST_STANDARD_SENSE_LEN];
1674 /* Keeps status of AEN's delivery to remote initiator */
1675 int delivery_status;
1678 #ifndef smp_mb__after_set_bit
1679 /* There is no smp_mb__after_set_bit() in the kernel */
1680 #define smp_mb__after_set_bit() smp_mb()
1684 * Registers target template
1685 * Returns 0 on success or appropriate error code otherwise
1687 int __scst_register_target_template(struct scst_tgt_template *vtt,
1688 const char *version);
1689 static inline int scst_register_target_template(struct scst_tgt_template *vtt)
1691 return __scst_register_target_template(vtt, SCST_INTERFACE_VERSION);
1695 * Unregisters target template
1697 void scst_unregister_target_template(struct scst_tgt_template *vtt);
1700 * Registers and returns target adapter
1701 * Returns new target structure on success or NULL otherwise.
1703 * If parameter "target_name" isn't NULL, then security group with name
1704 * "Default_##target_name", if created, will be used as the default
1705 * instead of "Default" one for all initiators not assigned to any other group.
1707 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
1708 const char *target_name);
1711 * Unregisters target adapter
1713 void scst_unregister(struct scst_tgt *tgt);
1716 * Registers and returns a session
1718 * Returns new session on success or NULL otherwise
1722 * atomic - true, if the function called in the atomic context. If false,
1723 * this function will block until the session registration is completed.
1724 * initiator_name - remote initiator's name, any NULL-terminated string,
1725 * e.g. iSCSI name, which used as the key to found appropriate access
1726 * control group. Could be NULL, then "default" group is used.
1727 * The groups are set up via /proc interface.
1728 * data - any target driver supplied data
1729 * result_fn - pointer to the function that will be
1730 * asynchronously called when session initialization finishes.
1731 * Can be NULL. Parameters:
1733 * - data - target driver supplied to scst_register_session() data
1734 * - result - session initialization result, 0 on success or
1735 * appropriate error code otherwise
1737 * Note: A session creation and initialization is a complex task,
1738 * which requires sleeping state, so it can't be fully done
1739 * in interrupt context. Therefore the "bottom half" of it, if
1740 * scst_register_session() is called from atomic context, will be
1741 * done in SCST thread context. In this case scst_register_session()
1742 * will return not completely initialized session, but the target
1743 * driver can supply commands to this session via scst_rx_cmd().
1744 * Those commands processing will be delayed inside SCST until
1745 * the session initialization is finished, then their processing
1746 * will be restarted. The target driver will be notified about
1747 * finish of the session initialization by function result_fn().
1748 * On success the target driver could do nothing, but if the
1749 * initialization fails, the target driver must ensure that
1750 * no more new commands being sent or will be sent to SCST after
1751 * result_fn() returns. All already sent to SCST commands for
1752 * failed session will be returned in xmit_response() with BUSY status.
1753 * In case of failure the driver shall call scst_unregister_session()
1754 * inside result_fn(), it will NOT be called automatically.
1756 struct scst_session *scst_register_session(struct scst_tgt *tgt, int atomic,
1757 const char *initiator_name, void *data,
1758 void (*result_fn) (struct scst_session *sess, void *data, int result));
1761 * Unregisters a session.
1763 * sess - session to be unregistered
1764 * wait - if true, instructs to wait until all commands, which
1765 * currently is being executed and belonged to the session, finished.
1766 * Otherwise, target driver should be prepared to receive
1767 * xmit_response() for the session's command after
1768 * scst_unregister_session() returns.
1769 * unreg_done_fn - pointer to the function that will be
1770 * asynchronously called when the last session's command finishes and
1771 * the session is about to be completely freed. Can be NULL.
1777 * - All outstanding commands will be finished regularly. After
1778 * scst_unregister_session() returned no new commands must be sent to
1779 * SCST via scst_rx_cmd().
1781 * - The caller must ensure that no scst_rx_cmd() or scst_rx_mgmt_fn_*() is
1782 * called in paralell with scst_unregister_session().
1784 * - Can be called before result_fn() of scst_register_session() called,
1785 * i.e. during the session registration/initialization.
1787 * - It is highly recommended to call scst_unregister_session() as soon as it
1788 * gets clear that session will be unregistered and not to wait until all
1789 * related commands finished. This function provides the wait functionality,
1790 * but it also starts recovering stuck commands, if there are any.
1791 * Otherwise, your target driver could wait for those commands forever.
1793 void scst_unregister_session(struct scst_session *sess, int wait,
1794 void (*unreg_done_fn) (struct scst_session *sess));
1797 * Registers dev handler driver
1798 * Returns 0 on success or appropriate error code otherwise
1800 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
1801 const char *version);
1802 static inline int scst_register_dev_driver(struct scst_dev_type *dev_type)
1804 return __scst_register_dev_driver(dev_type, SCST_INTERFACE_VERSION);
1808 * Unregisters dev handler driver
1810 void scst_unregister_dev_driver(struct scst_dev_type *dev_type);
1813 * Registers dev handler driver for virtual devices (eg VDISK)
1814 * Returns 0 on success or appropriate error code otherwise
1816 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
1817 const char *version);
1818 static inline int scst_register_virtual_dev_driver(
1819 struct scst_dev_type *dev_type)
1821 return __scst_register_virtual_dev_driver(dev_type,
1822 SCST_INTERFACE_VERSION);
1826 * Unregisters dev handler driver for virtual devices
1828 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type);
1831 * Creates and sends new command to SCST.
1832 * Must not be called in parallel with scst_unregister_session() for the
1833 * same sess. Returns the command on success or NULL otherwise
1835 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
1836 const uint8_t *lun, int lun_len, const uint8_t *cdb,
1837 int cdb_len, int atomic);
1840 * Notifies SCST that the driver finished its part of the command
1841 * initialization, and the command is ready for execution.
1842 * The second argument sets preferred command execition context.
1843 * See SCST_CONTEXT_* constants for details.
1847 * If cmd->set_sn_on_restart_cmd not set, this function, as well as
1848 * scst_cmd_init_stage1_done() and scst_restart_cmd(), must not be
1849 * called simultaneously for the same session (more precisely,
1850 * for the same session/LUN, i.e. tgt_dev), i.e. they must be
1851 * somehow externally serialized. This is needed to have lock free fast path in
1852 * scst_cmd_set_sn(). For majority of targets those functions are naturally
1853 * serialized by the single source of commands. Only iSCSI immediate commands
1854 * with multiple connections per session seems to be an exception. For it, some
1855 * mutex/lock shall be used for the serialization.
1857 void scst_cmd_init_done(struct scst_cmd *cmd,
1858 enum scst_exec_context pref_context);
1861 * Notifies SCST that the driver finished the first stage of the command
1862 * initialization, and the command is ready for execution, but after
1863 * SCST done the command's preprocessing preprocessing_done() function
1864 * should be called. The second argument sets preferred command execition
1865 * context. See SCST_CONTEXT_* constants for details.
1867 * See also scst_cmd_init_done() comment for the serialization requirements.
1869 static inline void scst_cmd_init_stage1_done(struct scst_cmd *cmd,
1870 enum scst_exec_context pref_context, int set_sn)
1872 cmd->preprocessing_only = 1;
1873 cmd->set_sn_on_restart_cmd = !set_sn;
1874 scst_cmd_init_done(cmd, pref_context);
1878 * Notifies SCST that the driver finished its part of the command's
1879 * preprocessing and it is ready for further processing.
1880 * The second argument sets data receiving completion status
1881 * (see SCST_PREPROCESS_STATUS_* constants for details)
1882 * The third argument sets preferred command execition context
1883 * (see SCST_CONTEXT_* constants for details).
1885 * See also scst_cmd_init_done() comment for the serialization requirements.
1887 void scst_restart_cmd(struct scst_cmd *cmd, int status,
1888 enum scst_exec_context pref_context);
1891 * Notifies SCST that the driver received all the necessary data
1892 * and the command is ready for further processing.
1893 * The second argument sets data receiving completion status
1894 * (see SCST_RX_STATUS_* constants for details)
1895 * The third argument sets preferred command execition context
1896 * (see SCST_CONTEXT_* constants for details)
1898 void scst_rx_data(struct scst_cmd *cmd, int status,
1899 enum scst_exec_context pref_context);
1902 * Notifies SCST that the driver sent the response and the command
1903 * can be freed now. Don't forget to set the delivery status, if it
1904 * isn't success, using scst_set_delivery_status() before calling
1905 * this function. The third argument sets preferred command execition
1906 * context (see SCST_CONTEXT_* constants for details)
1908 void scst_tgt_cmd_done(struct scst_cmd *cmd,
1909 enum scst_exec_context pref_context);
1912 * Creates new management command sends it for execution.
1913 * Must not be called in parallel with scst_unregister_session() for the
1914 * same sess. Returns 0 for success, error code otherwise.
1916 int scst_rx_mgmt_fn(struct scst_session *sess,
1917 const struct scst_rx_mgmt_params *params);
1920 * Creates new management command using tag and sends it for execution.
1921 * Can be used for SCST_ABORT_TASK only.
1922 * Must not be called in parallel with scst_unregister_session() for the
1923 * same sess. Returns 0 for success, error code otherwise.
1925 * Obsolete in favor of scst_rx_mgmt_fn()
1927 static inline int scst_rx_mgmt_fn_tag(struct scst_session *sess, int fn,
1928 uint64_t tag, int atomic, void *tgt_priv)
1930 struct scst_rx_mgmt_params params;
1932 BUG_ON(fn != SCST_ABORT_TASK);
1934 memset(¶ms, 0, sizeof(params));
1938 params.atomic = atomic;
1939 params.tgt_priv = tgt_priv;
1940 return scst_rx_mgmt_fn(sess, ¶ms);
1944 * Creates new management command using LUN and sends it for execution.
1945 * Currently can be used for any fn, except SCST_ABORT_TASK.
1946 * Must not be called in parallel with scst_unregister_session() for the
1947 * same sess. Returns 0 for success, error code otherwise.
1949 * Obsolete in favor of scst_rx_mgmt_fn()
1951 static inline int scst_rx_mgmt_fn_lun(struct scst_session *sess, int fn,
1952 const uint8_t *lun, int lun_len, int atomic, void *tgt_priv)
1954 struct scst_rx_mgmt_params params;
1956 BUG_ON(fn == SCST_ABORT_TASK);
1958 memset(¶ms, 0, sizeof(params));
1961 params.lun_len = lun_len;
1963 params.atomic = atomic;
1964 params.tgt_priv = tgt_priv;
1965 return scst_rx_mgmt_fn(sess, ¶ms);
1969 * Provides various info about command's CDB.
1971 * Returns: 0 on success, <0 if command is unknown, >0 if command is invalid.
1973 int scst_get_cdb_info(struct scst_cmd *cmd);
1976 * Set error SCSI status in the command and prepares it for returning it
1978 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status);
1981 * Set error in the command and fill the sense buffer
1983 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq);
1986 * Sets BUSY or TASK QUEUE FULL status
1988 void scst_set_busy(struct scst_cmd *cmd);
1991 * Sets initial Unit Attention for sess, replacing default scst_sense_reset_UA
1993 void scst_set_initial_UA(struct scst_session *sess, int key, int asc, int ascq);
1996 * Notifies SCST core that dev changed its capacity
1998 void scst_capacity_data_changed(struct scst_device *dev);
2001 * Finds a command based on the supplied tag comparing it with one
2002 * that previously set by scst_cmd_set_tag().
2003 * Returns the command on success or NULL otherwise
2005 struct scst_cmd *scst_find_cmd_by_tag(struct scst_session *sess, uint64_t tag);
2008 * Finds a command based on user supplied data and comparision
2009 * callback function, that should return true, if the command is found.
2010 * Returns the command on success or NULL otherwise
2012 struct scst_cmd *scst_find_cmd(struct scst_session *sess, void *data,
2013 int (*cmp_fn) (struct scst_cmd *cmd,
2017 * Translates SCST's data direction to DMA one
2019 static inline int scst_to_dma_dir(int scst_dir)
2025 * Translates SCST data direction to DMA one from the perspective
2026 * of the target device.
2028 static inline int scst_to_tgt_dma_dir(int scst_dir)
2030 if (scst_dir == SCST_DATA_WRITE)
2031 return DMA_FROM_DEVICE;
2032 else if (scst_dir == SCST_DATA_READ)
2033 return DMA_TO_DEVICE;
2038 * Returns 1, if cmd's CDB is locally handled by SCST and 0 otherwise.
2039 * Dev handlers parse() and dev_done() not called for such commands.
2041 static inline int scst_is_cmd_local(struct scst_cmd *cmd)
2044 switch (cmd->cdb[0]) {
2052 * Registers a virtual device.
2054 * dev_type - the device's device handler
2055 * dev_name - the new device name, NULL-terminated string. Must be uniq
2056 * among all virtual devices in the system. The value isn't
2057 * copied, only the reference is stored, so the value must
2058 * remain valid during the device lifetime.
2059 * Returns assinged to the device ID on success, or negative value otherwise
2061 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
2062 const char *dev_name);
2065 * Unegisters a virtual device.
2067 * id - the device's ID, returned by the registration function
2069 void scst_unregister_virtual_device(int id);
2072 * Get/Set functions for tgt's sg_tablesize
2074 static inline int scst_tgt_get_sg_tablesize(struct scst_tgt *tgt)
2076 return tgt->sg_tablesize;
2079 static inline void scst_tgt_set_sg_tablesize(struct scst_tgt *tgt, int val)
2081 tgt->sg_tablesize = val;
2085 * Get/Set functions for tgt's target private data
2087 static inline void *scst_tgt_get_tgt_priv(struct scst_tgt *tgt)
2089 return tgt->tgt_priv;
2092 static inline void scst_tgt_set_tgt_priv(struct scst_tgt *tgt, void *val)
2094 tgt->tgt_priv = val;
2098 * Get/Set functions for session's target private data
2100 static inline void *scst_sess_get_tgt_priv(struct scst_session *sess)
2102 return sess->tgt_priv;
2105 static inline void scst_sess_set_tgt_priv(struct scst_session *sess,
2108 sess->tgt_priv = val;
2111 /* Returns TRUE if cmd is being executed in atomic context */
2112 static inline int scst_cmd_atomic(struct scst_cmd *cmd)
2114 int res = cmd->atomic;
2115 #ifdef CONFIG_SCST_EXTRACHECKS
2116 if (unlikely((in_atomic() || in_interrupt() || irqs_disabled()) &&
2118 printk(KERN_ERR "ERROR: atomic context and non-atomic cmd\n");
2127 static inline enum scst_exec_context __scst_estimate_context(bool direct)
2130 return SCST_CONTEXT_TASKLET;
2131 else if (irqs_disabled())
2132 return SCST_CONTEXT_THREAD;
2134 return direct ? SCST_CONTEXT_DIRECT :
2135 SCST_CONTEXT_DIRECT_ATOMIC;
2138 static inline enum scst_exec_context scst_estimate_context(void)
2140 return __scst_estimate_context(0);
2143 static inline enum scst_exec_context scst_estimate_context_direct(void)
2145 return __scst_estimate_context(1);
2148 /* Returns cmd's CDB */
2149 static inline const uint8_t *scst_cmd_get_cdb(struct scst_cmd *cmd)
2154 /* Returns cmd's CDB length */
2155 static inline int scst_cmd_get_cdb_len(struct scst_cmd *cmd)
2157 return cmd->cdb_len;
2160 /* Returns cmd's extended CDB */
2161 static inline const uint8_t *scst_cmd_get_ext_cdb(struct scst_cmd *cmd)
2163 return cmd->ext_cdb;
2166 /* Returns cmd's extended CDB length */
2167 static inline int scst_cmd_get_ext_cdb_len(struct scst_cmd *cmd)
2169 return cmd->ext_cdb_len;
2172 /* Sets cmd's extended CDB and its length */
2173 static inline void scst_cmd_set_ext_cdb(struct scst_cmd *cmd,
2174 uint8_t *ext_cdb, unsigned int ext_cdb_len)
2176 cmd->ext_cdb = ext_cdb;
2177 cmd->ext_cdb_len = ext_cdb_len;
2180 /* Returns cmd's session */
2181 static inline struct scst_session *scst_cmd_get_session(struct scst_cmd *cmd)
2186 /* Returns cmd's response data length */
2187 static inline int scst_cmd_get_resp_data_len(struct scst_cmd *cmd)
2189 return cmd->resp_data_len;
2192 /* Returns if status should be sent for cmd */
2193 static inline int scst_cmd_get_is_send_status(struct scst_cmd *cmd)
2195 return cmd->is_send_status;
2199 * Returns pointer to cmd's SG data buffer.
2201 * Usage of this function is not recommended, use scst_get_buf_*()
2202 * family of functions instead.
2204 static inline struct scatterlist *scst_cmd_get_sg(struct scst_cmd *cmd)
2210 * Returns cmd's sg_cnt.
2212 * Usage of this function is not recommended, use scst_get_buf_*()
2213 * family of functions instead.
2215 static inline int scst_cmd_get_sg_cnt(struct scst_cmd *cmd)
2221 * Returns cmd's data buffer length.
2223 * In case if you need to iterate over data in the buffer, usage of
2224 * this function is not recommended, use scst_get_buf_*()
2225 * family of functions instead.
2227 static inline unsigned int scst_cmd_get_bufflen(struct scst_cmd *cmd)
2229 return cmd->bufflen;
2232 /* Returns pointer to cmd's target's SG data buffer */
2233 static inline struct scatterlist *scst_cmd_get_tgt_sg(struct scst_cmd *cmd)
2238 /* Returns cmd's target's sg_cnt */
2239 static inline int scst_cmd_get_tgt_sg_cnt(struct scst_cmd *cmd)
2241 return cmd->tgt_sg_cnt;
2244 /* Sets cmd's target's SG data buffer */
2245 static inline void scst_cmd_set_tgt_sg(struct scst_cmd *cmd,
2246 struct scatterlist *sg, int sg_cnt)
2249 cmd->tgt_sg_cnt = sg_cnt;
2250 cmd->tgt_data_buf_alloced = 1;
2253 /* Returns cmd's data direction */
2254 static inline scst_data_direction scst_cmd_get_data_direction(
2255 struct scst_cmd *cmd)
2257 return cmd->data_direction;
2260 /* Returns cmd's status byte from host device */
2261 static inline uint8_t scst_cmd_get_status(struct scst_cmd *cmd)
2266 /* Returns cmd's status from host adapter itself */
2267 static inline uint8_t scst_cmd_get_msg_status(struct scst_cmd *cmd)
2269 return cmd->msg_status;
2272 /* Returns cmd's status set by low-level driver to indicate its status */
2273 static inline uint8_t scst_cmd_get_host_status(struct scst_cmd *cmd)
2275 return cmd->host_status;
2278 /* Returns cmd's status set by SCSI mid-level */
2279 static inline uint8_t scst_cmd_get_driver_status(struct scst_cmd *cmd)
2281 return cmd->driver_status;
2284 /* Returns pointer to cmd's sense buffer */
2285 static inline uint8_t *scst_cmd_get_sense_buffer(struct scst_cmd *cmd)
2290 /* Returns cmd's sense buffer length */
2291 static inline int scst_cmd_get_sense_buffer_len(struct scst_cmd *cmd)
2293 return SCST_SENSE_BUFFERSIZE;
2297 * Get/Set functions for cmd's target SN
2299 static inline uint64_t scst_cmd_get_tag(struct scst_cmd *cmd)
2304 static inline void scst_cmd_set_tag(struct scst_cmd *cmd, uint64_t tag)
2310 * Get/Set functions for cmd's target private data.
2311 * Variant with *_lock must be used if target driver uses
2312 * scst_find_cmd() to avoid race with it, except inside scst_find_cmd()'s
2313 * callback, where lock is already taken.
2315 static inline void *scst_cmd_get_tgt_priv(struct scst_cmd *cmd)
2317 return cmd->tgt_priv;
2320 static inline void scst_cmd_set_tgt_priv(struct scst_cmd *cmd, void *val)
2322 cmd->tgt_priv = val;
2325 void *scst_cmd_get_tgt_priv_lock(struct scst_cmd *cmd);
2326 void scst_cmd_set_tgt_priv_lock(struct scst_cmd *cmd, void *val);
2329 * Get/Set functions for tgt_need_alloc_data_buf flag
2331 static inline int scst_cmd_get_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2333 return cmd->tgt_need_alloc_data_buf;
2336 static inline void scst_cmd_set_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2338 cmd->tgt_need_alloc_data_buf = 1;
2342 * Get/Set functions for tgt_data_buf_alloced flag
2344 static inline int scst_cmd_get_tgt_data_buff_alloced(struct scst_cmd *cmd)
2346 return cmd->tgt_data_buf_alloced;
2349 static inline void scst_cmd_set_tgt_data_buff_alloced(struct scst_cmd *cmd)
2351 cmd->tgt_data_buf_alloced = 1;
2355 * Get/Set functions for dh_data_buf_alloced flag
2357 static inline int scst_cmd_get_dh_data_buff_alloced(struct scst_cmd *cmd)
2359 return cmd->dh_data_buf_alloced;
2362 static inline void scst_cmd_set_dh_data_buff_alloced(struct scst_cmd *cmd)
2364 cmd->dh_data_buf_alloced = 1;
2368 * Get/Set functions for no_sgv flag
2370 static inline int scst_cmd_get_no_sgv(struct scst_cmd *cmd)
2375 static inline void scst_cmd_set_no_sgv(struct scst_cmd *cmd)
2381 * Get/Set functions for tgt_sn
2383 static inline int scst_cmd_get_tgt_sn(struct scst_cmd *cmd)
2385 BUG_ON(!cmd->tgt_sn_set);
2389 static inline void scst_cmd_set_tgt_sn(struct scst_cmd *cmd, uint32_t tgt_sn)
2391 cmd->tgt_sn_set = 1;
2392 cmd->tgt_sn = tgt_sn;
2396 * Returns 1 if the cmd was aborted, so its status is invalid and no
2397 * reply shall be sent to the remote initiator. A target driver should
2398 * only clear internal resources, associated with cmd.
2400 static inline int scst_cmd_aborted(struct scst_cmd *cmd)
2402 return test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags) &&
2403 !test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
2407 * Get/Set functions for expected data direction, transfer length
2408 * and its validity flag
2410 static inline int scst_cmd_is_expected_set(struct scst_cmd *cmd)
2412 return cmd->expected_values_set;
2415 static inline scst_data_direction scst_cmd_get_expected_data_direction(
2416 struct scst_cmd *cmd)
2418 return cmd->expected_data_direction;
2421 static inline int scst_cmd_get_expected_transfer_len(
2422 struct scst_cmd *cmd)
2424 return cmd->expected_transfer_len;
2427 static inline void scst_cmd_set_expected(struct scst_cmd *cmd,
2428 scst_data_direction expected_data_direction,
2429 int expected_transfer_len)
2431 cmd->expected_data_direction = expected_data_direction;
2432 cmd->expected_transfer_len = expected_transfer_len;
2433 cmd->expected_values_set = 1;
2437 * Get/clear functions for cmd's may_need_dma_sync
2439 static inline int scst_get_may_need_dma_sync(struct scst_cmd *cmd)
2441 return cmd->may_need_dma_sync;
2444 static inline void scst_clear_may_need_dma_sync(struct scst_cmd *cmd)
2446 cmd->may_need_dma_sync = 0;
2450 * Get/set functions for cmd's delivery_status. It is one of
2451 * SCST_CMD_DELIVERY_* constants. It specifies the status of the
2452 * command's delivery to initiator.
2454 static inline int scst_get_delivery_status(struct scst_cmd *cmd)
2456 return cmd->delivery_status;
2459 static inline void scst_set_delivery_status(struct scst_cmd *cmd,
2460 int delivery_status)
2462 cmd->delivery_status = delivery_status;
2466 * Get/Set function for mgmt cmd's target private data
2468 static inline void *scst_mgmt_cmd_get_tgt_priv(struct scst_mgmt_cmd *mcmd)
2470 return mcmd->tgt_priv;
2473 static inline void scst_mgmt_cmd_set_tgt_priv(struct scst_mgmt_cmd *mcmd,
2476 mcmd->tgt_priv = val;
2479 /* Returns mgmt cmd's completition status (SCST_MGMT_STATUS_* constants) */
2480 static inline int scst_mgmt_cmd_get_status(struct scst_mgmt_cmd *mcmd)
2482 return mcmd->status;
2485 /* Returns mgmt cmd's TM fn */
2486 static inline int scst_mgmt_cmd_get_fn(struct scst_mgmt_cmd *mcmd)
2492 * Called by dev handler's task_mgmt_fn() to notify SCST core that mcmd
2493 * is going to complete asynchronously.
2495 void scst_prepare_async_mcmd(struct scst_mgmt_cmd *mcmd);
2498 * Called by dev handler to notify SCST core that async. mcmd is completed
2499 * with status "status".
2501 void scst_async_mcmd_completed(struct scst_mgmt_cmd *mcmd, int status);
2503 /* Returns AEN's fn */
2504 static inline int scst_aen_get_event_fn(struct scst_aen *aen)
2506 return aen->event_fn;
2509 /* Returns AEN's session */
2510 static inline struct scst_session *scst_aen_get_sess(struct scst_aen *aen)
2515 /* Returns AEN's LUN */
2516 static inline uint64_t scst_aen_get_lun(struct scst_aen *aen)
2521 /* Returns SCSI AEN's sense */
2522 static inline const uint8_t *scst_aen_get_sense(struct scst_aen *aen)
2524 return aen->aen_sense;
2527 /* Returns SCSI AEN's sense length */
2528 static inline int scst_aen_get_sense_len(struct scst_aen *aen)
2530 return aen->aen_sense_len;
2534 * Get/set functions for AEN's delivery_status. It is one of
2535 * SCST_AEN_RES_* constants. It specifies the status of the
2536 * command's delivery to initiator.
2538 static inline int scst_get_aen_delivery_status(struct scst_aen *aen)
2540 return aen->delivery_status;
2543 static inline void scst_set_aen_delivery_status(struct scst_aen *aen,
2546 aen->delivery_status = status;
2550 * Notifies SCST that the driver has sent the AEN and it
2551 * can be freed now. Don't forget to set the delivery status, if it
2552 * isn't success, using scst_set_aen_delivery_status() before calling
2555 void scst_aen_done(struct scst_aen *aen);
2557 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2559 static inline struct page *sg_page(struct scatterlist *sg)
2564 static inline void *sg_virt(struct scatterlist *sg)
2566 return page_address(sg_page(sg)) + sg->offset;
2569 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
2571 memset(sgl, 0, sizeof(*sgl) * nents);
2574 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
2579 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
2580 unsigned int len, unsigned int offset)
2582 sg_assign_page(sg, page);
2583 sg->offset = offset;
2587 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
2589 static inline void sg_clear(struct scatterlist *sg)
2591 memset(sg, 0, sizeof(*sg));
2592 #ifdef CONFIG_DEBUG_SG
2593 sg->sg_magic = SG_MAGIC;
2598 * Functions for access to the commands data (SG) buffer,
2599 * including HIGHMEM environment. Should be used instead of direct
2600 * access. Returns the mapped buffer length for success, 0 for EOD,
2601 * negative error code otherwise.
2603 * "Buf" argument returns the mapped buffer
2605 * The "put" function unmaps the buffer.
2607 static inline int __scst_get_buf(struct scst_cmd *cmd, uint8_t **buf)
2610 struct scatterlist *sg = cmd->sg;
2611 int i = cmd->get_sg_buf_entry_num;
2615 if ((i >= cmd->sg_cnt) || unlikely(sg == NULL))
2618 *buf = page_address(sg_page(&sg[i]));
2619 *buf += sg[i].offset;
2622 cmd->get_sg_buf_entry_num++;
2628 static inline int scst_get_buf_first(struct scst_cmd *cmd, uint8_t **buf)
2630 cmd->get_sg_buf_entry_num = 0;
2631 cmd->may_need_dma_sync = 1;
2632 return __scst_get_buf(cmd, buf);
2635 static inline int scst_get_buf_next(struct scst_cmd *cmd, uint8_t **buf)
2637 return __scst_get_buf(cmd, buf);
2640 static inline void scst_put_buf(struct scst_cmd *cmd, void *buf)
2646 * Returns approximate higher rounded buffers count that
2647 * scst_get_buf_[first|next]() return.
2649 static inline int scst_get_buf_count(struct scst_cmd *cmd)
2651 return (cmd->sg_cnt == 0) ? 1 : cmd->sg_cnt;
2655 * Suspends and resumes any activity.
2656 * Function scst_suspend_activity() doesn't return 0, until there are any
2657 * active commands (state after SCST_CMD_STATE_INIT). If "interruptible"
2658 * is true, it returns after SCST_SUSPENDING_TIMEOUT or if it was interrupted
2659 * by a signal with the coresponding error status < 0. If "interruptible"
2660 * is false, it will wait virtually forever.
2662 * New arriving commands stay in that state until scst_resume_activity()
2665 int scst_suspend_activity(bool interruptible);
2666 void scst_resume_activity(void);
2669 * Main SCST commands processing routing. Must be used only by dev handlers.
2670 * Argument atomic is true if function called in atomic context.
2672 void scst_process_active_cmd(struct scst_cmd *cmd, bool atomic);
2675 * Checks if command can be executed (reservations, etc.) or there are local
2676 * events, like pending UAs. Returns < 0 if command must be aborted, > 0 if
2677 * there is an event and command should be immediately completed, or 0
2680 * !! Dev handlers implementing exec() callback must call this function there !!
2681 * !! just before the actual command's execution !!
2683 int scst_check_local_events(struct scst_cmd *cmd);
2686 * Returns the next state of the SCSI target state machine in case if command's
2687 * completed abnormally.
2689 int scst_get_cmd_abnormal_done_state(const struct scst_cmd *cmd);
2692 * Sets state of the SCSI target state machine in case if command's completed
2695 void scst_set_cmd_abnormal_done_state(struct scst_cmd *cmd);
2698 * Returns target driver's root entry in SCST's /proc hierarchy.
2699 * The driver can create own files/directoryes here, which should
2700 * be deleted in the driver's release().
2702 static inline struct proc_dir_entry *scst_proc_get_tgt_root(
2703 struct scst_tgt_template *vtt)
2705 return vtt->proc_tgt_root;
2709 * Returns device handler's root entry in SCST's /proc hierarchy.
2710 * The driver can create own files/directoryes here, which should
2711 * be deleted in the driver's detach()/release().
2713 static inline struct proc_dir_entry *scst_proc_get_dev_type_root(
2714 struct scst_dev_type *dtt)
2716 return dtt->proc_dev_type_root;
2720 ** Two library functions and the structure to help the drivers
2721 ** that use scst_debug.* facilities manage "trace_level" /proc entry.
2722 ** The functions service "standard" log levels and allow to work
2723 ** with driver specific levels, which should be passed inside as
2724 ** NULL-terminated array of struct scst_proc_log's, where:
2725 ** - val - the level's numeric value
2726 ** - token - its string representation
2729 struct scst_proc_log {
2734 int scst_proc_log_entry_read(struct seq_file *seq, unsigned long log_level,
2735 const struct scst_proc_log *tbl);
2737 int scst_proc_log_entry_write(struct file *file, const char __user *buf,
2738 unsigned long length, unsigned long *log_level,
2739 unsigned long default_level, const struct scst_proc_log *tbl);
2742 * helper data structure and function to create proc entry.
2744 struct scst_proc_data {
2745 struct file_operations seq_op;
2746 int (*show)(struct seq_file *, void *);
2750 int scst_single_seq_open(struct inode *inode, struct file *file);
2752 struct proc_dir_entry *scst_create_proc_entry(struct proc_dir_entry *root,
2753 const char *name, struct scst_proc_data *pdata);
2755 #define SCST_DEF_RW_SEQ_OP(x) \
2757 .owner = THIS_MODULE, \
2758 .open = scst_single_seq_open,\
2761 .llseek = seq_lseek, \
2762 .release = single_release, \
2766 * Adds and deletes (stops) num SCST's threads. Returns 0 on success,
2767 * error code otherwise.
2769 int scst_add_cmd_threads(int num);
2770 void scst_del_cmd_threads(int num);
2772 int scst_alloc_sense(struct scst_cmd *cmd, int atomic);
2773 int scst_alloc_set_sense(struct scst_cmd *cmd, int atomic,
2774 const uint8_t *sense, unsigned int len);
2776 void scst_set_sense(uint8_t *buffer, int len, int key, int asc, int ascq);
2779 * Returnes true if sense matches to (key, asc, ascq) and false otherwise.
2780 * Valid_mask is one or several SCST_SENSE_*_VALID constants setting valid
2781 * (key, asc, ascq) values.
2783 bool scst_analyze_sense(const uint8_t *sense, int len, unsigned int valid_mask,
2784 int key, int asc, int ascq);
2787 * Returnes a pseudo-random number for debugging purposes. Available only in
2790 unsigned long scst_random(void);
2793 * Sets response data length for cmd and truncates its SG vector accordingly.
2794 * The cmd->resp_data_len must not be set directly, it must be set only
2795 * using this function. Value of resp_data_len must be <= cmd->bufflen.
2797 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len);
2800 * Get/put global ref counter that prevents from entering into suspended
2801 * activities stage, so protects from any global management operations.
2803 void scst_get(void);
2804 void scst_put(void);
2809 void scst_cmd_get(struct scst_cmd *cmd);
2810 void scst_cmd_put(struct scst_cmd *cmd);
2813 * Allocates and returns pointer to SG vector with data size "size".
2814 * In *count returned the count of entries in the vector.
2815 * Returns NULL for failure.
2817 struct scatterlist *scst_alloc(int size, gfp_t gfp_mask, int *count);
2819 /* Frees SG vector returned by scst_alloc() */
2820 void scst_free(struct scatterlist *sg, int count);
2823 * Adds local to the current thread data to tgt_dev
2824 * (they will be local for the tgt_dev and current thread).
2826 void scst_add_thr_data(struct scst_tgt_dev *tgt_dev,
2827 struct scst_thr_data_hdr *data,
2828 void (*free_fn) (struct scst_thr_data_hdr *data));
2830 /* Deletes all local to threads data from tgt_dev */
2831 void scst_del_all_thr_data(struct scst_tgt_dev *tgt_dev);
2833 /* Deletes all local to threads data from all tgt_dev's of the dev */
2834 void scst_dev_del_all_thr_data(struct scst_device *dev);
2836 /* Finds local to the thread data. Returns NULL, if they not found. */
2837 struct scst_thr_data_hdr *__scst_find_thr_data(struct scst_tgt_dev *tgt_dev,
2838 struct task_struct *tsk);
2840 /* Finds local to the current thread data. Returns NULL, if they not found. */
2841 static inline struct scst_thr_data_hdr *scst_find_thr_data(
2842 struct scst_tgt_dev *tgt_dev)
2844 return __scst_find_thr_data(tgt_dev, current);
2847 static inline void scst_thr_data_get(struct scst_thr_data_hdr *data)
2849 atomic_inc(&data->ref);
2852 static inline void scst_thr_data_put(struct scst_thr_data_hdr *data)
2854 if (atomic_dec_and_test(&data->ref))
2855 data->free_fn(data);
2859 ** Generic parse() support routines.
2860 ** Done via pointer on functions to avoid unneeded dereferences on
2864 /* Calculates and returns block shift for the given sector size */
2865 int scst_calc_block_shift(int sector_size);
2867 /* Generic parse() for SBC (disk) devices */
2868 int scst_sbc_generic_parse(struct scst_cmd *cmd,
2869 int (*get_block_shift)(struct scst_cmd *cmd));
2871 /* Generic parse() for MMC (cdrom) devices */
2872 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
2873 int (*get_block_shift)(struct scst_cmd *cmd));
2875 /* Generic parse() for MO disk devices */
2876 int scst_modisk_generic_parse(struct scst_cmd *cmd,
2877 int (*get_block_shift)(struct scst_cmd *cmd));
2879 /* Generic parse() for tape devices */
2880 int scst_tape_generic_parse(struct scst_cmd *cmd,
2881 int (*get_block_size)(struct scst_cmd *cmd));
2883 /* Generic parse() for changer devices */
2884 int scst_changer_generic_parse(struct scst_cmd *cmd,
2885 int (*nothing)(struct scst_cmd *cmd));
2887 /* Generic parse() for "processor" devices */
2888 int scst_processor_generic_parse(struct scst_cmd *cmd,
2889 int (*nothing)(struct scst_cmd *cmd));
2891 /* Generic parse() for RAID devices */
2892 int scst_raid_generic_parse(struct scst_cmd *cmd,
2893 int (*nothing)(struct scst_cmd *cmd));
2896 ** Generic dev_done() support routines.
2897 ** Done via pointer on functions to avoid unneeded dereferences on
2901 /* Generic dev_done() for block devices */
2902 int scst_block_generic_dev_done(struct scst_cmd *cmd,
2903 void (*set_block_shift)(struct scst_cmd *cmd, int block_shift));
2905 /* Generic dev_done() for tape devices */
2906 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
2907 void (*set_block_size)(struct scst_cmd *cmd, int block_size));
2910 * Issues a MODE SENSE for control mode page data and sets the corresponding
2911 * dev's parameter from it. Returns 0 on success and not 0 otherwise.
2913 int scst_obtain_device_parameters(struct scst_device *dev);
2915 #endif /* __SCST_H */