[SCSI] iscsi class: sysfs group is_visible callout for session attrs
[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 <linux/slab.h>
26 #include <net/tcp.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_transport.h>
31 #include <scsi/scsi_transport_iscsi.h>
32 #include <scsi/iscsi_if.h>
33 #include <scsi/scsi_cmnd.h>
34
35 #define ISCSI_HOST_ATTRS 4
36
37 #define ISCSI_TRANSPORT_VERSION "2.0-870"
38
39 static int dbg_session;
40 module_param_named(debug_session, dbg_session, int,
41                    S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(debug_session,
43                  "Turn on debugging for sessions in scsi_transport_iscsi "
44                  "module. Set to 1 to turn on, and zero to turn off. Default "
45                  "is off.");
46
47 static int dbg_conn;
48 module_param_named(debug_conn, dbg_conn, int,
49                    S_IRUGO | S_IWUSR);
50 MODULE_PARM_DESC(debug_conn,
51                  "Turn on debugging for connections in scsi_transport_iscsi "
52                  "module. Set to 1 to turn on, and zero to turn off. Default "
53                  "is off.");
54
55 #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...)              \
56         do {                                                            \
57                 if (dbg_session)                                        \
58                         iscsi_cls_session_printk(KERN_INFO, _session,   \
59                                                  "%s: " dbg_fmt,        \
60                                                  __func__, ##arg);      \
61         } while (0);
62
63 #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...)                    \
64         do {                                                            \
65                 if (dbg_conn)                                           \
66                         iscsi_cls_conn_printk(KERN_INFO, _conn,         \
67                                               "%s: " dbg_fmt,           \
68                                               __func__, ##arg); \
69         } while (0);
70
71 struct iscsi_internal {
72         struct scsi_transport_template t;
73         struct iscsi_transport *iscsi_transport;
74         struct list_head list;
75         struct device dev;
76
77         struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
78         struct transport_container conn_cont;
79         struct transport_container session_cont;
80 };
81
82 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
83 static struct workqueue_struct *iscsi_eh_timer_workq;
84
85 /*
86  * list of registered transports and lock that must
87  * be held while accessing list. The iscsi_transport_lock must
88  * be acquired after the rx_queue_mutex.
89  */
90 static LIST_HEAD(iscsi_transports);
91 static DEFINE_SPINLOCK(iscsi_transport_lock);
92
93 #define to_iscsi_internal(tmpl) \
94         container_of(tmpl, struct iscsi_internal, t)
95
96 #define dev_to_iscsi_internal(_dev) \
97         container_of(_dev, struct iscsi_internal, dev)
98
99 static void iscsi_transport_release(struct device *dev)
100 {
101         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
102         kfree(priv);
103 }
104
105 /*
106  * iscsi_transport_class represents the iscsi_transports that are
107  * registered.
108  */
109 static struct class iscsi_transport_class = {
110         .name = "iscsi_transport",
111         .dev_release = iscsi_transport_release,
112 };
113
114 static ssize_t
115 show_transport_handle(struct device *dev, struct device_attribute *attr,
116                       char *buf)
117 {
118         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
119         return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
120 }
121 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
122
123 #define show_transport_attr(name, format)                               \
124 static ssize_t                                                          \
125 show_transport_##name(struct device *dev,                               \
126                       struct device_attribute *attr,char *buf)          \
127 {                                                                       \
128         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);       \
129         return sprintf(buf, format"\n", priv->iscsi_transport->name);   \
130 }                                                                       \
131 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
132
133 show_transport_attr(caps, "0x%x");
134
135 static struct attribute *iscsi_transport_attrs[] = {
136         &dev_attr_handle.attr,
137         &dev_attr_caps.attr,
138         NULL,
139 };
140
141 static struct attribute_group iscsi_transport_group = {
142         .attrs = iscsi_transport_attrs,
143 };
144
145 /*
146  * iSCSI endpoint attrs
147  */
148 #define iscsi_dev_to_endpoint(_dev) \
149         container_of(_dev, struct iscsi_endpoint, dev)
150
151 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store)    \
152 struct device_attribute dev_attr_##_prefix##_##_name =  \
153         __ATTR(_name,_mode,_show,_store)
154
155 static void iscsi_endpoint_release(struct device *dev)
156 {
157         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
158         kfree(ep);
159 }
160
161 static struct class iscsi_endpoint_class = {
162         .name = "iscsi_endpoint",
163         .dev_release = iscsi_endpoint_release,
164 };
165
166 static ssize_t
167 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
168 {
169         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
170         return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
171 }
172 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
173
174 static struct attribute *iscsi_endpoint_attrs[] = {
175         &dev_attr_ep_handle.attr,
176         NULL,
177 };
178
179 static struct attribute_group iscsi_endpoint_group = {
180         .attrs = iscsi_endpoint_attrs,
181 };
182
183 #define ISCSI_MAX_EPID -1
184
185 static int iscsi_match_epid(struct device *dev, void *data)
186 {
187         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
188         uint64_t *epid = (uint64_t *) data;
189
190         return *epid == ep->id;
191 }
192
193 struct iscsi_endpoint *
194 iscsi_create_endpoint(int dd_size)
195 {
196         struct device *dev;
197         struct iscsi_endpoint *ep;
198         uint64_t id;
199         int err;
200
201         for (id = 1; id < ISCSI_MAX_EPID; id++) {
202                 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
203                                         iscsi_match_epid);
204                 if (!dev)
205                         break;
206         }
207         if (id == ISCSI_MAX_EPID) {
208                 printk(KERN_ERR "Too many connections. Max supported %u\n",
209                        ISCSI_MAX_EPID - 1);
210                 return NULL;
211         }
212
213         ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
214         if (!ep)
215                 return NULL;
216
217         ep->id = id;
218         ep->dev.class = &iscsi_endpoint_class;
219         dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
220         err = device_register(&ep->dev);
221         if (err)
222                 goto free_ep;
223
224         err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
225         if (err)
226                 goto unregister_dev;
227
228         if (dd_size)
229                 ep->dd_data = &ep[1];
230         return ep;
231
232 unregister_dev:
233         device_unregister(&ep->dev);
234         return NULL;
235
236 free_ep:
237         kfree(ep);
238         return NULL;
239 }
240 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
241
242 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
243 {
244         sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
245         device_unregister(&ep->dev);
246 }
247 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
248
249 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
250 {
251         struct iscsi_endpoint *ep;
252         struct device *dev;
253
254         dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
255                                 iscsi_match_epid);
256         if (!dev)
257                 return NULL;
258
259         ep = iscsi_dev_to_endpoint(dev);
260         /*
261          * we can drop this now because the interface will prevent
262          * removals and lookups from racing.
263          */
264         put_device(dev);
265         return ep;
266 }
267 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
268
269 /*
270  * Interface to display network param to sysfs
271  */
272
273 static void iscsi_iface_release(struct device *dev)
274 {
275         struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
276         struct device *parent = iface->dev.parent;
277
278         kfree(iface);
279         put_device(parent);
280 }
281
282
283 static struct class iscsi_iface_class = {
284         .name = "iscsi_iface",
285         .dev_release = iscsi_iface_release,
286 };
287
288 #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store)  \
289 struct device_attribute dev_attr_##_prefix##_##_name =          \
290         __ATTR(_name, _mode, _show, _store)
291
292 /* iface attrs show */
293 #define iscsi_iface_attr_show(type, name, param_type, param)            \
294 static ssize_t                                                          \
295 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
296                      char *buf)                                         \
297 {                                                                       \
298         struct iscsi_iface *iface = iscsi_dev_to_iface(dev);            \
299         struct iscsi_transport *t = iface->transport;                   \
300         return t->get_iface_param(iface, param_type, param, buf);       \
301 }                                                                       \
302
303 #define iscsi_iface_net_attr(type, name, param)                         \
304         iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param)       \
305 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
306
307 /* generic read only ipvi4 attribute */
308 iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
309 iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
310 iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
311 iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
312
313 /* generic read only ipv6 attribute */
314 iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
315 iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
316 iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
317 iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
318                      ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
319 iscsi_iface_net_attr(ipv6_iface, linklocal_autocfg,
320                      ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
321
322 /* common read only iface attribute */
323 iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
324 iscsi_iface_net_attr(iface, vlan, ISCSI_NET_PARAM_VLAN_ID);
325
326 static mode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
327                                           struct attribute *attr, int i)
328 {
329         struct device *dev = container_of(kobj, struct device, kobj);
330         struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
331         struct iscsi_transport *t = iface->transport;
332
333         if (attr == &dev_attr_iface_enabled.attr)
334                 return (t->iface_param_mask & ISCSI_NET_IFACE_ENABLE) ?
335                                                                 S_IRUGO : 0;
336         else if (attr == &dev_attr_iface_vlan.attr)
337                 return (t->iface_param_mask & ISCSI_NET_VLAN_ID) ? S_IRUGO : 0;
338
339         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
340                 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
341                         return (t->iface_param_mask & ISCSI_NET_IPV4_ADDR) ?
342                                                                 S_IRUGO : 0;
343                 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
344                         return (t->iface_param_mask & ISCSI_NET_IPV4_GW) ?
345                                                                 S_IRUGO : 0;
346                 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
347                         return (t->iface_param_mask & ISCSI_NET_IPV4_SUBNET) ?
348                                                                 S_IRUGO : 0;
349                 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
350                         return (t->iface_param_mask & ISCSI_NET_IPV4_BOOTPROTO) ?
351                                                                  S_IRUGO : 0;
352         } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
353                 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
354                         return (t->iface_param_mask & ISCSI_NET_IPV6_ADDR) ?
355                                                                 S_IRUGO : 0;
356                 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
357                         return (t->iface_param_mask & ISCSI_NET_IPV6_LINKLOCAL) ?
358                                                                 S_IRUGO : 0;
359                 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
360                         return (t->iface_param_mask & ISCSI_NET_IPV6_ROUTER) ?
361                                                                 S_IRUGO : 0;
362                 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
363                         return (t->iface_param_mask & ISCSI_NET_IPV6_ADDR_AUTOCFG) ?
364                                                                 S_IRUGO : 0;
365                 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
366                         return (t->iface_param_mask & ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG) ?
367                                                                 S_IRUGO : 0;
368         }
369
370         return 0;
371 }
372
373 static struct attribute *iscsi_iface_attrs[] = {
374         &dev_attr_iface_enabled.attr,
375         &dev_attr_iface_vlan.attr,
376         &dev_attr_ipv4_iface_ipaddress.attr,
377         &dev_attr_ipv4_iface_gateway.attr,
378         &dev_attr_ipv4_iface_subnet.attr,
379         &dev_attr_ipv4_iface_bootproto.attr,
380         &dev_attr_ipv6_iface_ipaddress.attr,
381         &dev_attr_ipv6_iface_link_local_addr.attr,
382         &dev_attr_ipv6_iface_router_addr.attr,
383         &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
384         &dev_attr_ipv6_iface_linklocal_autocfg.attr,
385         NULL,
386 };
387
388 static struct attribute_group iscsi_iface_group = {
389         .attrs = iscsi_iface_attrs,
390         .is_visible = iscsi_iface_attr_is_visible,
391 };
392
393 struct iscsi_iface *
394 iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
395                    uint32_t iface_type, uint32_t iface_num, int dd_size)
396 {
397         struct iscsi_iface *iface;
398         int err;
399
400         iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
401         if (!iface)
402                 return NULL;
403
404         iface->transport = transport;
405         iface->iface_type = iface_type;
406         iface->iface_num = iface_num;
407         iface->dev.release = iscsi_iface_release;
408         iface->dev.class = &iscsi_iface_class;
409         /* parent reference released in iscsi_iface_release */
410         iface->dev.parent = get_device(&shost->shost_gendev);
411         if (iface_type == ISCSI_IFACE_TYPE_IPV4)
412                 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
413                              iface_num);
414         else
415                 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
416                              iface_num);
417
418         err = device_register(&iface->dev);
419         if (err)
420                 goto free_iface;
421
422         err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
423         if (err)
424                 goto unreg_iface;
425
426         if (dd_size)
427                 iface->dd_data = &iface[1];
428         return iface;
429
430 unreg_iface:
431         device_unregister(&iface->dev);
432         return NULL;
433
434 free_iface:
435         put_device(iface->dev.parent);
436         kfree(iface);
437         return NULL;
438 }
439 EXPORT_SYMBOL_GPL(iscsi_create_iface);
440
441 void iscsi_destroy_iface(struct iscsi_iface *iface)
442 {
443         sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
444         device_unregister(&iface->dev);
445 }
446 EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
447
448 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
449                             struct device *cdev)
450 {
451         struct Scsi_Host *shost = dev_to_shost(dev);
452         struct iscsi_cls_host *ihost = shost->shost_data;
453
454         memset(ihost, 0, sizeof(*ihost));
455         atomic_set(&ihost->nr_scans, 0);
456         mutex_init(&ihost->mutex);
457         return 0;
458 }
459
460 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
461                                "iscsi_host",
462                                iscsi_setup_host,
463                                NULL,
464                                NULL);
465
466 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
467                                "iscsi_session",
468                                NULL,
469                                NULL,
470                                NULL);
471
472 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
473                                "iscsi_connection",
474                                NULL,
475                                NULL,
476                                NULL);
477
478 static struct sock *nls;
479 static DEFINE_MUTEX(rx_queue_mutex);
480
481 static LIST_HEAD(sesslist);
482 static DEFINE_SPINLOCK(sesslock);
483 static LIST_HEAD(connlist);
484 static DEFINE_SPINLOCK(connlock);
485
486 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
487 {
488         struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
489         return sess->sid;
490 }
491
492 /*
493  * Returns the matching session to a given sid
494  */
495 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
496 {
497         unsigned long flags;
498         struct iscsi_cls_session *sess;
499
500         spin_lock_irqsave(&sesslock, flags);
501         list_for_each_entry(sess, &sesslist, sess_list) {
502                 if (sess->sid == sid) {
503                         spin_unlock_irqrestore(&sesslock, flags);
504                         return sess;
505                 }
506         }
507         spin_unlock_irqrestore(&sesslock, flags);
508         return NULL;
509 }
510
511 /*
512  * Returns the matching connection to a given sid / cid tuple
513  */
514 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
515 {
516         unsigned long flags;
517         struct iscsi_cls_conn *conn;
518
519         spin_lock_irqsave(&connlock, flags);
520         list_for_each_entry(conn, &connlist, conn_list) {
521                 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
522                         spin_unlock_irqrestore(&connlock, flags);
523                         return conn;
524                 }
525         }
526         spin_unlock_irqrestore(&connlock, flags);
527         return NULL;
528 }
529
530 /*
531  * The following functions can be used by LLDs that allocate
532  * their own scsi_hosts or by software iscsi LLDs
533  */
534 static struct {
535         int value;
536         char *name;
537 } iscsi_session_state_names[] = {
538         { ISCSI_SESSION_LOGGED_IN,      "LOGGED_IN" },
539         { ISCSI_SESSION_FAILED,         "FAILED" },
540         { ISCSI_SESSION_FREE,           "FREE" },
541 };
542
543 static const char *iscsi_session_state_name(int state)
544 {
545         int i;
546         char *name = NULL;
547
548         for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
549                 if (iscsi_session_state_names[i].value == state) {
550                         name = iscsi_session_state_names[i].name;
551                         break;
552                 }
553         }
554         return name;
555 }
556
557 int iscsi_session_chkready(struct iscsi_cls_session *session)
558 {
559         unsigned long flags;
560         int err;
561
562         spin_lock_irqsave(&session->lock, flags);
563         switch (session->state) {
564         case ISCSI_SESSION_LOGGED_IN:
565                 err = 0;
566                 break;
567         case ISCSI_SESSION_FAILED:
568                 err = DID_IMM_RETRY << 16;
569                 break;
570         case ISCSI_SESSION_FREE:
571                 err = DID_TRANSPORT_FAILFAST << 16;
572                 break;
573         default:
574                 err = DID_NO_CONNECT << 16;
575                 break;
576         }
577         spin_unlock_irqrestore(&session->lock, flags);
578         return err;
579 }
580 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
581
582 static void iscsi_session_release(struct device *dev)
583 {
584         struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
585         struct Scsi_Host *shost;
586
587         shost = iscsi_session_to_shost(session);
588         scsi_host_put(shost);
589         ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
590         kfree(session);
591 }
592
593 static int iscsi_is_session_dev(const struct device *dev)
594 {
595         return dev->release == iscsi_session_release;
596 }
597
598 static int iscsi_iter_session_fn(struct device *dev, void *data)
599 {
600         void (* fn) (struct iscsi_cls_session *) = data;
601
602         if (!iscsi_is_session_dev(dev))
603                 return 0;
604         fn(iscsi_dev_to_session(dev));
605         return 0;
606 }
607
608 void iscsi_host_for_each_session(struct Scsi_Host *shost,
609                                  void (*fn)(struct iscsi_cls_session *))
610 {
611         device_for_each_child(&shost->shost_gendev, fn,
612                               iscsi_iter_session_fn);
613 }
614 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
615
616 /**
617  * iscsi_scan_finished - helper to report when running scans are done
618  * @shost: scsi host
619  * @time: scan run time
620  *
621  * This function can be used by drives like qla4xxx to report to the scsi
622  * layer when the scans it kicked off at module load time are done.
623  */
624 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
625 {
626         struct iscsi_cls_host *ihost = shost->shost_data;
627         /*
628          * qla4xxx will have kicked off some session unblocks before calling
629          * scsi_scan_host, so just wait for them to complete.
630          */
631         return !atomic_read(&ihost->nr_scans);
632 }
633 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
634
635 struct iscsi_scan_data {
636         unsigned int channel;
637         unsigned int id;
638         unsigned int lun;
639 };
640
641 static int iscsi_user_scan_session(struct device *dev, void *data)
642 {
643         struct iscsi_scan_data *scan_data = data;
644         struct iscsi_cls_session *session;
645         struct Scsi_Host *shost;
646         struct iscsi_cls_host *ihost;
647         unsigned long flags;
648         unsigned int id;
649
650         if (!iscsi_is_session_dev(dev))
651                 return 0;
652
653         session = iscsi_dev_to_session(dev);
654
655         ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
656
657         shost = iscsi_session_to_shost(session);
658         ihost = shost->shost_data;
659
660         mutex_lock(&ihost->mutex);
661         spin_lock_irqsave(&session->lock, flags);
662         if (session->state != ISCSI_SESSION_LOGGED_IN) {
663                 spin_unlock_irqrestore(&session->lock, flags);
664                 goto user_scan_exit;
665         }
666         id = session->target_id;
667         spin_unlock_irqrestore(&session->lock, flags);
668
669         if (id != ISCSI_MAX_TARGET) {
670                 if ((scan_data->channel == SCAN_WILD_CARD ||
671                      scan_data->channel == 0) &&
672                     (scan_data->id == SCAN_WILD_CARD ||
673                      scan_data->id == id))
674                         scsi_scan_target(&session->dev, 0, id,
675                                          scan_data->lun, 1);
676         }
677
678 user_scan_exit:
679         mutex_unlock(&ihost->mutex);
680         ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
681         return 0;
682 }
683
684 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
685                            uint id, uint lun)
686 {
687         struct iscsi_scan_data scan_data;
688
689         scan_data.channel = channel;
690         scan_data.id = id;
691         scan_data.lun = lun;
692
693         return device_for_each_child(&shost->shost_gendev, &scan_data,
694                                      iscsi_user_scan_session);
695 }
696
697 static void iscsi_scan_session(struct work_struct *work)
698 {
699         struct iscsi_cls_session *session =
700                         container_of(work, struct iscsi_cls_session, scan_work);
701         struct Scsi_Host *shost = iscsi_session_to_shost(session);
702         struct iscsi_cls_host *ihost = shost->shost_data;
703         struct iscsi_scan_data scan_data;
704
705         scan_data.channel = 0;
706         scan_data.id = SCAN_WILD_CARD;
707         scan_data.lun = SCAN_WILD_CARD;
708
709         iscsi_user_scan_session(&session->dev, &scan_data);
710         atomic_dec(&ihost->nr_scans);
711 }
712
713 /**
714  * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
715  * @cmd: scsi cmd passed to scsi eh handler
716  *
717  * If the session is down this function will wait for the recovery
718  * timer to fire or for the session to be logged back in. If the
719  * recovery timer fires then FAST_IO_FAIL is returned. The caller
720  * should pass this error value to the scsi eh.
721  */
722 int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
723 {
724         struct iscsi_cls_session *session =
725                         starget_to_session(scsi_target(cmd->device));
726         unsigned long flags;
727         int ret = 0;
728
729         spin_lock_irqsave(&session->lock, flags);
730         while (session->state != ISCSI_SESSION_LOGGED_IN) {
731                 if (session->state == ISCSI_SESSION_FREE) {
732                         ret = FAST_IO_FAIL;
733                         break;
734                 }
735                 spin_unlock_irqrestore(&session->lock, flags);
736                 msleep(1000);
737                 spin_lock_irqsave(&session->lock, flags);
738         }
739         spin_unlock_irqrestore(&session->lock, flags);
740         return ret;
741 }
742 EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
743
744 static void session_recovery_timedout(struct work_struct *work)
745 {
746         struct iscsi_cls_session *session =
747                 container_of(work, struct iscsi_cls_session,
748                              recovery_work.work);
749         unsigned long flags;
750
751         iscsi_cls_session_printk(KERN_INFO, session,
752                                  "session recovery timed out after %d secs\n",
753                                  session->recovery_tmo);
754
755         spin_lock_irqsave(&session->lock, flags);
756         switch (session->state) {
757         case ISCSI_SESSION_FAILED:
758                 session->state = ISCSI_SESSION_FREE;
759                 break;
760         case ISCSI_SESSION_LOGGED_IN:
761         case ISCSI_SESSION_FREE:
762                 /* we raced with the unblock's flush */
763                 spin_unlock_irqrestore(&session->lock, flags);
764                 return;
765         }
766         spin_unlock_irqrestore(&session->lock, flags);
767
768         if (session->transport->session_recovery_timedout)
769                 session->transport->session_recovery_timedout(session);
770
771         ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
772         scsi_target_unblock(&session->dev);
773         ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
774 }
775
776 static void __iscsi_unblock_session(struct work_struct *work)
777 {
778         struct iscsi_cls_session *session =
779                         container_of(work, struct iscsi_cls_session,
780                                      unblock_work);
781         struct Scsi_Host *shost = iscsi_session_to_shost(session);
782         struct iscsi_cls_host *ihost = shost->shost_data;
783         unsigned long flags;
784
785         ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
786         /*
787          * The recovery and unblock work get run from the same workqueue,
788          * so try to cancel it if it was going to run after this unblock.
789          */
790         cancel_delayed_work(&session->recovery_work);
791         spin_lock_irqsave(&session->lock, flags);
792         session->state = ISCSI_SESSION_LOGGED_IN;
793         spin_unlock_irqrestore(&session->lock, flags);
794         /* start IO */
795         scsi_target_unblock(&session->dev);
796         /*
797          * Only do kernel scanning if the driver is properly hooked into
798          * the async scanning code (drivers like iscsi_tcp do login and
799          * scanning from userspace).
800          */
801         if (shost->hostt->scan_finished) {
802                 if (scsi_queue_work(shost, &session->scan_work))
803                         atomic_inc(&ihost->nr_scans);
804         }
805         ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
806 }
807
808 /**
809  * iscsi_unblock_session - set a session as logged in and start IO.
810  * @session: iscsi session
811  *
812  * Mark a session as ready to accept IO.
813  */
814 void iscsi_unblock_session(struct iscsi_cls_session *session)
815 {
816         queue_work(iscsi_eh_timer_workq, &session->unblock_work);
817         /*
818          * make sure all the events have completed before tell the driver
819          * it is safe
820          */
821         flush_workqueue(iscsi_eh_timer_workq);
822 }
823 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
824
825 static void __iscsi_block_session(struct work_struct *work)
826 {
827         struct iscsi_cls_session *session =
828                         container_of(work, struct iscsi_cls_session,
829                                      block_work);
830         unsigned long flags;
831
832         ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
833         spin_lock_irqsave(&session->lock, flags);
834         session->state = ISCSI_SESSION_FAILED;
835         spin_unlock_irqrestore(&session->lock, flags);
836         scsi_target_block(&session->dev);
837         ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
838         if (session->recovery_tmo >= 0)
839                 queue_delayed_work(iscsi_eh_timer_workq,
840                                    &session->recovery_work,
841                                    session->recovery_tmo * HZ);
842 }
843
844 void iscsi_block_session(struct iscsi_cls_session *session)
845 {
846         queue_work(iscsi_eh_timer_workq, &session->block_work);
847 }
848 EXPORT_SYMBOL_GPL(iscsi_block_session);
849
850 static void __iscsi_unbind_session(struct work_struct *work)
851 {
852         struct iscsi_cls_session *session =
853                         container_of(work, struct iscsi_cls_session,
854                                      unbind_work);
855         struct Scsi_Host *shost = iscsi_session_to_shost(session);
856         struct iscsi_cls_host *ihost = shost->shost_data;
857         unsigned long flags;
858
859         ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
860
861         /* Prevent new scans and make sure scanning is not in progress */
862         mutex_lock(&ihost->mutex);
863         spin_lock_irqsave(&session->lock, flags);
864         if (session->target_id == ISCSI_MAX_TARGET) {
865                 spin_unlock_irqrestore(&session->lock, flags);
866                 mutex_unlock(&ihost->mutex);
867                 return;
868         }
869         session->target_id = ISCSI_MAX_TARGET;
870         spin_unlock_irqrestore(&session->lock, flags);
871         mutex_unlock(&ihost->mutex);
872
873         scsi_remove_target(&session->dev);
874         iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
875         ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
876 }
877
878 struct iscsi_cls_session *
879 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
880                     int dd_size)
881 {
882         struct iscsi_cls_session *session;
883
884         session = kzalloc(sizeof(*session) + dd_size,
885                           GFP_KERNEL);
886         if (!session)
887                 return NULL;
888
889         session->transport = transport;
890         session->recovery_tmo = 120;
891         session->state = ISCSI_SESSION_FREE;
892         INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
893         INIT_LIST_HEAD(&session->sess_list);
894         INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
895         INIT_WORK(&session->block_work, __iscsi_block_session);
896         INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
897         INIT_WORK(&session->scan_work, iscsi_scan_session);
898         spin_lock_init(&session->lock);
899
900         /* this is released in the dev's release function */
901         scsi_host_get(shost);
902         session->dev.parent = &shost->shost_gendev;
903         session->dev.release = iscsi_session_release;
904         device_initialize(&session->dev);
905         if (dd_size)
906                 session->dd_data = &session[1];
907
908         ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
909         return session;
910 }
911 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
912
913 static int iscsi_get_next_target_id(struct device *dev, void *data)
914 {
915         struct iscsi_cls_session *session;
916         unsigned long flags;
917         int err = 0;
918
919         if (!iscsi_is_session_dev(dev))
920                 return 0;
921
922         session = iscsi_dev_to_session(dev);
923         spin_lock_irqsave(&session->lock, flags);
924         if (*((unsigned int *) data) == session->target_id)
925                 err = -EEXIST;
926         spin_unlock_irqrestore(&session->lock, flags);
927         return err;
928 }
929
930 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
931 {
932         struct Scsi_Host *shost = iscsi_session_to_shost(session);
933         struct iscsi_cls_host *ihost;
934         unsigned long flags;
935         unsigned int id = target_id;
936         int err;
937
938         ihost = shost->shost_data;
939         session->sid = atomic_add_return(1, &iscsi_session_nr);
940
941         if (id == ISCSI_MAX_TARGET) {
942                 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
943                         err = device_for_each_child(&shost->shost_gendev, &id,
944                                                     iscsi_get_next_target_id);
945                         if (!err)
946                                 break;
947                 }
948
949                 if (id == ISCSI_MAX_TARGET) {
950                         iscsi_cls_session_printk(KERN_ERR, session,
951                                                  "Too many iscsi targets. Max "
952                                                  "number of targets is %d.\n",
953                                                  ISCSI_MAX_TARGET - 1);
954                         err = -EOVERFLOW;
955                         goto release_host;
956                 }
957         }
958         session->target_id = id;
959
960         dev_set_name(&session->dev, "session%u", session->sid);
961         err = device_add(&session->dev);
962         if (err) {
963                 iscsi_cls_session_printk(KERN_ERR, session,
964                                          "could not register session's dev\n");
965                 goto release_host;
966         }
967         transport_register_device(&session->dev);
968
969         spin_lock_irqsave(&sesslock, flags);
970         list_add(&session->sess_list, &sesslist);
971         spin_unlock_irqrestore(&sesslock, flags);
972
973         iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
974         ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
975         return 0;
976
977 release_host:
978         scsi_host_put(shost);
979         return err;
980 }
981 EXPORT_SYMBOL_GPL(iscsi_add_session);
982
983 /**
984  * iscsi_create_session - create iscsi class session
985  * @shost: scsi host
986  * @transport: iscsi transport
987  * @dd_size: private driver data size
988  * @target_id: which target
989  *
990  * This can be called from a LLD or iscsi_transport.
991  */
992 struct iscsi_cls_session *
993 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
994                      int dd_size, unsigned int target_id)
995 {
996         struct iscsi_cls_session *session;
997
998         session = iscsi_alloc_session(shost, transport, dd_size);
999         if (!session)
1000                 return NULL;
1001
1002         if (iscsi_add_session(session, target_id)) {
1003                 iscsi_free_session(session);
1004                 return NULL;
1005         }
1006         return session;
1007 }
1008 EXPORT_SYMBOL_GPL(iscsi_create_session);
1009
1010 static void iscsi_conn_release(struct device *dev)
1011 {
1012         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1013         struct device *parent = conn->dev.parent;
1014
1015         ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
1016         kfree(conn);
1017         put_device(parent);
1018 }
1019
1020 static int iscsi_is_conn_dev(const struct device *dev)
1021 {
1022         return dev->release == iscsi_conn_release;
1023 }
1024
1025 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1026 {
1027         if (!iscsi_is_conn_dev(dev))
1028                 return 0;
1029         return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1030 }
1031
1032 void iscsi_remove_session(struct iscsi_cls_session *session)
1033 {
1034         struct Scsi_Host *shost = iscsi_session_to_shost(session);
1035         unsigned long flags;
1036         int err;
1037
1038         ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1039
1040         spin_lock_irqsave(&sesslock, flags);
1041         list_del(&session->sess_list);
1042         spin_unlock_irqrestore(&sesslock, flags);
1043
1044         /* make sure there are no blocks/unblocks queued */
1045         flush_workqueue(iscsi_eh_timer_workq);
1046         /* make sure the timedout callout is not running */
1047         if (!cancel_delayed_work(&session->recovery_work))
1048                 flush_workqueue(iscsi_eh_timer_workq);
1049         /*
1050          * If we are blocked let commands flow again. The lld or iscsi
1051          * layer should set up the queuecommand to fail commands.
1052          * We assume that LLD will not be calling block/unblock while
1053          * removing the session.
1054          */
1055         spin_lock_irqsave(&session->lock, flags);
1056         session->state = ISCSI_SESSION_FREE;
1057         spin_unlock_irqrestore(&session->lock, flags);
1058
1059         scsi_target_unblock(&session->dev);
1060         /* flush running scans then delete devices */
1061         scsi_flush_work(shost);
1062         __iscsi_unbind_session(&session->unbind_work);
1063
1064         /* hw iscsi may not have removed all connections from session */
1065         err = device_for_each_child(&session->dev, NULL,
1066                                     iscsi_iter_destroy_conn_fn);
1067         if (err)
1068                 iscsi_cls_session_printk(KERN_ERR, session,
1069                                          "Could not delete all connections "
1070                                          "for session. Error %d.\n", err);
1071
1072         transport_unregister_device(&session->dev);
1073
1074         ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
1075         device_del(&session->dev);
1076 }
1077 EXPORT_SYMBOL_GPL(iscsi_remove_session);
1078
1079 void iscsi_free_session(struct iscsi_cls_session *session)
1080 {
1081         ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
1082         iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
1083         put_device(&session->dev);
1084 }
1085 EXPORT_SYMBOL_GPL(iscsi_free_session);
1086
1087 /**
1088  * iscsi_destroy_session - destroy iscsi session
1089  * @session: iscsi_session
1090  *
1091  * Can be called by a LLD or iscsi_transport. There must not be
1092  * any running connections.
1093  */
1094 int iscsi_destroy_session(struct iscsi_cls_session *session)
1095 {
1096         iscsi_remove_session(session);
1097         ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
1098         iscsi_free_session(session);
1099         return 0;
1100 }
1101 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1102
1103 /**
1104  * iscsi_create_conn - create iscsi class connection
1105  * @session: iscsi cls session
1106  * @dd_size: private driver data size
1107  * @cid: connection id
1108  *
1109  * This can be called from a LLD or iscsi_transport. The connection
1110  * is child of the session so cid must be unique for all connections
1111  * on the session.
1112  *
1113  * Since we do not support MCS, cid will normally be zero. In some cases
1114  * for software iscsi we could be trying to preallocate a connection struct
1115  * in which case there could be two connection structs and cid would be
1116  * non-zero.
1117  */
1118 struct iscsi_cls_conn *
1119 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
1120 {
1121         struct iscsi_transport *transport = session->transport;
1122         struct iscsi_cls_conn *conn;
1123         unsigned long flags;
1124         int err;
1125
1126         conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
1127         if (!conn)
1128                 return NULL;
1129         if (dd_size)
1130                 conn->dd_data = &conn[1];
1131
1132         mutex_init(&conn->ep_mutex);
1133         INIT_LIST_HEAD(&conn->conn_list);
1134         conn->transport = transport;
1135         conn->cid = cid;
1136
1137         /* this is released in the dev's release function */
1138         if (!get_device(&session->dev))
1139                 goto free_conn;
1140
1141         dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
1142         conn->dev.parent = &session->dev;
1143         conn->dev.release = iscsi_conn_release;
1144         err = device_register(&conn->dev);
1145         if (err) {
1146                 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1147                                          "register connection's dev\n");
1148                 goto release_parent_ref;
1149         }
1150         transport_register_device(&conn->dev);
1151
1152         spin_lock_irqsave(&connlock, flags);
1153         list_add(&conn->conn_list, &connlist);
1154         spin_unlock_irqrestore(&connlock, flags);
1155
1156         ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
1157         return conn;
1158
1159 release_parent_ref:
1160         put_device(&session->dev);
1161 free_conn:
1162         kfree(conn);
1163         return NULL;
1164 }
1165
1166 EXPORT_SYMBOL_GPL(iscsi_create_conn);
1167
1168 /**
1169  * iscsi_destroy_conn - destroy iscsi class connection
1170  * @conn: iscsi cls session
1171  *
1172  * This can be called from a LLD or iscsi_transport.
1173  */
1174 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1175 {
1176         unsigned long flags;
1177
1178         spin_lock_irqsave(&connlock, flags);
1179         list_del(&conn->conn_list);
1180         spin_unlock_irqrestore(&connlock, flags);
1181
1182         transport_unregister_device(&conn->dev);
1183         ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
1184         device_unregister(&conn->dev);
1185         return 0;
1186 }
1187 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1188
1189 /*
1190  * iscsi interface functions
1191  */
1192 static struct iscsi_internal *
1193 iscsi_if_transport_lookup(struct iscsi_transport *tt)
1194 {
1195         struct iscsi_internal *priv;
1196         unsigned long flags;
1197
1198         spin_lock_irqsave(&iscsi_transport_lock, flags);
1199         list_for_each_entry(priv, &iscsi_transports, list) {
1200                 if (tt == priv->iscsi_transport) {
1201                         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1202                         return priv;
1203                 }
1204         }
1205         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1206         return NULL;
1207 }
1208
1209 static int
1210 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
1211 {
1212         return nlmsg_multicast(nls, skb, 0, group, gfp);
1213 }
1214
1215 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
1216                    char *data, uint32_t data_size)
1217 {
1218         struct nlmsghdr *nlh;
1219         struct sk_buff *skb;
1220         struct iscsi_uevent *ev;
1221         char *pdu;
1222         struct iscsi_internal *priv;
1223         int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1224                               data_size);
1225
1226         priv = iscsi_if_transport_lookup(conn->transport);
1227         if (!priv)
1228                 return -EINVAL;
1229
1230         skb = alloc_skb(len, GFP_ATOMIC);
1231         if (!skb) {
1232                 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
1233                 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1234                                       "control PDU: OOM\n");
1235                 return -ENOMEM;
1236         }
1237
1238         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1239         ev = NLMSG_DATA(nlh);
1240         memset(ev, 0, sizeof(*ev));
1241         ev->transport_handle = iscsi_handle(conn->transport);
1242         ev->type = ISCSI_KEVENT_RECV_PDU;
1243         ev->r.recv_req.cid = conn->cid;
1244         ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
1245         pdu = (char*)ev + sizeof(*ev);
1246         memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1247         memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1248
1249         return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1250 }
1251 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1252
1253 int iscsi_offload_mesg(struct Scsi_Host *shost,
1254                        struct iscsi_transport *transport, uint32_t type,
1255                        char *data, uint16_t data_size)
1256 {
1257         struct nlmsghdr *nlh;
1258         struct sk_buff *skb;
1259         struct iscsi_uevent *ev;
1260         int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1261
1262         skb = alloc_skb(len, GFP_ATOMIC);
1263         if (!skb) {
1264                 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1265                 return -ENOMEM;
1266         }
1267
1268         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1269         ev = NLMSG_DATA(nlh);
1270         memset(ev, 0, sizeof(*ev));
1271         ev->type = type;
1272         ev->transport_handle = iscsi_handle(transport);
1273         switch (type) {
1274         case ISCSI_KEVENT_PATH_REQ:
1275                 ev->r.req_path.host_no = shost->host_no;
1276                 break;
1277         case ISCSI_KEVENT_IF_DOWN:
1278                 ev->r.notify_if_down.host_no = shost->host_no;
1279                 break;
1280         }
1281
1282         memcpy((char *)ev + sizeof(*ev), data, data_size);
1283
1284         return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
1285 }
1286 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1287
1288 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
1289 {
1290         struct nlmsghdr *nlh;
1291         struct sk_buff  *skb;
1292         struct iscsi_uevent *ev;
1293         struct iscsi_internal *priv;
1294         int len = NLMSG_SPACE(sizeof(*ev));
1295
1296         priv = iscsi_if_transport_lookup(conn->transport);
1297         if (!priv)
1298                 return;
1299
1300         skb = alloc_skb(len, GFP_ATOMIC);
1301         if (!skb) {
1302                 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1303                                       "conn error (%d)\n", error);
1304                 return;
1305         }
1306
1307         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1308         ev = NLMSG_DATA(nlh);
1309         ev->transport_handle = iscsi_handle(conn->transport);
1310         ev->type = ISCSI_KEVENT_CONN_ERROR;
1311         ev->r.connerror.error = error;
1312         ev->r.connerror.cid = conn->cid;
1313         ev->r.connerror.sid = iscsi_conn_get_sid(conn);
1314
1315         iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1316
1317         iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1318                               error);
1319 }
1320 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
1321
1322 static int
1323 iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1324                     void *payload, int size)
1325 {
1326         struct sk_buff  *skb;
1327         struct nlmsghdr *nlh;
1328         int len = NLMSG_SPACE(size);
1329         int flags = multi ? NLM_F_MULTI : 0;
1330         int t = done ? NLMSG_DONE : type;
1331
1332         skb = alloc_skb(len, GFP_ATOMIC);
1333         if (!skb) {
1334                 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1335                 return -ENOMEM;
1336         }
1337
1338         nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
1339         nlh->nlmsg_flags = flags;
1340         memcpy(NLMSG_DATA(nlh), payload, size);
1341         return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
1342 }
1343
1344 static int
1345 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
1346 {
1347         struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1348         struct iscsi_stats *stats;
1349         struct sk_buff *skbstat;
1350         struct iscsi_cls_conn *conn;
1351         struct nlmsghdr *nlhstat;
1352         struct iscsi_uevent *evstat;
1353         struct iscsi_internal *priv;
1354         int len = NLMSG_SPACE(sizeof(*ev) +
1355                               sizeof(struct iscsi_stats) +
1356                               sizeof(struct iscsi_stats_custom) *
1357                               ISCSI_STATS_CUSTOM_MAX);
1358         int err = 0;
1359
1360         priv = iscsi_if_transport_lookup(transport);
1361         if (!priv)
1362                 return -EINVAL;
1363
1364         conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
1365         if (!conn)
1366                 return -EEXIST;
1367
1368         do {
1369                 int actual_size;
1370
1371                 skbstat = alloc_skb(len, GFP_ATOMIC);
1372                 if (!skbstat) {
1373                         iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1374                                               "deliver stats: OOM\n");
1375                         return -ENOMEM;
1376                 }
1377
1378                 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
1379                                       (len - sizeof(*nlhstat)), 0);
1380                 evstat = NLMSG_DATA(nlhstat);
1381                 memset(evstat, 0, sizeof(*evstat));
1382                 evstat->transport_handle = iscsi_handle(conn->transport);
1383                 evstat->type = nlh->nlmsg_type;
1384                 evstat->u.get_stats.cid =
1385                         ev->u.get_stats.cid;
1386                 evstat->u.get_stats.sid =
1387                         ev->u.get_stats.sid;
1388                 stats = (struct iscsi_stats *)
1389                         ((char*)evstat + sizeof(*evstat));
1390                 memset(stats, 0, sizeof(*stats));
1391
1392                 transport->get_stats(conn, stats);
1393                 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1394                                           sizeof(struct iscsi_stats) +
1395                                           sizeof(struct iscsi_stats_custom) *
1396                                           stats->custom_length);
1397                 actual_size -= sizeof(*nlhstat);
1398                 actual_size = NLMSG_LENGTH(actual_size);
1399                 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1400                 nlhstat->nlmsg_len = actual_size;
1401
1402                 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1403                                           GFP_ATOMIC);
1404         } while (err < 0 && err != -ECONNREFUSED);
1405
1406         return err;
1407 }
1408
1409 /**
1410  * iscsi_session_event - send session destr. completion event
1411  * @session: iscsi class session
1412  * @event: type of event
1413  */
1414 int iscsi_session_event(struct iscsi_cls_session *session,
1415                         enum iscsi_uevent_e event)
1416 {
1417         struct iscsi_internal *priv;
1418         struct Scsi_Host *shost;
1419         struct iscsi_uevent *ev;
1420         struct sk_buff  *skb;
1421         struct nlmsghdr *nlh;
1422         int rc, len = NLMSG_SPACE(sizeof(*ev));
1423
1424         priv = iscsi_if_transport_lookup(session->transport);
1425         if (!priv)
1426                 return -EINVAL;
1427         shost = iscsi_session_to_shost(session);
1428
1429         skb = alloc_skb(len, GFP_KERNEL);
1430         if (!skb) {
1431                 iscsi_cls_session_printk(KERN_ERR, session,
1432                                          "Cannot notify userspace of session "
1433                                          "event %u\n", event);
1434                 return -ENOMEM;
1435         }
1436
1437         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1438         ev = NLMSG_DATA(nlh);
1439         ev->transport_handle = iscsi_handle(session->transport);
1440
1441         ev->type = event;
1442         switch (event) {
1443         case ISCSI_KEVENT_DESTROY_SESSION:
1444                 ev->r.d_session.host_no = shost->host_no;
1445                 ev->r.d_session.sid = session->sid;
1446                 break;
1447         case ISCSI_KEVENT_CREATE_SESSION:
1448                 ev->r.c_session_ret.host_no = shost->host_no;
1449                 ev->r.c_session_ret.sid = session->sid;
1450                 break;
1451         case ISCSI_KEVENT_UNBIND_SESSION:
1452                 ev->r.unbind_session.host_no = shost->host_no;
1453                 ev->r.unbind_session.sid = session->sid;
1454                 break;
1455         default:
1456                 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1457                                          "%u.\n", event);
1458                 kfree_skb(skb);
1459                 return -EINVAL;
1460         }
1461
1462         /*
1463          * this will occur if the daemon is not up, so we just warn
1464          * the user and when the daemon is restarted it will handle it
1465          */
1466         rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
1467         if (rc == -ESRCH)
1468                 iscsi_cls_session_printk(KERN_ERR, session,
1469                                          "Cannot notify userspace of session "
1470                                          "event %u. Check iscsi daemon\n",
1471                                          event);
1472
1473         ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1474                                 event, rc);
1475         return rc;
1476 }
1477 EXPORT_SYMBOL_GPL(iscsi_session_event);
1478
1479 static int
1480 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1481                         struct iscsi_uevent *ev, uint32_t initial_cmdsn,
1482                         uint16_t cmds_max, uint16_t queue_depth)
1483 {
1484         struct iscsi_transport *transport = priv->iscsi_transport;
1485         struct iscsi_cls_session *session;
1486         struct Scsi_Host *shost;
1487
1488         session = transport->create_session(ep, cmds_max, queue_depth,
1489                                             initial_cmdsn);
1490         if (!session)
1491                 return -ENOMEM;
1492
1493         shost = iscsi_session_to_shost(session);
1494         ev->r.c_session_ret.host_no = shost->host_no;
1495         ev->r.c_session_ret.sid = session->sid;
1496         ISCSI_DBG_TRANS_SESSION(session,
1497                                 "Completed creating transport session\n");
1498         return 0;
1499 }
1500
1501 static int
1502 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1503 {
1504         struct iscsi_cls_conn *conn;
1505         struct iscsi_cls_session *session;
1506
1507         session = iscsi_session_lookup(ev->u.c_conn.sid);
1508         if (!session) {
1509                 printk(KERN_ERR "iscsi: invalid session %d.\n",
1510                        ev->u.c_conn.sid);
1511                 return -EINVAL;
1512         }
1513
1514         conn = transport->create_conn(session, ev->u.c_conn.cid);
1515         if (!conn) {
1516                 iscsi_cls_session_printk(KERN_ERR, session,
1517                                          "couldn't create a new connection.");
1518                 return -ENOMEM;
1519         }
1520
1521         ev->r.c_conn_ret.sid = session->sid;
1522         ev->r.c_conn_ret.cid = conn->cid;
1523
1524         ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
1525         return 0;
1526 }
1527
1528 static int
1529 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1530 {
1531         struct iscsi_cls_conn *conn;
1532
1533         conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1534         if (!conn)
1535                 return -EINVAL;
1536
1537         ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
1538         if (transport->destroy_conn)
1539                 transport->destroy_conn(conn);
1540
1541         return 0;
1542 }
1543
1544 static int
1545 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1546 {
1547         char *data = (char*)ev + sizeof(*ev);
1548         struct iscsi_cls_conn *conn;
1549         struct iscsi_cls_session *session;
1550         int err = 0, value = 0;
1551
1552         session = iscsi_session_lookup(ev->u.set_param.sid);
1553         conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1554         if (!conn || !session)
1555                 return -EINVAL;
1556
1557         switch (ev->u.set_param.param) {
1558         case ISCSI_PARAM_SESS_RECOVERY_TMO:
1559                 sscanf(data, "%d", &value);
1560                 session->recovery_tmo = value;
1561                 break;
1562         default:
1563                 err = transport->set_param(conn, ev->u.set_param.param,
1564                                            data, ev->u.set_param.len);
1565         }
1566
1567         return err;
1568 }
1569
1570 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1571                                struct iscsi_uevent *ev, int msg_type)
1572 {
1573         struct iscsi_endpoint *ep;
1574         struct sockaddr *dst_addr;
1575         struct Scsi_Host *shost = NULL;
1576         int non_blocking, err = 0;
1577
1578         if (!transport->ep_connect)
1579                 return -EINVAL;
1580
1581         if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1582                 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1583                 if (!shost) {
1584                         printk(KERN_ERR "ep connect failed. Could not find "
1585                                "host no %u\n",
1586                                ev->u.ep_connect_through_host.host_no);
1587                         return -ENODEV;
1588                 }
1589                 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1590         } else
1591                 non_blocking = ev->u.ep_connect.non_blocking;
1592
1593         dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1594         ep = transport->ep_connect(shost, dst_addr, non_blocking);
1595         if (IS_ERR(ep)) {
1596                 err = PTR_ERR(ep);
1597                 goto release_host;
1598         }
1599
1600         ev->r.ep_connect_ret.handle = ep->id;
1601 release_host:
1602         if (shost)
1603                 scsi_host_put(shost);
1604         return err;
1605 }
1606
1607 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1608                                   u64 ep_handle)
1609 {
1610         struct iscsi_cls_conn *conn;
1611         struct iscsi_endpoint *ep;
1612
1613         if (!transport->ep_disconnect)
1614                 return -EINVAL;
1615
1616         ep = iscsi_lookup_endpoint(ep_handle);
1617         if (!ep)
1618                 return -EINVAL;
1619         conn = ep->conn;
1620         if (conn) {
1621                 mutex_lock(&conn->ep_mutex);
1622                 conn->ep = NULL;
1623                 mutex_unlock(&conn->ep_mutex);
1624         }
1625
1626         transport->ep_disconnect(ep);
1627         return 0;
1628 }
1629
1630 static int
1631 iscsi_if_transport_ep(struct iscsi_transport *transport,
1632                       struct iscsi_uevent *ev, int msg_type)
1633 {
1634         struct iscsi_endpoint *ep;
1635         int rc = 0;
1636
1637         switch (msg_type) {
1638         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
1639         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1640                 rc = iscsi_if_ep_connect(transport, ev, msg_type);
1641                 break;
1642         case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1643                 if (!transport->ep_poll)
1644                         return -EINVAL;
1645
1646                 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1647                 if (!ep)
1648                         return -EINVAL;
1649
1650                 ev->r.retcode = transport->ep_poll(ep,
1651                                                    ev->u.ep_poll.timeout_ms);
1652                 break;
1653         case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1654                 rc = iscsi_if_ep_disconnect(transport,
1655                                             ev->u.ep_disconnect.ep_handle);
1656                 break;
1657         }
1658         return rc;
1659 }
1660
1661 static int
1662 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1663                 struct iscsi_uevent *ev)
1664 {
1665         struct Scsi_Host *shost;
1666         struct sockaddr *dst_addr;
1667         int err;
1668
1669         if (!transport->tgt_dscvr)
1670                 return -EINVAL;
1671
1672         shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1673         if (!shost) {
1674                 printk(KERN_ERR "target discovery could not find host no %u\n",
1675                        ev->u.tgt_dscvr.host_no);
1676                 return -ENODEV;
1677         }
1678
1679
1680         dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1681         err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1682                                    ev->u.tgt_dscvr.enable, dst_addr);
1683         scsi_host_put(shost);
1684         return err;
1685 }
1686
1687 static int
1688 iscsi_set_host_param(struct iscsi_transport *transport,
1689                      struct iscsi_uevent *ev)
1690 {
1691         char *data = (char*)ev + sizeof(*ev);
1692         struct Scsi_Host *shost;
1693         int err;
1694
1695         if (!transport->set_host_param)
1696                 return -ENOSYS;
1697
1698         shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1699         if (!shost) {
1700                 printk(KERN_ERR "set_host_param could not find host no %u\n",
1701                        ev->u.set_host_param.host_no);
1702                 return -ENODEV;
1703         }
1704
1705         err = transport->set_host_param(shost, ev->u.set_host_param.param,
1706                                         data, ev->u.set_host_param.len);
1707         scsi_host_put(shost);
1708         return err;
1709 }
1710
1711 static int
1712 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1713 {
1714         struct Scsi_Host *shost;
1715         struct iscsi_path *params;
1716         int err;
1717
1718         if (!transport->set_path)
1719                 return -ENOSYS;
1720
1721         shost = scsi_host_lookup(ev->u.set_path.host_no);
1722         if (!shost) {
1723                 printk(KERN_ERR "set path could not find host no %u\n",
1724                        ev->u.set_path.host_no);
1725                 return -ENODEV;
1726         }
1727
1728         params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1729         err = transport->set_path(shost, params);
1730
1731         scsi_host_put(shost);
1732         return err;
1733 }
1734
1735 static int
1736 iscsi_set_iface_params(struct iscsi_transport *transport,
1737                        struct iscsi_uevent *ev)
1738 {
1739         char *data = (char *)ev + sizeof(*ev);
1740         struct Scsi_Host *shost;
1741         int err;
1742
1743         if (!transport->set_iface_param)
1744                 return -ENOSYS;
1745
1746         shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1747         if (!shost) {
1748                 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1749                        ev->u.set_iface_params.host_no);
1750                 return -ENODEV;
1751         }
1752
1753         err = transport->set_iface_param(shost, data,
1754                                          ev->u.set_iface_params.count);
1755         scsi_host_put(shost);
1756         return err;
1757 }
1758
1759 static int
1760 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
1761 {
1762         int err = 0;
1763         struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1764         struct iscsi_transport *transport = NULL;
1765         struct iscsi_internal *priv;
1766         struct iscsi_cls_session *session;
1767         struct iscsi_cls_conn *conn;
1768         struct iscsi_endpoint *ep = NULL;
1769
1770         if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1771                 *group = ISCSI_NL_GRP_UIP;
1772         else
1773                 *group = ISCSI_NL_GRP_ISCSID;
1774
1775         priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1776         if (!priv)
1777                 return -EINVAL;
1778         transport = priv->iscsi_transport;
1779
1780         if (!try_module_get(transport->owner))
1781                 return -EINVAL;
1782
1783         switch (nlh->nlmsg_type) {
1784         case ISCSI_UEVENT_CREATE_SESSION:
1785                 err = iscsi_if_create_session(priv, ep, ev,
1786                                               ev->u.c_session.initial_cmdsn,
1787                                               ev->u.c_session.cmds_max,
1788                                               ev->u.c_session.queue_depth);
1789                 break;
1790         case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1791                 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
1792                 if (!ep) {
1793                         err = -EINVAL;
1794                         break;
1795                 }
1796
1797                 err = iscsi_if_create_session(priv, ep, ev,
1798                                         ev->u.c_bound_session.initial_cmdsn,
1799                                         ev->u.c_bound_session.cmds_max,
1800                                         ev->u.c_bound_session.queue_depth);
1801                 break;
1802         case ISCSI_UEVENT_DESTROY_SESSION:
1803                 session = iscsi_session_lookup(ev->u.d_session.sid);
1804                 if (session)
1805                         transport->destroy_session(session);
1806                 else
1807                         err = -EINVAL;
1808                 break;
1809         case ISCSI_UEVENT_UNBIND_SESSION:
1810                 session = iscsi_session_lookup(ev->u.d_session.sid);
1811                 if (session)
1812                         scsi_queue_work(iscsi_session_to_shost(session),
1813                                         &session->unbind_work);
1814                 else
1815                         err = -EINVAL;
1816                 break;
1817         case ISCSI_UEVENT_CREATE_CONN:
1818                 err = iscsi_if_create_conn(transport, ev);
1819                 break;
1820         case ISCSI_UEVENT_DESTROY_CONN:
1821                 err = iscsi_if_destroy_conn(transport, ev);
1822                 break;
1823         case ISCSI_UEVENT_BIND_CONN:
1824                 session = iscsi_session_lookup(ev->u.b_conn.sid);
1825                 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1826
1827                 if (conn && conn->ep)
1828                         iscsi_if_ep_disconnect(transport, conn->ep->id);
1829
1830                 if (!session || !conn) {
1831                         err = -EINVAL;
1832                         break;
1833                 }
1834
1835                 ev->r.retcode = transport->bind_conn(session, conn,
1836                                                 ev->u.b_conn.transport_eph,
1837                                                 ev->u.b_conn.is_leading);
1838                 if (ev->r.retcode || !transport->ep_connect)
1839                         break;
1840
1841                 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
1842                 if (ep) {
1843                         ep->conn = conn;
1844
1845                         mutex_lock(&conn->ep_mutex);
1846                         conn->ep = ep;
1847                         mutex_unlock(&conn->ep_mutex);
1848                 } else
1849                         iscsi_cls_conn_printk(KERN_ERR, conn,
1850                                               "Could not set ep conn "
1851                                               "binding\n");
1852                 break;
1853         case ISCSI_UEVENT_SET_PARAM:
1854                 err = iscsi_set_param(transport, ev);
1855                 break;
1856         case ISCSI_UEVENT_START_CONN:
1857                 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1858                 if (conn)
1859                         ev->r.retcode = transport->start_conn(conn);
1860                 else
1861                         err = -EINVAL;
1862                 break;
1863         case ISCSI_UEVENT_STOP_CONN:
1864                 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1865                 if (conn)
1866                         transport->stop_conn(conn, ev->u.stop_conn.flag);
1867                 else
1868                         err = -EINVAL;
1869                 break;
1870         case ISCSI_UEVENT_SEND_PDU:
1871                 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1872                 if (conn)
1873                         ev->r.retcode = transport->send_pdu(conn,
1874                                 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1875                                 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1876                                 ev->u.send_pdu.data_size);
1877                 else
1878                         err = -EINVAL;
1879                 break;
1880         case ISCSI_UEVENT_GET_STATS:
1881                 err = iscsi_if_get_stats(transport, nlh);
1882                 break;
1883         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1884         case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1885         case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1886         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
1887                 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1888                 break;
1889         case ISCSI_UEVENT_TGT_DSCVR:
1890                 err = iscsi_tgt_dscvr(transport, ev);
1891                 break;
1892         case ISCSI_UEVENT_SET_HOST_PARAM:
1893                 err = iscsi_set_host_param(transport, ev);
1894                 break;
1895         case ISCSI_UEVENT_PATH_UPDATE:
1896                 err = iscsi_set_path(transport, ev);
1897                 break;
1898         case ISCSI_UEVENT_SET_IFACE_PARAMS:
1899                 err = iscsi_set_iface_params(transport, ev);
1900                 break;
1901         default:
1902                 err = -ENOSYS;
1903                 break;
1904         }
1905
1906         module_put(transport->owner);
1907         return err;
1908 }
1909
1910 /*
1911  * Get message from skb.  Each message is processed by iscsi_if_recv_msg.
1912  * Malformed skbs with wrong lengths or invalid creds are not processed.
1913  */
1914 static void
1915 iscsi_if_rx(struct sk_buff *skb)
1916 {
1917         mutex_lock(&rx_queue_mutex);
1918         while (skb->len >= NLMSG_SPACE(0)) {
1919                 int err;
1920                 uint32_t rlen;
1921                 struct nlmsghdr *nlh;
1922                 struct iscsi_uevent *ev;
1923                 uint32_t group;
1924
1925                 nlh = nlmsg_hdr(skb);
1926                 if (nlh->nlmsg_len < sizeof(*nlh) ||
1927                     skb->len < nlh->nlmsg_len) {
1928                         break;
1929                 }
1930
1931                 ev = NLMSG_DATA(nlh);
1932                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1933                 if (rlen > skb->len)
1934                         rlen = skb->len;
1935
1936                 err = iscsi_if_recv_msg(skb, nlh, &group);
1937                 if (err) {
1938                         ev->type = ISCSI_KEVENT_IF_ERROR;
1939                         ev->iferror = err;
1940                 }
1941                 do {
1942                         /*
1943                          * special case for GET_STATS:
1944                          * on success - sending reply and stats from
1945                          * inside of if_recv_msg(),
1946                          * on error - fall through.
1947                          */
1948                         if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1949                                 break;
1950                         err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
1951                                 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1952                 } while (err < 0 && err != -ECONNREFUSED);
1953                 skb_pull(skb, rlen);
1954         }
1955         mutex_unlock(&rx_queue_mutex);
1956 }
1957
1958 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store)              \
1959 struct device_attribute dev_attr_##_prefix##_##_name =  \
1960         __ATTR(_name,_mode,_show,_store)
1961
1962 /*
1963  * iSCSI connection attrs
1964  */
1965 #define iscsi_conn_attr_show(param)                                     \
1966 static ssize_t                                                          \
1967 show_conn_param_##param(struct device *dev,                             \
1968                         struct device_attribute *attr, char *buf)       \
1969 {                                                                       \
1970         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);   \
1971         struct iscsi_transport *t = conn->transport;                    \
1972         return t->get_conn_param(conn, param, buf);                     \
1973 }
1974
1975 #define iscsi_conn_attr(field, param)                                   \
1976         iscsi_conn_attr_show(param)                                     \
1977 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param,  \
1978                         NULL);
1979
1980 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1981 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1982 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1983 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1984 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1985 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1986 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1987 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1988 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1989 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1990 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1991
1992 #define iscsi_conn_ep_attr_show(param)                                  \
1993 static ssize_t show_conn_ep_param_##param(struct device *dev,           \
1994                                           struct device_attribute *attr,\
1995                                           char *buf)                    \
1996 {                                                                       \
1997         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);   \
1998         struct iscsi_transport *t = conn->transport;                    \
1999         struct iscsi_endpoint *ep;                                      \
2000         ssize_t rc;                                                     \
2001                                                                         \
2002         /*                                                              \
2003          * Need to make sure ep_disconnect does not free the LLD's      \
2004          * interconnect resources while we are trying to read them.     \
2005          */                                                             \
2006         mutex_lock(&conn->ep_mutex);                                    \
2007         ep = conn->ep;                                                  \
2008         if (!ep && t->ep_connect) {                                     \
2009                 mutex_unlock(&conn->ep_mutex);                          \
2010                 return -ENOTCONN;                                       \
2011         }                                                               \
2012                                                                         \
2013         if (ep)                                                         \
2014                 rc = t->get_ep_param(ep, param, buf);                   \
2015         else                                                            \
2016                 rc = t->get_conn_param(conn, param, buf);               \
2017         mutex_unlock(&conn->ep_mutex);                                  \
2018         return rc;                                                      \
2019 }
2020
2021 #define iscsi_conn_ep_attr(field, param)                                \
2022         iscsi_conn_ep_attr_show(param)                                  \
2023 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO,                           \
2024                         show_conn_ep_param_##param, NULL);
2025
2026 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2027 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2028
2029 static struct attribute *iscsi_conn_attrs[] = {
2030         &dev_attr_conn_max_recv_dlength.attr,
2031         &dev_attr_conn_max_xmit_dlength.attr,
2032         &dev_attr_conn_header_digest.attr,
2033         &dev_attr_conn_data_digest.attr,
2034         &dev_attr_conn_ifmarker.attr,
2035         &dev_attr_conn_ofmarker.attr,
2036         &dev_attr_conn_address.attr,
2037         &dev_attr_conn_port.attr,
2038         &dev_attr_conn_exp_statsn.attr,
2039         &dev_attr_conn_persistent_address.attr,
2040         &dev_attr_conn_persistent_port.attr,
2041         &dev_attr_conn_ping_tmo.attr,
2042         &dev_attr_conn_recv_tmo.attr,
2043         NULL,
2044 };
2045
2046 static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2047                                          struct attribute *attr, int i)
2048 {
2049         struct device *cdev = container_of(kobj, struct device, kobj);
2050         struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2051         struct iscsi_transport *t = conn->transport;
2052         int param;
2053
2054         if (attr == &dev_attr_conn_max_recv_dlength.attr)
2055                 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2056         else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2057                 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2058         else if (attr == &dev_attr_conn_header_digest.attr)
2059                 param = ISCSI_PARAM_HDRDGST_EN;
2060         else if (attr == &dev_attr_conn_data_digest.attr)
2061                 param = ISCSI_PARAM_DATADGST_EN;
2062         else if (attr == &dev_attr_conn_ifmarker.attr)
2063                 param = ISCSI_PARAM_IFMARKER_EN;
2064         else if (attr == &dev_attr_conn_ofmarker.attr)
2065                 param = ISCSI_PARAM_OFMARKER_EN;
2066         else if (attr == &dev_attr_conn_address.attr)
2067                 param = ISCSI_PARAM_CONN_ADDRESS;
2068         else if (attr == &dev_attr_conn_port.attr)
2069                 param = ISCSI_PARAM_CONN_PORT;
2070         else if (attr == &dev_attr_conn_exp_statsn.attr)
2071                 param = ISCSI_PARAM_EXP_STATSN;
2072         else if (attr == &dev_attr_conn_persistent_address.attr)
2073                 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2074         else if (attr == &dev_attr_conn_persistent_port.attr)
2075                 param = ISCSI_PARAM_PERSISTENT_PORT;
2076         else if (attr == &dev_attr_conn_ping_tmo.attr)
2077                 param = ISCSI_PARAM_PING_TMO;
2078         else if (attr == &dev_attr_conn_recv_tmo.attr)
2079                 param = ISCSI_PARAM_RECV_TMO;
2080         else {
2081                 WARN_ONCE(1, "Invalid conn attr");
2082                 return 0;
2083         }
2084
2085         return t->attr_is_visible(ISCSI_PARAM, param);
2086 }
2087
2088 static struct attribute_group iscsi_conn_group = {
2089         .attrs = iscsi_conn_attrs,
2090         .is_visible = iscsi_conn_attr_is_visible,
2091 };
2092
2093 /*
2094  * iSCSI session attrs
2095  */
2096 #define iscsi_session_attr_show(param, perm)                            \
2097 static ssize_t                                                          \
2098 show_session_param_##param(struct device *dev,                          \
2099                            struct device_attribute *attr, char *buf)    \
2100 {                                                                       \
2101         struct iscsi_cls_session *session =                             \
2102                 iscsi_dev_to_session(dev->parent);                      \
2103         struct iscsi_transport *t = session->transport;                 \
2104                                                                         \
2105         if (perm && !capable(CAP_SYS_ADMIN))                            \
2106                 return -EACCES;                                         \
2107         return t->get_session_param(session, param, buf);               \
2108 }
2109
2110 #define iscsi_session_attr(field, param, perm)                          \
2111         iscsi_session_attr_show(param, perm)                            \
2112 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
2113                         NULL);
2114 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2115 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2116 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2117 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2118 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2119 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2120 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2121 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2122 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2123 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2124 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2125 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2126 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2127 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
2128 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2129 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2130 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
2131 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
2132 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
2133 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2134 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
2135
2136 static ssize_t
2137 show_priv_session_state(struct device *dev, struct device_attribute *attr,
2138                         char *buf)
2139 {
2140         struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
2141         return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2142 }
2143 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2144                         NULL);
2145
2146 #define iscsi_priv_session_attr_show(field, format)                     \
2147 static ssize_t                                                          \
2148 show_priv_session_##field(struct device *dev,                           \
2149                           struct device_attribute *attr, char *buf)     \
2150 {                                                                       \
2151         struct iscsi_cls_session *session =                             \
2152                         iscsi_dev_to_session(dev->parent);              \
2153         if (session->field == -1)                                       \
2154                 return sprintf(buf, "off\n");                           \
2155         return sprintf(buf, format"\n", session->field);                \
2156 }
2157
2158 #define iscsi_priv_session_attr_store(field)                            \
2159 static ssize_t                                                          \
2160 store_priv_session_##field(struct device *dev,                          \
2161                            struct device_attribute *attr,               \
2162                            const char *buf, size_t count)               \
2163 {                                                                       \
2164         int val;                                                        \
2165         char *cp;                                                       \
2166         struct iscsi_cls_session *session =                             \
2167                 iscsi_dev_to_session(dev->parent);                      \
2168         if ((session->state == ISCSI_SESSION_FREE) ||                   \
2169             (session->state == ISCSI_SESSION_FAILED))                   \
2170                 return -EBUSY;                                          \
2171         if (strncmp(buf, "off", 3) == 0)                                \
2172                 session->field = -1;                                    \
2173         else {                                                          \
2174                 val = simple_strtoul(buf, &cp, 0);                      \
2175                 if (*cp != '\0' && *cp != '\n')                         \
2176                         return -EINVAL;                                 \
2177                 session->field = val;                                   \
2178         }                                                               \
2179         return count;                                                   \
2180 }
2181
2182 #define iscsi_priv_session_rw_attr(field, format)                       \
2183         iscsi_priv_session_attr_show(field, format)                     \
2184         iscsi_priv_session_attr_store(field)                            \
2185 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR,            \
2186                         show_priv_session_##field,                      \
2187                         store_priv_session_##field)
2188 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
2189
2190 static struct attribute *iscsi_session_attrs[] = {
2191         &dev_attr_sess_initial_r2t.attr,
2192         &dev_attr_sess_max_outstanding_r2t.attr,
2193         &dev_attr_sess_immediate_data.attr,
2194         &dev_attr_sess_first_burst_len.attr,
2195         &dev_attr_sess_max_burst_len.attr,
2196         &dev_attr_sess_data_pdu_in_order.attr,
2197         &dev_attr_sess_data_seq_in_order.attr,
2198         &dev_attr_sess_erl.attr,
2199         &dev_attr_sess_targetname.attr,
2200         &dev_attr_sess_tpgt.attr,
2201         &dev_attr_sess_password.attr,
2202         &dev_attr_sess_password_in.attr,
2203         &dev_attr_sess_username.attr,
2204         &dev_attr_sess_username_in.attr,
2205         &dev_attr_sess_fast_abort.attr,
2206         &dev_attr_sess_abort_tmo.attr,
2207         &dev_attr_sess_lu_reset_tmo.attr,
2208         &dev_attr_sess_tgt_reset_tmo.attr,
2209         &dev_attr_sess_ifacename.attr,
2210         &dev_attr_sess_initiatorname.attr,
2211         &dev_attr_sess_targetalias.attr,
2212         &dev_attr_priv_sess_recovery_tmo.attr,
2213         &dev_attr_priv_sess_state.attr,
2214         NULL,
2215 };
2216
2217 static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2218                                             struct attribute *attr, int i)
2219 {
2220         struct device *cdev = container_of(kobj, struct device, kobj);
2221         struct iscsi_cls_session *session = transport_class_to_session(cdev);
2222         struct iscsi_transport *t = session->transport;
2223         int param;
2224
2225         if (attr == &dev_attr_sess_initial_r2t.attr)
2226                 param = ISCSI_PARAM_INITIAL_R2T_EN;
2227         else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2228                 param = ISCSI_PARAM_MAX_R2T;
2229         else if (attr == &dev_attr_sess_immediate_data.attr)
2230                 param = ISCSI_PARAM_IMM_DATA_EN;
2231         else if (attr == &dev_attr_sess_first_burst_len.attr)
2232                 param = ISCSI_PARAM_FIRST_BURST;
2233         else if (attr == &dev_attr_sess_max_burst_len.attr)
2234                 param = ISCSI_PARAM_MAX_BURST;
2235         else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2236                 param = ISCSI_PARAM_PDU_INORDER_EN;
2237         else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2238                 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2239         else if (attr == &dev_attr_sess_erl.attr)
2240                 param = ISCSI_PARAM_ERL;
2241         else if (attr == &dev_attr_sess_targetname.attr)
2242                 param = ISCSI_PARAM_TARGET_NAME;
2243         else if (attr == &dev_attr_sess_tpgt.attr)
2244                 param = ISCSI_PARAM_TPGT;
2245         else if (attr == &dev_attr_sess_password.attr)
2246                 param = ISCSI_PARAM_USERNAME;
2247         else if (attr == &dev_attr_sess_password_in.attr)
2248                 param = ISCSI_PARAM_USERNAME_IN;
2249         else if (attr == &dev_attr_sess_username.attr)
2250                 param = ISCSI_PARAM_PASSWORD;
2251         else if (attr == &dev_attr_sess_username_in.attr)
2252                 param = ISCSI_PARAM_PASSWORD_IN;
2253         else if (attr == &dev_attr_sess_fast_abort.attr)
2254                 param = ISCSI_PARAM_FAST_ABORT;
2255         else if (attr == &dev_attr_sess_abort_tmo.attr)
2256                 param = ISCSI_PARAM_ABORT_TMO;
2257         else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2258                 param = ISCSI_PARAM_LU_RESET_TMO;
2259         else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2260                 param = ISCSI_PARAM_TGT_RESET_TMO;
2261         else if (attr == &dev_attr_sess_ifacename.attr)
2262                 param = ISCSI_PARAM_IFACE_NAME;
2263         else if (attr == &dev_attr_sess_initiatorname.attr)
2264                 param = ISCSI_PARAM_INITIATOR_NAME;
2265         else if (attr == &dev_attr_sess_targetalias.attr)
2266                 param = ISCSI_PARAM_TARGET_ALIAS;
2267         else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2268                 return S_IRUGO | S_IWUSR;
2269         else if (attr == &dev_attr_priv_sess_state.attr)
2270                 return S_IRUGO;
2271         else {
2272                 WARN_ONCE(1, "Invalid session attr");
2273                 return 0;
2274         }
2275
2276         return t->attr_is_visible(ISCSI_PARAM, param);
2277 }
2278
2279 static struct attribute_group iscsi_session_group = {
2280         .attrs = iscsi_session_attrs,
2281         .is_visible = iscsi_session_attr_is_visible,
2282 };
2283
2284 /*
2285  * iSCSI host attrs
2286  */
2287 #define iscsi_host_attr_show(param)                                     \
2288 static ssize_t                                                          \
2289 show_host_param_##param(struct device *dev,                             \
2290                         struct device_attribute *attr, char *buf)       \
2291 {                                                                       \
2292         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
2293         struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2294         return priv->iscsi_transport->get_host_param(shost, param, buf); \
2295 }
2296
2297 #define iscsi_host_attr(field, param)                                   \
2298         iscsi_host_attr_show(param)                                     \
2299 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param,  \
2300                         NULL);
2301
2302 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
2303 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
2304 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
2305 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
2306
2307 #define SETUP_HOST_RD_ATTR(field, param_flag)                           \
2308 do {                                                                    \
2309         if (tt->host_param_mask & param_flag) {                         \
2310                 priv->host_attrs[count] = &dev_attr_host_##field; \
2311                 count++;                                                \
2312         }                                                               \
2313 } while (0)
2314
2315 static int iscsi_session_match(struct attribute_container *cont,
2316                            struct device *dev)
2317 {
2318         struct iscsi_cls_session *session;
2319         struct Scsi_Host *shost;
2320         struct iscsi_internal *priv;
2321
2322         if (!iscsi_is_session_dev(dev))
2323                 return 0;
2324
2325         session = iscsi_dev_to_session(dev);
2326         shost = iscsi_session_to_shost(session);
2327         if (!shost->transportt)
2328                 return 0;
2329
2330         priv = to_iscsi_internal(shost->transportt);
2331         if (priv->session_cont.ac.class != &iscsi_session_class.class)
2332                 return 0;
2333
2334         return &priv->session_cont.ac == cont;
2335 }
2336
2337 static int iscsi_conn_match(struct attribute_container *cont,
2338                            struct device *dev)
2339 {
2340         struct iscsi_cls_session *session;
2341         struct iscsi_cls_conn *conn;
2342         struct Scsi_Host *shost;
2343         struct iscsi_internal *priv;
2344
2345         if (!iscsi_is_conn_dev(dev))
2346                 return 0;
2347
2348         conn = iscsi_dev_to_conn(dev);
2349         session = iscsi_dev_to_session(conn->dev.parent);
2350         shost = iscsi_session_to_shost(session);
2351
2352         if (!shost->transportt)
2353                 return 0;
2354
2355         priv = to_iscsi_internal(shost->transportt);
2356         if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2357                 return 0;
2358
2359         return &priv->conn_cont.ac == cont;
2360 }
2361
2362 static int iscsi_host_match(struct attribute_container *cont,
2363                             struct device *dev)
2364 {
2365         struct Scsi_Host *shost;
2366         struct iscsi_internal *priv;
2367
2368         if (!scsi_is_host_device(dev))
2369                 return 0;
2370
2371         shost = dev_to_shost(dev);
2372         if (!shost->transportt  ||
2373             shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2374                 return 0;
2375
2376         priv = to_iscsi_internal(shost->transportt);
2377         return &priv->t.host_attrs.ac == cont;
2378 }
2379
2380 struct scsi_transport_template *
2381 iscsi_register_transport(struct iscsi_transport *tt)
2382 {
2383         struct iscsi_internal *priv;
2384         unsigned long flags;
2385         int count = 0, err;
2386
2387         BUG_ON(!tt);
2388
2389         priv = iscsi_if_transport_lookup(tt);
2390         if (priv)
2391                 return NULL;
2392
2393         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2394         if (!priv)
2395                 return NULL;
2396         INIT_LIST_HEAD(&priv->list);
2397         priv->iscsi_transport = tt;
2398         priv->t.user_scan = iscsi_user_scan;
2399         priv->t.create_work_queue = 1;
2400
2401         priv->dev.class = &iscsi_transport_class;
2402         dev_set_name(&priv->dev, "%s", tt->name);
2403         err = device_register(&priv->dev);
2404         if (err)
2405                 goto free_priv;
2406
2407         err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
2408         if (err)
2409                 goto unregister_dev;
2410
2411         /* host parameters */
2412         priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
2413         priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2414         priv->t.host_attrs.ac.match = iscsi_host_match;
2415         priv->t.host_size = sizeof(struct iscsi_cls_host);
2416         transport_container_register(&priv->t.host_attrs);
2417
2418         SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
2419         SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
2420         SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
2421         SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
2422         BUG_ON(count > ISCSI_HOST_ATTRS);
2423         priv->host_attrs[count] = NULL;
2424         count = 0;
2425
2426         /* connection parameters */
2427         priv->conn_cont.ac.class = &iscsi_connection_class.class;
2428         priv->conn_cont.ac.match = iscsi_conn_match;
2429         priv->conn_cont.ac.grp = &iscsi_conn_group;
2430         transport_container_register(&priv->conn_cont);
2431
2432         /* session parameters */
2433         priv->session_cont.ac.class = &iscsi_session_class.class;
2434         priv->session_cont.ac.match = iscsi_session_match;
2435         priv->session_cont.ac.grp = &iscsi_session_group;
2436         transport_container_register(&priv->session_cont);
2437
2438         spin_lock_irqsave(&iscsi_transport_lock, flags);
2439         list_add(&priv->list, &iscsi_transports);
2440         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2441
2442         printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
2443         return &priv->t;
2444
2445 unregister_dev:
2446         device_unregister(&priv->dev);
2447         return NULL;
2448 free_priv:
2449         kfree(priv);
2450         return NULL;
2451 }
2452 EXPORT_SYMBOL_GPL(iscsi_register_transport);
2453
2454 int iscsi_unregister_transport(struct iscsi_transport *tt)
2455 {
2456         struct iscsi_internal *priv;
2457         unsigned long flags;
2458
2459         BUG_ON(!tt);
2460
2461         mutex_lock(&rx_queue_mutex);
2462
2463         priv = iscsi_if_transport_lookup(tt);
2464         BUG_ON (!priv);
2465
2466         spin_lock_irqsave(&iscsi_transport_lock, flags);
2467         list_del(&priv->list);
2468         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2469
2470         transport_container_unregister(&priv->conn_cont);
2471         transport_container_unregister(&priv->session_cont);
2472         transport_container_unregister(&priv->t.host_attrs);
2473
2474         sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2475         device_unregister(&priv->dev);
2476         mutex_unlock(&rx_queue_mutex);
2477
2478         return 0;
2479 }
2480 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2481
2482 static __init int iscsi_transport_init(void)
2483 {
2484         int err;
2485
2486         printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
2487                 ISCSI_TRANSPORT_VERSION);
2488
2489         atomic_set(&iscsi_session_nr, 0);
2490
2491         err = class_register(&iscsi_transport_class);
2492         if (err)
2493                 return err;
2494
2495         err = class_register(&iscsi_endpoint_class);
2496         if (err)
2497                 goto unregister_transport_class;
2498
2499         err = class_register(&iscsi_iface_class);
2500         if (err)
2501                 goto unregister_endpoint_class;
2502
2503         err = transport_class_register(&iscsi_host_class);
2504         if (err)
2505                 goto unregister_iface_class;
2506
2507         err = transport_class_register(&iscsi_connection_class);
2508         if (err)
2509                 goto unregister_host_class;
2510
2511         err = transport_class_register(&iscsi_session_class);
2512         if (err)
2513                 goto unregister_conn_class;
2514
2515         nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2516                                     NULL, THIS_MODULE);
2517         if (!nls) {
2518                 err = -ENOBUFS;
2519                 goto unregister_session_class;
2520         }
2521
2522         iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2523         if (!iscsi_eh_timer_workq)
2524                 goto release_nls;
2525
2526         return 0;
2527
2528 release_nls:
2529         netlink_kernel_release(nls);
2530 unregister_session_class:
2531         transport_class_unregister(&iscsi_session_class);
2532 unregister_conn_class:
2533         transport_class_unregister(&iscsi_connection_class);
2534 unregister_host_class:
2535         transport_class_unregister(&iscsi_host_class);
2536 unregister_iface_class:
2537         class_unregister(&iscsi_iface_class);
2538 unregister_endpoint_class:
2539         class_unregister(&iscsi_endpoint_class);
2540 unregister_transport_class:
2541         class_unregister(&iscsi_transport_class);
2542         return err;
2543 }
2544
2545 static void __exit iscsi_transport_exit(void)
2546 {
2547         destroy_workqueue(iscsi_eh_timer_workq);
2548         netlink_kernel_release(nls);
2549         transport_class_unregister(&iscsi_connection_class);
2550         transport_class_unregister(&iscsi_session_class);
2551         transport_class_unregister(&iscsi_host_class);
2552         class_unregister(&iscsi_endpoint_class);
2553         class_unregister(&iscsi_iface_class);
2554         class_unregister(&iscsi_transport_class);
2555 }
2556
2557 module_init(iscsi_transport_init);
2558 module_exit(iscsi_transport_exit);
2559
2560 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2561               "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2562               "Alex Aizman <itn780@yahoo.com>");
2563 MODULE_DESCRIPTION("iSCSI Transport Interface");
2564 MODULE_LICENSE("GPL");
2565 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
2566 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);