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