blob: 2adfab8c11c1bd541e840094aa6730823a5d18b5 [file] [log] [blame]
Alex Aizman0896b752005-08-04 19:33:07 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07005 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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>
Arjan van de Ven0b950672006-01-11 13:16:10 +010024#include <linux/mutex.h>
Alex Aizman0896b752005-08-04 19:33:07 -070025#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <scsi/scsi.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_device.h>
29#include <scsi/scsi_transport.h>
30#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070031#include <scsi/iscsi_if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Mike Christie88dfd342008-05-21 15:54:16 -050033#define ISCSI_SESSION_ATTRS 21
Mike Christiec238c3b2008-01-31 13:36:51 -060034#define ISCSI_CONN_ATTRS 13
Mike Christied8196ed2007-05-30 12:57:25 -050035#define ISCSI_HOST_ATTRS 4
Mike Christied82ff9be2008-05-21 15:54:13 -050036
Mike Christie4c2133c2008-06-16 10:11:34 -050037#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39struct iscsi_internal {
Mike Christie790f39a2006-05-18 20:31:39 -050040 int daemon_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070042 struct iscsi_transport *iscsi_transport;
43 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010044 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050045
Tony Jonesee959b02008-02-22 00:13:36 +010046 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070047 struct transport_container conn_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010048 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070049 struct transport_container session_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010050 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Mike Christie41be1442007-02-28 17:32:18 -060053static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060054static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050055
Alex Aizman0896b752005-08-04 19:33:07 -070056/*
57 * list of registered transports and lock that must
58 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010059 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070060 */
61static LIST_HEAD(iscsi_transports);
62static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alex Aizman0896b752005-08-04 19:33:07 -070064#define to_iscsi_internal(tmpl) \
65 container_of(tmpl, struct iscsi_internal, t)
66
Tony Jonesee959b02008-02-22 00:13:36 +010067#define dev_to_iscsi_internal(_dev) \
68 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070069
Tony Jonesee959b02008-02-22 00:13:36 +010070static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -070071{
Tony Jonesee959b02008-02-22 00:13:36 +010072 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -070073 kfree(priv);
74}
75
76/*
77 * iscsi_transport_class represents the iscsi_transports that are
78 * registered.
79 */
80static struct class iscsi_transport_class = {
81 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +010082 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -070083};
84
85static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +010086show_transport_handle(struct device *dev, struct device_attribute *attr,
87 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -070088{
Tony Jonesee959b02008-02-22 00:13:36 +010089 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -050090 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070091}
Tony Jonesee959b02008-02-22 00:13:36 +010092static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -070093
94#define show_transport_attr(name, format) \
95static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +010096show_transport_##name(struct device *dev, \
97 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -070098{ \
Tony Jonesee959b02008-02-22 00:13:36 +010099 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700100 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
101} \
Tony Jonesee959b02008-02-22 00:13:36 +0100102static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700103
104show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700105
106static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100107 &dev_attr_handle.attr,
108 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700109 NULL,
110};
111
112static struct attribute_group iscsi_transport_group = {
113 .attrs = iscsi_transport_attrs,
114};
115
Mike Christied82ff9be2008-05-21 15:54:13 -0500116/*
117 * iSCSI endpoint attrs
118 */
119#define iscsi_dev_to_endpoint(_dev) \
120 container_of(_dev, struct iscsi_endpoint, dev)
121
122#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
123struct device_attribute dev_attr_##_prefix##_##_name = \
124 __ATTR(_name,_mode,_show,_store)
125
126static void iscsi_endpoint_release(struct device *dev)
127{
128 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
129 kfree(ep);
130}
131
132static struct class iscsi_endpoint_class = {
133 .name = "iscsi_endpoint",
134 .dev_release = iscsi_endpoint_release,
135};
136
137static ssize_t
138show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
139{
140 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500141 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500142}
143static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
144
145static struct attribute *iscsi_endpoint_attrs[] = {
146 &dev_attr_ep_handle.attr,
147 NULL,
148};
149
150static struct attribute_group iscsi_endpoint_group = {
151 .attrs = iscsi_endpoint_attrs,
152};
153
154#define ISCSI_MAX_EPID -1
155
156static int iscsi_match_epid(struct device *dev, void *data)
157{
158 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500159 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500160
161 return *epid == ep->id;
162}
163
164struct iscsi_endpoint *
165iscsi_create_endpoint(int dd_size)
166{
167 struct device *dev;
168 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500169 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500170 int err;
171
172 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400173 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500174 iscsi_match_epid);
175 if (!dev)
176 break;
177 }
178 if (id == ISCSI_MAX_EPID) {
179 printk(KERN_ERR "Too many connections. Max supported %u\n",
180 ISCSI_MAX_EPID - 1);
181 return NULL;
182 }
183
184 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
185 if (!ep)
186 return NULL;
187
188 ep->id = id;
189 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100190 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500191 err = device_register(&ep->dev);
192 if (err)
193 goto free_ep;
194
195 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
196 if (err)
197 goto unregister_dev;
198
199 if (dd_size)
200 ep->dd_data = &ep[1];
201 return ep;
202
203unregister_dev:
204 device_unregister(&ep->dev);
205 return NULL;
206
207free_ep:
208 kfree(ep);
209 return NULL;
210}
211EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
212
213void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
214{
215 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
216 device_unregister(&ep->dev);
217}
218EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
219
220struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
221{
Mike Christief80f8682008-06-16 10:11:35 -0500222 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500223 struct device *dev;
224
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400225 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500226 iscsi_match_epid);
227 if (!dev)
228 return NULL;
229
Mike Christief80f8682008-06-16 10:11:35 -0500230 ep = iscsi_dev_to_endpoint(dev);
231 /*
232 * we can drop this now because the interface will prevent
233 * removals and lookups from racing.
234 */
235 put_device(dev);
236 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500237}
238EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
239
Mike Christie30a6c652006-04-06 21:13:39 -0500240static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100241 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500242{
243 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500244 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500245
246 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600247 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500248 mutex_init(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600249
Kay Sieversaab0de22008-05-02 06:02:41 +0200250 snprintf(ihost->scan_workq_name, sizeof(ihost->scan_workq_name),
251 "iscsi_scan_%d", shost->host_no);
Mike Christiebd976f62008-01-31 13:36:46 -0600252 ihost->scan_workq = create_singlethread_workqueue(
253 ihost->scan_workq_name);
254 if (!ihost->scan_workq)
Mike Christie26974782007-12-13 12:43:29 -0600255 return -ENOMEM;
256 return 0;
257}
258
259static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100260 struct device *cdev)
Mike Christie26974782007-12-13 12:43:29 -0600261{
262 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500263 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600264
Mike Christiebd976f62008-01-31 13:36:46 -0600265 destroy_workqueue(ihost->scan_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500266 return 0;
267}
268
269static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
270 "iscsi_host",
271 iscsi_setup_host,
Mike Christie26974782007-12-13 12:43:29 -0600272 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500273 NULL);
274
Alex Aizman0896b752005-08-04 19:33:07 -0700275static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
276 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 NULL,
278 NULL,
279 NULL);
280
Alex Aizman0896b752005-08-04 19:33:07 -0700281static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
282 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 NULL,
284 NULL,
285 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700286
287static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100288static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700289
Mike Christie7b7232f2006-02-01 21:06:49 -0600290static LIST_HEAD(sesslist);
291static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700292static LIST_HEAD(connlist);
293static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Mike Christieb5c7a122006-04-06 21:13:33 -0500295static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
296{
297 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
298 return sess->sid;
299}
300
301/*
302 * Returns the matching session to a given sid
303 */
304static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600305{
306 unsigned long flags;
307 struct iscsi_cls_session *sess;
308
309 spin_lock_irqsave(&sesslock, flags);
310 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500311 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600312 spin_unlock_irqrestore(&sesslock, flags);
313 return sess;
314 }
315 }
316 spin_unlock_irqrestore(&sesslock, flags);
317 return NULL;
318}
319
Mike Christieb5c7a122006-04-06 21:13:33 -0500320/*
321 * Returns the matching connection to a given sid / cid tuple
322 */
323static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600324{
325 unsigned long flags;
326 struct iscsi_cls_conn *conn;
327
328 spin_lock_irqsave(&connlock, flags);
329 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500330 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600331 spin_unlock_irqrestore(&connlock, flags);
332 return conn;
333 }
334 }
335 spin_unlock_irqrestore(&connlock, flags);
336 return NULL;
337}
338
Mike Christie7b8631b2006-01-13 18:05:50 -0600339/*
340 * The following functions can be used by LLDs that allocate
341 * their own scsi_hosts or by software iscsi LLDs
342 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600343static struct {
344 int value;
345 char *name;
346} iscsi_session_state_names[] = {
347 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
348 { ISCSI_SESSION_FAILED, "FAILED" },
349 { ISCSI_SESSION_FREE, "FREE" },
350};
351
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200352static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600353{
354 int i;
355 char *name = NULL;
356
357 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
358 if (iscsi_session_state_names[i].value == state) {
359 name = iscsi_session_state_names[i].name;
360 break;
361 }
362 }
363 return name;
364}
365
366int iscsi_session_chkready(struct iscsi_cls_session *session)
367{
368 unsigned long flags;
369 int err;
370
371 spin_lock_irqsave(&session->lock, flags);
372 switch (session->state) {
373 case ISCSI_SESSION_LOGGED_IN:
374 err = 0;
375 break;
376 case ISCSI_SESSION_FAILED:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500377 err = DID_TRANSPORT_DISRUPTED << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600378 break;
379 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500380 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600381 break;
382 default:
383 err = DID_NO_CONNECT << 16;
384 break;
385 }
386 spin_unlock_irqrestore(&session->lock, flags);
387 return err;
388}
389EXPORT_SYMBOL_GPL(iscsi_session_chkready);
390
Mike Christie7b8631b2006-01-13 18:05:50 -0600391static void iscsi_session_release(struct device *dev)
392{
393 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600394 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Mike Christie7b8631b2006-01-13 18:05:50 -0600396 shost = iscsi_session_to_shost(session);
397 scsi_host_put(shost);
398 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600399}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Mike Christie7b8631b2006-01-13 18:05:50 -0600401static int iscsi_is_session_dev(const struct device *dev)
402{
403 return dev->release == iscsi_session_release;
404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Mike Christiea4804cd2008-05-21 15:54:00 -0500406static int iscsi_iter_session_fn(struct device *dev, void *data)
407{
408 void (* fn) (struct iscsi_cls_session *) = data;
409
410 if (!iscsi_is_session_dev(dev))
411 return 0;
412 fn(iscsi_dev_to_session(dev));
413 return 0;
414}
415
416void iscsi_host_for_each_session(struct Scsi_Host *shost,
417 void (*fn)(struct iscsi_cls_session *))
418{
419 device_for_each_child(&shost->shost_gendev, fn,
420 iscsi_iter_session_fn);
421}
422EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
423
Mike Christie8aae18a2008-01-31 13:36:48 -0600424/**
425 * iscsi_scan_finished - helper to report when running scans are done
426 * @shost: scsi host
427 * @time: scan run time
428 *
429 * This function can be used by drives like qla4xxx to report to the scsi
430 * layer when the scans it kicked off at module load time are done.
431 */
432int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
433{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500434 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600435 /*
436 * qla4xxx will have kicked off some session unblocks before calling
437 * scsi_scan_host, so just wait for them to complete.
438 */
439 return !atomic_read(&ihost->nr_scans);
440}
441EXPORT_SYMBOL_GPL(iscsi_scan_finished);
442
Mike Christie79706342008-05-21 15:54:12 -0500443struct iscsi_scan_data {
444 unsigned int channel;
445 unsigned int id;
446 unsigned int lun;
447};
448
449static int iscsi_user_scan_session(struct device *dev, void *data)
450{
451 struct iscsi_scan_data *scan_data = data;
452 struct iscsi_cls_session *session;
453 struct Scsi_Host *shost;
454 struct iscsi_cls_host *ihost;
455 unsigned long flags;
456 unsigned int id;
457
458 if (!iscsi_is_session_dev(dev))
459 return 0;
460
461 session = iscsi_dev_to_session(dev);
462 shost = iscsi_session_to_shost(session);
463 ihost = shost->shost_data;
464
465 mutex_lock(&ihost->mutex);
466 spin_lock_irqsave(&session->lock, flags);
467 if (session->state != ISCSI_SESSION_LOGGED_IN) {
468 spin_unlock_irqrestore(&session->lock, flags);
469 mutex_unlock(&ihost->mutex);
470 return 0;
471 }
472 id = session->target_id;
473 spin_unlock_irqrestore(&session->lock, flags);
474
475 if (id != ISCSI_MAX_TARGET) {
476 if ((scan_data->channel == SCAN_WILD_CARD ||
477 scan_data->channel == 0) &&
478 (scan_data->id == SCAN_WILD_CARD ||
479 scan_data->id == id))
480 scsi_scan_target(&session->dev, 0, id,
481 scan_data->lun, 1);
482 }
483 mutex_unlock(&ihost->mutex);
484 return 0;
485}
486
Mike Christie30a6c652006-04-06 21:13:39 -0500487static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
488 uint id, uint lun)
489{
Mike Christie79706342008-05-21 15:54:12 -0500490 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500491
Mike Christie79706342008-05-21 15:54:12 -0500492 scan_data.channel = channel;
493 scan_data.id = id;
494 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500495
Mike Christie79706342008-05-21 15:54:12 -0500496 return device_for_each_child(&shost->shost_gendev, &scan_data,
497 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500498}
499
Mike Christiebd976f62008-01-31 13:36:46 -0600500static void iscsi_scan_session(struct work_struct *work)
501{
502 struct iscsi_cls_session *session =
503 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600504 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500505 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500506 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600507
Mike Christie79706342008-05-21 15:54:12 -0500508 scan_data.channel = 0;
509 scan_data.id = SCAN_WILD_CARD;
510 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600511
Mike Christie79706342008-05-21 15:54:12 -0500512 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600513 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600514}
515
David Howellsc4028952006-11-22 14:57:56 +0000516static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500517{
David Howellsc4028952006-11-22 14:57:56 +0000518 struct iscsi_cls_session *session =
519 container_of(work, struct iscsi_cls_session,
520 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600521 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500522
Mike Christie322d7392008-01-31 13:36:52 -0600523 iscsi_cls_session_printk(KERN_INFO, session,
524 "session recovery timed out after %d secs\n",
525 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500526
Mike Christie6eabafb2008-01-31 13:36:43 -0600527 spin_lock_irqsave(&session->lock, flags);
528 switch (session->state) {
529 case ISCSI_SESSION_FAILED:
530 session->state = ISCSI_SESSION_FREE;
531 break;
532 case ISCSI_SESSION_LOGGED_IN:
533 case ISCSI_SESSION_FREE:
534 /* we raced with the unblock's flush */
535 spin_unlock_irqrestore(&session->lock, flags);
536 return;
537 }
538 spin_unlock_irqrestore(&session->lock, flags);
539
Mike Christie30a6c652006-04-06 21:13:39 -0500540 if (session->transport->session_recovery_timedout)
541 session->transport->session_recovery_timedout(session);
542
543 scsi_target_unblock(&session->dev);
544}
545
Mike Christie45ab33b2008-03-04 13:26:55 -0600546static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500547{
Mike Christie45ab33b2008-03-04 13:26:55 -0600548 struct iscsi_cls_session *session =
549 container_of(work, struct iscsi_cls_session,
550 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600551 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500552 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600553 unsigned long flags;
554
Mike Christie45ab33b2008-03-04 13:26:55 -0600555 /*
556 * The recovery and unblock work get run from the same workqueue,
557 * so try to cancel it if it was going to run after this unblock.
558 */
559 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600560 spin_lock_irqsave(&session->lock, flags);
561 session->state = ISCSI_SESSION_LOGGED_IN;
562 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600563 /* start IO */
564 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600565 /*
566 * Only do kernel scanning if the driver is properly hooked into
567 * the async scanning code (drivers like iscsi_tcp do login and
568 * scanning from userspace).
569 */
570 if (shost->hostt->scan_finished) {
571 if (queue_work(ihost->scan_workq, &session->scan_work))
572 atomic_inc(&ihost->nr_scans);
573 }
Mike Christie6eabafb2008-01-31 13:36:43 -0600574}
Mike Christie45ab33b2008-03-04 13:26:55 -0600575
576/**
577 * iscsi_unblock_session - set a session as logged in and start IO.
578 * @session: iscsi session
579 *
580 * Mark a session as ready to accept IO.
581 */
582void iscsi_unblock_session(struct iscsi_cls_session *session)
583{
584 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
585 /*
586 * make sure all the events have completed before tell the driver
587 * it is safe
588 */
589 flush_workqueue(iscsi_eh_timer_workq);
590}
Mike Christie30a6c652006-04-06 21:13:39 -0500591EXPORT_SYMBOL_GPL(iscsi_unblock_session);
592
Mike Christie45ab33b2008-03-04 13:26:55 -0600593static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500594{
Mike Christie45ab33b2008-03-04 13:26:55 -0600595 struct iscsi_cls_session *session =
596 container_of(work, struct iscsi_cls_session,
597 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600598 unsigned long flags;
599
600 spin_lock_irqsave(&session->lock, flags);
601 session->state = ISCSI_SESSION_FAILED;
602 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500603 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600604 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
605 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500606}
Mike Christie45ab33b2008-03-04 13:26:55 -0600607
608void iscsi_block_session(struct iscsi_cls_session *session)
609{
610 queue_work(iscsi_eh_timer_workq, &session->block_work);
611}
Mike Christie30a6c652006-04-06 21:13:39 -0500612EXPORT_SYMBOL_GPL(iscsi_block_session);
613
Mike Christie26974782007-12-13 12:43:29 -0600614static void __iscsi_unbind_session(struct work_struct *work)
615{
616 struct iscsi_cls_session *session =
617 container_of(work, struct iscsi_cls_session,
618 unbind_work);
619 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500620 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500621 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600622
623 /* Prevent new scans and make sure scanning is not in progress */
624 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500625 spin_lock_irqsave(&session->lock, flags);
626 if (session->target_id == ISCSI_MAX_TARGET) {
627 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600628 mutex_unlock(&ihost->mutex);
629 return;
630 }
Mike Christie79706342008-05-21 15:54:12 -0500631 session->target_id = ISCSI_MAX_TARGET;
632 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600633 mutex_unlock(&ihost->mutex);
634
635 scsi_remove_target(&session->dev);
636 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
637}
638
639static int iscsi_unbind_session(struct iscsi_cls_session *session)
640{
641 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500642 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600643
Mike Christiebd976f62008-01-31 13:36:46 -0600644 return queue_work(ihost->scan_workq, &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -0600645}
646
Mike Christie7b8631b2006-01-13 18:05:50 -0600647struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500648iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
649 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600650{
651 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Mike Christie5d91e202008-05-21 15:54:01 -0500653 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500654 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600655 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500656 return NULL;
657
Mike Christie7b8631b2006-01-13 18:05:50 -0600658 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500659 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600660 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000661 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500662 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600663 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
664 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600665 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600666 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600667 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600668
Mike Christie6a8a0d32006-06-28 12:00:31 -0500669 /* this is released in the dev's release function */
670 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500671 session->dev.parent = &shost->shost_gendev;
672 session->dev.release = iscsi_session_release;
673 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500674 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500675 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500676 return session;
677}
678EXPORT_SYMBOL_GPL(iscsi_alloc_session);
679
Mike Christie79706342008-05-21 15:54:12 -0500680static int iscsi_get_next_target_id(struct device *dev, void *data)
681{
682 struct iscsi_cls_session *session;
683 unsigned long flags;
684 int err = 0;
685
686 if (!iscsi_is_session_dev(dev))
687 return 0;
688
689 session = iscsi_dev_to_session(dev);
690 spin_lock_irqsave(&session->lock, flags);
691 if (*((unsigned int *) data) == session->target_id)
692 err = -EEXIST;
693 spin_unlock_irqrestore(&session->lock, flags);
694 return err;
695}
696
Mike Christie6a8a0d32006-06-28 12:00:31 -0500697int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500698{
699 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500700 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600701 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500702 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500703 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500704
Mike Christie30a6c652006-04-06 21:13:39 -0500705 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600706 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500707
708 if (id == ISCSI_MAX_TARGET) {
709 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
710 err = device_for_each_child(&shost->shost_gendev, &id,
711 iscsi_get_next_target_id);
712 if (!err)
713 break;
714 }
715
716 if (id == ISCSI_MAX_TARGET) {
717 iscsi_cls_session_printk(KERN_ERR, session,
718 "Too many iscsi targets. Max "
719 "number of targets is %d.\n",
720 ISCSI_MAX_TARGET - 1);
721 goto release_host;
722 }
723 }
724 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500725
Kay Sievers71610f52008-12-03 22:41:36 +0100726 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500727 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600728 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600729 iscsi_cls_session_printk(KERN_ERR, session,
730 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500731 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600732 }
733 transport_register_device(&session->dev);
734
Mike Christie26974782007-12-13 12:43:29 -0600735 spin_lock_irqsave(&sesslock, flags);
736 list_add(&session->sess_list, &sesslist);
737 spin_unlock_irqrestore(&sesslock, flags);
738
Mike Christie26974782007-12-13 12:43:29 -0600739 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500740 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500741
Mike Christie8434aa82006-06-28 12:00:30 -0500742release_host:
743 scsi_host_put(shost);
744 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600745}
Mike Christie8434aa82006-06-28 12:00:30 -0500746EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600747
748/**
Mike Christie8434aa82006-06-28 12:00:30 -0500749 * iscsi_create_session - create iscsi class session
750 * @shost: scsi host
751 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500752 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500753 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600754 *
Mike Christie8434aa82006-06-28 12:00:30 -0500755 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500756 */
Mike Christie8434aa82006-06-28 12:00:30 -0500757struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500758iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
759 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500760{
761 struct iscsi_cls_session *session;
762
Mike Christie5d91e202008-05-21 15:54:01 -0500763 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500764 if (!session)
765 return NULL;
766
Mike Christie6a8a0d32006-06-28 12:00:31 -0500767 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500768 iscsi_free_session(session);
769 return NULL;
770 }
771 return session;
772}
773EXPORT_SYMBOL_GPL(iscsi_create_session);
774
Mike Christie26974782007-12-13 12:43:29 -0600775static void iscsi_conn_release(struct device *dev)
776{
777 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
778 struct device *parent = conn->dev.parent;
779
780 kfree(conn);
781 put_device(parent);
782}
783
784static int iscsi_is_conn_dev(const struct device *dev)
785{
786 return dev->release == iscsi_conn_release;
787}
788
789static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
790{
791 if (!iscsi_is_conn_dev(dev))
792 return 0;
793 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
794}
795
Mike Christie8434aa82006-06-28 12:00:30 -0500796void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600797{
Mike Christie30a6c652006-04-06 21:13:39 -0500798 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500799 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600800 unsigned long flags;
801 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500802
Mike Christie26974782007-12-13 12:43:29 -0600803 spin_lock_irqsave(&sesslock, flags);
804 list_del(&session->sess_list);
805 spin_unlock_irqrestore(&sesslock, flags);
806
Mike Christie45ab33b2008-03-04 13:26:55 -0600807 /* make sure there are no blocks/unblocks queued */
808 flush_workqueue(iscsi_eh_timer_workq);
809 /* make sure the timedout callout is not running */
810 if (!cancel_delayed_work(&session->recovery_work))
811 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600812 /*
813 * If we are blocked let commands flow again. The lld or iscsi
814 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -0600815 * We assume that LLD will not be calling block/unblock while
816 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -0600817 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600818 spin_lock_irqsave(&session->lock, flags);
819 session->state = ISCSI_SESSION_FREE;
820 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -0600821
Mike Christie45ab33b2008-03-04 13:26:55 -0600822 scsi_target_unblock(&session->dev);
823 /* flush running scans then delete devices */
Mike Christiebd976f62008-01-31 13:36:46 -0600824 flush_workqueue(ihost->scan_workq);
Mike Christie45ab33b2008-03-04 13:26:55 -0600825 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -0500826
Mike Christie26974782007-12-13 12:43:29 -0600827 /* hw iscsi may not have removed all connections from session */
828 err = device_for_each_child(&session->dev, NULL,
829 iscsi_iter_destroy_conn_fn);
830 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600831 iscsi_cls_session_printk(KERN_ERR, session,
832 "Could not delete all connections "
833 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500834
Mike Christie7b8631b2006-01-13 18:05:50 -0600835 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500836 device_del(&session->dev);
837}
838EXPORT_SYMBOL_GPL(iscsi_remove_session);
839
840void iscsi_free_session(struct iscsi_cls_session *session)
841{
Mike Christie26974782007-12-13 12:43:29 -0600842 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500843 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600844}
Mike Christie8434aa82006-06-28 12:00:30 -0500845EXPORT_SYMBOL_GPL(iscsi_free_session);
846
847/**
848 * iscsi_destroy_session - destroy iscsi session
849 * @session: iscsi_session
850 *
851 * Can be called by a LLD or iscsi_transport. There must not be
852 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500853 */
Mike Christie8434aa82006-06-28 12:00:30 -0500854int iscsi_destroy_session(struct iscsi_cls_session *session)
855{
856 iscsi_remove_session(session);
857 iscsi_free_session(session);
858 return 0;
859}
Mike Christie7b8631b2006-01-13 18:05:50 -0600860EXPORT_SYMBOL_GPL(iscsi_destroy_session);
861
Mike Christie7b8631b2006-01-13 18:05:50 -0600862/**
863 * iscsi_create_conn - create iscsi class connection
864 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -0500865 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -0600866 * @cid: connection id
867 *
868 * This can be called from a LLD or iscsi_transport. The connection
869 * is child of the session so cid must be unique for all connections
870 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500871 *
872 * Since we do not support MCS, cid will normally be zero. In some cases
873 * for software iscsi we could be trying to preallocate a connection struct
874 * in which case there could be two connection structs and cid would be
875 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500876 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600877struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -0500878iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -0600879{
880 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600881 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600882 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600883 int err;
884
Mike Christie5d91e202008-05-21 15:54:01 -0500885 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600886 if (!conn)
887 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -0500888 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600889 conn->dd_data = &conn[1];
890
891 INIT_LIST_HEAD(&conn->conn_list);
892 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500893 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600894
895 /* this is released in the dev's release function */
896 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400897 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500898
Kay Sievers71610f52008-12-03 22:41:36 +0100899 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600900 conn->dev.parent = &session->dev;
901 conn->dev.release = iscsi_conn_release;
902 err = device_register(&conn->dev);
903 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600904 iscsi_cls_session_printk(KERN_ERR, session, "could not "
905 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600906 goto release_parent_ref;
907 }
908 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600909
910 spin_lock_irqsave(&connlock, flags);
911 list_add(&conn->conn_list, &connlist);
912 conn->active = 1;
913 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600914 return conn;
915
916release_parent_ref:
917 put_device(&session->dev);
918free_conn:
919 kfree(conn);
920 return NULL;
921}
922
923EXPORT_SYMBOL_GPL(iscsi_create_conn);
924
925/**
926 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500927 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600928 *
Mike Christie26974782007-12-13 12:43:29 -0600929 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500930 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600931int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
932{
Mike Christie26974782007-12-13 12:43:29 -0600933 unsigned long flags;
934
935 spin_lock_irqsave(&connlock, flags);
936 conn->active = 0;
937 list_del(&conn->conn_list);
938 spin_unlock_irqrestore(&connlock, flags);
939
Mike Christie7b8631b2006-01-13 18:05:50 -0600940 transport_unregister_device(&conn->dev);
941 device_unregister(&conn->dev);
942 return 0;
943}
Mike Christie7b8631b2006-01-13 18:05:50 -0600944EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
945
946/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600947 * iscsi interface functions
948 */
Alex Aizman0896b752005-08-04 19:33:07 -0700949static struct iscsi_internal *
950iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Alex Aizman0896b752005-08-04 19:33:07 -0700952 struct iscsi_internal *priv;
953 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Alex Aizman0896b752005-08-04 19:33:07 -0700955 spin_lock_irqsave(&iscsi_transport_lock, flags);
956 list_for_each_entry(priv, &iscsi_transports, list) {
957 if (tt == priv->iscsi_transport) {
958 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
959 return priv;
960 }
961 }
962 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
963 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Alex Aizman0896b752005-08-04 19:33:07 -0700966static int
Mike Christie43a145a2006-10-16 18:09:38 -0400967iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500968{
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800969 return netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500970}
971
972static int
Mike Christie43a145a2006-10-16 18:09:38 -0400973iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700974{
Alex Aizman0896b752005-08-04 19:33:07 -0700975 int rc;
976
Mike Christie790f39a2006-05-18 20:31:39 -0500977 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700978 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700979 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
980 return rc;
981 }
982
Alex Aizman0896b752005-08-04 19:33:07 -0700983 return 0;
984}
985
Mike Christie7b7232f2006-02-01 21:06:49 -0600986int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700987 char *data, uint32_t data_size)
988{
989 struct nlmsghdr *nlh;
990 struct sk_buff *skb;
991 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700992 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500993 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700994 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
995 data_size);
996
Mike Christie790f39a2006-05-18 20:31:39 -0500997 priv = iscsi_if_transport_lookup(conn->transport);
998 if (!priv)
999 return -EINVAL;
1000
Mike Christie43a145a2006-10-16 18:09:38 -04001001 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001002 if (!skb) {
Mike Christiee5bd7b542008-09-24 11:46:10 -05001003 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001004 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1005 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001006 return -ENOMEM;
1007 }
1008
Mike Christie790f39a2006-05-18 20:31:39 -05001009 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001010 ev = NLMSG_DATA(nlh);
1011 memset(ev, 0, sizeof(*ev));
1012 ev->transport_handle = iscsi_handle(conn->transport);
1013 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001014 ev->r.recv_req.cid = conn->cid;
1015 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001016 pdu = (char*)ev + sizeof(*ev);
1017 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1018 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1019
Mike Christie43a145a2006-10-16 18:09:38 -04001020 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -07001021}
1022EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1023
Mike Christiee5bd7b542008-09-24 11:46:10 -05001024void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001025{
1026 struct nlmsghdr *nlh;
1027 struct sk_buff *skb;
1028 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001029 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001030 int len = NLMSG_SPACE(sizeof(*ev));
1031
Mike Christie790f39a2006-05-18 20:31:39 -05001032 priv = iscsi_if_transport_lookup(conn->transport);
1033 if (!priv)
1034 return;
1035
Mike Christie43a145a2006-10-16 18:09:38 -04001036 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001037 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001038 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1039 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001040 return;
1041 }
1042
Mike Christie790f39a2006-05-18 20:31:39 -05001043 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001044 ev = NLMSG_DATA(nlh);
1045 ev->transport_handle = iscsi_handle(conn->transport);
1046 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001047 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001048 ev->r.connerror.cid = conn->cid;
1049 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001050
Mike Christie43a145a2006-10-16 18:09:38 -04001051 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001052
Mike Christie322d7392008-01-31 13:36:52 -06001053 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1054 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001055}
Mike Christiee5bd7b542008-09-24 11:46:10 -05001056EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001057
1058static int
1059iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
1060 void *payload, int size)
1061{
1062 struct sk_buff *skb;
1063 struct nlmsghdr *nlh;
1064 int len = NLMSG_SPACE(size);
1065 int flags = multi ? NLM_F_MULTI : 0;
1066 int t = done ? NLMSG_DONE : type;
1067
Mike Christie43a145a2006-10-16 18:09:38 -04001068 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001069 if (!skb) {
1070 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1071 return -ENOMEM;
1072 }
Alex Aizman0896b752005-08-04 19:33:07 -07001073
1074 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
1075 nlh->nlmsg_flags = flags;
1076 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -04001077 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -07001078}
1079
1080static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001081iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001082{
1083 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1084 struct iscsi_stats *stats;
1085 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001086 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001087 struct nlmsghdr *nlhstat;
1088 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001089 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001090 int len = NLMSG_SPACE(sizeof(*ev) +
1091 sizeof(struct iscsi_stats) +
1092 sizeof(struct iscsi_stats_custom) *
1093 ISCSI_STATS_CUSTOM_MAX);
1094 int err = 0;
1095
Mike Christie790f39a2006-05-18 20:31:39 -05001096 priv = iscsi_if_transport_lookup(transport);
1097 if (!priv)
1098 return -EINVAL;
1099
Mike Christieb5c7a122006-04-06 21:13:33 -05001100 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001101 if (!conn)
1102 return -EEXIST;
1103
1104 do {
1105 int actual_size;
1106
Mike Christie43a145a2006-10-16 18:09:38 -04001107 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001108 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001109 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1110 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001111 return -ENOMEM;
1112 }
1113
Mike Christie790f39a2006-05-18 20:31:39 -05001114 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001115 (len - sizeof(*nlhstat)), 0);
1116 evstat = NLMSG_DATA(nlhstat);
1117 memset(evstat, 0, sizeof(*evstat));
1118 evstat->transport_handle = iscsi_handle(conn->transport);
1119 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001120 evstat->u.get_stats.cid =
1121 ev->u.get_stats.cid;
1122 evstat->u.get_stats.sid =
1123 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001124 stats = (struct iscsi_stats *)
1125 ((char*)evstat + sizeof(*evstat));
1126 memset(stats, 0, sizeof(*stats));
1127
Mike Christie7b7232f2006-02-01 21:06:49 -06001128 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001129 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1130 sizeof(struct iscsi_stats) +
1131 sizeof(struct iscsi_stats_custom) *
1132 stats->custom_length);
1133 actual_size -= sizeof(*nlhstat);
1134 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001135 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001136 nlhstat->nlmsg_len = actual_size;
1137
Mike Christie43a145a2006-10-16 18:09:38 -04001138 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -07001139 } while (err < 0 && err != -ECONNREFUSED);
1140
1141 return err;
1142}
1143
Mike Christie53cb8a12006-06-28 12:00:32 -05001144/**
Mike Christie26974782007-12-13 12:43:29 -06001145 * iscsi_session_event - send session destr. completion event
1146 * @session: iscsi class session
1147 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001148 */
Mike Christie26974782007-12-13 12:43:29 -06001149int iscsi_session_event(struct iscsi_cls_session *session,
1150 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001151{
1152 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001153 struct Scsi_Host *shost;
1154 struct iscsi_uevent *ev;
1155 struct sk_buff *skb;
1156 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001157 int rc, len = NLMSG_SPACE(sizeof(*ev));
1158
Mike Christie26974782007-12-13 12:43:29 -06001159 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001160 if (!priv)
1161 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001162 shost = iscsi_session_to_shost(session);
1163
Mike Christie43a145a2006-10-16 18:09:38 -04001164 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001165 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001166 iscsi_cls_session_printk(KERN_ERR, session,
1167 "Cannot notify userspace of session "
1168 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001169 return -ENOMEM;
1170 }
1171
1172 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1173 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001174 ev->transport_handle = iscsi_handle(session->transport);
1175
1176 ev->type = event;
1177 switch (event) {
1178 case ISCSI_KEVENT_DESTROY_SESSION:
1179 ev->r.d_session.host_no = shost->host_no;
1180 ev->r.d_session.sid = session->sid;
1181 break;
1182 case ISCSI_KEVENT_CREATE_SESSION:
1183 ev->r.c_session_ret.host_no = shost->host_no;
1184 ev->r.c_session_ret.sid = session->sid;
1185 break;
1186 case ISCSI_KEVENT_UNBIND_SESSION:
1187 ev->r.unbind_session.host_no = shost->host_no;
1188 ev->r.unbind_session.sid = session->sid;
1189 break;
1190 default:
Mike Christie322d7392008-01-31 13:36:52 -06001191 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1192 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001193 kfree_skb(skb);
1194 return -EINVAL;
1195 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001196
1197 /*
1198 * this will occur if the daemon is not up, so we just warn
1199 * the user and when the daemon is restarted it will handle it
1200 */
Mike Christie43a145a2006-10-16 18:09:38 -04001201 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001202 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001203 iscsi_cls_session_printk(KERN_ERR, session,
1204 "Cannot notify userspace of session "
1205 "event %u. Check iscsi daemon\n",
1206 event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001207 return rc;
1208}
Mike Christie26974782007-12-13 12:43:29 -06001209EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001210
Alex Aizman0896b752005-08-04 19:33:07 -07001211static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001212iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1213 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001214 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001215{
1216 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001217 struct iscsi_cls_session *session;
Mike Christied82ff9be2008-05-21 15:54:13 -05001218 uint32_t host_no;
Mike Christie7b8631b2006-01-13 18:05:50 -06001219
Mike Christied82ff9be2008-05-21 15:54:13 -05001220 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie40753ca2008-05-21 15:53:56 -05001221 initial_cmdsn, &host_no);
Mike Christie7b7232f2006-02-01 21:06:49 -06001222 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001223 return -ENOMEM;
1224
Mike Christie40753ca2008-05-21 15:53:56 -05001225 ev->r.c_session_ret.host_no = host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001226 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001227 return 0;
1228}
1229
1230static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001231iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001232{
Mike Christie7b8631b2006-01-13 18:05:50 -06001233 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001234 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001235
Mike Christieb5c7a122006-04-06 21:13:33 -05001236 session = iscsi_session_lookup(ev->u.c_conn.sid);
1237 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001238 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001239 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001240 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001241 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001242
Mike Christie7b7232f2006-02-01 21:06:49 -06001243 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001244 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001245 iscsi_cls_session_printk(KERN_ERR, session,
1246 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001247 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001248 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001249
Mike Christieb5c7a122006-04-06 21:13:33 -05001250 ev->r.c_conn_ret.sid = session->sid;
1251 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001252 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001253}
1254
1255static int
1256iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1257{
Mike Christie7b8631b2006-01-13 18:05:50 -06001258 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001259
Mike Christieb5c7a122006-04-06 21:13:33 -05001260 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001261 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001262 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001263
Mike Christie7b8631b2006-01-13 18:05:50 -06001264 if (transport->destroy_conn)
1265 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001266 return 0;
1267}
1268
Mike Christiefd7255f2006-04-06 21:13:36 -05001269static int
1270iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1271{
1272 char *data = (char*)ev + sizeof(*ev);
1273 struct iscsi_cls_conn *conn;
1274 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001275 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001276
1277 session = iscsi_session_lookup(ev->u.set_param.sid);
1278 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1279 if (!conn || !session)
1280 return -EINVAL;
1281
1282 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001283 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001284 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001285 if (value != 0)
1286 session->recovery_tmo = value;
1287 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001288 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001289 err = transport->set_param(conn, ev->u.set_param.param,
1290 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001291 }
1292
1293 return err;
1294}
1295
Mike Christie7b8631b2006-01-13 18:05:50 -06001296static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001297iscsi_if_transport_ep(struct iscsi_transport *transport,
1298 struct iscsi_uevent *ev, int msg_type)
1299{
Mike Christied82ff9be2008-05-21 15:54:13 -05001300 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001301 struct sockaddr *dst_addr;
1302 int rc = 0;
1303
1304 switch (msg_type) {
1305 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1306 if (!transport->ep_connect)
1307 return -EINVAL;
1308
1309 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christied82ff9be2008-05-21 15:54:13 -05001310 ep = transport->ep_connect(dst_addr,
1311 ev->u.ep_connect.non_blocking);
1312 if (IS_ERR(ep))
1313 return PTR_ERR(ep);
1314
1315 ev->r.ep_connect_ret.handle = ep->id;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001316 break;
1317 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1318 if (!transport->ep_poll)
1319 return -EINVAL;
1320
Mike Christied82ff9be2008-05-21 15:54:13 -05001321 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1322 if (!ep)
1323 return -EINVAL;
1324
1325 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001326 ev->u.ep_poll.timeout_ms);
1327 break;
1328 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1329 if (!transport->ep_disconnect)
1330 return -EINVAL;
1331
Mike Christied82ff9be2008-05-21 15:54:13 -05001332 ep = iscsi_lookup_endpoint(ev->u.ep_disconnect.ep_handle);
1333 if (!ep)
1334 return -EINVAL;
1335
1336 transport->ep_disconnect(ep);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001337 break;
1338 }
1339 return rc;
1340}
1341
1342static int
Mike Christie01cb2252006-06-28 12:00:22 -05001343iscsi_tgt_dscvr(struct iscsi_transport *transport,
1344 struct iscsi_uevent *ev)
1345{
Mike Christie2174a042007-05-30 12:57:10 -05001346 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001347 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001348 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001349
1350 if (!transport->tgt_dscvr)
1351 return -EINVAL;
1352
Mike Christie2174a042007-05-30 12:57:10 -05001353 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001354 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001355 printk(KERN_ERR "target discovery could not find host no %u\n",
1356 ev->u.tgt_dscvr.host_no);
1357 return -ENODEV;
1358 }
1359
1360
Mike Christie01cb2252006-06-28 12:00:22 -05001361 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001362 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1363 ev->u.tgt_dscvr.enable, dst_addr);
1364 scsi_host_put(shost);
1365 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001366}
1367
1368static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001369iscsi_set_host_param(struct iscsi_transport *transport,
1370 struct iscsi_uevent *ev)
1371{
1372 char *data = (char*)ev + sizeof(*ev);
1373 struct Scsi_Host *shost;
1374 int err;
1375
1376 if (!transport->set_host_param)
1377 return -ENOSYS;
1378
1379 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001380 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001381 printk(KERN_ERR "set_host_param could not find host no %u\n",
1382 ev->u.set_host_param.host_no);
1383 return -ENODEV;
1384 }
1385
1386 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1387 data, ev->u.set_host_param.len);
1388 scsi_host_put(shost);
1389 return err;
1390}
1391
1392static int
Alex Aizman0896b752005-08-04 19:33:07 -07001393iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1394{
1395 int err = 0;
1396 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1397 struct iscsi_transport *transport = NULL;
1398 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001399 struct iscsi_cls_session *session;
1400 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001401 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001402
Alex Aizman0896b752005-08-04 19:33:07 -07001403 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1404 if (!priv)
1405 return -EINVAL;
1406 transport = priv->iscsi_transport;
1407
Mike Christie7b7232f2006-02-01 21:06:49 -06001408 if (!try_module_get(transport->owner))
1409 return -EINVAL;
1410
Mike Christie790f39a2006-05-18 20:31:39 -05001411 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1412
Alex Aizman0896b752005-08-04 19:33:07 -07001413 switch (nlh->nlmsg_type) {
1414 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001415 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001416 ev->u.c_session.initial_cmdsn,
1417 ev->u.c_session.cmds_max,
1418 ev->u.c_session.queue_depth);
1419 break;
1420 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001421 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001422 if (!ep) {
1423 err = -EINVAL;
1424 break;
1425 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001426
1427 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001428 ev->u.c_bound_session.initial_cmdsn,
1429 ev->u.c_bound_session.cmds_max,
1430 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001431 break;
1432 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001433 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001434 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001435 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001436 else
1437 err = -EINVAL;
1438 break;
1439 case ISCSI_UEVENT_UNBIND_SESSION:
1440 session = iscsi_session_lookup(ev->u.d_session.sid);
1441 if (session)
1442 iscsi_unbind_session(session);
1443 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001444 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001445 break;
1446 case ISCSI_UEVENT_CREATE_CONN:
1447 err = iscsi_if_create_conn(transport, ev);
1448 break;
1449 case ISCSI_UEVENT_DESTROY_CONN:
1450 err = iscsi_if_destroy_conn(transport, ev);
1451 break;
1452 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001453 session = iscsi_session_lookup(ev->u.b_conn.sid);
1454 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001455
1456 if (session && conn)
1457 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001458 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001459 ev->u.b_conn.is_leading);
1460 else
1461 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001462 break;
1463 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001464 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001465 break;
1466 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001467 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001468 if (conn)
1469 ev->r.retcode = transport->start_conn(conn);
1470 else
1471 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001472 break;
1473 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001474 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001475 if (conn)
1476 transport->stop_conn(conn, ev->u.stop_conn.flag);
1477 else
1478 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001479 break;
1480 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001481 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001482 if (conn)
1483 ev->r.retcode = transport->send_pdu(conn,
1484 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1485 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1486 ev->u.send_pdu.data_size);
1487 else
1488 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001489 break;
1490 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001491 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001492 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001493 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1494 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1495 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1496 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1497 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001498 case ISCSI_UEVENT_TGT_DSCVR:
1499 err = iscsi_tgt_dscvr(transport, ev);
1500 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001501 case ISCSI_UEVENT_SET_HOST_PARAM:
1502 err = iscsi_set_host_param(transport, ev);
1503 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001504 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001505 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001506 break;
1507 }
1508
Mike Christie7b7232f2006-02-01 21:06:49 -06001509 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001510 return err;
1511}
1512
Mike Christieb5c7a122006-04-06 21:13:33 -05001513/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001514 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1515 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001516 */
Alex Aizman0896b752005-08-04 19:33:07 -07001517static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001518iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001519{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001520 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001521 while (skb->len >= NLMSG_SPACE(0)) {
1522 int err;
1523 uint32_t rlen;
1524 struct nlmsghdr *nlh;
1525 struct iscsi_uevent *ev;
1526
1527 nlh = nlmsg_hdr(skb);
1528 if (nlh->nlmsg_len < sizeof(*nlh) ||
1529 skb->len < nlh->nlmsg_len) {
1530 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001531 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001532
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001533 ev = NLMSG_DATA(nlh);
1534 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1535 if (rlen > skb->len)
1536 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001537
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001538 err = iscsi_if_recv_msg(skb, nlh);
1539 if (err) {
1540 ev->type = ISCSI_KEVENT_IF_ERROR;
1541 ev->iferror = err;
1542 }
1543 do {
1544 /*
1545 * special case for GET_STATS:
1546 * on success - sending reply and stats from
1547 * inside of if_recv_msg(),
1548 * on error - fall through.
1549 */
1550 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001551 break;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001552 err = iscsi_if_send_reply(
1553 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1554 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1555 } while (err < 0 && err != -ECONNREFUSED);
1556 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001557 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001558 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001559}
1560
Mike Christiefd7255f2006-04-06 21:13:36 -05001561#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001562struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001563 __ATTR(_name,_mode,_show,_store)
1564
Alex Aizman0896b752005-08-04 19:33:07 -07001565/*
1566 * iSCSI connection attrs
1567 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001568#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001570show_conn_param_##param(struct device *dev, \
1571 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001572{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001573 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001574 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001575 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001576}
1577
Mike Christiea54a52c2006-06-28 12:00:23 -05001578#define iscsi_conn_attr(field, param) \
1579 iscsi_conn_attr_show(param) \
1580static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001581 NULL);
1582
Mike Christiea54a52c2006-06-28 12:00:23 -05001583iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1584iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1585iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1586iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1587iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1588iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1589iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1590iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1591iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1592iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1593iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001594iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1595iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597/*
Alex Aizman0896b752005-08-04 19:33:07 -07001598 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
Mike Christieb2c64162007-05-30 12:57:16 -05001600#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001602show_session_param_##param(struct device *dev, \
1603 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001604{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001605 struct iscsi_cls_session *session = \
1606 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001607 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001608 \
1609 if (perm && !capable(CAP_SYS_ADMIN)) \
1610 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001611 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Mike Christieb2c64162007-05-30 12:57:16 -05001614#define iscsi_session_attr(field, param, perm) \
1615 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001616static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001617 NULL);
1618
Mike Christieb2c64162007-05-30 12:57:16 -05001619iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1620iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1621iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1622iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1623iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1624iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1625iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1626iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1627iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1628iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1629iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1630iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1631iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1632iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001633iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1634iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1635iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05001636iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
1637iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0)
Mike Christiefd7255f2006-04-06 21:13:36 -05001638
Mike Christie6eabafb2008-01-31 13:36:43 -06001639static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001640show_priv_session_state(struct device *dev, struct device_attribute *attr,
1641 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06001642{
Tony Jonesee959b02008-02-22 00:13:36 +01001643 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06001644 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1645}
1646static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1647 NULL);
1648
Mike Christiefd7255f2006-04-06 21:13:36 -05001649#define iscsi_priv_session_attr_show(field, format) \
1650static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001651show_priv_session_##field(struct device *dev, \
1652 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001653{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001654 struct iscsi_cls_session *session = \
1655 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001656 return sprintf(buf, format"\n", session->field); \
1657}
1658
1659#define iscsi_priv_session_attr(field, format) \
1660 iscsi_priv_session_attr_show(field, format) \
1661static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1662 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001663iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001664
Mike Christie1819dc82007-05-30 12:57:08 -05001665/*
1666 * iSCSI host attrs
1667 */
1668#define iscsi_host_attr_show(param) \
1669static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001670show_host_param_##param(struct device *dev, \
1671 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05001672{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001673 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05001674 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1675 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1676}
1677
1678#define iscsi_host_attr(field, param) \
1679 iscsi_host_attr_show(param) \
1680static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1681 NULL);
1682
Mike Christied8196ed2007-05-30 12:57:25 -05001683iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001684iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001685iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001686iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001687
Mike Christiefd7255f2006-04-06 21:13:36 -05001688#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1689do { \
Tony Jonesee959b02008-02-22 00:13:36 +01001690 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001691 count++; \
1692} while (0)
1693
Mike Christiea54a52c2006-06-28 12:00:23 -05001694
Mike Christiefd7255f2006-04-06 21:13:36 -05001695#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1696do { \
1697 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001698 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001700 } \
1701} while (0)
1702
Mike Christiefd7255f2006-04-06 21:13:36 -05001703#define SETUP_CONN_RD_ATTR(field, param_flag) \
1704do { \
1705 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001706 priv->conn_attrs[count] = &dev_attr_conn_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001707 count++; \
1708 } \
1709} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Mike Christie1819dc82007-05-30 12:57:08 -05001711#define SETUP_HOST_RD_ATTR(field, param_flag) \
1712do { \
1713 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001714 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05001715 count++; \
1716 } \
1717} while (0)
1718
Alex Aizman0896b752005-08-04 19:33:07 -07001719static int iscsi_session_match(struct attribute_container *cont,
1720 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Mike Christie7b8631b2006-01-13 18:05:50 -06001722 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001724 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Alex Aizman0896b752005-08-04 19:33:07 -07001726 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return 0;
1728
Mike Christie7b8631b2006-01-13 18:05:50 -06001729 session = iscsi_dev_to_session(dev);
1730 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001731 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 0;
1733
Alex Aizman0896b752005-08-04 19:33:07 -07001734 priv = to_iscsi_internal(shost->transportt);
1735 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1736 return 0;
1737
1738 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Alex Aizman0896b752005-08-04 19:33:07 -07001741static int iscsi_conn_match(struct attribute_container *cont,
1742 struct device *dev)
1743{
Mike Christie7b8631b2006-01-13 18:05:50 -06001744 struct iscsi_cls_session *session;
1745 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001746 struct Scsi_Host *shost;
1747 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Alex Aizman0896b752005-08-04 19:33:07 -07001749 if (!iscsi_is_conn_dev(dev))
1750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Mike Christie7b8631b2006-01-13 18:05:50 -06001752 conn = iscsi_dev_to_conn(dev);
1753 session = iscsi_dev_to_session(conn->dev.parent);
1754 shost = iscsi_session_to_shost(session);
1755
Alex Aizman0896b752005-08-04 19:33:07 -07001756 if (!shost->transportt)
1757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Alex Aizman0896b752005-08-04 19:33:07 -07001759 priv = to_iscsi_internal(shost->transportt);
1760 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1761 return 0;
1762
1763 return &priv->conn_cont.ac == cont;
1764}
1765
Mike Christie30a6c652006-04-06 21:13:39 -05001766static int iscsi_host_match(struct attribute_container *cont,
1767 struct device *dev)
1768{
1769 struct Scsi_Host *shost;
1770 struct iscsi_internal *priv;
1771
1772 if (!scsi_is_host_device(dev))
1773 return 0;
1774
1775 shost = dev_to_shost(dev);
1776 if (!shost->transportt ||
1777 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1778 return 0;
1779
1780 priv = to_iscsi_internal(shost->transportt);
1781 return &priv->t.host_attrs.ac == cont;
1782}
1783
Mike Christie7b8631b2006-01-13 18:05:50 -06001784struct scsi_transport_template *
1785iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001786{
1787 struct iscsi_internal *priv;
1788 unsigned long flags;
1789 int count = 0, err;
1790
1791 BUG_ON(!tt);
1792
1793 priv = iscsi_if_transport_lookup(tt);
1794 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001795 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001796
Jes Sorensen24669f752006-01-16 10:31:18 -05001797 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001798 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001799 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001800 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001801 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001802 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001803 priv->t.user_scan = iscsi_user_scan;
Mike Christiea4804cd2008-05-21 15:54:00 -05001804 if (!(tt->caps & CAP_DATA_PATH_OFFLOAD))
1805 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07001806
Tony Jonesee959b02008-02-22 00:13:36 +01001807 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01001808 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01001809 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001810 if (err)
1811 goto free_priv;
1812
Tony Jonesee959b02008-02-22 00:13:36 +01001813 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07001814 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01001815 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07001816
Mike Christie30a6c652006-04-06 21:13:39 -05001817 /* host parameters */
1818 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1819 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1820 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05001821 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001822 transport_container_register(&priv->t.host_attrs);
1823
Mike Christied8196ed2007-05-30 12:57:25 -05001824 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001825 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001826 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001827 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001828 BUG_ON(count > ISCSI_HOST_ATTRS);
1829 priv->host_attrs[count] = NULL;
1830 count = 0;
1831
Alex Aizman0896b752005-08-04 19:33:07 -07001832 /* connection parameters */
1833 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1834 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1835 priv->conn_cont.ac.match = iscsi_conn_match;
1836 transport_container_register(&priv->conn_cont);
1837
Mike Christiefd7255f2006-04-06 21:13:36 -05001838 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1839 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1840 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1841 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1842 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1843 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1844 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1845 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001846 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001847 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1848 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001849 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1850 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001851
1852 BUG_ON(count > ISCSI_CONN_ATTRS);
1853 priv->conn_attrs[count] = NULL;
1854 count = 0;
1855
1856 /* session parameters */
1857 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1858 priv->session_cont.ac.class = &iscsi_session_class.class;
1859 priv->session_cont.ac.match = iscsi_session_match;
1860 transport_container_register(&priv->session_cont);
1861
Mike Christiefd7255f2006-04-06 21:13:36 -05001862 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1863 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1864 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1865 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1866 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1867 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1868 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1869 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001870 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1871 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001872 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1873 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1874 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1875 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001876 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001877 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1878 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie88dfd342008-05-21 15:54:16 -05001879 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
1880 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
Mike Christie30a6c652006-04-06 21:13:39 -05001881 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06001882 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001885 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Alex Aizman0896b752005-08-04 19:33:07 -07001887 spin_lock_irqsave(&iscsi_transport_lock, flags);
1888 list_add(&priv->list, &iscsi_transports);
1889 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Alex Aizman0896b752005-08-04 19:33:07 -07001891 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001892 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Tony Jonesee959b02008-02-22 00:13:36 +01001894unregister_dev:
1895 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05001896 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001897free_priv:
1898 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001899 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
Alex Aizman0896b752005-08-04 19:33:07 -07001901EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Alex Aizman0896b752005-08-04 19:33:07 -07001903int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Alex Aizman0896b752005-08-04 19:33:07 -07001905 struct iscsi_internal *priv;
1906 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Alex Aizman0896b752005-08-04 19:33:07 -07001908 BUG_ON(!tt);
1909
Arjan van de Ven0b950672006-01-11 13:16:10 +01001910 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001911
1912 priv = iscsi_if_transport_lookup(tt);
1913 BUG_ON (!priv);
1914
Alex Aizman0896b752005-08-04 19:33:07 -07001915 spin_lock_irqsave(&iscsi_transport_lock, flags);
1916 list_del(&priv->list);
1917 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1918
1919 transport_container_unregister(&priv->conn_cont);
1920 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001921 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001922
Tony Jonesee959b02008-02-22 00:13:36 +01001923 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1924 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001925 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001926
1927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931static __init int iscsi_transport_init(void)
1932{
Alex Aizman0896b752005-08-04 19:33:07 -07001933 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Meelis Roos09492602006-12-17 12:10:26 -06001935 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001936 ISCSI_TRANSPORT_VERSION);
1937
Mike Christie41be1442007-02-28 17:32:18 -06001938 atomic_set(&iscsi_session_nr, 0);
1939
Alex Aizman0896b752005-08-04 19:33:07 -07001940 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (err)
1942 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001943
Mike Christied82ff9be2008-05-21 15:54:13 -05001944 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001945 if (err)
1946 goto unregister_transport_class;
1947
Mike Christied82ff9be2008-05-21 15:54:13 -05001948 err = transport_class_register(&iscsi_host_class);
1949 if (err)
1950 goto unregister_endpoint_class;
1951
Mike Christie30a6c652006-04-06 21:13:39 -05001952 err = transport_class_register(&iscsi_connection_class);
1953 if (err)
1954 goto unregister_host_class;
1955
Alex Aizman0896b752005-08-04 19:33:07 -07001956 err = transport_class_register(&iscsi_session_class);
1957 if (err)
1958 goto unregister_conn_class;
1959
Mike Christied82ff9be2008-05-21 15:54:13 -05001960 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
1961 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001962 if (!nls) {
1963 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001964 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001965 }
1966
Mike Christied8bf5412007-12-13 12:43:27 -06001967 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1968 if (!iscsi_eh_timer_workq)
1969 goto release_nls;
1970
Mike Christie43a145a2006-10-16 18:09:38 -04001971 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001972
Mike Christied8bf5412007-12-13 12:43:27 -06001973release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001974 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001975unregister_session_class:
1976 transport_class_unregister(&iscsi_session_class);
1977unregister_conn_class:
1978 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001979unregister_host_class:
1980 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05001981unregister_endpoint_class:
1982 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001983unregister_transport_class:
1984 class_unregister(&iscsi_transport_class);
1985 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
1988static void __exit iscsi_transport_exit(void)
1989{
Mike Christied8bf5412007-12-13 12:43:27 -06001990 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001991 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001992 transport_class_unregister(&iscsi_connection_class);
1993 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001994 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05001995 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001996 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999module_init(iscsi_transport_init);
2000module_exit(iscsi_transport_exit);
2001
Alex Aizman0896b752005-08-04 19:33:07 -07002002MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2003 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2004 "Alex Aizman <itn780@yahoo.com>");
2005MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002007MODULE_VERSION(ISCSI_TRANSPORT_VERSION);