blob: 212a8d846748601b332f2e047f9f08850180ba51 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Alex Aizman0896b752005-08-04 19:33:07 -070026#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <scsi/scsi.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070032#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053033#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +053035#define ISCSI_SESSION_ATTRS 23
Mike Christiec238c3b2008-01-31 13:36:51 -060036#define ISCSI_CONN_ATTRS 13
Mike Christied8196ed2007-05-30 12:57:25 -050037#define ISCSI_HOST_ATTRS 4
Mike Christied82ff9be2008-05-21 15:54:13 -050038
Mike Christie4c2133c2008-06-16 10:11:34 -050039#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mike Christie632248a2009-08-20 15:11:01 -050041static int dbg_session;
42module_param_named(debug_session, dbg_session, int,
43 S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(debug_session,
45 "Turn on debugging for sessions in scsi_transport_iscsi "
46 "module. Set to 1 to turn on, and zero to turn off. Default "
47 "is off.");
48
49static int dbg_conn;
50module_param_named(debug_conn, dbg_conn, int,
51 S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(debug_conn,
53 "Turn on debugging for connections in scsi_transport_iscsi "
54 "module. Set to 1 to turn on, and zero to turn off. Default "
55 "is off.");
56
57#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
58 do { \
59 if (dbg_session) \
60 iscsi_cls_session_printk(KERN_INFO, _session, \
61 "%s: " dbg_fmt, \
62 __func__, ##arg); \
63 } while (0);
64
65#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
66 do { \
67 if (dbg_conn) \
68 iscsi_cls_conn_printk(KERN_INFO, _conn, \
69 "%s: " dbg_fmt, \
70 __func__, ##arg); \
71 } while (0);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct iscsi_internal {
74 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070075 struct iscsi_transport *iscsi_transport;
76 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010077 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050078
Tony Jonesee959b02008-02-22 00:13:36 +010079 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070080 struct transport_container conn_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010081 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070082 struct transport_container session_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010083 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Mike Christie41be1442007-02-28 17:32:18 -060086static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060087static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050088
Alex Aizman0896b752005-08-04 19:33:07 -070089/*
90 * list of registered transports and lock that must
91 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010092 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070093 */
94static LIST_HEAD(iscsi_transports);
95static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Alex Aizman0896b752005-08-04 19:33:07 -070097#define to_iscsi_internal(tmpl) \
98 container_of(tmpl, struct iscsi_internal, t)
99
Tony Jonesee959b02008-02-22 00:13:36 +0100100#define dev_to_iscsi_internal(_dev) \
101 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700102
Tony Jonesee959b02008-02-22 00:13:36 +0100103static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700104{
Tony Jonesee959b02008-02-22 00:13:36 +0100105 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700106 kfree(priv);
107}
108
109/*
110 * iscsi_transport_class represents the iscsi_transports that are
111 * registered.
112 */
113static struct class iscsi_transport_class = {
114 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100115 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700116};
117
118static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100119show_transport_handle(struct device *dev, struct device_attribute *attr,
120 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700121{
Tony Jonesee959b02008-02-22 00:13:36 +0100122 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500123 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700124}
Tony Jonesee959b02008-02-22 00:13:36 +0100125static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700126
127#define show_transport_attr(name, format) \
128static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100129show_transport_##name(struct device *dev, \
130 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700131{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100132 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700133 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
134} \
Tony Jonesee959b02008-02-22 00:13:36 +0100135static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700136
137show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700138
139static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100140 &dev_attr_handle.attr,
141 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700142 NULL,
143};
144
145static struct attribute_group iscsi_transport_group = {
146 .attrs = iscsi_transport_attrs,
147};
148
Mike Christied82ff9be2008-05-21 15:54:13 -0500149/*
150 * iSCSI endpoint attrs
151 */
152#define iscsi_dev_to_endpoint(_dev) \
153 container_of(_dev, struct iscsi_endpoint, dev)
154
155#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
156struct device_attribute dev_attr_##_prefix##_##_name = \
157 __ATTR(_name,_mode,_show,_store)
158
159static void iscsi_endpoint_release(struct device *dev)
160{
161 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
162 kfree(ep);
163}
164
165static struct class iscsi_endpoint_class = {
166 .name = "iscsi_endpoint",
167 .dev_release = iscsi_endpoint_release,
168};
169
170static ssize_t
171show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
172{
173 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500174 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500175}
176static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
177
178static struct attribute *iscsi_endpoint_attrs[] = {
179 &dev_attr_ep_handle.attr,
180 NULL,
181};
182
183static struct attribute_group iscsi_endpoint_group = {
184 .attrs = iscsi_endpoint_attrs,
185};
186
187#define ISCSI_MAX_EPID -1
188
189static int iscsi_match_epid(struct device *dev, void *data)
190{
191 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500192 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500193
194 return *epid == ep->id;
195}
196
197struct iscsi_endpoint *
198iscsi_create_endpoint(int dd_size)
199{
200 struct device *dev;
201 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500202 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500203 int err;
204
205 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400206 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500207 iscsi_match_epid);
208 if (!dev)
209 break;
210 }
211 if (id == ISCSI_MAX_EPID) {
212 printk(KERN_ERR "Too many connections. Max supported %u\n",
213 ISCSI_MAX_EPID - 1);
214 return NULL;
215 }
216
217 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
218 if (!ep)
219 return NULL;
220
221 ep->id = id;
222 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100223 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500224 err = device_register(&ep->dev);
225 if (err)
226 goto free_ep;
227
228 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
229 if (err)
230 goto unregister_dev;
231
232 if (dd_size)
233 ep->dd_data = &ep[1];
234 return ep;
235
236unregister_dev:
237 device_unregister(&ep->dev);
238 return NULL;
239
240free_ep:
241 kfree(ep);
242 return NULL;
243}
244EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
245
246void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
247{
248 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
249 device_unregister(&ep->dev);
250}
251EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
252
253struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
254{
Mike Christief80f8682008-06-16 10:11:35 -0500255 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500256 struct device *dev;
257
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400258 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500259 iscsi_match_epid);
260 if (!dev)
261 return NULL;
262
Mike Christief80f8682008-06-16 10:11:35 -0500263 ep = iscsi_dev_to_endpoint(dev);
264 /*
265 * we can drop this now because the interface will prevent
266 * removals and lookups from racing.
267 */
268 put_device(dev);
269 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500270}
271EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
272
Mike Christie30a6c652006-04-06 21:13:39 -0500273static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100274 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500275{
276 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500277 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500278
279 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600280 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500281 mutex_init(&ihost->mutex);
Mike Christie30a6c652006-04-06 21:13:39 -0500282 return 0;
283}
284
285static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
286 "iscsi_host",
287 iscsi_setup_host,
Mike Christie06d25af2009-03-05 14:46:02 -0600288 NULL,
Mike Christie30a6c652006-04-06 21:13:39 -0500289 NULL);
290
Alex Aizman0896b752005-08-04 19:33:07 -0700291static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
292 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 NULL,
294 NULL,
295 NULL);
296
Alex Aizman0896b752005-08-04 19:33:07 -0700297static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
298 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 NULL,
300 NULL,
301 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700302
303static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100304static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700305
Mike Christie7b7232f2006-02-01 21:06:49 -0600306static LIST_HEAD(sesslist);
307static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700308static LIST_HEAD(connlist);
309static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Mike Christieb5c7a122006-04-06 21:13:33 -0500311static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
312{
313 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
314 return sess->sid;
315}
316
317/*
318 * Returns the matching session to a given sid
319 */
320static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600321{
322 unsigned long flags;
323 struct iscsi_cls_session *sess;
324
325 spin_lock_irqsave(&sesslock, flags);
326 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500327 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600328 spin_unlock_irqrestore(&sesslock, flags);
329 return sess;
330 }
331 }
332 spin_unlock_irqrestore(&sesslock, flags);
333 return NULL;
334}
335
Mike Christieb5c7a122006-04-06 21:13:33 -0500336/*
337 * Returns the matching connection to a given sid / cid tuple
338 */
339static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600340{
341 unsigned long flags;
342 struct iscsi_cls_conn *conn;
343
344 spin_lock_irqsave(&connlock, flags);
345 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500346 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600347 spin_unlock_irqrestore(&connlock, flags);
348 return conn;
349 }
350 }
351 spin_unlock_irqrestore(&connlock, flags);
352 return NULL;
353}
354
Mike Christie7b8631b2006-01-13 18:05:50 -0600355/*
356 * The following functions can be used by LLDs that allocate
357 * their own scsi_hosts or by software iscsi LLDs
358 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600359static struct {
360 int value;
361 char *name;
362} iscsi_session_state_names[] = {
363 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
364 { ISCSI_SESSION_FAILED, "FAILED" },
365 { ISCSI_SESSION_FREE, "FREE" },
366};
367
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200368static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600369{
370 int i;
371 char *name = NULL;
372
373 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
374 if (iscsi_session_state_names[i].value == state) {
375 name = iscsi_session_state_names[i].name;
376 break;
377 }
378 }
379 return name;
380}
381
382int iscsi_session_chkready(struct iscsi_cls_session *session)
383{
384 unsigned long flags;
385 int err;
386
387 spin_lock_irqsave(&session->lock, flags);
388 switch (session->state) {
389 case ISCSI_SESSION_LOGGED_IN:
390 err = 0;
391 break;
392 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500393 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600394 break;
395 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500396 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600397 break;
398 default:
399 err = DID_NO_CONNECT << 16;
400 break;
401 }
402 spin_unlock_irqrestore(&session->lock, flags);
403 return err;
404}
405EXPORT_SYMBOL_GPL(iscsi_session_chkready);
406
Mike Christie7b8631b2006-01-13 18:05:50 -0600407static void iscsi_session_release(struct device *dev)
408{
409 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600410 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Mike Christie7b8631b2006-01-13 18:05:50 -0600412 shost = iscsi_session_to_shost(session);
413 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500414 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600415 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Mike Christie7b8631b2006-01-13 18:05:50 -0600418static int iscsi_is_session_dev(const struct device *dev)
419{
420 return dev->release == iscsi_session_release;
421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Mike Christiea4804cd2008-05-21 15:54:00 -0500423static int iscsi_iter_session_fn(struct device *dev, void *data)
424{
425 void (* fn) (struct iscsi_cls_session *) = data;
426
427 if (!iscsi_is_session_dev(dev))
428 return 0;
429 fn(iscsi_dev_to_session(dev));
430 return 0;
431}
432
433void iscsi_host_for_each_session(struct Scsi_Host *shost,
434 void (*fn)(struct iscsi_cls_session *))
435{
436 device_for_each_child(&shost->shost_gendev, fn,
437 iscsi_iter_session_fn);
438}
439EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
440
Mike Christie8aae18a2008-01-31 13:36:48 -0600441/**
442 * iscsi_scan_finished - helper to report when running scans are done
443 * @shost: scsi host
444 * @time: scan run time
445 *
446 * This function can be used by drives like qla4xxx to report to the scsi
447 * layer when the scans it kicked off at module load time are done.
448 */
449int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
450{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500451 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600452 /*
453 * qla4xxx will have kicked off some session unblocks before calling
454 * scsi_scan_host, so just wait for them to complete.
455 */
456 return !atomic_read(&ihost->nr_scans);
457}
458EXPORT_SYMBOL_GPL(iscsi_scan_finished);
459
Mike Christie79706342008-05-21 15:54:12 -0500460struct iscsi_scan_data {
461 unsigned int channel;
462 unsigned int id;
463 unsigned int lun;
464};
465
466static int iscsi_user_scan_session(struct device *dev, void *data)
467{
468 struct iscsi_scan_data *scan_data = data;
469 struct iscsi_cls_session *session;
470 struct Scsi_Host *shost;
471 struct iscsi_cls_host *ihost;
472 unsigned long flags;
473 unsigned int id;
474
475 if (!iscsi_is_session_dev(dev))
476 return 0;
477
478 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500479
480 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
481
Mike Christie79706342008-05-21 15:54:12 -0500482 shost = iscsi_session_to_shost(session);
483 ihost = shost->shost_data;
484
485 mutex_lock(&ihost->mutex);
486 spin_lock_irqsave(&session->lock, flags);
487 if (session->state != ISCSI_SESSION_LOGGED_IN) {
488 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500489 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500490 }
491 id = session->target_id;
492 spin_unlock_irqrestore(&session->lock, flags);
493
494 if (id != ISCSI_MAX_TARGET) {
495 if ((scan_data->channel == SCAN_WILD_CARD ||
496 scan_data->channel == 0) &&
497 (scan_data->id == SCAN_WILD_CARD ||
498 scan_data->id == id))
499 scsi_scan_target(&session->dev, 0, id,
500 scan_data->lun, 1);
501 }
Mike Christie632248a2009-08-20 15:11:01 -0500502
503user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500504 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500505 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500506 return 0;
507}
508
Mike Christie30a6c652006-04-06 21:13:39 -0500509static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
510 uint id, uint lun)
511{
Mike Christie79706342008-05-21 15:54:12 -0500512 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500513
Mike Christie79706342008-05-21 15:54:12 -0500514 scan_data.channel = channel;
515 scan_data.id = id;
516 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500517
Mike Christie79706342008-05-21 15:54:12 -0500518 return device_for_each_child(&shost->shost_gendev, &scan_data,
519 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500520}
521
Mike Christiebd976f62008-01-31 13:36:46 -0600522static void iscsi_scan_session(struct work_struct *work)
523{
524 struct iscsi_cls_session *session =
525 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600526 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500527 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500528 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600529
Mike Christie79706342008-05-21 15:54:12 -0500530 scan_data.channel = 0;
531 scan_data.id = SCAN_WILD_CARD;
532 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600533
Mike Christie79706342008-05-21 15:54:12 -0500534 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600535 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600536}
537
Mike Christiec01be6d2010-07-22 16:59:49 +0530538/**
539 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700540 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530541 *
542 * If the session is down this function will wait for the recovery
543 * timer to fire or for the session to be logged back in. If the
544 * recovery timer fires then FAST_IO_FAIL is returned. The caller
545 * should pass this error value to the scsi eh.
546 */
547int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
548{
549 struct iscsi_cls_session *session =
550 starget_to_session(scsi_target(cmd->device));
551 unsigned long flags;
552 int ret = 0;
553
554 spin_lock_irqsave(&session->lock, flags);
555 while (session->state != ISCSI_SESSION_LOGGED_IN) {
556 if (session->state == ISCSI_SESSION_FREE) {
557 ret = FAST_IO_FAIL;
558 break;
559 }
560 spin_unlock_irqrestore(&session->lock, flags);
561 msleep(1000);
562 spin_lock_irqsave(&session->lock, flags);
563 }
564 spin_unlock_irqrestore(&session->lock, flags);
565 return ret;
566}
567EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
568
David Howellsc4028952006-11-22 14:57:56 +0000569static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500570{
David Howellsc4028952006-11-22 14:57:56 +0000571 struct iscsi_cls_session *session =
572 container_of(work, struct iscsi_cls_session,
573 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600574 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500575
Mike Christie322d7392008-01-31 13:36:52 -0600576 iscsi_cls_session_printk(KERN_INFO, session,
577 "session recovery timed out after %d secs\n",
578 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500579
Mike Christie6eabafb2008-01-31 13:36:43 -0600580 spin_lock_irqsave(&session->lock, flags);
581 switch (session->state) {
582 case ISCSI_SESSION_FAILED:
583 session->state = ISCSI_SESSION_FREE;
584 break;
585 case ISCSI_SESSION_LOGGED_IN:
586 case ISCSI_SESSION_FREE:
587 /* we raced with the unblock's flush */
588 spin_unlock_irqrestore(&session->lock, flags);
589 return;
590 }
591 spin_unlock_irqrestore(&session->lock, flags);
592
Mike Christie30a6c652006-04-06 21:13:39 -0500593 if (session->transport->session_recovery_timedout)
594 session->transport->session_recovery_timedout(session);
595
Mike Christie632248a2009-08-20 15:11:01 -0500596 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500597 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500598 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500599}
600
Mike Christie45ab33b2008-03-04 13:26:55 -0600601static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500602{
Mike Christie45ab33b2008-03-04 13:26:55 -0600603 struct iscsi_cls_session *session =
604 container_of(work, struct iscsi_cls_session,
605 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600606 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500607 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600608 unsigned long flags;
609
Mike Christie632248a2009-08-20 15:11:01 -0500610 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600611 /*
612 * The recovery and unblock work get run from the same workqueue,
613 * so try to cancel it if it was going to run after this unblock.
614 */
615 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600616 spin_lock_irqsave(&session->lock, flags);
617 session->state = ISCSI_SESSION_LOGGED_IN;
618 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600619 /* start IO */
620 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600621 /*
622 * Only do kernel scanning if the driver is properly hooked into
623 * the async scanning code (drivers like iscsi_tcp do login and
624 * scanning from userspace).
625 */
626 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600627 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600628 atomic_inc(&ihost->nr_scans);
629 }
Mike Christie632248a2009-08-20 15:11:01 -0500630 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600631}
Mike Christie45ab33b2008-03-04 13:26:55 -0600632
633/**
634 * iscsi_unblock_session - set a session as logged in and start IO.
635 * @session: iscsi session
636 *
637 * Mark a session as ready to accept IO.
638 */
639void iscsi_unblock_session(struct iscsi_cls_session *session)
640{
641 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
642 /*
643 * make sure all the events have completed before tell the driver
644 * it is safe
645 */
646 flush_workqueue(iscsi_eh_timer_workq);
647}
Mike Christie30a6c652006-04-06 21:13:39 -0500648EXPORT_SYMBOL_GPL(iscsi_unblock_session);
649
Mike Christie45ab33b2008-03-04 13:26:55 -0600650static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500651{
Mike Christie45ab33b2008-03-04 13:26:55 -0600652 struct iscsi_cls_session *session =
653 container_of(work, struct iscsi_cls_session,
654 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600655 unsigned long flags;
656
Mike Christie632248a2009-08-20 15:11:01 -0500657 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600658 spin_lock_irqsave(&session->lock, flags);
659 session->state = ISCSI_SESSION_FAILED;
660 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500661 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500662 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600663 if (session->recovery_tmo >= 0)
664 queue_delayed_work(iscsi_eh_timer_workq,
665 &session->recovery_work,
666 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500667}
Mike Christie45ab33b2008-03-04 13:26:55 -0600668
669void iscsi_block_session(struct iscsi_cls_session *session)
670{
671 queue_work(iscsi_eh_timer_workq, &session->block_work);
672}
Mike Christie30a6c652006-04-06 21:13:39 -0500673EXPORT_SYMBOL_GPL(iscsi_block_session);
674
Mike Christie26974782007-12-13 12:43:29 -0600675static void __iscsi_unbind_session(struct work_struct *work)
676{
677 struct iscsi_cls_session *session =
678 container_of(work, struct iscsi_cls_session,
679 unbind_work);
680 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500681 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500682 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600683
Mike Christie632248a2009-08-20 15:11:01 -0500684 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
685
Mike Christie26974782007-12-13 12:43:29 -0600686 /* Prevent new scans and make sure scanning is not in progress */
687 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500688 spin_lock_irqsave(&session->lock, flags);
689 if (session->target_id == ISCSI_MAX_TARGET) {
690 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600691 mutex_unlock(&ihost->mutex);
692 return;
693 }
Mike Christie79706342008-05-21 15:54:12 -0500694 session->target_id = ISCSI_MAX_TARGET;
695 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600696 mutex_unlock(&ihost->mutex);
697
698 scsi_remove_target(&session->dev);
699 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500700 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -0600701}
702
Mike Christie7b8631b2006-01-13 18:05:50 -0600703struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500704iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
705 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600706{
707 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Mike Christie5d91e202008-05-21 15:54:01 -0500709 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500710 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600711 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500712 return NULL;
713
Mike Christie7b8631b2006-01-13 18:05:50 -0600714 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500715 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600716 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000717 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500718 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600719 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
720 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600721 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600722 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600723 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600724
Mike Christie6a8a0d32006-06-28 12:00:31 -0500725 /* this is released in the dev's release function */
726 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500727 session->dev.parent = &shost->shost_gendev;
728 session->dev.release = iscsi_session_release;
729 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500730 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500731 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -0500732
733 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500734 return session;
735}
736EXPORT_SYMBOL_GPL(iscsi_alloc_session);
737
Mike Christie79706342008-05-21 15:54:12 -0500738static int iscsi_get_next_target_id(struct device *dev, void *data)
739{
740 struct iscsi_cls_session *session;
741 unsigned long flags;
742 int err = 0;
743
744 if (!iscsi_is_session_dev(dev))
745 return 0;
746
747 session = iscsi_dev_to_session(dev);
748 spin_lock_irqsave(&session->lock, flags);
749 if (*((unsigned int *) data) == session->target_id)
750 err = -EEXIST;
751 spin_unlock_irqrestore(&session->lock, flags);
752 return err;
753}
754
Mike Christie6a8a0d32006-06-28 12:00:31 -0500755int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500756{
757 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500758 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600759 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500760 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500761 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500762
Mike Christie30a6c652006-04-06 21:13:39 -0500763 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600764 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500765
766 if (id == ISCSI_MAX_TARGET) {
767 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
768 err = device_for_each_child(&shost->shost_gendev, &id,
769 iscsi_get_next_target_id);
770 if (!err)
771 break;
772 }
773
774 if (id == ISCSI_MAX_TARGET) {
775 iscsi_cls_session_printk(KERN_ERR, session,
776 "Too many iscsi targets. Max "
777 "number of targets is %d.\n",
778 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +0530779 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -0500780 goto release_host;
781 }
782 }
783 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500784
Kay Sievers71610f52008-12-03 22:41:36 +0100785 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500786 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600787 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600788 iscsi_cls_session_printk(KERN_ERR, session,
789 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500790 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600791 }
792 transport_register_device(&session->dev);
793
Mike Christie26974782007-12-13 12:43:29 -0600794 spin_lock_irqsave(&sesslock, flags);
795 list_add(&session->sess_list, &sesslist);
796 spin_unlock_irqrestore(&sesslock, flags);
797
Mike Christie26974782007-12-13 12:43:29 -0600798 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500799 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500800 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500801
Mike Christie8434aa82006-06-28 12:00:30 -0500802release_host:
803 scsi_host_put(shost);
804 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600805}
Mike Christie8434aa82006-06-28 12:00:30 -0500806EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600807
808/**
Mike Christie8434aa82006-06-28 12:00:30 -0500809 * iscsi_create_session - create iscsi class session
810 * @shost: scsi host
811 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500812 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500813 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600814 *
Mike Christie8434aa82006-06-28 12:00:30 -0500815 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500816 */
Mike Christie8434aa82006-06-28 12:00:30 -0500817struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500818iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
819 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500820{
821 struct iscsi_cls_session *session;
822
Mike Christie5d91e202008-05-21 15:54:01 -0500823 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500824 if (!session)
825 return NULL;
826
Mike Christie6a8a0d32006-06-28 12:00:31 -0500827 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500828 iscsi_free_session(session);
829 return NULL;
830 }
831 return session;
832}
833EXPORT_SYMBOL_GPL(iscsi_create_session);
834
Mike Christie26974782007-12-13 12:43:29 -0600835static void iscsi_conn_release(struct device *dev)
836{
837 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
838 struct device *parent = conn->dev.parent;
839
Mike Christie632248a2009-08-20 15:11:01 -0500840 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -0600841 kfree(conn);
842 put_device(parent);
843}
844
845static int iscsi_is_conn_dev(const struct device *dev)
846{
847 return dev->release == iscsi_conn_release;
848}
849
850static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
851{
852 if (!iscsi_is_conn_dev(dev))
853 return 0;
854 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
855}
856
Mike Christie8434aa82006-06-28 12:00:30 -0500857void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600858{
Mike Christie30a6c652006-04-06 21:13:39 -0500859 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -0600860 unsigned long flags;
861 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500862
Mike Christie632248a2009-08-20 15:11:01 -0500863 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
864
Mike Christie26974782007-12-13 12:43:29 -0600865 spin_lock_irqsave(&sesslock, flags);
866 list_del(&session->sess_list);
867 spin_unlock_irqrestore(&sesslock, flags);
868
Mike Christie45ab33b2008-03-04 13:26:55 -0600869 /* make sure there are no blocks/unblocks queued */
870 flush_workqueue(iscsi_eh_timer_workq);
871 /* make sure the timedout callout is not running */
872 if (!cancel_delayed_work(&session->recovery_work))
873 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600874 /*
875 * If we are blocked let commands flow again. The lld or iscsi
876 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -0600877 * We assume that LLD will not be calling block/unblock while
878 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -0600879 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600880 spin_lock_irqsave(&session->lock, flags);
881 session->state = ISCSI_SESSION_FREE;
882 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -0600883
Mike Christie45ab33b2008-03-04 13:26:55 -0600884 scsi_target_unblock(&session->dev);
885 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -0600886 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -0600887 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -0500888
Mike Christie26974782007-12-13 12:43:29 -0600889 /* hw iscsi may not have removed all connections from session */
890 err = device_for_each_child(&session->dev, NULL,
891 iscsi_iter_destroy_conn_fn);
892 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600893 iscsi_cls_session_printk(KERN_ERR, session,
894 "Could not delete all connections "
895 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500896
Mike Christie7b8631b2006-01-13 18:05:50 -0600897 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500898
899 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500900 device_del(&session->dev);
901}
902EXPORT_SYMBOL_GPL(iscsi_remove_session);
903
904void iscsi_free_session(struct iscsi_cls_session *session)
905{
Mike Christie632248a2009-08-20 15:11:01 -0500906 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -0600907 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500908 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600909}
Mike Christie8434aa82006-06-28 12:00:30 -0500910EXPORT_SYMBOL_GPL(iscsi_free_session);
911
912/**
913 * iscsi_destroy_session - destroy iscsi session
914 * @session: iscsi_session
915 *
916 * Can be called by a LLD or iscsi_transport. There must not be
917 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500918 */
Mike Christie8434aa82006-06-28 12:00:30 -0500919int iscsi_destroy_session(struct iscsi_cls_session *session)
920{
921 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -0500922 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500923 iscsi_free_session(session);
924 return 0;
925}
Mike Christie7b8631b2006-01-13 18:05:50 -0600926EXPORT_SYMBOL_GPL(iscsi_destroy_session);
927
Mike Christie7b8631b2006-01-13 18:05:50 -0600928/**
929 * iscsi_create_conn - create iscsi class connection
930 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -0500931 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -0600932 * @cid: connection id
933 *
934 * This can be called from a LLD or iscsi_transport. The connection
935 * is child of the session so cid must be unique for all connections
936 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500937 *
938 * Since we do not support MCS, cid will normally be zero. In some cases
939 * for software iscsi we could be trying to preallocate a connection struct
940 * in which case there could be two connection structs and cid would be
941 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500942 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600943struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -0500944iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -0600945{
946 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600947 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600948 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600949 int err;
950
Mike Christie5d91e202008-05-21 15:54:01 -0500951 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600952 if (!conn)
953 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -0500954 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600955 conn->dd_data = &conn[1];
956
Mike Christie22a39fb2011-02-16 15:04:33 -0600957 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -0600958 INIT_LIST_HEAD(&conn->conn_list);
959 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500960 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600961
962 /* this is released in the dev's release function */
963 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400964 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500965
Kay Sievers71610f52008-12-03 22:41:36 +0100966 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600967 conn->dev.parent = &session->dev;
968 conn->dev.release = iscsi_conn_release;
969 err = device_register(&conn->dev);
970 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600971 iscsi_cls_session_printk(KERN_ERR, session, "could not "
972 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600973 goto release_parent_ref;
974 }
975 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600976
977 spin_lock_irqsave(&connlock, flags);
978 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -0600979 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500980
981 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600982 return conn;
983
984release_parent_ref:
985 put_device(&session->dev);
986free_conn:
987 kfree(conn);
988 return NULL;
989}
990
991EXPORT_SYMBOL_GPL(iscsi_create_conn);
992
993/**
994 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500995 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600996 *
Mike Christie26974782007-12-13 12:43:29 -0600997 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500998 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600999int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1000{
Mike Christie26974782007-12-13 12:43:29 -06001001 unsigned long flags;
1002
1003 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001004 list_del(&conn->conn_list);
1005 spin_unlock_irqrestore(&connlock, flags);
1006
Mike Christie7b8631b2006-01-13 18:05:50 -06001007 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001008 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001009 device_unregister(&conn->dev);
1010 return 0;
1011}
Mike Christie7b8631b2006-01-13 18:05:50 -06001012EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1013
1014/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001015 * iscsi interface functions
1016 */
Alex Aizman0896b752005-08-04 19:33:07 -07001017static struct iscsi_internal *
1018iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Alex Aizman0896b752005-08-04 19:33:07 -07001020 struct iscsi_internal *priv;
1021 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Alex Aizman0896b752005-08-04 19:33:07 -07001023 spin_lock_irqsave(&iscsi_transport_lock, flags);
1024 list_for_each_entry(priv, &iscsi_transports, list) {
1025 if (tt == priv->iscsi_transport) {
1026 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1027 return priv;
1028 }
1029 }
1030 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1031 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Alex Aizman0896b752005-08-04 19:33:07 -07001034static int
Michael Chan43514772009-06-08 18:14:41 -07001035iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001036{
Michael Chan43514772009-06-08 18:14:41 -07001037 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001038}
1039
Mike Christie7b7232f2006-02-01 21:06:49 -06001040int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001041 char *data, uint32_t data_size)
1042{
1043 struct nlmsghdr *nlh;
1044 struct sk_buff *skb;
1045 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001046 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001047 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001048 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1049 data_size);
1050
Mike Christie790f39a2006-05-18 20:31:39 -05001051 priv = iscsi_if_transport_lookup(conn->transport);
1052 if (!priv)
1053 return -EINVAL;
1054
Mike Christie43a145a2006-10-16 18:09:38 -04001055 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001056 if (!skb) {
Mike Christiee5bd7b542008-09-24 11:46:10 -05001057 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001058 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1059 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001060 return -ENOMEM;
1061 }
1062
Michael Chan43514772009-06-08 18:14:41 -07001063 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001064 ev = NLMSG_DATA(nlh);
1065 memset(ev, 0, sizeof(*ev));
1066 ev->transport_handle = iscsi_handle(conn->transport);
1067 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001068 ev->r.recv_req.cid = conn->cid;
1069 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001070 pdu = (char*)ev + sizeof(*ev);
1071 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1072 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1073
Michael Chan43514772009-06-08 18:14:41 -07001074 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001075}
1076EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1077
Michael Chan43514772009-06-08 18:14:41 -07001078int iscsi_offload_mesg(struct Scsi_Host *shost,
1079 struct iscsi_transport *transport, uint32_t type,
1080 char *data, uint16_t data_size)
1081{
1082 struct nlmsghdr *nlh;
1083 struct sk_buff *skb;
1084 struct iscsi_uevent *ev;
1085 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1086
Michael Chana541f842009-07-29 08:49:52 +00001087 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001088 if (!skb) {
1089 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1090 return -ENOMEM;
1091 }
1092
1093 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1094 ev = NLMSG_DATA(nlh);
1095 memset(ev, 0, sizeof(*ev));
1096 ev->type = type;
1097 ev->transport_handle = iscsi_handle(transport);
1098 switch (type) {
1099 case ISCSI_KEVENT_PATH_REQ:
1100 ev->r.req_path.host_no = shost->host_no;
1101 break;
1102 case ISCSI_KEVENT_IF_DOWN:
1103 ev->r.notify_if_down.host_no = shost->host_no;
1104 break;
1105 }
1106
1107 memcpy((char *)ev + sizeof(*ev), data, data_size);
1108
Michael Chana541f842009-07-29 08:49:52 +00001109 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001110}
1111EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1112
Mike Christiee5bd7b542008-09-24 11:46:10 -05001113void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001114{
1115 struct nlmsghdr *nlh;
1116 struct sk_buff *skb;
1117 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001118 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001119 int len = NLMSG_SPACE(sizeof(*ev));
1120
Mike Christie790f39a2006-05-18 20:31:39 -05001121 priv = iscsi_if_transport_lookup(conn->transport);
1122 if (!priv)
1123 return;
1124
Mike Christie43a145a2006-10-16 18:09:38 -04001125 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001126 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001127 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1128 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001129 return;
1130 }
1131
Michael Chan43514772009-06-08 18:14:41 -07001132 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001133 ev = NLMSG_DATA(nlh);
1134 ev->transport_handle = iscsi_handle(conn->transport);
1135 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001136 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001137 ev->r.connerror.cid = conn->cid;
1138 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001139
Michael Chan43514772009-06-08 18:14:41 -07001140 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001141
Mike Christie322d7392008-01-31 13:36:52 -06001142 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1143 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001144}
Mike Christiee5bd7b542008-09-24 11:46:10 -05001145EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001146
1147static int
Michael Chan43514772009-06-08 18:14:41 -07001148iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1149 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001150{
1151 struct sk_buff *skb;
1152 struct nlmsghdr *nlh;
1153 int len = NLMSG_SPACE(size);
1154 int flags = multi ? NLM_F_MULTI : 0;
1155 int t = done ? NLMSG_DONE : type;
1156
Mike Christie43a145a2006-10-16 18:09:38 -04001157 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001158 if (!skb) {
1159 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1160 return -ENOMEM;
1161 }
Alex Aizman0896b752005-08-04 19:33:07 -07001162
Michael Chan43514772009-06-08 18:14:41 -07001163 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001164 nlh->nlmsg_flags = flags;
1165 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001166 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001167}
1168
1169static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001170iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001171{
1172 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1173 struct iscsi_stats *stats;
1174 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001175 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001176 struct nlmsghdr *nlhstat;
1177 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001178 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001179 int len = NLMSG_SPACE(sizeof(*ev) +
1180 sizeof(struct iscsi_stats) +
1181 sizeof(struct iscsi_stats_custom) *
1182 ISCSI_STATS_CUSTOM_MAX);
1183 int err = 0;
1184
Mike Christie790f39a2006-05-18 20:31:39 -05001185 priv = iscsi_if_transport_lookup(transport);
1186 if (!priv)
1187 return -EINVAL;
1188
Mike Christieb5c7a122006-04-06 21:13:33 -05001189 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001190 if (!conn)
1191 return -EEXIST;
1192
1193 do {
1194 int actual_size;
1195
Mike Christie43a145a2006-10-16 18:09:38 -04001196 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001197 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001198 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1199 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001200 return -ENOMEM;
1201 }
1202
Michael Chan43514772009-06-08 18:14:41 -07001203 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001204 (len - sizeof(*nlhstat)), 0);
1205 evstat = NLMSG_DATA(nlhstat);
1206 memset(evstat, 0, sizeof(*evstat));
1207 evstat->transport_handle = iscsi_handle(conn->transport);
1208 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001209 evstat->u.get_stats.cid =
1210 ev->u.get_stats.cid;
1211 evstat->u.get_stats.sid =
1212 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001213 stats = (struct iscsi_stats *)
1214 ((char*)evstat + sizeof(*evstat));
1215 memset(stats, 0, sizeof(*stats));
1216
Mike Christie7b7232f2006-02-01 21:06:49 -06001217 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001218 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1219 sizeof(struct iscsi_stats) +
1220 sizeof(struct iscsi_stats_custom) *
1221 stats->custom_length);
1222 actual_size -= sizeof(*nlhstat);
1223 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001224 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001225 nlhstat->nlmsg_len = actual_size;
1226
Michael Chan43514772009-06-08 18:14:41 -07001227 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1228 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001229 } while (err < 0 && err != -ECONNREFUSED);
1230
1231 return err;
1232}
1233
Mike Christie53cb8a12006-06-28 12:00:32 -05001234/**
Mike Christie26974782007-12-13 12:43:29 -06001235 * iscsi_session_event - send session destr. completion event
1236 * @session: iscsi class session
1237 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001238 */
Mike Christie26974782007-12-13 12:43:29 -06001239int iscsi_session_event(struct iscsi_cls_session *session,
1240 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001241{
1242 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001243 struct Scsi_Host *shost;
1244 struct iscsi_uevent *ev;
1245 struct sk_buff *skb;
1246 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001247 int rc, len = NLMSG_SPACE(sizeof(*ev));
1248
Mike Christie26974782007-12-13 12:43:29 -06001249 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001250 if (!priv)
1251 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001252 shost = iscsi_session_to_shost(session);
1253
Mike Christie43a145a2006-10-16 18:09:38 -04001254 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001255 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001256 iscsi_cls_session_printk(KERN_ERR, session,
1257 "Cannot notify userspace of session "
1258 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001259 return -ENOMEM;
1260 }
1261
Michael Chan43514772009-06-08 18:14:41 -07001262 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001263 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001264 ev->transport_handle = iscsi_handle(session->transport);
1265
1266 ev->type = event;
1267 switch (event) {
1268 case ISCSI_KEVENT_DESTROY_SESSION:
1269 ev->r.d_session.host_no = shost->host_no;
1270 ev->r.d_session.sid = session->sid;
1271 break;
1272 case ISCSI_KEVENT_CREATE_SESSION:
1273 ev->r.c_session_ret.host_no = shost->host_no;
1274 ev->r.c_session_ret.sid = session->sid;
1275 break;
1276 case ISCSI_KEVENT_UNBIND_SESSION:
1277 ev->r.unbind_session.host_no = shost->host_no;
1278 ev->r.unbind_session.sid = session->sid;
1279 break;
1280 default:
Mike Christie322d7392008-01-31 13:36:52 -06001281 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1282 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001283 kfree_skb(skb);
1284 return -EINVAL;
1285 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001286
1287 /*
1288 * this will occur if the daemon is not up, so we just warn
1289 * the user and when the daemon is restarted it will handle it
1290 */
Michael Chan43514772009-06-08 18:14:41 -07001291 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001292 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001293 iscsi_cls_session_printk(KERN_ERR, session,
1294 "Cannot notify userspace of session "
1295 "event %u. Check iscsi daemon\n",
1296 event);
Mike Christie632248a2009-08-20 15:11:01 -05001297
1298 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1299 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001300 return rc;
1301}
Mike Christie26974782007-12-13 12:43:29 -06001302EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001303
Alex Aizman0896b752005-08-04 19:33:07 -07001304static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001305iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1306 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001307 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001308{
1309 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001310 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001311 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001312
Mike Christied82ff9be2008-05-21 15:54:13 -05001313 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001314 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001315 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001316 return -ENOMEM;
1317
Mike Christie5e7facb2009-03-05 14:46:06 -06001318 shost = iscsi_session_to_shost(session);
1319 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001320 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001321 ISCSI_DBG_TRANS_SESSION(session,
1322 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001323 return 0;
1324}
1325
1326static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001327iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001328{
Mike Christie7b8631b2006-01-13 18:05:50 -06001329 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001330 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001331
Mike Christieb5c7a122006-04-06 21:13:33 -05001332 session = iscsi_session_lookup(ev->u.c_conn.sid);
1333 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001334 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001335 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001336 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001337 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001338
Mike Christie7b7232f2006-02-01 21:06:49 -06001339 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001340 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001341 iscsi_cls_session_printk(KERN_ERR, session,
1342 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001343 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001344 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001345
Mike Christieb5c7a122006-04-06 21:13:33 -05001346 ev->r.c_conn_ret.sid = session->sid;
1347 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001348
1349 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001350 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001351}
1352
1353static int
1354iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1355{
Mike Christie7b8631b2006-01-13 18:05:50 -06001356 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001357
Mike Christieb5c7a122006-04-06 21:13:33 -05001358 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001359 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001360 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001361
Mike Christie632248a2009-08-20 15:11:01 -05001362 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001363 if (transport->destroy_conn)
1364 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001365
Mike Christie7b8631b2006-01-13 18:05:50 -06001366 return 0;
1367}
1368
Mike Christiefd7255f2006-04-06 21:13:36 -05001369static int
1370iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1371{
1372 char *data = (char*)ev + sizeof(*ev);
1373 struct iscsi_cls_conn *conn;
1374 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001375 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001376
1377 session = iscsi_session_lookup(ev->u.set_param.sid);
1378 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1379 if (!conn || !session)
1380 return -EINVAL;
1381
1382 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001383 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001384 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001385 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001386 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001387 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001388 err = transport->set_param(conn, ev->u.set_param.param,
1389 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001390 }
1391
1392 return err;
1393}
1394
Mike Christie10eb0f02009-05-13 17:57:38 -05001395static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1396 struct iscsi_uevent *ev, int msg_type)
1397{
1398 struct iscsi_endpoint *ep;
1399 struct sockaddr *dst_addr;
1400 struct Scsi_Host *shost = NULL;
1401 int non_blocking, err = 0;
1402
1403 if (!transport->ep_connect)
1404 return -EINVAL;
1405
1406 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1407 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1408 if (!shost) {
1409 printk(KERN_ERR "ep connect failed. Could not find "
1410 "host no %u\n",
1411 ev->u.ep_connect_through_host.host_no);
1412 return -ENODEV;
1413 }
1414 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1415 } else
1416 non_blocking = ev->u.ep_connect.non_blocking;
1417
1418 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1419 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1420 if (IS_ERR(ep)) {
1421 err = PTR_ERR(ep);
1422 goto release_host;
1423 }
1424
1425 ev->r.ep_connect_ret.handle = ep->id;
1426release_host:
1427 if (shost)
1428 scsi_host_put(shost);
1429 return err;
1430}
1431
Mike Christie22a39fb2011-02-16 15:04:33 -06001432static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1433 u64 ep_handle)
1434{
1435 struct iscsi_cls_conn *conn;
1436 struct iscsi_endpoint *ep;
1437
1438 if (!transport->ep_disconnect)
1439 return -EINVAL;
1440
1441 ep = iscsi_lookup_endpoint(ep_handle);
1442 if (!ep)
1443 return -EINVAL;
1444 conn = ep->conn;
1445 if (conn) {
1446 mutex_lock(&conn->ep_mutex);
1447 conn->ep = NULL;
1448 mutex_unlock(&conn->ep_mutex);
1449 }
1450
1451 transport->ep_disconnect(ep);
1452 return 0;
1453}
1454
Mike Christie7b8631b2006-01-13 18:05:50 -06001455static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001456iscsi_if_transport_ep(struct iscsi_transport *transport,
1457 struct iscsi_uevent *ev, int msg_type)
1458{
Mike Christied82ff9be2008-05-21 15:54:13 -05001459 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001460 int rc = 0;
1461
1462 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001463 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001464 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001465 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001466 break;
1467 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1468 if (!transport->ep_poll)
1469 return -EINVAL;
1470
Mike Christied82ff9be2008-05-21 15:54:13 -05001471 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1472 if (!ep)
1473 return -EINVAL;
1474
1475 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001476 ev->u.ep_poll.timeout_ms);
1477 break;
1478 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001479 rc = iscsi_if_ep_disconnect(transport,
1480 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001481 break;
1482 }
1483 return rc;
1484}
1485
1486static int
Mike Christie01cb2252006-06-28 12:00:22 -05001487iscsi_tgt_dscvr(struct iscsi_transport *transport,
1488 struct iscsi_uevent *ev)
1489{
Mike Christie2174a042007-05-30 12:57:10 -05001490 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001491 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001492 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001493
1494 if (!transport->tgt_dscvr)
1495 return -EINVAL;
1496
Mike Christie2174a042007-05-30 12:57:10 -05001497 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001498 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001499 printk(KERN_ERR "target discovery could not find host no %u\n",
1500 ev->u.tgt_dscvr.host_no);
1501 return -ENODEV;
1502 }
1503
1504
Mike Christie01cb2252006-06-28 12:00:22 -05001505 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001506 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1507 ev->u.tgt_dscvr.enable, dst_addr);
1508 scsi_host_put(shost);
1509 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001510}
1511
1512static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001513iscsi_set_host_param(struct iscsi_transport *transport,
1514 struct iscsi_uevent *ev)
1515{
1516 char *data = (char*)ev + sizeof(*ev);
1517 struct Scsi_Host *shost;
1518 int err;
1519
1520 if (!transport->set_host_param)
1521 return -ENOSYS;
1522
1523 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001524 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001525 printk(KERN_ERR "set_host_param could not find host no %u\n",
1526 ev->u.set_host_param.host_no);
1527 return -ENODEV;
1528 }
1529
1530 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1531 data, ev->u.set_host_param.len);
1532 scsi_host_put(shost);
1533 return err;
1534}
1535
1536static int
Michael Chan43514772009-06-08 18:14:41 -07001537iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1538{
1539 struct Scsi_Host *shost;
1540 struct iscsi_path *params;
1541 int err;
1542
1543 if (!transport->set_path)
1544 return -ENOSYS;
1545
1546 shost = scsi_host_lookup(ev->u.set_path.host_no);
1547 if (!shost) {
1548 printk(KERN_ERR "set path could not find host no %u\n",
1549 ev->u.set_path.host_no);
1550 return -ENODEV;
1551 }
1552
1553 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1554 err = transport->set_path(shost, params);
1555
1556 scsi_host_put(shost);
1557 return err;
1558}
1559
1560static int
Mike Christie56c155b2011-07-25 13:48:37 -05001561iscsi_set_iface_params(struct iscsi_transport *transport,
1562 struct iscsi_uevent *ev)
1563{
1564 char *data = (char *)ev + sizeof(*ev);
1565 struct Scsi_Host *shost;
1566 int err;
1567
1568 if (!transport->set_iface_param)
1569 return -ENOSYS;
1570
1571 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1572 if (!shost) {
1573 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1574 ev->u.set_iface_params.host_no);
1575 return -ENODEV;
1576 }
1577
1578 err = transport->set_iface_param(shost, data,
1579 ev->u.set_iface_params.count);
1580 scsi_host_put(shost);
1581 return err;
1582}
1583
1584static int
Michael Chan43514772009-06-08 18:14:41 -07001585iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001586{
1587 int err = 0;
1588 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1589 struct iscsi_transport *transport = NULL;
1590 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001591 struct iscsi_cls_session *session;
1592 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001593 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001594
Michael Chan43514772009-06-08 18:14:41 -07001595 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1596 *group = ISCSI_NL_GRP_UIP;
1597 else
1598 *group = ISCSI_NL_GRP_ISCSID;
1599
Alex Aizman0896b752005-08-04 19:33:07 -07001600 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1601 if (!priv)
1602 return -EINVAL;
1603 transport = priv->iscsi_transport;
1604
Mike Christie7b7232f2006-02-01 21:06:49 -06001605 if (!try_module_get(transport->owner))
1606 return -EINVAL;
1607
Alex Aizman0896b752005-08-04 19:33:07 -07001608 switch (nlh->nlmsg_type) {
1609 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001610 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001611 ev->u.c_session.initial_cmdsn,
1612 ev->u.c_session.cmds_max,
1613 ev->u.c_session.queue_depth);
1614 break;
1615 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001616 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001617 if (!ep) {
1618 err = -EINVAL;
1619 break;
1620 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001621
1622 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001623 ev->u.c_bound_session.initial_cmdsn,
1624 ev->u.c_bound_session.cmds_max,
1625 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001626 break;
1627 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001628 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001629 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001630 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001631 else
1632 err = -EINVAL;
1633 break;
1634 case ISCSI_UEVENT_UNBIND_SESSION:
1635 session = iscsi_session_lookup(ev->u.d_session.sid);
1636 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001637 scsi_queue_work(iscsi_session_to_shost(session),
1638 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001639 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001640 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001641 break;
1642 case ISCSI_UEVENT_CREATE_CONN:
1643 err = iscsi_if_create_conn(transport, ev);
1644 break;
1645 case ISCSI_UEVENT_DESTROY_CONN:
1646 err = iscsi_if_destroy_conn(transport, ev);
1647 break;
1648 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001649 session = iscsi_session_lookup(ev->u.b_conn.sid);
1650 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001651
Mike Christie22a39fb2011-02-16 15:04:33 -06001652 if (conn && conn->ep)
1653 iscsi_if_ep_disconnect(transport, conn->ep->id);
1654
1655 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001656 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001657 break;
1658 }
1659
1660 ev->r.retcode = transport->bind_conn(session, conn,
1661 ev->u.b_conn.transport_eph,
1662 ev->u.b_conn.is_leading);
1663 if (ev->r.retcode || !transport->ep_connect)
1664 break;
1665
1666 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
1667 if (ep) {
1668 ep->conn = conn;
1669
1670 mutex_lock(&conn->ep_mutex);
1671 conn->ep = ep;
1672 mutex_unlock(&conn->ep_mutex);
1673 } else
1674 iscsi_cls_conn_printk(KERN_ERR, conn,
1675 "Could not set ep conn "
1676 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001677 break;
1678 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001679 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001680 break;
1681 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001682 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001683 if (conn)
1684 ev->r.retcode = transport->start_conn(conn);
1685 else
1686 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001687 break;
1688 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001689 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001690 if (conn)
1691 transport->stop_conn(conn, ev->u.stop_conn.flag);
1692 else
1693 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001694 break;
1695 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001696 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001697 if (conn)
1698 ev->r.retcode = transport->send_pdu(conn,
1699 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1700 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1701 ev->u.send_pdu.data_size);
1702 else
1703 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001704 break;
1705 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001706 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001707 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001708 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1709 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1710 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001711 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001712 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1713 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001714 case ISCSI_UEVENT_TGT_DSCVR:
1715 err = iscsi_tgt_dscvr(transport, ev);
1716 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001717 case ISCSI_UEVENT_SET_HOST_PARAM:
1718 err = iscsi_set_host_param(transport, ev);
1719 break;
Michael Chan43514772009-06-08 18:14:41 -07001720 case ISCSI_UEVENT_PATH_UPDATE:
1721 err = iscsi_set_path(transport, ev);
1722 break;
Mike Christie56c155b2011-07-25 13:48:37 -05001723 case ISCSI_UEVENT_SET_IFACE_PARAMS:
1724 err = iscsi_set_iface_params(transport, ev);
1725 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001726 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001727 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001728 break;
1729 }
1730
Mike Christie7b7232f2006-02-01 21:06:49 -06001731 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001732 return err;
1733}
1734
Mike Christieb5c7a122006-04-06 21:13:33 -05001735/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001736 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1737 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001738 */
Alex Aizman0896b752005-08-04 19:33:07 -07001739static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001740iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001741{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001742 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001743 while (skb->len >= NLMSG_SPACE(0)) {
1744 int err;
1745 uint32_t rlen;
1746 struct nlmsghdr *nlh;
1747 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07001748 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001749
1750 nlh = nlmsg_hdr(skb);
1751 if (nlh->nlmsg_len < sizeof(*nlh) ||
1752 skb->len < nlh->nlmsg_len) {
1753 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001754 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001755
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001756 ev = NLMSG_DATA(nlh);
1757 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1758 if (rlen > skb->len)
1759 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001760
Michael Chan43514772009-06-08 18:14:41 -07001761 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001762 if (err) {
1763 ev->type = ISCSI_KEVENT_IF_ERROR;
1764 ev->iferror = err;
1765 }
1766 do {
1767 /*
1768 * special case for GET_STATS:
1769 * on success - sending reply and stats from
1770 * inside of if_recv_msg(),
1771 * on error - fall through.
1772 */
1773 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001774 break;
Michael Chan43514772009-06-08 18:14:41 -07001775 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001776 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1777 } while (err < 0 && err != -ECONNREFUSED);
1778 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001779 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001780 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001781}
1782
Mike Christiefd7255f2006-04-06 21:13:36 -05001783#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001784struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001785 __ATTR(_name,_mode,_show,_store)
1786
Alex Aizman0896b752005-08-04 19:33:07 -07001787/*
1788 * iSCSI connection attrs
1789 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001790#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001792show_conn_param_##param(struct device *dev, \
1793 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001794{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001795 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001796 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001797 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001798}
1799
Mike Christiea54a52c2006-06-28 12:00:23 -05001800#define iscsi_conn_attr(field, param) \
1801 iscsi_conn_attr_show(param) \
1802static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001803 NULL);
1804
Mike Christiea54a52c2006-06-28 12:00:23 -05001805iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1806iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1807iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1808iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1809iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1810iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1811iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05001812iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1813iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001814iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1815iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Mike Christie289324b2011-02-16 15:04:37 -06001817#define iscsi_conn_ep_attr_show(param) \
1818static ssize_t show_conn_ep_param_##param(struct device *dev, \
1819 struct device_attribute *attr,\
1820 char *buf) \
1821{ \
1822 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
1823 struct iscsi_transport *t = conn->transport; \
1824 struct iscsi_endpoint *ep; \
1825 ssize_t rc; \
1826 \
1827 /* \
1828 * Need to make sure ep_disconnect does not free the LLD's \
1829 * interconnect resources while we are trying to read them. \
1830 */ \
1831 mutex_lock(&conn->ep_mutex); \
1832 ep = conn->ep; \
1833 if (!ep && t->ep_connect) { \
1834 mutex_unlock(&conn->ep_mutex); \
1835 return -ENOTCONN; \
1836 } \
1837 \
1838 if (ep) \
1839 rc = t->get_ep_param(ep, param, buf); \
1840 else \
1841 rc = t->get_conn_param(conn, param, buf); \
1842 mutex_unlock(&conn->ep_mutex); \
1843 return rc; \
1844}
1845
1846#define iscsi_conn_ep_attr(field, param) \
1847 iscsi_conn_ep_attr_show(param) \
1848static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
1849 show_conn_ep_param_##param, NULL);
1850
1851iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1852iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/*
Alex Aizman0896b752005-08-04 19:33:07 -07001855 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 */
Mike Christieb2c64162007-05-30 12:57:16 -05001857#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001859show_session_param_##param(struct device *dev, \
1860 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001861{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001862 struct iscsi_cls_session *session = \
1863 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001864 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001865 \
1866 if (perm && !capable(CAP_SYS_ADMIN)) \
1867 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001868 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Mike Christieb2c64162007-05-30 12:57:16 -05001871#define iscsi_session_attr(field, param, perm) \
1872 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001873static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001874 NULL);
1875
Mike Christieb2c64162007-05-30 12:57:16 -05001876iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1877iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1878iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1879iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1880iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1881iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1882iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1883iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1884iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1885iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1886iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1887iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1888iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1889iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001890iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1891iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1892iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06001893iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05001894iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05301895iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
1896iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05001897
Mike Christie6eabafb2008-01-31 13:36:43 -06001898static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001899show_priv_session_state(struct device *dev, struct device_attribute *attr,
1900 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06001901{
Tony Jonesee959b02008-02-22 00:13:36 +01001902 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06001903 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1904}
1905static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1906 NULL);
1907
Mike Christiefd7255f2006-04-06 21:13:36 -05001908#define iscsi_priv_session_attr_show(field, format) \
1909static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001910show_priv_session_##field(struct device *dev, \
1911 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001912{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001913 struct iscsi_cls_session *session = \
1914 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05301915 if (session->field == -1) \
1916 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001917 return sprintf(buf, format"\n", session->field); \
1918}
1919
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05301920#define iscsi_priv_session_attr_store(field) \
1921static ssize_t \
1922store_priv_session_##field(struct device *dev, \
1923 struct device_attribute *attr, \
1924 const char *buf, size_t count) \
1925{ \
1926 int val; \
1927 char *cp; \
1928 struct iscsi_cls_session *session = \
1929 iscsi_dev_to_session(dev->parent); \
1930 if ((session->state == ISCSI_SESSION_FREE) || \
1931 (session->state == ISCSI_SESSION_FAILED)) \
1932 return -EBUSY; \
1933 if (strncmp(buf, "off", 3) == 0) \
1934 session->field = -1; \
1935 else { \
1936 val = simple_strtoul(buf, &cp, 0); \
1937 if (*cp != '\0' && *cp != '\n') \
1938 return -EINVAL; \
1939 session->field = val; \
1940 } \
1941 return count; \
1942}
1943
1944#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001945 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05301946 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03001947static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05301948 show_priv_session_##field, \
1949 store_priv_session_##field)
1950iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001951
Mike Christie1819dc82007-05-30 12:57:08 -05001952/*
1953 * iSCSI host attrs
1954 */
1955#define iscsi_host_attr_show(param) \
1956static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001957show_host_param_##param(struct device *dev, \
1958 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05001959{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001960 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05001961 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1962 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1963}
1964
1965#define iscsi_host_attr(field, param) \
1966 iscsi_host_attr_show(param) \
1967static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1968 NULL);
1969
Mike Christied8196ed2007-05-30 12:57:25 -05001970iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001971iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001972iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001973iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001974
Mike Christiefd7255f2006-04-06 21:13:36 -05001975#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1976do { \
Tony Jonesee959b02008-02-22 00:13:36 +01001977 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001978 count++; \
1979} while (0)
1980
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05301981#define SETUP_PRIV_SESSION_RW_ATTR(field) \
1982do { \
1983 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
1984 count++; \
1985} while (0)
Mike Christiea54a52c2006-06-28 12:00:23 -05001986
Mike Christiefd7255f2006-04-06 21:13:36 -05001987#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1988do { \
1989 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001990 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001992 } \
1993} while (0)
1994
Mike Christiefd7255f2006-04-06 21:13:36 -05001995#define SETUP_CONN_RD_ATTR(field, param_flag) \
1996do { \
1997 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001998 priv->conn_attrs[count] = &dev_attr_conn_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001999 count++; \
2000 } \
2001} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Mike Christie1819dc82007-05-30 12:57:08 -05002003#define SETUP_HOST_RD_ATTR(field, param_flag) \
2004do { \
2005 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01002006 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05002007 count++; \
2008 } \
2009} while (0)
2010
Alex Aizman0896b752005-08-04 19:33:07 -07002011static int iscsi_session_match(struct attribute_container *cont,
2012 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Mike Christie7b8631b2006-01-13 18:05:50 -06002014 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002016 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Alex Aizman0896b752005-08-04 19:33:07 -07002018 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return 0;
2020
Mike Christie7b8631b2006-01-13 18:05:50 -06002021 session = iscsi_dev_to_session(dev);
2022 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002023 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return 0;
2025
Alex Aizman0896b752005-08-04 19:33:07 -07002026 priv = to_iscsi_internal(shost->transportt);
2027 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2028 return 0;
2029
2030 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Alex Aizman0896b752005-08-04 19:33:07 -07002033static int iscsi_conn_match(struct attribute_container *cont,
2034 struct device *dev)
2035{
Mike Christie7b8631b2006-01-13 18:05:50 -06002036 struct iscsi_cls_session *session;
2037 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002038 struct Scsi_Host *shost;
2039 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Alex Aizman0896b752005-08-04 19:33:07 -07002041 if (!iscsi_is_conn_dev(dev))
2042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Mike Christie7b8631b2006-01-13 18:05:50 -06002044 conn = iscsi_dev_to_conn(dev);
2045 session = iscsi_dev_to_session(conn->dev.parent);
2046 shost = iscsi_session_to_shost(session);
2047
Alex Aizman0896b752005-08-04 19:33:07 -07002048 if (!shost->transportt)
2049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Alex Aizman0896b752005-08-04 19:33:07 -07002051 priv = to_iscsi_internal(shost->transportt);
2052 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2053 return 0;
2054
2055 return &priv->conn_cont.ac == cont;
2056}
2057
Mike Christie30a6c652006-04-06 21:13:39 -05002058static int iscsi_host_match(struct attribute_container *cont,
2059 struct device *dev)
2060{
2061 struct Scsi_Host *shost;
2062 struct iscsi_internal *priv;
2063
2064 if (!scsi_is_host_device(dev))
2065 return 0;
2066
2067 shost = dev_to_shost(dev);
2068 if (!shost->transportt ||
2069 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2070 return 0;
2071
2072 priv = to_iscsi_internal(shost->transportt);
2073 return &priv->t.host_attrs.ac == cont;
2074}
2075
Mike Christie7b8631b2006-01-13 18:05:50 -06002076struct scsi_transport_template *
2077iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002078{
2079 struct iscsi_internal *priv;
2080 unsigned long flags;
2081 int count = 0, err;
2082
2083 BUG_ON(!tt);
2084
2085 priv = iscsi_if_transport_lookup(tt);
2086 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002087 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002088
Jes Sorensen24669f752006-01-16 10:31:18 -05002089 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002090 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002091 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002092 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002093 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002094 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002095 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002096
Tony Jonesee959b02008-02-22 00:13:36 +01002097 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002098 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002099 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002100 if (err)
2101 goto free_priv;
2102
Tony Jonesee959b02008-02-22 00:13:36 +01002103 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002104 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002105 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002106
Mike Christie30a6c652006-04-06 21:13:39 -05002107 /* host parameters */
2108 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
2109 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2110 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002111 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002112 transport_container_register(&priv->t.host_attrs);
2113
Mike Christied8196ed2007-05-30 12:57:25 -05002114 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05002115 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05002116 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002117 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002118 BUG_ON(count > ISCSI_HOST_ATTRS);
2119 priv->host_attrs[count] = NULL;
2120 count = 0;
2121
Alex Aizman0896b752005-08-04 19:33:07 -07002122 /* connection parameters */
2123 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
2124 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2125 priv->conn_cont.ac.match = iscsi_conn_match;
2126 transport_container_register(&priv->conn_cont);
2127
Mike Christiefd7255f2006-04-06 21:13:36 -05002128 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
2129 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
2130 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
2131 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
2132 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
2133 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
2134 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
2135 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05002136 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05002137 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
2138 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06002139 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
2140 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07002141
2142 BUG_ON(count > ISCSI_CONN_ATTRS);
2143 priv->conn_attrs[count] = NULL;
2144 count = 0;
2145
2146 /* session parameters */
2147 priv->session_cont.ac.attrs = &priv->session_attrs[0];
2148 priv->session_cont.ac.class = &iscsi_session_class.class;
2149 priv->session_cont.ac.match = iscsi_session_match;
2150 transport_container_register(&priv->session_cont);
2151
Mike Christiefd7255f2006-04-06 21:13:36 -05002152 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
2153 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
2154 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
2155 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
2156 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
2157 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
2158 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
2159 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05002160 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
2161 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05002162 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
2163 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
2164 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
2165 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06002166 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002167 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
2168 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002169 SETUP_SESSION_RD_ATTR(tgt_reset_tmo,ISCSI_TGT_RESET_TMO);
Mike Christie88dfd342008-05-21 15:54:16 -05002170 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
2171 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302172 SETUP_SESSION_RD_ATTR(targetalias, ISCSI_TARGET_ALIAS);
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302173 SETUP_PRIV_SESSION_RW_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06002174 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07002177 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Alex Aizman0896b752005-08-04 19:33:07 -07002179 spin_lock_irqsave(&iscsi_transport_lock, flags);
2180 list_add(&priv->list, &iscsi_transports);
2181 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Alex Aizman0896b752005-08-04 19:33:07 -07002183 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002184 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Tony Jonesee959b02008-02-22 00:13:36 +01002186unregister_dev:
2187 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002188 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002189free_priv:
2190 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002191 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
Alex Aizman0896b752005-08-04 19:33:07 -07002193EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Alex Aizman0896b752005-08-04 19:33:07 -07002195int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Alex Aizman0896b752005-08-04 19:33:07 -07002197 struct iscsi_internal *priv;
2198 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Alex Aizman0896b752005-08-04 19:33:07 -07002200 BUG_ON(!tt);
2201
Arjan van de Ven0b950672006-01-11 13:16:10 +01002202 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002203
2204 priv = iscsi_if_transport_lookup(tt);
2205 BUG_ON (!priv);
2206
Alex Aizman0896b752005-08-04 19:33:07 -07002207 spin_lock_irqsave(&iscsi_transport_lock, flags);
2208 list_del(&priv->list);
2209 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2210
2211 transport_container_unregister(&priv->conn_cont);
2212 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002213 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002214
Tony Jonesee959b02008-02-22 00:13:36 +01002215 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2216 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002217 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002218
2219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223static __init int iscsi_transport_init(void)
2224{
Alex Aizman0896b752005-08-04 19:33:07 -07002225 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Meelis Roos09492602006-12-17 12:10:26 -06002227 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002228 ISCSI_TRANSPORT_VERSION);
2229
Mike Christie41be1442007-02-28 17:32:18 -06002230 atomic_set(&iscsi_session_nr, 0);
2231
Alex Aizman0896b752005-08-04 19:33:07 -07002232 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (err)
2234 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002235
Mike Christied82ff9be2008-05-21 15:54:13 -05002236 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002237 if (err)
2238 goto unregister_transport_class;
2239
Mike Christied82ff9be2008-05-21 15:54:13 -05002240 err = transport_class_register(&iscsi_host_class);
2241 if (err)
2242 goto unregister_endpoint_class;
2243
Mike Christie30a6c652006-04-06 21:13:39 -05002244 err = transport_class_register(&iscsi_connection_class);
2245 if (err)
2246 goto unregister_host_class;
2247
Alex Aizman0896b752005-08-04 19:33:07 -07002248 err = transport_class_register(&iscsi_session_class);
2249 if (err)
2250 goto unregister_conn_class;
2251
Mike Christied82ff9be2008-05-21 15:54:13 -05002252 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2253 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002254 if (!nls) {
2255 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002256 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002257 }
2258
Mike Christied8bf5412007-12-13 12:43:27 -06002259 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2260 if (!iscsi_eh_timer_workq)
2261 goto release_nls;
2262
Mike Christie43a145a2006-10-16 18:09:38 -04002263 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002264
Mike Christied8bf5412007-12-13 12:43:27 -06002265release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002266 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002267unregister_session_class:
2268 transport_class_unregister(&iscsi_session_class);
2269unregister_conn_class:
2270 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002271unregister_host_class:
2272 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002273unregister_endpoint_class:
2274 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002275unregister_transport_class:
2276 class_unregister(&iscsi_transport_class);
2277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280static void __exit iscsi_transport_exit(void)
2281{
Mike Christied8bf5412007-12-13 12:43:27 -06002282 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002283 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002284 transport_class_unregister(&iscsi_connection_class);
2285 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002286 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002287 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002288 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
2291module_init(iscsi_transport_init);
2292module_exit(iscsi_transport_exit);
2293
Alex Aizman0896b752005-08-04 19:33:07 -07002294MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2295 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2296 "Alex Aizman <itn780@yahoo.com>");
2297MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002299MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002300MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);