blob: ad897df36615ff4b06bf70da9db57dc0ca0a21c9 [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
Mike Christie632248a2009-08-20 15:11:01 -050039static int dbg_session;
40module_param_named(debug_session, dbg_session, int,
41 S_IRUGO | S_IWUSR);
42MODULE_PARM_DESC(debug_session,
43 "Turn on debugging for sessions in scsi_transport_iscsi "
44 "module. Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47static int dbg_conn;
48module_param_named(debug_conn, dbg_conn, int,
49 S_IRUGO | S_IWUSR);
50MODULE_PARM_DESC(debug_conn,
51 "Turn on debugging for connections in scsi_transport_iscsi "
52 "module. Set to 1 to turn on, and zero to turn off. Default "
53 "is off.");
54
55#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (dbg_session) \
58 iscsi_cls_session_printk(KERN_INFO, _session, \
59 "%s: " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
63#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
64 do { \
65 if (dbg_conn) \
66 iscsi_cls_conn_printk(KERN_INFO, _conn, \
67 "%s: " dbg_fmt, \
68 __func__, ##arg); \
69 } while (0);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct iscsi_internal {
72 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070073 struct iscsi_transport *iscsi_transport;
74 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010075 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050076
Tony Jonesee959b02008-02-22 00:13:36 +010077 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070078 struct transport_container conn_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010079 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070080 struct transport_container session_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010081 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Mike Christie41be1442007-02-28 17:32:18 -060084static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060085static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050086
Alex Aizman0896b752005-08-04 19:33:07 -070087/*
88 * list of registered transports and lock that must
89 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010090 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070091 */
92static LIST_HEAD(iscsi_transports);
93static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alex Aizman0896b752005-08-04 19:33:07 -070095#define to_iscsi_internal(tmpl) \
96 container_of(tmpl, struct iscsi_internal, t)
97
Tony Jonesee959b02008-02-22 00:13:36 +010098#define dev_to_iscsi_internal(_dev) \
99 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700100
Tony Jonesee959b02008-02-22 00:13:36 +0100101static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700102{
Tony Jonesee959b02008-02-22 00:13:36 +0100103 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700104 kfree(priv);
105}
106
107/*
108 * iscsi_transport_class represents the iscsi_transports that are
109 * registered.
110 */
111static struct class iscsi_transport_class = {
112 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100113 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700114};
115
116static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100117show_transport_handle(struct device *dev, struct device_attribute *attr,
118 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700119{
Tony Jonesee959b02008-02-22 00:13:36 +0100120 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500121 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700122}
Tony Jonesee959b02008-02-22 00:13:36 +0100123static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700124
125#define show_transport_attr(name, format) \
126static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100127show_transport_##name(struct device *dev, \
128 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700129{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100130 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700131 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
132} \
Tony Jonesee959b02008-02-22 00:13:36 +0100133static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700134
135show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700136
137static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100138 &dev_attr_handle.attr,
139 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700140 NULL,
141};
142
143static struct attribute_group iscsi_transport_group = {
144 .attrs = iscsi_transport_attrs,
145};
146
Mike Christied82ff9be2008-05-21 15:54:13 -0500147/*
148 * iSCSI endpoint attrs
149 */
150#define iscsi_dev_to_endpoint(_dev) \
151 container_of(_dev, struct iscsi_endpoint, dev)
152
153#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
154struct device_attribute dev_attr_##_prefix##_##_name = \
155 __ATTR(_name,_mode,_show,_store)
156
157static void iscsi_endpoint_release(struct device *dev)
158{
159 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
160 kfree(ep);
161}
162
163static struct class iscsi_endpoint_class = {
164 .name = "iscsi_endpoint",
165 .dev_release = iscsi_endpoint_release,
166};
167
168static ssize_t
169show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
170{
171 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500172 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500173}
174static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
175
176static struct attribute *iscsi_endpoint_attrs[] = {
177 &dev_attr_ep_handle.attr,
178 NULL,
179};
180
181static struct attribute_group iscsi_endpoint_group = {
182 .attrs = iscsi_endpoint_attrs,
183};
184
185#define ISCSI_MAX_EPID -1
186
187static int iscsi_match_epid(struct device *dev, void *data)
188{
189 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500190 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500191
192 return *epid == ep->id;
193}
194
195struct iscsi_endpoint *
196iscsi_create_endpoint(int dd_size)
197{
198 struct device *dev;
199 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500200 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500201 int err;
202
203 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400204 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500205 iscsi_match_epid);
206 if (!dev)
207 break;
208 }
209 if (id == ISCSI_MAX_EPID) {
210 printk(KERN_ERR "Too many connections. Max supported %u\n",
211 ISCSI_MAX_EPID - 1);
212 return NULL;
213 }
214
215 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
216 if (!ep)
217 return NULL;
218
219 ep->id = id;
220 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100221 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500222 err = device_register(&ep->dev);
223 if (err)
224 goto free_ep;
225
226 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
227 if (err)
228 goto unregister_dev;
229
230 if (dd_size)
231 ep->dd_data = &ep[1];
232 return ep;
233
234unregister_dev:
235 device_unregister(&ep->dev);
236 return NULL;
237
238free_ep:
239 kfree(ep);
240 return NULL;
241}
242EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
243
244void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
245{
246 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
247 device_unregister(&ep->dev);
248}
249EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
250
251struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
252{
Mike Christief80f8682008-06-16 10:11:35 -0500253 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500254 struct device *dev;
255
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400256 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500257 iscsi_match_epid);
258 if (!dev)
259 return NULL;
260
Mike Christief80f8682008-06-16 10:11:35 -0500261 ep = iscsi_dev_to_endpoint(dev);
262 /*
263 * we can drop this now because the interface will prevent
264 * removals and lookups from racing.
265 */
266 put_device(dev);
267 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500268}
269EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
270
Mike Christie30a6c652006-04-06 21:13:39 -0500271static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100272 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500273{
274 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500275 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500276
277 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600278 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500279 mutex_init(&ihost->mutex);
Mike Christie30a6c652006-04-06 21:13:39 -0500280 return 0;
281}
282
283static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
284 "iscsi_host",
285 iscsi_setup_host,
Mike Christie06d25af2009-03-05 14:46:02 -0600286 NULL,
Mike Christie30a6c652006-04-06 21:13:39 -0500287 NULL);
288
Alex Aizman0896b752005-08-04 19:33:07 -0700289static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
290 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 NULL,
292 NULL,
293 NULL);
294
Alex Aizman0896b752005-08-04 19:33:07 -0700295static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
296 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 NULL,
298 NULL,
299 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700300
301static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100302static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700303
Mike Christie7b7232f2006-02-01 21:06:49 -0600304static LIST_HEAD(sesslist);
305static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700306static LIST_HEAD(connlist);
307static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Mike Christieb5c7a122006-04-06 21:13:33 -0500309static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
310{
311 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
312 return sess->sid;
313}
314
315/*
316 * Returns the matching session to a given sid
317 */
318static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600319{
320 unsigned long flags;
321 struct iscsi_cls_session *sess;
322
323 spin_lock_irqsave(&sesslock, flags);
324 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500325 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600326 spin_unlock_irqrestore(&sesslock, flags);
327 return sess;
328 }
329 }
330 spin_unlock_irqrestore(&sesslock, flags);
331 return NULL;
332}
333
Mike Christieb5c7a122006-04-06 21:13:33 -0500334/*
335 * Returns the matching connection to a given sid / cid tuple
336 */
337static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600338{
339 unsigned long flags;
340 struct iscsi_cls_conn *conn;
341
342 spin_lock_irqsave(&connlock, flags);
343 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500344 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600345 spin_unlock_irqrestore(&connlock, flags);
346 return conn;
347 }
348 }
349 spin_unlock_irqrestore(&connlock, flags);
350 return NULL;
351}
352
Mike Christie7b8631b2006-01-13 18:05:50 -0600353/*
354 * The following functions can be used by LLDs that allocate
355 * their own scsi_hosts or by software iscsi LLDs
356 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600357static struct {
358 int value;
359 char *name;
360} iscsi_session_state_names[] = {
361 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
362 { ISCSI_SESSION_FAILED, "FAILED" },
363 { ISCSI_SESSION_FREE, "FREE" },
364};
365
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200366static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600367{
368 int i;
369 char *name = NULL;
370
371 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
372 if (iscsi_session_state_names[i].value == state) {
373 name = iscsi_session_state_names[i].name;
374 break;
375 }
376 }
377 return name;
378}
379
380int iscsi_session_chkready(struct iscsi_cls_session *session)
381{
382 unsigned long flags;
383 int err;
384
385 spin_lock_irqsave(&session->lock, flags);
386 switch (session->state) {
387 case ISCSI_SESSION_LOGGED_IN:
388 err = 0;
389 break;
390 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500391 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600392 break;
393 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500394 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600395 break;
396 default:
397 err = DID_NO_CONNECT << 16;
398 break;
399 }
400 spin_unlock_irqrestore(&session->lock, flags);
401 return err;
402}
403EXPORT_SYMBOL_GPL(iscsi_session_chkready);
404
Mike Christie7b8631b2006-01-13 18:05:50 -0600405static void iscsi_session_release(struct device *dev)
406{
407 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600408 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Mike Christie7b8631b2006-01-13 18:05:50 -0600410 shost = iscsi_session_to_shost(session);
411 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500412 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600413 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600414}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Mike Christie7b8631b2006-01-13 18:05:50 -0600416static int iscsi_is_session_dev(const struct device *dev)
417{
418 return dev->release == iscsi_session_release;
419}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Mike Christiea4804cd2008-05-21 15:54:00 -0500421static int iscsi_iter_session_fn(struct device *dev, void *data)
422{
423 void (* fn) (struct iscsi_cls_session *) = data;
424
425 if (!iscsi_is_session_dev(dev))
426 return 0;
427 fn(iscsi_dev_to_session(dev));
428 return 0;
429}
430
431void iscsi_host_for_each_session(struct Scsi_Host *shost,
432 void (*fn)(struct iscsi_cls_session *))
433{
434 device_for_each_child(&shost->shost_gendev, fn,
435 iscsi_iter_session_fn);
436}
437EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
438
Mike Christie8aae18a2008-01-31 13:36:48 -0600439/**
440 * iscsi_scan_finished - helper to report when running scans are done
441 * @shost: scsi host
442 * @time: scan run time
443 *
444 * This function can be used by drives like qla4xxx to report to the scsi
445 * layer when the scans it kicked off at module load time are done.
446 */
447int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
448{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500449 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600450 /*
451 * qla4xxx will have kicked off some session unblocks before calling
452 * scsi_scan_host, so just wait for them to complete.
453 */
454 return !atomic_read(&ihost->nr_scans);
455}
456EXPORT_SYMBOL_GPL(iscsi_scan_finished);
457
Mike Christie79706342008-05-21 15:54:12 -0500458struct iscsi_scan_data {
459 unsigned int channel;
460 unsigned int id;
461 unsigned int lun;
462};
463
464static int iscsi_user_scan_session(struct device *dev, void *data)
465{
466 struct iscsi_scan_data *scan_data = data;
467 struct iscsi_cls_session *session;
468 struct Scsi_Host *shost;
469 struct iscsi_cls_host *ihost;
470 unsigned long flags;
471 unsigned int id;
472
473 if (!iscsi_is_session_dev(dev))
474 return 0;
475
476 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500477
478 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
479
Mike Christie79706342008-05-21 15:54:12 -0500480 shost = iscsi_session_to_shost(session);
481 ihost = shost->shost_data;
482
483 mutex_lock(&ihost->mutex);
484 spin_lock_irqsave(&session->lock, flags);
485 if (session->state != ISCSI_SESSION_LOGGED_IN) {
486 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500487 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500488 }
489 id = session->target_id;
490 spin_unlock_irqrestore(&session->lock, flags);
491
492 if (id != ISCSI_MAX_TARGET) {
493 if ((scan_data->channel == SCAN_WILD_CARD ||
494 scan_data->channel == 0) &&
495 (scan_data->id == SCAN_WILD_CARD ||
496 scan_data->id == id))
497 scsi_scan_target(&session->dev, 0, id,
498 scan_data->lun, 1);
499 }
Mike Christie632248a2009-08-20 15:11:01 -0500500
501user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500502 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500503 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500504 return 0;
505}
506
Mike Christie30a6c652006-04-06 21:13:39 -0500507static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
508 uint id, uint lun)
509{
Mike Christie79706342008-05-21 15:54:12 -0500510 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500511
Mike Christie79706342008-05-21 15:54:12 -0500512 scan_data.channel = channel;
513 scan_data.id = id;
514 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500515
Mike Christie79706342008-05-21 15:54:12 -0500516 return device_for_each_child(&shost->shost_gendev, &scan_data,
517 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500518}
519
Mike Christiebd976f62008-01-31 13:36:46 -0600520static void iscsi_scan_session(struct work_struct *work)
521{
522 struct iscsi_cls_session *session =
523 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600524 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500525 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500526 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600527
Mike Christie79706342008-05-21 15:54:12 -0500528 scan_data.channel = 0;
529 scan_data.id = SCAN_WILD_CARD;
530 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600531
Mike Christie79706342008-05-21 15:54:12 -0500532 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600533 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600534}
535
David Howellsc4028952006-11-22 14:57:56 +0000536static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500537{
David Howellsc4028952006-11-22 14:57:56 +0000538 struct iscsi_cls_session *session =
539 container_of(work, struct iscsi_cls_session,
540 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600541 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500542
Mike Christie322d7392008-01-31 13:36:52 -0600543 iscsi_cls_session_printk(KERN_INFO, session,
544 "session recovery timed out after %d secs\n",
545 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500546
Mike Christie6eabafb2008-01-31 13:36:43 -0600547 spin_lock_irqsave(&session->lock, flags);
548 switch (session->state) {
549 case ISCSI_SESSION_FAILED:
550 session->state = ISCSI_SESSION_FREE;
551 break;
552 case ISCSI_SESSION_LOGGED_IN:
553 case ISCSI_SESSION_FREE:
554 /* we raced with the unblock's flush */
555 spin_unlock_irqrestore(&session->lock, flags);
556 return;
557 }
558 spin_unlock_irqrestore(&session->lock, flags);
559
Mike Christie30a6c652006-04-06 21:13:39 -0500560 if (session->transport->session_recovery_timedout)
561 session->transport->session_recovery_timedout(session);
562
Mike Christie632248a2009-08-20 15:11:01 -0500563 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500564 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500565 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500566}
567
Mike Christie45ab33b2008-03-04 13:26:55 -0600568static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500569{
Mike Christie45ab33b2008-03-04 13:26:55 -0600570 struct iscsi_cls_session *session =
571 container_of(work, struct iscsi_cls_session,
572 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600573 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500574 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600575 unsigned long flags;
576
Mike Christie632248a2009-08-20 15:11:01 -0500577 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600578 /*
579 * The recovery and unblock work get run from the same workqueue,
580 * so try to cancel it if it was going to run after this unblock.
581 */
582 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600583 spin_lock_irqsave(&session->lock, flags);
584 session->state = ISCSI_SESSION_LOGGED_IN;
585 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600586 /* start IO */
587 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600588 /*
589 * Only do kernel scanning if the driver is properly hooked into
590 * the async scanning code (drivers like iscsi_tcp do login and
591 * scanning from userspace).
592 */
593 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600594 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600595 atomic_inc(&ihost->nr_scans);
596 }
Mike Christie632248a2009-08-20 15:11:01 -0500597 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600598}
Mike Christie45ab33b2008-03-04 13:26:55 -0600599
600/**
601 * iscsi_unblock_session - set a session as logged in and start IO.
602 * @session: iscsi session
603 *
604 * Mark a session as ready to accept IO.
605 */
606void iscsi_unblock_session(struct iscsi_cls_session *session)
607{
608 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
609 /*
610 * make sure all the events have completed before tell the driver
611 * it is safe
612 */
613 flush_workqueue(iscsi_eh_timer_workq);
614}
Mike Christie30a6c652006-04-06 21:13:39 -0500615EXPORT_SYMBOL_GPL(iscsi_unblock_session);
616
Mike Christie45ab33b2008-03-04 13:26:55 -0600617static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500618{
Mike Christie45ab33b2008-03-04 13:26:55 -0600619 struct iscsi_cls_session *session =
620 container_of(work, struct iscsi_cls_session,
621 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600622 unsigned long flags;
623
Mike Christie632248a2009-08-20 15:11:01 -0500624 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600625 spin_lock_irqsave(&session->lock, flags);
626 session->state = ISCSI_SESSION_FAILED;
627 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500628 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500629 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christied8bf5412007-12-13 12:43:27 -0600630 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
631 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500632}
Mike Christie45ab33b2008-03-04 13:26:55 -0600633
634void iscsi_block_session(struct iscsi_cls_session *session)
635{
636 queue_work(iscsi_eh_timer_workq, &session->block_work);
637}
Mike Christie30a6c652006-04-06 21:13:39 -0500638EXPORT_SYMBOL_GPL(iscsi_block_session);
639
Mike Christie26974782007-12-13 12:43:29 -0600640static void __iscsi_unbind_session(struct work_struct *work)
641{
642 struct iscsi_cls_session *session =
643 container_of(work, struct iscsi_cls_session,
644 unbind_work);
645 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500646 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500647 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600648
Mike Christie632248a2009-08-20 15:11:01 -0500649 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
650
Mike Christie26974782007-12-13 12:43:29 -0600651 /* Prevent new scans and make sure scanning is not in progress */
652 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500653 spin_lock_irqsave(&session->lock, flags);
654 if (session->target_id == ISCSI_MAX_TARGET) {
655 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600656 mutex_unlock(&ihost->mutex);
657 return;
658 }
Mike Christie79706342008-05-21 15:54:12 -0500659 session->target_id = ISCSI_MAX_TARGET;
660 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600661 mutex_unlock(&ihost->mutex);
662
663 scsi_remove_target(&session->dev);
664 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500665 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -0600666}
667
Mike Christie7b8631b2006-01-13 18:05:50 -0600668struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500669iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
670 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600671{
672 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Mike Christie5d91e202008-05-21 15:54:01 -0500674 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500675 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600676 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500677 return NULL;
678
Mike Christie7b8631b2006-01-13 18:05:50 -0600679 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500680 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600681 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000682 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500683 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600684 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
685 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600686 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600687 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600688 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600689
Mike Christie6a8a0d32006-06-28 12:00:31 -0500690 /* this is released in the dev's release function */
691 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500692 session->dev.parent = &shost->shost_gendev;
693 session->dev.release = iscsi_session_release;
694 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500695 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500696 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -0500697
698 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500699 return session;
700}
701EXPORT_SYMBOL_GPL(iscsi_alloc_session);
702
Mike Christie79706342008-05-21 15:54:12 -0500703static int iscsi_get_next_target_id(struct device *dev, void *data)
704{
705 struct iscsi_cls_session *session;
706 unsigned long flags;
707 int err = 0;
708
709 if (!iscsi_is_session_dev(dev))
710 return 0;
711
712 session = iscsi_dev_to_session(dev);
713 spin_lock_irqsave(&session->lock, flags);
714 if (*((unsigned int *) data) == session->target_id)
715 err = -EEXIST;
716 spin_unlock_irqrestore(&session->lock, flags);
717 return err;
718}
719
Mike Christie6a8a0d32006-06-28 12:00:31 -0500720int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500721{
722 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500723 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600724 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500725 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500726 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500727
Mike Christie30a6c652006-04-06 21:13:39 -0500728 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600729 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500730
731 if (id == ISCSI_MAX_TARGET) {
732 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
733 err = device_for_each_child(&shost->shost_gendev, &id,
734 iscsi_get_next_target_id);
735 if (!err)
736 break;
737 }
738
739 if (id == ISCSI_MAX_TARGET) {
740 iscsi_cls_session_printk(KERN_ERR, session,
741 "Too many iscsi targets. Max "
742 "number of targets is %d.\n",
743 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +0530744 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -0500745 goto release_host;
746 }
747 }
748 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500749
Kay Sievers71610f52008-12-03 22:41:36 +0100750 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500751 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600752 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600753 iscsi_cls_session_printk(KERN_ERR, session,
754 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500755 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600756 }
757 transport_register_device(&session->dev);
758
Mike Christie26974782007-12-13 12:43:29 -0600759 spin_lock_irqsave(&sesslock, flags);
760 list_add(&session->sess_list, &sesslist);
761 spin_unlock_irqrestore(&sesslock, flags);
762
Mike Christie26974782007-12-13 12:43:29 -0600763 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500764 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500765 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500766
Mike Christie8434aa82006-06-28 12:00:30 -0500767release_host:
768 scsi_host_put(shost);
769 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600770}
Mike Christie8434aa82006-06-28 12:00:30 -0500771EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600772
773/**
Mike Christie8434aa82006-06-28 12:00:30 -0500774 * iscsi_create_session - create iscsi class session
775 * @shost: scsi host
776 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500777 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500778 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600779 *
Mike Christie8434aa82006-06-28 12:00:30 -0500780 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500781 */
Mike Christie8434aa82006-06-28 12:00:30 -0500782struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500783iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
784 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500785{
786 struct iscsi_cls_session *session;
787
Mike Christie5d91e202008-05-21 15:54:01 -0500788 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500789 if (!session)
790 return NULL;
791
Mike Christie6a8a0d32006-06-28 12:00:31 -0500792 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500793 iscsi_free_session(session);
794 return NULL;
795 }
796 return session;
797}
798EXPORT_SYMBOL_GPL(iscsi_create_session);
799
Mike Christie26974782007-12-13 12:43:29 -0600800static void iscsi_conn_release(struct device *dev)
801{
802 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
803 struct device *parent = conn->dev.parent;
804
Mike Christie632248a2009-08-20 15:11:01 -0500805 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -0600806 kfree(conn);
807 put_device(parent);
808}
809
810static int iscsi_is_conn_dev(const struct device *dev)
811{
812 return dev->release == iscsi_conn_release;
813}
814
815static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
816{
817 if (!iscsi_is_conn_dev(dev))
818 return 0;
819 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
820}
821
Mike Christie8434aa82006-06-28 12:00:30 -0500822void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600823{
Mike Christie30a6c652006-04-06 21:13:39 -0500824 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -0600825 unsigned long flags;
826 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500827
Mike Christie632248a2009-08-20 15:11:01 -0500828 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
829
Mike Christie26974782007-12-13 12:43:29 -0600830 spin_lock_irqsave(&sesslock, flags);
831 list_del(&session->sess_list);
832 spin_unlock_irqrestore(&sesslock, flags);
833
Mike Christie45ab33b2008-03-04 13:26:55 -0600834 /* make sure there are no blocks/unblocks queued */
835 flush_workqueue(iscsi_eh_timer_workq);
836 /* make sure the timedout callout is not running */
837 if (!cancel_delayed_work(&session->recovery_work))
838 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600839 /*
840 * If we are blocked let commands flow again. The lld or iscsi
841 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -0600842 * We assume that LLD will not be calling block/unblock while
843 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -0600844 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600845 spin_lock_irqsave(&session->lock, flags);
846 session->state = ISCSI_SESSION_FREE;
847 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -0600848
Mike Christie45ab33b2008-03-04 13:26:55 -0600849 scsi_target_unblock(&session->dev);
850 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -0600851 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -0600852 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -0500853
Mike Christie26974782007-12-13 12:43:29 -0600854 /* hw iscsi may not have removed all connections from session */
855 err = device_for_each_child(&session->dev, NULL,
856 iscsi_iter_destroy_conn_fn);
857 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600858 iscsi_cls_session_printk(KERN_ERR, session,
859 "Could not delete all connections "
860 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500861
Mike Christie7b8631b2006-01-13 18:05:50 -0600862 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500863
864 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500865 device_del(&session->dev);
866}
867EXPORT_SYMBOL_GPL(iscsi_remove_session);
868
869void iscsi_free_session(struct iscsi_cls_session *session)
870{
Mike Christie632248a2009-08-20 15:11:01 -0500871 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -0600872 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500873 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600874}
Mike Christie8434aa82006-06-28 12:00:30 -0500875EXPORT_SYMBOL_GPL(iscsi_free_session);
876
877/**
878 * iscsi_destroy_session - destroy iscsi session
879 * @session: iscsi_session
880 *
881 * Can be called by a LLD or iscsi_transport. There must not be
882 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500883 */
Mike Christie8434aa82006-06-28 12:00:30 -0500884int iscsi_destroy_session(struct iscsi_cls_session *session)
885{
886 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -0500887 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500888 iscsi_free_session(session);
889 return 0;
890}
Mike Christie7b8631b2006-01-13 18:05:50 -0600891EXPORT_SYMBOL_GPL(iscsi_destroy_session);
892
Mike Christie7b8631b2006-01-13 18:05:50 -0600893/**
894 * iscsi_create_conn - create iscsi class connection
895 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -0500896 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -0600897 * @cid: connection id
898 *
899 * This can be called from a LLD or iscsi_transport. The connection
900 * is child of the session so cid must be unique for all connections
901 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500902 *
903 * Since we do not support MCS, cid will normally be zero. In some cases
904 * for software iscsi we could be trying to preallocate a connection struct
905 * in which case there could be two connection structs and cid would be
906 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500907 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600908struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -0500909iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -0600910{
911 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600912 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600913 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600914 int err;
915
Mike Christie5d91e202008-05-21 15:54:01 -0500916 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600917 if (!conn)
918 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -0500919 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600920 conn->dd_data = &conn[1];
921
922 INIT_LIST_HEAD(&conn->conn_list);
923 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500924 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600925
926 /* this is released in the dev's release function */
927 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400928 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500929
Kay Sievers71610f52008-12-03 22:41:36 +0100930 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600931 conn->dev.parent = &session->dev;
932 conn->dev.release = iscsi_conn_release;
933 err = device_register(&conn->dev);
934 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600935 iscsi_cls_session_printk(KERN_ERR, session, "could not "
936 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600937 goto release_parent_ref;
938 }
939 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600940
941 spin_lock_irqsave(&connlock, flags);
942 list_add(&conn->conn_list, &connlist);
943 conn->active = 1;
944 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500945
946 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600947 return conn;
948
949release_parent_ref:
950 put_device(&session->dev);
951free_conn:
952 kfree(conn);
953 return NULL;
954}
955
956EXPORT_SYMBOL_GPL(iscsi_create_conn);
957
958/**
959 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500960 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600961 *
Mike Christie26974782007-12-13 12:43:29 -0600962 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500963 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600964int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
965{
Mike Christie26974782007-12-13 12:43:29 -0600966 unsigned long flags;
967
968 spin_lock_irqsave(&connlock, flags);
969 conn->active = 0;
970 list_del(&conn->conn_list);
971 spin_unlock_irqrestore(&connlock, flags);
972
Mike Christie7b8631b2006-01-13 18:05:50 -0600973 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500974 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600975 device_unregister(&conn->dev);
976 return 0;
977}
Mike Christie7b8631b2006-01-13 18:05:50 -0600978EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
979
980/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600981 * iscsi interface functions
982 */
Alex Aizman0896b752005-08-04 19:33:07 -0700983static struct iscsi_internal *
984iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Alex Aizman0896b752005-08-04 19:33:07 -0700986 struct iscsi_internal *priv;
987 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Alex Aizman0896b752005-08-04 19:33:07 -0700989 spin_lock_irqsave(&iscsi_transport_lock, flags);
990 list_for_each_entry(priv, &iscsi_transports, list) {
991 if (tt == priv->iscsi_transport) {
992 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
993 return priv;
994 }
995 }
996 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
997 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Alex Aizman0896b752005-08-04 19:33:07 -07001000static int
Michael Chan43514772009-06-08 18:14:41 -07001001iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001002{
Michael Chan43514772009-06-08 18:14:41 -07001003 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001004}
1005
Mike Christie7b7232f2006-02-01 21:06:49 -06001006int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001007 char *data, uint32_t data_size)
1008{
1009 struct nlmsghdr *nlh;
1010 struct sk_buff *skb;
1011 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001012 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001013 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001014 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1015 data_size);
1016
Mike Christie790f39a2006-05-18 20:31:39 -05001017 priv = iscsi_if_transport_lookup(conn->transport);
1018 if (!priv)
1019 return -EINVAL;
1020
Mike Christie43a145a2006-10-16 18:09:38 -04001021 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001022 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05001023 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001024 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1025 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001026 return -ENOMEM;
1027 }
1028
Michael Chan43514772009-06-08 18:14:41 -07001029 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001030 ev = NLMSG_DATA(nlh);
1031 memset(ev, 0, sizeof(*ev));
1032 ev->transport_handle = iscsi_handle(conn->transport);
1033 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001034 ev->r.recv_req.cid = conn->cid;
1035 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001036 pdu = (char*)ev + sizeof(*ev);
1037 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1038 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1039
Michael Chan43514772009-06-08 18:14:41 -07001040 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001041}
1042EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1043
Michael Chan43514772009-06-08 18:14:41 -07001044int iscsi_offload_mesg(struct Scsi_Host *shost,
1045 struct iscsi_transport *transport, uint32_t type,
1046 char *data, uint16_t data_size)
1047{
1048 struct nlmsghdr *nlh;
1049 struct sk_buff *skb;
1050 struct iscsi_uevent *ev;
1051 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1052
Michael Chana541f842009-07-29 08:49:52 +00001053 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001054 if (!skb) {
1055 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1056 return -ENOMEM;
1057 }
1058
1059 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1060 ev = NLMSG_DATA(nlh);
1061 memset(ev, 0, sizeof(*ev));
1062 ev->type = type;
1063 ev->transport_handle = iscsi_handle(transport);
1064 switch (type) {
1065 case ISCSI_KEVENT_PATH_REQ:
1066 ev->r.req_path.host_no = shost->host_no;
1067 break;
1068 case ISCSI_KEVENT_IF_DOWN:
1069 ev->r.notify_if_down.host_no = shost->host_no;
1070 break;
1071 }
1072
1073 memcpy((char *)ev + sizeof(*ev), data, data_size);
1074
Michael Chana541f842009-07-29 08:49:52 +00001075 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001076}
1077EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1078
Mike Christiee5bd7b52008-09-24 11:46:10 -05001079void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001080{
1081 struct nlmsghdr *nlh;
1082 struct sk_buff *skb;
1083 struct iscsi_uevent *ev;
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
Mike Christie790f39a2006-05-18 20:31:39 -05001087 priv = iscsi_if_transport_lookup(conn->transport);
1088 if (!priv)
1089 return;
1090
Mike Christie43a145a2006-10-16 18:09:38 -04001091 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001092 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001093 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1094 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001095 return;
1096 }
1097
Michael Chan43514772009-06-08 18:14:41 -07001098 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001099 ev = NLMSG_DATA(nlh);
1100 ev->transport_handle = iscsi_handle(conn->transport);
1101 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001102 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001103 ev->r.connerror.cid = conn->cid;
1104 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001105
Michael Chan43514772009-06-08 18:14:41 -07001106 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001107
Mike Christie322d7392008-01-31 13:36:52 -06001108 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1109 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001110}
Mike Christiee5bd7b52008-09-24 11:46:10 -05001111EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001112
1113static int
Michael Chan43514772009-06-08 18:14:41 -07001114iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1115 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001116{
1117 struct sk_buff *skb;
1118 struct nlmsghdr *nlh;
1119 int len = NLMSG_SPACE(size);
1120 int flags = multi ? NLM_F_MULTI : 0;
1121 int t = done ? NLMSG_DONE : type;
1122
Mike Christie43a145a2006-10-16 18:09:38 -04001123 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001124 if (!skb) {
1125 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1126 return -ENOMEM;
1127 }
Alex Aizman0896b752005-08-04 19:33:07 -07001128
Michael Chan43514772009-06-08 18:14:41 -07001129 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001130 nlh->nlmsg_flags = flags;
1131 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001132 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001133}
1134
1135static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001136iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001137{
1138 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1139 struct iscsi_stats *stats;
1140 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001141 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001142 struct nlmsghdr *nlhstat;
1143 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001144 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001145 int len = NLMSG_SPACE(sizeof(*ev) +
1146 sizeof(struct iscsi_stats) +
1147 sizeof(struct iscsi_stats_custom) *
1148 ISCSI_STATS_CUSTOM_MAX);
1149 int err = 0;
1150
Mike Christie790f39a2006-05-18 20:31:39 -05001151 priv = iscsi_if_transport_lookup(transport);
1152 if (!priv)
1153 return -EINVAL;
1154
Mike Christieb5c7a122006-04-06 21:13:33 -05001155 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001156 if (!conn)
1157 return -EEXIST;
1158
1159 do {
1160 int actual_size;
1161
Mike Christie43a145a2006-10-16 18:09:38 -04001162 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001163 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001164 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1165 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001166 return -ENOMEM;
1167 }
1168
Michael Chan43514772009-06-08 18:14:41 -07001169 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001170 (len - sizeof(*nlhstat)), 0);
1171 evstat = NLMSG_DATA(nlhstat);
1172 memset(evstat, 0, sizeof(*evstat));
1173 evstat->transport_handle = iscsi_handle(conn->transport);
1174 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001175 evstat->u.get_stats.cid =
1176 ev->u.get_stats.cid;
1177 evstat->u.get_stats.sid =
1178 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001179 stats = (struct iscsi_stats *)
1180 ((char*)evstat + sizeof(*evstat));
1181 memset(stats, 0, sizeof(*stats));
1182
Mike Christie7b7232f2006-02-01 21:06:49 -06001183 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001184 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1185 sizeof(struct iscsi_stats) +
1186 sizeof(struct iscsi_stats_custom) *
1187 stats->custom_length);
1188 actual_size -= sizeof(*nlhstat);
1189 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001190 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001191 nlhstat->nlmsg_len = actual_size;
1192
Michael Chan43514772009-06-08 18:14:41 -07001193 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1194 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001195 } while (err < 0 && err != -ECONNREFUSED);
1196
1197 return err;
1198}
1199
Mike Christie53cb8a12006-06-28 12:00:32 -05001200/**
Mike Christie26974782007-12-13 12:43:29 -06001201 * iscsi_session_event - send session destr. completion event
1202 * @session: iscsi class session
1203 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001204 */
Mike Christie26974782007-12-13 12:43:29 -06001205int iscsi_session_event(struct iscsi_cls_session *session,
1206 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001207{
1208 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001209 struct Scsi_Host *shost;
1210 struct iscsi_uevent *ev;
1211 struct sk_buff *skb;
1212 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001213 int rc, len = NLMSG_SPACE(sizeof(*ev));
1214
Mike Christie26974782007-12-13 12:43:29 -06001215 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001216 if (!priv)
1217 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001218 shost = iscsi_session_to_shost(session);
1219
Mike Christie43a145a2006-10-16 18:09:38 -04001220 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001221 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001222 iscsi_cls_session_printk(KERN_ERR, session,
1223 "Cannot notify userspace of session "
1224 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001225 return -ENOMEM;
1226 }
1227
Michael Chan43514772009-06-08 18:14:41 -07001228 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001229 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001230 ev->transport_handle = iscsi_handle(session->transport);
1231
1232 ev->type = event;
1233 switch (event) {
1234 case ISCSI_KEVENT_DESTROY_SESSION:
1235 ev->r.d_session.host_no = shost->host_no;
1236 ev->r.d_session.sid = session->sid;
1237 break;
1238 case ISCSI_KEVENT_CREATE_SESSION:
1239 ev->r.c_session_ret.host_no = shost->host_no;
1240 ev->r.c_session_ret.sid = session->sid;
1241 break;
1242 case ISCSI_KEVENT_UNBIND_SESSION:
1243 ev->r.unbind_session.host_no = shost->host_no;
1244 ev->r.unbind_session.sid = session->sid;
1245 break;
1246 default:
Mike Christie322d7392008-01-31 13:36:52 -06001247 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1248 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001249 kfree_skb(skb);
1250 return -EINVAL;
1251 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001252
1253 /*
1254 * this will occur if the daemon is not up, so we just warn
1255 * the user and when the daemon is restarted it will handle it
1256 */
Michael Chan43514772009-06-08 18:14:41 -07001257 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001258 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001259 iscsi_cls_session_printk(KERN_ERR, session,
1260 "Cannot notify userspace of session "
1261 "event %u. Check iscsi daemon\n",
1262 event);
Mike Christie632248a2009-08-20 15:11:01 -05001263
1264 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1265 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001266 return rc;
1267}
Mike Christie26974782007-12-13 12:43:29 -06001268EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001269
Alex Aizman0896b752005-08-04 19:33:07 -07001270static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001271iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1272 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001273 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001274{
1275 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001276 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001277 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001278
Mike Christied82ff9be2008-05-21 15:54:13 -05001279 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001280 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001281 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001282 return -ENOMEM;
1283
Mike Christie5e7facb2009-03-05 14:46:06 -06001284 shost = iscsi_session_to_shost(session);
1285 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001286 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001287 ISCSI_DBG_TRANS_SESSION(session,
1288 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001289 return 0;
1290}
1291
1292static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001293iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001294{
Mike Christie7b8631b2006-01-13 18:05:50 -06001295 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001296 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001297
Mike Christieb5c7a122006-04-06 21:13:33 -05001298 session = iscsi_session_lookup(ev->u.c_conn.sid);
1299 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001300 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001301 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001302 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001303 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001304
Mike Christie7b7232f2006-02-01 21:06:49 -06001305 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001306 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001307 iscsi_cls_session_printk(KERN_ERR, session,
1308 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001309 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001310 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001311
Mike Christieb5c7a122006-04-06 21:13:33 -05001312 ev->r.c_conn_ret.sid = session->sid;
1313 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001314
1315 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001316 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001317}
1318
1319static int
1320iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1321{
Mike Christie7b8631b2006-01-13 18:05:50 -06001322 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001323
Mike Christieb5c7a122006-04-06 21:13:33 -05001324 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001325 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001326 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001327
Mike Christie632248a2009-08-20 15:11:01 -05001328 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001329 if (transport->destroy_conn)
1330 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001331
Mike Christie7b8631b2006-01-13 18:05:50 -06001332 return 0;
1333}
1334
Mike Christiefd7255f2006-04-06 21:13:36 -05001335static int
1336iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1337{
1338 char *data = (char*)ev + sizeof(*ev);
1339 struct iscsi_cls_conn *conn;
1340 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001341 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001342
1343 session = iscsi_session_lookup(ev->u.set_param.sid);
1344 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1345 if (!conn || !session)
1346 return -EINVAL;
1347
1348 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001349 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001350 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001351 if (value != 0)
1352 session->recovery_tmo = value;
1353 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001354 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001355 err = transport->set_param(conn, ev->u.set_param.param,
1356 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001357 }
1358
1359 return err;
1360}
1361
Mike Christie10eb0f02009-05-13 17:57:38 -05001362static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1363 struct iscsi_uevent *ev, int msg_type)
1364{
1365 struct iscsi_endpoint *ep;
1366 struct sockaddr *dst_addr;
1367 struct Scsi_Host *shost = NULL;
1368 int non_blocking, err = 0;
1369
1370 if (!transport->ep_connect)
1371 return -EINVAL;
1372
1373 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1374 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1375 if (!shost) {
1376 printk(KERN_ERR "ep connect failed. Could not find "
1377 "host no %u\n",
1378 ev->u.ep_connect_through_host.host_no);
1379 return -ENODEV;
1380 }
1381 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1382 } else
1383 non_blocking = ev->u.ep_connect.non_blocking;
1384
1385 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1386 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1387 if (IS_ERR(ep)) {
1388 err = PTR_ERR(ep);
1389 goto release_host;
1390 }
1391
1392 ev->r.ep_connect_ret.handle = ep->id;
1393release_host:
1394 if (shost)
1395 scsi_host_put(shost);
1396 return err;
1397}
1398
Mike Christie7b8631b2006-01-13 18:05:50 -06001399static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001400iscsi_if_transport_ep(struct iscsi_transport *transport,
1401 struct iscsi_uevent *ev, int msg_type)
1402{
Mike Christied82ff9be2008-05-21 15:54:13 -05001403 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001404 int rc = 0;
1405
1406 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001407 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001408 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001409 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001410 break;
1411 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1412 if (!transport->ep_poll)
1413 return -EINVAL;
1414
Mike Christied82ff9be2008-05-21 15:54:13 -05001415 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1416 if (!ep)
1417 return -EINVAL;
1418
1419 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001420 ev->u.ep_poll.timeout_ms);
1421 break;
1422 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1423 if (!transport->ep_disconnect)
1424 return -EINVAL;
1425
Mike Christied82ff9be2008-05-21 15:54:13 -05001426 ep = iscsi_lookup_endpoint(ev->u.ep_disconnect.ep_handle);
1427 if (!ep)
1428 return -EINVAL;
1429
1430 transport->ep_disconnect(ep);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001431 break;
1432 }
1433 return rc;
1434}
1435
1436static int
Mike Christie01cb2252006-06-28 12:00:22 -05001437iscsi_tgt_dscvr(struct iscsi_transport *transport,
1438 struct iscsi_uevent *ev)
1439{
Mike Christie2174a042007-05-30 12:57:10 -05001440 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001441 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001442 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001443
1444 if (!transport->tgt_dscvr)
1445 return -EINVAL;
1446
Mike Christie2174a042007-05-30 12:57:10 -05001447 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001448 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001449 printk(KERN_ERR "target discovery could not find host no %u\n",
1450 ev->u.tgt_dscvr.host_no);
1451 return -ENODEV;
1452 }
1453
1454
Mike Christie01cb2252006-06-28 12:00:22 -05001455 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001456 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1457 ev->u.tgt_dscvr.enable, dst_addr);
1458 scsi_host_put(shost);
1459 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001460}
1461
1462static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001463iscsi_set_host_param(struct iscsi_transport *transport,
1464 struct iscsi_uevent *ev)
1465{
1466 char *data = (char*)ev + sizeof(*ev);
1467 struct Scsi_Host *shost;
1468 int err;
1469
1470 if (!transport->set_host_param)
1471 return -ENOSYS;
1472
1473 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001474 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001475 printk(KERN_ERR "set_host_param could not find host no %u\n",
1476 ev->u.set_host_param.host_no);
1477 return -ENODEV;
1478 }
1479
1480 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1481 data, ev->u.set_host_param.len);
1482 scsi_host_put(shost);
1483 return err;
1484}
1485
1486static int
Michael Chan43514772009-06-08 18:14:41 -07001487iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1488{
1489 struct Scsi_Host *shost;
1490 struct iscsi_path *params;
1491 int err;
1492
1493 if (!transport->set_path)
1494 return -ENOSYS;
1495
1496 shost = scsi_host_lookup(ev->u.set_path.host_no);
1497 if (!shost) {
1498 printk(KERN_ERR "set path could not find host no %u\n",
1499 ev->u.set_path.host_no);
1500 return -ENODEV;
1501 }
1502
1503 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1504 err = transport->set_path(shost, params);
1505
1506 scsi_host_put(shost);
1507 return err;
1508}
1509
1510static int
1511iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001512{
1513 int err = 0;
1514 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1515 struct iscsi_transport *transport = NULL;
1516 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001517 struct iscsi_cls_session *session;
1518 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001519 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001520
Michael Chan43514772009-06-08 18:14:41 -07001521 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1522 *group = ISCSI_NL_GRP_UIP;
1523 else
1524 *group = ISCSI_NL_GRP_ISCSID;
1525
Alex Aizman0896b752005-08-04 19:33:07 -07001526 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1527 if (!priv)
1528 return -EINVAL;
1529 transport = priv->iscsi_transport;
1530
Mike Christie7b7232f2006-02-01 21:06:49 -06001531 if (!try_module_get(transport->owner))
1532 return -EINVAL;
1533
Alex Aizman0896b752005-08-04 19:33:07 -07001534 switch (nlh->nlmsg_type) {
1535 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001536 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001537 ev->u.c_session.initial_cmdsn,
1538 ev->u.c_session.cmds_max,
1539 ev->u.c_session.queue_depth);
1540 break;
1541 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001542 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001543 if (!ep) {
1544 err = -EINVAL;
1545 break;
1546 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001547
1548 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001549 ev->u.c_bound_session.initial_cmdsn,
1550 ev->u.c_bound_session.cmds_max,
1551 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001552 break;
1553 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001554 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001555 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001556 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001557 else
1558 err = -EINVAL;
1559 break;
1560 case ISCSI_UEVENT_UNBIND_SESSION:
1561 session = iscsi_session_lookup(ev->u.d_session.sid);
1562 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001563 scsi_queue_work(iscsi_session_to_shost(session),
1564 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001565 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001566 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001567 break;
1568 case ISCSI_UEVENT_CREATE_CONN:
1569 err = iscsi_if_create_conn(transport, ev);
1570 break;
1571 case ISCSI_UEVENT_DESTROY_CONN:
1572 err = iscsi_if_destroy_conn(transport, ev);
1573 break;
1574 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001575 session = iscsi_session_lookup(ev->u.b_conn.sid);
1576 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001577
1578 if (session && conn)
1579 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001580 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001581 ev->u.b_conn.is_leading);
1582 else
1583 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001584 break;
1585 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001586 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001587 break;
1588 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001589 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001590 if (conn)
1591 ev->r.retcode = transport->start_conn(conn);
1592 else
1593 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001594 break;
1595 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001596 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001597 if (conn)
1598 transport->stop_conn(conn, ev->u.stop_conn.flag);
1599 else
1600 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001601 break;
1602 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001603 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001604 if (conn)
1605 ev->r.retcode = transport->send_pdu(conn,
1606 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1607 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1608 ev->u.send_pdu.data_size);
1609 else
1610 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001611 break;
1612 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001613 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001614 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001615 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1616 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1617 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001618 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001619 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1620 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001621 case ISCSI_UEVENT_TGT_DSCVR:
1622 err = iscsi_tgt_dscvr(transport, ev);
1623 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001624 case ISCSI_UEVENT_SET_HOST_PARAM:
1625 err = iscsi_set_host_param(transport, ev);
1626 break;
Michael Chan43514772009-06-08 18:14:41 -07001627 case ISCSI_UEVENT_PATH_UPDATE:
1628 err = iscsi_set_path(transport, ev);
1629 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001630 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001631 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001632 break;
1633 }
1634
Mike Christie7b7232f2006-02-01 21:06:49 -06001635 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001636 return err;
1637}
1638
Mike Christieb5c7a122006-04-06 21:13:33 -05001639/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001640 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1641 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001642 */
Alex Aizman0896b752005-08-04 19:33:07 -07001643static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001644iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001645{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001646 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001647 while (skb->len >= NLMSG_SPACE(0)) {
1648 int err;
1649 uint32_t rlen;
1650 struct nlmsghdr *nlh;
1651 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07001652 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001653
1654 nlh = nlmsg_hdr(skb);
1655 if (nlh->nlmsg_len < sizeof(*nlh) ||
1656 skb->len < nlh->nlmsg_len) {
1657 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001658 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001659
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001660 ev = NLMSG_DATA(nlh);
1661 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1662 if (rlen > skb->len)
1663 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001664
Michael Chan43514772009-06-08 18:14:41 -07001665 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001666 if (err) {
1667 ev->type = ISCSI_KEVENT_IF_ERROR;
1668 ev->iferror = err;
1669 }
1670 do {
1671 /*
1672 * special case for GET_STATS:
1673 * on success - sending reply and stats from
1674 * inside of if_recv_msg(),
1675 * on error - fall through.
1676 */
1677 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001678 break;
Michael Chan43514772009-06-08 18:14:41 -07001679 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001680 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1681 } while (err < 0 && err != -ECONNREFUSED);
1682 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001683 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001684 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001685}
1686
Mike Christiefd7255f2006-04-06 21:13:36 -05001687#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001688struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001689 __ATTR(_name,_mode,_show,_store)
1690
Alex Aizman0896b752005-08-04 19:33:07 -07001691/*
1692 * iSCSI connection attrs
1693 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001694#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001696show_conn_param_##param(struct device *dev, \
1697 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001698{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001699 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001700 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001701 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001702}
1703
Mike Christiea54a52c2006-06-28 12:00:23 -05001704#define iscsi_conn_attr(field, param) \
1705 iscsi_conn_attr_show(param) \
1706static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001707 NULL);
1708
Mike Christiea54a52c2006-06-28 12:00:23 -05001709iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1710iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1711iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1712iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1713iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1714iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1715iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1716iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1717iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1718iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1719iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001720iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1721iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723/*
Alex Aizman0896b752005-08-04 19:33:07 -07001724 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
Mike Christieb2c64162007-05-30 12:57:16 -05001726#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001728show_session_param_##param(struct device *dev, \
1729 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001730{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001731 struct iscsi_cls_session *session = \
1732 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001733 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001734 \
1735 if (perm && !capable(CAP_SYS_ADMIN)) \
1736 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001737 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Mike Christieb2c64162007-05-30 12:57:16 -05001740#define iscsi_session_attr(field, param, perm) \
1741 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001742static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001743 NULL);
1744
Mike Christieb2c64162007-05-30 12:57:16 -05001745iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1746iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1747iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1748iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1749iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1750iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1751iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1752iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1753iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1754iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1755iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1756iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1757iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1758iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001759iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1760iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1761iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05001762iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
1763iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0)
Mike Christiefd7255f2006-04-06 21:13:36 -05001764
Mike Christie6eabafb2008-01-31 13:36:43 -06001765static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001766show_priv_session_state(struct device *dev, struct device_attribute *attr,
1767 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06001768{
Tony Jonesee959b02008-02-22 00:13:36 +01001769 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06001770 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1771}
1772static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1773 NULL);
1774
Mike Christiefd7255f2006-04-06 21:13:36 -05001775#define iscsi_priv_session_attr_show(field, format) \
1776static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001777show_priv_session_##field(struct device *dev, \
1778 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001779{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001780 struct iscsi_cls_session *session = \
1781 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001782 return sprintf(buf, format"\n", session->field); \
1783}
1784
1785#define iscsi_priv_session_attr(field, format) \
1786 iscsi_priv_session_attr_show(field, format) \
1787static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1788 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001789iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001790
Mike Christie1819dc82007-05-30 12:57:08 -05001791/*
1792 * iSCSI host attrs
1793 */
1794#define iscsi_host_attr_show(param) \
1795static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001796show_host_param_##param(struct device *dev, \
1797 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05001798{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001799 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05001800 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1801 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1802}
1803
1804#define iscsi_host_attr(field, param) \
1805 iscsi_host_attr_show(param) \
1806static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1807 NULL);
1808
Mike Christied8196ed2007-05-30 12:57:25 -05001809iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001810iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001811iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001812iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001813
Mike Christiefd7255f2006-04-06 21:13:36 -05001814#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1815do { \
Tony Jonesee959b02008-02-22 00:13:36 +01001816 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001817 count++; \
1818} while (0)
1819
Mike Christiea54a52c2006-06-28 12:00:23 -05001820
Mike Christiefd7255f2006-04-06 21:13:36 -05001821#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1822do { \
1823 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001824 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001826 } \
1827} while (0)
1828
Mike Christiefd7255f2006-04-06 21:13:36 -05001829#define SETUP_CONN_RD_ATTR(field, param_flag) \
1830do { \
1831 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001832 priv->conn_attrs[count] = &dev_attr_conn_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001833 count++; \
1834 } \
1835} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Mike Christie1819dc82007-05-30 12:57:08 -05001837#define SETUP_HOST_RD_ATTR(field, param_flag) \
1838do { \
1839 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001840 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05001841 count++; \
1842 } \
1843} while (0)
1844
Alex Aizman0896b752005-08-04 19:33:07 -07001845static int iscsi_session_match(struct attribute_container *cont,
1846 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Mike Christie7b8631b2006-01-13 18:05:50 -06001848 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001850 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Alex Aizman0896b752005-08-04 19:33:07 -07001852 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
1854
Mike Christie7b8631b2006-01-13 18:05:50 -06001855 session = iscsi_dev_to_session(dev);
1856 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001857 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return 0;
1859
Alex Aizman0896b752005-08-04 19:33:07 -07001860 priv = to_iscsi_internal(shost->transportt);
1861 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1862 return 0;
1863
1864 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Alex Aizman0896b752005-08-04 19:33:07 -07001867static int iscsi_conn_match(struct attribute_container *cont,
1868 struct device *dev)
1869{
Mike Christie7b8631b2006-01-13 18:05:50 -06001870 struct iscsi_cls_session *session;
1871 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001872 struct Scsi_Host *shost;
1873 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Alex Aizman0896b752005-08-04 19:33:07 -07001875 if (!iscsi_is_conn_dev(dev))
1876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Mike Christie7b8631b2006-01-13 18:05:50 -06001878 conn = iscsi_dev_to_conn(dev);
1879 session = iscsi_dev_to_session(conn->dev.parent);
1880 shost = iscsi_session_to_shost(session);
1881
Alex Aizman0896b752005-08-04 19:33:07 -07001882 if (!shost->transportt)
1883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Alex Aizman0896b752005-08-04 19:33:07 -07001885 priv = to_iscsi_internal(shost->transportt);
1886 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1887 return 0;
1888
1889 return &priv->conn_cont.ac == cont;
1890}
1891
Mike Christie30a6c652006-04-06 21:13:39 -05001892static int iscsi_host_match(struct attribute_container *cont,
1893 struct device *dev)
1894{
1895 struct Scsi_Host *shost;
1896 struct iscsi_internal *priv;
1897
1898 if (!scsi_is_host_device(dev))
1899 return 0;
1900
1901 shost = dev_to_shost(dev);
1902 if (!shost->transportt ||
1903 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1904 return 0;
1905
1906 priv = to_iscsi_internal(shost->transportt);
1907 return &priv->t.host_attrs.ac == cont;
1908}
1909
Mike Christie7b8631b2006-01-13 18:05:50 -06001910struct scsi_transport_template *
1911iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001912{
1913 struct iscsi_internal *priv;
1914 unsigned long flags;
1915 int count = 0, err;
1916
1917 BUG_ON(!tt);
1918
1919 priv = iscsi_if_transport_lookup(tt);
1920 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001921 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001922
Jes Sorensen24669f752006-01-16 10:31:18 -05001923 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001924 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001925 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001926 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07001927 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001928 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06001929 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07001930
Tony Jonesee959b02008-02-22 00:13:36 +01001931 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01001932 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01001933 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001934 if (err)
1935 goto free_priv;
1936
Tony Jonesee959b02008-02-22 00:13:36 +01001937 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07001938 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01001939 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07001940
Mike Christie30a6c652006-04-06 21:13:39 -05001941 /* host parameters */
1942 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1943 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1944 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05001945 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001946 transport_container_register(&priv->t.host_attrs);
1947
Mike Christied8196ed2007-05-30 12:57:25 -05001948 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001949 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001950 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001951 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001952 BUG_ON(count > ISCSI_HOST_ATTRS);
1953 priv->host_attrs[count] = NULL;
1954 count = 0;
1955
Alex Aizman0896b752005-08-04 19:33:07 -07001956 /* connection parameters */
1957 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1958 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1959 priv->conn_cont.ac.match = iscsi_conn_match;
1960 transport_container_register(&priv->conn_cont);
1961
Mike Christiefd7255f2006-04-06 21:13:36 -05001962 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1963 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1964 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1965 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1966 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1967 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1968 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1969 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001970 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001971 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1972 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001973 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1974 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001975
1976 BUG_ON(count > ISCSI_CONN_ATTRS);
1977 priv->conn_attrs[count] = NULL;
1978 count = 0;
1979
1980 /* session parameters */
1981 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1982 priv->session_cont.ac.class = &iscsi_session_class.class;
1983 priv->session_cont.ac.match = iscsi_session_match;
1984 transport_container_register(&priv->session_cont);
1985
Mike Christiefd7255f2006-04-06 21:13:36 -05001986 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1987 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1988 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1989 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1990 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1991 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1992 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1993 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001994 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1995 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001996 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1997 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1998 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1999 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06002000 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002001 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
2002 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie88dfd342008-05-21 15:54:16 -05002003 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
2004 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
Mike Christie30a6c652006-04-06 21:13:39 -05002005 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06002006 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07002009 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Alex Aizman0896b752005-08-04 19:33:07 -07002011 spin_lock_irqsave(&iscsi_transport_lock, flags);
2012 list_add(&priv->list, &iscsi_transports);
2013 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Alex Aizman0896b752005-08-04 19:33:07 -07002015 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002016 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Tony Jonesee959b02008-02-22 00:13:36 +01002018unregister_dev:
2019 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002020 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002021free_priv:
2022 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002023 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
Alex Aizman0896b752005-08-04 19:33:07 -07002025EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Alex Aizman0896b752005-08-04 19:33:07 -07002027int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Alex Aizman0896b752005-08-04 19:33:07 -07002029 struct iscsi_internal *priv;
2030 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Alex Aizman0896b752005-08-04 19:33:07 -07002032 BUG_ON(!tt);
2033
Arjan van de Ven0b950672006-01-11 13:16:10 +01002034 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002035
2036 priv = iscsi_if_transport_lookup(tt);
2037 BUG_ON (!priv);
2038
Alex Aizman0896b752005-08-04 19:33:07 -07002039 spin_lock_irqsave(&iscsi_transport_lock, flags);
2040 list_del(&priv->list);
2041 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2042
2043 transport_container_unregister(&priv->conn_cont);
2044 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002045 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002046
Tony Jonesee959b02008-02-22 00:13:36 +01002047 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2048 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002049 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002050
2051 return 0;
2052}
2053EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055static __init int iscsi_transport_init(void)
2056{
Alex Aizman0896b752005-08-04 19:33:07 -07002057 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Meelis Roos09492602006-12-17 12:10:26 -06002059 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002060 ISCSI_TRANSPORT_VERSION);
2061
Mike Christie41be1442007-02-28 17:32:18 -06002062 atomic_set(&iscsi_session_nr, 0);
2063
Alex Aizman0896b752005-08-04 19:33:07 -07002064 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (err)
2066 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002067
Mike Christied82ff9be2008-05-21 15:54:13 -05002068 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002069 if (err)
2070 goto unregister_transport_class;
2071
Mike Christied82ff9be2008-05-21 15:54:13 -05002072 err = transport_class_register(&iscsi_host_class);
2073 if (err)
2074 goto unregister_endpoint_class;
2075
Mike Christie30a6c652006-04-06 21:13:39 -05002076 err = transport_class_register(&iscsi_connection_class);
2077 if (err)
2078 goto unregister_host_class;
2079
Alex Aizman0896b752005-08-04 19:33:07 -07002080 err = transport_class_register(&iscsi_session_class);
2081 if (err)
2082 goto unregister_conn_class;
2083
Mike Christied82ff9be2008-05-21 15:54:13 -05002084 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2085 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002086 if (!nls) {
2087 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002088 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002089 }
2090
Mike Christied8bf5412007-12-13 12:43:27 -06002091 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2092 if (!iscsi_eh_timer_workq)
2093 goto release_nls;
2094
Mike Christie43a145a2006-10-16 18:09:38 -04002095 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002096
Mike Christied8bf5412007-12-13 12:43:27 -06002097release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002098 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002099unregister_session_class:
2100 transport_class_unregister(&iscsi_session_class);
2101unregister_conn_class:
2102 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002103unregister_host_class:
2104 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002105unregister_endpoint_class:
2106 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002107unregister_transport_class:
2108 class_unregister(&iscsi_transport_class);
2109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112static void __exit iscsi_transport_exit(void)
2113{
Mike Christied8bf5412007-12-13 12:43:27 -06002114 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002115 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002116 transport_class_unregister(&iscsi_connection_class);
2117 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002118 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002119 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002120 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
2123module_init(iscsi_transport_init);
2124module_exit(iscsi_transport_exit);
2125
Alex Aizman0896b752005-08-04 19:33:07 -07002126MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2127 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2128 "Alex Aizman <itn780@yahoo.com>");
2129MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002131MODULE_VERSION(ISCSI_TRANSPORT_VERSION);