4 * Copyright (C) 2004-2006 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Main SCSI target mid-level include file.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, version 2
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
23 #include <linux/types.h>
24 #include <linux/version.h>
25 #include <linux/blkdev.h>
26 #include <linux/interrupt.h>
28 #include <asm/kmap_types.h>
30 #include <../drivers/scsi/scsi.h>
32 /* Version numbers, the same as for the kernel */
33 #define SCST_VERSION_CODE 0x000906
34 #define SCST_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
35 #define SCST_VERSION_STRING "0.9.6-pre1"
37 /*************************************************************
38 ** States of command processing state machine
39 *************************************************************/
41 /* A cmd is created, but scst_cmd_init_done() not called */
42 #define SCST_CMD_STATE_INIT_WAIT 1
44 /* LUN translation (cmd->tgt_dev assignment) */
45 #define SCST_CMD_STATE_INIT 2
48 * Again LUN translation (cmd->tgt_dev assignment), used if dev handler
49 * wants to restart cmd on another LUN
51 #define SCST_CMD_STATE_REINIT 3
53 /* Dev handler's parse() is going to be called */
54 #define SCST_CMD_STATE_DEV_PARSE 4
56 /* Allocation of the cmd's data buffer */
57 #define SCST_CMD_STATE_PREPARE_SPACE 5
59 /* Target driver's rdy_to_xfer() is going to be called */
60 #define SCST_CMD_STATE_RDY_TO_XFER 6
62 /* Waiting for data from the initiator (until scst_rx_data() called) */
63 #define SCST_CMD_STATE_DATA_WAIT 7
65 /* CDB is going to be sent to SCSI mid-level for execution */
66 #define SCST_CMD_STATE_SEND_TO_MIDLEV 8
68 /* Waiting for CDB's execution finish */
69 #define SCST_CMD_STATE_EXECUTING 9
71 /* Dev handler's dev_done() is going to be called */
72 #define SCST_CMD_STATE_DEV_DONE 10
74 /* Target driver's xmit_response() is going to be called */
75 #define SCST_CMD_STATE_XMIT_RESP 11
77 /* Waiting for response's transmission finish */
78 #define SCST_CMD_STATE_XMIT_WAIT 12
80 /* The cmd finished */
81 #define SCST_CMD_STATE_FINISHED 14
83 /*************************************************************
84 ** Can be retuned instead of cmd's state by dev handlers'
85 ** functions, if the command's state should be set by default
86 *************************************************************/
87 #define SCST_CMD_STATE_DEFAULT 0
89 /*************************************************************
90 ** Can be retuned instead of cmd's state by dev handlers'
91 ** functions, if it is impossible to complete requested
92 ** task in atomic context. The cmd will be restarted in thread
94 *************************************************************/
95 #define SCST_CMD_STATE_NEED_THREAD_CTX 100
97 /*************************************************************
98 ** States of mgmt command processing state machine
99 *************************************************************/
101 /* LUN translation (mcmd->tgt_dev assignment) */
102 #define SCST_MGMT_CMD_STATE_INIT 1
104 /* Mgmt cmd is ready for processing */
105 #define SCST_MGMT_CMD_STATE_READY 2
107 /* Mgmt cmd is being executing */
108 #define SCST_MGMT_CMD_STATE_EXECUTING 3
110 /* Target driver's task_mgmt_fn_done() is going to be called */
111 #define SCST_MGMT_CMD_STATE_DONE 4
113 /* The mcmd finished */
114 #define SCST_MGMT_CMD_STATE_FINISHED 5
116 /*************************************************************
117 ** Constants for "atomic" parameter of SCST's functions
118 *************************************************************/
119 #define SCST_NON_ATOMIC 0
120 #define SCST_ATOMIC 1
122 /*************************************************************
123 ** Values for pref_context parameter of scst_cmd_init_done() and
125 *************************************************************/
128 * Direct cmd's processing (i.e. regular function calls in the current
129 * context), sleeping is allowed, no restrictions
131 #define SCST_CONTEXT_DIRECT 0
134 * Direct cmd's processing (i.e. regular function calls in the current
135 * context) sleeping is not allowed
137 #define SCST_CONTEXT_DIRECT_ATOMIC 1
139 /* Tasklet or thread context required for cmd's processing */
140 #define SCST_CONTEXT_TASKLET 2
142 /* Thread context required for cmd's processing */
143 #define SCST_CONTEXT_THREAD 3
146 * Additional bit to set processible environment.
147 * Private for SCST, ie must not be used target drivers.
149 #define SCST_PROCESSIBLE_ENV 0x10000000
151 /*************************************************************
152 ** Values for status parameter of scst_rx_data()
153 *************************************************************/
156 #define SCST_RX_STATUS_SUCCESS 0
159 * Data receiving finished with error, so set the sense and
160 * finish the command, including xmit_response() call
162 #define SCST_RX_STATUS_ERROR 1
165 * Data receiving finished with error and the sense is set,
166 * so finish the command, including xmit_response() call
168 #define SCST_RX_STATUS_ERROR_SENSE_SET 2
171 * Data receiving finished with fatal error, so finish the command,
172 * but don't call xmit_response()
174 #define SCST_RX_STATUS_ERROR_FATAL 3
176 /*************************************************************
177 ** Allowed return codes for xmit_response(), rdy_to_xfer(),
179 *************************************************************/
182 #define SCST_TGT_RES_SUCCESS 0
184 /* Internal device queue is full, retry again later */
185 #define SCST_TGT_RES_QUEUE_FULL -1
188 * It is impossible to complete requested task in atomic context.
189 * The cmd will be restarted in thread context.
191 #define SCST_TGT_RES_NEED_THREAD_CTX -2
194 * Fatal error, if returned by xmit_response() the cmd will
195 * be destroyed, if by any other function, xmit_response()
196 * will be called with HARDWARE ERROR sense data
198 #define SCST_TGT_RES_FATAL_ERROR -3
200 /*************************************************************
201 ** Allowed return codes for dev handler's exec()
202 *************************************************************/
204 /* The cmd is done, go to other ones */
205 #define SCST_EXEC_COMPLETED 0
207 /* The cmd should be sent to SCSI mid-level */
208 #define SCST_EXEC_NOT_COMPLETED 1
211 * Thread context is required to execute the command.
212 * Exec() will be called again in the thread context.
214 #define SCST_EXEC_NEED_THREAD 2
216 /*************************************************************
217 ** Allowed return codes for dev handler's task_mgmt_fn()
218 *************************************************************/
220 /* The command is done with success, no firther actions required */
221 #define SCST_DEV_TM_COMPLETED_SUCCESS 0
223 /* The command is failed, no firther actions required */
224 #define SCST_DEV_TM_COMPLETED_FAILED -1
226 /* Regular standard actions for the command should be done */
227 #define SCST_DEV_TM_NOT_COMPLETED 1
229 /*************************************************************
230 ** Default timeout for cmd's CDB execution
231 ** by SCSI mid-level (cmd's "timeout" field).
232 *************************************************************/
233 #define SCST_DEFAULT_TIMEOUT (30*HZ)
235 /*************************************************************
236 ** Data direction aliases
237 *************************************************************/
238 #define SCST_DATA_UNKNOWN DMA_BIDIRECTIONAL
239 #define SCST_DATA_WRITE DMA_TO_DEVICE
240 #define SCST_DATA_READ DMA_FROM_DEVICE
241 #define SCST_DATA_NONE DMA_NONE
243 /*************************************************************
244 ** Flags of cmd->tgt_resp_flags
245 *************************************************************/
248 * Set if cmd is finished and there is status/sense to be sent.
249 * The status should be not sent (i.e. the flag not set) if the
250 * possibility to perform a command in "chunks" (i.e. with multiple
251 * xmit_response()/rdy_to_xfer()) is used (not implemented and,
252 * probably, will never be).
254 #define SCST_TSC_FLAG_STATUS 0x2
256 /*************************************************************
257 ** Values for management functions
258 *************************************************************/
259 #define SCST_ABORT_TASK 0
260 #define SCST_ABORT_TASK_SET 1
261 #define SCST_CLEAR_ACA 2
262 #define SCST_CLEAR_TASK_SET 3
263 #define SCST_LUN_RESET 4
264 #define SCST_TARGET_RESET 5
266 /** SCST extensions **/
269 * Notifies about I_T nexus loss event in the corresponding session.
270 * Aborts all tasks there, resets the reservation, if any, and sets
271 * up the I_T Nexus loss UA.
273 #define SCST_NEXUS_LOSS_SESS 6
275 /* Aborts all tasks in the corresponding session with TASK_ABORTED status */
276 #define SCST_ABORT_ALL_TASKS_SESS 7
279 * Notifies about I_T nexus loss event. Aborts all tasks in all sessions
280 * of the tgt, resets the reservations, if any, and sets up the I_T Nexus
283 #define SCST_NEXUS_LOSS 8
285 /* Aborts all tasks in all sessions of the tgt with TASK_ABORTED status */
286 #define SCST_ABORT_ALL_TASKS 9
288 /*************************************************************
289 ** Values for mgmt cmd's "status"
290 *************************************************************/
291 #define SCST_MGMT_STATUS_SUCCESS 0
292 #define SCST_MGMT_STATUS_FAILED -1
294 /*************************************************************
295 ** Session initialization phases
296 *************************************************************/
298 /* Set if session is being initialized */
299 #define SCST_SESS_IPH_INITING 0
301 /* Set if the session is successfully initialized */
302 #define SCST_SESS_IPH_SUCCESS 1
304 /* Set if the session initialization failed */
305 #define SCST_SESS_IPH_FAILED 2
307 /* Set if session is initialized and ready */
308 #define SCST_SESS_IPH_READY 3
310 /*************************************************************
311 ** Cmd's async (atomic) flags
312 *************************************************************/
314 /* Set if the cmd is aborted and ABORTED sense will be sent as the result */
315 #define SCST_CMD_ABORTED 0
317 /* Set if the cmd is aborted by other initiator */
318 #define SCST_CMD_ABORTED_OTHER 1
320 /* Set if no response should be sent to the target about this cmd */
321 #define SCST_CMD_NO_RESP 2
324 * Set if the cmd is being executed. Needed to guarantee that
325 * "no further responses from the task are sent to
326 * the SCSI initiator port" after response from the TM function is
327 * sent (SAM) as well as correct ABORT TASK status code
329 #define SCST_CMD_EXECUTING 3
332 * Set if the cmd status/data are being xmitted. The purpose is the
333 * same as for SCST_CMD_EXECUTING
335 #define SCST_CMD_XMITTING 4
337 /* Set if the cmd was done or aborted out of its SN */
338 #define SCST_CMD_OUT_OF_SN 5
340 /* Set if the cmd is dead and can be destroyed at any time */
341 #define SCST_CMD_CAN_BE_DESTROYED 6
343 /*************************************************************
345 *************************************************************/
347 /* Set if tgt_dev has Unit Attention sense */
348 #define SCST_TGT_DEV_UA_PENDING 0
350 /* Set if tgt_dev is RESERVED by another session */
351 #define SCST_TGT_DEV_RESERVED 1
354 #define SCST_TGT_DEV_UNDER_TM_DBG 10
357 /*************************************************************
358 ** Commands that are not listed anywhere else
359 *************************************************************/
360 #ifndef REPORT_DEVICE_IDENTIFIER
361 #define REPORT_DEVICE_IDENTIFIER 0xA3
363 #ifndef INIT_ELEMENT_STATUS
364 #define INIT_ELEMENT_STATUS 0x07
366 #ifndef INIT_ELEMENT_STATUS_RANGE
367 #define INIT_ELEMENT_STATUS_RANGE 0x37
369 #ifndef PREVENT_ALLOW_MEDIUM
370 #define PREVENT_ALLOW_MEDIUM 0x1E
372 #ifndef READ_ATTRIBUTE
373 #define READ_ATTRIBUTE 0x8C
375 #ifndef REQUEST_VOLUME_ADDRESS
376 #define REQUEST_VOLUME_ADDRESS 0xB5
378 #ifndef WRITE_ATTRIBUTE
379 #define WRITE_ATTRIBUTE 0x8D
381 #ifndef WRITE_VERIFY_16
382 #define WRITE_VERIFY_16 0x8E
385 #define VERIFY_6 0x13
388 #define VERIFY_12 0xAF
391 /*************************************************************
392 ** Control byte field in CDB
393 *************************************************************/
394 #ifndef CONTROL_BYTE_LINK_BIT
395 #define CONTROL_BYTE_LINK_BIT 0x01
397 #ifndef CONTROL_BYTE_NACA_BIT
398 #define CONTROL_BYTE_NACA_BIT 0x04
401 /*************************************************************
402 ** Byte 1 in INQUIRY CDB
403 *************************************************************/
404 #define SCST_INQ_EVPD 0x01
406 /*************************************************************
407 ** Byte 3 in Standard INQUIRY data
408 *************************************************************/
409 #define SCST_INQ_BYTE3 3
411 #define SCST_INQ_NORMACA_BIT 0x20
413 /*************************************************************
414 ** Byte 2 in RESERVE_10 CDB
415 *************************************************************/
416 #define SCST_RES_3RDPTY 0x10
417 #define SCST_RES_LONGID 0x02
419 /*************************************************************
420 ** Misc SCSI constants
421 *************************************************************/
422 #define SCST_SENSE_ASC_UA_RESET 0x29
424 /*************************************************************
425 ** Name of the entry in /proc
426 *************************************************************/
427 #define SCST_PROC_ENTRY_NAME "scsi_tgt"
429 /*************************************************************
430 ** Used with scst_set_cmd_error()
431 *************************************************************/
432 #define SCST_LOAD_SENSE(key_asc_ascq) key_asc_ascq
434 #define SCST_SENSE_VALID(sense) ((((uint8_t *)(sense))[0] & 0x70) == 0x70)
436 #define SCST_NO_SENSE(sense) (((uint8_t *)(sense))[2] == 0)
438 /*************************************************************
440 *************************************************************/
445 struct scst_mgmt_cmd;
448 struct scst_dev_type;
449 struct scst_info_cdb;
454 typedef uint32_t lun_t;
456 typedef enum dma_data_direction scst_data_direction;
459 * Scsi_Target_Template: defines what functions a target driver will
460 * have to provide in order to work with the target mid-level.
461 * MUST HAVEs define functions that are expected to be in order to work.
462 * OPTIONAL says that there is a choice.
463 * Also, pay attention to the fact that a command is BLOCKING or NON-BLOCKING.
464 * NON-BLOCKING means that a function returns immediately and will not wait
465 * for actual data transfer to finish. Blocking in such command could have
466 * negative impact on overall system performance. If blocking is necessary,
467 * it is worth to consider creating dedicated thread(s) in target driver, to
468 * which the commands would be passed and which would perform blocking
469 * operations instead of SCST.
470 * If the function allowed to sleep or not is determined by its last
471 * argument, which is true, if sleeping is not allowed. In this case,
472 * if the function requires sleeping, it can return
473 * SCST_TGT_RES_NEED_THREAD_CTX, and it will be recalled in the thread context,
474 * where sleeping is allowed.
476 struct scst_tgt_template
481 * Name of the template. Must be unique to identify
482 * the template. MUST HAVE
487 * SG tablesize allows to check whether scatter/gather can be used
493 * True, if this target adapter uses unchecked DMA onto an ISA bus.
495 unsigned unchecked_isa_dma:1;
498 * True, if this target adapter wants to use clustering
499 * (i.e. smaller number of segments).
501 unsigned use_clustering:1;
504 * True, if corresponding function supports execution in
505 * the atomic (non-sleeping) context
507 unsigned xmit_response_atomic:1;
508 unsigned rdy_to_xfer_atomic:1;
510 /* True, if the template doesn't need the entry in /proc */
511 unsigned no_proc_entry:1;
514 * True, if the target requires that *ALL* affecting by a task
515 * management command outstanding SCSI commands finished before
516 * sending the TM command reply. Otherwise, the TM reply will be
517 * send immediately after it is insured, that the affecting SCSI
518 * commands will reach xmit_response() with ABORTED flag set (see
519 * also scst_cmd_aborted()).
521 unsigned tm_sync_reply:1;
524 * This function should detect the target adapters that
525 * are present in the system. The function should return a value
526 * >= 0 to signify the number of detected target adapters.
527 * A negative value should be returned whenever there is
532 int (*detect) (struct scst_tgt_template *tgt_template);
535 * This function should free up the resources allocated to the device.
536 * The function should return 0 to indicate successful release
537 * or a negative value if there are some issues with the release.
538 * In the current version the return value is ignored.
542 int (*release) (struct scst_tgt *tgt);
545 * This function is equivalent to the SCSI
546 * queuecommand. The target should transmit the response
547 * buffer and the status in the scst_cmd struct.
548 * The expectation is that this executing this command is NON-BLOCKING.
550 * After the response is actually transmitted, the target
551 * should call the scst_tgt_cmd_done() function of the
552 * mid-level, which will allow it to free up the command.
553 * Returns one of the SCST_TGT_RES_* constants.
555 * Pay attention to "atomic" attribute of the cmd, which can be get
556 * by scst_cmd_atomic(): it is true if the function called in the
557 * atomic (non-sleeping) context.
561 int (*xmit_response) (struct scst_cmd *cmd);
564 * This function allows the target driver to handle data buffer
565 * allocations on its own.
566 * Shall return 0 in case of success, and != 0 (preferrably -ENOMEM)
567 * in case of error. In case of returning successfully,
568 * scst_cmd->data_buf_alloced will be set by SCST.
570 * If allocation in atomic context - cf. scst_cmd_atomic() - is not
571 * desired or fails and consequently != 0 is returned, this function
572 * will be re-called in thread context.
574 * Please note that the driver will have to handle all relevant details
575 * such as scatterlist setup, highmem, freeing the allocated memory, ...
580 int (*alloc_data_buf) (struct scst_cmd *cmd);
583 * This function informs the driver that data
584 * buffer corresponding to the said command have now been
585 * allocated and it is OK to receive data for this command.
586 * This function is necessary because a SCSI target does not
587 * have any control over the commands it receives. Most lower
588 * level protocols have a corresponding function which informs
589 * the initiator that buffers have been allocated e.g., XFER_
590 * RDY in Fibre Channel. After the data is actually received
591 * the low-level driver needs to call scst_rx_data() in order to
592 * continue processing this command.
593 * Returns one of the SCST_TGT_RES_* constants.
594 * This command is expected to be NON-BLOCKING.
596 * Pay attention to "atomic" attribute of the cmd, which can be get
597 * by scst_cmd_atomic(): it is true if the function called in the
598 * atomic (non-sleeping) context.
602 int (*rdy_to_xfer) (struct scst_cmd *cmd);
605 * Called to notify the driver that the command is about to be freed.
606 * Necessary, because for aborted commands xmit_response() could not
607 * be called. Could be called on IRQ context.
611 void (*on_free_cmd) (struct scst_cmd *cmd);
614 * This function informs the driver that a
615 * received task management function has been completed. This
616 * function is necessary because low-level protocols have some
617 * means of informing the initiator about the completion of a
618 * Task Management function. This function being called will
619 * signify that a Task Management function is completed as far
620 * as the mid-level is concerned. Any information that must be
621 * stored about the command is the responsibility of the low-
622 * level driver. No return value expected.
623 * This function is expected to be NON-BLOCKING
625 * Pay attention to "atomic" attribute of the cmd, which can be get
626 * by scst_cmd_atomic(): it is true if the function called in the
627 * atomic (non-sleeping) context.
629 * MUST HAVE if the target supports ABORTs
631 void (*task_mgmt_fn_done) (struct scst_mgmt_cmd *mgmt_cmd);
634 * This function is used for Asynchronous Event Notification.
635 * It is the responsibility of the driver to notify any/all
636 * initiators about the Asynchronous Event reported.
637 * Returns one of the SCST_TGT_RES_* constants.
638 * This command is expected to be NON-BLOCKING, but can sleep.
640 * MUST HAVE if low-level protocol supports AEN
644 int (*report_aen) (int mgmt_fn, const uint8_t *lun, int lun_len);
647 * This function can be used to export the driver's statistics and
648 * other infos to the world outside the kernel.
652 int (*proc_info) (char *buffer, char **start, off_t offset,
653 int length, int *eof, struct scst_tgt *tgt, int inout);
655 /* Private, must be inited to 0 by memset() */
657 /* List of targets per template, protected by scst_mutex */
658 struct list_head tgt_list;
660 /* List entry of global templates list */
661 struct list_head scst_template_list_entry;
663 /* The pointer to the /proc directory entry */
664 struct proc_dir_entry *proc_tgt_root;
666 /* Dedicated thread number */
669 /* Device number in /proc */
675 /* Name of the dev handler. Must be unique. MUST HAVE */
678 /* SCSI type of the supported device. MUST HAVE */
682 * True, if corresponding function supports execution in
683 * the atomic (non-sleeping) context
685 unsigned parse_atomic:1;
686 unsigned exec_atomic:1;
687 unsigned dev_done_atomic:1;
690 * Called when new device is attaching to the dev handler
691 * Returns 0 on success, error code otherwise.
693 int (*attach) (struct scst_device *dev);
695 /* Called when new device is detaching from the dev handler */
696 void (*detach) (struct scst_device *dev);
699 * Called when new tgt_dev (session) is attaching to the dev handler.
700 * Returns 0 on success, error code otherwise.
702 int (*attach_tgt) (struct scst_tgt_dev *tgt_dev);
704 /* Called when tgt_dev (session) is detaching from the dev handler */
705 void (*detach_tgt) (struct scst_tgt_dev *tgt_dev);
708 * Called to parse CDB from the cmd and initialize
709 * cmd->bufflen and cmd->data_direction (both - REQUIRED).
710 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
711 * if the next default state should be used, or
712 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
713 * context, but requires sleeping. In the last case, the function
714 * will be recalled in the thread context, where sleeping is allowed.
716 * Pay attention to "atomic" attribute of the cmd, which can be get
717 * by scst_cmd_atomic(): it is true if the function called in the
718 * atomic (non-sleeping) context.
722 int (*parse) (struct scst_cmd *cmd,
723 const struct scst_info_cdb *cdb_info);
726 * Called to execute CDB. Useful, for instance, to implement
727 * data caching. The result of CDB execution is reported via
728 * cmd->scst_cmd_done() callback.
730 * - SCST_EXEC_COMPLETED - the cmd is done, go to other ones
731 * - SCST_EXEC_NEED_THREAD - thread context is required to execute
732 * the command. Exec() will be called again in the thread context.
733 * - SCST_EXEC_NOT_COMPLETED - the cmd should be sent to SCSI mid-level.
735 * Pay attention to "atomic" attribute of the cmd, which can be get
736 * by scst_cmd_atomic(): it is true if the function called in the
737 * atomic (non-sleeping) context.
739 * OPTIONAL, if not set, the commands will be sent directly to SCSI
742 int (*exec) (struct scst_cmd *cmd);
745 * Called to notify dev handler about the result of cmd execution
746 * and perform some post processing. Cmd's fields tgt_resp_flags and
747 * resp_data_len should be set by this function, but SCST offers good
749 * Returns the command's next state or SCST_CMD_STATE_DEFAULT,
750 * if the next default state should be used, or
751 * SCST_CMD_STATE_NEED_THREAD_CTX if the function called in atomic
752 * context, but requires sleeping. In the last case, the function
753 * will be recalled in the thread context, where sleeping is allowed.
755 * Pay attention to "atomic" attribute of the cmd, which can be get
756 * by scst_cmd_atomic(): it is true if the function called in the
757 * atomic (non-sleeping) context.
759 int (*dev_done) (struct scst_cmd *cmd);
762 * Called to notify dev hander that the command is about to be freed.
763 * Could be called on IRQ context.
765 void (*on_free_cmd) (struct scst_cmd *cmd);
768 * Called to execute a task management command.
770 * - SCST_DEV_TM_COMPLETED_SUCCESS - the command is done with success,
771 * no firther actions required
772 * - SCST_DEV_TM_COMPLETED_FAILED - the command is failed,
773 * no firther actions required
774 * - SCST_DEV_TM_NOT_COMPLETED - regular standard actions for the command
777 * Called with BH off. Might be called under a lock and IRQ off.
779 int (*task_mgmt_fn) (struct scst_mgmt_cmd *mgmt_cmd,
780 struct scst_tgt_dev *tgt_dev);
783 * This function can be used to export the handler's statistics and
784 * other infos to the world outside the kernel.
788 int (*proc_info) (char *buffer, char **start, off_t offset,
789 int length, int *eof, struct scst_dev_type *dev_type,
792 struct module *module;
796 /* list entry in scst_dev_type_list */
797 struct list_head dev_type_list_entry;
799 /* The pointer to the /proc directory entry */
800 struct proc_dir_entry *proc_dev_type_root;
805 /* List of remote sessions per target, protected by scst_mutex */
806 struct list_head sess_list;
808 /* List entry of targets per template (tgts_list) */
809 struct list_head tgt_list_entry;
811 struct scst_tgt_template *tgtt; /* corresponding target template */
813 /* Used to wait until session finished to unregister */
814 wait_queue_head_t unreg_waitQ;
816 /* Device number in /proc */
820 * The following fields used to store and retry cmds if
821 * target's internal queue is full, so the target is unable to accept
822 * the cmd returning QUEUE FULL
824 atomic_t finished_cmds;
825 int retry_cmds; /* protected by tgt_lock */
827 struct list_head retry_cmd_list; /* protected by tgt_lock */
828 struct timer_list retry_timer;
829 int retry_timer_active;
831 /* Maximum SG table size */
834 /* Used for storage of target driver private stuff */
840 /* Initialization phase, one of SCST_SESS_IPH_* constants */
843 atomic_t refcnt; /* get/put counter */
845 /* Alive commands for this session. Serialized by scst_list_lock */
848 /*************************************************************
849 ** Session's flags. Serialized by scst_list_lock
850 *************************************************************/
852 /* Set if the session is shutting down */
853 unsigned int shutting_down:1;
855 /* Set if the session is waiting in suspended state */
856 unsigned int waiting:1;
858 /**************************************************************/
861 * List of tgt_dev's for this session, protected by scst_mutex
862 * and suspended activity
864 struct list_head sess_tgt_dev_list;
866 /* Access control for this session and list entry there */
867 struct scst_acg *acg;
869 /* List entry for the sessions list inside ACG */
870 struct list_head acg_sess_list_entry;
872 /* Used for storage of target driver private stuff */
876 * List of cmds in this session. Used to find a cmd in the
877 * session. Protected by scst_list_lock.
879 * ToDo: make it protected by own lock.
881 struct list_head search_cmd_list;
883 struct scst_tgt *tgt; /* corresponding target */
886 * List entry for the list that keeps all sessions, which were
887 * stopped due to device block
889 struct list_head dev_wait_sess_list_entry;
891 /* Name of attached initiator */
892 const char *initiator_name;
894 /* Used if scst_unregister_session() called in wait mode */
895 struct semaphore *shutdown_mutex;
897 /* List entry of sessions per target */
898 struct list_head sess_list_entry;
900 /* List entry for the list that keeps session, waiting for the init */
901 struct list_head sess_mgmt_list_entry;
904 * Lists of deffered during session initialization commands.
905 * Protected by scst_list_lock.
907 struct list_head init_deferred_cmd_list;
908 struct list_head init_deferred_mcmd_list;
911 * Functions and data for user callbacks from scst_register_session()
912 * and scst_unregister_session()
915 void (*init_result_fn) (struct scst_session *sess, void *data,
917 void (*unreg_done_fn) (struct scst_session *sess);
920 enum scst_cmd_queue_type
922 SCST_CMD_QUEUE_UNTAGGED = 0,
923 SCST_CMD_QUEUE_SIMPLE,
924 SCST_CMD_QUEUE_ORDERED,
925 SCST_CMD_QUEUE_HEAD_OF_QUEUE,
931 /* List entry for global *_cmd_list */
932 struct list_head cmd_list_entry;
934 struct scst_session *sess; /* corresponding session */
936 /* Cmd state, one of SCST_CMD_STATE_* constants */
939 /*************************************************************
941 *************************************************************/
943 * Set if expected_sn was incremented, i.e. cmd was sent to
944 * SCSI mid-level for execution
946 unsigned int sent_to_midlev:1;
948 /* Set if the cmd's action is completed */
949 unsigned int completed:1;
951 /* Set if we should ignore Unit Attention in scst_check_sense() */
952 unsigned int ua_ignore:1;
954 /* Set if cmd is being processed in atomic context */
955 unsigned int atomic:1;
957 /* Set if cmd must be processed only in non-atomic context */
958 unsigned int non_atomic_only:1;
960 /* Set if cmd is internally generated */
961 unsigned int internal:1;
963 /* Set if cmd is being retried */
964 unsigned int retry:1;
966 /* Set if the device should be unblock after cmd's finish */
967 unsigned int blocking:1;
970 * Set if the target driver wants to alloc data buffers on its own.
971 * In this case alloc_data_buf() must be provided in the target driver
974 unsigned int data_buf_tgt_alloc:1;
977 * Set by SCST if the custom data buffer allocation by the target driver
980 unsigned int data_buf_alloced:1;
982 /* Set if the target driver called scst_set_expected() */
983 unsigned int expected_values_set:1;
986 * Set if the cmd is being processed from the thread/tasklet,
987 * i.e. if another cmd is moved to the active list during processing
988 * of the current one, then another cmd will be processed after
989 * the current. Helps to save some context switches.
991 unsigned int processible_env:1;
994 * Set if the cmd was delayed by task management debugging code.
995 * Used only if DEBUG_TM is on.
997 unsigned int tm_dbg_delayed:1;
1000 * Set if the cmd must be ignored by task management debugging code.
1001 * Used only if DEBUG_TM is on.
1003 unsigned int tm_dbg_immut:1;
1006 * Set if the SG buffer was modified by scst_set_resp_data_len()
1008 unsigned int sg_buff_modified:1;
1011 * Set if the cmd's memory requirements are checked and found
1014 unsigned int mem_checked:1;
1016 /**************************************************************/
1018 unsigned long cmd_flags; /* cmd's async flags */
1020 struct scst_tgt_template *tgtt; /* to save extra dereferences */
1021 struct scst_tgt *tgt; /* to save extra dereferences */
1022 struct scst_device *dev; /* to save extra dereferences */
1024 lun_t lun; /* LUN for this cmd */
1026 struct scst_tgt_dev *tgt_dev; /* corresponding device for this cmd */
1028 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
1029 struct scsi_request *scsi_req; /* SCSI request */
1032 /* List entry for tgt_dev's SN related lists */
1033 struct list_head sn_cmd_list_entry;
1035 /* Cmd's serial number, used to execute cmd's in order of arrival */
1038 /* List entry for session's search_cmd_list */
1039 struct list_head search_cmd_list_entry;
1042 * Used to found the cmd by scst_find_cmd_by_tag(). Set by the
1043 * target driver on the cmd's initialization time
1047 /* CDB and its len */
1048 uint8_t cdb[MAX_COMMAND_SIZE];
1051 enum scst_cmd_queue_type queue_type;
1053 int timeout; /* CDB execution timeout */
1054 int retries; /* Amount of retries that will be done by SCSI mid-level */
1056 /* SCSI data direction, one of SCST_DATA_* constants */
1057 scst_data_direction data_direction;
1059 /* Remote initiator supplied values, if any */
1060 scst_data_direction expected_data_direction;
1061 unsigned int expected_transfer_len;
1063 /* cmd data length */
1066 /* Completition routine */
1067 void (*scst_cmd_done) (struct scst_cmd *cmd, int next_state);
1069 struct sgv_pool_obj *sgv; /* sgv object */
1071 size_t bufflen; /* cmd buffer length */
1072 struct scatterlist *sg; /* cmd data buffer SG vector */
1073 int sg_cnt; /* SG segments count */
1075 /* scst_get_sg_buf_[first,next]() support */
1076 int get_sg_buf_entry_num;
1079 * The following two fields should be corrected by the dev_done(),
1082 int tgt_resp_flags; /* response flags (SCST_TSC_FLAG_* constants) */
1085 * Response data length in data buffer. This field must not be set
1086 * directly, use scst_set_resp_data_len() for that
1090 uint8_t status; /* status byte from target device */
1091 uint8_t masked_status; /* set from host device by status_byte() */
1092 uint8_t msg_status; /* return status from host adapter itself */
1093 uint16_t host_status; /* set by low-level driver to indicate status */
1094 uint16_t driver_status; /* set by mid-level */
1097 * Used to restore the SG vector if it was modified by
1098 * scst_set_resp_data_len()
1100 int orig_sg_cnt, orig_sg_entry, orig_entry_len;
1102 uint8_t sense_buffer[SCSI_SENSE_BUFFERSIZE]; /* sense buffer */
1104 /* The corresponding mgmt cmd, if any. Protected by scst_list_lock */
1105 struct scst_mgmt_cmd *mgmt_cmnd;
1107 /* List entry for dev's blocked_cmd_list */
1108 struct list_head blocked_cmd_list_entry;
1111 * Fileio private fields
1113 struct list_head fileio_cmd_list_entry;
1116 /* Used for storage of target driver private stuff */
1119 /* Used for storage of dev handler private stuff */
1123 * Used to store previous tgt_dev if dev handler returns
1124 * SCST_CMD_STATE_REINIT state
1126 struct scst_tgt_dev *tgt_dev_saved;
1128 struct scst_cmd *orig_cmd; /* Used to issue REQUEST SENSE */
1131 struct scst_mgmt_cmd
1133 /* List entry for *_mgmt_cmd_list */
1134 struct list_head mgmt_cmd_list_entry;
1136 struct scst_session *sess;
1138 /* Mgmt cmd state, one of SCST_MGMT_CMD_STATE_* constants */
1143 unsigned int completed:1; /* set, if the mcmd is completed */
1145 /* Number of commands to complete before sending response */
1148 /* Number of completed commands */
1149 int completed_cmd_count;
1151 lun_t lun; /* LUN for this mgmt cmd */
1153 uint32_t tag; /* tag of the corresponding cmd */
1155 /* corresponding cmd (to be aborted, found by tag) */
1156 struct scst_cmd *cmd_to_abort;
1158 /* corresponding device for this mgmt cmd (found by lun) */
1159 struct scst_tgt_dev *mcmd_tgt_dev;
1161 /* completition status, one of the SCST_MGMT_STATUS_* constants */
1164 /* Used for storage of target driver private stuff */
1170 struct scst_dev_type *handler; /* corresponding dev handler */
1172 /* Used to translate SCSI's cmd to SCST's cmd */
1173 struct gendisk *rq_disk;
1175 /*************************************************************
1176 ** Dev's flags. Updates serialized by dev_lock or suspended
1178 *************************************************************/
1180 /* Set if dev is RESERVED */
1181 unsigned int dev_reserved:1;
1183 /* Set if dev accepts only one command at time */
1184 unsigned int dev_serialized:1;
1186 /* Set if double reset UA is possible */
1187 unsigned int dev_double_ua_possible:1;
1189 /* Set if reset UA sent (to avoid double reset UAs) */
1190 unsigned int dev_reset_ua_sent:1;
1192 /**************************************************************/
1194 spinlock_t dev_lock; /* device lock */
1197 * How many times device was blocked for new cmds execution.
1198 * Protected by dev_lock
1203 * How many there are "on_dev" commands, i.e. ones those are being
1204 * executed by the underlying SCSI/virtual device.
1206 atomic_t on_dev_count;
1208 struct list_head blocked_cmd_list; /* protected by dev_lock */
1210 /* Corresponding real SCSI device, could be NULL for virtual devices */
1211 struct scsi_device *scsi_dev;
1213 /* Used for storage of dev handler private stuff */
1216 /* Used to wait for requested amount of "on_dev" commands */
1217 wait_queue_head_t on_dev_waitQ;
1219 /* A list entry used during RESETs, protected by scst_mutex */
1220 struct list_head reset_dev_list_entry;
1222 /* Virtual device internal ID */
1225 /* Pointer to virtual device name, for convenience only */
1226 const char *virt_name;
1228 /* List entry in global devices list */
1229 struct list_head dev_list_entry;
1232 * List of tgt_dev's, one per session, protected by scst_mutex and
1233 * suspended activity
1235 struct list_head dev_tgt_dev_list;
1237 /* List of acg_dev's, one per acg, protected by scst_mutex */
1238 struct list_head dev_acg_dev_list;
1242 * Used to store per-session specific device information
1246 /* List entry in sess->sess_tgt_dev_list */
1247 struct list_head sess_tgt_dev_list_entry;
1249 struct scst_acg_dev *acg_dev; /* corresponding acg_dev */
1252 * How many cmds alive on this dev in this session.
1253 * Protected by scst_list_lock.
1257 spinlock_t tgt_dev_lock; /* per-session device lock */
1259 /* List of UA's for this device, protected by tgt_dev_lock */
1260 struct list_head UA_list;
1262 unsigned long tgt_dev_flags; /* tgt_dev's flags */
1264 /* Used for storage of dev handler private stuff */
1268 * Used to execute cmd's in order of arrival.
1270 * Protected by sn_lock, except next_sn and expected_sn.
1271 * Expected_sn protected by itself, since only one thread, which
1272 * processes SN matching command, can increment it at any time.
1273 * Next_sn must be the same type as expected_sn to overflow
1276 int next_sn; /* protected by scst_list_lock */
1280 struct list_head deferred_cmd_list;
1281 struct list_head skipped_sn_list;
1283 struct scst_session *sess; /* corresponding session */
1285 /* list entry in dev->dev_tgt_dev_list */
1286 struct list_head dev_tgt_dev_list_entry;
1288 /* internal tmp list entry */
1289 struct list_head extra_tgt_dev_list_entry;
1293 * Used to store ACG-specific device information, like LUN
1297 struct scst_device *dev; /* corresponding device */
1298 lun_t lun; /* device's LUN in this acg */
1299 unsigned int rd_only_flag:1; /* if != 0, then read only */
1300 struct scst_acg *acg; /* parent acg */
1302 /* list entry in dev->dev_acg_dev_list */
1303 struct list_head dev_acg_dev_list_entry;
1305 /* list entry in acg->acg_dev_list */
1306 struct list_head acg_dev_list_entry;
1310 * ACG - access control group. Used to store group related
1311 * control information.
1315 /* List of acg_dev's in this acg, protected by scst_mutex */
1316 struct list_head acg_dev_list;
1318 /* List of attached sessions, protected by scst_mutex */
1319 struct list_head acg_sess_list;
1321 /* List of attached acn's, protected by scst_mutex */
1322 struct list_head acn_list;
1324 /* List entry in scst_acg_list */
1325 struct list_head scst_acg_list_entry;
1327 /* Name of this acg */
1328 const char *acg_name;
1330 /* The pointer to the /proc directory entry */
1331 struct proc_dir_entry *acg_proc_root;
1335 * ACN - access control name. Used to store names, by which
1336 * incoming sessions will be assigned to appropriate ACG.
1340 /* Initiator's name */
1342 /* List entry in acg->acn_list */
1343 struct list_head acn_list_entry;
1347 * Used to store per-session UNIT ATTENTIONs
1349 struct scst_tgt_dev_UA
1351 /* List entry in tgt_dev->UA_list */
1352 struct list_head UA_list_entry;
1353 /* Unit Attention sense */
1354 uint8_t UA_sense_buffer[SCSI_SENSE_BUFFERSIZE];
1359 SCST_TRANSFER_LEN_TYPE_FIXED = 0x01, /* must be equviv 1 (FIXED_BIT in cdb) */
1360 SCST_SMALL_TIMEOUT = 0x02,
1361 SCST_LONG_TIMEOUT = 0x04,
1362 SCST_UNKNOWN_LENGTH = 0x08,
1365 struct scst_info_cdb
1367 enum scst_cdb_flags flags;
1368 scst_data_direction direction;
1369 unsigned int transfer_len;
1370 unsigned short cdb_len;
1371 const char *op_name;
1375 * Sense data for the appropriate errors. Can be used with
1376 * scst_set_cmd_error()
1378 #define scst_sense_no_sense NO_SENSE, 0x00, 0
1379 #define scst_sense_hardw_error HARDWARE_ERROR, 0x44, 0
1380 #define scst_sense_aborted_command ABORTED_COMMAND, 0x00, 0
1381 #define scst_sense_invalid_opcode ILLEGAL_REQUEST, 0x20, 0
1382 #define scst_sense_invalid_field_in_cdb ILLEGAL_REQUEST, 0x24, 0
1383 #define scst_sense_invalid_field_in_parm_list ILLEGAL_REQUEST, 0x26, 0
1384 #define scst_sense_reset_UA UNIT_ATTENTION, 0x29, 0
1385 #define scst_sense_nexus_loss_UA UNIT_ATTENTION, 0x29, 0x7
1386 #define scst_sense_saving_params_unsup ILLEGAL_REQUEST, 0x39, 0
1387 #define scst_sense_lun_not_supported ILLEGAL_REQUEST, 0x25, 0
1388 #define scst_sense_data_protect DATA_PROTECT, 0x00, 0
1389 #define scst_sense_miscompare_error MISCOMPARE, 0x1D, 0
1390 #define scst_sense_block_out_range_error ILLEGAL_REQUEST, 0x21, 0
1391 #define scst_sense_medium_changed_UA UNIT_ATTENTION, 0x28, 0
1392 #define scst_sense_read_error MEDIUM_ERROR, 0x11, 0
1393 #define scst_sense_write_error MEDIUM_ERROR, 0x03, 0
1394 #define scst_sense_not_ready NOT_READY, 0x04, 0x10
1396 #ifndef smp_mb__after_set_bit
1397 /* There is no smp_mb__after_set_bit() in the kernel */
1398 #define smp_mb__after_set_bit() smp_mb();
1402 * Registers target template
1403 * Returns 0 on success or appropriate error code otherwise
1405 int scst_register_target_template(struct scst_tgt_template *vtt);
1408 * Unregisters target template
1410 void scst_unregister_target_template(struct scst_tgt_template *vtt);
1413 * Registers and returns target adapter
1414 * Returns new target structure on success or NULL otherwise
1416 struct scst_tgt *scst_register(struct scst_tgt_template *vtt);
1419 * Unregisters target adapter
1421 void scst_unregister(struct scst_tgt *tgt);
1424 * Registers and returns a session
1426 * Returns new session on success or NULL otherwise
1430 * atomic - true, if the function called in the atomic context
1431 * initiator_name - remote initiator's name, any NULL-terminated string,
1432 * e.g. iSCSI name, which used as the key to found appropriate access
1433 * control group. Could be NULL, then "default" group is used.
1434 * The groups are set up via /proc interface.
1435 * data - any target driver supplied data
1436 * result_fn - pointer to the function that will be
1437 * asynchronously called when session initialization finishes.
1438 * Can be NULL. Parameters:
1440 * - data - target driver supplied to scst_register_session() data
1441 * - result - session initialization result, 0 on success or
1442 * appropriate error code otherwise
1444 * Note: A session creation and initialization is a complex task,
1445 * which requires sleeping state, so it can't be fully done
1446 * in interrupt context. Therefore the "bottom half" of it, if
1447 * scst_register_session() is called from atomic context, will be
1448 * done in SCST thread context. In this case scst_register_session()
1449 * will return not completely initialized session, but the target
1450 * driver can supply commands to this session via scst_rx_cmd().
1451 * Those commands processing will be delayed inside SCST until
1452 * the session initialization is finished, then their processing
1453 * will be restarted. The target driver will be notified about
1454 * finish of the session initialization by function result_fn().
1455 * On success the target driver could do nothing, but if the
1456 * initialization fails, the target driver must ensure that
1457 * no more new commands being sent or will be sent to SCST after
1458 * result_fn() returns. All already sent to SCST commands for
1459 * failed session will be returned in xmit_response() with BUSY status.
1460 * In case of failure the driver shall call scst_unregister_session()
1461 * inside result_fn(), it will NOT be called automatically.
1463 struct scst_session *scst_register_session(struct scst_tgt *tgt, int atomic,
1464 const char *initiator_name, void *data,
1465 void (*result_fn) (struct scst_session *sess, void *data, int result));
1468 * Unregisters a session.
1470 * sess - session to be unregistered
1471 * wait - if true, instructs to wait until all commands, which
1472 * currently is being executed and belonged to the session, finished.
1473 * Otherwise, target driver should be prepared to receive
1474 * xmit_response() for the session's command after
1475 * scst_unregister_session() returns.
1476 * unreg_done_fn - pointer to the function that will be
1477 * asynchronously called when the last session's command finishes and
1478 * the session is about to be completely freed. Can be NULL.
1482 * Note: All outstanding commands will be finished regularly. After
1483 * scst_unregister_session() returned no new commands must be sent to
1484 * SCST via scst_rx_cmd(). Also, the caller must ensure that no
1485 * scst_rx_cmd() or scst_rx_mgmt_fn_*() is called in paralell with
1486 * scst_unregister_session().
1487 * Can be called before result_fn() of scst_register_session() called,
1488 * i.e. during the session registration/initialization.
1490 void scst_unregister_session(struct scst_session *sess, int wait,
1491 void (*unreg_done_fn) (struct scst_session *sess));
1494 * Registers dev handler driver
1495 * Returns 0 on success or appropriate error code otherwise
1497 int scst_register_dev_driver(struct scst_dev_type *dev_type);
1500 * Unregisters dev handler driver
1502 void scst_unregister_dev_driver(struct scst_dev_type *dev_type);
1505 * Registers dev handler driver for virtual devices (eg FILEIO)
1506 * Returns 0 on success or appropriate error code otherwise
1508 int scst_register_virtual_dev_driver(struct scst_dev_type *dev_type);
1511 * Unregisters dev handler driver for virtual devices
1513 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type);
1516 * Creates and sends new command to SCST.
1517 * Must not been called in parallel with scst_unregister_session() for the
1518 * same sess. Returns the command on success or NULL otherwise
1520 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
1521 const uint8_t *lun, int lun_len,
1522 const uint8_t *cdb, int cdb_len, int atomic);
1525 * Notifies SCST that the driver finished its part of the command
1526 * initialization, and the command is ready for execution.
1527 * The second argument sets preferred command execition context.
1528 * See SCST_CONTEXT_* constants for details
1530 void scst_cmd_init_done(struct scst_cmd *cmd, int pref_context);
1533 * Notifies SCST that the driver received all the necessary data
1534 * and the command is ready for further processing.
1535 * The second argument sets data receiving completion status
1536 * (see SCST_RX_STATUS_* constants for details)
1537 * The third argument sets preferred command execition context
1538 * (see SCST_CONTEXT_* constants for details)
1540 void scst_rx_data(struct scst_cmd *cmd, int status, int pref_context);
1543 * Notifies SCST that the driver sent the response and the command
1546 void scst_tgt_cmd_done(struct scst_cmd *cmd);
1549 * Creates new management command using tag and sends it for execution.
1550 * Can be used for SCST_ABORT_TASK only.
1551 * Must not been called in parallel with scst_unregister_session() for the
1552 * same sess. Returns 0 for success, error code otherwise.
1554 int scst_rx_mgmt_fn_tag(struct scst_session *sess, int fn, uint32_t tag,
1555 int atomic, void *tgt_priv);
1558 * Creates new management command using LUN and sends it for execution.
1559 * Currently can be used for any fn, except SCST_ABORT_TASK.
1560 * Must not been called in parallel with scst_unregister_session() for the
1561 * same sess. Returns 0 for success, error code otherwise.
1563 int scst_rx_mgmt_fn_lun(struct scst_session *sess, int fn,
1564 const uint8_t *lun, int lun_len,
1565 int atomic, void *tgt_priv);
1568 * Provides various CDB info
1570 * cdb_p - pointer to CDB
1571 * dev_type - SCSI device type
1572 * info_p - the result structure (output)
1573 * Returns 0 on success, -1 otherwise
1575 int scst_get_cdb_info(const uint8_t *cdb_p, int dev_type,
1576 struct scst_info_cdb *info_p);
1579 * Set error SCSI status in the command and prepares it for returning it
1581 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status);
1584 * Set error in the command and fill the sense buffer
1586 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq);
1589 * Sets BUSY or TASK QUEUE FULL status
1591 void scst_set_busy(struct scst_cmd *cmd);
1594 * Finds a command based on the supplied tag comparing it with one
1595 * that previously set by scst_cmd_set_tag().
1596 * Returns the command on success or NULL otherwise
1598 struct scst_cmd *scst_find_cmd_by_tag(struct scst_session *sess, uint32_t tag);
1601 * Finds a command based on user supplied data and comparision
1602 * callback function, that should return true, if the command is found.
1603 * Returns the command on success or NULL otherwise
1605 struct scst_cmd *scst_find_cmd(struct scst_session *sess, void *data,
1606 int (*cmp_fn) (struct scst_cmd *cmd,
1610 * Translates SCST's data direction to DMA one
1612 static inline int scst_to_dma_dir(int scst_dir)
1618 * Translates SCST data direction to DMA one from the perspective
1619 * of the target device.
1621 static inline int scst_to_tgt_dma_dir(int scst_dir)
1623 if (scst_dir == SCST_DATA_WRITE)
1624 return DMA_FROM_DEVICE;
1625 else if (scst_dir == SCST_DATA_READ)
1626 return DMA_TO_DEVICE;
1631 * Registers a virtual device.
1633 * dev_type - the device's device handler
1634 * dev_name - the new device name, NULL-terminated string. Must be uniq
1635 * among all virtual devices in the system. The value isn't
1636 * copied, only the reference is stored, so the value must
1637 * remain valid during the device lifetime.
1638 * Returns assinged to the device ID on success, or negative value otherwise
1640 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
1641 const char *dev_name);
1644 * Unegisters a virtual device.
1646 * id - the device's ID, returned by the registration function
1648 void scst_unregister_virtual_device(int id);
1651 * Get/Set functions for tgt's target private data
1653 static inline void *scst_tgt_get_tgt_priv(struct scst_tgt *tgt)
1655 return tgt->tgt_priv;
1658 static inline void scst_tgt_set_tgt_priv(struct scst_tgt *tgt, void *val)
1660 tgt->tgt_priv = val;
1664 * Get/Set functions for session's target private data
1666 static inline void *scst_sess_get_tgt_priv(struct scst_session *sess)
1668 return sess->tgt_priv;
1671 static inline void scst_sess_set_tgt_priv(struct scst_session *sess,
1674 sess->tgt_priv = val;
1677 /* Returns TRUE if cmd is being executed in atomic context */
1678 static inline int scst_cmd_atomic(struct scst_cmd *cmd)
1680 int res = cmd->atomic;
1682 if (unlikely(in_atomic() && !res)) {
1683 printk(KERN_ERR "ERROR: in_atomic() and non-atomic cmd\n");
1692 /* Returns cmd's session */
1693 static inline struct scst_session *scst_cmd_get_session(struct scst_cmd *cmd)
1698 /* Returns cmd's response data length */
1699 static inline int scst_cmd_get_resp_data_len(struct scst_cmd *cmd)
1701 return cmd->resp_data_len;
1704 /* Returns cmd's response flags (SCST_TSC_FLAG_* constants) */
1705 static inline int scst_cmd_get_tgt_resp_flags(struct scst_cmd *cmd)
1707 return cmd->tgt_resp_flags;
1711 * Returns pointer to cmd's SG data buffer.
1713 * Usage of this function is not recommended, use scst_get_buf_*()
1714 * family of functions instead.
1716 static inline struct scatterlist *scst_cmd_get_sg(struct scst_cmd *cmd)
1722 * Returns cmd's data buffer length.
1724 * In case if you need to iterate over data in the buffer, usage of
1725 * this function is not recommended, use scst_get_buf_*()
1726 * family of functions instead.
1728 static inline unsigned int scst_cmd_get_bufflen(struct scst_cmd *cmd)
1730 return cmd->bufflen;
1734 * Returns cmd's sg_cnt.
1736 * Usage of this function is not recommended, use scst_get_buf_*()
1737 * family of functions instead.
1739 static inline unsigned short scst_cmd_get_sg_cnt(struct scst_cmd *cmd)
1744 /* Returns cmd's data direction */
1745 static inline scst_data_direction scst_cmd_get_data_direction(
1746 struct scst_cmd *cmd)
1748 return cmd->data_direction;
1751 /* Returns cmd's relative data offset */
1752 static inline unsigned int scst_cmd_get_offset(struct scst_cmd *cmd)
1757 /* Returns cmd's status byte from host device */
1758 static inline uint8_t scst_cmd_get_status(struct scst_cmd *cmd)
1763 /* Returns cmd's status byte set from host device by status_byte() */
1764 static inline uint8_t scst_cmd_get_masked_status(struct scst_cmd *cmd)
1766 return cmd->masked_status;
1769 /* Returns cmd's status from host adapter itself */
1770 static inline uint8_t scst_cmd_get_msg_status(struct scst_cmd *cmd)
1772 return cmd->msg_status;
1775 /* Returns cmd's status set by low-level driver to indicate its status */
1776 static inline uint16_t scst_cmd_get_host_status(struct scst_cmd *cmd)
1778 return cmd->host_status;
1781 /* Returns cmd's status set by SCSI mid-level */
1782 static inline uint16_t scst_cmd_get_driver_status(struct scst_cmd *cmd)
1784 return cmd->driver_status;
1787 /* Returns pointer to cmd's sense buffer */
1788 static inline uint8_t *scst_cmd_get_sense_buffer(struct scst_cmd *cmd)
1790 return cmd->sense_buffer;
1793 /* Returns cmd's sense buffer length */
1794 static inline int scst_cmd_get_sense_buffer_len(struct scst_cmd *cmd)
1796 return sizeof(cmd->sense_buffer);
1800 * Get/Set functions for cmd's target SN
1802 static inline uint32_t scst_cmd_get_tag(struct scst_cmd *cmd)
1807 static inline void scst_cmd_set_tag(struct scst_cmd *cmd, uint32_t tag)
1813 * Get/Set functions for cmd's target private data.
1814 * Variant with *_lock must be used if target driver uses
1815 * scst_find_cmd() to avoid race with it, except inside scst_find_cmd()'s
1816 * callback, where lock is already taken.
1818 static inline void *scst_cmd_get_tgt_priv(struct scst_cmd *cmd)
1820 return cmd->tgt_priv;
1823 static inline void scst_cmd_set_tgt_priv(struct scst_cmd *cmd, void *val)
1825 cmd->tgt_priv = val;
1828 void *scst_cmd_get_tgt_priv_lock(struct scst_cmd *cmd);
1829 void scst_cmd_set_tgt_priv_lock(struct scst_cmd *cmd, void *val);
1832 * Get/Set functions for data_buf_tgt_alloc flag
1834 static inline int scst_cmd_get_data_buf_tgt_alloc(struct scst_cmd *cmd)
1836 return cmd->data_buf_tgt_alloc;
1839 static inline void scst_cmd_set_data_buf_tgt_alloc(struct scst_cmd *cmd)
1841 cmd->data_buf_tgt_alloc = 1;
1845 * Get/Set functions for data_buf_alloced flag
1847 static inline int scst_cmd_get_data_buff_alloced(struct scst_cmd *cmd)
1849 return cmd->data_buf_alloced;
1852 static inline void scst_cmd_set_data_buff_alloced(struct scst_cmd *cmd)
1854 cmd->data_buf_alloced = 1;
1858 * Returns 1 if the cmd was aborted, so its status is invalid and no
1859 * reply shall be sent to the remote initiator. A target driver should
1860 * only clear internal resources, associated with cmd.
1862 static inline int scst_cmd_aborted(struct scst_cmd *cmd)
1864 return test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags) &&
1865 !test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
1869 * Get/Set functions for expected data direction, transfer length
1870 * and its validity flag
1872 static inline int scst_cmd_is_expected_set(struct scst_cmd *cmd)
1874 return cmd->expected_values_set;
1877 static inline scst_data_direction scst_cmd_get_expected_data_direction(
1878 struct scst_cmd *cmd)
1880 return cmd->expected_data_direction;
1883 static inline unsigned int scst_cmd_get_expected_transfer_len(
1884 struct scst_cmd *cmd)
1886 return cmd->expected_transfer_len;
1889 static inline void scst_cmd_set_expected(struct scst_cmd *cmd,
1890 scst_data_direction expected_data_direction,
1891 unsigned int expected_transfer_len)
1893 cmd->expected_data_direction = expected_data_direction;
1894 cmd->expected_transfer_len = expected_transfer_len;
1895 cmd->expected_values_set = 1;
1899 * Get/Set function for mgmt cmd's target private data
1901 static inline void *scst_mgmt_cmd_get_tgt_priv(struct scst_mgmt_cmd *mcmd)
1903 return mcmd->tgt_priv;
1906 static inline void scst_mgmt_cmd_set_tgt_priv(struct scst_mgmt_cmd *mcmd,
1909 mcmd->tgt_priv = val;
1913 * Returns mgmt cmd's completition status (SCST_MGMT_STATUS_* constants)
1915 static inline int scst_mgmt_cmd_get_status(struct scst_mgmt_cmd *mcmd)
1917 return mcmd->status;
1921 * Functions for access to the commands data (SG) buffer,
1922 * including HIGHMEM environment. Should be used instead of direct
1923 * access. Returns the mapped buffer length for success, 0 for EOD,
1924 * negative error code otherwise.
1926 * "Buf" argument returns the mapped buffer
1928 * The "put" function unmaps the buffer.
1930 int __scst_get_buf(struct scst_cmd *cmd, uint8_t **buf);
1931 static inline int scst_get_buf_first(struct scst_cmd *cmd, uint8_t **buf)
1933 cmd->get_sg_buf_entry_num = 0;
1934 return __scst_get_buf(cmd, buf);
1937 static inline int scst_get_buf_next(struct scst_cmd *cmd, uint8_t **buf)
1939 return __scst_get_buf(cmd, buf);
1942 static inline void scst_put_buf(struct scst_cmd *cmd, void *buf)
1946 if (scst_cmd_atomic(cmd)) {
1953 kunmap_atomic(buf, km);
1961 * Returns approximate higher rounded buffers count that
1962 * scst_get_buf_[first|next]() return.
1964 static inline int scst_get_buf_count(struct scst_cmd *cmd)
1968 res = (cmd->bufflen >> PAGE_SHIFT) + 1;
1970 res = (cmd->sg_cnt == 0) ? 1 : cmd->sg_cnt;
1976 * Suspends and resumes any activity.
1977 * scst_suspend_activity() doesn't return until there are any
1978 * active commands (state after SCST_CMD_STATE_INIT). New arriving
1979 * commands stay in that state until scst_resume_activity() is called.
1981 void scst_suspend_activity(void);
1982 void scst_resume_activity(void);
1985 * Returns target driver's root entry in SCST's /proc hierarchy.
1986 * The driver can create own files/directoryes here, which should
1987 * be deleted in the driver's release().
1989 static inline struct proc_dir_entry *scst_proc_get_tgt_root(
1990 struct scst_tgt_template *vtt)
1992 return vtt->proc_tgt_root;
1996 * Returns device handler's root entry in SCST's /proc hierarchy.
1997 * The driver can create own files/directoryes here, which should
1998 * be deleted in the driver's detach()/release().
2000 static inline struct proc_dir_entry *scst_proc_get_dev_type_root(
2001 struct scst_dev_type *dtt)
2003 return dtt->proc_dev_type_root;
2007 ** Two library functions and the structure to help the drivers
2008 ** that use scst_debug.* facilities manage "trace_level" /proc entry.
2009 ** The functions service "standard" log levels and allow to work
2010 ** with driver specific levels, which should be passed inside as
2011 ** NULL-terminated array of struct scst_proc_log's, where:
2012 ** - val - the level's numeric value
2013 ** - token - its string representation
2016 struct scst_proc_log {
2021 int scst_proc_log_entry_read(char *buffer, char **start,
2022 off_t offset, int length, int *eof, void *data,
2023 unsigned long log_level, const struct scst_proc_log *tbl);
2025 int scst_proc_log_entry_write(struct file *file, const char *buf,
2026 unsigned long length, void *data, unsigned long *log_level,
2027 unsigned long default_level, const struct scst_proc_log *tbl);
2030 * Adds and deletes (stops) num SCST's threads. Returns 0 on success,
2031 * error code otherwise.
2033 int scst_add_threads(int num);
2034 void scst_del_threads(int num);
2036 void scst_set_cmd_error_sense(struct scst_cmd *cmd, uint8_t *sense,
2040 * Returnes a pseudo-random number for debugging purposes. Available only with
2043 unsigned long scst_random(void);
2046 * Sets response data length for cmd and truncates its SG vector accordingly.
2047 * The cmd->resp_data_len must not be set directly, it must be set only
2048 * using this function. Value of resp_data_len must be <= cmd->bufflen.
2050 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len);
2053 * Checks if total memory allocated by commands is less, than defined
2054 * limit (scst_cur_max_cmd_mem) and returns 0, if it is so. Otherwise,
2055 * returnes 1 and sets on cmd QUEUE FULL or BUSY status as well as
2056 * SCST_CMD_STATE_XMIT_RESP state. Target drivers and dev handlers are
2057 * required to call this function if they allocate data buffers on their
2060 int scst_check_mem(struct scst_cmd *cmd);
2063 * Get/put global ref counter that prevents from entering into suspended
2064 * activities stage, so protects from any global management operations.
2066 void scst_get(void);
2067 void scst_put(void);
2069 #endif /* __SCST_H */