]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/scsi_transport_iscsi.c
Merge branch 'kvm-updates/2.6.30' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[mv-sheeva.git] / drivers / scsi / scsi_transport_iscsi.c
1 /*
2  * iSCSI transport class definitions
3  *
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
8  *
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.
13  *
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.
18  *
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.
22  */
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>
32
33 #define ISCSI_SESSION_ATTRS 21
34 #define ISCSI_CONN_ATTRS 13
35 #define ISCSI_HOST_ATTRS 4
36
37 #define ISCSI_TRANSPORT_VERSION "2.0-870"
38
39 struct iscsi_internal {
40         int daemon_pid;
41         struct scsi_transport_template t;
42         struct iscsi_transport *iscsi_transport;
43         struct list_head list;
44         struct device dev;
45
46         struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
47         struct transport_container conn_cont;
48         struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
49         struct transport_container session_cont;
50         struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
51 };
52
53 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
54 static struct workqueue_struct *iscsi_eh_timer_workq;
55
56 /*
57  * list of registered transports and lock that must
58  * be held while accessing list. The iscsi_transport_lock must
59  * be acquired after the rx_queue_mutex.
60  */
61 static LIST_HEAD(iscsi_transports);
62 static DEFINE_SPINLOCK(iscsi_transport_lock);
63
64 #define to_iscsi_internal(tmpl) \
65         container_of(tmpl, struct iscsi_internal, t)
66
67 #define dev_to_iscsi_internal(_dev) \
68         container_of(_dev, struct iscsi_internal, dev)
69
70 static void iscsi_transport_release(struct device *dev)
71 {
72         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
73         kfree(priv);
74 }
75
76 /*
77  * iscsi_transport_class represents the iscsi_transports that are
78  * registered.
79  */
80 static struct class iscsi_transport_class = {
81         .name = "iscsi_transport",
82         .dev_release = iscsi_transport_release,
83 };
84
85 static ssize_t
86 show_transport_handle(struct device *dev, struct device_attribute *attr,
87                       char *buf)
88 {
89         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
90         return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
91 }
92 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
93
94 #define show_transport_attr(name, format)                               \
95 static ssize_t                                                          \
96 show_transport_##name(struct device *dev,                               \
97                       struct device_attribute *attr,char *buf)          \
98 {                                                                       \
99         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);       \
100         return sprintf(buf, format"\n", priv->iscsi_transport->name);   \
101 }                                                                       \
102 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
103
104 show_transport_attr(caps, "0x%x");
105
106 static struct attribute *iscsi_transport_attrs[] = {
107         &dev_attr_handle.attr,
108         &dev_attr_caps.attr,
109         NULL,
110 };
111
112 static struct attribute_group iscsi_transport_group = {
113         .attrs = iscsi_transport_attrs,
114 };
115
116 /*
117  * iSCSI endpoint attrs
118  */
119 #define iscsi_dev_to_endpoint(_dev) \
120         container_of(_dev, struct iscsi_endpoint, dev)
121
122 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store)    \
123 struct device_attribute dev_attr_##_prefix##_##_name =  \
124         __ATTR(_name,_mode,_show,_store)
125
126 static void iscsi_endpoint_release(struct device *dev)
127 {
128         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
129         kfree(ep);
130 }
131
132 static struct class iscsi_endpoint_class = {
133         .name = "iscsi_endpoint",
134         .dev_release = iscsi_endpoint_release,
135 };
136
137 static ssize_t
138 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
139 {
140         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
141         return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
142 }
143 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
144
145 static struct attribute *iscsi_endpoint_attrs[] = {
146         &dev_attr_ep_handle.attr,
147         NULL,
148 };
149
150 static struct attribute_group iscsi_endpoint_group = {
151         .attrs = iscsi_endpoint_attrs,
152 };
153
154 #define ISCSI_MAX_EPID -1
155
156 static int iscsi_match_epid(struct device *dev, void *data)
157 {
158         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
159         uint64_t *epid = (uint64_t *) data;
160
161         return *epid == ep->id;
162 }
163
164 struct iscsi_endpoint *
165 iscsi_create_endpoint(int dd_size)
166 {
167         struct device *dev;
168         struct iscsi_endpoint *ep;
169         uint64_t id;
170         int err;
171
172         for (id = 1; id < ISCSI_MAX_EPID; id++) {
173                 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
174                                         iscsi_match_epid);
175                 if (!dev)
176                         break;
177         }
178         if (id == ISCSI_MAX_EPID) {
179                 printk(KERN_ERR "Too many connections. Max supported %u\n",
180                        ISCSI_MAX_EPID - 1);
181                 return NULL;
182         }
183
184         ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
185         if (!ep)
186                 return NULL;
187
188         ep->id = id;
189         ep->dev.class = &iscsi_endpoint_class;
190         dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
191         err = device_register(&ep->dev);
192         if (err)
193                 goto free_ep;
194
195         err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
196         if (err)
197                 goto unregister_dev;
198
199         if (dd_size)
200                 ep->dd_data = &ep[1];
201         return ep;
202
203 unregister_dev:
204         device_unregister(&ep->dev);
205         return NULL;
206
207 free_ep:
208         kfree(ep);
209         return NULL;
210 }
211 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
212
213 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
214 {
215         sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
216         device_unregister(&ep->dev);
217 }
218 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
219
220 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
221 {
222         struct iscsi_endpoint *ep;
223         struct device *dev;
224
225         dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
226                                 iscsi_match_epid);
227         if (!dev)
228                 return NULL;
229
230         ep = iscsi_dev_to_endpoint(dev);
231         /*
232          * we can drop this now because the interface will prevent
233          * removals and lookups from racing.
234          */
235         put_device(dev);
236         return ep;
237 }
238 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
239
240 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
241                             struct device *cdev)
242 {
243         struct Scsi_Host *shost = dev_to_shost(dev);
244         struct iscsi_cls_host *ihost = shost->shost_data;
245
246         memset(ihost, 0, sizeof(*ihost));
247         atomic_set(&ihost->nr_scans, 0);
248         mutex_init(&ihost->mutex);
249
250         snprintf(ihost->scan_workq_name, sizeof(ihost->scan_workq_name),
251                  "iscsi_scan_%d", shost->host_no);
252         ihost->scan_workq = create_singlethread_workqueue(
253                                                 ihost->scan_workq_name);
254         if (!ihost->scan_workq)
255                 return -ENOMEM;
256         return 0;
257 }
258
259 static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
260                              struct device *cdev)
261 {
262         struct Scsi_Host *shost = dev_to_shost(dev);
263         struct iscsi_cls_host *ihost = shost->shost_data;
264
265         destroy_workqueue(ihost->scan_workq);
266         return 0;
267 }
268
269 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
270                                "iscsi_host",
271                                iscsi_setup_host,
272                                iscsi_remove_host,
273                                NULL);
274
275 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
276                                "iscsi_session",
277                                NULL,
278                                NULL,
279                                NULL);
280
281 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
282                                "iscsi_connection",
283                                NULL,
284                                NULL,
285                                NULL);
286
287 static struct sock *nls;
288 static DEFINE_MUTEX(rx_queue_mutex);
289
290 static LIST_HEAD(sesslist);
291 static DEFINE_SPINLOCK(sesslock);
292 static LIST_HEAD(connlist);
293 static DEFINE_SPINLOCK(connlock);
294
295 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
296 {
297         struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
298         return sess->sid;
299 }
300
301 /*
302  * Returns the matching session to a given sid
303  */
304 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
305 {
306         unsigned long flags;
307         struct iscsi_cls_session *sess;
308
309         spin_lock_irqsave(&sesslock, flags);
310         list_for_each_entry(sess, &sesslist, sess_list) {
311                 if (sess->sid == sid) {
312                         spin_unlock_irqrestore(&sesslock, flags);
313                         return sess;
314                 }
315         }
316         spin_unlock_irqrestore(&sesslock, flags);
317         return NULL;
318 }
319
320 /*
321  * Returns the matching connection to a given sid / cid tuple
322  */
323 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
324 {
325         unsigned long flags;
326         struct iscsi_cls_conn *conn;
327
328         spin_lock_irqsave(&connlock, flags);
329         list_for_each_entry(conn, &connlist, conn_list) {
330                 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
331                         spin_unlock_irqrestore(&connlock, flags);
332                         return conn;
333                 }
334         }
335         spin_unlock_irqrestore(&connlock, flags);
336         return NULL;
337 }
338
339 /*
340  * The following functions can be used by LLDs that allocate
341  * their own scsi_hosts or by software iscsi LLDs
342  */
343 static struct {
344         int value;
345         char *name;
346 } iscsi_session_state_names[] = {
347         { ISCSI_SESSION_LOGGED_IN,      "LOGGED_IN" },
348         { ISCSI_SESSION_FAILED,         "FAILED" },
349         { ISCSI_SESSION_FREE,           "FREE" },
350 };
351
352 static const char *iscsi_session_state_name(int state)
353 {
354         int i;
355         char *name = NULL;
356
357         for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
358                 if (iscsi_session_state_names[i].value == state) {
359                         name = iscsi_session_state_names[i].name;
360                         break;
361                 }
362         }
363         return name;
364 }
365
366 int iscsi_session_chkready(struct iscsi_cls_session *session)
367 {
368         unsigned long flags;
369         int err;
370
371         spin_lock_irqsave(&session->lock, flags);
372         switch (session->state) {
373         case ISCSI_SESSION_LOGGED_IN:
374                 err = 0;
375                 break;
376         case ISCSI_SESSION_FAILED:
377                 err = DID_TRANSPORT_DISRUPTED << 16;
378                 break;
379         case ISCSI_SESSION_FREE:
380                 err = DID_TRANSPORT_FAILFAST << 16;
381                 break;
382         default:
383                 err = DID_NO_CONNECT << 16;
384                 break;
385         }
386         spin_unlock_irqrestore(&session->lock, flags);
387         return err;
388 }
389 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
390
391 static void iscsi_session_release(struct device *dev)
392 {
393         struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
394         struct Scsi_Host *shost;
395
396         shost = iscsi_session_to_shost(session);
397         scsi_host_put(shost);
398         kfree(session);
399 }
400
401 static int iscsi_is_session_dev(const struct device *dev)
402 {
403         return dev->release == iscsi_session_release;
404 }
405
406 static int iscsi_iter_session_fn(struct device *dev, void *data)
407 {
408         void (* fn) (struct iscsi_cls_session *) = data;
409
410         if (!iscsi_is_session_dev(dev))
411                 return 0;
412         fn(iscsi_dev_to_session(dev));
413         return 0;
414 }
415
416 void iscsi_host_for_each_session(struct Scsi_Host *shost,
417                                  void (*fn)(struct iscsi_cls_session *))
418 {
419         device_for_each_child(&shost->shost_gendev, fn,
420                               iscsi_iter_session_fn);
421 }
422 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
423
424 /**
425  * iscsi_scan_finished - helper to report when running scans are done
426  * @shost: scsi host
427  * @time: scan run time
428  *
429  * This function can be used by drives like qla4xxx to report to the scsi
430  * layer when the scans it kicked off at module load time are done.
431  */
432 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
433 {
434         struct iscsi_cls_host *ihost = shost->shost_data;
435         /*
436          * qla4xxx will have kicked off some session unblocks before calling
437          * scsi_scan_host, so just wait for them to complete.
438          */
439         return !atomic_read(&ihost->nr_scans);
440 }
441 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
442
443 struct iscsi_scan_data {
444         unsigned int channel;
445         unsigned int id;
446         unsigned int lun;
447 };
448
449 static int iscsi_user_scan_session(struct device *dev, void *data)
450 {
451         struct iscsi_scan_data *scan_data = data;
452         struct iscsi_cls_session *session;
453         struct Scsi_Host *shost;
454         struct iscsi_cls_host *ihost;
455         unsigned long flags;
456         unsigned int id;
457
458         if (!iscsi_is_session_dev(dev))
459                 return 0;
460
461         session = iscsi_dev_to_session(dev);
462         shost = iscsi_session_to_shost(session);
463         ihost = shost->shost_data;
464
465         mutex_lock(&ihost->mutex);
466         spin_lock_irqsave(&session->lock, flags);
467         if (session->state != ISCSI_SESSION_LOGGED_IN) {
468                 spin_unlock_irqrestore(&session->lock, flags);
469                 mutex_unlock(&ihost->mutex);
470                 return 0;
471         }
472         id = session->target_id;
473         spin_unlock_irqrestore(&session->lock, flags);
474
475         if (id != ISCSI_MAX_TARGET) {
476                 if ((scan_data->channel == SCAN_WILD_CARD ||
477                      scan_data->channel == 0) &&
478                     (scan_data->id == SCAN_WILD_CARD ||
479                      scan_data->id == id))
480                         scsi_scan_target(&session->dev, 0, id,
481                                          scan_data->lun, 1);
482         }
483         mutex_unlock(&ihost->mutex);
484         return 0;
485 }
486
487 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
488                            uint id, uint lun)
489 {
490         struct iscsi_scan_data scan_data;
491
492         scan_data.channel = channel;
493         scan_data.id = id;
494         scan_data.lun = lun;
495
496         return device_for_each_child(&shost->shost_gendev, &scan_data,
497                                      iscsi_user_scan_session);
498 }
499
500 static void iscsi_scan_session(struct work_struct *work)
501 {
502         struct iscsi_cls_session *session =
503                         container_of(work, struct iscsi_cls_session, scan_work);
504         struct Scsi_Host *shost = iscsi_session_to_shost(session);
505         struct iscsi_cls_host *ihost = shost->shost_data;
506         struct iscsi_scan_data scan_data;
507
508         scan_data.channel = 0;
509         scan_data.id = SCAN_WILD_CARD;
510         scan_data.lun = SCAN_WILD_CARD;
511
512         iscsi_user_scan_session(&session->dev, &scan_data);
513         atomic_dec(&ihost->nr_scans);
514 }
515
516 static void session_recovery_timedout(struct work_struct *work)
517 {
518         struct iscsi_cls_session *session =
519                 container_of(work, struct iscsi_cls_session,
520                              recovery_work.work);
521         unsigned long flags;
522
523         iscsi_cls_session_printk(KERN_INFO, session,
524                                  "session recovery timed out after %d secs\n",
525                                  session->recovery_tmo);
526
527         spin_lock_irqsave(&session->lock, flags);
528         switch (session->state) {
529         case ISCSI_SESSION_FAILED:
530                 session->state = ISCSI_SESSION_FREE;
531                 break;
532         case ISCSI_SESSION_LOGGED_IN:
533         case ISCSI_SESSION_FREE:
534                 /* we raced with the unblock's flush */
535                 spin_unlock_irqrestore(&session->lock, flags);
536                 return;
537         }
538         spin_unlock_irqrestore(&session->lock, flags);
539
540         if (session->transport->session_recovery_timedout)
541                 session->transport->session_recovery_timedout(session);
542
543         scsi_target_unblock(&session->dev);
544 }
545
546 static void __iscsi_unblock_session(struct work_struct *work)
547 {
548         struct iscsi_cls_session *session =
549                         container_of(work, struct iscsi_cls_session,
550                                      unblock_work);
551         struct Scsi_Host *shost = iscsi_session_to_shost(session);
552         struct iscsi_cls_host *ihost = shost->shost_data;
553         unsigned long flags;
554
555         /*
556          * The recovery and unblock work get run from the same workqueue,
557          * so try to cancel it if it was going to run after this unblock.
558          */
559         cancel_delayed_work(&session->recovery_work);
560         spin_lock_irqsave(&session->lock, flags);
561         session->state = ISCSI_SESSION_LOGGED_IN;
562         spin_unlock_irqrestore(&session->lock, flags);
563         /* start IO */
564         scsi_target_unblock(&session->dev);
565         /*
566          * Only do kernel scanning if the driver is properly hooked into
567          * the async scanning code (drivers like iscsi_tcp do login and
568          * scanning from userspace).
569          */
570         if (shost->hostt->scan_finished) {
571                 if (queue_work(ihost->scan_workq, &session->scan_work))
572                         atomic_inc(&ihost->nr_scans);
573         }
574 }
575
576 /**
577  * iscsi_unblock_session - set a session as logged in and start IO.
578  * @session: iscsi session
579  *
580  * Mark a session as ready to accept IO.
581  */
582 void iscsi_unblock_session(struct iscsi_cls_session *session)
583 {
584         queue_work(iscsi_eh_timer_workq, &session->unblock_work);
585         /*
586          * make sure all the events have completed before tell the driver
587          * it is safe
588          */
589         flush_workqueue(iscsi_eh_timer_workq);
590 }
591 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
592
593 static void __iscsi_block_session(struct work_struct *work)
594 {
595         struct iscsi_cls_session *session =
596                         container_of(work, struct iscsi_cls_session,
597                                      block_work);
598         unsigned long flags;
599
600         spin_lock_irqsave(&session->lock, flags);
601         session->state = ISCSI_SESSION_FAILED;
602         spin_unlock_irqrestore(&session->lock, flags);
603         scsi_target_block(&session->dev);
604         queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
605                            session->recovery_tmo * HZ);
606 }
607
608 void iscsi_block_session(struct iscsi_cls_session *session)
609 {
610         queue_work(iscsi_eh_timer_workq, &session->block_work);
611 }
612 EXPORT_SYMBOL_GPL(iscsi_block_session);
613
614 static void __iscsi_unbind_session(struct work_struct *work)
615 {
616         struct iscsi_cls_session *session =
617                         container_of(work, struct iscsi_cls_session,
618                                      unbind_work);
619         struct Scsi_Host *shost = iscsi_session_to_shost(session);
620         struct iscsi_cls_host *ihost = shost->shost_data;
621         unsigned long flags;
622
623         /* Prevent new scans and make sure scanning is not in progress */
624         mutex_lock(&ihost->mutex);
625         spin_lock_irqsave(&session->lock, flags);
626         if (session->target_id == ISCSI_MAX_TARGET) {
627                 spin_unlock_irqrestore(&session->lock, flags);
628                 mutex_unlock(&ihost->mutex);
629                 return;
630         }
631         session->target_id = ISCSI_MAX_TARGET;
632         spin_unlock_irqrestore(&session->lock, flags);
633         mutex_unlock(&ihost->mutex);
634
635         scsi_remove_target(&session->dev);
636         iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
637 }
638
639 static int iscsi_unbind_session(struct iscsi_cls_session *session)
640 {
641         struct Scsi_Host *shost = iscsi_session_to_shost(session);
642         struct iscsi_cls_host *ihost = shost->shost_data;
643
644         return queue_work(ihost->scan_workq, &session->unbind_work);
645 }
646
647 struct iscsi_cls_session *
648 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
649                     int dd_size)
650 {
651         struct iscsi_cls_session *session;
652
653         session = kzalloc(sizeof(*session) + dd_size,
654                           GFP_KERNEL);
655         if (!session)
656                 return NULL;
657
658         session->transport = transport;
659         session->recovery_tmo = 120;
660         session->state = ISCSI_SESSION_FREE;
661         INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
662         INIT_LIST_HEAD(&session->sess_list);
663         INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
664         INIT_WORK(&session->block_work, __iscsi_block_session);
665         INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
666         INIT_WORK(&session->scan_work, iscsi_scan_session);
667         spin_lock_init(&session->lock);
668
669         /* this is released in the dev's release function */
670         scsi_host_get(shost);
671         session->dev.parent = &shost->shost_gendev;
672         session->dev.release = iscsi_session_release;
673         device_initialize(&session->dev);
674         if (dd_size)
675                 session->dd_data = &session[1];
676         return session;
677 }
678 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
679
680 static int iscsi_get_next_target_id(struct device *dev, void *data)
681 {
682         struct iscsi_cls_session *session;
683         unsigned long flags;
684         int err = 0;
685
686         if (!iscsi_is_session_dev(dev))
687                 return 0;
688
689         session = iscsi_dev_to_session(dev);
690         spin_lock_irqsave(&session->lock, flags);
691         if (*((unsigned int *) data) == session->target_id)
692                 err = -EEXIST;
693         spin_unlock_irqrestore(&session->lock, flags);
694         return err;
695 }
696
697 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
698 {
699         struct Scsi_Host *shost = iscsi_session_to_shost(session);
700         struct iscsi_cls_host *ihost;
701         unsigned long flags;
702         unsigned int id = target_id;
703         int err;
704
705         ihost = shost->shost_data;
706         session->sid = atomic_add_return(1, &iscsi_session_nr);
707
708         if (id == ISCSI_MAX_TARGET) {
709                 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
710                         err = device_for_each_child(&shost->shost_gendev, &id,
711                                                     iscsi_get_next_target_id);
712                         if (!err)
713                                 break;
714                 }
715
716                 if (id == ISCSI_MAX_TARGET) {
717                         iscsi_cls_session_printk(KERN_ERR, session,
718                                                  "Too many iscsi targets. Max "
719                                                  "number of targets is %d.\n",
720                                                  ISCSI_MAX_TARGET - 1);
721                         goto release_host;
722                 }
723         }
724         session->target_id = id;
725
726         dev_set_name(&session->dev, "session%u", session->sid);
727         err = device_add(&session->dev);
728         if (err) {
729                 iscsi_cls_session_printk(KERN_ERR, session,
730                                          "could not register session's dev\n");
731                 goto release_host;
732         }
733         transport_register_device(&session->dev);
734
735         spin_lock_irqsave(&sesslock, flags);
736         list_add(&session->sess_list, &sesslist);
737         spin_unlock_irqrestore(&sesslock, flags);
738
739         iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
740         return 0;
741
742 release_host:
743         scsi_host_put(shost);
744         return err;
745 }
746 EXPORT_SYMBOL_GPL(iscsi_add_session);
747
748 /**
749  * iscsi_create_session - create iscsi class session
750  * @shost: scsi host
751  * @transport: iscsi transport
752  * @dd_size: private driver data size
753  * @target_id: which target
754  *
755  * This can be called from a LLD or iscsi_transport.
756  */
757 struct iscsi_cls_session *
758 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
759                      int dd_size, unsigned int target_id)
760 {
761         struct iscsi_cls_session *session;
762
763         session = iscsi_alloc_session(shost, transport, dd_size);
764         if (!session)
765                 return NULL;
766
767         if (iscsi_add_session(session, target_id)) {
768                 iscsi_free_session(session);
769                 return NULL;
770         }
771         return session;
772 }
773 EXPORT_SYMBOL_GPL(iscsi_create_session);
774
775 static void iscsi_conn_release(struct device *dev)
776 {
777         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
778         struct device *parent = conn->dev.parent;
779
780         kfree(conn);
781         put_device(parent);
782 }
783
784 static int iscsi_is_conn_dev(const struct device *dev)
785 {
786         return dev->release == iscsi_conn_release;
787 }
788
789 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
790 {
791         if (!iscsi_is_conn_dev(dev))
792                 return 0;
793         return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
794 }
795
796 void iscsi_remove_session(struct iscsi_cls_session *session)
797 {
798         struct Scsi_Host *shost = iscsi_session_to_shost(session);
799         struct iscsi_cls_host *ihost = shost->shost_data;
800         unsigned long flags;
801         int err;
802
803         spin_lock_irqsave(&sesslock, flags);
804         list_del(&session->sess_list);
805         spin_unlock_irqrestore(&sesslock, flags);
806
807         /* make sure there are no blocks/unblocks queued */
808         flush_workqueue(iscsi_eh_timer_workq);
809         /* make sure the timedout callout is not running */
810         if (!cancel_delayed_work(&session->recovery_work))
811                 flush_workqueue(iscsi_eh_timer_workq);
812         /*
813          * If we are blocked let commands flow again. The lld or iscsi
814          * layer should set up the queuecommand to fail commands.
815          * We assume that LLD will not be calling block/unblock while
816          * removing the session.
817          */
818         spin_lock_irqsave(&session->lock, flags);
819         session->state = ISCSI_SESSION_FREE;
820         spin_unlock_irqrestore(&session->lock, flags);
821
822         scsi_target_unblock(&session->dev);
823         /* flush running scans then delete devices */
824         flush_workqueue(ihost->scan_workq);
825         __iscsi_unbind_session(&session->unbind_work);
826
827         /* hw iscsi may not have removed all connections from session */
828         err = device_for_each_child(&session->dev, NULL,
829                                     iscsi_iter_destroy_conn_fn);
830         if (err)
831                 iscsi_cls_session_printk(KERN_ERR, session,
832                                          "Could not delete all connections "
833                                          "for session. Error %d.\n", err);
834
835         transport_unregister_device(&session->dev);
836         device_del(&session->dev);
837 }
838 EXPORT_SYMBOL_GPL(iscsi_remove_session);
839
840 void iscsi_free_session(struct iscsi_cls_session *session)
841 {
842         iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
843         put_device(&session->dev);
844 }
845 EXPORT_SYMBOL_GPL(iscsi_free_session);
846
847 /**
848  * iscsi_destroy_session - destroy iscsi session
849  * @session: iscsi_session
850  *
851  * Can be called by a LLD or iscsi_transport. There must not be
852  * any running connections.
853  */
854 int iscsi_destroy_session(struct iscsi_cls_session *session)
855 {
856         iscsi_remove_session(session);
857         iscsi_free_session(session);
858         return 0;
859 }
860 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
861
862 /**
863  * iscsi_create_conn - create iscsi class connection
864  * @session: iscsi cls session
865  * @dd_size: private driver data size
866  * @cid: connection id
867  *
868  * This can be called from a LLD or iscsi_transport. The connection
869  * is child of the session so cid must be unique for all connections
870  * on the session.
871  *
872  * Since we do not support MCS, cid will normally be zero. In some cases
873  * for software iscsi we could be trying to preallocate a connection struct
874  * in which case there could be two connection structs and cid would be
875  * non-zero.
876  */
877 struct iscsi_cls_conn *
878 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
879 {
880         struct iscsi_transport *transport = session->transport;
881         struct iscsi_cls_conn *conn;
882         unsigned long flags;
883         int err;
884
885         conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
886         if (!conn)
887                 return NULL;
888         if (dd_size)
889                 conn->dd_data = &conn[1];
890
891         INIT_LIST_HEAD(&conn->conn_list);
892         conn->transport = transport;
893         conn->cid = cid;
894
895         /* this is released in the dev's release function */
896         if (!get_device(&session->dev))
897                 goto free_conn;
898
899         dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
900         conn->dev.parent = &session->dev;
901         conn->dev.release = iscsi_conn_release;
902         err = device_register(&conn->dev);
903         if (err) {
904                 iscsi_cls_session_printk(KERN_ERR, session, "could not "
905                                          "register connection's dev\n");
906                 goto release_parent_ref;
907         }
908         transport_register_device(&conn->dev);
909
910         spin_lock_irqsave(&connlock, flags);
911         list_add(&conn->conn_list, &connlist);
912         conn->active = 1;
913         spin_unlock_irqrestore(&connlock, flags);
914         return conn;
915
916 release_parent_ref:
917         put_device(&session->dev);
918 free_conn:
919         kfree(conn);
920         return NULL;
921 }
922
923 EXPORT_SYMBOL_GPL(iscsi_create_conn);
924
925 /**
926  * iscsi_destroy_conn - destroy iscsi class connection
927  * @conn: iscsi cls session
928  *
929  * This can be called from a LLD or iscsi_transport.
930  */
931 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
932 {
933         unsigned long flags;
934
935         spin_lock_irqsave(&connlock, flags);
936         conn->active = 0;
937         list_del(&conn->conn_list);
938         spin_unlock_irqrestore(&connlock, flags);
939
940         transport_unregister_device(&conn->dev);
941         device_unregister(&conn->dev);
942         return 0;
943 }
944 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
945
946 /*
947  * iscsi interface functions
948  */
949 static struct iscsi_internal *
950 iscsi_if_transport_lookup(struct iscsi_transport *tt)
951 {
952         struct iscsi_internal *priv;
953         unsigned long flags;
954
955         spin_lock_irqsave(&iscsi_transport_lock, flags);
956         list_for_each_entry(priv, &iscsi_transports, list) {
957                 if (tt == priv->iscsi_transport) {
958                         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
959                         return priv;
960                 }
961         }
962         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
963         return NULL;
964 }
965
966 static int
967 iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
968 {
969         int rc;
970
971         rc = netlink_broadcast(nls, skb, 0, 1, gfp);
972         if (rc < 0) {
973                 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
974                 return rc;
975         }
976
977         return 0;
978 }
979
980 static int
981 iscsi_unicast_skb(struct sk_buff *skb, int pid)
982 {
983         int rc;
984
985         rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
986         if (rc < 0) {
987                 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
988                 return rc;
989         }
990
991         return 0;
992 }
993
994 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
995                    char *data, uint32_t data_size)
996 {
997         struct nlmsghdr *nlh;
998         struct sk_buff *skb;
999         struct iscsi_uevent *ev;
1000         char *pdu;
1001         struct iscsi_internal *priv;
1002         int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1003                               data_size);
1004
1005         priv = iscsi_if_transport_lookup(conn->transport);
1006         if (!priv)
1007                 return -EINVAL;
1008
1009         skb = alloc_skb(len, GFP_ATOMIC);
1010         if (!skb) {
1011                 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
1012                 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1013                                       "control PDU: OOM\n");
1014                 return -ENOMEM;
1015         }
1016
1017         nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1018         ev = NLMSG_DATA(nlh);
1019         memset(ev, 0, sizeof(*ev));
1020         ev->transport_handle = iscsi_handle(conn->transport);
1021         ev->type = ISCSI_KEVENT_RECV_PDU;
1022         ev->r.recv_req.cid = conn->cid;
1023         ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
1024         pdu = (char*)ev + sizeof(*ev);
1025         memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1026         memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1027
1028         return iscsi_unicast_skb(skb, priv->daemon_pid);
1029 }
1030 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1031
1032 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
1033 {
1034         struct nlmsghdr *nlh;
1035         struct sk_buff  *skb;
1036         struct iscsi_uevent *ev;
1037         struct iscsi_internal *priv;
1038         int len = NLMSG_SPACE(sizeof(*ev));
1039
1040         priv = iscsi_if_transport_lookup(conn->transport);
1041         if (!priv)
1042                 return;
1043
1044         skb = alloc_skb(len, GFP_ATOMIC);
1045         if (!skb) {
1046                 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1047                                       "conn error (%d)\n", error);
1048                 return;
1049         }
1050
1051         nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1052         ev = NLMSG_DATA(nlh);
1053         ev->transport_handle = iscsi_handle(conn->transport);
1054         ev->type = ISCSI_KEVENT_CONN_ERROR;
1055         ev->r.connerror.error = error;
1056         ev->r.connerror.cid = conn->cid;
1057         ev->r.connerror.sid = iscsi_conn_get_sid(conn);
1058
1059         iscsi_broadcast_skb(skb, GFP_ATOMIC);
1060
1061         iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1062                               error);
1063 }
1064 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
1065
1066 static int
1067 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
1068                       void *payload, int size)
1069 {
1070         struct sk_buff  *skb;
1071         struct nlmsghdr *nlh;
1072         int len = NLMSG_SPACE(size);
1073         int flags = multi ? NLM_F_MULTI : 0;
1074         int t = done ? NLMSG_DONE : type;
1075
1076         skb = alloc_skb(len, GFP_ATOMIC);
1077         if (!skb) {
1078                 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1079                 return -ENOMEM;
1080         }
1081
1082         nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
1083         nlh->nlmsg_flags = flags;
1084         memcpy(NLMSG_DATA(nlh), payload, size);
1085         return iscsi_unicast_skb(skb, pid);
1086 }
1087
1088 static int
1089 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
1090 {
1091         struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1092         struct iscsi_stats *stats;
1093         struct sk_buff *skbstat;
1094         struct iscsi_cls_conn *conn;
1095         struct nlmsghdr *nlhstat;
1096         struct iscsi_uevent *evstat;
1097         struct iscsi_internal *priv;
1098         int len = NLMSG_SPACE(sizeof(*ev) +
1099                               sizeof(struct iscsi_stats) +
1100                               sizeof(struct iscsi_stats_custom) *
1101                               ISCSI_STATS_CUSTOM_MAX);
1102         int err = 0;
1103
1104         priv = iscsi_if_transport_lookup(transport);
1105         if (!priv)
1106                 return -EINVAL;
1107
1108         conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
1109         if (!conn)
1110                 return -EEXIST;
1111
1112         do {
1113                 int actual_size;
1114
1115                 skbstat = alloc_skb(len, GFP_ATOMIC);
1116                 if (!skbstat) {
1117                         iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1118                                               "deliver stats: OOM\n");
1119                         return -ENOMEM;
1120                 }
1121
1122                 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
1123                                       (len - sizeof(*nlhstat)), 0);
1124                 evstat = NLMSG_DATA(nlhstat);
1125                 memset(evstat, 0, sizeof(*evstat));
1126                 evstat->transport_handle = iscsi_handle(conn->transport);
1127                 evstat->type = nlh->nlmsg_type;
1128                 evstat->u.get_stats.cid =
1129                         ev->u.get_stats.cid;
1130                 evstat->u.get_stats.sid =
1131                         ev->u.get_stats.sid;
1132                 stats = (struct iscsi_stats *)
1133                         ((char*)evstat + sizeof(*evstat));
1134                 memset(stats, 0, sizeof(*stats));
1135
1136                 transport->get_stats(conn, stats);
1137                 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1138                                           sizeof(struct iscsi_stats) +
1139                                           sizeof(struct iscsi_stats_custom) *
1140                                           stats->custom_length);
1141                 actual_size -= sizeof(*nlhstat);
1142                 actual_size = NLMSG_LENGTH(actual_size);
1143                 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1144                 nlhstat->nlmsg_len = actual_size;
1145
1146                 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
1147         } while (err < 0 && err != -ECONNREFUSED);
1148
1149         return err;
1150 }
1151
1152 /**
1153  * iscsi_session_event - send session destr. completion event
1154  * @session: iscsi class session
1155  * @event: type of event
1156  */
1157 int iscsi_session_event(struct iscsi_cls_session *session,
1158                         enum iscsi_uevent_e event)
1159 {
1160         struct iscsi_internal *priv;
1161         struct Scsi_Host *shost;
1162         struct iscsi_uevent *ev;
1163         struct sk_buff  *skb;
1164         struct nlmsghdr *nlh;
1165         int rc, len = NLMSG_SPACE(sizeof(*ev));
1166
1167         priv = iscsi_if_transport_lookup(session->transport);
1168         if (!priv)
1169                 return -EINVAL;
1170         shost = iscsi_session_to_shost(session);
1171
1172         skb = alloc_skb(len, GFP_KERNEL);
1173         if (!skb) {
1174                 iscsi_cls_session_printk(KERN_ERR, session,
1175                                          "Cannot notify userspace of session "
1176                                          "event %u\n", event);
1177                 return -ENOMEM;
1178         }
1179
1180         nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1181         ev = NLMSG_DATA(nlh);
1182         ev->transport_handle = iscsi_handle(session->transport);
1183
1184         ev->type = event;
1185         switch (event) {
1186         case ISCSI_KEVENT_DESTROY_SESSION:
1187                 ev->r.d_session.host_no = shost->host_no;
1188                 ev->r.d_session.sid = session->sid;
1189                 break;
1190         case ISCSI_KEVENT_CREATE_SESSION:
1191                 ev->r.c_session_ret.host_no = shost->host_no;
1192                 ev->r.c_session_ret.sid = session->sid;
1193                 break;
1194         case ISCSI_KEVENT_UNBIND_SESSION:
1195                 ev->r.unbind_session.host_no = shost->host_no;
1196                 ev->r.unbind_session.sid = session->sid;
1197                 break;
1198         default:
1199                 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1200                                          "%u.\n", event);
1201                 kfree_skb(skb);
1202                 return -EINVAL;
1203         }
1204
1205         /*
1206          * this will occur if the daemon is not up, so we just warn
1207          * the user and when the daemon is restarted it will handle it
1208          */
1209         rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
1210         if (rc < 0)
1211                 iscsi_cls_session_printk(KERN_ERR, session,
1212                                          "Cannot notify userspace of session "
1213                                          "event %u. Check iscsi daemon\n",
1214                                          event);
1215         return rc;
1216 }
1217 EXPORT_SYMBOL_GPL(iscsi_session_event);
1218
1219 static int
1220 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1221                         struct iscsi_uevent *ev, uint32_t initial_cmdsn,
1222                         uint16_t cmds_max, uint16_t queue_depth)
1223 {
1224         struct iscsi_transport *transport = priv->iscsi_transport;
1225         struct iscsi_cls_session *session;
1226         uint32_t host_no;
1227
1228         session = transport->create_session(ep, cmds_max, queue_depth,
1229                                             initial_cmdsn, &host_no);
1230         if (!session)
1231                 return -ENOMEM;
1232
1233         ev->r.c_session_ret.host_no = host_no;
1234         ev->r.c_session_ret.sid = session->sid;
1235         return 0;
1236 }
1237
1238 static int
1239 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1240 {
1241         struct iscsi_cls_conn *conn;
1242         struct iscsi_cls_session *session;
1243
1244         session = iscsi_session_lookup(ev->u.c_conn.sid);
1245         if (!session) {
1246                 printk(KERN_ERR "iscsi: invalid session %d.\n",
1247                        ev->u.c_conn.sid);
1248                 return -EINVAL;
1249         }
1250
1251         conn = transport->create_conn(session, ev->u.c_conn.cid);
1252         if (!conn) {
1253                 iscsi_cls_session_printk(KERN_ERR, session,
1254                                          "couldn't create a new connection.");
1255                 return -ENOMEM;
1256         }
1257
1258         ev->r.c_conn_ret.sid = session->sid;
1259         ev->r.c_conn_ret.cid = conn->cid;
1260         return 0;
1261 }
1262
1263 static int
1264 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1265 {
1266         struct iscsi_cls_conn *conn;
1267
1268         conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1269         if (!conn)
1270                 return -EINVAL;
1271
1272         if (transport->destroy_conn)
1273                 transport->destroy_conn(conn);
1274         return 0;
1275 }
1276
1277 static int
1278 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1279 {
1280         char *data = (char*)ev + sizeof(*ev);
1281         struct iscsi_cls_conn *conn;
1282         struct iscsi_cls_session *session;
1283         int err = 0, value = 0;
1284
1285         session = iscsi_session_lookup(ev->u.set_param.sid);
1286         conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1287         if (!conn || !session)
1288                 return -EINVAL;
1289
1290         switch (ev->u.set_param.param) {
1291         case ISCSI_PARAM_SESS_RECOVERY_TMO:
1292                 sscanf(data, "%d", &value);
1293                 if (value != 0)
1294                         session->recovery_tmo = value;
1295                 break;
1296         default:
1297                 err = transport->set_param(conn, ev->u.set_param.param,
1298                                            data, ev->u.set_param.len);
1299         }
1300
1301         return err;
1302 }
1303
1304 static int
1305 iscsi_if_transport_ep(struct iscsi_transport *transport,
1306                       struct iscsi_uevent *ev, int msg_type)
1307 {
1308         struct iscsi_endpoint *ep;
1309         struct sockaddr *dst_addr;
1310         int rc = 0;
1311
1312         switch (msg_type) {
1313         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1314                 if (!transport->ep_connect)
1315                         return -EINVAL;
1316
1317                 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1318                 ep = transport->ep_connect(dst_addr,
1319                                            ev->u.ep_connect.non_blocking);
1320                 if (IS_ERR(ep))
1321                         return PTR_ERR(ep);
1322
1323                 ev->r.ep_connect_ret.handle = ep->id;
1324                 break;
1325         case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1326                 if (!transport->ep_poll)
1327                         return -EINVAL;
1328
1329                 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1330                 if (!ep)
1331                         return -EINVAL;
1332
1333                 ev->r.retcode = transport->ep_poll(ep,
1334                                                    ev->u.ep_poll.timeout_ms);
1335                 break;
1336         case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1337                 if (!transport->ep_disconnect)
1338                         return -EINVAL;
1339
1340                 ep = iscsi_lookup_endpoint(ev->u.ep_disconnect.ep_handle);
1341                 if (!ep)
1342                         return -EINVAL;
1343
1344                 transport->ep_disconnect(ep);
1345                 break;
1346         }
1347         return rc;
1348 }
1349
1350 static int
1351 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1352                 struct iscsi_uevent *ev)
1353 {
1354         struct Scsi_Host *shost;
1355         struct sockaddr *dst_addr;
1356         int err;
1357
1358         if (!transport->tgt_dscvr)
1359                 return -EINVAL;
1360
1361         shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1362         if (!shost) {
1363                 printk(KERN_ERR "target discovery could not find host no %u\n",
1364                        ev->u.tgt_dscvr.host_no);
1365                 return -ENODEV;
1366         }
1367
1368
1369         dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1370         err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1371                                    ev->u.tgt_dscvr.enable, dst_addr);
1372         scsi_host_put(shost);
1373         return err;
1374 }
1375
1376 static int
1377 iscsi_set_host_param(struct iscsi_transport *transport,
1378                      struct iscsi_uevent *ev)
1379 {
1380         char *data = (char*)ev + sizeof(*ev);
1381         struct Scsi_Host *shost;
1382         int err;
1383
1384         if (!transport->set_host_param)
1385                 return -ENOSYS;
1386
1387         shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1388         if (!shost) {
1389                 printk(KERN_ERR "set_host_param could not find host no %u\n",
1390                        ev->u.set_host_param.host_no);
1391                 return -ENODEV;
1392         }
1393
1394         err = transport->set_host_param(shost, ev->u.set_host_param.param,
1395                                         data, ev->u.set_host_param.len);
1396         scsi_host_put(shost);
1397         return err;
1398 }
1399
1400 static int
1401 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1402 {
1403         int err = 0;
1404         struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1405         struct iscsi_transport *transport = NULL;
1406         struct iscsi_internal *priv;
1407         struct iscsi_cls_session *session;
1408         struct iscsi_cls_conn *conn;
1409         struct iscsi_endpoint *ep = NULL;
1410
1411         priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1412         if (!priv)
1413                 return -EINVAL;
1414         transport = priv->iscsi_transport;
1415
1416         if (!try_module_get(transport->owner))
1417                 return -EINVAL;
1418
1419         priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1420
1421         switch (nlh->nlmsg_type) {
1422         case ISCSI_UEVENT_CREATE_SESSION:
1423                 err = iscsi_if_create_session(priv, ep, ev,
1424                                               ev->u.c_session.initial_cmdsn,
1425                                               ev->u.c_session.cmds_max,
1426                                               ev->u.c_session.queue_depth);
1427                 break;
1428         case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1429                 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
1430                 if (!ep) {
1431                         err = -EINVAL;
1432                         break;
1433                 }
1434
1435                 err = iscsi_if_create_session(priv, ep, ev,
1436                                         ev->u.c_bound_session.initial_cmdsn,
1437                                         ev->u.c_bound_session.cmds_max,
1438                                         ev->u.c_bound_session.queue_depth);
1439                 break;
1440         case ISCSI_UEVENT_DESTROY_SESSION:
1441                 session = iscsi_session_lookup(ev->u.d_session.sid);
1442                 if (session)
1443                         transport->destroy_session(session);
1444                 else
1445                         err = -EINVAL;
1446                 break;
1447         case ISCSI_UEVENT_UNBIND_SESSION:
1448                 session = iscsi_session_lookup(ev->u.d_session.sid);
1449                 if (session)
1450                         iscsi_unbind_session(session);
1451                 else
1452                         err = -EINVAL;
1453                 break;
1454         case ISCSI_UEVENT_CREATE_CONN:
1455                 err = iscsi_if_create_conn(transport, ev);
1456                 break;
1457         case ISCSI_UEVENT_DESTROY_CONN:
1458                 err = iscsi_if_destroy_conn(transport, ev);
1459                 break;
1460         case ISCSI_UEVENT_BIND_CONN:
1461                 session = iscsi_session_lookup(ev->u.b_conn.sid);
1462                 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1463
1464                 if (session && conn)
1465                         ev->r.retcode = transport->bind_conn(session, conn,
1466                                         ev->u.b_conn.transport_eph,
1467                                         ev->u.b_conn.is_leading);
1468                 else
1469                         err = -EINVAL;
1470                 break;
1471         case ISCSI_UEVENT_SET_PARAM:
1472                 err = iscsi_set_param(transport, ev);
1473                 break;
1474         case ISCSI_UEVENT_START_CONN:
1475                 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1476                 if (conn)
1477                         ev->r.retcode = transport->start_conn(conn);
1478                 else
1479                         err = -EINVAL;
1480                 break;
1481         case ISCSI_UEVENT_STOP_CONN:
1482                 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1483                 if (conn)
1484                         transport->stop_conn(conn, ev->u.stop_conn.flag);
1485                 else
1486                         err = -EINVAL;
1487                 break;
1488         case ISCSI_UEVENT_SEND_PDU:
1489                 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1490                 if (conn)
1491                         ev->r.retcode = transport->send_pdu(conn,
1492                                 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1493                                 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1494                                 ev->u.send_pdu.data_size);
1495                 else
1496                         err = -EINVAL;
1497                 break;
1498         case ISCSI_UEVENT_GET_STATS:
1499                 err = iscsi_if_get_stats(transport, nlh);
1500                 break;
1501         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1502         case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1503         case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1504                 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1505                 break;
1506         case ISCSI_UEVENT_TGT_DSCVR:
1507                 err = iscsi_tgt_dscvr(transport, ev);
1508                 break;
1509         case ISCSI_UEVENT_SET_HOST_PARAM:
1510                 err = iscsi_set_host_param(transport, ev);
1511                 break;
1512         default:
1513                 err = -ENOSYS;
1514                 break;
1515         }
1516
1517         module_put(transport->owner);
1518         return err;
1519 }
1520
1521 /*
1522  * Get message from skb.  Each message is processed by iscsi_if_recv_msg.
1523  * Malformed skbs with wrong lengths or invalid creds are not processed.
1524  */
1525 static void
1526 iscsi_if_rx(struct sk_buff *skb)
1527 {
1528         mutex_lock(&rx_queue_mutex);
1529         while (skb->len >= NLMSG_SPACE(0)) {
1530                 int err;
1531                 uint32_t rlen;
1532                 struct nlmsghdr *nlh;
1533                 struct iscsi_uevent *ev;
1534
1535                 nlh = nlmsg_hdr(skb);
1536                 if (nlh->nlmsg_len < sizeof(*nlh) ||
1537                     skb->len < nlh->nlmsg_len) {
1538                         break;
1539                 }
1540
1541                 ev = NLMSG_DATA(nlh);
1542                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1543                 if (rlen > skb->len)
1544                         rlen = skb->len;
1545
1546                 err = iscsi_if_recv_msg(skb, nlh);
1547                 if (err) {
1548                         ev->type = ISCSI_KEVENT_IF_ERROR;
1549                         ev->iferror = err;
1550                 }
1551                 do {
1552                         /*
1553                          * special case for GET_STATS:
1554                          * on success - sending reply and stats from
1555                          * inside of if_recv_msg(),
1556                          * on error - fall through.
1557                          */
1558                         if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1559                                 break;
1560                         err = iscsi_if_send_reply(
1561                                 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1562                                 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1563                 } while (err < 0 && err != -ECONNREFUSED);
1564                 skb_pull(skb, rlen);
1565         }
1566         mutex_unlock(&rx_queue_mutex);
1567 }
1568
1569 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store)              \
1570 struct device_attribute dev_attr_##_prefix##_##_name =  \
1571         __ATTR(_name,_mode,_show,_store)
1572
1573 /*
1574  * iSCSI connection attrs
1575  */
1576 #define iscsi_conn_attr_show(param)                                     \
1577 static ssize_t                                                          \
1578 show_conn_param_##param(struct device *dev,                             \
1579                         struct device_attribute *attr, char *buf)       \
1580 {                                                                       \
1581         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);   \
1582         struct iscsi_transport *t = conn->transport;                    \
1583         return t->get_conn_param(conn, param, buf);                     \
1584 }
1585
1586 #define iscsi_conn_attr(field, param)                                   \
1587         iscsi_conn_attr_show(param)                                     \
1588 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param,  \
1589                         NULL);
1590
1591 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1592 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1593 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1594 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1595 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1596 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1597 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1598 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1599 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1600 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1601 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1602 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1603 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1604
1605 /*
1606  * iSCSI session attrs
1607  */
1608 #define iscsi_session_attr_show(param, perm)                            \
1609 static ssize_t                                                          \
1610 show_session_param_##param(struct device *dev,                          \
1611                            struct device_attribute *attr, char *buf)    \
1612 {                                                                       \
1613         struct iscsi_cls_session *session =                             \
1614                 iscsi_dev_to_session(dev->parent);                      \
1615         struct iscsi_transport *t = session->transport;                 \
1616                                                                         \
1617         if (perm && !capable(CAP_SYS_ADMIN))                            \
1618                 return -EACCES;                                         \
1619         return t->get_session_param(session, param, buf);               \
1620 }
1621
1622 #define iscsi_session_attr(field, param, perm)                          \
1623         iscsi_session_attr_show(param, perm)                            \
1624 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1625                         NULL);
1626
1627 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1628 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1629 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1630 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1631 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1632 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1633 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1634 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1635 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1636 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1637 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1638 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1639 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1640 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
1641 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1642 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1643 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
1644 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
1645 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0)
1646
1647 static ssize_t
1648 show_priv_session_state(struct device *dev, struct device_attribute *attr,
1649                         char *buf)
1650 {
1651         struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
1652         return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1653 }
1654 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1655                         NULL);
1656
1657 #define iscsi_priv_session_attr_show(field, format)                     \
1658 static ssize_t                                                          \
1659 show_priv_session_##field(struct device *dev,                           \
1660                           struct device_attribute *attr, char *buf)     \
1661 {                                                                       \
1662         struct iscsi_cls_session *session =                             \
1663                         iscsi_dev_to_session(dev->parent);              \
1664         return sprintf(buf, format"\n", session->field);                \
1665 }
1666
1667 #define iscsi_priv_session_attr(field, format)                          \
1668         iscsi_priv_session_attr_show(field, format)                     \
1669 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1670                         NULL)
1671 iscsi_priv_session_attr(recovery_tmo, "%d");
1672
1673 /*
1674  * iSCSI host attrs
1675  */
1676 #define iscsi_host_attr_show(param)                                     \
1677 static ssize_t                                                          \
1678 show_host_param_##param(struct device *dev,                             \
1679                         struct device_attribute *attr, char *buf)       \
1680 {                                                                       \
1681         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
1682         struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1683         return priv->iscsi_transport->get_host_param(shost, param, buf); \
1684 }
1685
1686 #define iscsi_host_attr(field, param)                                   \
1687         iscsi_host_attr_show(param)                                     \
1688 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param,  \
1689                         NULL);
1690
1691 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
1692 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
1693 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
1694 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
1695
1696 #define SETUP_PRIV_SESSION_RD_ATTR(field)                               \
1697 do {                                                                    \
1698         priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
1699         count++;                                                        \
1700 } while (0)
1701
1702
1703 #define SETUP_SESSION_RD_ATTR(field, param_flag)                        \
1704 do {                                                                    \
1705         if (tt->param_mask & param_flag) {                              \
1706                 priv->session_attrs[count] = &dev_attr_sess_##field; \
1707                 count++;                                                \
1708         }                                                               \
1709 } while (0)
1710
1711 #define SETUP_CONN_RD_ATTR(field, param_flag)                           \
1712 do {                                                                    \
1713         if (tt->param_mask & param_flag) {                              \
1714                 priv->conn_attrs[count] = &dev_attr_conn_##field; \
1715                 count++;                                                \
1716         }                                                               \
1717 } while (0)
1718
1719 #define SETUP_HOST_RD_ATTR(field, param_flag)                           \
1720 do {                                                                    \
1721         if (tt->host_param_mask & param_flag) {                         \
1722                 priv->host_attrs[count] = &dev_attr_host_##field; \
1723                 count++;                                                \
1724         }                                                               \
1725 } while (0)
1726
1727 static int iscsi_session_match(struct attribute_container *cont,
1728                            struct device *dev)
1729 {
1730         struct iscsi_cls_session *session;
1731         struct Scsi_Host *shost;
1732         struct iscsi_internal *priv;
1733
1734         if (!iscsi_is_session_dev(dev))
1735                 return 0;
1736
1737         session = iscsi_dev_to_session(dev);
1738         shost = iscsi_session_to_shost(session);
1739         if (!shost->transportt)
1740                 return 0;
1741
1742         priv = to_iscsi_internal(shost->transportt);
1743         if (priv->session_cont.ac.class != &iscsi_session_class.class)
1744                 return 0;
1745
1746         return &priv->session_cont.ac == cont;
1747 }
1748
1749 static int iscsi_conn_match(struct attribute_container *cont,
1750                            struct device *dev)
1751 {
1752         struct iscsi_cls_session *session;
1753         struct iscsi_cls_conn *conn;
1754         struct Scsi_Host *shost;
1755         struct iscsi_internal *priv;
1756
1757         if (!iscsi_is_conn_dev(dev))
1758                 return 0;
1759
1760         conn = iscsi_dev_to_conn(dev);
1761         session = iscsi_dev_to_session(conn->dev.parent);
1762         shost = iscsi_session_to_shost(session);
1763
1764         if (!shost->transportt)
1765                 return 0;
1766
1767         priv = to_iscsi_internal(shost->transportt);
1768         if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1769                 return 0;
1770
1771         return &priv->conn_cont.ac == cont;
1772 }
1773
1774 static int iscsi_host_match(struct attribute_container *cont,
1775                             struct device *dev)
1776 {
1777         struct Scsi_Host *shost;
1778         struct iscsi_internal *priv;
1779
1780         if (!scsi_is_host_device(dev))
1781                 return 0;
1782
1783         shost = dev_to_shost(dev);
1784         if (!shost->transportt  ||
1785             shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1786                 return 0;
1787
1788         priv = to_iscsi_internal(shost->transportt);
1789         return &priv->t.host_attrs.ac == cont;
1790 }
1791
1792 struct scsi_transport_template *
1793 iscsi_register_transport(struct iscsi_transport *tt)
1794 {
1795         struct iscsi_internal *priv;
1796         unsigned long flags;
1797         int count = 0, err;
1798
1799         BUG_ON(!tt);
1800
1801         priv = iscsi_if_transport_lookup(tt);
1802         if (priv)
1803                 return NULL;
1804
1805         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1806         if (!priv)
1807                 return NULL;
1808         INIT_LIST_HEAD(&priv->list);
1809         priv->daemon_pid = -1;
1810         priv->iscsi_transport = tt;
1811         priv->t.user_scan = iscsi_user_scan;
1812         if (!(tt->caps & CAP_DATA_PATH_OFFLOAD))
1813                 priv->t.create_work_queue = 1;
1814
1815         priv->dev.class = &iscsi_transport_class;
1816         dev_set_name(&priv->dev, "%s", tt->name);
1817         err = device_register(&priv->dev);
1818         if (err)
1819                 goto free_priv;
1820
1821         err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
1822         if (err)
1823                 goto unregister_dev;
1824
1825         /* host parameters */
1826         priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1827         priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1828         priv->t.host_attrs.ac.match = iscsi_host_match;
1829         priv->t.host_size = sizeof(struct iscsi_cls_host);
1830         transport_container_register(&priv->t.host_attrs);
1831
1832         SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
1833         SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
1834         SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
1835         SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
1836         BUG_ON(count > ISCSI_HOST_ATTRS);
1837         priv->host_attrs[count] = NULL;
1838         count = 0;
1839
1840         /* connection parameters */
1841         priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1842         priv->conn_cont.ac.class = &iscsi_connection_class.class;
1843         priv->conn_cont.ac.match = iscsi_conn_match;
1844         transport_container_register(&priv->conn_cont);
1845
1846         SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1847         SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1848         SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1849         SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1850         SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1851         SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1852         SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1853         SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1854         SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1855         SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1856         SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1857         SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1858         SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
1859
1860         BUG_ON(count > ISCSI_CONN_ATTRS);
1861         priv->conn_attrs[count] = NULL;
1862         count = 0;
1863
1864         /* session parameters */
1865         priv->session_cont.ac.attrs = &priv->session_attrs[0];
1866         priv->session_cont.ac.class = &iscsi_session_class.class;
1867         priv->session_cont.ac.match = iscsi_session_match;
1868         transport_container_register(&priv->session_cont);
1869
1870         SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1871         SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1872         SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1873         SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1874         SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1875         SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1876         SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1877         SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1878         SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1879         SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1880         SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1881         SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1882         SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1883         SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
1884         SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
1885         SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1886         SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
1887         SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
1888         SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
1889         SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1890         SETUP_PRIV_SESSION_RD_ATTR(state);
1891
1892         BUG_ON(count > ISCSI_SESSION_ATTRS);
1893         priv->session_attrs[count] = NULL;
1894
1895         spin_lock_irqsave(&iscsi_transport_lock, flags);
1896         list_add(&priv->list, &iscsi_transports);
1897         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1898
1899         printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1900         return &priv->t;
1901
1902 unregister_dev:
1903         device_unregister(&priv->dev);
1904         return NULL;
1905 free_priv:
1906         kfree(priv);
1907         return NULL;
1908 }
1909 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1910
1911 int iscsi_unregister_transport(struct iscsi_transport *tt)
1912 {
1913         struct iscsi_internal *priv;
1914         unsigned long flags;
1915
1916         BUG_ON(!tt);
1917
1918         mutex_lock(&rx_queue_mutex);
1919
1920         priv = iscsi_if_transport_lookup(tt);
1921         BUG_ON (!priv);
1922
1923         spin_lock_irqsave(&iscsi_transport_lock, flags);
1924         list_del(&priv->list);
1925         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1926
1927         transport_container_unregister(&priv->conn_cont);
1928         transport_container_unregister(&priv->session_cont);
1929         transport_container_unregister(&priv->t.host_attrs);
1930
1931         sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1932         device_unregister(&priv->dev);
1933         mutex_unlock(&rx_queue_mutex);
1934
1935         return 0;
1936 }
1937 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1938
1939 static __init int iscsi_transport_init(void)
1940 {
1941         int err;
1942
1943         printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
1944                 ISCSI_TRANSPORT_VERSION);
1945
1946         atomic_set(&iscsi_session_nr, 0);
1947
1948         err = class_register(&iscsi_transport_class);
1949         if (err)
1950                 return err;
1951
1952         err = class_register(&iscsi_endpoint_class);
1953         if (err)
1954                 goto unregister_transport_class;
1955
1956         err = transport_class_register(&iscsi_host_class);
1957         if (err)
1958                 goto unregister_endpoint_class;
1959
1960         err = transport_class_register(&iscsi_connection_class);
1961         if (err)
1962                 goto unregister_host_class;
1963
1964         err = transport_class_register(&iscsi_session_class);
1965         if (err)
1966                 goto unregister_conn_class;
1967
1968         nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
1969                                     NULL, THIS_MODULE);
1970         if (!nls) {
1971                 err = -ENOBUFS;
1972                 goto unregister_session_class;
1973         }
1974
1975         iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1976         if (!iscsi_eh_timer_workq)
1977                 goto release_nls;
1978
1979         return 0;
1980
1981 release_nls:
1982         netlink_kernel_release(nls);
1983 unregister_session_class:
1984         transport_class_unregister(&iscsi_session_class);
1985 unregister_conn_class:
1986         transport_class_unregister(&iscsi_connection_class);
1987 unregister_host_class:
1988         transport_class_unregister(&iscsi_host_class);
1989 unregister_endpoint_class:
1990         class_unregister(&iscsi_endpoint_class);
1991 unregister_transport_class:
1992         class_unregister(&iscsi_transport_class);
1993         return err;
1994 }
1995
1996 static void __exit iscsi_transport_exit(void)
1997 {
1998         destroy_workqueue(iscsi_eh_timer_workq);
1999         netlink_kernel_release(nls);
2000         transport_class_unregister(&iscsi_connection_class);
2001         transport_class_unregister(&iscsi_session_class);
2002         transport_class_unregister(&iscsi_host_class);
2003         class_unregister(&iscsi_endpoint_class);
2004         class_unregister(&iscsi_transport_class);
2005 }
2006
2007 module_init(iscsi_transport_init);
2008 module_exit(iscsi_transport_exit);
2009
2010 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2011               "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2012               "Alex Aizman <itn780@yahoo.com>");
2013 MODULE_DESCRIPTION("iSCSI Transport Interface");
2014 MODULE_LICENSE("GPL");
2015 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);