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