Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / scsi / scsi_transport_iscsi.c
blob8e88e83152fa9f39426af010e2d29510b3f910a9
1 /*
2 * iSCSI transport class definitions
4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <net/tcp.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_transport.h>
30 #include <scsi/scsi_transport_iscsi.h>
31 #include <scsi/iscsi_if.h>
33 #define ISCSI_SESSION_ATTRS 19
34 #define ISCSI_CONN_ATTRS 13
35 #define ISCSI_HOST_ATTRS 4
36 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
37 #define ISCSI_TRANSPORT_VERSION "2.0-868"
38 =======
39 #define ISCSI_TRANSPORT_VERSION "2.0-869"
40 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
42 struct iscsi_internal {
43 int daemon_pid;
44 struct scsi_transport_template t;
45 struct iscsi_transport *iscsi_transport;
46 struct list_head list;
47 struct class_device cdev;
49 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
50 struct transport_container conn_cont;
51 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
52 struct transport_container session_cont;
53 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
56 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
57 static struct workqueue_struct *iscsi_eh_timer_workq;
60 * list of registered transports and lock that must
61 * be held while accessing list. The iscsi_transport_lock must
62 * be acquired after the rx_queue_mutex.
64 static LIST_HEAD(iscsi_transports);
65 static DEFINE_SPINLOCK(iscsi_transport_lock);
67 #define to_iscsi_internal(tmpl) \
68 container_of(tmpl, struct iscsi_internal, t)
70 #define cdev_to_iscsi_internal(_cdev) \
71 container_of(_cdev, struct iscsi_internal, cdev)
73 static void iscsi_transport_release(struct class_device *cdev)
75 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
76 kfree(priv);
80 * iscsi_transport_class represents the iscsi_transports that are
81 * registered.
83 static struct class iscsi_transport_class = {
84 .name = "iscsi_transport",
85 .release = iscsi_transport_release,
88 static ssize_t
89 show_transport_handle(struct class_device *cdev, char *buf)
91 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
92 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
94 static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
96 #define show_transport_attr(name, format) \
97 static ssize_t \
98 show_transport_##name(struct class_device *cdev, char *buf) \
99 { \
100 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
101 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
103 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
105 show_transport_attr(caps, "0x%x");
106 show_transport_attr(max_lun, "%d");
107 show_transport_attr(max_conn, "%d");
108 show_transport_attr(max_cmd_len, "%d");
110 static struct attribute *iscsi_transport_attrs[] = {
111 &class_device_attr_handle.attr,
112 &class_device_attr_caps.attr,
113 &class_device_attr_max_lun.attr,
114 &class_device_attr_max_conn.attr,
115 &class_device_attr_max_cmd_len.attr,
116 NULL,
119 static struct attribute_group iscsi_transport_group = {
120 .attrs = iscsi_transport_attrs,
125 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
126 struct class_device *cdev)
128 struct Scsi_Host *shost = dev_to_shost(dev);
129 struct iscsi_host *ihost = shost->shost_data;
131 memset(ihost, 0, sizeof(*ihost));
132 INIT_LIST_HEAD(&ihost->sessions);
133 mutex_init(&ihost->mutex);
134 atomic_set(&ihost->nr_scans, 0);
136 snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
137 shost->host_no);
138 ihost->scan_workq = create_singlethread_workqueue(
139 ihost->scan_workq_name);
140 if (!ihost->scan_workq)
141 return -ENOMEM;
142 return 0;
145 static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
146 struct class_device *cdev)
148 struct Scsi_Host *shost = dev_to_shost(dev);
149 struct iscsi_host *ihost = shost->shost_data;
151 destroy_workqueue(ihost->scan_workq);
152 return 0;
155 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
156 "iscsi_host",
157 iscsi_setup_host,
158 iscsi_remove_host,
159 NULL);
161 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
162 "iscsi_session",
163 NULL,
164 NULL,
165 NULL);
167 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
168 "iscsi_connection",
169 NULL,
170 NULL,
171 NULL);
173 static struct sock *nls;
174 static DEFINE_MUTEX(rx_queue_mutex);
176 static LIST_HEAD(sesslist);
177 static DEFINE_SPINLOCK(sesslock);
178 static LIST_HEAD(connlist);
179 static DEFINE_SPINLOCK(connlock);
181 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
183 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
184 return sess->sid;
188 * Returns the matching session to a given sid
190 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
192 unsigned long flags;
193 struct iscsi_cls_session *sess;
195 spin_lock_irqsave(&sesslock, flags);
196 list_for_each_entry(sess, &sesslist, sess_list) {
197 if (sess->sid == sid) {
198 spin_unlock_irqrestore(&sesslock, flags);
199 return sess;
202 spin_unlock_irqrestore(&sesslock, flags);
203 return NULL;
207 * Returns the matching connection to a given sid / cid tuple
209 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
211 unsigned long flags;
212 struct iscsi_cls_conn *conn;
214 spin_lock_irqsave(&connlock, flags);
215 list_for_each_entry(conn, &connlist, conn_list) {
216 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
217 spin_unlock_irqrestore(&connlock, flags);
218 return conn;
221 spin_unlock_irqrestore(&connlock, flags);
222 return NULL;
226 * The following functions can be used by LLDs that allocate
227 * their own scsi_hosts or by software iscsi LLDs
229 static struct {
230 int value;
231 char *name;
232 } iscsi_session_state_names[] = {
233 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
234 { ISCSI_SESSION_FAILED, "FAILED" },
235 { ISCSI_SESSION_FREE, "FREE" },
238 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
239 const char *iscsi_session_state_name(int state)
240 =======
241 static const char *iscsi_session_state_name(int state)
242 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
244 int i;
245 char *name = NULL;
247 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
248 if (iscsi_session_state_names[i].value == state) {
249 name = iscsi_session_state_names[i].name;
250 break;
253 return name;
256 int iscsi_session_chkready(struct iscsi_cls_session *session)
258 unsigned long flags;
259 int err;
261 spin_lock_irqsave(&session->lock, flags);
262 switch (session->state) {
263 case ISCSI_SESSION_LOGGED_IN:
264 err = 0;
265 break;
266 case ISCSI_SESSION_FAILED:
267 err = DID_IMM_RETRY << 16;
268 break;
269 case ISCSI_SESSION_FREE:
270 err = DID_NO_CONNECT << 16;
271 break;
272 default:
273 err = DID_NO_CONNECT << 16;
274 break;
276 spin_unlock_irqrestore(&session->lock, flags);
277 return err;
279 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
281 static void iscsi_session_release(struct device *dev)
283 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
284 struct Scsi_Host *shost;
286 shost = iscsi_session_to_shost(session);
287 scsi_host_put(shost);
288 kfree(session);
291 static int iscsi_is_session_dev(const struct device *dev)
293 return dev->release == iscsi_session_release;
297 * iscsi_scan_finished - helper to report when running scans are done
298 * @shost: scsi host
299 * @time: scan run time
301 * This function can be used by drives like qla4xxx to report to the scsi
302 * layer when the scans it kicked off at module load time are done.
304 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
306 struct iscsi_host *ihost = shost->shost_data;
308 * qla4xxx will have kicked off some session unblocks before calling
309 * scsi_scan_host, so just wait for them to complete.
311 return !atomic_read(&ihost->nr_scans);
313 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
315 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
316 uint id, uint lun)
318 struct iscsi_host *ihost = shost->shost_data;
319 struct iscsi_cls_session *session;
321 mutex_lock(&ihost->mutex);
322 list_for_each_entry(session, &ihost->sessions, host_list) {
323 if ((channel == SCAN_WILD_CARD || channel == 0) &&
324 (id == SCAN_WILD_CARD || id == session->target_id))
325 scsi_scan_target(&session->dev, 0,
326 session->target_id, lun, 1);
328 mutex_unlock(&ihost->mutex);
330 return 0;
333 static void iscsi_scan_session(struct work_struct *work)
335 struct iscsi_cls_session *session =
336 container_of(work, struct iscsi_cls_session, scan_work);
337 struct Scsi_Host *shost = iscsi_session_to_shost(session);
338 struct iscsi_host *ihost = shost->shost_data;
339 unsigned long flags;
341 spin_lock_irqsave(&session->lock, flags);
342 if (session->state != ISCSI_SESSION_LOGGED_IN) {
343 spin_unlock_irqrestore(&session->lock, flags);
344 goto done;
346 spin_unlock_irqrestore(&session->lock, flags);
348 scsi_scan_target(&session->dev, 0, session->target_id,
349 SCAN_WILD_CARD, 1);
350 done:
351 atomic_dec(&ihost->nr_scans);
354 static void session_recovery_timedout(struct work_struct *work)
356 struct iscsi_cls_session *session =
357 container_of(work, struct iscsi_cls_session,
358 recovery_work.work);
359 unsigned long flags;
361 iscsi_cls_session_printk(KERN_INFO, session,
362 "session recovery timed out after %d secs\n",
363 session->recovery_tmo);
365 spin_lock_irqsave(&session->lock, flags);
366 switch (session->state) {
367 case ISCSI_SESSION_FAILED:
368 session->state = ISCSI_SESSION_FREE;
369 break;
370 case ISCSI_SESSION_LOGGED_IN:
371 case ISCSI_SESSION_FREE:
372 /* we raced with the unblock's flush */
373 spin_unlock_irqrestore(&session->lock, flags);
374 return;
376 spin_unlock_irqrestore(&session->lock, flags);
378 if (session->transport->session_recovery_timedout)
379 session->transport->session_recovery_timedout(session);
381 scsi_target_unblock(&session->dev);
384 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
385 void __iscsi_unblock_session(struct iscsi_cls_session *session)
387 if (!cancel_delayed_work(&session->recovery_work))
388 flush_workqueue(iscsi_eh_timer_workq);
389 scsi_target_unblock(&session->dev);
392 void iscsi_unblock_session(struct iscsi_cls_session *session)
393 =======
394 static void __iscsi_unblock_session(struct work_struct *work)
395 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
397 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
398 =======
399 struct iscsi_cls_session *session =
400 container_of(work, struct iscsi_cls_session,
401 unblock_work);
402 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
403 struct Scsi_Host *shost = iscsi_session_to_shost(session);
404 struct iscsi_host *ihost = shost->shost_data;
405 unsigned long flags;
407 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
408 =======
410 * The recovery and unblock work get run from the same workqueue,
411 * so try to cancel it if it was going to run after this unblock.
413 cancel_delayed_work(&session->recovery_work);
414 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
415 spin_lock_irqsave(&session->lock, flags);
416 session->state = ISCSI_SESSION_LOGGED_IN;
417 spin_unlock_irqrestore(&session->lock, flags);
418 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
420 __iscsi_unblock_session(session);
421 =======
422 /* start IO */
423 scsi_target_unblock(&session->dev);
424 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
426 * Only do kernel scanning if the driver is properly hooked into
427 * the async scanning code (drivers like iscsi_tcp do login and
428 * scanning from userspace).
430 if (shost->hostt->scan_finished) {
431 if (queue_work(ihost->scan_workq, &session->scan_work))
432 atomic_inc(&ihost->nr_scans);
435 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
436 =======
439 * iscsi_unblock_session - set a session as logged in and start IO.
440 * @session: iscsi session
442 * Mark a session as ready to accept IO.
444 void iscsi_unblock_session(struct iscsi_cls_session *session)
446 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
448 * make sure all the events have completed before tell the driver
449 * it is safe
451 flush_workqueue(iscsi_eh_timer_workq);
453 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
454 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
456 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
457 void iscsi_block_session(struct iscsi_cls_session *session)
458 =======
459 static void __iscsi_block_session(struct work_struct *work)
460 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
462 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
463 =======
464 struct iscsi_cls_session *session =
465 container_of(work, struct iscsi_cls_session,
466 block_work);
467 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
468 unsigned long flags;
470 spin_lock_irqsave(&session->lock, flags);
471 session->state = ISCSI_SESSION_FAILED;
472 spin_unlock_irqrestore(&session->lock, flags);
473 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
475 =======
476 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
477 scsi_target_block(&session->dev);
478 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
479 session->recovery_tmo * HZ);
481 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
482 =======
484 void iscsi_block_session(struct iscsi_cls_session *session)
486 queue_work(iscsi_eh_timer_workq, &session->block_work);
488 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
489 EXPORT_SYMBOL_GPL(iscsi_block_session);
491 static void __iscsi_unbind_session(struct work_struct *work)
493 struct iscsi_cls_session *session =
494 container_of(work, struct iscsi_cls_session,
495 unbind_work);
496 struct Scsi_Host *shost = iscsi_session_to_shost(session);
497 struct iscsi_host *ihost = shost->shost_data;
499 /* Prevent new scans and make sure scanning is not in progress */
500 mutex_lock(&ihost->mutex);
501 if (list_empty(&session->host_list)) {
502 mutex_unlock(&ihost->mutex);
503 return;
505 list_del_init(&session->host_list);
506 mutex_unlock(&ihost->mutex);
508 scsi_remove_target(&session->dev);
509 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
512 static int iscsi_unbind_session(struct iscsi_cls_session *session)
514 struct Scsi_Host *shost = iscsi_session_to_shost(session);
515 struct iscsi_host *ihost = shost->shost_data;
517 return queue_work(ihost->scan_workq, &session->unbind_work);
520 struct iscsi_cls_session *
521 iscsi_alloc_session(struct Scsi_Host *shost,
522 struct iscsi_transport *transport)
524 struct iscsi_cls_session *session;
526 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
527 GFP_KERNEL);
528 if (!session)
529 return NULL;
531 session->transport = transport;
532 session->recovery_tmo = 120;
533 session->state = ISCSI_SESSION_FREE;
534 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
535 INIT_LIST_HEAD(&session->host_list);
536 INIT_LIST_HEAD(&session->sess_list);
537 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
538 =======
539 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
540 INIT_WORK(&session->block_work, __iscsi_block_session);
541 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
542 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
543 INIT_WORK(&session->scan_work, iscsi_scan_session);
544 spin_lock_init(&session->lock);
546 /* this is released in the dev's release function */
547 scsi_host_get(shost);
548 session->dev.parent = &shost->shost_gendev;
549 session->dev.release = iscsi_session_release;
550 device_initialize(&session->dev);
551 if (transport->sessiondata_size)
552 session->dd_data = &session[1];
553 return session;
555 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
557 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
559 struct Scsi_Host *shost = iscsi_session_to_shost(session);
560 struct iscsi_host *ihost;
561 unsigned long flags;
562 int err;
564 ihost = shost->shost_data;
565 session->sid = atomic_add_return(1, &iscsi_session_nr);
566 session->target_id = target_id;
568 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
569 session->sid);
570 err = device_add(&session->dev);
571 if (err) {
572 iscsi_cls_session_printk(KERN_ERR, session,
573 "could not register session's dev\n");
574 goto release_host;
576 transport_register_device(&session->dev);
578 spin_lock_irqsave(&sesslock, flags);
579 list_add(&session->sess_list, &sesslist);
580 spin_unlock_irqrestore(&sesslock, flags);
582 mutex_lock(&ihost->mutex);
583 list_add(&session->host_list, &ihost->sessions);
584 mutex_unlock(&ihost->mutex);
586 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
587 return 0;
589 release_host:
590 scsi_host_put(shost);
591 return err;
593 EXPORT_SYMBOL_GPL(iscsi_add_session);
596 * iscsi_create_session - create iscsi class session
597 * @shost: scsi host
598 * @transport: iscsi transport
599 * @target_id: which target
601 * This can be called from a LLD or iscsi_transport.
603 struct iscsi_cls_session *
604 iscsi_create_session(struct Scsi_Host *shost,
605 struct iscsi_transport *transport,
606 unsigned int target_id)
608 struct iscsi_cls_session *session;
610 session = iscsi_alloc_session(shost, transport);
611 if (!session)
612 return NULL;
614 if (iscsi_add_session(session, target_id)) {
615 iscsi_free_session(session);
616 return NULL;
618 return session;
620 EXPORT_SYMBOL_GPL(iscsi_create_session);
622 static void iscsi_conn_release(struct device *dev)
624 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
625 struct device *parent = conn->dev.parent;
627 kfree(conn);
628 put_device(parent);
631 static int iscsi_is_conn_dev(const struct device *dev)
633 return dev->release == iscsi_conn_release;
636 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
638 if (!iscsi_is_conn_dev(dev))
639 return 0;
640 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
643 void iscsi_remove_session(struct iscsi_cls_session *session)
645 struct Scsi_Host *shost = iscsi_session_to_shost(session);
646 struct iscsi_host *ihost = shost->shost_data;
647 unsigned long flags;
648 int err;
650 spin_lock_irqsave(&sesslock, flags);
651 list_del(&session->sess_list);
652 spin_unlock_irqrestore(&sesslock, flags);
654 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
655 =======
656 /* make sure there are no blocks/unblocks queued */
657 flush_workqueue(iscsi_eh_timer_workq);
658 /* make sure the timedout callout is not running */
659 if (!cancel_delayed_work(&session->recovery_work))
660 flush_workqueue(iscsi_eh_timer_workq);
661 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
663 * If we are blocked let commands flow again. The lld or iscsi
664 * layer should set up the queuecommand to fail commands.
665 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
666 =======
667 * We assume that LLD will not be calling block/unblock while
668 * removing the session.
669 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
671 spin_lock_irqsave(&session->lock, flags);
672 session->state = ISCSI_SESSION_FREE;
673 spin_unlock_irqrestore(&session->lock, flags);
674 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
675 __iscsi_unblock_session(session);
676 __iscsi_unbind_session(&session->unbind_work);
677 =======
678 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
680 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
681 /* flush running scans */
682 =======
683 scsi_target_unblock(&session->dev);
684 /* flush running scans then delete devices */
685 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
686 flush_workqueue(ihost->scan_workq);
687 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
689 * If the session dropped while removing devices then we need to make
690 * sure it is not blocked
692 if (!cancel_delayed_work(&session->recovery_work))
693 flush_workqueue(iscsi_eh_timer_workq);
694 =======
695 __iscsi_unbind_session(&session->unbind_work);
696 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
698 /* hw iscsi may not have removed all connections from session */
699 err = device_for_each_child(&session->dev, NULL,
700 iscsi_iter_destroy_conn_fn);
701 if (err)
702 iscsi_cls_session_printk(KERN_ERR, session,
703 "Could not delete all connections "
704 "for session. Error %d.\n", err);
706 transport_unregister_device(&session->dev);
707 device_del(&session->dev);
709 EXPORT_SYMBOL_GPL(iscsi_remove_session);
711 void iscsi_free_session(struct iscsi_cls_session *session)
713 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
714 put_device(&session->dev);
716 EXPORT_SYMBOL_GPL(iscsi_free_session);
719 * iscsi_destroy_session - destroy iscsi session
720 * @session: iscsi_session
722 * Can be called by a LLD or iscsi_transport. There must not be
723 * any running connections.
725 int iscsi_destroy_session(struct iscsi_cls_session *session)
727 iscsi_remove_session(session);
728 iscsi_free_session(session);
729 return 0;
731 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
734 * iscsi_create_conn - create iscsi class connection
735 * @session: iscsi cls session
736 * @cid: connection id
738 * This can be called from a LLD or iscsi_transport. The connection
739 * is child of the session so cid must be unique for all connections
740 * on the session.
742 * Since we do not support MCS, cid will normally be zero. In some cases
743 * for software iscsi we could be trying to preallocate a connection struct
744 * in which case there could be two connection structs and cid would be
745 * non-zero.
747 struct iscsi_cls_conn *
748 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
750 struct iscsi_transport *transport = session->transport;
751 struct iscsi_cls_conn *conn;
752 unsigned long flags;
753 int err;
755 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
756 if (!conn)
757 return NULL;
759 if (transport->conndata_size)
760 conn->dd_data = &conn[1];
762 INIT_LIST_HEAD(&conn->conn_list);
763 conn->transport = transport;
764 conn->cid = cid;
766 /* this is released in the dev's release function */
767 if (!get_device(&session->dev))
768 goto free_conn;
770 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
771 session->sid, cid);
772 conn->dev.parent = &session->dev;
773 conn->dev.release = iscsi_conn_release;
774 err = device_register(&conn->dev);
775 if (err) {
776 iscsi_cls_session_printk(KERN_ERR, session, "could not "
777 "register connection's dev\n");
778 goto release_parent_ref;
780 transport_register_device(&conn->dev);
782 spin_lock_irqsave(&connlock, flags);
783 list_add(&conn->conn_list, &connlist);
784 conn->active = 1;
785 spin_unlock_irqrestore(&connlock, flags);
786 return conn;
788 release_parent_ref:
789 put_device(&session->dev);
790 free_conn:
791 kfree(conn);
792 return NULL;
795 EXPORT_SYMBOL_GPL(iscsi_create_conn);
798 * iscsi_destroy_conn - destroy iscsi class connection
799 * @conn: iscsi cls session
801 * This can be called from a LLD or iscsi_transport.
803 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
805 unsigned long flags;
807 spin_lock_irqsave(&connlock, flags);
808 conn->active = 0;
809 list_del(&conn->conn_list);
810 spin_unlock_irqrestore(&connlock, flags);
812 transport_unregister_device(&conn->dev);
813 device_unregister(&conn->dev);
814 return 0;
816 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
819 * iscsi interface functions
821 static struct iscsi_internal *
822 iscsi_if_transport_lookup(struct iscsi_transport *tt)
824 struct iscsi_internal *priv;
825 unsigned long flags;
827 spin_lock_irqsave(&iscsi_transport_lock, flags);
828 list_for_each_entry(priv, &iscsi_transports, list) {
829 if (tt == priv->iscsi_transport) {
830 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
831 return priv;
834 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
835 return NULL;
838 static int
839 iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
841 int rc;
843 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
844 if (rc < 0) {
845 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
846 return rc;
849 return 0;
852 static int
853 iscsi_unicast_skb(struct sk_buff *skb, int pid)
855 int rc;
857 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
858 if (rc < 0) {
859 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
860 return rc;
863 return 0;
866 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
867 char *data, uint32_t data_size)
869 struct nlmsghdr *nlh;
870 struct sk_buff *skb;
871 struct iscsi_uevent *ev;
872 char *pdu;
873 struct iscsi_internal *priv;
874 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
875 data_size);
877 priv = iscsi_if_transport_lookup(conn->transport);
878 if (!priv)
879 return -EINVAL;
881 skb = alloc_skb(len, GFP_ATOMIC);
882 if (!skb) {
883 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
884 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
885 "control PDU: OOM\n");
886 return -ENOMEM;
889 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
890 ev = NLMSG_DATA(nlh);
891 memset(ev, 0, sizeof(*ev));
892 ev->transport_handle = iscsi_handle(conn->transport);
893 ev->type = ISCSI_KEVENT_RECV_PDU;
894 ev->r.recv_req.cid = conn->cid;
895 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
896 pdu = (char*)ev + sizeof(*ev);
897 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
898 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
900 return iscsi_unicast_skb(skb, priv->daemon_pid);
902 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
904 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
906 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
907 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
908 =======
909 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
910 struct nlmsghdr *nlh;
911 struct sk_buff *skb;
912 struct iscsi_uevent *ev;
913 struct iscsi_internal *priv;
914 int len = NLMSG_SPACE(sizeof(*ev));
915 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
916 unsigned long flags;
917 =======
918 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
920 priv = iscsi_if_transport_lookup(conn->transport);
921 if (!priv)
922 return;
924 <<<<<<< HEAD:drivers/scsi/scsi_transport_iscsi.c
925 spin_lock_irqsave(&session->lock, flags);
926 if (session->state == ISCSI_SESSION_LOGGED_IN)
927 session->state = ISCSI_SESSION_FAILED;
928 spin_unlock_irqrestore(&session->lock, flags);
930 =======
931 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/scsi_transport_iscsi.c
932 skb = alloc_skb(len, GFP_ATOMIC);
933 if (!skb) {
934 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
935 "conn error (%d)\n", error);
936 return;
939 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
940 ev = NLMSG_DATA(nlh);
941 ev->transport_handle = iscsi_handle(conn->transport);
942 ev->type = ISCSI_KEVENT_CONN_ERROR;
943 ev->r.connerror.error = error;
944 ev->r.connerror.cid = conn->cid;
945 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
947 iscsi_broadcast_skb(skb, GFP_ATOMIC);
949 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
950 error);
952 EXPORT_SYMBOL_GPL(iscsi_conn_error);
954 static int
955 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
956 void *payload, int size)
958 struct sk_buff *skb;
959 struct nlmsghdr *nlh;
960 int len = NLMSG_SPACE(size);
961 int flags = multi ? NLM_F_MULTI : 0;
962 int t = done ? NLMSG_DONE : type;
964 skb = alloc_skb(len, GFP_ATOMIC);
965 if (!skb) {
966 printk(KERN_ERR "Could not allocate skb to send reply.\n");
967 return -ENOMEM;
970 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
971 nlh->nlmsg_flags = flags;
972 memcpy(NLMSG_DATA(nlh), payload, size);
973 return iscsi_unicast_skb(skb, pid);
976 static int
977 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
979 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
980 struct iscsi_stats *stats;
981 struct sk_buff *skbstat;
982 struct iscsi_cls_conn *conn;
983 struct nlmsghdr *nlhstat;
984 struct iscsi_uevent *evstat;
985 struct iscsi_internal *priv;
986 int len = NLMSG_SPACE(sizeof(*ev) +
987 sizeof(struct iscsi_stats) +
988 sizeof(struct iscsi_stats_custom) *
989 ISCSI_STATS_CUSTOM_MAX);
990 int err = 0;
992 priv = iscsi_if_transport_lookup(transport);
993 if (!priv)
994 return -EINVAL;
996 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
997 if (!conn)
998 return -EEXIST;
1000 do {
1001 int actual_size;
1003 skbstat = alloc_skb(len, GFP_ATOMIC);
1004 if (!skbstat) {
1005 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1006 "deliver stats: OOM\n");
1007 return -ENOMEM;
1010 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
1011 (len - sizeof(*nlhstat)), 0);
1012 evstat = NLMSG_DATA(nlhstat);
1013 memset(evstat, 0, sizeof(*evstat));
1014 evstat->transport_handle = iscsi_handle(conn->transport);
1015 evstat->type = nlh->nlmsg_type;
1016 evstat->u.get_stats.cid =
1017 ev->u.get_stats.cid;
1018 evstat->u.get_stats.sid =
1019 ev->u.get_stats.sid;
1020 stats = (struct iscsi_stats *)
1021 ((char*)evstat + sizeof(*evstat));
1022 memset(stats, 0, sizeof(*stats));
1024 transport->get_stats(conn, stats);
1025 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1026 sizeof(struct iscsi_stats) +
1027 sizeof(struct iscsi_stats_custom) *
1028 stats->custom_length);
1029 actual_size -= sizeof(*nlhstat);
1030 actual_size = NLMSG_LENGTH(actual_size);
1031 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1032 nlhstat->nlmsg_len = actual_size;
1034 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
1035 } while (err < 0 && err != -ECONNREFUSED);
1037 return err;
1041 * iscsi_session_event - send session destr. completion event
1042 * @session: iscsi class session
1043 * @event: type of event
1045 int iscsi_session_event(struct iscsi_cls_session *session,
1046 enum iscsi_uevent_e event)
1048 struct iscsi_internal *priv;
1049 struct Scsi_Host *shost;
1050 struct iscsi_uevent *ev;
1051 struct sk_buff *skb;
1052 struct nlmsghdr *nlh;
1053 int rc, len = NLMSG_SPACE(sizeof(*ev));
1055 priv = iscsi_if_transport_lookup(session->transport);
1056 if (!priv)
1057 return -EINVAL;
1058 shost = iscsi_session_to_shost(session);
1060 skb = alloc_skb(len, GFP_KERNEL);
1061 if (!skb) {
1062 iscsi_cls_session_printk(KERN_ERR, session,
1063 "Cannot notify userspace of session "
1064 "event %u\n", event);
1065 return -ENOMEM;
1068 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1069 ev = NLMSG_DATA(nlh);
1070 ev->transport_handle = iscsi_handle(session->transport);
1072 ev->type = event;
1073 switch (event) {
1074 case ISCSI_KEVENT_DESTROY_SESSION:
1075 ev->r.d_session.host_no = shost->host_no;
1076 ev->r.d_session.sid = session->sid;
1077 break;
1078 case ISCSI_KEVENT_CREATE_SESSION:
1079 ev->r.c_session_ret.host_no = shost->host_no;
1080 ev->r.c_session_ret.sid = session->sid;
1081 break;
1082 case ISCSI_KEVENT_UNBIND_SESSION:
1083 ev->r.unbind_session.host_no = shost->host_no;
1084 ev->r.unbind_session.sid = session->sid;
1085 break;
1086 default:
1087 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1088 "%u.\n", event);
1089 kfree_skb(skb);
1090 return -EINVAL;
1094 * this will occur if the daemon is not up, so we just warn
1095 * the user and when the daemon is restarted it will handle it
1097 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
1098 if (rc < 0)
1099 iscsi_cls_session_printk(KERN_ERR, session,
1100 "Cannot notify userspace of session "
1101 "event %u. Check iscsi daemon\n",
1102 event);
1103 return rc;
1105 EXPORT_SYMBOL_GPL(iscsi_session_event);
1107 static int
1108 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
1110 struct iscsi_transport *transport = priv->iscsi_transport;
1111 struct iscsi_cls_session *session;
1112 uint32_t hostno;
1114 session = transport->create_session(transport, &priv->t,
1115 ev->u.c_session.cmds_max,
1116 ev->u.c_session.queue_depth,
1117 ev->u.c_session.initial_cmdsn,
1118 &hostno);
1119 if (!session)
1120 return -ENOMEM;
1122 ev->r.c_session_ret.host_no = hostno;
1123 ev->r.c_session_ret.sid = session->sid;
1124 return 0;
1127 static int
1128 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1130 struct iscsi_cls_conn *conn;
1131 struct iscsi_cls_session *session;
1133 session = iscsi_session_lookup(ev->u.c_conn.sid);
1134 if (!session) {
1135 printk(KERN_ERR "iscsi: invalid session %d.\n",
1136 ev->u.c_conn.sid);
1137 return -EINVAL;
1140 conn = transport->create_conn(session, ev->u.c_conn.cid);
1141 if (!conn) {
1142 iscsi_cls_session_printk(KERN_ERR, session,
1143 "couldn't create a new connection.");
1144 return -ENOMEM;
1147 ev->r.c_conn_ret.sid = session->sid;
1148 ev->r.c_conn_ret.cid = conn->cid;
1149 return 0;
1152 static int
1153 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1155 struct iscsi_cls_conn *conn;
1157 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1158 if (!conn)
1159 return -EINVAL;
1161 if (transport->destroy_conn)
1162 transport->destroy_conn(conn);
1163 return 0;
1166 static int
1167 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1169 char *data = (char*)ev + sizeof(*ev);
1170 struct iscsi_cls_conn *conn;
1171 struct iscsi_cls_session *session;
1172 int err = 0, value = 0;
1174 session = iscsi_session_lookup(ev->u.set_param.sid);
1175 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1176 if (!conn || !session)
1177 return -EINVAL;
1179 switch (ev->u.set_param.param) {
1180 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1181 sscanf(data, "%d", &value);
1182 if (value != 0)
1183 session->recovery_tmo = value;
1184 break;
1185 default:
1186 err = transport->set_param(conn, ev->u.set_param.param,
1187 data, ev->u.set_param.len);
1190 return err;
1193 static int
1194 iscsi_if_transport_ep(struct iscsi_transport *transport,
1195 struct iscsi_uevent *ev, int msg_type)
1197 struct sockaddr *dst_addr;
1198 int rc = 0;
1200 switch (msg_type) {
1201 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1202 if (!transport->ep_connect)
1203 return -EINVAL;
1205 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1206 rc = transport->ep_connect(dst_addr,
1207 ev->u.ep_connect.non_blocking,
1208 &ev->r.ep_connect_ret.handle);
1209 break;
1210 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1211 if (!transport->ep_poll)
1212 return -EINVAL;
1214 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1215 ev->u.ep_poll.timeout_ms);
1216 break;
1217 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1218 if (!transport->ep_disconnect)
1219 return -EINVAL;
1221 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1222 break;
1224 return rc;
1227 static int
1228 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1229 struct iscsi_uevent *ev)
1231 struct Scsi_Host *shost;
1232 struct sockaddr *dst_addr;
1233 int err;
1235 if (!transport->tgt_dscvr)
1236 return -EINVAL;
1238 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1239 if (IS_ERR(shost)) {
1240 printk(KERN_ERR "target discovery could not find host no %u\n",
1241 ev->u.tgt_dscvr.host_no);
1242 return -ENODEV;
1246 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1247 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1248 ev->u.tgt_dscvr.enable, dst_addr);
1249 scsi_host_put(shost);
1250 return err;
1253 static int
1254 iscsi_set_host_param(struct iscsi_transport *transport,
1255 struct iscsi_uevent *ev)
1257 char *data = (char*)ev + sizeof(*ev);
1258 struct Scsi_Host *shost;
1259 int err;
1261 if (!transport->set_host_param)
1262 return -ENOSYS;
1264 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1265 if (IS_ERR(shost)) {
1266 printk(KERN_ERR "set_host_param could not find host no %u\n",
1267 ev->u.set_host_param.host_no);
1268 return -ENODEV;
1271 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1272 data, ev->u.set_host_param.len);
1273 scsi_host_put(shost);
1274 return err;
1277 static int
1278 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1280 int err = 0;
1281 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1282 struct iscsi_transport *transport = NULL;
1283 struct iscsi_internal *priv;
1284 struct iscsi_cls_session *session;
1285 struct iscsi_cls_conn *conn;
1287 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1288 if (!priv)
1289 return -EINVAL;
1290 transport = priv->iscsi_transport;
1292 if (!try_module_get(transport->owner))
1293 return -EINVAL;
1295 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1297 switch (nlh->nlmsg_type) {
1298 case ISCSI_UEVENT_CREATE_SESSION:
1299 err = iscsi_if_create_session(priv, ev);
1300 break;
1301 case ISCSI_UEVENT_DESTROY_SESSION:
1302 session = iscsi_session_lookup(ev->u.d_session.sid);
1303 if (session)
1304 transport->destroy_session(session);
1305 else
1306 err = -EINVAL;
1307 break;
1308 case ISCSI_UEVENT_UNBIND_SESSION:
1309 session = iscsi_session_lookup(ev->u.d_session.sid);
1310 if (session)
1311 iscsi_unbind_session(session);
1312 else
1313 err = -EINVAL;
1314 break;
1315 case ISCSI_UEVENT_CREATE_CONN:
1316 err = iscsi_if_create_conn(transport, ev);
1317 break;
1318 case ISCSI_UEVENT_DESTROY_CONN:
1319 err = iscsi_if_destroy_conn(transport, ev);
1320 break;
1321 case ISCSI_UEVENT_BIND_CONN:
1322 session = iscsi_session_lookup(ev->u.b_conn.sid);
1323 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1325 if (session && conn)
1326 ev->r.retcode = transport->bind_conn(session, conn,
1327 ev->u.b_conn.transport_eph,
1328 ev->u.b_conn.is_leading);
1329 else
1330 err = -EINVAL;
1331 break;
1332 case ISCSI_UEVENT_SET_PARAM:
1333 err = iscsi_set_param(transport, ev);
1334 break;
1335 case ISCSI_UEVENT_START_CONN:
1336 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1337 if (conn)
1338 ev->r.retcode = transport->start_conn(conn);
1339 else
1340 err = -EINVAL;
1341 break;
1342 case ISCSI_UEVENT_STOP_CONN:
1343 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1344 if (conn)
1345 transport->stop_conn(conn, ev->u.stop_conn.flag);
1346 else
1347 err = -EINVAL;
1348 break;
1349 case ISCSI_UEVENT_SEND_PDU:
1350 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1351 if (conn)
1352 ev->r.retcode = transport->send_pdu(conn,
1353 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1354 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1355 ev->u.send_pdu.data_size);
1356 else
1357 err = -EINVAL;
1358 break;
1359 case ISCSI_UEVENT_GET_STATS:
1360 err = iscsi_if_get_stats(transport, nlh);
1361 break;
1362 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1363 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1364 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1365 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1366 break;
1367 case ISCSI_UEVENT_TGT_DSCVR:
1368 err = iscsi_tgt_dscvr(transport, ev);
1369 break;
1370 case ISCSI_UEVENT_SET_HOST_PARAM:
1371 err = iscsi_set_host_param(transport, ev);
1372 break;
1373 default:
1374 err = -ENOSYS;
1375 break;
1378 module_put(transport->owner);
1379 return err;
1383 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1384 * Malformed skbs with wrong lengths or invalid creds are not processed.
1386 static void
1387 iscsi_if_rx(struct sk_buff *skb)
1389 mutex_lock(&rx_queue_mutex);
1390 while (skb->len >= NLMSG_SPACE(0)) {
1391 int err;
1392 uint32_t rlen;
1393 struct nlmsghdr *nlh;
1394 struct iscsi_uevent *ev;
1396 nlh = nlmsg_hdr(skb);
1397 if (nlh->nlmsg_len < sizeof(*nlh) ||
1398 skb->len < nlh->nlmsg_len) {
1399 break;
1402 ev = NLMSG_DATA(nlh);
1403 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1404 if (rlen > skb->len)
1405 rlen = skb->len;
1407 err = iscsi_if_recv_msg(skb, nlh);
1408 if (err) {
1409 ev->type = ISCSI_KEVENT_IF_ERROR;
1410 ev->iferror = err;
1412 do {
1414 * special case for GET_STATS:
1415 * on success - sending reply and stats from
1416 * inside of if_recv_msg(),
1417 * on error - fall through.
1419 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1420 break;
1421 err = iscsi_if_send_reply(
1422 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1423 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1424 } while (err < 0 && err != -ECONNREFUSED);
1425 skb_pull(skb, rlen);
1427 mutex_unlock(&rx_queue_mutex);
1430 #define iscsi_cdev_to_conn(_cdev) \
1431 iscsi_dev_to_conn(_cdev->dev)
1433 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1434 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1435 __ATTR(_name,_mode,_show,_store)
1438 * iSCSI connection attrs
1440 #define iscsi_conn_attr_show(param) \
1441 static ssize_t \
1442 show_conn_param_##param(struct class_device *cdev, char *buf) \
1444 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1445 struct iscsi_transport *t = conn->transport; \
1446 return t->get_conn_param(conn, param, buf); \
1449 #define iscsi_conn_attr(field, param) \
1450 iscsi_conn_attr_show(param) \
1451 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1452 NULL);
1454 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1455 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1456 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1457 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1458 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1459 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1460 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1461 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1462 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1463 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1464 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1465 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1466 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1468 #define iscsi_cdev_to_session(_cdev) \
1469 iscsi_dev_to_session(_cdev->dev)
1472 * iSCSI session attrs
1474 #define iscsi_session_attr_show(param, perm) \
1475 static ssize_t \
1476 show_session_param_##param(struct class_device *cdev, char *buf) \
1478 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1479 struct iscsi_transport *t = session->transport; \
1481 if (perm && !capable(CAP_SYS_ADMIN)) \
1482 return -EACCES; \
1483 return t->get_session_param(session, param, buf); \
1486 #define iscsi_session_attr(field, param, perm) \
1487 iscsi_session_attr_show(param, perm) \
1488 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1489 NULL);
1491 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1492 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1493 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1494 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1495 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1496 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1497 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1498 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1499 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1500 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1501 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1502 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1503 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1504 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
1505 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1506 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1507 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
1509 static ssize_t
1510 show_priv_session_state(struct class_device *cdev, char *buf)
1512 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);
1513 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1515 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1516 NULL);
1518 #define iscsi_priv_session_attr_show(field, format) \
1519 static ssize_t \
1520 show_priv_session_##field(struct class_device *cdev, char *buf) \
1522 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
1523 return sprintf(buf, format"\n", session->field); \
1526 #define iscsi_priv_session_attr(field, format) \
1527 iscsi_priv_session_attr_show(field, format) \
1528 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1529 NULL)
1530 iscsi_priv_session_attr(recovery_tmo, "%d");
1533 * iSCSI host attrs
1535 #define iscsi_host_attr_show(param) \
1536 static ssize_t \
1537 show_host_param_##param(struct class_device *cdev, char *buf) \
1539 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1540 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1541 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1544 #define iscsi_host_attr(field, param) \
1545 iscsi_host_attr_show(param) \
1546 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1547 NULL);
1549 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
1550 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
1551 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
1552 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
1554 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1555 do { \
1556 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1557 count++; \
1558 } while (0)
1561 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1562 do { \
1563 if (tt->param_mask & param_flag) { \
1564 priv->session_attrs[count] = &class_device_attr_sess_##field; \
1565 count++; \
1567 } while (0)
1569 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1570 do { \
1571 if (tt->param_mask & param_flag) { \
1572 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1573 count++; \
1575 } while (0)
1577 #define SETUP_HOST_RD_ATTR(field, param_flag) \
1578 do { \
1579 if (tt->host_param_mask & param_flag) { \
1580 priv->host_attrs[count] = &class_device_attr_host_##field; \
1581 count++; \
1583 } while (0)
1585 static int iscsi_session_match(struct attribute_container *cont,
1586 struct device *dev)
1588 struct iscsi_cls_session *session;
1589 struct Scsi_Host *shost;
1590 struct iscsi_internal *priv;
1592 if (!iscsi_is_session_dev(dev))
1593 return 0;
1595 session = iscsi_dev_to_session(dev);
1596 shost = iscsi_session_to_shost(session);
1597 if (!shost->transportt)
1598 return 0;
1600 priv = to_iscsi_internal(shost->transportt);
1601 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1602 return 0;
1604 return &priv->session_cont.ac == cont;
1607 static int iscsi_conn_match(struct attribute_container *cont,
1608 struct device *dev)
1610 struct iscsi_cls_session *session;
1611 struct iscsi_cls_conn *conn;
1612 struct Scsi_Host *shost;
1613 struct iscsi_internal *priv;
1615 if (!iscsi_is_conn_dev(dev))
1616 return 0;
1618 conn = iscsi_dev_to_conn(dev);
1619 session = iscsi_dev_to_session(conn->dev.parent);
1620 shost = iscsi_session_to_shost(session);
1622 if (!shost->transportt)
1623 return 0;
1625 priv = to_iscsi_internal(shost->transportt);
1626 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1627 return 0;
1629 return &priv->conn_cont.ac == cont;
1632 static int iscsi_host_match(struct attribute_container *cont,
1633 struct device *dev)
1635 struct Scsi_Host *shost;
1636 struct iscsi_internal *priv;
1638 if (!scsi_is_host_device(dev))
1639 return 0;
1641 shost = dev_to_shost(dev);
1642 if (!shost->transportt ||
1643 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1644 return 0;
1646 priv = to_iscsi_internal(shost->transportt);
1647 return &priv->t.host_attrs.ac == cont;
1650 struct scsi_transport_template *
1651 iscsi_register_transport(struct iscsi_transport *tt)
1653 struct iscsi_internal *priv;
1654 unsigned long flags;
1655 int count = 0, err;
1657 BUG_ON(!tt);
1659 priv = iscsi_if_transport_lookup(tt);
1660 if (priv)
1661 return NULL;
1663 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1664 if (!priv)
1665 return NULL;
1666 INIT_LIST_HEAD(&priv->list);
1667 priv->daemon_pid = -1;
1668 priv->iscsi_transport = tt;
1669 priv->t.user_scan = iscsi_user_scan;
1671 priv->cdev.class = &iscsi_transport_class;
1672 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1673 err = class_device_register(&priv->cdev);
1674 if (err)
1675 goto free_priv;
1677 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1678 if (err)
1679 goto unregister_cdev;
1681 /* host parameters */
1682 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1683 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1684 priv->t.host_attrs.ac.match = iscsi_host_match;
1685 priv->t.host_size = sizeof(struct iscsi_host);
1686 transport_container_register(&priv->t.host_attrs);
1688 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
1689 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
1690 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
1691 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
1692 BUG_ON(count > ISCSI_HOST_ATTRS);
1693 priv->host_attrs[count] = NULL;
1694 count = 0;
1696 /* connection parameters */
1697 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1698 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1699 priv->conn_cont.ac.match = iscsi_conn_match;
1700 transport_container_register(&priv->conn_cont);
1702 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1703 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1704 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1705 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1706 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1707 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1708 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1709 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1710 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1711 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1712 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1713 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1714 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
1716 BUG_ON(count > ISCSI_CONN_ATTRS);
1717 priv->conn_attrs[count] = NULL;
1718 count = 0;
1720 /* session parameters */
1721 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1722 priv->session_cont.ac.class = &iscsi_session_class.class;
1723 priv->session_cont.ac.match = iscsi_session_match;
1724 transport_container_register(&priv->session_cont);
1726 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1727 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1728 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1729 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1730 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1731 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1732 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1733 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1734 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1735 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1736 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1737 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1738 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1739 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
1740 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
1741 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1742 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
1743 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1744 SETUP_PRIV_SESSION_RD_ATTR(state);
1746 BUG_ON(count > ISCSI_SESSION_ATTRS);
1747 priv->session_attrs[count] = NULL;
1749 spin_lock_irqsave(&iscsi_transport_lock, flags);
1750 list_add(&priv->list, &iscsi_transports);
1751 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1753 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1754 return &priv->t;
1756 unregister_cdev:
1757 class_device_unregister(&priv->cdev);
1758 free_priv:
1759 kfree(priv);
1760 return NULL;
1762 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1764 int iscsi_unregister_transport(struct iscsi_transport *tt)
1766 struct iscsi_internal *priv;
1767 unsigned long flags;
1769 BUG_ON(!tt);
1771 mutex_lock(&rx_queue_mutex);
1773 priv = iscsi_if_transport_lookup(tt);
1774 BUG_ON (!priv);
1776 spin_lock_irqsave(&iscsi_transport_lock, flags);
1777 list_del(&priv->list);
1778 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1780 transport_container_unregister(&priv->conn_cont);
1781 transport_container_unregister(&priv->session_cont);
1782 transport_container_unregister(&priv->t.host_attrs);
1784 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1785 class_device_unregister(&priv->cdev);
1786 mutex_unlock(&rx_queue_mutex);
1788 return 0;
1790 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1792 static __init int iscsi_transport_init(void)
1794 int err;
1796 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
1797 ISCSI_TRANSPORT_VERSION);
1799 atomic_set(&iscsi_session_nr, 0);
1801 err = class_register(&iscsi_transport_class);
1802 if (err)
1803 return err;
1805 err = transport_class_register(&iscsi_host_class);
1806 if (err)
1807 goto unregister_transport_class;
1809 err = transport_class_register(&iscsi_connection_class);
1810 if (err)
1811 goto unregister_host_class;
1813 err = transport_class_register(&iscsi_session_class);
1814 if (err)
1815 goto unregister_conn_class;
1817 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
1818 THIS_MODULE);
1819 if (!nls) {
1820 err = -ENOBUFS;
1821 goto unregister_session_class;
1824 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1825 if (!iscsi_eh_timer_workq)
1826 goto release_nls;
1828 return 0;
1830 release_nls:
1831 netlink_kernel_release(nls);
1832 unregister_session_class:
1833 transport_class_unregister(&iscsi_session_class);
1834 unregister_conn_class:
1835 transport_class_unregister(&iscsi_connection_class);
1836 unregister_host_class:
1837 transport_class_unregister(&iscsi_host_class);
1838 unregister_transport_class:
1839 class_unregister(&iscsi_transport_class);
1840 return err;
1843 static void __exit iscsi_transport_exit(void)
1845 destroy_workqueue(iscsi_eh_timer_workq);
1846 netlink_kernel_release(nls);
1847 transport_class_unregister(&iscsi_connection_class);
1848 transport_class_unregister(&iscsi_session_class);
1849 transport_class_unregister(&iscsi_host_class);
1850 class_unregister(&iscsi_transport_class);
1853 module_init(iscsi_transport_init);
1854 module_exit(iscsi_transport_exit);
1856 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1857 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1858 "Alex Aizman <itn780@yahoo.com>");
1859 MODULE_DESCRIPTION("iSCSI Transport Interface");
1860 MODULE_LICENSE("GPL");
1861 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);