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