2 * Copyright (C) 2002 - 2003 Ardis Technolgies <roman@ardistech.com>
3 * Copyright (C) 2007 - 2009 Vladislav Bolkhovitin
4 * Copyright (C) 2007 - 2009 ID7 Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, version 2
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
20 #include <linux/pagemap.h>
22 #include <linux/net.h>
27 #include "iscsi_hdr.h"
28 #include "iscsi_scst.h"
30 #include "iscsi_dbg.h"
32 #define iscsi_sense_crc_error ABORTED_COMMAND, 0x47, 0x5
33 #define iscsi_sense_unexpected_unsolicited_data ABORTED_COMMAND, 0xC, 0xC
35 struct iscsi_sess_param {
39 unsigned int max_recv_data_length;
40 unsigned int max_xmit_data_length;
41 unsigned int max_burst_length;
42 unsigned int first_burst_length;
43 int default_wait_time;
44 int default_retain_time;
45 unsigned int max_outstanding_r2t;
47 int data_sequence_inorder;
48 int error_recovery_level;
57 struct iscsi_trgt_param {
61 struct network_thread_info {
62 struct task_struct *task;
69 struct scst_tgt *scst_tgt;
71 struct mutex target_mutex;
73 struct list_head session_list; /* protected by target_mutex */
75 /* Both protected by target_mgmt_mutex */
76 struct iscsi_trgt_param trgt_param;
78 * Put here to have uniform parameters checking and assigning
79 * from various places, including iscsi-scst-adm.
81 struct iscsi_sess_param trgt_sess_param;
83 struct list_head target_list_entry;
85 char name[ISCSI_NAME_LEN];
88 #define ISCSI_HASH_ORDER 8
89 #define cmnd_hashfn(itt) hash_long((itt), ISCSI_HASH_ORDER)
91 struct iscsi_session {
92 struct iscsi_target *target;
93 struct scst_session *scst_sess;
95 struct list_head pending_list; /* protected by sn_lock */
97 /* Unprotected, since accessed only from a single read thread */
100 u32 max_queued_cmnds; /* unprotected, since read-only */
101 atomic_t active_cmds;
104 u32 exp_cmd_sn; /* protected by sn_lock */
106 /* All 3 protected by sn_lock */
109 struct iscsi_cmnd *tm_rsp;
111 /* Read only, if there are connection(s) */
112 struct iscsi_sess_param sess_param;
114 spinlock_t cmnd_hash_lock;
115 struct list_head cmnd_hash[1 << ISCSI_HASH_ORDER];
117 struct list_head conn_list; /* protected by target_mutex */
119 struct list_head session_list_entry;
121 /* All protected by target_mutex, where necessary */
122 struct iscsi_session *sess_reinst_successor;
123 unsigned int sess_reinstating:1;
124 unsigned int sess_shutting_down:1;
126 /* All don't need any protection */
127 char *initiator_name;
131 #define ISCSI_CONN_IOV_MAX (PAGE_SIZE/sizeof(struct iovec))
133 #define ISCSI_CONN_RD_STATE_IDLE 0
134 #define ISCSI_CONN_RD_STATE_IN_LIST 1
135 #define ISCSI_CONN_RD_STATE_PROCESSING 2
137 #define ISCSI_CONN_WR_STATE_IDLE 0
138 #define ISCSI_CONN_WR_STATE_IN_LIST 1
139 #define ISCSI_CONN_WR_STATE_SPACE_WAIT 2
140 #define ISCSI_CONN_WR_STATE_PROCESSING 3
143 struct iscsi_session *session; /* owning session */
145 /* Both protected by session->sn_lock */
149 #define ISCSI_CONN_REINSTATING 1
150 #define ISCSI_CONN_SHUTTINGDOWN 2
151 unsigned long conn_aflags;
153 spinlock_t cmd_list_lock; /* BH lock */
155 /* Protected by cmd_list_lock */
156 struct list_head cmd_list; /* in/outcoming pdus */
158 atomic_t conn_ref_cnt;
160 spinlock_t write_list_lock;
161 /* List of data pdus to be sent, protected by write_list_lock */
162 struct list_head write_list;
163 /* List of data pdus being sent, protected by write_list_lock */
164 struct list_head written_list;
166 struct timer_list rsp_timer;
168 /* All 2 protected by iscsi_wr_lock */
169 unsigned short wr_state;
170 unsigned short wr_space_ready:1;
172 struct list_head wr_list_entry;
174 #ifdef CONFIG_SCST_EXTRACHECKS
175 struct task_struct *wr_task;
179 * All are unprotected, since accessed only from a single write
182 struct iscsi_cmnd *write_cmnd;
183 struct iovec *write_iop;
185 struct iovec write_iov[2];
190 /* Both don't need any protection */
194 void (*old_state_change)(struct sock *);
195 void (*old_data_ready)(struct sock *, int);
196 void (*old_write_space)(struct sock *);
198 /* Both read only. Stay here for better CPU cache locality. */
202 /* All 5 protected by iscsi_rd_lock */
203 unsigned short rd_state;
204 unsigned short rd_data_ready:1;
205 /* Let's save some cache footprint by putting them here */
206 unsigned short closing:1;
207 unsigned short active_close:1;
208 unsigned short deleting:1;
210 struct list_head rd_list_entry;
212 #ifdef CONFIG_SCST_EXTRACHECKS
213 struct task_struct *rd_task;
217 * All are unprotected, since accessed only from a single read
220 struct iscsi_cmnd *read_cmnd;
221 struct msghdr read_msg;
224 struct iovec *read_iov;
225 struct task_struct *rx_task;
228 struct iscsi_target *target;
230 struct list_head conn_list_entry; /* list entry in session conn_list */
232 /* All protected by target_mutex, where necessary */
233 struct iscsi_conn *conn_reinst_successor;
234 struct list_head reinst_pending_cmd_list;
236 wait_queue_head_t read_state_waitQ;
237 struct completion ready_to_free;
239 /* Doesn't need any protection */
242 #ifndef CONFIG_SCST_PROC
243 /* Doesn't need any protection */
244 struct kobject iscsi_conn_kobj;
245 #endif /* CONFIG_SCST_PROC */
249 struct iscsi_hdr bhs;
251 unsigned int ahssize;
252 unsigned int datasize;
255 typedef void (iscsi_show_info_t)(struct seq_file *seq,
256 struct iscsi_target *target);
258 /** Command's states **/
260 /* New command and SCST processes it */
261 #define ISCSI_CMD_STATE_NEW 0
263 /* SCST processes cmd after scst_rx_cmd() */
264 #define ISCSI_CMD_STATE_RX_CMD 1
266 /* The command returned from preprocessing_done() */
267 #define ISCSI_CMD_STATE_AFTER_PREPROC 2
269 /* The command is waiting for session or connection reinstatement finished */
270 #define ISCSI_CMD_STATE_REINST_PENDING 3
272 /* scst_restart_cmd() called and SCST processing it */
273 #define ISCSI_CMD_STATE_RESTARTED 4
275 /* SCST done processing */
276 #define ISCSI_CMD_STATE_PROCESSED 5
279 #define ISCSI_CMD_STATE_AEN 6
281 /** Command's reject reasons **/
282 #define ISCSI_REJECT_SCSI_CMD 1
283 #define ISCSI_REJECT_CMD 2
284 #define ISCSI_REJECT_DATA 3
287 * Most of the fields don't need any protection, since accessed from only a
288 * single thread, except where noted.
291 struct iscsi_conn *conn;
294 * Some flags protected by conn->write_list_lock, but all modified only
295 * from single read thread or when there are no references to cmd.
297 unsigned int hashed:1;
298 unsigned int should_close_conn:1;
299 unsigned int should_close_all_conn:1;
300 unsigned int pending:1;
301 unsigned int own_sg:1;
302 unsigned int on_write_list:1;
303 unsigned int write_processing_started:1;
304 unsigned int data_waiting:1;
305 unsigned int force_cleanup_done:1;
306 unsigned int dec_active_cmnds:1;
307 unsigned int ddigest_checked:1;
308 unsigned int rejected:1;
309 unsigned int reject_reason:2;
310 #ifdef CONFIG_SCST_EXTRACHECKS
311 unsigned int on_rx_digest_list:1;
312 unsigned int release_called:1;
315 /* It's async. with the above flags */
316 volatile unsigned int tm_aborted;
318 struct list_head hash_list_entry;
320 spinlock_t rsp_cmd_lock; /* BH lock */
323 * Unions are for readability and grepability and to save some
328 /* Protected by rsp_cmd_lock */
329 struct list_head rsp_cmd_list;
330 struct list_head rsp_cmd_list_entry;
334 struct list_head pending_list_entry;
335 struct list_head reinst_pending_cmd_list_entry;
339 struct list_head write_list_entry;
340 struct list_head written_list_entry;
343 /* Both modified only from single write thread */
344 unsigned int on_written_list:1;
345 unsigned long write_timeout;
348 * All unprotected, since could be accessed from only a single
351 struct iscsi_cmnd *parent_req;
352 struct iscsi_cmnd *cmd_req;
355 * All unprotected, since could be accessed from only a single
359 /* Request only fields */
361 struct list_head rx_ddigest_cmd_list;
362 struct list_head rx_ddigest_cmd_list_entry;
366 struct scst_cmd *scst_cmd;
367 struct scst_aen *scst_aen;
372 /* Response only fields */
374 struct scatterlist rsp_sg[2];
375 struct iscsi_sense_data sense_hdr;
380 #if defined(CONFIG_TCP_ZERO_COPY_TRANSFER_COMPLETION_NOTIFICATION)
381 atomic_t net_ref_cnt;
384 struct iscsi_pdu pdu;
386 struct scatterlist *sg;
388 unsigned int bufflen;
391 u32 is_unsolicited_data;
398 struct list_head cmd_list_entry;
401 /* Flags for req_cmnd_release_force() */
402 #define ISCSI_FORCE_RELEASE_WRITE 1
404 #define ISCSI_RSP_TIMEOUT (30 * HZ)
406 extern struct mutex target_mgmt_mutex;
408 extern const struct file_operations ctr_fops;
410 extern spinlock_t iscsi_rd_lock;
411 extern struct list_head iscsi_rd_list;
412 extern wait_queue_head_t iscsi_rd_waitQ;
414 extern spinlock_t iscsi_wr_lock;
415 extern struct list_head iscsi_wr_list;
416 extern wait_queue_head_t iscsi_wr_waitQ;
419 extern struct iscsi_cmnd *cmnd_alloc(struct iscsi_conn *,
420 struct iscsi_cmnd *parent);
421 extern int cmnd_rx_start(struct iscsi_cmnd *);
422 extern int cmnd_rx_continue(struct iscsi_cmnd *req);
423 extern void cmnd_rx_end(struct iscsi_cmnd *);
424 extern void cmnd_tx_start(struct iscsi_cmnd *);
425 extern void cmnd_tx_end(struct iscsi_cmnd *);
426 extern void req_cmnd_release_force(struct iscsi_cmnd *req, int flags);
427 extern void rsp_cmnd_release(struct iscsi_cmnd *);
428 extern void cmnd_done(struct iscsi_cmnd *cmnd);
429 extern void conn_abort(struct iscsi_conn *conn);
430 extern void iscsi_restart_cmnd(struct iscsi_cmnd *cmnd);
433 extern struct iscsi_conn *conn_lookup(struct iscsi_session *, u16);
434 extern void conn_reinst_finished(struct iscsi_conn *);
435 extern int conn_add(struct iscsi_session *, struct iscsi_kern_conn_info *);
436 extern int conn_del(struct iscsi_session *, struct iscsi_kern_conn_info *);
437 #ifdef CONFIG_SCST_PROC
438 extern int conn_free(struct iscsi_conn *);
440 extern void iscsi_make_conn_rd_active(struct iscsi_conn *conn);
442 #define ISCSI_CONN_ACTIVE_CLOSE 1
443 #define ISCSI_CONN_DELETING 2
444 extern void __mark_conn_closed(struct iscsi_conn *, int);
446 extern void mark_conn_closed(struct iscsi_conn *);
447 extern void iscsi_make_conn_wr_active(struct iscsi_conn *);
449 #ifdef CONFIG_SCST_PROC
450 extern void conn_info_show(struct seq_file *, struct iscsi_session *);
454 extern int iscsi_send(struct iscsi_conn *conn);
455 #if defined(CONFIG_TCP_ZERO_COPY_TRANSFER_COMPLETION_NOTIFICATION)
456 extern void iscsi_get_page_callback(struct page *page);
457 extern void iscsi_put_page_callback(struct page *page);
459 extern int istrd(void *arg);
460 extern int istwr(void *arg);
461 extern void iscsi_task_mgmt_affected_cmds_done(struct scst_mgmt_cmd *scst_mcmd);
464 #ifndef CONFIG_SCST_PROC
465 extern const struct attribute *iscsi_tgt_attrs[];
467 struct iscsi_target *target_lookup_by_id(u32);
468 extern int target_add(struct iscsi_kern_target_info *);
469 extern int target_del(u32 id);
470 extern void target_del_session(struct iscsi_target *target,
471 struct iscsi_session *session, int flags);
472 extern void target_del_all_sess(struct iscsi_target *target, int flags);
473 extern void target_del_all(void);
475 extern const struct seq_operations iscsi_seq_op;
478 #ifdef CONFIG_SCST_PROC
479 extern int iscsi_procfs_init(void);
480 extern void iscsi_procfs_exit(void);
482 extern const struct attribute *iscsi_attrs[];
486 #ifdef CONFIG_SCST_PROC
487 int print_digest_state(char *p, size_t size, unsigned long flags);
489 extern const struct attribute *iscsi_sess_attrs[];
491 extern const struct file_operations session_seq_fops;
492 extern struct iscsi_session *session_lookup(struct iscsi_target *, u64);
493 extern void sess_reinst_finished(struct iscsi_session *);
494 extern int session_add(struct iscsi_target *, struct iscsi_kern_session_info *);
495 extern int session_del(struct iscsi_target *, u64);
496 extern int session_free(struct iscsi_session *session, bool del);
499 extern int iscsi_param_set(struct iscsi_target *,
500 struct iscsi_kern_param_info *, int);
503 extern int event_send(u32, u64, u32, u32, int);
504 extern int event_init(void);
505 extern void event_exit(void);
507 #define get_pgcnt(size, offset) \
508 ((((size) + ((offset) & ~PAGE_MASK)) + PAGE_SIZE - 1) >> PAGE_SHIFT)
510 static inline void iscsi_cmnd_get_length(struct iscsi_pdu *pdu)
512 #if defined(__BIG_ENDIAN)
513 pdu->ahssize = pdu->bhs.length.ahslength * 4;
514 pdu->datasize = pdu->bhs.length.datalength;
515 #elif defined(__LITTLE_ENDIAN)
516 pdu->ahssize = (pdu->bhs.length & 0xff) * 4;
517 pdu->datasize = be32_to_cpu(pdu->bhs.length & ~0xff);
523 static inline void iscsi_cmnd_set_length(struct iscsi_pdu *pdu)
525 #if defined(__BIG_ENDIAN)
526 pdu->bhs.length.ahslength = pdu->ahssize / 4;
527 pdu->bhs.length.datalength = pdu->datasize;
528 #elif defined(__LITTLE_ENDIAN)
529 pdu->bhs.length = cpu_to_be32(pdu->datasize) | (pdu->ahssize / 4);
535 extern struct scst_tgt_template iscsi_template;
538 * Skip this command if result is not 0. Must be called under
539 * corresponding lock.
541 static inline bool cmnd_get_check(struct iscsi_cmnd *cmnd)
543 int r = atomic_inc_return(&cmnd->ref_cnt);
545 if (unlikely(r == 1)) {
546 TRACE_DBG("cmnd %p is being destroyed", cmnd);
547 atomic_dec(&cmnd->ref_cnt);
549 /* Necessary code is serialized by locks in cmnd_done() */
551 TRACE_DBG("cmnd %p, new ref_cnt %d", cmnd,
552 atomic_read(&cmnd->ref_cnt));
558 static inline void cmnd_get(struct iscsi_cmnd *cmnd)
560 atomic_inc(&cmnd->ref_cnt);
561 TRACE_DBG("cmnd %p, new cmnd->ref_cnt %d", cmnd,
562 atomic_read(&cmnd->ref_cnt));
565 static inline void cmnd_get_ordered(struct iscsi_cmnd *cmnd)
568 /* See comments for each cmnd_get_ordered() use */
569 smp_mb__after_atomic_inc();
572 static inline void cmnd_put(struct iscsi_cmnd *cmnd)
574 TRACE_DBG("cmnd %p, new ref_cnt %d", cmnd,
575 atomic_read(&cmnd->ref_cnt)-1);
577 EXTRACHECKS_BUG_ON(atomic_read(&cmnd->ref_cnt) == 0);
579 if (atomic_dec_and_test(&cmnd->ref_cnt))
583 /* conn->write_list_lock supposed to be locked and BHs off */
584 static inline void cmd_add_on_write_list(struct iscsi_conn *conn,
585 struct iscsi_cmnd *cmnd)
587 TRACE_DBG("%p", cmnd);
588 list_add_tail(&cmnd->write_list_entry, &conn->write_list);
589 cmnd->on_write_list = 1;
592 /* conn->write_list_lock supposed to be locked and BHs off */
593 static inline void cmd_del_from_write_list(struct iscsi_cmnd *cmnd)
595 TRACE_DBG("%p", cmnd);
596 list_del(&cmnd->write_list_entry);
597 cmnd->on_write_list = 0;
600 static inline void cmd_add_on_rx_ddigest_list(struct iscsi_cmnd *req,
601 struct iscsi_cmnd *cmnd)
603 TRACE_DBG("Adding RX ddigest cmd %p to digest list "
604 "of req %p", cmnd, req);
605 list_add_tail(&cmnd->rx_ddigest_cmd_list_entry,
606 &req->rx_ddigest_cmd_list);
607 #ifdef CONFIG_SCST_EXTRACHECKS
608 cmnd->on_rx_digest_list = 1;
612 static inline void cmd_del_from_rx_ddigest_list(struct iscsi_cmnd *cmnd)
614 TRACE_DBG("Deleting RX digest cmd %p from digest list", cmnd);
615 list_del(&cmnd->rx_ddigest_cmd_list_entry);
616 #ifdef CONFIG_SCST_EXTRACHECKS
617 cmnd->on_rx_digest_list = 0;
621 static inline int test_write_ready(struct iscsi_conn *conn)
624 * No need for write_list protection, in the worst case we will be
627 return !list_empty(&conn->write_list) || conn->write_cmnd;
630 static inline void conn_get(struct iscsi_conn *conn)
632 atomic_inc(&conn->conn_ref_cnt);
633 TRACE_DBG("conn %p, new conn_ref_cnt %d", conn,
634 atomic_read(&conn->conn_ref_cnt));
637 static inline void conn_get_ordered(struct iscsi_conn *conn)
640 /* See comments for each conn_get_ordered() use */
641 smp_mb__after_atomic_inc();
644 static inline void conn_put(struct iscsi_conn *conn)
646 TRACE_DBG("conn %p, new conn_ref_cnt %d", conn,
647 atomic_read(&conn->conn_ref_cnt)-1);
648 sBUG_ON(atomic_read(&conn->conn_ref_cnt) == 0);
651 * Make it always ordered to protect from undesired side effects like
652 * accessing just destroyed by close_conn() conn caused by reordering
653 * of this atomic_dec().
655 smp_mb__before_atomic_dec();
656 atomic_dec(&conn->conn_ref_cnt);
659 #ifdef CONFIG_SCST_EXTRACHECKS
660 extern void iscsi_extracheck_is_rd_thread(struct iscsi_conn *conn);
661 extern void iscsi_extracheck_is_wr_thread(struct iscsi_conn *conn);
663 static inline void iscsi_extracheck_is_rd_thread(struct iscsi_conn *conn) {}
664 static inline void iscsi_extracheck_is_wr_thread(struct iscsi_conn *conn) {}
667 #endif /* __ISCSI_H__ */