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/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(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + d)
46 #define SCST_VERSION_CODE SCST_VERSION(1, 0, 2, 0)
47 #define SCST_VERSION_STRING "1.0.2"
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 = 0x001,
481 SCST_SMALL_TIMEOUT = 0x002,
482 SCST_LONG_TIMEOUT = 0x004,
483 SCST_UNKNOWN_LENGTH = 0x008,
484 SCST_INFO_NOT_FOUND = 0x010, /* must be single bit */
485 SCST_VERIFY_BYTCHK_MISMATCH_ALLOWED = 0x020,
486 SCST_IMPLICIT_HQ = 0x040,
487 SCST_SKIP_UA = 0x080,
488 SCST_WRITE_MEDIUM = 0x100,
489 SCST_LOCAL_CMD = 0x200,
490 SCST_LOCAL_EXEC_NEEDED = 0x400,
494 * Scsi_Target_Template: defines what functions a target driver will
495 * have to provide in order to work with the target mid-level.
496 * MUST HAVEs define functions that are expected to be in order to work.
497 * OPTIONAL says that there is a choice.
499 * Also, pay attention to the fact that a command is BLOCKING or NON-BLOCKING.
500 * NON-BLOCKING means that a function returns immediately and will not wait
501 * for actual data transfer to finish. Blocking in such command could have
502 * negative impact on overall system performance. If blocking is necessary,
503 * it is worth to consider creating dedicated thread(s) in target driver, to
504 * which the commands would be passed and which would perform blocking
505 * operations instead of SCST.
507 * If the function allowed to sleep or not is determined by its last
508 * argument, which is true, if sleeping is not allowed. In this case,
509 * if the function requires sleeping, it can return
510 * SCST_TGT_RES_NEED_THREAD_CTX, and it will be recalled in the thread context,
511 * where sleeping is allowed.
513 struct scst_tgt_template {
517 * SG tablesize allows to check whether scatter/gather can be used
523 * True, if this target adapter uses unchecked DMA onto an ISA bus.
525 unsigned unchecked_isa_dma:1;
528 * True, if this target adapter can benefit from using SG-vector
529 * clustering (i.e. smaller number of segments).
531 unsigned use_clustering:1;
534 * True, if this target adapter doesn't support SG-vector clustering
536 unsigned no_clustering:1;
539 * True, if corresponding function supports execution in
540 * the atomic (non-sleeping) context
542 unsigned xmit_response_atomic:1;
543 unsigned rdy_to_xfer_atomic:1;
545 /* True, if the template doesn't need the entry in /proc */
546 unsigned no_proc_entry:1;
549 * This function is equivalent to the SCSI
550 * queuecommand. The target should transmit the response
551 * buffer and the status in the scst_cmd struct.
552 * The expectation is that this executing this command is NON-BLOCKING.
554 * After the response is actually transmitted, the target
555 * should call the scst_tgt_cmd_done() function of the
556 * mid-level, which will allow it to free up the command.
557 * Returns one of the SCST_TGT_RES_* constants.
559 * Pay attention to "atomic" attribute of the cmd, which can be get
560 * by scst_cmd_atomic(): it is true if the function called in the
561 * atomic (non-sleeping) context.
565 int (*xmit_response) (struct scst_cmd *cmd);
568 * This function informs the driver that data
569 * buffer corresponding to the said command have now been
570 * allocated and it is OK to receive data for this command.
571 * This function is necessary because a SCSI target does not
572 * have any control over the commands it receives. Most lower
573 * level protocols have a corresponding function which informs
574 * the initiator that buffers have been allocated e.g., XFER_
575 * RDY in Fibre Channel. After the data is actually received
576 * the low-level driver needs to call scst_rx_data() in order to
577 * continue processing this command.
578 * Returns one of the SCST_TGT_RES_* constants.
579 * This command is expected to be NON-BLOCKING.
581 * Pay attention to "atomic" attribute of the cmd, which can be get
582 * by scst_cmd_atomic(): it is true if the function called in the
583 * atomic (non-sleeping) context.
587 int (*rdy_to_xfer) (struct scst_cmd *cmd);
590 * Called to notify the driver that the command is about to be freed.
591 * Necessary, because for aborted commands xmit_response() could not
592 * be called. Could be called on IRQ context.
596 void (*on_free_cmd) (struct scst_cmd *cmd);
599 * This function allows target driver to handle data buffer
600 * allocations on its own.
602 * Target driver doesn't have to always allocate buffer in this
603 * function, but if it decide to do it, it must check that
604 * scst_cmd_get_data_buff_alloced() returns 0, otherwise to avoid
605 * double buffer allocation and memory leaks alloc_data_buf() shall
608 * Shall return 0 in case of success or < 0 (preferrably -ENOMEM)
609 * in case of error, or > 0 if the regular SCST allocation should be
610 * done. In case of returning successfully,
611 * scst_cmd->tgt_data_buf_alloced will be set by SCST.
613 * It is possible that both target driver and dev handler request own
614 * memory allocation. In this case, data will be memcpy() between
615 * buffers, where necessary.
617 * If allocation in atomic context - cf. scst_cmd_atomic() - is not
618 * desired or fails and consequently < 0 is returned, this function
619 * will be re-called in thread context.
621 * Please note that the driver will have to handle itself all relevant
622 * details such as scatterlist setup, highmem, freeing the allocated
627 int (*alloc_data_buf) (struct scst_cmd *cmd);
630 * This function informs the driver that data
631 * buffer corresponding to the said command have now been
632 * allocated and other preprocessing tasks have been done.
633 * A target driver could need to do some actions at this stage.
634 * After the target driver done the needed actions, it shall call
635 * scst_restart_cmd() in order to continue processing this command.
637 * Called only if the cmd is queued using scst_cmd_init_stage1_done()
638 * instead of scst_cmd_init_done().
640 * Returns void, the result is expected to be returned using
641 * scst_restart_cmd().
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 void (*preprocessing_done) (struct scst_cmd *cmd);
654 * This function informs the driver that the said command is about
657 * Returns one of the SCST_PREPROCESS_* constants.
659 * This command is expected to be NON-BLOCKING.
661 * Pay attention to "atomic" attribute of the cmd, which can be get
662 * by scst_cmd_atomic(): it is true if the function called in the
663 * atomic (non-sleeping) context.
667 int (*pre_exec) (struct scst_cmd *cmd);
670 * This function informs the driver that all affected by the
671 * corresponding task management function commands have beed completed.
672 * No return value expected.
674 * This function is expected to be NON-BLOCKING.
676 * Called without any locks held from a thread context.
680 void (*task_mgmt_affected_cmds_done) (struct scst_mgmt_cmd *mgmt_cmd);
683 * This function informs the driver that the corresponding task
684 * management function has been completed, i.e. all the corresponding
685 * commands completed and freed. No return value expected.
687 * This function is expected to be NON-BLOCKING.
689 * Called without any locks held from a thread context.
691 * MUST HAVE if the target supports task management.
693 void (*task_mgmt_fn_done) (struct scst_mgmt_cmd *mgmt_cmd);
696 * This function should detect the target adapters that
697 * are present in the system. The function should return a value
698 * >= 0 to signify the number of detected target adapters.
699 * A negative value should be returned whenever there is
704 int (*detect) (struct scst_tgt_template *tgt_template);
707 * This function should free up the resources allocated to the device.
708 * The function should return 0 to indicate successful release
709 * or a negative value if there are some issues with the release.
710 * In the current version the return value is ignored.
714 int (*release) (struct scst_tgt *tgt);
717 * This function is used for Asynchronous Event Notifications.
719 * Returns one of the SCST_AEN_RES_* constants.
720 * After AEN is sent, target driver must call scst_aen_done() and,
721 * optionally, scst_set_aen_delivery_status().
723 * This command is expected to be NON-BLOCKING, but can sleep.
725 * MUST HAVE, if low-level protocol supports AENs.
727 int (*report_aen) (struct scst_aen *aen);
730 * Those functions can be used to export the driver's statistics and
731 * other infos to the world outside the kernel as well as to get some
732 * management commands from it.
736 int (*read_proc) (struct seq_file *seq, struct scst_tgt *tgt);
737 int (*write_proc) (char *buffer, char **start, off_t offset,
738 int length, int *eof, struct scst_tgt *tgt);
741 * Name of the template. Must be unique to identify
742 * the template. MUST HAVE
744 const char name[SCST_MAX_NAME];
747 * Number of additional threads to the pool of dedicated threads.
748 * Used if xmit_response() or rdy_to_xfer() is blocking.
749 * It is the target driver's duty to ensure that not more, than that
750 * number of threads, are blocked in those functions at any time.
754 /* Private, must be inited to 0 by memset() */
756 /* List of targets per template, protected by scst_mutex */
757 struct list_head tgt_list;
759 /* List entry of global templates list */
760 struct list_head scst_template_list_entry;
762 /* The pointer to the /proc directory entry */
763 struct proc_dir_entry *proc_tgt_root;
765 /* Device number in /proc */
769 struct scst_dev_type {
770 /* SCSI type of the supported device. MUST HAVE */
774 * True, if corresponding function supports execution in
775 * the atomic (non-sleeping) context
777 unsigned parse_atomic:1;
778 unsigned exec_atomic:1;
779 unsigned dev_done_atomic:1;
781 /* Set, if no /proc files should be automatically created by SCST */
785 * Should be set, if exec() is synchronous. This is a hint to SCST core
786 * to optimize commands order management.
788 unsigned exec_sync:1;
791 * Called to parse CDB from the cmd and initialize
792 * cmd->bufflen and cmd->data_direction (both - REQUIRED).
793 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
794 * if the next default state should be used, or
795 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
796 * context, but requires sleeping, or SCST_CMD_STATE_STOP if the
797 * command should not be further processed for now. In the
798 * SCST_CMD_STATE_NEED_THREAD_CTX case the function
799 * will be recalled in the thread context, where sleeping is allowed.
801 * Pay attention to "atomic" attribute of the cmd, which can be get
802 * by scst_cmd_atomic(): it is true if the function called in the
803 * atomic (non-sleeping) context.
807 int (*parse) (struct scst_cmd *cmd);
810 * Called to execute CDB. Useful, for instance, to implement
811 * data caching. The result of CDB execution is reported via
812 * cmd->scst_cmd_done() callback.
814 * - SCST_EXEC_COMPLETED - the cmd is done, go to other ones
815 * - SCST_EXEC_NEED_THREAD - thread context is required to execute
816 * the command. Exec() will be called again in the thread context.
817 * - SCST_EXEC_NOT_COMPLETED - the cmd should be sent to SCSI
820 * Pay attention to "atomic" attribute of the cmd, which can be get
821 * by scst_cmd_atomic(): it is true if the function called in the
822 * atomic (non-sleeping) context.
824 * If this function provides sync execution, you should set
825 * exec_sync flag and consider to setup dedicated threads by
826 * setting threads_num > 0.
828 * !! If this function is implemented, scst_check_local_events() !!
829 * !! shall be called inside it just before the actual command's !!
832 * OPTIONAL, if not set, the commands will be sent directly to SCSI
835 int (*exec) (struct scst_cmd *cmd);
838 * Called to notify dev handler about the result of cmd execution
839 * and perform some post processing. Cmd's fields is_send_status and
840 * resp_data_len should be set by this function, but SCST offers good
842 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
843 * if the next default state should be used, or
844 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
845 * context, but requires sleeping. In the last case, the function
846 * will be recalled in the thread context, where sleeping is allowed.
848 * Pay attention to "atomic" attribute of the cmd, which can be get
849 * by scst_cmd_atomic(): it is true if the function called in the
850 * atomic (non-sleeping) context.
852 int (*dev_done) (struct scst_cmd *cmd);
855 * Called to notify dev hander that the command is about to be freed.
856 * Could be called on IRQ context.
858 void (*on_free_cmd) (struct scst_cmd *cmd);
861 * Called to execute a task management command.
863 * - SCST_MGMT_STATUS_SUCCESS - the command is done with success,
864 * no firther actions required
865 * - The SCST_MGMT_STATUS_* error code if the command is failed and
866 * no further actions required
867 * - SCST_DEV_TM_NOT_COMPLETED - regular standard actions for the
868 * command should be done
870 * Called without any locks held from a thread context.
872 int (*task_mgmt_fn) (struct scst_mgmt_cmd *mgmt_cmd,
873 struct scst_tgt_dev *tgt_dev);
876 * Called when new device is attaching to the dev handler
877 * Returns 0 on success, error code otherwise.
879 int (*attach) (struct scst_device *dev);
881 /* Called when new device is detaching from the dev handler */
882 void (*detach) (struct scst_device *dev);
885 * Called when new tgt_dev (session) is attaching to the dev handler.
886 * Returns 0 on success, error code otherwise.
888 int (*attach_tgt) (struct scst_tgt_dev *tgt_dev);
890 /* Called when tgt_dev (session) is detaching from the dev handler */
891 void (*detach_tgt) (struct scst_tgt_dev *tgt_dev);
894 * Those functions can be used to export the handler's statistics and
895 * other infos to the world outside the kernel as well as to get some
896 * management commands from it.
900 int (*read_proc) (struct seq_file *seq, struct scst_dev_type *dev_type);
901 int (*write_proc) (char *buffer, char **start, off_t offset,
902 int length, int *eof, struct scst_dev_type *dev_type);
905 * Name of the dev handler. Must be unique. MUST HAVE.
907 * It's SCST_MAX_NAME + few more bytes to match scst_user expectations.
909 char name[SCST_MAX_NAME + 10];
912 * Number of dedicated threads. If 0 - no dedicated threads will
913 * be created, if <0 - creation of dedicated threads is prohibited.
917 struct module *module;
921 /* list entry in scst_dev_type_list */
922 struct list_head dev_type_list_entry;
924 /* The pointer to the /proc directory entry */
925 struct proc_dir_entry *proc_dev_type_root;
929 /* List of remote sessions per target, protected by scst_mutex */
930 struct list_head sess_list;
932 /* List entry of targets per template (tgts_list) */
933 struct list_head tgt_list_entry;
935 struct scst_tgt_template *tgtt; /* corresponding target template */
938 * Maximum SG table size. Needed here, since different cards on the
939 * same target template can have different SG table limitations.
943 /* Used for storage of target driver private stuff */
947 * The following fields used to store and retry cmds if
948 * target's internal queue is full, so the target is unable to accept
949 * the cmd returning QUEUE FULL
951 bool retry_timer_active;
952 struct timer_list retry_timer;
953 atomic_t finished_cmds;
954 int retry_cmds; /* protected by tgt_lock */
956 struct list_head retry_cmd_list; /* protected by tgt_lock */
958 /* Used to wait until session finished to unregister */
959 wait_queue_head_t unreg_waitQ;
961 /* Device number in /proc */
964 /* Name on the default security group ("Default_target_name") */
965 char *default_group_name;
968 /* Hash size and hash fn for hash based lun translation */
969 #define TGT_DEV_HASH_SHIFT 5
970 #define TGT_DEV_HASH_SIZE (1<<TGT_DEV_HASH_SHIFT)
971 #define HASH_VAL(_val) (_val & (TGT_DEV_HASH_SIZE - 1))
973 struct scst_session {
975 * Initialization phase, one of SCST_SESS_IPH_* constants, protected by
980 struct scst_tgt *tgt; /* corresponding target */
982 /* Used for storage of target driver private stuff */
986 * Hash list of tgt_dev's for this session, protected by scst_mutex
987 * and suspended activity
989 struct list_head sess_tgt_dev_list_hash[TGT_DEV_HASH_SIZE];
992 * List of cmds in this session. Used to find a cmd in the
993 * session. Protected by sess_list_lock.
995 struct list_head search_cmd_list;
997 atomic_t refcnt; /* get/put counter */
1000 * Alive commands for this session. ToDo: make it part of the common
1003 atomic_t sess_cmd_count;
1005 spinlock_t sess_list_lock; /* protects search_cmd_list, etc */
1007 /* Access control for this session and list entry there */
1008 struct scst_acg *acg;
1010 /* List entry for the sessions list inside ACG */
1011 struct list_head acg_sess_list_entry;
1013 /* Name of attached initiator */
1014 const char *initiator_name;
1016 /* List entry of sessions per target */
1017 struct list_head sess_list_entry;
1019 /* List entry for the list that keeps session, waiting for the init */
1020 struct list_head sess_init_list_entry;
1023 * List entry for the list that keeps session, waiting for the shutdown
1025 struct list_head sess_shut_list_entry;
1028 * Lists of deffered during session initialization commands.
1029 * Protected by sess_list_lock.
1031 struct list_head init_deferred_cmd_list;
1032 struct list_head init_deferred_mcmd_list;
1035 * Shutdown phase, one of SCST_SESS_SPH_* constants, unprotected.
1036 * Async. relating to init_phase, must be a separate variable, because
1037 * session could be unregistered before async. registration is finished.
1039 unsigned long shut_phase;
1041 /* Used if scst_unregister_session() called in wait mode */
1042 struct completion *shutdown_compl;
1045 * Functions and data for user callbacks from scst_register_session()
1046 * and scst_unregister_session()
1048 void *reg_sess_data;
1049 void (*init_result_fn) (struct scst_session *sess, void *data,
1051 void (*unreg_done_fn) (struct scst_session *sess);
1053 #ifdef CONFIG_SCST_MEASURE_LATENCY /* must be last */
1054 spinlock_t meas_lock;
1055 uint64_t scst_time, processing_time;
1056 unsigned int processed_cmds;
1060 struct scst_cmd_lists {
1061 spinlock_t cmd_list_lock;
1062 struct list_head active_cmd_list;
1063 wait_queue_head_t cmd_list_waitQ;
1064 struct list_head lists_list_entry;
1068 /* List entry for below *_cmd_lists */
1069 struct list_head cmd_list_entry;
1071 /* Pointer to lists of commands with the lock */
1072 struct scst_cmd_lists *cmd_lists;
1076 struct scst_session *sess; /* corresponding session */
1078 /* Cmd state, one of SCST_CMD_STATE_* constants */
1081 /*************************************************************
1083 *************************************************************/
1086 * Set if expected_sn should be incremented, i.e. cmd was sent
1089 unsigned int sent_for_exec:1;
1091 /* Set if the cmd's action is completed */
1092 unsigned int completed:1;
1094 /* Set if we should ignore Unit Attention in scst_check_sense() */
1095 unsigned int ua_ignore:1;
1097 /* Set if cmd is being processed in atomic context */
1098 unsigned int atomic:1;
1100 /* Set if this command was sent in double UA possible state */
1101 unsigned int double_ua_possible:1;
1103 /* Set if this command contains status */
1104 unsigned int is_send_status:1;
1106 /* Set if cmd is being retried */
1107 unsigned int retry:1;
1109 /* Set if cmd is internally generated */
1110 unsigned int internal:1;
1112 /* Set if the device was blocked by scst_inc_on_dev_cmd() (for debug) */
1113 unsigned int inc_blocking:1;
1115 /* Set if the device should be unblocked after cmd's finish */
1116 unsigned int needs_unblocking:1;
1118 /* Set if scst_dec_on_dev_cmd() call is needed on the cmd's finish */
1119 unsigned int dec_on_dev_needed:1;
1122 * Set if the target driver wants to alloc data buffers on its own.
1123 * In this case alloc_data_buf() must be provided in the target driver
1126 unsigned int tgt_need_alloc_data_buf:1;
1129 * Set by SCST if the custom data buffer allocation by the target driver
1132 unsigned int tgt_data_buf_alloced:1;
1134 /* Set if custom data buffer allocated by dev handler */
1135 unsigned int dh_data_buf_alloced:1;
1137 /* Set if the target driver called scst_set_expected() */
1138 unsigned int expected_values_set:1;
1141 * Set if the cmd was delayed by task management debugging code.
1142 * Used only if CONFIG_SCST_DEBUG_TM is on.
1144 unsigned int tm_dbg_delayed:1;
1147 * Set if the cmd must be ignored by task management debugging code.
1148 * Used only if CONFIG_SCST_DEBUG_TM is on.
1150 unsigned int tm_dbg_immut:1;
1153 * Set if the SG buffer was modified by scst_set_resp_data_len()
1155 unsigned int sg_buff_modified:1;
1158 * Set if scst_cmd_init_stage1_done() called and the target
1159 * want that preprocessing_done() will be called
1161 unsigned int preprocessing_only:1;
1163 /* Set if cmd's SN was set */
1164 unsigned int sn_set:1;
1166 /* Set if hq_cmd_count was incremented */
1167 unsigned int hq_cmd_inced:1;
1170 * Set if scst_cmd_init_stage1_done() called and the target wants
1171 * that the SN for the cmd won't be assigned until scst_restart_cmd()
1173 unsigned int set_sn_on_restart_cmd:1;
1175 /* Set if the cmd's must not use sgv cache for data buffer */
1176 unsigned int no_sgv:1;
1179 * Set if target driver may need to call dma_sync_sg() or similar
1180 * function before transferring cmd' data to the target device
1183 unsigned int may_need_dma_sync:1;
1185 /* Set if the cmd was done or aborted out of its SN */
1186 unsigned int out_of_sn:1;
1188 /* Set if increment expected_sn in cmd->scst_cmd_done() */
1189 unsigned int inc_expected_sn_on_done:1;
1191 /* Set if tgt_sn field is valid */
1192 unsigned int tgt_sn_set:1;
1194 /* Set if cmd is done */
1195 unsigned int done:1;
1197 /* Set if cmd is finished */
1198 unsigned int finished:1;
1200 /**************************************************************/
1202 unsigned long cmd_flags; /* cmd's async flags */
1204 /* Keeps status of cmd's status/data delivery to remote initiator */
1205 int delivery_status;
1207 struct scst_tgt_template *tgtt; /* to save extra dereferences */
1208 struct scst_tgt *tgt; /* to save extra dereferences */
1209 struct scst_device *dev; /* to save extra dereferences */
1211 struct scst_tgt_dev *tgt_dev; /* corresponding device for this cmd */
1213 uint64_t lun; /* LUN for this cmd */
1215 unsigned long start_time;
1217 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1218 struct scsi_request *scsi_req; /* SCSI request */
1221 /* List entry for tgt_dev's SN related lists */
1222 struct list_head sn_cmd_list_entry;
1224 /* Cmd's serial number, used to execute cmd's in order of arrival */
1227 /* The corresponding sn_slot in tgt_dev->sn_slots */
1230 /* List entry for session's search_cmd_list */
1231 struct list_head search_cmd_list_entry;
1234 * Used to found the cmd by scst_find_cmd_by_tag(). Set by the
1235 * target driver on the cmd's initialization time
1239 uint32_t tgt_sn; /* SN set by target driver (for TM purposes) */
1241 /* CDB and its len */
1242 uint8_t cdb[SCST_MAX_CDB_SIZE];
1243 short cdb_len; /* it might be -1 */
1244 unsigned short ext_cdb_len;
1247 enum scst_cdb_flags op_flags;
1248 const char *op_name;
1250 enum scst_cmd_queue_type queue_type;
1252 int timeout; /* CDB execution timeout in seconds */
1253 int retries; /* Amount of retries that will be done by SCSI mid-level */
1255 /* SCSI data direction, one of SCST_DATA_* constants */
1256 scst_data_direction data_direction;
1258 /* Remote initiator supplied values, if any */
1259 scst_data_direction expected_data_direction;
1260 int expected_transfer_len;
1261 int expected_in_transfer_len; /* for bidi writes */
1264 * Cmd data length. Could be different from bufflen for commands like
1265 * VERIFY, which transfer different amount of data (if any), than
1270 /* Completition routine */
1271 void (*scst_cmd_done) (struct scst_cmd *cmd, int next_state,
1272 enum scst_exec_context pref_context);
1274 struct sgv_pool_obj *sgv; /* sgv object */
1275 int bufflen; /* cmd buffer length */
1276 struct scatterlist *sg; /* cmd data buffer SG vector */
1277 int sg_cnt; /* SG segments count */
1280 * Response data length in data buffer. This field must not be set
1281 * directly, use scst_set_resp_data_len() for that
1285 /* scst_get_sg_buf_[first,next]() support */
1286 int get_sg_buf_entry_num;
1288 /* Bidirectional transfers support */
1289 int in_bufflen; /* WRITE buffer length */
1290 struct sgv_pool_obj *in_sgv; /* WRITE sgv object */
1291 struct scatterlist *in_sg; /* WRITE data buffer SG vector */
1292 int in_sg_cnt; /* WRITE SG segments count */
1295 * Used if both target driver and dev handler request own memory
1296 * allocation. In other cases, both are equal to sg and sg_cnt
1299 * If target driver requests own memory allocations, it MUST use
1300 * functions scst_cmd_get_tgt_sg*() to get sg and sg_cnt! Otherwise,
1301 * it may use functions scst_cmd_get_sg*().
1303 struct scatterlist *tgt_sg;
1305 struct scatterlist *tgt_in_sg; /* bidirectional */
1306 int tgt_in_sg_cnt; /* bidirectional */
1309 * The status fields in case of errors must be set using
1310 * scst_set_cmd_error_status()!
1312 uint8_t status; /* status byte from target device */
1313 uint8_t msg_status; /* return status from host adapter itself */
1314 uint8_t host_status; /* set by low-level driver to indicate status */
1315 uint8_t driver_status; /* set by mid-level */
1317 uint8_t *sense; /* pointer to sense buffer */
1318 unsigned short sense_bufflen; /* length of the sense buffer, if any */
1320 /* Used for storage of target driver private stuff */
1323 /* Used for storage of dev handler private stuff */
1327 * Used to restore the SG vector if it was modified by
1328 * scst_set_resp_data_len()
1330 int orig_sg_cnt, orig_sg_entry, orig_entry_len;
1332 /* Used to retry commands in case of double UA */
1333 int dbl_ua_orig_resp_data_len, dbl_ua_orig_data_direction;
1335 /* List corresponding mgmt cmd, if any, protected by sess_list_lock */
1336 struct list_head mgmt_cmd_list;
1338 /* List entry for dev's blocked_cmd_list */
1339 struct list_head blocked_cmd_list_entry;
1341 struct scst_cmd *orig_cmd; /* Used to issue REQUEST SENSE */
1343 #ifdef CONFIG_SCST_MEASURE_LATENCY /* must be last */
1344 uint64_t start, pre_exec_finish, post_exec_start;
1348 struct scst_rx_mgmt_params {
1356 unsigned char tag_set;
1357 unsigned char lun_set;
1358 unsigned char cmd_sn_set;
1361 struct scst_mgmt_cmd_stub {
1362 struct scst_mgmt_cmd *mcmd;
1364 /* List entry in cmd->mgmt_cmd_list */
1365 struct list_head cmd_mgmt_cmd_list_entry;
1368 struct scst_mgmt_cmd {
1369 /* List entry for *_mgmt_cmd_list */
1370 struct list_head mgmt_cmd_list_entry;
1372 struct scst_session *sess;
1374 /* Mgmt cmd state, one of SCST_MCMD_STATE_* constants */
1379 unsigned int completed:1; /* set, if the mcmd is completed */
1380 /* Set if device(s) should be unblocked after mcmd's finish */
1381 unsigned int needs_unblocking:1;
1382 unsigned int lun_set:1; /* set, if lun field is valid */
1383 unsigned int cmd_sn_set:1; /* set, if cmd_sn field is valid */
1384 /* set, if scst_mgmt_affected_cmds_done was called */
1385 unsigned int affected_cmds_done_called:1;
1388 * Number of commands to finish before sending response,
1389 * protected by scst_mcmd_lock
1391 int cmd_finish_wait_count;
1394 * Number of commands to complete (done) before resetting reservation,
1395 * protected by scst_mcmd_lock
1397 int cmd_done_wait_count;
1399 /* Number of completed commands, protected by scst_mcmd_lock */
1400 int completed_cmd_count;
1402 uint64_t lun; /* LUN for this mgmt cmd */
1403 /* or (and for iSCSI) */
1404 uint64_t tag; /* tag of the corresponding cmd */
1406 uint32_t cmd_sn; /* affected command's highest SN */
1408 /* corresponding cmd (to be aborted, found by tag) */
1409 struct scst_cmd *cmd_to_abort;
1411 /* corresponding device for this mgmt cmd (found by lun) */
1412 struct scst_tgt_dev *mcmd_tgt_dev;
1414 /* completition status, one of the SCST_MGMT_STATUS_* constants */
1417 /* Used for storage of target driver private stuff */
1421 struct scst_device {
1422 struct scst_dev_type *handler; /* corresponding dev handler */
1424 struct scst_mem_lim dev_mem_lim;
1426 unsigned short type; /* SCSI type of the device */
1428 /*************************************************************
1429 ** Dev's flags. Updates serialized by dev_lock or suspended
1431 *************************************************************/
1433 /* Set if dev is RESERVED */
1434 unsigned short dev_reserved:1;
1436 /* Set if double reset UA is possible */
1437 unsigned short dev_double_ua_possible:1;
1439 /**************************************************************/
1441 /*************************************************************
1442 ** Dev's control mode page related values. Updates serialized
1443 ** by scst_block_dev(). It's long to not interfere with the
1445 *************************************************************/
1447 unsigned long queue_alg:4;
1448 unsigned long tst:3;
1449 unsigned long tas:1;
1450 unsigned long swp:1;
1451 unsigned long d_sense:1;
1454 * Set if device implements own ordered commands management. If not set
1455 * and queue_alg is SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER,
1456 * expected_sn will be incremented only after commands finished.
1458 unsigned long has_own_order_mgmt:1;
1460 /**************************************************************/
1462 /* Used for storage of dev handler private stuff */
1465 /* Used to translate SCSI's cmd to SCST's cmd */
1466 struct gendisk *rq_disk;
1468 /* Corresponding real SCSI device, could be NULL for virtual devices */
1469 struct scsi_device *scsi_dev;
1471 /* Pointer to lists of commands with the lock */
1472 struct scst_cmd_lists *p_cmd_lists;
1474 /* Lists of commands with lock, if dedicated threads are used */
1475 struct scst_cmd_lists cmd_lists;
1477 /* Per-device dedicated IO context */
1478 struct io_context *dev_io_ctx;
1480 /* How many cmds alive on this dev */
1481 atomic_t dev_cmd_count;
1483 /* How many write cmds alive on this dev. Temporary, ToDo */
1484 atomic_t write_cmd_count;
1486 spinlock_t dev_lock; /* device lock */
1489 * How many times device was blocked for new cmds execution.
1490 * Protected by dev_lock
1495 * How many there are "on_dev" commands, i.e. ones those are being
1496 * executed by the underlying SCSI/virtual device.
1498 atomic_t on_dev_count;
1500 struct list_head blocked_cmd_list; /* protected by dev_lock */
1502 /* Used to wait for requested amount of "on_dev" commands */
1503 wait_queue_head_t on_dev_waitQ;
1505 /* A list entry used during TM, protected by scst_mutex */
1506 struct list_head tm_dev_list_entry;
1508 /* Virtual device internal ID */
1511 /* Pointer to virtual device name, for convenience only */
1512 const char *virt_name;
1514 /* List entry in global devices list */
1515 struct list_head dev_list_entry;
1518 * List of tgt_dev's, one per session, protected by scst_mutex or
1519 * dev_lock for reads and both for writes
1521 struct list_head dev_tgt_dev_list;
1523 /* List of acg_dev's, one per acg, protected by scst_mutex */
1524 struct list_head dev_acg_dev_list;
1526 /* List of dedicated threads, protected by scst_mutex */
1527 struct list_head threads_list;
1534 * Used to store threads local tgt_dev specific data
1536 struct scst_thr_data_hdr {
1537 /* List entry in tgt_dev->thr_data_list */
1538 struct list_head thr_data_list_entry;
1539 struct task_struct *owner_thr; /* the owner thread */
1541 /* Function that will be called on the tgt_dev destruction */
1542 void (*free_fn) (struct scst_thr_data_hdr *data);
1546 * Used to store per-session specific device information
1548 struct scst_tgt_dev {
1549 /* List entry in sess->sess_tgt_dev_list_hash */
1550 struct list_head sess_tgt_dev_list_entry;
1552 struct scst_device *dev; /* to save extra dereferences */
1553 uint64_t lun; /* to save extra dereferences */
1556 struct sgv_pool *pool;
1559 unsigned long tgt_dev_flags; /* tgt_dev's async flags */
1561 /* Used for storage of dev handler private stuff */
1564 /* How many cmds alive on this dev in this session */
1565 atomic_t tgt_dev_cmd_count;
1568 * Used to execute cmd's in order of arrival, honoring SCSI task
1571 * Protected by sn_lock, except expected_sn, which is protected by
1572 * itself. Curr_sn must have the same size as expected_sn to
1573 * overflow simultaneously.
1577 unsigned long expected_sn;
1578 unsigned long curr_sn;
1580 struct list_head deferred_cmd_list;
1581 struct list_head skipped_sn_list;
1584 * Set if the prev cmd was ORDERED. Size must allow unprotected
1585 * modifications independant to the neighbour fields.
1587 unsigned long prev_cmd_ordered;
1589 int num_free_sn_slots; /* if it's <0, then all slots are busy */
1590 atomic_t *cur_sn_slot;
1591 atomic_t sn_slots[15];
1593 /* List of scst_thr_data_hdr and lock */
1594 spinlock_t thr_data_lock;
1595 struct list_head thr_data_list;
1597 /* Per-(device, session) dedicated IO context */
1598 struct io_context *tgt_dev_io_ctx;
1600 spinlock_t tgt_dev_lock; /* per-session device lock */
1602 /* List of UA's for this device, protected by tgt_dev_lock */
1603 struct list_head UA_list;
1605 struct scst_session *sess; /* corresponding session */
1606 struct scst_acg_dev *acg_dev; /* corresponding acg_dev */
1608 /* list entry in dev->dev_tgt_dev_list */
1609 struct list_head dev_tgt_dev_list_entry;
1611 /* internal tmp list entry */
1612 struct list_head extra_tgt_dev_list_entry;
1615 * Stored Unit Attention sense and its length for possible
1616 * subsequent REQUEST SENSE. Both protected by tgt_dev_lock.
1618 unsigned short tgt_dev_valid_sense_len;
1619 uint8_t tgt_dev_sense[SCST_SENSE_BUFFERSIZE];
1623 * Used to store ACG-specific device information, like LUN
1625 struct scst_acg_dev {
1626 struct scst_device *dev; /* corresponding device */
1627 uint64_t lun; /* device's LUN in this acg */
1628 unsigned int rd_only_flag:1; /* if != 0, then read only */
1629 struct scst_acg *acg; /* parent acg */
1631 /* list entry in dev->dev_acg_dev_list */
1632 struct list_head dev_acg_dev_list_entry;
1634 /* list entry in acg->acg_dev_list */
1635 struct list_head acg_dev_list_entry;
1639 * ACG - access control group. Used to store group related
1640 * control information.
1643 /* List of acg_dev's in this acg, protected by scst_mutex */
1644 struct list_head acg_dev_list;
1646 /* List of attached sessions, protected by scst_mutex */
1647 struct list_head acg_sess_list;
1649 /* List of attached acn's, protected by scst_mutex */
1650 struct list_head acn_list;
1652 /* List entry in scst_acg_list */
1653 struct list_head scst_acg_list_entry;
1655 /* Name of this acg */
1656 const char *acg_name;
1658 /* The pointer to the /proc directory entry */
1659 struct proc_dir_entry *acg_proc_root;
1663 * ACN - access control name. Used to store names, by which
1664 * incoming sessions will be assigned to appropriate ACG.
1667 /* Initiator's name */
1669 /* List entry in acg->acn_list */
1670 struct list_head acn_list_entry;
1674 * Used to store per-session UNIT ATTENTIONs
1676 struct scst_tgt_dev_UA {
1677 /* List entry in tgt_dev->UA_list */
1678 struct list_head UA_list_entry;
1680 /* Set if UA is global for session */
1681 unsigned int global_UA:1;
1683 /* Unit Attention sense */
1684 uint8_t UA_sense_buffer[SCST_SENSE_BUFFERSIZE];
1687 /* Used to deliver AENs */
1689 int event_fn; /* AEN fn */
1691 struct scst_session *sess; /* corresponding session */
1692 uint64_t lun; /* corresponding LUN in SCSI form */
1698 uint8_t aen_sense[SCST_STANDARD_SENSE_LEN];
1702 /* Keeps status of AEN's delivery to remote initiator */
1703 int delivery_status;
1706 #ifndef smp_mb__after_set_bit
1707 /* There is no smp_mb__after_set_bit() in the kernel */
1708 #define smp_mb__after_set_bit() smp_mb()
1712 * Registers target template
1713 * Returns 0 on success or appropriate error code otherwise
1715 int __scst_register_target_template(struct scst_tgt_template *vtt,
1716 const char *version);
1717 static inline int scst_register_target_template(struct scst_tgt_template *vtt)
1719 return __scst_register_target_template(vtt, SCST_INTERFACE_VERSION);
1723 * Unregisters target template
1725 void scst_unregister_target_template(struct scst_tgt_template *vtt);
1728 * Registers and returns target adapter
1729 * Returns new target structure on success or NULL otherwise.
1731 * If parameter "target_name" isn't NULL, then security group with name
1732 * "Default_##target_name", if created, will be used as the default
1733 * instead of "Default" one for all initiators not assigned to any other group.
1735 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
1736 const char *target_name);
1739 * Unregisters target adapter
1741 void scst_unregister(struct scst_tgt *tgt);
1744 * Registers and returns a session
1746 * Returns new session on success or NULL otherwise
1750 * atomic - true, if the function called in the atomic context. If false,
1751 * this function will block until the session registration is completed.
1752 * initiator_name - remote initiator's name, any NULL-terminated string,
1753 * e.g. iSCSI name, which used as the key to found appropriate access
1754 * control group. Could be NULL, then "default" group is used.
1755 * The groups are set up via /proc interface.
1756 * data - any target driver supplied data
1757 * result_fn - pointer to the function that will be
1758 * asynchronously called when session initialization finishes.
1759 * Can be NULL. Parameters:
1761 * - data - target driver supplied to scst_register_session() data
1762 * - result - session initialization result, 0 on success or
1763 * appropriate error code otherwise
1765 * Note: A session creation and initialization is a complex task,
1766 * which requires sleeping state, so it can't be fully done
1767 * in interrupt context. Therefore the "bottom half" of it, if
1768 * scst_register_session() is called from atomic context, will be
1769 * done in SCST thread context. In this case scst_register_session()
1770 * will return not completely initialized session, but the target
1771 * driver can supply commands to this session via scst_rx_cmd().
1772 * Those commands processing will be delayed inside SCST until
1773 * the session initialization is finished, then their processing
1774 * will be restarted. The target driver will be notified about
1775 * finish of the session initialization by function result_fn().
1776 * On success the target driver could do nothing, but if the
1777 * initialization fails, the target driver must ensure that
1778 * no more new commands being sent or will be sent to SCST after
1779 * result_fn() returns. All already sent to SCST commands for
1780 * failed session will be returned in xmit_response() with BUSY status.
1781 * In case of failure the driver shall call scst_unregister_session()
1782 * inside result_fn(), it will NOT be called automatically.
1784 struct scst_session *scst_register_session(struct scst_tgt *tgt, int atomic,
1785 const char *initiator_name, void *data,
1786 void (*result_fn) (struct scst_session *sess, void *data, int result));
1789 * Unregisters a session.
1791 * sess - session to be unregistered
1792 * wait - if true, instructs to wait until all commands, which
1793 * currently is being executed and belonged to the session, finished.
1794 * Otherwise, target driver should be prepared to receive
1795 * xmit_response() for the session's command after
1796 * scst_unregister_session() returns.
1797 * unreg_done_fn - pointer to the function that will be
1798 * asynchronously called when the last session's command finishes and
1799 * the session is about to be completely freed. Can be NULL.
1805 * - All outstanding commands will be finished regularly. After
1806 * scst_unregister_session() returned no new commands must be sent to
1807 * SCST via scst_rx_cmd().
1809 * - The caller must ensure that no scst_rx_cmd() or scst_rx_mgmt_fn_*() is
1810 * called in paralell with scst_unregister_session().
1812 * - Can be called before result_fn() of scst_register_session() called,
1813 * i.e. during the session registration/initialization.
1815 * - It is highly recommended to call scst_unregister_session() as soon as it
1816 * gets clear that session will be unregistered and not to wait until all
1817 * related commands finished. This function provides the wait functionality,
1818 * but it also starts recovering stuck commands, if there are any.
1819 * Otherwise, your target driver could wait for those commands forever.
1821 void scst_unregister_session(struct scst_session *sess, int wait,
1822 void (*unreg_done_fn) (struct scst_session *sess));
1825 * Registers dev handler driver
1826 * Returns 0 on success or appropriate error code otherwise
1828 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
1829 const char *version);
1830 static inline int scst_register_dev_driver(struct scst_dev_type *dev_type)
1832 return __scst_register_dev_driver(dev_type, SCST_INTERFACE_VERSION);
1836 * Unregisters dev handler driver
1838 void scst_unregister_dev_driver(struct scst_dev_type *dev_type);
1841 * Registers dev handler driver for virtual devices (eg VDISK)
1842 * Returns 0 on success or appropriate error code otherwise
1844 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
1845 const char *version);
1846 static inline int scst_register_virtual_dev_driver(
1847 struct scst_dev_type *dev_type)
1849 return __scst_register_virtual_dev_driver(dev_type,
1850 SCST_INTERFACE_VERSION);
1854 * Unregisters dev handler driver for virtual devices
1856 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type);
1859 * Creates and sends new command to SCST.
1860 * Must not be called in parallel with scst_unregister_session() for the
1861 * same sess. Returns the command on success or NULL otherwise
1863 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
1864 const uint8_t *lun, int lun_len, const uint8_t *cdb,
1865 int cdb_len, int atomic);
1868 * Notifies SCST that the driver finished its part of the command
1869 * initialization, and the command is ready for execution.
1870 * The second argument sets preferred command execition context.
1871 * See SCST_CONTEXT_* constants for details.
1875 * If cmd->set_sn_on_restart_cmd not set, this function, as well as
1876 * scst_cmd_init_stage1_done() and scst_restart_cmd(), must not be
1877 * called simultaneously for the same session (more precisely,
1878 * for the same session/LUN, i.e. tgt_dev), i.e. they must be
1879 * somehow externally serialized. This is needed to have lock free fast path in
1880 * scst_cmd_set_sn(). For majority of targets those functions are naturally
1881 * serialized by the single source of commands. Only iSCSI immediate commands
1882 * with multiple connections per session seems to be an exception. For it, some
1883 * mutex/lock shall be used for the serialization.
1885 void scst_cmd_init_done(struct scst_cmd *cmd,
1886 enum scst_exec_context pref_context);
1889 * Notifies SCST that the driver finished the first stage of the command
1890 * initialization, and the command is ready for execution, but after
1891 * SCST done the command's preprocessing preprocessing_done() function
1892 * should be called. The second argument sets preferred command execition
1893 * context. See SCST_CONTEXT_* constants for details.
1895 * See also scst_cmd_init_done() comment for the serialization requirements.
1897 static inline void scst_cmd_init_stage1_done(struct scst_cmd *cmd,
1898 enum scst_exec_context pref_context, int set_sn)
1900 cmd->preprocessing_only = 1;
1901 cmd->set_sn_on_restart_cmd = !set_sn;
1902 scst_cmd_init_done(cmd, pref_context);
1906 * Notifies SCST that the driver finished its part of the command's
1907 * preprocessing and it is ready for further processing.
1908 * The second argument sets data receiving completion status
1909 * (see SCST_PREPROCESS_STATUS_* constants for details)
1910 * The third argument sets preferred command execition context
1911 * (see SCST_CONTEXT_* constants for details).
1913 * See also scst_cmd_init_done() comment for the serialization requirements.
1915 void scst_restart_cmd(struct scst_cmd *cmd, int status,
1916 enum scst_exec_context pref_context);
1919 * Notifies SCST that the driver received all the necessary data
1920 * and the command is ready for further processing.
1921 * The second argument sets data receiving completion status
1922 * (see SCST_RX_STATUS_* constants for details)
1923 * The third argument sets preferred command execition context
1924 * (see SCST_CONTEXT_* constants for details)
1926 void scst_rx_data(struct scst_cmd *cmd, int status,
1927 enum scst_exec_context pref_context);
1930 * Notifies SCST that the driver sent the response and the command
1931 * can be freed now. Don't forget to set the delivery status, if it
1932 * isn't success, using scst_set_delivery_status() before calling
1933 * this function. The third argument sets preferred command execition
1934 * context (see SCST_CONTEXT_* constants for details)
1936 void scst_tgt_cmd_done(struct scst_cmd *cmd,
1937 enum scst_exec_context pref_context);
1940 * Creates new management command sends it for execution.
1941 * Must not be called in parallel with scst_unregister_session() for the
1942 * same sess. Returns 0 for success, error code otherwise.
1944 int scst_rx_mgmt_fn(struct scst_session *sess,
1945 const struct scst_rx_mgmt_params *params);
1948 * Creates new management command using tag and sends it for execution.
1949 * Can be used for SCST_ABORT_TASK only.
1950 * Must not be called in parallel with scst_unregister_session() for the
1951 * same sess. Returns 0 for success, error code otherwise.
1953 * Obsolete in favor of scst_rx_mgmt_fn()
1955 static inline int scst_rx_mgmt_fn_tag(struct scst_session *sess, int fn,
1956 uint64_t tag, int atomic, void *tgt_priv)
1958 struct scst_rx_mgmt_params params;
1960 BUG_ON(fn != SCST_ABORT_TASK);
1962 memset(¶ms, 0, sizeof(params));
1966 params.atomic = atomic;
1967 params.tgt_priv = tgt_priv;
1968 return scst_rx_mgmt_fn(sess, ¶ms);
1972 * Creates new management command using LUN and sends it for execution.
1973 * Currently can be used for any fn, except SCST_ABORT_TASK.
1974 * Must not be called in parallel with scst_unregister_session() for the
1975 * same sess. Returns 0 for success, error code otherwise.
1977 * Obsolete in favor of scst_rx_mgmt_fn()
1979 static inline int scst_rx_mgmt_fn_lun(struct scst_session *sess, int fn,
1980 const uint8_t *lun, int lun_len, int atomic, void *tgt_priv)
1982 struct scst_rx_mgmt_params params;
1984 BUG_ON(fn == SCST_ABORT_TASK);
1986 memset(¶ms, 0, sizeof(params));
1989 params.lun_len = lun_len;
1991 params.atomic = atomic;
1992 params.tgt_priv = tgt_priv;
1993 return scst_rx_mgmt_fn(sess, ¶ms);
1997 * Provides various info about command's CDB.
1999 * Returns: 0 on success, <0 if command is unknown, >0 if command is invalid.
2001 int scst_get_cdb_info(struct scst_cmd *cmd);
2004 * Set error SCSI status in the command and prepares it for returning it
2006 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status);
2009 * Set error in the command and fill the sense buffer
2011 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq);
2014 * Sets BUSY or TASK QUEUE FULL status
2016 void scst_set_busy(struct scst_cmd *cmd);
2019 * Check if sense in the sense buffer, if any, in the correct format. If not,
2020 * convert it to the correct format.
2022 void scst_check_convert_sense(struct scst_cmd *cmd);
2025 * Sets initial Unit Attention for sess, replacing default scst_sense_reset_UA
2027 void scst_set_initial_UA(struct scst_session *sess, int key, int asc, int ascq);
2030 * Notifies SCST core that dev changed its capacity
2032 void scst_capacity_data_changed(struct scst_device *dev);
2035 * Finds a command based on the supplied tag comparing it with one
2036 * that previously set by scst_cmd_set_tag().
2037 * Returns the command on success or NULL otherwise
2039 struct scst_cmd *scst_find_cmd_by_tag(struct scst_session *sess, uint64_t tag);
2042 * Finds a command based on user supplied data and comparision
2043 * callback function, that should return true, if the command is found.
2044 * Returns the command on success or NULL otherwise
2046 struct scst_cmd *scst_find_cmd(struct scst_session *sess, void *data,
2047 int (*cmp_fn) (struct scst_cmd *cmd,
2051 * Translates SCST's data direction to DMA one from backend storage
2054 enum dma_data_direction scst_to_dma_dir(int scst_dir);
2057 * Translates SCST data direction to DMA data direction from the perspective
2058 * of the target device.
2060 enum dma_data_direction scst_to_tgt_dma_dir(int scst_dir);
2063 * Returns 1, if cmd's CDB is locally handled by SCST and 0 otherwise.
2064 * Dev handlers parse() and dev_done() not called for such commands.
2066 static inline bool scst_is_cmd_local(struct scst_cmd *cmd)
2068 return (cmd->op_flags & SCST_LOCAL_CMD) != 0;
2072 * Registers a virtual device.
2074 * dev_type - the device's device handler
2075 * dev_name - the new device name, NULL-terminated string. Must be uniq
2076 * among all virtual devices in the system. The value isn't
2077 * copied, only the reference is stored, so the value must
2078 * remain valid during the device lifetime.
2079 * Returns assinged to the device ID on success, or negative value otherwise
2081 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
2082 const char *dev_name);
2085 * Unegisters a virtual device.
2087 * id - the device's ID, returned by the registration function
2089 void scst_unregister_virtual_device(int id);
2092 * Get/Set functions for tgt's sg_tablesize
2094 static inline int scst_tgt_get_sg_tablesize(struct scst_tgt *tgt)
2096 return tgt->sg_tablesize;
2099 static inline void scst_tgt_set_sg_tablesize(struct scst_tgt *tgt, int val)
2101 tgt->sg_tablesize = val;
2105 * Get/Set functions for tgt's target private data
2107 static inline void *scst_tgt_get_tgt_priv(struct scst_tgt *tgt)
2109 return tgt->tgt_priv;
2112 static inline void scst_tgt_set_tgt_priv(struct scst_tgt *tgt, void *val)
2114 tgt->tgt_priv = val;
2118 * Get/Set functions for session's target private data
2120 static inline void *scst_sess_get_tgt_priv(struct scst_session *sess)
2122 return sess->tgt_priv;
2125 static inline void scst_sess_set_tgt_priv(struct scst_session *sess,
2128 sess->tgt_priv = val;
2132 * Returns TRUE if cmd is being executed in atomic context.
2134 * Note: checkpatch will complain on the use of in_atomic() below. You can
2135 * safely ignore this warning since in_atomic() is used here only for debugging
2138 static inline int scst_cmd_atomic(struct scst_cmd *cmd)
2140 int res = cmd->atomic;
2141 #ifdef CONFIG_SCST_EXTRACHECKS
2142 if (unlikely((in_atomic() || in_interrupt() || irqs_disabled()) &&
2144 printk(KERN_ERR "ERROR: atomic context and non-atomic cmd\n");
2153 static inline enum scst_exec_context __scst_estimate_context(bool direct)
2156 return SCST_CONTEXT_TASKLET;
2157 else if (irqs_disabled())
2158 return SCST_CONTEXT_THREAD;
2160 return direct ? SCST_CONTEXT_DIRECT :
2161 SCST_CONTEXT_DIRECT_ATOMIC;
2164 static inline enum scst_exec_context scst_estimate_context(void)
2166 return __scst_estimate_context(0);
2169 static inline enum scst_exec_context scst_estimate_context_direct(void)
2171 return __scst_estimate_context(1);
2174 /* Returns cmd's CDB */
2175 static inline const uint8_t *scst_cmd_get_cdb(struct scst_cmd *cmd)
2180 /* Returns cmd's CDB length */
2181 static inline int scst_cmd_get_cdb_len(struct scst_cmd *cmd)
2183 return cmd->cdb_len;
2186 /* Returns cmd's extended CDB */
2187 static inline const uint8_t *scst_cmd_get_ext_cdb(struct scst_cmd *cmd)
2189 return cmd->ext_cdb;
2192 /* Returns cmd's extended CDB length */
2193 static inline int scst_cmd_get_ext_cdb_len(struct scst_cmd *cmd)
2195 return cmd->ext_cdb_len;
2198 /* Sets cmd's extended CDB and its length */
2199 static inline void scst_cmd_set_ext_cdb(struct scst_cmd *cmd,
2200 uint8_t *ext_cdb, unsigned int ext_cdb_len)
2202 cmd->ext_cdb = ext_cdb;
2203 cmd->ext_cdb_len = ext_cdb_len;
2206 /* Returns cmd's session */
2207 static inline struct scst_session *scst_cmd_get_session(struct scst_cmd *cmd)
2212 /* Returns cmd's response data length */
2213 static inline int scst_cmd_get_resp_data_len(struct scst_cmd *cmd)
2215 return cmd->resp_data_len;
2218 /* Returns if status should be sent for cmd */
2219 static inline int scst_cmd_get_is_send_status(struct scst_cmd *cmd)
2221 return cmd->is_send_status;
2225 * Returns pointer to cmd's SG data buffer.
2227 * Usage of this function is not recommended, use scst_get_buf_*()
2228 * family of functions instead.
2230 static inline struct scatterlist *scst_cmd_get_sg(struct scst_cmd *cmd)
2236 * Returns cmd's sg_cnt.
2238 * Usage of this function is not recommended, use scst_get_buf_*()
2239 * family of functions instead.
2241 static inline int scst_cmd_get_sg_cnt(struct scst_cmd *cmd)
2247 * Returns cmd's data buffer length.
2249 * In case if you need to iterate over data in the buffer, usage of
2250 * this function is not recommended, use scst_get_buf_*()
2251 * family of functions instead.
2253 static inline unsigned int scst_cmd_get_bufflen(struct scst_cmd *cmd)
2255 return cmd->bufflen;
2259 * Returns pointer to cmd's bidirectional in (WRITE) SG data buffer.
2261 * Usage of this function is not recommended, use scst_get_in_buf_*()
2262 * family of functions instead.
2264 static inline struct scatterlist *scst_cmd_get_in_sg(struct scst_cmd *cmd)
2270 * Returns cmd's bidirectional in (WRITE) sg_cnt.
2272 * Usage of this function is not recommended, use scst_get_in_buf_*()
2273 * family of functions instead.
2275 static inline int scst_cmd_get_in_sg_cnt(struct scst_cmd *cmd)
2277 return cmd->in_sg_cnt;
2281 * Returns cmd's bidirectional in (WRITE) data buffer length.
2283 * In case if you need to iterate over data in the buffer, usage of
2284 * this function is not recommended, use scst_get_in_buf_*()
2285 * family of functions instead.
2287 static inline unsigned int scst_cmd_get_in_bufflen(struct scst_cmd *cmd)
2289 return cmd->in_bufflen;
2292 /* Returns pointer to cmd's target's SG data buffer */
2293 static inline struct scatterlist *scst_cmd_get_tgt_sg(struct scst_cmd *cmd)
2298 /* Returns cmd's target's sg_cnt */
2299 static inline int scst_cmd_get_tgt_sg_cnt(struct scst_cmd *cmd)
2301 return cmd->tgt_sg_cnt;
2304 /* Sets cmd's target's SG data buffer */
2305 static inline void scst_cmd_set_tgt_sg(struct scst_cmd *cmd,
2306 struct scatterlist *sg, int sg_cnt)
2309 cmd->tgt_sg_cnt = sg_cnt;
2310 cmd->tgt_data_buf_alloced = 1;
2313 /* Returns pointer to cmd's target's IN SG data buffer */
2314 static inline struct scatterlist *scst_cmd_get_in_tgt_sg(struct scst_cmd *cmd)
2316 return cmd->tgt_in_sg;
2319 /* Returns cmd's target's IN sg_cnt */
2320 static inline int scst_cmd_get_tgt_in_sg_cnt(struct scst_cmd *cmd)
2322 return cmd->tgt_in_sg_cnt;
2325 /* Sets cmd's target's IN SG data buffer */
2326 static inline void scst_cmd_set_tgt_in_sg(struct scst_cmd *cmd,
2327 struct scatterlist *sg, int sg_cnt)
2329 WARN_ON(!cmd->tgt_data_buf_alloced);
2331 cmd->tgt_in_sg = sg;
2332 cmd->tgt_in_sg_cnt = sg_cnt;
2335 /* Returns cmd's data direction */
2336 static inline scst_data_direction scst_cmd_get_data_direction(
2337 struct scst_cmd *cmd)
2339 return cmd->data_direction;
2342 /* Returns cmd's status byte from host device */
2343 static inline uint8_t scst_cmd_get_status(struct scst_cmd *cmd)
2348 /* Returns cmd's status from host adapter itself */
2349 static inline uint8_t scst_cmd_get_msg_status(struct scst_cmd *cmd)
2351 return cmd->msg_status;
2354 /* Returns cmd's status set by low-level driver to indicate its status */
2355 static inline uint8_t scst_cmd_get_host_status(struct scst_cmd *cmd)
2357 return cmd->host_status;
2360 /* Returns cmd's status set by SCSI mid-level */
2361 static inline uint8_t scst_cmd_get_driver_status(struct scst_cmd *cmd)
2363 return cmd->driver_status;
2366 /* Returns pointer to cmd's sense buffer */
2367 static inline uint8_t *scst_cmd_get_sense_buffer(struct scst_cmd *cmd)
2372 /* Returns cmd's sense buffer length */
2373 static inline int scst_cmd_get_sense_buffer_len(struct scst_cmd *cmd)
2375 return cmd->sense_bufflen;
2379 * Get/Set functions for cmd's target SN
2381 static inline uint64_t scst_cmd_get_tag(struct scst_cmd *cmd)
2386 static inline void scst_cmd_set_tag(struct scst_cmd *cmd, uint64_t tag)
2392 * Get/Set functions for cmd's target private data.
2393 * Variant with *_lock must be used if target driver uses
2394 * scst_find_cmd() to avoid race with it, except inside scst_find_cmd()'s
2395 * callback, where lock is already taken.
2397 static inline void *scst_cmd_get_tgt_priv(struct scst_cmd *cmd)
2399 return cmd->tgt_priv;
2402 static inline void scst_cmd_set_tgt_priv(struct scst_cmd *cmd, void *val)
2404 cmd->tgt_priv = val;
2407 void *scst_cmd_get_tgt_priv_lock(struct scst_cmd *cmd);
2408 void scst_cmd_set_tgt_priv_lock(struct scst_cmd *cmd, void *val);
2411 * Get/Set functions for tgt_need_alloc_data_buf flag
2413 static inline int scst_cmd_get_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2415 return cmd->tgt_need_alloc_data_buf;
2418 static inline void scst_cmd_set_tgt_need_alloc_data_buf(struct scst_cmd *cmd)
2420 cmd->tgt_need_alloc_data_buf = 1;
2424 * Get/Set functions for tgt_data_buf_alloced flag
2426 static inline int scst_cmd_get_tgt_data_buff_alloced(struct scst_cmd *cmd)
2428 return cmd->tgt_data_buf_alloced;
2431 static inline void scst_cmd_set_tgt_data_buff_alloced(struct scst_cmd *cmd)
2433 cmd->tgt_data_buf_alloced = 1;
2437 * Get/Set functions for dh_data_buf_alloced flag
2439 static inline int scst_cmd_get_dh_data_buff_alloced(struct scst_cmd *cmd)
2441 return cmd->dh_data_buf_alloced;
2444 static inline void scst_cmd_set_dh_data_buff_alloced(struct scst_cmd *cmd)
2446 cmd->dh_data_buf_alloced = 1;
2450 * Get/Set functions for no_sgv flag
2452 static inline int scst_cmd_get_no_sgv(struct scst_cmd *cmd)
2457 static inline void scst_cmd_set_no_sgv(struct scst_cmd *cmd)
2463 * Get/Set functions for tgt_sn
2465 static inline int scst_cmd_get_tgt_sn(struct scst_cmd *cmd)
2467 BUG_ON(!cmd->tgt_sn_set);
2471 static inline void scst_cmd_set_tgt_sn(struct scst_cmd *cmd, uint32_t tgt_sn)
2473 cmd->tgt_sn_set = 1;
2474 cmd->tgt_sn = tgt_sn;
2478 * Returns 1 if the cmd was aborted, so its status is invalid and no
2479 * reply shall be sent to the remote initiator. A target driver should
2480 * only clear internal resources, associated with cmd.
2482 static inline int scst_cmd_aborted(struct scst_cmd *cmd)
2484 return test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags) &&
2485 !test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
2488 /* Returns sense data format for cmd's dev */
2489 static inline bool scst_get_cmd_dev_d_sense(struct scst_cmd *cmd)
2491 return (cmd->dev != NULL) ? cmd->dev->d_sense : 0;
2495 * Get/Set functions for expected data direction, transfer length
2496 * and its validity flag
2498 static inline int scst_cmd_is_expected_set(struct scst_cmd *cmd)
2500 return cmd->expected_values_set;
2503 static inline scst_data_direction scst_cmd_get_expected_data_direction(
2504 struct scst_cmd *cmd)
2506 return cmd->expected_data_direction;
2509 static inline int scst_cmd_get_expected_transfer_len(
2510 struct scst_cmd *cmd)
2512 return cmd->expected_transfer_len;
2515 static inline int scst_cmd_get_expected_in_transfer_len(
2516 struct scst_cmd *cmd)
2518 return cmd->expected_in_transfer_len;
2521 static inline void scst_cmd_set_expected(struct scst_cmd *cmd,
2522 scst_data_direction expected_data_direction,
2523 int expected_transfer_len)
2525 cmd->expected_data_direction = expected_data_direction;
2526 cmd->expected_transfer_len = expected_transfer_len;
2527 cmd->expected_values_set = 1;
2530 static inline void scst_cmd_set_expected_in_transfer_len(struct scst_cmd *cmd,
2531 int expected_in_transfer_len)
2533 WARN_ON(!cmd->expected_values_set);
2534 cmd->expected_in_transfer_len = expected_in_transfer_len;
2538 * Get/clear functions for cmd's may_need_dma_sync
2540 static inline int scst_get_may_need_dma_sync(struct scst_cmd *cmd)
2542 return cmd->may_need_dma_sync;
2545 static inline void scst_clear_may_need_dma_sync(struct scst_cmd *cmd)
2547 cmd->may_need_dma_sync = 0;
2551 * Get/set functions for cmd's delivery_status. It is one of
2552 * SCST_CMD_DELIVERY_* constants. It specifies the status of the
2553 * command's delivery to initiator.
2555 static inline int scst_get_delivery_status(struct scst_cmd *cmd)
2557 return cmd->delivery_status;
2560 static inline void scst_set_delivery_status(struct scst_cmd *cmd,
2561 int delivery_status)
2563 cmd->delivery_status = delivery_status;
2567 * Get/Set function for mgmt cmd's target private data
2569 static inline void *scst_mgmt_cmd_get_tgt_priv(struct scst_mgmt_cmd *mcmd)
2571 return mcmd->tgt_priv;
2574 static inline void scst_mgmt_cmd_set_tgt_priv(struct scst_mgmt_cmd *mcmd,
2577 mcmd->tgt_priv = val;
2580 /* Returns mgmt cmd's completition status (SCST_MGMT_STATUS_* constants) */
2581 static inline int scst_mgmt_cmd_get_status(struct scst_mgmt_cmd *mcmd)
2583 return mcmd->status;
2586 /* Returns mgmt cmd's TM fn */
2587 static inline int scst_mgmt_cmd_get_fn(struct scst_mgmt_cmd *mcmd)
2593 * Called by dev handler's task_mgmt_fn() to notify SCST core that mcmd
2594 * is going to complete asynchronously.
2596 void scst_prepare_async_mcmd(struct scst_mgmt_cmd *mcmd);
2599 * Called by dev handler to notify SCST core that async. mcmd is completed
2600 * with status "status".
2602 void scst_async_mcmd_completed(struct scst_mgmt_cmd *mcmd, int status);
2604 /* Returns AEN's fn */
2605 static inline int scst_aen_get_event_fn(struct scst_aen *aen)
2607 return aen->event_fn;
2610 /* Returns AEN's session */
2611 static inline struct scst_session *scst_aen_get_sess(struct scst_aen *aen)
2616 /* Returns AEN's LUN */
2617 static inline uint64_t scst_aen_get_lun(struct scst_aen *aen)
2622 /* Returns SCSI AEN's sense */
2623 static inline const uint8_t *scst_aen_get_sense(struct scst_aen *aen)
2625 return aen->aen_sense;
2628 /* Returns SCSI AEN's sense length */
2629 static inline int scst_aen_get_sense_len(struct scst_aen *aen)
2631 return aen->aen_sense_len;
2635 * Get/set functions for AEN's delivery_status. It is one of
2636 * SCST_AEN_RES_* constants. It specifies the status of the
2637 * command's delivery to initiator.
2639 static inline int scst_get_aen_delivery_status(struct scst_aen *aen)
2641 return aen->delivery_status;
2644 static inline void scst_set_aen_delivery_status(struct scst_aen *aen,
2647 aen->delivery_status = status;
2651 * Notifies SCST that the driver has sent the AEN and it
2652 * can be freed now. Don't forget to set the delivery status, if it
2653 * isn't success, using scst_set_aen_delivery_status() before calling
2656 void scst_aen_done(struct scst_aen *aen);
2658 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
2661 * The macro's sg_page(), sg_virt(), sg_init_table(), sg_assign_page() and
2662 * sg_set_page() have been introduced in the 2.6.24 kernel. The definitions
2663 * below are backports of the 2.6.24 macro's for older kernels. There is one
2664 * exception however: when compiling SCST on a system with a pre-2.6.24 kernel
2665 * (e.g. RHEL 5.x) where the OFED kernel headers have been installed, do not
2666 * define the backported macro's because OFED has already defined these.
2669 #ifndef __BACKPORT_LINUX_SCATTERLIST_H_TO_2_6_23__
2671 static inline struct page *sg_page(struct scatterlist *sg)
2676 static inline void *sg_virt(struct scatterlist *sg)
2678 return page_address(sg_page(sg)) + sg->offset;
2681 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
2683 memset(sgl, 0, sizeof(*sgl) * nents);
2686 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
2691 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
2692 unsigned int len, unsigned int offset)
2694 sg_assign_page(sg, page);
2695 sg->offset = offset;
2701 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
2703 static inline void sg_clear(struct scatterlist *sg)
2705 memset(sg, 0, sizeof(*sg));
2706 #ifdef CONFIG_DEBUG_SG
2707 sg->sg_magic = SG_MAGIC;
2712 * Functions for access to the commands data (SG) buffer,
2713 * including HIGHMEM environment. Should be used instead of direct
2714 * access. Returns the mapped buffer length for success, 0 for EOD,
2715 * negative error code otherwise.
2717 * "Buf" argument returns the mapped buffer
2719 * The "put" function unmaps the buffer.
2721 static inline int __scst_get_buf(struct scst_cmd *cmd, struct scatterlist *sg,
2722 int sg_cnt, uint8_t **buf)
2725 int i = cmd->get_sg_buf_entry_num;
2729 if ((i >= sg_cnt) || unlikely(sg == NULL))
2732 *buf = page_address(sg_page(&sg[i]));
2733 *buf += sg[i].offset;
2736 cmd->get_sg_buf_entry_num++;
2742 static inline int scst_get_buf_first(struct scst_cmd *cmd, uint8_t **buf)
2744 cmd->get_sg_buf_entry_num = 0;
2745 cmd->may_need_dma_sync = 1;
2746 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
2749 static inline int scst_get_buf_next(struct scst_cmd *cmd, uint8_t **buf)
2751 return __scst_get_buf(cmd, cmd->sg, cmd->sg_cnt, buf);
2754 static inline void scst_put_buf(struct scst_cmd *cmd, void *buf)
2759 static inline int scst_get_in_buf_first(struct scst_cmd *cmd, uint8_t **buf)
2761 cmd->get_sg_buf_entry_num = 0;
2762 cmd->may_need_dma_sync = 1;
2763 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
2766 static inline int scst_get_in_buf_next(struct scst_cmd *cmd, uint8_t **buf)
2768 return __scst_get_buf(cmd, cmd->in_sg, cmd->in_sg_cnt, buf);
2771 static inline void scst_put_in_buf(struct scst_cmd *cmd, void *buf)
2777 * Returns approximate higher rounded buffers count that
2778 * scst_get_buf_[first|next]() return.
2780 static inline int scst_get_buf_count(struct scst_cmd *cmd)
2782 return (cmd->sg_cnt == 0) ? 1 : cmd->sg_cnt;
2786 * Returns approximate higher rounded buffers count that
2787 * scst_get_in_buf_[first|next]() return.
2789 static inline int scst_get_in_buf_count(struct scst_cmd *cmd)
2791 return (cmd->in_sg_cnt == 0) ? 1 : cmd->in_sg_cnt;
2795 * Suspends and resumes any activity.
2796 * Function scst_suspend_activity() doesn't return 0, until there are any
2797 * active commands (state after SCST_CMD_STATE_INIT). If "interruptible"
2798 * is true, it returns after SCST_SUSPENDING_TIMEOUT or if it was interrupted
2799 * by a signal with the coresponding error status < 0. If "interruptible"
2800 * is false, it will wait virtually forever.
2802 * New arriving commands stay in that state until scst_resume_activity()
2805 int scst_suspend_activity(bool interruptible);
2806 void scst_resume_activity(void);
2809 * Main SCST commands processing routing. Must be used only by dev handlers.
2810 * Argument atomic is true if function called in atomic context.
2812 void scst_process_active_cmd(struct scst_cmd *cmd, bool atomic);
2815 * Checks if command can be executed (reservations, etc.) or there are local
2816 * events, like pending UAs. Returns < 0 if command must be aborted, > 0 if
2817 * there is an event and command should be immediately completed, or 0
2820 * !! Dev handlers implementing exec() callback must call this function there !!
2821 * !! just before the actual command's execution !!
2823 int scst_check_local_events(struct scst_cmd *cmd);
2826 * Returns the next state of the SCSI target state machine in case if command's
2827 * completed abnormally.
2829 int scst_get_cmd_abnormal_done_state(const struct scst_cmd *cmd);
2832 * Sets state of the SCSI target state machine in case if command's completed
2835 void scst_set_cmd_abnormal_done_state(struct scst_cmd *cmd);
2838 * Returns target driver's root entry in SCST's /proc hierarchy.
2839 * The driver can create own files/directoryes here, which should
2840 * be deleted in the driver's release().
2842 static inline struct proc_dir_entry *scst_proc_get_tgt_root(
2843 struct scst_tgt_template *vtt)
2845 return vtt->proc_tgt_root;
2849 * Returns device handler's root entry in SCST's /proc hierarchy.
2850 * The driver can create own files/directoryes here, which should
2851 * be deleted in the driver's detach()/release().
2853 static inline struct proc_dir_entry *scst_proc_get_dev_type_root(
2854 struct scst_dev_type *dtt)
2856 return dtt->proc_dev_type_root;
2860 ** Two library functions and the structure to help the drivers
2861 ** that use scst_debug.* facilities manage "trace_level" /proc entry.
2862 ** The functions service "standard" log levels and allow to work
2863 ** with driver specific levels, which should be passed inside as
2864 ** NULL-terminated array of struct scst_proc_log's, where:
2865 ** - val - the level's numeric value
2866 ** - token - its string representation
2869 struct scst_proc_log {
2874 int scst_proc_log_entry_read(struct seq_file *seq, unsigned long log_level,
2875 const struct scst_proc_log *tbl);
2877 int scst_proc_log_entry_write(struct file *file, const char __user *buf,
2878 unsigned long length, unsigned long *log_level,
2879 unsigned long default_level, const struct scst_proc_log *tbl);
2882 * helper data structure and function to create proc entry.
2884 struct scst_proc_data {
2885 const struct file_operations seq_op;
2886 int (*show)(struct seq_file *, void *);
2890 int scst_single_seq_open(struct inode *inode, struct file *file);
2892 struct proc_dir_entry *scst_create_proc_entry(struct proc_dir_entry *root,
2893 const char *name, struct scst_proc_data *pdata);
2895 #define SCST_DEF_RW_SEQ_OP(x) \
2897 .owner = THIS_MODULE, \
2898 .open = scst_single_seq_open,\
2901 .llseek = seq_lseek, \
2902 .release = single_release, \
2906 * Adds and deletes (stops) num of global SCST's threads. Returns 0 on
2907 * success, error code otherwise.
2909 int scst_add_global_threads(int num);
2910 void scst_del_global_threads(int num);
2912 int scst_alloc_sense(struct scst_cmd *cmd, int atomic);
2913 int scst_alloc_set_sense(struct scst_cmd *cmd, int atomic,
2914 const uint8_t *sense, unsigned int len);
2916 void scst_set_sense(uint8_t *buffer, int len, bool d_sense,
2917 int key, int asc, int ascq);
2920 * Returnes true if sense matches to (key, asc, ascq) and false otherwise.
2921 * Valid_mask is one or several SCST_SENSE_*_VALID constants setting valid
2922 * (key, asc, ascq) values.
2924 bool scst_analyze_sense(const uint8_t *sense, int len,
2925 unsigned int valid_mask, int key, int asc, int ascq);
2928 * Returnes a pseudo-random number for debugging purposes. Available only in
2931 unsigned long scst_random(void);
2934 * Sets response data length for cmd and truncates its SG vector accordingly.
2935 * The cmd->resp_data_len must not be set directly, it must be set only
2936 * using this function. Value of resp_data_len must be <= cmd->bufflen.
2938 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len);
2941 * Get/put global ref counter that prevents from entering into suspended
2942 * activities stage, so protects from any global management operations.
2944 void scst_get(void);
2945 void scst_put(void);
2950 void scst_cmd_get(struct scst_cmd *cmd);
2951 void scst_cmd_put(struct scst_cmd *cmd);
2954 * Allocates and returns pointer to SG vector with data size "size".
2955 * In *count returned the count of entries in the vector.
2956 * Returns NULL for failure.
2958 struct scatterlist *scst_alloc(int size, gfp_t gfp_mask, int *count);
2960 /* Frees SG vector returned by scst_alloc() */
2961 void scst_free(struct scatterlist *sg, int count);
2964 * Adds local to the current thread data to tgt_dev
2965 * (they will be local for the tgt_dev and current thread).
2967 void scst_add_thr_data(struct scst_tgt_dev *tgt_dev,
2968 struct scst_thr_data_hdr *data,
2969 void (*free_fn) (struct scst_thr_data_hdr *data));
2971 /* Deletes all local to threads data from tgt_dev */
2972 void scst_del_all_thr_data(struct scst_tgt_dev *tgt_dev);
2974 /* Deletes all local to threads data from all tgt_dev's of the dev */
2975 void scst_dev_del_all_thr_data(struct scst_device *dev);
2977 /* Finds local to the thread data. Returns NULL, if they not found. */
2978 struct scst_thr_data_hdr *__scst_find_thr_data(struct scst_tgt_dev *tgt_dev,
2979 struct task_struct *tsk);
2981 /* Finds local to the current thread data. Returns NULL, if they not found. */
2982 static inline struct scst_thr_data_hdr *scst_find_thr_data(
2983 struct scst_tgt_dev *tgt_dev)
2985 return __scst_find_thr_data(tgt_dev, current);
2988 static inline void scst_thr_data_get(struct scst_thr_data_hdr *data)
2990 atomic_inc(&data->ref);
2993 static inline void scst_thr_data_put(struct scst_thr_data_hdr *data)
2995 if (atomic_dec_and_test(&data->ref))
2996 data->free_fn(data);
3000 ** Generic parse() support routines.
3001 ** Done via pointer on functions to avoid unneeded dereferences on
3005 /* Calculates and returns block shift for the given sector size */
3006 int scst_calc_block_shift(int sector_size);
3008 /* Generic parse() for SBC (disk) devices */
3009 int scst_sbc_generic_parse(struct scst_cmd *cmd,
3010 int (*get_block_shift)(struct scst_cmd *cmd));
3012 /* Generic parse() for MMC (cdrom) devices */
3013 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
3014 int (*get_block_shift)(struct scst_cmd *cmd));
3016 /* Generic parse() for MO disk devices */
3017 int scst_modisk_generic_parse(struct scst_cmd *cmd,
3018 int (*get_block_shift)(struct scst_cmd *cmd));
3020 /* Generic parse() for tape devices */
3021 int scst_tape_generic_parse(struct scst_cmd *cmd,
3022 int (*get_block_size)(struct scst_cmd *cmd));
3024 /* Generic parse() for changer devices */
3025 int scst_changer_generic_parse(struct scst_cmd *cmd,
3026 int (*nothing)(struct scst_cmd *cmd));
3028 /* Generic parse() for "processor" devices */
3029 int scst_processor_generic_parse(struct scst_cmd *cmd,
3030 int (*nothing)(struct scst_cmd *cmd));
3032 /* Generic parse() for RAID devices */
3033 int scst_raid_generic_parse(struct scst_cmd *cmd,
3034 int (*nothing)(struct scst_cmd *cmd));
3037 ** Generic dev_done() support routines.
3038 ** Done via pointer on functions to avoid unneeded dereferences on
3042 /* Generic dev_done() for block devices */
3043 int scst_block_generic_dev_done(struct scst_cmd *cmd,
3044 void (*set_block_shift)(struct scst_cmd *cmd, int block_shift));
3046 /* Generic dev_done() for tape devices */
3047 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
3048 void (*set_block_size)(struct scst_cmd *cmd, int block_size));
3051 * Issues a MODE SENSE for control mode page data and sets the corresponding
3052 * dev's parameter from it. Returns 0 on success and not 0 otherwise.
3054 int scst_obtain_device_parameters(struct scst_device *dev);
3056 #endif /* __SCST_H */