blob: 783e33c65eb7c354a5e6613f2653c0b95517f51c [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 {
40 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070041 struct iscsi_transport *iscsi_transport;
42 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010043 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050044
Tony Jonesee959b02008-02-22 00:13:36 +010045 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070046 struct transport_container conn_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010047 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070048 struct transport_container session_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010049 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Mike Christie41be1442007-02-28 17:32:18 -060052static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060053static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050054
Alex Aizman0896b752005-08-04 19:33:07 -070055/*
56 * list of registered transports and lock that must
57 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010058 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070059 */
60static LIST_HEAD(iscsi_transports);
61static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Alex Aizman0896b752005-08-04 19:33:07 -070063#define to_iscsi_internal(tmpl) \
64 container_of(tmpl, struct iscsi_internal, t)
65
Tony Jonesee959b02008-02-22 00:13:36 +010066#define dev_to_iscsi_internal(_dev) \
67 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070068
Tony Jonesee959b02008-02-22 00:13:36 +010069static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -070070{
Tony Jonesee959b02008-02-22 00:13:36 +010071 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -070072 kfree(priv);
73}
74
75/*
76 * iscsi_transport_class represents the iscsi_transports that are
77 * registered.
78 */
79static struct class iscsi_transport_class = {
80 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +010081 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -070082};
83
84static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +010085show_transport_handle(struct device *dev, struct device_attribute *attr,
86 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -070087{
Tony Jonesee959b02008-02-22 00:13:36 +010088 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -050089 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070090}
Tony Jonesee959b02008-02-22 00:13:36 +010091static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -070092
93#define show_transport_attr(name, format) \
94static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +010095show_transport_##name(struct device *dev, \
96 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -070097{ \
Tony Jonesee959b02008-02-22 00:13:36 +010098 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -070099 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
100} \
Tony Jonesee959b02008-02-22 00:13:36 +0100101static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700102
103show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700104
105static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100106 &dev_attr_handle.attr,
107 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700108 NULL,
109};
110
111static struct attribute_group iscsi_transport_group = {
112 .attrs = iscsi_transport_attrs,
113};
114
Mike Christied82ff9be2008-05-21 15:54:13 -0500115/*
116 * iSCSI endpoint attrs
117 */
118#define iscsi_dev_to_endpoint(_dev) \
119 container_of(_dev, struct iscsi_endpoint, dev)
120
121#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
122struct device_attribute dev_attr_##_prefix##_##_name = \
123 __ATTR(_name,_mode,_show,_store)
124
125static void iscsi_endpoint_release(struct device *dev)
126{
127 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
128 kfree(ep);
129}
130
131static struct class iscsi_endpoint_class = {
132 .name = "iscsi_endpoint",
133 .dev_release = iscsi_endpoint_release,
134};
135
136static ssize_t
137show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
138{
139 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500140 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500141}
142static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
143
144static struct attribute *iscsi_endpoint_attrs[] = {
145 &dev_attr_ep_handle.attr,
146 NULL,
147};
148
149static struct attribute_group iscsi_endpoint_group = {
150 .attrs = iscsi_endpoint_attrs,
151};
152
153#define ISCSI_MAX_EPID -1
154
155static int iscsi_match_epid(struct device *dev, void *data)
156{
157 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500158 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500159
160 return *epid == ep->id;
161}
162
163struct iscsi_endpoint *
164iscsi_create_endpoint(int dd_size)
165{
166 struct device *dev;
167 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500168 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500169 int err;
170
171 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400172 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500173 iscsi_match_epid);
174 if (!dev)
175 break;
176 }
177 if (id == ISCSI_MAX_EPID) {
178 printk(KERN_ERR "Too many connections. Max supported %u\n",
179 ISCSI_MAX_EPID - 1);
180 return NULL;
181 }
182
183 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
184 if (!ep)
185 return NULL;
186
187 ep->id = id;
188 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100189 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500190 err = device_register(&ep->dev);
191 if (err)
192 goto free_ep;
193
194 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
195 if (err)
196 goto unregister_dev;
197
198 if (dd_size)
199 ep->dd_data = &ep[1];
200 return ep;
201
202unregister_dev:
203 device_unregister(&ep->dev);
204 return NULL;
205
206free_ep:
207 kfree(ep);
208 return NULL;
209}
210EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
211
212void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
213{
214 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
215 device_unregister(&ep->dev);
216}
217EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
218
219struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
220{
Mike Christief80f8682008-06-16 10:11:35 -0500221 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500222 struct device *dev;
223
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400224 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500225 iscsi_match_epid);
226 if (!dev)
227 return NULL;
228
Mike Christief80f8682008-06-16 10:11:35 -0500229 ep = iscsi_dev_to_endpoint(dev);
230 /*
231 * we can drop this now because the interface will prevent
232 * removals and lookups from racing.
233 */
234 put_device(dev);
235 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500236}
237EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
238
Mike Christie30a6c652006-04-06 21:13:39 -0500239static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100240 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500241{
242 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500243 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500244
245 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600246 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500247 mutex_init(&ihost->mutex);
Mike Christie30a6c652006-04-06 21:13:39 -0500248 return 0;
249}
250
251static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
252 "iscsi_host",
253 iscsi_setup_host,
Mike Christie06d25af2009-03-05 14:46:02 -0600254 NULL,
Mike Christie30a6c652006-04-06 21:13:39 -0500255 NULL);
256
Alex Aizman0896b752005-08-04 19:33:07 -0700257static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
258 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 NULL,
260 NULL,
261 NULL);
262
Alex Aizman0896b752005-08-04 19:33:07 -0700263static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
264 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 NULL,
266 NULL,
267 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700268
269static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100270static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700271
Mike Christie7b7232f2006-02-01 21:06:49 -0600272static LIST_HEAD(sesslist);
273static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700274static LIST_HEAD(connlist);
275static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Mike Christieb5c7a122006-04-06 21:13:33 -0500277static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
278{
279 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
280 return sess->sid;
281}
282
283/*
284 * Returns the matching session to a given sid
285 */
286static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600287{
288 unsigned long flags;
289 struct iscsi_cls_session *sess;
290
291 spin_lock_irqsave(&sesslock, flags);
292 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500293 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600294 spin_unlock_irqrestore(&sesslock, flags);
295 return sess;
296 }
297 }
298 spin_unlock_irqrestore(&sesslock, flags);
299 return NULL;
300}
301
Mike Christieb5c7a122006-04-06 21:13:33 -0500302/*
303 * Returns the matching connection to a given sid / cid tuple
304 */
305static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600306{
307 unsigned long flags;
308 struct iscsi_cls_conn *conn;
309
310 spin_lock_irqsave(&connlock, flags);
311 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500312 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600313 spin_unlock_irqrestore(&connlock, flags);
314 return conn;
315 }
316 }
317 spin_unlock_irqrestore(&connlock, flags);
318 return NULL;
319}
320
Mike Christie7b8631b2006-01-13 18:05:50 -0600321/*
322 * The following functions can be used by LLDs that allocate
323 * their own scsi_hosts or by software iscsi LLDs
324 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600325static struct {
326 int value;
327 char *name;
328} iscsi_session_state_names[] = {
329 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
330 { ISCSI_SESSION_FAILED, "FAILED" },
331 { ISCSI_SESSION_FREE, "FREE" },
332};
333
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200334static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600335{
336 int i;
337 char *name = NULL;
338
339 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
340 if (iscsi_session_state_names[i].value == state) {
341 name = iscsi_session_state_names[i].name;
342 break;
343 }
344 }
345 return name;
346}
347
348int iscsi_session_chkready(struct iscsi_cls_session *session)
349{
350 unsigned long flags;
351 int err;
352
353 spin_lock_irqsave(&session->lock, flags);
354 switch (session->state) {
355 case ISCSI_SESSION_LOGGED_IN:
356 err = 0;
357 break;
358 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500359 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600360 break;
361 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500362 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600363 break;
364 default:
365 err = DID_NO_CONNECT << 16;
366 break;
367 }
368 spin_unlock_irqrestore(&session->lock, flags);
369 return err;
370}
371EXPORT_SYMBOL_GPL(iscsi_session_chkready);
372
Mike Christie7b8631b2006-01-13 18:05:50 -0600373static void iscsi_session_release(struct device *dev)
374{
375 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600376 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Mike Christie7b8631b2006-01-13 18:05:50 -0600378 shost = iscsi_session_to_shost(session);
379 scsi_host_put(shost);
380 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Mike Christie7b8631b2006-01-13 18:05:50 -0600383static int iscsi_is_session_dev(const struct device *dev)
384{
385 return dev->release == iscsi_session_release;
386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Mike Christiea4804cd2008-05-21 15:54:00 -0500388static int iscsi_iter_session_fn(struct device *dev, void *data)
389{
390 void (* fn) (struct iscsi_cls_session *) = data;
391
392 if (!iscsi_is_session_dev(dev))
393 return 0;
394 fn(iscsi_dev_to_session(dev));
395 return 0;
396}
397
398void iscsi_host_for_each_session(struct Scsi_Host *shost,
399 void (*fn)(struct iscsi_cls_session *))
400{
401 device_for_each_child(&shost->shost_gendev, fn,
402 iscsi_iter_session_fn);
403}
404EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
405
Mike Christie8aae18a2008-01-31 13:36:48 -0600406/**
407 * iscsi_scan_finished - helper to report when running scans are done
408 * @shost: scsi host
409 * @time: scan run time
410 *
411 * This function can be used by drives like qla4xxx to report to the scsi
412 * layer when the scans it kicked off at module load time are done.
413 */
414int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
415{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500416 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600417 /*
418 * qla4xxx will have kicked off some session unblocks before calling
419 * scsi_scan_host, so just wait for them to complete.
420 */
421 return !atomic_read(&ihost->nr_scans);
422}
423EXPORT_SYMBOL_GPL(iscsi_scan_finished);
424
Mike Christie79706342008-05-21 15:54:12 -0500425struct iscsi_scan_data {
426 unsigned int channel;
427 unsigned int id;
428 unsigned int lun;
429};
430
431static int iscsi_user_scan_session(struct device *dev, void *data)
432{
433 struct iscsi_scan_data *scan_data = data;
434 struct iscsi_cls_session *session;
435 struct Scsi_Host *shost;
436 struct iscsi_cls_host *ihost;
437 unsigned long flags;
438 unsigned int id;
439
440 if (!iscsi_is_session_dev(dev))
441 return 0;
442
443 session = iscsi_dev_to_session(dev);
444 shost = iscsi_session_to_shost(session);
445 ihost = shost->shost_data;
446
447 mutex_lock(&ihost->mutex);
448 spin_lock_irqsave(&session->lock, flags);
449 if (session->state != ISCSI_SESSION_LOGGED_IN) {
450 spin_unlock_irqrestore(&session->lock, flags);
451 mutex_unlock(&ihost->mutex);
452 return 0;
453 }
454 id = session->target_id;
455 spin_unlock_irqrestore(&session->lock, flags);
456
457 if (id != ISCSI_MAX_TARGET) {
458 if ((scan_data->channel == SCAN_WILD_CARD ||
459 scan_data->channel == 0) &&
460 (scan_data->id == SCAN_WILD_CARD ||
461 scan_data->id == id))
462 scsi_scan_target(&session->dev, 0, id,
463 scan_data->lun, 1);
464 }
465 mutex_unlock(&ihost->mutex);
466 return 0;
467}
468
Mike Christie30a6c652006-04-06 21:13:39 -0500469static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
470 uint id, uint lun)
471{
Mike Christie79706342008-05-21 15:54:12 -0500472 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500473
Mike Christie79706342008-05-21 15:54:12 -0500474 scan_data.channel = channel;
475 scan_data.id = id;
476 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500477
Mike Christie79706342008-05-21 15:54:12 -0500478 return device_for_each_child(&shost->shost_gendev, &scan_data,
479 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500480}
481
Mike Christiebd976f62008-01-31 13:36:46 -0600482static void iscsi_scan_session(struct work_struct *work)
483{
484 struct iscsi_cls_session *session =
485 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600486 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500487 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500488 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600489
Mike Christie79706342008-05-21 15:54:12 -0500490 scan_data.channel = 0;
491 scan_data.id = SCAN_WILD_CARD;
492 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600493
Mike Christie79706342008-05-21 15:54:12 -0500494 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600495 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600496}
497
David Howellsc4028952006-11-22 14:57:56 +0000498static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500499{
David Howellsc4028952006-11-22 14:57:56 +0000500 struct iscsi_cls_session *session =
501 container_of(work, struct iscsi_cls_session,
502 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600503 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500504
Mike Christie322d7392008-01-31 13:36:52 -0600505 iscsi_cls_session_printk(KERN_INFO, session,
506 "session recovery timed out after %d secs\n",
507 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500508
Mike Christie6eabafb2008-01-31 13:36:43 -0600509 spin_lock_irqsave(&session->lock, flags);
510 switch (session->state) {
511 case ISCSI_SESSION_FAILED:
512 session->state = ISCSI_SESSION_FREE;
513 break;
514 case ISCSI_SESSION_LOGGED_IN:
515 case ISCSI_SESSION_FREE:
516 /* we raced with the unblock's flush */
517 spin_unlock_irqrestore(&session->lock, flags);
518 return;
519 }
520 spin_unlock_irqrestore(&session->lock, flags);
521
Mike Christie30a6c652006-04-06 21:13:39 -0500522 if (session->transport->session_recovery_timedout)
523 session->transport->session_recovery_timedout(session);
524
525 scsi_target_unblock(&session->dev);
526}
527
Mike Christie45ab33b2008-03-04 13:26:55 -0600528static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500529{
Mike Christie45ab33b2008-03-04 13:26:55 -0600530 struct iscsi_cls_session *session =
531 container_of(work, struct iscsi_cls_session,
532 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600533 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500534 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600535 unsigned long flags;
536
Mike Christie45ab33b2008-03-04 13:26:55 -0600537 /*
538 * The recovery and unblock work get run from the same workqueue,
539 * so try to cancel it if it was going to run after this unblock.
540 */
541 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600542 spin_lock_irqsave(&session->lock, flags);
543 session->state = ISCSI_SESSION_LOGGED_IN;
544 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600545 /* start IO */
546 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600547 /*
548 * Only do kernel scanning if the driver is properly hooked into
549 * the async scanning code (drivers like iscsi_tcp do login and
550 * scanning from userspace).
551 */
552 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600553 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600554 atomic_inc(&ihost->nr_scans);
555 }
Mike Christie6eabafb2008-01-31 13:36:43 -0600556}
Mike Christie45ab33b2008-03-04 13:26:55 -0600557
558/**
559 * iscsi_unblock_session - set a session as logged in and start IO.
560 * @session: iscsi session
561 *
562 * Mark a session as ready to accept IO.
563 */
564void iscsi_unblock_session(struct iscsi_cls_session *session)
565{
566 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
567 /*
568 * make sure all the events have completed before tell the driver
569 * it is safe
570 */
571 flush_workqueue(iscsi_eh_timer_workq);
572}
Mike Christie30a6c652006-04-06 21:13:39 -0500573EXPORT_SYMBOL_GPL(iscsi_unblock_session);
574
Mike Christie45ab33b2008-03-04 13:26:55 -0600575static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500576{
Mike Christie45ab33b2008-03-04 13:26:55 -0600577 struct iscsi_cls_session *session =
578 container_of(work, struct iscsi_cls_session,
579 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600580 unsigned long flags;
581
582 spin_lock_irqsave(&session->lock, flags);
583 session->state = ISCSI_SESSION_FAILED;
584 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500585 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600586 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
587 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500588}
Mike Christie45ab33b2008-03-04 13:26:55 -0600589
590void iscsi_block_session(struct iscsi_cls_session *session)
591{
592 queue_work(iscsi_eh_timer_workq, &session->block_work);
593}
Mike Christie30a6c652006-04-06 21:13:39 -0500594EXPORT_SYMBOL_GPL(iscsi_block_session);
595
Mike Christie26974782007-12-13 12:43:29 -0600596static void __iscsi_unbind_session(struct work_struct *work)
597{
598 struct iscsi_cls_session *session =
599 container_of(work, struct iscsi_cls_session,
600 unbind_work);
601 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500602 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500603 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600604
605 /* Prevent new scans and make sure scanning is not in progress */
606 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500607 spin_lock_irqsave(&session->lock, flags);
608 if (session->target_id == ISCSI_MAX_TARGET) {
609 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600610 mutex_unlock(&ihost->mutex);
611 return;
612 }
Mike Christie79706342008-05-21 15:54:12 -0500613 session->target_id = ISCSI_MAX_TARGET;
614 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600615 mutex_unlock(&ihost->mutex);
616
617 scsi_remove_target(&session->dev);
618 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
619}
620
Mike Christie7b8631b2006-01-13 18:05:50 -0600621struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500622iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
623 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600624{
625 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Mike Christie5d91e202008-05-21 15:54:01 -0500627 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500628 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600629 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500630 return NULL;
631
Mike Christie7b8631b2006-01-13 18:05:50 -0600632 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500633 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600634 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000635 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500636 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600637 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
638 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600639 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600640 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600641 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600642
Mike Christie6a8a0d32006-06-28 12:00:31 -0500643 /* this is released in the dev's release function */
644 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500645 session->dev.parent = &shost->shost_gendev;
646 session->dev.release = iscsi_session_release;
647 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500648 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500649 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500650 return session;
651}
652EXPORT_SYMBOL_GPL(iscsi_alloc_session);
653
Mike Christie79706342008-05-21 15:54:12 -0500654static int iscsi_get_next_target_id(struct device *dev, void *data)
655{
656 struct iscsi_cls_session *session;
657 unsigned long flags;
658 int err = 0;
659
660 if (!iscsi_is_session_dev(dev))
661 return 0;
662
663 session = iscsi_dev_to_session(dev);
664 spin_lock_irqsave(&session->lock, flags);
665 if (*((unsigned int *) data) == session->target_id)
666 err = -EEXIST;
667 spin_unlock_irqrestore(&session->lock, flags);
668 return err;
669}
670
Mike Christie6a8a0d32006-06-28 12:00:31 -0500671int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500672{
673 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500674 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600675 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500676 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500677 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500678
Mike Christie30a6c652006-04-06 21:13:39 -0500679 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600680 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500681
682 if (id == ISCSI_MAX_TARGET) {
683 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
684 err = device_for_each_child(&shost->shost_gendev, &id,
685 iscsi_get_next_target_id);
686 if (!err)
687 break;
688 }
689
690 if (id == ISCSI_MAX_TARGET) {
691 iscsi_cls_session_printk(KERN_ERR, session,
692 "Too many iscsi targets. Max "
693 "number of targets is %d.\n",
694 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +0530695 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -0500696 goto release_host;
697 }
698 }
699 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500700
Kay Sievers71610f52008-12-03 22:41:36 +0100701 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500702 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600703 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600704 iscsi_cls_session_printk(KERN_ERR, session,
705 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500706 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600707 }
708 transport_register_device(&session->dev);
709
Mike Christie26974782007-12-13 12:43:29 -0600710 spin_lock_irqsave(&sesslock, flags);
711 list_add(&session->sess_list, &sesslist);
712 spin_unlock_irqrestore(&sesslock, flags);
713
Mike Christie26974782007-12-13 12:43:29 -0600714 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500715 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500716
Mike Christie8434aa82006-06-28 12:00:30 -0500717release_host:
718 scsi_host_put(shost);
719 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600720}
Mike Christie8434aa82006-06-28 12:00:30 -0500721EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600722
723/**
Mike Christie8434aa82006-06-28 12:00:30 -0500724 * iscsi_create_session - create iscsi class session
725 * @shost: scsi host
726 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500727 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500728 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600729 *
Mike Christie8434aa82006-06-28 12:00:30 -0500730 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500731 */
Mike Christie8434aa82006-06-28 12:00:30 -0500732struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500733iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
734 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500735{
736 struct iscsi_cls_session *session;
737
Mike Christie5d91e202008-05-21 15:54:01 -0500738 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500739 if (!session)
740 return NULL;
741
Mike Christie6a8a0d32006-06-28 12:00:31 -0500742 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500743 iscsi_free_session(session);
744 return NULL;
745 }
746 return session;
747}
748EXPORT_SYMBOL_GPL(iscsi_create_session);
749
Mike Christie26974782007-12-13 12:43:29 -0600750static void iscsi_conn_release(struct device *dev)
751{
752 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
753 struct device *parent = conn->dev.parent;
754
755 kfree(conn);
756 put_device(parent);
757}
758
759static int iscsi_is_conn_dev(const struct device *dev)
760{
761 return dev->release == iscsi_conn_release;
762}
763
764static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
765{
766 if (!iscsi_is_conn_dev(dev))
767 return 0;
768 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
769}
770
Mike Christie8434aa82006-06-28 12:00:30 -0500771void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600772{
Mike Christie30a6c652006-04-06 21:13:39 -0500773 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -0600774 unsigned long flags;
775 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500776
Mike Christie26974782007-12-13 12:43:29 -0600777 spin_lock_irqsave(&sesslock, flags);
778 list_del(&session->sess_list);
779 spin_unlock_irqrestore(&sesslock, flags);
780
Mike Christie45ab33b2008-03-04 13:26:55 -0600781 /* make sure there are no blocks/unblocks queued */
782 flush_workqueue(iscsi_eh_timer_workq);
783 /* make sure the timedout callout is not running */
784 if (!cancel_delayed_work(&session->recovery_work))
785 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600786 /*
787 * If we are blocked let commands flow again. The lld or iscsi
788 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -0600789 * We assume that LLD will not be calling block/unblock while
790 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -0600791 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600792 spin_lock_irqsave(&session->lock, flags);
793 session->state = ISCSI_SESSION_FREE;
794 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -0600795
Mike Christie45ab33b2008-03-04 13:26:55 -0600796 scsi_target_unblock(&session->dev);
797 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -0600798 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -0600799 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -0500800
Mike Christie26974782007-12-13 12:43:29 -0600801 /* hw iscsi may not have removed all connections from session */
802 err = device_for_each_child(&session->dev, NULL,
803 iscsi_iter_destroy_conn_fn);
804 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600805 iscsi_cls_session_printk(KERN_ERR, session,
806 "Could not delete all connections "
807 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500808
Mike Christie7b8631b2006-01-13 18:05:50 -0600809 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500810 device_del(&session->dev);
811}
812EXPORT_SYMBOL_GPL(iscsi_remove_session);
813
814void iscsi_free_session(struct iscsi_cls_session *session)
815{
Mike Christie26974782007-12-13 12:43:29 -0600816 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500817 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600818}
Mike Christie8434aa82006-06-28 12:00:30 -0500819EXPORT_SYMBOL_GPL(iscsi_free_session);
820
821/**
822 * iscsi_destroy_session - destroy iscsi session
823 * @session: iscsi_session
824 *
825 * Can be called by a LLD or iscsi_transport. There must not be
826 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500827 */
Mike Christie8434aa82006-06-28 12:00:30 -0500828int iscsi_destroy_session(struct iscsi_cls_session *session)
829{
830 iscsi_remove_session(session);
831 iscsi_free_session(session);
832 return 0;
833}
Mike Christie7b8631b2006-01-13 18:05:50 -0600834EXPORT_SYMBOL_GPL(iscsi_destroy_session);
835
Mike Christie7b8631b2006-01-13 18:05:50 -0600836/**
837 * iscsi_create_conn - create iscsi class connection
838 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -0500839 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -0600840 * @cid: connection id
841 *
842 * This can be called from a LLD or iscsi_transport. The connection
843 * is child of the session so cid must be unique for all connections
844 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500845 *
846 * Since we do not support MCS, cid will normally be zero. In some cases
847 * for software iscsi we could be trying to preallocate a connection struct
848 * in which case there could be two connection structs and cid would be
849 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500850 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600851struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -0500852iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -0600853{
854 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600855 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600856 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600857 int err;
858
Mike Christie5d91e202008-05-21 15:54:01 -0500859 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600860 if (!conn)
861 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -0500862 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600863 conn->dd_data = &conn[1];
864
865 INIT_LIST_HEAD(&conn->conn_list);
866 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500867 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600868
869 /* this is released in the dev's release function */
870 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400871 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500872
Kay Sievers71610f52008-12-03 22:41:36 +0100873 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600874 conn->dev.parent = &session->dev;
875 conn->dev.release = iscsi_conn_release;
876 err = device_register(&conn->dev);
877 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600878 iscsi_cls_session_printk(KERN_ERR, session, "could not "
879 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600880 goto release_parent_ref;
881 }
882 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600883
884 spin_lock_irqsave(&connlock, flags);
885 list_add(&conn->conn_list, &connlist);
886 conn->active = 1;
887 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600888 return conn;
889
890release_parent_ref:
891 put_device(&session->dev);
892free_conn:
893 kfree(conn);
894 return NULL;
895}
896
897EXPORT_SYMBOL_GPL(iscsi_create_conn);
898
899/**
900 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500901 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600902 *
Mike Christie26974782007-12-13 12:43:29 -0600903 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500904 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600905int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
906{
Mike Christie26974782007-12-13 12:43:29 -0600907 unsigned long flags;
908
909 spin_lock_irqsave(&connlock, flags);
910 conn->active = 0;
911 list_del(&conn->conn_list);
912 spin_unlock_irqrestore(&connlock, flags);
913
Mike Christie7b8631b2006-01-13 18:05:50 -0600914 transport_unregister_device(&conn->dev);
915 device_unregister(&conn->dev);
916 return 0;
917}
Mike Christie7b8631b2006-01-13 18:05:50 -0600918EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
919
920/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600921 * iscsi interface functions
922 */
Alex Aizman0896b752005-08-04 19:33:07 -0700923static struct iscsi_internal *
924iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Alex Aizman0896b752005-08-04 19:33:07 -0700926 struct iscsi_internal *priv;
927 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Alex Aizman0896b752005-08-04 19:33:07 -0700929 spin_lock_irqsave(&iscsi_transport_lock, flags);
930 list_for_each_entry(priv, &iscsi_transports, list) {
931 if (tt == priv->iscsi_transport) {
932 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
933 return priv;
934 }
935 }
936 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
937 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Alex Aizman0896b752005-08-04 19:33:07 -0700940static int
Michael Chan43514772009-06-08 18:14:41 -0700941iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500942{
Michael Chan43514772009-06-08 18:14:41 -0700943 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -0700944}
945
Mike Christie7b7232f2006-02-01 21:06:49 -0600946int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700947 char *data, uint32_t data_size)
948{
949 struct nlmsghdr *nlh;
950 struct sk_buff *skb;
951 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700952 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500953 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700954 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
955 data_size);
956
Mike Christie790f39a2006-05-18 20:31:39 -0500957 priv = iscsi_if_transport_lookup(conn->transport);
958 if (!priv)
959 return -EINVAL;
960
Mike Christie43a145a2006-10-16 18:09:38 -0400961 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700962 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -0500963 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -0600964 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
965 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700966 return -ENOMEM;
967 }
968
Michael Chan43514772009-06-08 18:14:41 -0700969 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700970 ev = NLMSG_DATA(nlh);
971 memset(ev, 0, sizeof(*ev));
972 ev->transport_handle = iscsi_handle(conn->transport);
973 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -0500974 ev->r.recv_req.cid = conn->cid;
975 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700976 pdu = (char*)ev + sizeof(*ev);
977 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
978 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
979
Michael Chan43514772009-06-08 18:14:41 -0700980 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700981}
982EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
983
Michael Chan43514772009-06-08 18:14:41 -0700984int iscsi_offload_mesg(struct Scsi_Host *shost,
985 struct iscsi_transport *transport, uint32_t type,
986 char *data, uint16_t data_size)
987{
988 struct nlmsghdr *nlh;
989 struct sk_buff *skb;
990 struct iscsi_uevent *ev;
991 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
992
993 skb = alloc_skb(len, GFP_NOIO);
994 if (!skb) {
995 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
996 return -ENOMEM;
997 }
998
999 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1000 ev = NLMSG_DATA(nlh);
1001 memset(ev, 0, sizeof(*ev));
1002 ev->type = type;
1003 ev->transport_handle = iscsi_handle(transport);
1004 switch (type) {
1005 case ISCSI_KEVENT_PATH_REQ:
1006 ev->r.req_path.host_no = shost->host_no;
1007 break;
1008 case ISCSI_KEVENT_IF_DOWN:
1009 ev->r.notify_if_down.host_no = shost->host_no;
1010 break;
1011 }
1012
1013 memcpy((char *)ev + sizeof(*ev), data, data_size);
1014
1015 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_NOIO);
1016}
1017EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1018
Mike Christiee5bd7b52008-09-24 11:46:10 -05001019void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001020{
1021 struct nlmsghdr *nlh;
1022 struct sk_buff *skb;
1023 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001024 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001025 int len = NLMSG_SPACE(sizeof(*ev));
1026
Mike Christie790f39a2006-05-18 20:31:39 -05001027 priv = iscsi_if_transport_lookup(conn->transport);
1028 if (!priv)
1029 return;
1030
Mike Christie43a145a2006-10-16 18:09:38 -04001031 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001032 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001033 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1034 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001035 return;
1036 }
1037
Michael Chan43514772009-06-08 18:14:41 -07001038 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001039 ev = NLMSG_DATA(nlh);
1040 ev->transport_handle = iscsi_handle(conn->transport);
1041 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001042 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001043 ev->r.connerror.cid = conn->cid;
1044 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001045
Michael Chan43514772009-06-08 18:14:41 -07001046 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001047
Mike Christie322d7392008-01-31 13:36:52 -06001048 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1049 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001050}
Mike Christiee5bd7b52008-09-24 11:46:10 -05001051EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001052
1053static int
Michael Chan43514772009-06-08 18:14:41 -07001054iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1055 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001056{
1057 struct sk_buff *skb;
1058 struct nlmsghdr *nlh;
1059 int len = NLMSG_SPACE(size);
1060 int flags = multi ? NLM_F_MULTI : 0;
1061 int t = done ? NLMSG_DONE : type;
1062
Mike Christie43a145a2006-10-16 18:09:38 -04001063 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001064 if (!skb) {
1065 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1066 return -ENOMEM;
1067 }
Alex Aizman0896b752005-08-04 19:33:07 -07001068
Michael Chan43514772009-06-08 18:14:41 -07001069 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001070 nlh->nlmsg_flags = flags;
1071 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001072 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001073}
1074
1075static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001076iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001077{
1078 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1079 struct iscsi_stats *stats;
1080 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001081 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001082 struct nlmsghdr *nlhstat;
1083 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001084 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001085 int len = NLMSG_SPACE(sizeof(*ev) +
1086 sizeof(struct iscsi_stats) +
1087 sizeof(struct iscsi_stats_custom) *
1088 ISCSI_STATS_CUSTOM_MAX);
1089 int err = 0;
1090
Mike Christie790f39a2006-05-18 20:31:39 -05001091 priv = iscsi_if_transport_lookup(transport);
1092 if (!priv)
1093 return -EINVAL;
1094
Mike Christieb5c7a122006-04-06 21:13:33 -05001095 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001096 if (!conn)
1097 return -EEXIST;
1098
1099 do {
1100 int actual_size;
1101
Mike Christie43a145a2006-10-16 18:09:38 -04001102 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001103 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001104 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1105 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001106 return -ENOMEM;
1107 }
1108
Michael Chan43514772009-06-08 18:14:41 -07001109 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001110 (len - sizeof(*nlhstat)), 0);
1111 evstat = NLMSG_DATA(nlhstat);
1112 memset(evstat, 0, sizeof(*evstat));
1113 evstat->transport_handle = iscsi_handle(conn->transport);
1114 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001115 evstat->u.get_stats.cid =
1116 ev->u.get_stats.cid;
1117 evstat->u.get_stats.sid =
1118 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001119 stats = (struct iscsi_stats *)
1120 ((char*)evstat + sizeof(*evstat));
1121 memset(stats, 0, sizeof(*stats));
1122
Mike Christie7b7232f2006-02-01 21:06:49 -06001123 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001124 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1125 sizeof(struct iscsi_stats) +
1126 sizeof(struct iscsi_stats_custom) *
1127 stats->custom_length);
1128 actual_size -= sizeof(*nlhstat);
1129 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001130 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001131 nlhstat->nlmsg_len = actual_size;
1132
Michael Chan43514772009-06-08 18:14:41 -07001133 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1134 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001135 } while (err < 0 && err != -ECONNREFUSED);
1136
1137 return err;
1138}
1139
Mike Christie53cb8a12006-06-28 12:00:32 -05001140/**
Mike Christie26974782007-12-13 12:43:29 -06001141 * iscsi_session_event - send session destr. completion event
1142 * @session: iscsi class session
1143 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001144 */
Mike Christie26974782007-12-13 12:43:29 -06001145int iscsi_session_event(struct iscsi_cls_session *session,
1146 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001147{
1148 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001149 struct Scsi_Host *shost;
1150 struct iscsi_uevent *ev;
1151 struct sk_buff *skb;
1152 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001153 int rc, len = NLMSG_SPACE(sizeof(*ev));
1154
Mike Christie26974782007-12-13 12:43:29 -06001155 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001156 if (!priv)
1157 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001158 shost = iscsi_session_to_shost(session);
1159
Mike Christie43a145a2006-10-16 18:09:38 -04001160 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001161 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001162 iscsi_cls_session_printk(KERN_ERR, session,
1163 "Cannot notify userspace of session "
1164 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001165 return -ENOMEM;
1166 }
1167
Michael Chan43514772009-06-08 18:14:41 -07001168 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001169 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001170 ev->transport_handle = iscsi_handle(session->transport);
1171
1172 ev->type = event;
1173 switch (event) {
1174 case ISCSI_KEVENT_DESTROY_SESSION:
1175 ev->r.d_session.host_no = shost->host_no;
1176 ev->r.d_session.sid = session->sid;
1177 break;
1178 case ISCSI_KEVENT_CREATE_SESSION:
1179 ev->r.c_session_ret.host_no = shost->host_no;
1180 ev->r.c_session_ret.sid = session->sid;
1181 break;
1182 case ISCSI_KEVENT_UNBIND_SESSION:
1183 ev->r.unbind_session.host_no = shost->host_no;
1184 ev->r.unbind_session.sid = session->sid;
1185 break;
1186 default:
Mike Christie322d7392008-01-31 13:36:52 -06001187 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1188 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001189 kfree_skb(skb);
1190 return -EINVAL;
1191 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001192
1193 /*
1194 * this will occur if the daemon is not up, so we just warn
1195 * the user and when the daemon is restarted it will handle it
1196 */
Michael Chan43514772009-06-08 18:14:41 -07001197 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001198 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001199 iscsi_cls_session_printk(KERN_ERR, session,
1200 "Cannot notify userspace of session "
1201 "event %u. Check iscsi daemon\n",
1202 event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001203 return rc;
1204}
Mike Christie26974782007-12-13 12:43:29 -06001205EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001206
Alex Aizman0896b752005-08-04 19:33:07 -07001207static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001208iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1209 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001210 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001211{
1212 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001213 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001214 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001215
Mike Christied82ff9be2008-05-21 15:54:13 -05001216 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001217 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001218 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001219 return -ENOMEM;
1220
Mike Christie5e7facb2009-03-05 14:46:06 -06001221 shost = iscsi_session_to_shost(session);
1222 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001223 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001224 return 0;
1225}
1226
1227static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001228iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001229{
Mike Christie7b8631b2006-01-13 18:05:50 -06001230 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001231 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001232
Mike Christieb5c7a122006-04-06 21:13:33 -05001233 session = iscsi_session_lookup(ev->u.c_conn.sid);
1234 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001235 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001236 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001237 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001238 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001239
Mike Christie7b7232f2006-02-01 21:06:49 -06001240 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001241 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001242 iscsi_cls_session_printk(KERN_ERR, session,
1243 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001244 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001245 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001246
Mike Christieb5c7a122006-04-06 21:13:33 -05001247 ev->r.c_conn_ret.sid = session->sid;
1248 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001249 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001250}
1251
1252static int
1253iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1254{
Mike Christie7b8631b2006-01-13 18:05:50 -06001255 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001256
Mike Christieb5c7a122006-04-06 21:13:33 -05001257 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001258 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001259 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001260
Mike Christie7b8631b2006-01-13 18:05:50 -06001261 if (transport->destroy_conn)
1262 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001263 return 0;
1264}
1265
Mike Christiefd7255f2006-04-06 21:13:36 -05001266static int
1267iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1268{
1269 char *data = (char*)ev + sizeof(*ev);
1270 struct iscsi_cls_conn *conn;
1271 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001272 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001273
1274 session = iscsi_session_lookup(ev->u.set_param.sid);
1275 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1276 if (!conn || !session)
1277 return -EINVAL;
1278
1279 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001280 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001281 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001282 if (value != 0)
1283 session->recovery_tmo = value;
1284 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001285 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001286 err = transport->set_param(conn, ev->u.set_param.param,
1287 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001288 }
1289
1290 return err;
1291}
1292
Mike Christie10eb0f02009-05-13 17:57:38 -05001293static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1294 struct iscsi_uevent *ev, int msg_type)
1295{
1296 struct iscsi_endpoint *ep;
1297 struct sockaddr *dst_addr;
1298 struct Scsi_Host *shost = NULL;
1299 int non_blocking, err = 0;
1300
1301 if (!transport->ep_connect)
1302 return -EINVAL;
1303
1304 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1305 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1306 if (!shost) {
1307 printk(KERN_ERR "ep connect failed. Could not find "
1308 "host no %u\n",
1309 ev->u.ep_connect_through_host.host_no);
1310 return -ENODEV;
1311 }
1312 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1313 } else
1314 non_blocking = ev->u.ep_connect.non_blocking;
1315
1316 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1317 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1318 if (IS_ERR(ep)) {
1319 err = PTR_ERR(ep);
1320 goto release_host;
1321 }
1322
1323 ev->r.ep_connect_ret.handle = ep->id;
1324release_host:
1325 if (shost)
1326 scsi_host_put(shost);
1327 return err;
1328}
1329
Mike Christie7b8631b2006-01-13 18:05:50 -06001330static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001331iscsi_if_transport_ep(struct iscsi_transport *transport,
1332 struct iscsi_uevent *ev, int msg_type)
1333{
Mike Christied82ff9be2008-05-21 15:54:13 -05001334 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001335 int rc = 0;
1336
1337 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001338 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001339 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001340 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001341 break;
1342 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1343 if (!transport->ep_poll)
1344 return -EINVAL;
1345
Mike Christied82ff9be2008-05-21 15:54:13 -05001346 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1347 if (!ep)
1348 return -EINVAL;
1349
1350 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001351 ev->u.ep_poll.timeout_ms);
1352 break;
1353 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1354 if (!transport->ep_disconnect)
1355 return -EINVAL;
1356
Mike Christied82ff9be2008-05-21 15:54:13 -05001357 ep = iscsi_lookup_endpoint(ev->u.ep_disconnect.ep_handle);
1358 if (!ep)
1359 return -EINVAL;
1360
1361 transport->ep_disconnect(ep);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001362 break;
1363 }
1364 return rc;
1365}
1366
1367static int
Mike Christie01cb2252006-06-28 12:00:22 -05001368iscsi_tgt_dscvr(struct iscsi_transport *transport,
1369 struct iscsi_uevent *ev)
1370{
Mike Christie2174a042007-05-30 12:57:10 -05001371 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001372 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001373 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001374
1375 if (!transport->tgt_dscvr)
1376 return -EINVAL;
1377
Mike Christie2174a042007-05-30 12:57:10 -05001378 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001379 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001380 printk(KERN_ERR "target discovery could not find host no %u\n",
1381 ev->u.tgt_dscvr.host_no);
1382 return -ENODEV;
1383 }
1384
1385
Mike Christie01cb2252006-06-28 12:00:22 -05001386 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001387 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1388 ev->u.tgt_dscvr.enable, dst_addr);
1389 scsi_host_put(shost);
1390 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001391}
1392
1393static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001394iscsi_set_host_param(struct iscsi_transport *transport,
1395 struct iscsi_uevent *ev)
1396{
1397 char *data = (char*)ev + sizeof(*ev);
1398 struct Scsi_Host *shost;
1399 int err;
1400
1401 if (!transport->set_host_param)
1402 return -ENOSYS;
1403
1404 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001405 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001406 printk(KERN_ERR "set_host_param could not find host no %u\n",
1407 ev->u.set_host_param.host_no);
1408 return -ENODEV;
1409 }
1410
1411 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1412 data, ev->u.set_host_param.len);
1413 scsi_host_put(shost);
1414 return err;
1415}
1416
1417static int
Michael Chan43514772009-06-08 18:14:41 -07001418iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1419{
1420 struct Scsi_Host *shost;
1421 struct iscsi_path *params;
1422 int err;
1423
1424 if (!transport->set_path)
1425 return -ENOSYS;
1426
1427 shost = scsi_host_lookup(ev->u.set_path.host_no);
1428 if (!shost) {
1429 printk(KERN_ERR "set path could not find host no %u\n",
1430 ev->u.set_path.host_no);
1431 return -ENODEV;
1432 }
1433
1434 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1435 err = transport->set_path(shost, params);
1436
1437 scsi_host_put(shost);
1438 return err;
1439}
1440
1441static int
1442iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001443{
1444 int err = 0;
1445 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1446 struct iscsi_transport *transport = NULL;
1447 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001448 struct iscsi_cls_session *session;
1449 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001450 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001451
Michael Chan43514772009-06-08 18:14:41 -07001452 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1453 *group = ISCSI_NL_GRP_UIP;
1454 else
1455 *group = ISCSI_NL_GRP_ISCSID;
1456
Alex Aizman0896b752005-08-04 19:33:07 -07001457 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1458 if (!priv)
1459 return -EINVAL;
1460 transport = priv->iscsi_transport;
1461
Mike Christie7b7232f2006-02-01 21:06:49 -06001462 if (!try_module_get(transport->owner))
1463 return -EINVAL;
1464
Alex Aizman0896b752005-08-04 19:33:07 -07001465 switch (nlh->nlmsg_type) {
1466 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001467 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001468 ev->u.c_session.initial_cmdsn,
1469 ev->u.c_session.cmds_max,
1470 ev->u.c_session.queue_depth);
1471 break;
1472 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001473 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001474 if (!ep) {
1475 err = -EINVAL;
1476 break;
1477 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001478
1479 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001480 ev->u.c_bound_session.initial_cmdsn,
1481 ev->u.c_bound_session.cmds_max,
1482 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001483 break;
1484 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001485 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001486 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001487 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001488 else
1489 err = -EINVAL;
1490 break;
1491 case ISCSI_UEVENT_UNBIND_SESSION:
1492 session = iscsi_session_lookup(ev->u.d_session.sid);
1493 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001494 scsi_queue_work(iscsi_session_to_shost(session),
1495 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001496 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001497 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001498 break;
1499 case ISCSI_UEVENT_CREATE_CONN:
1500 err = iscsi_if_create_conn(transport, ev);
1501 break;
1502 case ISCSI_UEVENT_DESTROY_CONN:
1503 err = iscsi_if_destroy_conn(transport, ev);
1504 break;
1505 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001506 session = iscsi_session_lookup(ev->u.b_conn.sid);
1507 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001508
1509 if (session && conn)
1510 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001511 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001512 ev->u.b_conn.is_leading);
1513 else
1514 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001515 break;
1516 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001517 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001518 break;
1519 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001520 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001521 if (conn)
1522 ev->r.retcode = transport->start_conn(conn);
1523 else
1524 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001525 break;
1526 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001527 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001528 if (conn)
1529 transport->stop_conn(conn, ev->u.stop_conn.flag);
1530 else
1531 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001532 break;
1533 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001534 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001535 if (conn)
1536 ev->r.retcode = transport->send_pdu(conn,
1537 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1538 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1539 ev->u.send_pdu.data_size);
1540 else
1541 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001542 break;
1543 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001544 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001545 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001546 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1547 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1548 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001549 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001550 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1551 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001552 case ISCSI_UEVENT_TGT_DSCVR:
1553 err = iscsi_tgt_dscvr(transport, ev);
1554 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001555 case ISCSI_UEVENT_SET_HOST_PARAM:
1556 err = iscsi_set_host_param(transport, ev);
1557 break;
Michael Chan43514772009-06-08 18:14:41 -07001558 case ISCSI_UEVENT_PATH_UPDATE:
1559 err = iscsi_set_path(transport, ev);
1560 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001561 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001562 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001563 break;
1564 }
1565
Mike Christie7b7232f2006-02-01 21:06:49 -06001566 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001567 return err;
1568}
1569
Mike Christieb5c7a122006-04-06 21:13:33 -05001570/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001571 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1572 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001573 */
Alex Aizman0896b752005-08-04 19:33:07 -07001574static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001575iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001576{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001577 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001578 while (skb->len >= NLMSG_SPACE(0)) {
1579 int err;
1580 uint32_t rlen;
1581 struct nlmsghdr *nlh;
1582 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07001583 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001584
1585 nlh = nlmsg_hdr(skb);
1586 if (nlh->nlmsg_len < sizeof(*nlh) ||
1587 skb->len < nlh->nlmsg_len) {
1588 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001589 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001590
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001591 ev = NLMSG_DATA(nlh);
1592 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1593 if (rlen > skb->len)
1594 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001595
Michael Chan43514772009-06-08 18:14:41 -07001596 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001597 if (err) {
1598 ev->type = ISCSI_KEVENT_IF_ERROR;
1599 ev->iferror = err;
1600 }
1601 do {
1602 /*
1603 * special case for GET_STATS:
1604 * on success - sending reply and stats from
1605 * inside of if_recv_msg(),
1606 * on error - fall through.
1607 */
1608 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001609 break;
Michael Chan43514772009-06-08 18:14:41 -07001610 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001611 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1612 } while (err < 0 && err != -ECONNREFUSED);
1613 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001614 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001615 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001616}
1617
Mike Christiefd7255f2006-04-06 21:13:36 -05001618#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001619struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001620 __ATTR(_name,_mode,_show,_store)
1621
Alex Aizman0896b752005-08-04 19:33:07 -07001622/*
1623 * iSCSI connection attrs
1624 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001625#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001627show_conn_param_##param(struct device *dev, \
1628 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001629{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001630 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001631 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001632 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001633}
1634
Mike Christiea54a52c2006-06-28 12:00:23 -05001635#define iscsi_conn_attr(field, param) \
1636 iscsi_conn_attr_show(param) \
1637static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001638 NULL);
1639
Mike Christiea54a52c2006-06-28 12:00:23 -05001640iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1641iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1642iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1643iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1644iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1645iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1646iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1647iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1648iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1649iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1650iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001651iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1652iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654/*
Alex Aizman0896b752005-08-04 19:33:07 -07001655 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
Mike Christieb2c64162007-05-30 12:57:16 -05001657#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001659show_session_param_##param(struct device *dev, \
1660 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001661{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001662 struct iscsi_cls_session *session = \
1663 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001664 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001665 \
1666 if (perm && !capable(CAP_SYS_ADMIN)) \
1667 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001668 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Mike Christieb2c64162007-05-30 12:57:16 -05001671#define iscsi_session_attr(field, param, perm) \
1672 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001673static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001674 NULL);
1675
Mike Christieb2c64162007-05-30 12:57:16 -05001676iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1677iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1678iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1679iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1680iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1681iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1682iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1683iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1684iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1685iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1686iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1687iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1688iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1689iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001690iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1691iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1692iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05001693iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
1694iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0)
Mike Christiefd7255f2006-04-06 21:13:36 -05001695
Mike Christie6eabafb2008-01-31 13:36:43 -06001696static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001697show_priv_session_state(struct device *dev, struct device_attribute *attr,
1698 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06001699{
Tony Jonesee959b02008-02-22 00:13:36 +01001700 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06001701 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1702}
1703static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1704 NULL);
1705
Mike Christiefd7255f2006-04-06 21:13:36 -05001706#define iscsi_priv_session_attr_show(field, format) \
1707static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001708show_priv_session_##field(struct device *dev, \
1709 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001710{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001711 struct iscsi_cls_session *session = \
1712 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001713 return sprintf(buf, format"\n", session->field); \
1714}
1715
1716#define iscsi_priv_session_attr(field, format) \
1717 iscsi_priv_session_attr_show(field, format) \
1718static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1719 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001720iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001721
Mike Christie1819dc82007-05-30 12:57:08 -05001722/*
1723 * iSCSI host attrs
1724 */
1725#define iscsi_host_attr_show(param) \
1726static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001727show_host_param_##param(struct device *dev, \
1728 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05001729{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001730 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05001731 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1732 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1733}
1734
1735#define iscsi_host_attr(field, param) \
1736 iscsi_host_attr_show(param) \
1737static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1738 NULL);
1739
Mike Christied8196ed2007-05-30 12:57:25 -05001740iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001741iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001742iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001743iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001744
Mike Christiefd7255f2006-04-06 21:13:36 -05001745#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1746do { \
Tony Jonesee959b02008-02-22 00:13:36 +01001747 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001748 count++; \
1749} while (0)
1750
Mike Christiea54a52c2006-06-28 12:00:23 -05001751
Mike Christiefd7255f2006-04-06 21:13:36 -05001752#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1753do { \
1754 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001755 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001757 } \
1758} while (0)
1759
Mike Christiefd7255f2006-04-06 21:13:36 -05001760#define SETUP_CONN_RD_ATTR(field, param_flag) \
1761do { \
1762 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001763 priv->conn_attrs[count] = &dev_attr_conn_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001764 count++; \
1765 } \
1766} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Mike Christie1819dc82007-05-30 12:57:08 -05001768#define SETUP_HOST_RD_ATTR(field, param_flag) \
1769do { \
1770 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001771 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05001772 count++; \
1773 } \
1774} while (0)
1775
Alex Aizman0896b752005-08-04 19:33:07 -07001776static int iscsi_session_match(struct attribute_container *cont,
1777 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Mike Christie7b8631b2006-01-13 18:05:50 -06001779 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001781 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Alex Aizman0896b752005-08-04 19:33:07 -07001783 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return 0;
1785
Mike Christie7b8631b2006-01-13 18:05:50 -06001786 session = iscsi_dev_to_session(dev);
1787 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001788 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 0;
1790
Alex Aizman0896b752005-08-04 19:33:07 -07001791 priv = to_iscsi_internal(shost->transportt);
1792 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1793 return 0;
1794
1795 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Alex Aizman0896b752005-08-04 19:33:07 -07001798static int iscsi_conn_match(struct attribute_container *cont,
1799 struct device *dev)
1800{
Mike Christie7b8631b2006-01-13 18:05:50 -06001801 struct iscsi_cls_session *session;
1802 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001803 struct Scsi_Host *shost;
1804 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Alex Aizman0896b752005-08-04 19:33:07 -07001806 if (!iscsi_is_conn_dev(dev))
1807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Mike Christie7b8631b2006-01-13 18:05:50 -06001809 conn = iscsi_dev_to_conn(dev);
1810 session = iscsi_dev_to_session(conn->dev.parent);
1811 shost = iscsi_session_to_shost(session);
1812
Alex Aizman0896b752005-08-04 19:33:07 -07001813 if (!shost->transportt)
1814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Alex Aizman0896b752005-08-04 19:33:07 -07001816 priv = to_iscsi_internal(shost->transportt);
1817 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1818 return 0;
1819
1820 return &priv->conn_cont.ac == cont;
1821}
1822
Mike Christie30a6c652006-04-06 21:13:39 -05001823static int iscsi_host_match(struct attribute_container *cont,
1824 struct device *dev)
1825{
1826 struct Scsi_Host *shost;
1827 struct iscsi_internal *priv;
1828
1829 if (!scsi_is_host_device(dev))
1830 return 0;
1831
1832 shost = dev_to_shost(dev);
1833 if (!shost->transportt ||
1834 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1835 return 0;
1836
1837 priv = to_iscsi_internal(shost->transportt);
1838 return &priv->t.host_attrs.ac == cont;
1839}
1840
Mike Christie7b8631b2006-01-13 18:05:50 -06001841struct scsi_transport_template *
1842iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001843{
1844 struct iscsi_internal *priv;
1845 unsigned long flags;
1846 int count = 0, err;
1847
1848 BUG_ON(!tt);
1849
1850 priv = iscsi_if_transport_lookup(tt);
1851 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001852 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001853
Jes Sorensen24669f752006-01-16 10:31:18 -05001854 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001855 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001856 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001857 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07001858 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001859 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06001860 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07001861
Tony Jonesee959b02008-02-22 00:13:36 +01001862 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01001863 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01001864 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001865 if (err)
1866 goto free_priv;
1867
Tony Jonesee959b02008-02-22 00:13:36 +01001868 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07001869 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01001870 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07001871
Mike Christie30a6c652006-04-06 21:13:39 -05001872 /* host parameters */
1873 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1874 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1875 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05001876 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001877 transport_container_register(&priv->t.host_attrs);
1878
Mike Christied8196ed2007-05-30 12:57:25 -05001879 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001880 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001881 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001882 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001883 BUG_ON(count > ISCSI_HOST_ATTRS);
1884 priv->host_attrs[count] = NULL;
1885 count = 0;
1886
Alex Aizman0896b752005-08-04 19:33:07 -07001887 /* connection parameters */
1888 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1889 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1890 priv->conn_cont.ac.match = iscsi_conn_match;
1891 transport_container_register(&priv->conn_cont);
1892
Mike Christiefd7255f2006-04-06 21:13:36 -05001893 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1894 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1895 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1896 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1897 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1898 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1899 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1900 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001901 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001902 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1903 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001904 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1905 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001906
1907 BUG_ON(count > ISCSI_CONN_ATTRS);
1908 priv->conn_attrs[count] = NULL;
1909 count = 0;
1910
1911 /* session parameters */
1912 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1913 priv->session_cont.ac.class = &iscsi_session_class.class;
1914 priv->session_cont.ac.match = iscsi_session_match;
1915 transport_container_register(&priv->session_cont);
1916
Mike Christiefd7255f2006-04-06 21:13:36 -05001917 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1918 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1919 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1920 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1921 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1922 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1923 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1924 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001925 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1926 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001927 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1928 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1929 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1930 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001931 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001932 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1933 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie88dfd342008-05-21 15:54:16 -05001934 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
1935 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
Mike Christie30a6c652006-04-06 21:13:39 -05001936 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06001937 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001940 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Alex Aizman0896b752005-08-04 19:33:07 -07001942 spin_lock_irqsave(&iscsi_transport_lock, flags);
1943 list_add(&priv->list, &iscsi_transports);
1944 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Alex Aizman0896b752005-08-04 19:33:07 -07001946 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001947 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Tony Jonesee959b02008-02-22 00:13:36 +01001949unregister_dev:
1950 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05001951 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001952free_priv:
1953 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001954 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
Alex Aizman0896b752005-08-04 19:33:07 -07001956EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Alex Aizman0896b752005-08-04 19:33:07 -07001958int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Alex Aizman0896b752005-08-04 19:33:07 -07001960 struct iscsi_internal *priv;
1961 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Alex Aizman0896b752005-08-04 19:33:07 -07001963 BUG_ON(!tt);
1964
Arjan van de Ven0b950672006-01-11 13:16:10 +01001965 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001966
1967 priv = iscsi_if_transport_lookup(tt);
1968 BUG_ON (!priv);
1969
Alex Aizman0896b752005-08-04 19:33:07 -07001970 spin_lock_irqsave(&iscsi_transport_lock, flags);
1971 list_del(&priv->list);
1972 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1973
1974 transport_container_unregister(&priv->conn_cont);
1975 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001976 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001977
Tony Jonesee959b02008-02-22 00:13:36 +01001978 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1979 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001980 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001981
1982 return 0;
1983}
1984EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986static __init int iscsi_transport_init(void)
1987{
Alex Aizman0896b752005-08-04 19:33:07 -07001988 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Meelis Roos09492602006-12-17 12:10:26 -06001990 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001991 ISCSI_TRANSPORT_VERSION);
1992
Mike Christie41be1442007-02-28 17:32:18 -06001993 atomic_set(&iscsi_session_nr, 0);
1994
Alex Aizman0896b752005-08-04 19:33:07 -07001995 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (err)
1997 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001998
Mike Christied82ff9be2008-05-21 15:54:13 -05001999 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002000 if (err)
2001 goto unregister_transport_class;
2002
Mike Christied82ff9be2008-05-21 15:54:13 -05002003 err = transport_class_register(&iscsi_host_class);
2004 if (err)
2005 goto unregister_endpoint_class;
2006
Mike Christie30a6c652006-04-06 21:13:39 -05002007 err = transport_class_register(&iscsi_connection_class);
2008 if (err)
2009 goto unregister_host_class;
2010
Alex Aizman0896b752005-08-04 19:33:07 -07002011 err = transport_class_register(&iscsi_session_class);
2012 if (err)
2013 goto unregister_conn_class;
2014
Mike Christied82ff9be2008-05-21 15:54:13 -05002015 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2016 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002017 if (!nls) {
2018 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002019 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002020 }
2021
Mike Christied8bf5412007-12-13 12:43:27 -06002022 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2023 if (!iscsi_eh_timer_workq)
2024 goto release_nls;
2025
Mike Christie43a145a2006-10-16 18:09:38 -04002026 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002027
Mike Christied8bf5412007-12-13 12:43:27 -06002028release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002029 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002030unregister_session_class:
2031 transport_class_unregister(&iscsi_session_class);
2032unregister_conn_class:
2033 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002034unregister_host_class:
2035 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002036unregister_endpoint_class:
2037 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002038unregister_transport_class:
2039 class_unregister(&iscsi_transport_class);
2040 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
2043static void __exit iscsi_transport_exit(void)
2044{
Mike Christied8bf5412007-12-13 12:43:27 -06002045 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002046 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002047 transport_class_unregister(&iscsi_connection_class);
2048 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002049 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002050 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002051 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054module_init(iscsi_transport_init);
2055module_exit(iscsi_transport_exit);
2056
Alex Aizman0896b752005-08-04 19:33:07 -07002057MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2058 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2059 "Alex Aizman <itn780@yahoo.com>");
2060MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002062MODULE_VERSION(ISCSI_TRANSPORT_VERSION);