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