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