blob: 4a803ebaf5081b345f964ea61964270b5af29576 [file] [log] [blame]
Alex Aizman0896b752005-08-04 19:33:07 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07005 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23#include <linux/module.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010024#include <linux/mutex.h>
Alex Aizman0896b752005-08-04 19:33:07 -070025#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <scsi/scsi.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_device.h>
29#include <scsi/scsi_transport.h>
30#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070031#include <scsi/iscsi_if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Mike Christie88dfd342008-05-21 15:54:16 -050033#define ISCSI_SESSION_ATTRS 21
Mike Christiec238c3b2008-01-31 13:36:51 -060034#define ISCSI_CONN_ATTRS 13
Mike Christied8196ed2007-05-30 12:57:25 -050035#define ISCSI_HOST_ATTRS 4
Mike Christied82ff9be2008-05-21 15:54:13 -050036
Mike Christie4c2133c2008-06-16 10:11:34 -050037#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39struct iscsi_internal {
Mike Christie790f39a2006-05-18 20:31:39 -050040 int daemon_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070042 struct iscsi_transport *iscsi_transport;
43 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010044 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050045
Tony Jonesee959b02008-02-22 00:13:36 +010046 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070047 struct transport_container conn_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010048 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070049 struct transport_container session_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010050 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Mike Christie41be1442007-02-28 17:32:18 -060053static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060054static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050055
Alex Aizman0896b752005-08-04 19:33:07 -070056/*
57 * list of registered transports and lock that must
58 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010059 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070060 */
61static LIST_HEAD(iscsi_transports);
62static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alex Aizman0896b752005-08-04 19:33:07 -070064#define to_iscsi_internal(tmpl) \
65 container_of(tmpl, struct iscsi_internal, t)
66
Tony Jonesee959b02008-02-22 00:13:36 +010067#define dev_to_iscsi_internal(_dev) \
68 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070069
Tony Jonesee959b02008-02-22 00:13:36 +010070static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -070071{
Tony Jonesee959b02008-02-22 00:13:36 +010072 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -070073 kfree(priv);
74}
75
76/*
77 * iscsi_transport_class represents the iscsi_transports that are
78 * registered.
79 */
80static struct class iscsi_transport_class = {
81 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +010082 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -070083};
84
85static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +010086show_transport_handle(struct device *dev, struct device_attribute *attr,
87 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -070088{
Tony Jonesee959b02008-02-22 00:13:36 +010089 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -050090 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070091}
Tony Jonesee959b02008-02-22 00:13:36 +010092static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -070093
94#define show_transport_attr(name, format) \
95static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +010096show_transport_##name(struct device *dev, \
97 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -070098{ \
Tony Jonesee959b02008-02-22 00:13:36 +010099 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700100 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
101} \
Tony Jonesee959b02008-02-22 00:13:36 +0100102static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700103
104show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700105
106static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100107 &dev_attr_handle.attr,
108 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700109 NULL,
110};
111
112static struct attribute_group iscsi_transport_group = {
113 .attrs = iscsi_transport_attrs,
114};
115
Mike Christied82ff9be2008-05-21 15:54:13 -0500116/*
117 * iSCSI endpoint attrs
118 */
119#define iscsi_dev_to_endpoint(_dev) \
120 container_of(_dev, struct iscsi_endpoint, dev)
121
122#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
123struct device_attribute dev_attr_##_prefix##_##_name = \
124 __ATTR(_name,_mode,_show,_store)
125
126static void iscsi_endpoint_release(struct device *dev)
127{
128 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
129 kfree(ep);
130}
131
132static struct class iscsi_endpoint_class = {
133 .name = "iscsi_endpoint",
134 .dev_release = iscsi_endpoint_release,
135};
136
137static ssize_t
138show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
139{
140 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500141 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500142}
143static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
144
145static struct attribute *iscsi_endpoint_attrs[] = {
146 &dev_attr_ep_handle.attr,
147 NULL,
148};
149
150static struct attribute_group iscsi_endpoint_group = {
151 .attrs = iscsi_endpoint_attrs,
152};
153
154#define ISCSI_MAX_EPID -1
155
156static int iscsi_match_epid(struct device *dev, void *data)
157{
158 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500159 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500160
161 return *epid == ep->id;
162}
163
164struct iscsi_endpoint *
165iscsi_create_endpoint(int dd_size)
166{
167 struct device *dev;
168 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500169 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500170 int err;
171
172 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400173 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500174 iscsi_match_epid);
175 if (!dev)
176 break;
177 }
178 if (id == ISCSI_MAX_EPID) {
179 printk(KERN_ERR "Too many connections. Max supported %u\n",
180 ISCSI_MAX_EPID - 1);
181 return NULL;
182 }
183
184 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
185 if (!ep)
186 return NULL;
187
188 ep->id = id;
189 ep->dev.class = &iscsi_endpoint_class;
Mike Christie21536062008-09-24 11:46:11 -0500190 snprintf(ep->dev.bus_id, BUS_ID_SIZE, "ep-%llu",
191 (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500192 err = device_register(&ep->dev);
193 if (err)
194 goto free_ep;
195
196 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
197 if (err)
198 goto unregister_dev;
199
200 if (dd_size)
201 ep->dd_data = &ep[1];
202 return ep;
203
204unregister_dev:
205 device_unregister(&ep->dev);
206 return NULL;
207
208free_ep:
209 kfree(ep);
210 return NULL;
211}
212EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
213
214void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
215{
216 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
217 device_unregister(&ep->dev);
218}
219EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
220
221struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
222{
Mike Christief80f8682008-06-16 10:11:35 -0500223 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500224 struct device *dev;
225
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400226 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500227 iscsi_match_epid);
228 if (!dev)
229 return NULL;
230
Mike Christief80f8682008-06-16 10:11:35 -0500231 ep = iscsi_dev_to_endpoint(dev);
232 /*
233 * we can drop this now because the interface will prevent
234 * removals and lookups from racing.
235 */
236 put_device(dev);
237 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500238}
239EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
240
Mike Christie30a6c652006-04-06 21:13:39 -0500241static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100242 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500243{
244 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500245 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500246
247 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600248 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500249 mutex_init(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600250
Kay Sieversaab0de22008-05-02 06:02:41 +0200251 snprintf(ihost->scan_workq_name, sizeof(ihost->scan_workq_name),
252 "iscsi_scan_%d", shost->host_no);
Mike Christiebd976f62008-01-31 13:36:46 -0600253 ihost->scan_workq = create_singlethread_workqueue(
254 ihost->scan_workq_name);
255 if (!ihost->scan_workq)
Mike Christie26974782007-12-13 12:43:29 -0600256 return -ENOMEM;
257 return 0;
258}
259
260static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100261 struct device *cdev)
Mike Christie26974782007-12-13 12:43:29 -0600262{
263 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500264 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600265
Mike Christiebd976f62008-01-31 13:36:46 -0600266 destroy_workqueue(ihost->scan_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500267 return 0;
268}
269
270static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
271 "iscsi_host",
272 iscsi_setup_host,
Mike Christie26974782007-12-13 12:43:29 -0600273 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500274 NULL);
275
Alex Aizman0896b752005-08-04 19:33:07 -0700276static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
277 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 NULL,
279 NULL,
280 NULL);
281
Alex Aizman0896b752005-08-04 19:33:07 -0700282static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
283 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 NULL,
285 NULL,
286 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700287
288static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100289static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700290
Mike Christie7b7232f2006-02-01 21:06:49 -0600291static LIST_HEAD(sesslist);
292static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700293static LIST_HEAD(connlist);
294static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Mike Christieb5c7a122006-04-06 21:13:33 -0500296static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
297{
298 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
299 return sess->sid;
300}
301
302/*
303 * Returns the matching session to a given sid
304 */
305static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600306{
307 unsigned long flags;
308 struct iscsi_cls_session *sess;
309
310 spin_lock_irqsave(&sesslock, flags);
311 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500312 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600313 spin_unlock_irqrestore(&sesslock, flags);
314 return sess;
315 }
316 }
317 spin_unlock_irqrestore(&sesslock, flags);
318 return NULL;
319}
320
Mike Christieb5c7a122006-04-06 21:13:33 -0500321/*
322 * Returns the matching connection to a given sid / cid tuple
323 */
324static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600325{
326 unsigned long flags;
327 struct iscsi_cls_conn *conn;
328
329 spin_lock_irqsave(&connlock, flags);
330 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500331 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600332 spin_unlock_irqrestore(&connlock, flags);
333 return conn;
334 }
335 }
336 spin_unlock_irqrestore(&connlock, flags);
337 return NULL;
338}
339
Mike Christie7b8631b2006-01-13 18:05:50 -0600340/*
341 * The following functions can be used by LLDs that allocate
342 * their own scsi_hosts or by software iscsi LLDs
343 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600344static struct {
345 int value;
346 char *name;
347} iscsi_session_state_names[] = {
348 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
349 { ISCSI_SESSION_FAILED, "FAILED" },
350 { ISCSI_SESSION_FREE, "FREE" },
351};
352
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200353static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600354{
355 int i;
356 char *name = NULL;
357
358 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
359 if (iscsi_session_state_names[i].value == state) {
360 name = iscsi_session_state_names[i].name;
361 break;
362 }
363 }
364 return name;
365}
366
367int iscsi_session_chkready(struct iscsi_cls_session *session)
368{
369 unsigned long flags;
370 int err;
371
372 spin_lock_irqsave(&session->lock, flags);
373 switch (session->state) {
374 case ISCSI_SESSION_LOGGED_IN:
375 err = 0;
376 break;
377 case ISCSI_SESSION_FAILED:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500378 err = DID_TRANSPORT_DISRUPTED << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600379 break;
380 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500381 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600382 break;
383 default:
384 err = DID_NO_CONNECT << 16;
385 break;
386 }
387 spin_unlock_irqrestore(&session->lock, flags);
388 return err;
389}
390EXPORT_SYMBOL_GPL(iscsi_session_chkready);
391
Mike Christie7b8631b2006-01-13 18:05:50 -0600392static void iscsi_session_release(struct device *dev)
393{
394 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600395 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Mike Christie7b8631b2006-01-13 18:05:50 -0600397 shost = iscsi_session_to_shost(session);
398 scsi_host_put(shost);
399 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Mike Christie7b8631b2006-01-13 18:05:50 -0600402static int iscsi_is_session_dev(const struct device *dev)
403{
404 return dev->release == iscsi_session_release;
405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Mike Christiea4804cd2008-05-21 15:54:00 -0500407static int iscsi_iter_session_fn(struct device *dev, void *data)
408{
409 void (* fn) (struct iscsi_cls_session *) = data;
410
411 if (!iscsi_is_session_dev(dev))
412 return 0;
413 fn(iscsi_dev_to_session(dev));
414 return 0;
415}
416
417void iscsi_host_for_each_session(struct Scsi_Host *shost,
418 void (*fn)(struct iscsi_cls_session *))
419{
420 device_for_each_child(&shost->shost_gendev, fn,
421 iscsi_iter_session_fn);
422}
423EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
424
Mike Christie8aae18a2008-01-31 13:36:48 -0600425/**
426 * iscsi_scan_finished - helper to report when running scans are done
427 * @shost: scsi host
428 * @time: scan run time
429 *
430 * This function can be used by drives like qla4xxx to report to the scsi
431 * layer when the scans it kicked off at module load time are done.
432 */
433int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
434{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500435 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600436 /*
437 * qla4xxx will have kicked off some session unblocks before calling
438 * scsi_scan_host, so just wait for them to complete.
439 */
440 return !atomic_read(&ihost->nr_scans);
441}
442EXPORT_SYMBOL_GPL(iscsi_scan_finished);
443
Mike Christie79706342008-05-21 15:54:12 -0500444struct iscsi_scan_data {
445 unsigned int channel;
446 unsigned int id;
447 unsigned int lun;
448};
449
450static int iscsi_user_scan_session(struct device *dev, void *data)
451{
452 struct iscsi_scan_data *scan_data = data;
453 struct iscsi_cls_session *session;
454 struct Scsi_Host *shost;
455 struct iscsi_cls_host *ihost;
456 unsigned long flags;
457 unsigned int id;
458
459 if (!iscsi_is_session_dev(dev))
460 return 0;
461
462 session = iscsi_dev_to_session(dev);
463 shost = iscsi_session_to_shost(session);
464 ihost = shost->shost_data;
465
466 mutex_lock(&ihost->mutex);
467 spin_lock_irqsave(&session->lock, flags);
468 if (session->state != ISCSI_SESSION_LOGGED_IN) {
469 spin_unlock_irqrestore(&session->lock, flags);
470 mutex_unlock(&ihost->mutex);
471 return 0;
472 }
473 id = session->target_id;
474 spin_unlock_irqrestore(&session->lock, flags);
475
476 if (id != ISCSI_MAX_TARGET) {
477 if ((scan_data->channel == SCAN_WILD_CARD ||
478 scan_data->channel == 0) &&
479 (scan_data->id == SCAN_WILD_CARD ||
480 scan_data->id == id))
481 scsi_scan_target(&session->dev, 0, id,
482 scan_data->lun, 1);
483 }
484 mutex_unlock(&ihost->mutex);
485 return 0;
486}
487
Mike Christie30a6c652006-04-06 21:13:39 -0500488static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
489 uint id, uint lun)
490{
Mike Christie79706342008-05-21 15:54:12 -0500491 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500492
Mike Christie79706342008-05-21 15:54:12 -0500493 scan_data.channel = channel;
494 scan_data.id = id;
495 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500496
Mike Christie79706342008-05-21 15:54:12 -0500497 return device_for_each_child(&shost->shost_gendev, &scan_data,
498 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500499}
500
Mike Christiebd976f62008-01-31 13:36:46 -0600501static void iscsi_scan_session(struct work_struct *work)
502{
503 struct iscsi_cls_session *session =
504 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600505 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500506 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500507 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600508
Mike Christie79706342008-05-21 15:54:12 -0500509 scan_data.channel = 0;
510 scan_data.id = SCAN_WILD_CARD;
511 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600512
Mike Christie79706342008-05-21 15:54:12 -0500513 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600514 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600515}
516
David Howellsc4028952006-11-22 14:57:56 +0000517static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500518{
David Howellsc4028952006-11-22 14:57:56 +0000519 struct iscsi_cls_session *session =
520 container_of(work, struct iscsi_cls_session,
521 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600522 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500523
Mike Christie322d7392008-01-31 13:36:52 -0600524 iscsi_cls_session_printk(KERN_INFO, session,
525 "session recovery timed out after %d secs\n",
526 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500527
Mike Christie6eabafb2008-01-31 13:36:43 -0600528 spin_lock_irqsave(&session->lock, flags);
529 switch (session->state) {
530 case ISCSI_SESSION_FAILED:
531 session->state = ISCSI_SESSION_FREE;
532 break;
533 case ISCSI_SESSION_LOGGED_IN:
534 case ISCSI_SESSION_FREE:
535 /* we raced with the unblock's flush */
536 spin_unlock_irqrestore(&session->lock, flags);
537 return;
538 }
539 spin_unlock_irqrestore(&session->lock, flags);
540
Mike Christie30a6c652006-04-06 21:13:39 -0500541 if (session->transport->session_recovery_timedout)
542 session->transport->session_recovery_timedout(session);
543
544 scsi_target_unblock(&session->dev);
545}
546
Mike Christie45ab33b2008-03-04 13:26:55 -0600547static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500548{
Mike Christie45ab33b2008-03-04 13:26:55 -0600549 struct iscsi_cls_session *session =
550 container_of(work, struct iscsi_cls_session,
551 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600552 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500553 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600554 unsigned long flags;
555
Mike Christie45ab33b2008-03-04 13:26:55 -0600556 /*
557 * The recovery and unblock work get run from the same workqueue,
558 * so try to cancel it if it was going to run after this unblock.
559 */
560 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600561 spin_lock_irqsave(&session->lock, flags);
562 session->state = ISCSI_SESSION_LOGGED_IN;
563 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600564 /* start IO */
565 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600566 /*
567 * Only do kernel scanning if the driver is properly hooked into
568 * the async scanning code (drivers like iscsi_tcp do login and
569 * scanning from userspace).
570 */
571 if (shost->hostt->scan_finished) {
572 if (queue_work(ihost->scan_workq, &session->scan_work))
573 atomic_inc(&ihost->nr_scans);
574 }
Mike Christie6eabafb2008-01-31 13:36:43 -0600575}
Mike Christie45ab33b2008-03-04 13:26:55 -0600576
577/**
578 * iscsi_unblock_session - set a session as logged in and start IO.
579 * @session: iscsi session
580 *
581 * Mark a session as ready to accept IO.
582 */
583void iscsi_unblock_session(struct iscsi_cls_session *session)
584{
585 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
586 /*
587 * make sure all the events have completed before tell the driver
588 * it is safe
589 */
590 flush_workqueue(iscsi_eh_timer_workq);
591}
Mike Christie30a6c652006-04-06 21:13:39 -0500592EXPORT_SYMBOL_GPL(iscsi_unblock_session);
593
Mike Christie45ab33b2008-03-04 13:26:55 -0600594static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500595{
Mike Christie45ab33b2008-03-04 13:26:55 -0600596 struct iscsi_cls_session *session =
597 container_of(work, struct iscsi_cls_session,
598 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600599 unsigned long flags;
600
601 spin_lock_irqsave(&session->lock, flags);
602 session->state = ISCSI_SESSION_FAILED;
603 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500604 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600605 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
606 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500607}
Mike Christie45ab33b2008-03-04 13:26:55 -0600608
609void iscsi_block_session(struct iscsi_cls_session *session)
610{
611 queue_work(iscsi_eh_timer_workq, &session->block_work);
612}
Mike Christie30a6c652006-04-06 21:13:39 -0500613EXPORT_SYMBOL_GPL(iscsi_block_session);
614
Mike Christie26974782007-12-13 12:43:29 -0600615static void __iscsi_unbind_session(struct work_struct *work)
616{
617 struct iscsi_cls_session *session =
618 container_of(work, struct iscsi_cls_session,
619 unbind_work);
620 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500621 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500622 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600623
624 /* Prevent new scans and make sure scanning is not in progress */
625 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500626 spin_lock_irqsave(&session->lock, flags);
627 if (session->target_id == ISCSI_MAX_TARGET) {
628 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600629 mutex_unlock(&ihost->mutex);
630 return;
631 }
Mike Christie79706342008-05-21 15:54:12 -0500632 session->target_id = ISCSI_MAX_TARGET;
633 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600634 mutex_unlock(&ihost->mutex);
635
636 scsi_remove_target(&session->dev);
637 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
638}
639
640static int iscsi_unbind_session(struct iscsi_cls_session *session)
641{
642 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500643 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600644
Mike Christiebd976f62008-01-31 13:36:46 -0600645 return queue_work(ihost->scan_workq, &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -0600646}
647
Mike Christie7b8631b2006-01-13 18:05:50 -0600648struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500649iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
650 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600651{
652 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Mike Christie5d91e202008-05-21 15:54:01 -0500654 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500655 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600656 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500657 return NULL;
658
Mike Christie7b8631b2006-01-13 18:05:50 -0600659 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500660 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600661 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000662 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500663 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600664 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
665 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600666 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600667 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600668 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600669
Mike Christie6a8a0d32006-06-28 12:00:31 -0500670 /* this is released in the dev's release function */
671 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500672 session->dev.parent = &shost->shost_gendev;
673 session->dev.release = iscsi_session_release;
674 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500675 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500676 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500677 return session;
678}
679EXPORT_SYMBOL_GPL(iscsi_alloc_session);
680
Mike Christie79706342008-05-21 15:54:12 -0500681static int iscsi_get_next_target_id(struct device *dev, void *data)
682{
683 struct iscsi_cls_session *session;
684 unsigned long flags;
685 int err = 0;
686
687 if (!iscsi_is_session_dev(dev))
688 return 0;
689
690 session = iscsi_dev_to_session(dev);
691 spin_lock_irqsave(&session->lock, flags);
692 if (*((unsigned int *) data) == session->target_id)
693 err = -EEXIST;
694 spin_unlock_irqrestore(&session->lock, flags);
695 return err;
696}
697
Mike Christie6a8a0d32006-06-28 12:00:31 -0500698int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500699{
700 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500701 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600702 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500703 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500704 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500705
Mike Christie30a6c652006-04-06 21:13:39 -0500706 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600707 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500708
709 if (id == ISCSI_MAX_TARGET) {
710 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
711 err = device_for_each_child(&shost->shost_gendev, &id,
712 iscsi_get_next_target_id);
713 if (!err)
714 break;
715 }
716
717 if (id == ISCSI_MAX_TARGET) {
718 iscsi_cls_session_printk(KERN_ERR, session,
719 "Too many iscsi targets. Max "
720 "number of targets is %d.\n",
721 ISCSI_MAX_TARGET - 1);
722 goto release_host;
723 }
724 }
725 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500726
Mike Christieb5c7a122006-04-06 21:13:33 -0500727 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
728 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500729 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600730 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600731 iscsi_cls_session_printk(KERN_ERR, session,
732 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500733 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600734 }
735 transport_register_device(&session->dev);
736
Mike Christie26974782007-12-13 12:43:29 -0600737 spin_lock_irqsave(&sesslock, flags);
738 list_add(&session->sess_list, &sesslist);
739 spin_unlock_irqrestore(&sesslock, flags);
740
Mike Christie26974782007-12-13 12:43:29 -0600741 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500742 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500743
Mike Christie8434aa82006-06-28 12:00:30 -0500744release_host:
745 scsi_host_put(shost);
746 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600747}
Mike Christie8434aa82006-06-28 12:00:30 -0500748EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600749
750/**
Mike Christie8434aa82006-06-28 12:00:30 -0500751 * iscsi_create_session - create iscsi class session
752 * @shost: scsi host
753 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500754 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500755 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600756 *
Mike Christie8434aa82006-06-28 12:00:30 -0500757 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500758 */
Mike Christie8434aa82006-06-28 12:00:30 -0500759struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500760iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
761 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500762{
763 struct iscsi_cls_session *session;
764
Mike Christie5d91e202008-05-21 15:54:01 -0500765 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500766 if (!session)
767 return NULL;
768
Mike Christie6a8a0d32006-06-28 12:00:31 -0500769 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500770 iscsi_free_session(session);
771 return NULL;
772 }
773 return session;
774}
775EXPORT_SYMBOL_GPL(iscsi_create_session);
776
Mike Christie26974782007-12-13 12:43:29 -0600777static void iscsi_conn_release(struct device *dev)
778{
779 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
780 struct device *parent = conn->dev.parent;
781
782 kfree(conn);
783 put_device(parent);
784}
785
786static int iscsi_is_conn_dev(const struct device *dev)
787{
788 return dev->release == iscsi_conn_release;
789}
790
791static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
792{
793 if (!iscsi_is_conn_dev(dev))
794 return 0;
795 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
796}
797
Mike Christie8434aa82006-06-28 12:00:30 -0500798void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600799{
Mike Christie30a6c652006-04-06 21:13:39 -0500800 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500801 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600802 unsigned long flags;
803 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500804
Mike Christie26974782007-12-13 12:43:29 -0600805 spin_lock_irqsave(&sesslock, flags);
806 list_del(&session->sess_list);
807 spin_unlock_irqrestore(&sesslock, flags);
808
Mike Christie45ab33b2008-03-04 13:26:55 -0600809 /* make sure there are no blocks/unblocks queued */
810 flush_workqueue(iscsi_eh_timer_workq);
811 /* make sure the timedout callout is not running */
812 if (!cancel_delayed_work(&session->recovery_work))
813 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600814 /*
815 * If we are blocked let commands flow again. The lld or iscsi
816 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -0600817 * We assume that LLD will not be calling block/unblock while
818 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -0600819 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600820 spin_lock_irqsave(&session->lock, flags);
821 session->state = ISCSI_SESSION_FREE;
822 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -0600823
Mike Christie45ab33b2008-03-04 13:26:55 -0600824 scsi_target_unblock(&session->dev);
825 /* flush running scans then delete devices */
Mike Christiebd976f62008-01-31 13:36:46 -0600826 flush_workqueue(ihost->scan_workq);
Mike Christie45ab33b2008-03-04 13:26:55 -0600827 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -0500828
Mike Christie26974782007-12-13 12:43:29 -0600829 /* hw iscsi may not have removed all connections from session */
830 err = device_for_each_child(&session->dev, NULL,
831 iscsi_iter_destroy_conn_fn);
832 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600833 iscsi_cls_session_printk(KERN_ERR, session,
834 "Could not delete all connections "
835 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500836
Mike Christie7b8631b2006-01-13 18:05:50 -0600837 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500838 device_del(&session->dev);
839}
840EXPORT_SYMBOL_GPL(iscsi_remove_session);
841
842void iscsi_free_session(struct iscsi_cls_session *session)
843{
Mike Christie26974782007-12-13 12:43:29 -0600844 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500845 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600846}
Mike Christie8434aa82006-06-28 12:00:30 -0500847EXPORT_SYMBOL_GPL(iscsi_free_session);
848
849/**
850 * iscsi_destroy_session - destroy iscsi session
851 * @session: iscsi_session
852 *
853 * Can be called by a LLD or iscsi_transport. There must not be
854 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500855 */
Mike Christie8434aa82006-06-28 12:00:30 -0500856int iscsi_destroy_session(struct iscsi_cls_session *session)
857{
858 iscsi_remove_session(session);
859 iscsi_free_session(session);
860 return 0;
861}
Mike Christie7b8631b2006-01-13 18:05:50 -0600862EXPORT_SYMBOL_GPL(iscsi_destroy_session);
863
Mike Christie7b8631b2006-01-13 18:05:50 -0600864/**
865 * iscsi_create_conn - create iscsi class connection
866 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -0500867 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -0600868 * @cid: connection id
869 *
870 * This can be called from a LLD or iscsi_transport. The connection
871 * is child of the session so cid must be unique for all connections
872 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500873 *
874 * Since we do not support MCS, cid will normally be zero. In some cases
875 * for software iscsi we could be trying to preallocate a connection struct
876 * in which case there could be two connection structs and cid would be
877 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500878 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600879struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -0500880iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -0600881{
882 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600883 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600884 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600885 int err;
886
Mike Christie5d91e202008-05-21 15:54:01 -0500887 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600888 if (!conn)
889 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -0500890 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600891 conn->dd_data = &conn[1];
892
893 INIT_LIST_HEAD(&conn->conn_list);
894 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500895 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600896
897 /* this is released in the dev's release function */
898 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400899 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500900
Mike Christie7b8631b2006-01-13 18:05:50 -0600901 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500902 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600903 conn->dev.parent = &session->dev;
904 conn->dev.release = iscsi_conn_release;
905 err = device_register(&conn->dev);
906 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600907 iscsi_cls_session_printk(KERN_ERR, session, "could not "
908 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600909 goto release_parent_ref;
910 }
911 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600912
913 spin_lock_irqsave(&connlock, flags);
914 list_add(&conn->conn_list, &connlist);
915 conn->active = 1;
916 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600917 return conn;
918
919release_parent_ref:
920 put_device(&session->dev);
921free_conn:
922 kfree(conn);
923 return NULL;
924}
925
926EXPORT_SYMBOL_GPL(iscsi_create_conn);
927
928/**
929 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500930 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600931 *
Mike Christie26974782007-12-13 12:43:29 -0600932 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500933 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600934int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
935{
Mike Christie26974782007-12-13 12:43:29 -0600936 unsigned long flags;
937
938 spin_lock_irqsave(&connlock, flags);
939 conn->active = 0;
940 list_del(&conn->conn_list);
941 spin_unlock_irqrestore(&connlock, flags);
942
Mike Christie7b8631b2006-01-13 18:05:50 -0600943 transport_unregister_device(&conn->dev);
944 device_unregister(&conn->dev);
945 return 0;
946}
Mike Christie7b8631b2006-01-13 18:05:50 -0600947EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
948
949/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600950 * iscsi interface functions
951 */
Alex Aizman0896b752005-08-04 19:33:07 -0700952static struct iscsi_internal *
953iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Alex Aizman0896b752005-08-04 19:33:07 -0700955 struct iscsi_internal *priv;
956 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Alex Aizman0896b752005-08-04 19:33:07 -0700958 spin_lock_irqsave(&iscsi_transport_lock, flags);
959 list_for_each_entry(priv, &iscsi_transports, list) {
960 if (tt == priv->iscsi_transport) {
961 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
962 return priv;
963 }
964 }
965 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
966 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Alex Aizman0896b752005-08-04 19:33:07 -0700969static int
Mike Christie43a145a2006-10-16 18:09:38 -0400970iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500971{
Mike Christie53cb8a12006-06-28 12:00:32 -0500972 int rc;
973
Mike Christie9aaa2b42006-07-24 15:47:34 -0500974 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500975 if (rc < 0) {
Mike Christie53cb8a12006-06-28 12:00:32 -0500976 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
977 return rc;
978 }
979
Mike Christie53cb8a12006-06-28 12:00:32 -0500980 return 0;
981}
982
983static int
Mike Christie43a145a2006-10-16 18:09:38 -0400984iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700985{
Alex Aizman0896b752005-08-04 19:33:07 -0700986 int rc;
987
Mike Christie790f39a2006-05-18 20:31:39 -0500988 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700989 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700990 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
991 return rc;
992 }
993
Alex Aizman0896b752005-08-04 19:33:07 -0700994 return 0;
995}
996
Mike Christie7b7232f2006-02-01 21:06:49 -0600997int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700998 char *data, uint32_t data_size)
999{
1000 struct nlmsghdr *nlh;
1001 struct sk_buff *skb;
1002 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001003 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001004 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001005 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1006 data_size);
1007
Mike Christie790f39a2006-05-18 20:31:39 -05001008 priv = iscsi_if_transport_lookup(conn->transport);
1009 if (!priv)
1010 return -EINVAL;
1011
Mike Christie43a145a2006-10-16 18:09:38 -04001012 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001013 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05001014 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001015 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1016 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001017 return -ENOMEM;
1018 }
1019
Mike Christie790f39a2006-05-18 20:31:39 -05001020 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001021 ev = NLMSG_DATA(nlh);
1022 memset(ev, 0, sizeof(*ev));
1023 ev->transport_handle = iscsi_handle(conn->transport);
1024 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001025 ev->r.recv_req.cid = conn->cid;
1026 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001027 pdu = (char*)ev + sizeof(*ev);
1028 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1029 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1030
Mike Christie43a145a2006-10-16 18:09:38 -04001031 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -07001032}
1033EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1034
Mike Christiee5bd7b52008-09-24 11:46:10 -05001035void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001036{
1037 struct nlmsghdr *nlh;
1038 struct sk_buff *skb;
1039 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001040 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001041 int len = NLMSG_SPACE(sizeof(*ev));
1042
Mike Christie790f39a2006-05-18 20:31:39 -05001043 priv = iscsi_if_transport_lookup(conn->transport);
1044 if (!priv)
1045 return;
1046
Mike Christie43a145a2006-10-16 18:09:38 -04001047 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001048 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001049 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1050 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001051 return;
1052 }
1053
Mike Christie790f39a2006-05-18 20:31:39 -05001054 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001055 ev = NLMSG_DATA(nlh);
1056 ev->transport_handle = iscsi_handle(conn->transport);
1057 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001058 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001059 ev->r.connerror.cid = conn->cid;
1060 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001061
Mike Christie43a145a2006-10-16 18:09:38 -04001062 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001063
Mike Christie322d7392008-01-31 13:36:52 -06001064 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1065 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001066}
Mike Christiee5bd7b52008-09-24 11:46:10 -05001067EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001068
1069static int
1070iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
1071 void *payload, int size)
1072{
1073 struct sk_buff *skb;
1074 struct nlmsghdr *nlh;
1075 int len = NLMSG_SPACE(size);
1076 int flags = multi ? NLM_F_MULTI : 0;
1077 int t = done ? NLMSG_DONE : type;
1078
Mike Christie43a145a2006-10-16 18:09:38 -04001079 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001080 if (!skb) {
1081 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1082 return -ENOMEM;
1083 }
Alex Aizman0896b752005-08-04 19:33:07 -07001084
1085 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
1086 nlh->nlmsg_flags = flags;
1087 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -04001088 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -07001089}
1090
1091static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001092iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001093{
1094 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1095 struct iscsi_stats *stats;
1096 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001097 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001098 struct nlmsghdr *nlhstat;
1099 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001100 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001101 int len = NLMSG_SPACE(sizeof(*ev) +
1102 sizeof(struct iscsi_stats) +
1103 sizeof(struct iscsi_stats_custom) *
1104 ISCSI_STATS_CUSTOM_MAX);
1105 int err = 0;
1106
Mike Christie790f39a2006-05-18 20:31:39 -05001107 priv = iscsi_if_transport_lookup(transport);
1108 if (!priv)
1109 return -EINVAL;
1110
Mike Christieb5c7a122006-04-06 21:13:33 -05001111 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001112 if (!conn)
1113 return -EEXIST;
1114
1115 do {
1116 int actual_size;
1117
Mike Christie43a145a2006-10-16 18:09:38 -04001118 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001119 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001120 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1121 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001122 return -ENOMEM;
1123 }
1124
Mike Christie790f39a2006-05-18 20:31:39 -05001125 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001126 (len - sizeof(*nlhstat)), 0);
1127 evstat = NLMSG_DATA(nlhstat);
1128 memset(evstat, 0, sizeof(*evstat));
1129 evstat->transport_handle = iscsi_handle(conn->transport);
1130 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001131 evstat->u.get_stats.cid =
1132 ev->u.get_stats.cid;
1133 evstat->u.get_stats.sid =
1134 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001135 stats = (struct iscsi_stats *)
1136 ((char*)evstat + sizeof(*evstat));
1137 memset(stats, 0, sizeof(*stats));
1138
Mike Christie7b7232f2006-02-01 21:06:49 -06001139 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001140 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1141 sizeof(struct iscsi_stats) +
1142 sizeof(struct iscsi_stats_custom) *
1143 stats->custom_length);
1144 actual_size -= sizeof(*nlhstat);
1145 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001146 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001147 nlhstat->nlmsg_len = actual_size;
1148
Mike Christie43a145a2006-10-16 18:09:38 -04001149 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -07001150 } while (err < 0 && err != -ECONNREFUSED);
1151
1152 return err;
1153}
1154
Mike Christie53cb8a12006-06-28 12:00:32 -05001155/**
Mike Christie26974782007-12-13 12:43:29 -06001156 * iscsi_session_event - send session destr. completion event
1157 * @session: iscsi class session
1158 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001159 */
Mike Christie26974782007-12-13 12:43:29 -06001160int iscsi_session_event(struct iscsi_cls_session *session,
1161 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001162{
1163 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001164 struct Scsi_Host *shost;
1165 struct iscsi_uevent *ev;
1166 struct sk_buff *skb;
1167 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001168 int rc, len = NLMSG_SPACE(sizeof(*ev));
1169
Mike Christie26974782007-12-13 12:43:29 -06001170 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001171 if (!priv)
1172 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001173 shost = iscsi_session_to_shost(session);
1174
Mike Christie43a145a2006-10-16 18:09:38 -04001175 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001176 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001177 iscsi_cls_session_printk(KERN_ERR, session,
1178 "Cannot notify userspace of session "
1179 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001180 return -ENOMEM;
1181 }
1182
1183 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1184 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001185 ev->transport_handle = iscsi_handle(session->transport);
1186
1187 ev->type = event;
1188 switch (event) {
1189 case ISCSI_KEVENT_DESTROY_SESSION:
1190 ev->r.d_session.host_no = shost->host_no;
1191 ev->r.d_session.sid = session->sid;
1192 break;
1193 case ISCSI_KEVENT_CREATE_SESSION:
1194 ev->r.c_session_ret.host_no = shost->host_no;
1195 ev->r.c_session_ret.sid = session->sid;
1196 break;
1197 case ISCSI_KEVENT_UNBIND_SESSION:
1198 ev->r.unbind_session.host_no = shost->host_no;
1199 ev->r.unbind_session.sid = session->sid;
1200 break;
1201 default:
Mike Christie322d7392008-01-31 13:36:52 -06001202 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1203 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001204 kfree_skb(skb);
1205 return -EINVAL;
1206 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001207
1208 /*
1209 * this will occur if the daemon is not up, so we just warn
1210 * the user and when the daemon is restarted it will handle it
1211 */
Mike Christie43a145a2006-10-16 18:09:38 -04001212 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001213 if (rc < 0)
Mike Christie322d7392008-01-31 13:36:52 -06001214 iscsi_cls_session_printk(KERN_ERR, session,
1215 "Cannot notify userspace of session "
1216 "event %u. Check iscsi daemon\n",
1217 event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001218 return rc;
1219}
Mike Christie26974782007-12-13 12:43:29 -06001220EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001221
Alex Aizman0896b752005-08-04 19:33:07 -07001222static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001223iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1224 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001225 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001226{
1227 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001228 struct iscsi_cls_session *session;
Mike Christied82ff9be2008-05-21 15:54:13 -05001229 uint32_t host_no;
Mike Christie7b8631b2006-01-13 18:05:50 -06001230
Mike Christied82ff9be2008-05-21 15:54:13 -05001231 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie40753ca2008-05-21 15:53:56 -05001232 initial_cmdsn, &host_no);
Mike Christie7b7232f2006-02-01 21:06:49 -06001233 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001234 return -ENOMEM;
1235
Mike Christie40753ca2008-05-21 15:53:56 -05001236 ev->r.c_session_ret.host_no = host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001237 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001238 return 0;
1239}
1240
1241static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001242iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001243{
Mike Christie7b8631b2006-01-13 18:05:50 -06001244 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001245 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001246
Mike Christieb5c7a122006-04-06 21:13:33 -05001247 session = iscsi_session_lookup(ev->u.c_conn.sid);
1248 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001249 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001250 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001251 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001252 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001253
Mike Christie7b7232f2006-02-01 21:06:49 -06001254 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001255 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001256 iscsi_cls_session_printk(KERN_ERR, session,
1257 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001258 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001259 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001260
Mike Christieb5c7a122006-04-06 21:13:33 -05001261 ev->r.c_conn_ret.sid = session->sid;
1262 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001263 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001264}
1265
1266static int
1267iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1268{
Mike Christie7b8631b2006-01-13 18:05:50 -06001269 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001270
Mike Christieb5c7a122006-04-06 21:13:33 -05001271 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001272 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001273 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001274
Mike Christie7b8631b2006-01-13 18:05:50 -06001275 if (transport->destroy_conn)
1276 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001277 return 0;
1278}
1279
Mike Christiefd7255f2006-04-06 21:13:36 -05001280static int
1281iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1282{
1283 char *data = (char*)ev + sizeof(*ev);
1284 struct iscsi_cls_conn *conn;
1285 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001286 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001287
1288 session = iscsi_session_lookup(ev->u.set_param.sid);
1289 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1290 if (!conn || !session)
1291 return -EINVAL;
1292
1293 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001294 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001295 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001296 if (value != 0)
1297 session->recovery_tmo = value;
1298 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001299 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001300 err = transport->set_param(conn, ev->u.set_param.param,
1301 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001302 }
1303
1304 return err;
1305}
1306
Mike Christie7b8631b2006-01-13 18:05:50 -06001307static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001308iscsi_if_transport_ep(struct iscsi_transport *transport,
1309 struct iscsi_uevent *ev, int msg_type)
1310{
Mike Christied82ff9be2008-05-21 15:54:13 -05001311 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001312 struct sockaddr *dst_addr;
1313 int rc = 0;
1314
1315 switch (msg_type) {
1316 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1317 if (!transport->ep_connect)
1318 return -EINVAL;
1319
1320 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christied82ff9be2008-05-21 15:54:13 -05001321 ep = transport->ep_connect(dst_addr,
1322 ev->u.ep_connect.non_blocking);
1323 if (IS_ERR(ep))
1324 return PTR_ERR(ep);
1325
1326 ev->r.ep_connect_ret.handle = ep->id;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001327 break;
1328 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1329 if (!transport->ep_poll)
1330 return -EINVAL;
1331
Mike Christied82ff9be2008-05-21 15:54:13 -05001332 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1333 if (!ep)
1334 return -EINVAL;
1335
1336 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001337 ev->u.ep_poll.timeout_ms);
1338 break;
1339 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1340 if (!transport->ep_disconnect)
1341 return -EINVAL;
1342
Mike Christied82ff9be2008-05-21 15:54:13 -05001343 ep = iscsi_lookup_endpoint(ev->u.ep_disconnect.ep_handle);
1344 if (!ep)
1345 return -EINVAL;
1346
1347 transport->ep_disconnect(ep);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001348 break;
1349 }
1350 return rc;
1351}
1352
1353static int
Mike Christie01cb2252006-06-28 12:00:22 -05001354iscsi_tgt_dscvr(struct iscsi_transport *transport,
1355 struct iscsi_uevent *ev)
1356{
Mike Christie2174a042007-05-30 12:57:10 -05001357 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001358 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001359 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001360
1361 if (!transport->tgt_dscvr)
1362 return -EINVAL;
1363
Mike Christie2174a042007-05-30 12:57:10 -05001364 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001365 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001366 printk(KERN_ERR "target discovery could not find host no %u\n",
1367 ev->u.tgt_dscvr.host_no);
1368 return -ENODEV;
1369 }
1370
1371
Mike Christie01cb2252006-06-28 12:00:22 -05001372 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001373 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1374 ev->u.tgt_dscvr.enable, dst_addr);
1375 scsi_host_put(shost);
1376 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001377}
1378
1379static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001380iscsi_set_host_param(struct iscsi_transport *transport,
1381 struct iscsi_uevent *ev)
1382{
1383 char *data = (char*)ev + sizeof(*ev);
1384 struct Scsi_Host *shost;
1385 int err;
1386
1387 if (!transport->set_host_param)
1388 return -ENOSYS;
1389
1390 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001391 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001392 printk(KERN_ERR "set_host_param could not find host no %u\n",
1393 ev->u.set_host_param.host_no);
1394 return -ENODEV;
1395 }
1396
1397 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1398 data, ev->u.set_host_param.len);
1399 scsi_host_put(shost);
1400 return err;
1401}
1402
1403static int
Alex Aizman0896b752005-08-04 19:33:07 -07001404iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1405{
1406 int err = 0;
1407 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1408 struct iscsi_transport *transport = NULL;
1409 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001410 struct iscsi_cls_session *session;
1411 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001412 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001413
Alex Aizman0896b752005-08-04 19:33:07 -07001414 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1415 if (!priv)
1416 return -EINVAL;
1417 transport = priv->iscsi_transport;
1418
Mike Christie7b7232f2006-02-01 21:06:49 -06001419 if (!try_module_get(transport->owner))
1420 return -EINVAL;
1421
Mike Christie790f39a2006-05-18 20:31:39 -05001422 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1423
Alex Aizman0896b752005-08-04 19:33:07 -07001424 switch (nlh->nlmsg_type) {
1425 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001426 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001427 ev->u.c_session.initial_cmdsn,
1428 ev->u.c_session.cmds_max,
1429 ev->u.c_session.queue_depth);
1430 break;
1431 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001432 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001433 if (!ep) {
1434 err = -EINVAL;
1435 break;
1436 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001437
1438 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001439 ev->u.c_bound_session.initial_cmdsn,
1440 ev->u.c_bound_session.cmds_max,
1441 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001442 break;
1443 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001444 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001445 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001446 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001447 else
1448 err = -EINVAL;
1449 break;
1450 case ISCSI_UEVENT_UNBIND_SESSION:
1451 session = iscsi_session_lookup(ev->u.d_session.sid);
1452 if (session)
1453 iscsi_unbind_session(session);
1454 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001455 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001456 break;
1457 case ISCSI_UEVENT_CREATE_CONN:
1458 err = iscsi_if_create_conn(transport, ev);
1459 break;
1460 case ISCSI_UEVENT_DESTROY_CONN:
1461 err = iscsi_if_destroy_conn(transport, ev);
1462 break;
1463 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001464 session = iscsi_session_lookup(ev->u.b_conn.sid);
1465 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001466
1467 if (session && conn)
1468 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001469 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001470 ev->u.b_conn.is_leading);
1471 else
1472 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001473 break;
1474 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001475 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001476 break;
1477 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001478 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001479 if (conn)
1480 ev->r.retcode = transport->start_conn(conn);
1481 else
1482 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001483 break;
1484 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001485 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001486 if (conn)
1487 transport->stop_conn(conn, ev->u.stop_conn.flag);
1488 else
1489 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001490 break;
1491 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001492 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001493 if (conn)
1494 ev->r.retcode = transport->send_pdu(conn,
1495 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1496 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1497 ev->u.send_pdu.data_size);
1498 else
1499 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001500 break;
1501 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001502 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001503 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001504 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1505 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1506 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1507 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1508 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001509 case ISCSI_UEVENT_TGT_DSCVR:
1510 err = iscsi_tgt_dscvr(transport, ev);
1511 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001512 case ISCSI_UEVENT_SET_HOST_PARAM:
1513 err = iscsi_set_host_param(transport, ev);
1514 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001515 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001516 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001517 break;
1518 }
1519
Mike Christie7b7232f2006-02-01 21:06:49 -06001520 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001521 return err;
1522}
1523
Mike Christieb5c7a122006-04-06 21:13:33 -05001524/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001525 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1526 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001527 */
Alex Aizman0896b752005-08-04 19:33:07 -07001528static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001529iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001530{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001531 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001532 while (skb->len >= NLMSG_SPACE(0)) {
1533 int err;
1534 uint32_t rlen;
1535 struct nlmsghdr *nlh;
1536 struct iscsi_uevent *ev;
1537
1538 nlh = nlmsg_hdr(skb);
1539 if (nlh->nlmsg_len < sizeof(*nlh) ||
1540 skb->len < nlh->nlmsg_len) {
1541 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001542 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001543
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001544 ev = NLMSG_DATA(nlh);
1545 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1546 if (rlen > skb->len)
1547 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001548
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001549 err = iscsi_if_recv_msg(skb, nlh);
1550 if (err) {
1551 ev->type = ISCSI_KEVENT_IF_ERROR;
1552 ev->iferror = err;
1553 }
1554 do {
1555 /*
1556 * special case for GET_STATS:
1557 * on success - sending reply and stats from
1558 * inside of if_recv_msg(),
1559 * on error - fall through.
1560 */
1561 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001562 break;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001563 err = iscsi_if_send_reply(
1564 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1565 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1566 } while (err < 0 && err != -ECONNREFUSED);
1567 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001568 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001569 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001570}
1571
Mike Christiefd7255f2006-04-06 21:13:36 -05001572#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001573struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001574 __ATTR(_name,_mode,_show,_store)
1575
Alex Aizman0896b752005-08-04 19:33:07 -07001576/*
1577 * iSCSI connection attrs
1578 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001579#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001581show_conn_param_##param(struct device *dev, \
1582 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001583{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001584 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001585 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001586 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001587}
1588
Mike Christiea54a52c2006-06-28 12:00:23 -05001589#define iscsi_conn_attr(field, param) \
1590 iscsi_conn_attr_show(param) \
1591static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001592 NULL);
1593
Mike Christiea54a52c2006-06-28 12:00:23 -05001594iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1595iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1596iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1597iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1598iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1599iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1600iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1601iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1602iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1603iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1604iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001605iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1606iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608/*
Alex Aizman0896b752005-08-04 19:33:07 -07001609 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
Mike Christieb2c64162007-05-30 12:57:16 -05001611#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001613show_session_param_##param(struct device *dev, \
1614 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001615{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001616 struct iscsi_cls_session *session = \
1617 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001618 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001619 \
1620 if (perm && !capable(CAP_SYS_ADMIN)) \
1621 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001622 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Mike Christieb2c64162007-05-30 12:57:16 -05001625#define iscsi_session_attr(field, param, perm) \
1626 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001627static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001628 NULL);
1629
Mike Christieb2c64162007-05-30 12:57:16 -05001630iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1631iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1632iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1633iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1634iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1635iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1636iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1637iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1638iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1639iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1640iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1641iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1642iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1643iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001644iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1645iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1646iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05001647iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
1648iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0)
Mike Christiefd7255f2006-04-06 21:13:36 -05001649
Mike Christie6eabafb2008-01-31 13:36:43 -06001650static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001651show_priv_session_state(struct device *dev, struct device_attribute *attr,
1652 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06001653{
Tony Jonesee959b02008-02-22 00:13:36 +01001654 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06001655 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1656}
1657static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1658 NULL);
1659
Mike Christiefd7255f2006-04-06 21:13:36 -05001660#define iscsi_priv_session_attr_show(field, format) \
1661static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001662show_priv_session_##field(struct device *dev, \
1663 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001664{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001665 struct iscsi_cls_session *session = \
1666 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001667 return sprintf(buf, format"\n", session->field); \
1668}
1669
1670#define iscsi_priv_session_attr(field, format) \
1671 iscsi_priv_session_attr_show(field, format) \
1672static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1673 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001674iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001675
Mike Christie1819dc82007-05-30 12:57:08 -05001676/*
1677 * iSCSI host attrs
1678 */
1679#define iscsi_host_attr_show(param) \
1680static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001681show_host_param_##param(struct device *dev, \
1682 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05001683{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001684 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05001685 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1686 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1687}
1688
1689#define iscsi_host_attr(field, param) \
1690 iscsi_host_attr_show(param) \
1691static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1692 NULL);
1693
Mike Christied8196ed2007-05-30 12:57:25 -05001694iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001695iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001696iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001697iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001698
Mike Christiefd7255f2006-04-06 21:13:36 -05001699#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1700do { \
Tony Jonesee959b02008-02-22 00:13:36 +01001701 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001702 count++; \
1703} while (0)
1704
Mike Christiea54a52c2006-06-28 12:00:23 -05001705
Mike Christiefd7255f2006-04-06 21:13:36 -05001706#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1707do { \
1708 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001709 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001711 } \
1712} while (0)
1713
Mike Christiefd7255f2006-04-06 21:13:36 -05001714#define SETUP_CONN_RD_ATTR(field, param_flag) \
1715do { \
1716 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001717 priv->conn_attrs[count] = &dev_attr_conn_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001718 count++; \
1719 } \
1720} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Mike Christie1819dc82007-05-30 12:57:08 -05001722#define SETUP_HOST_RD_ATTR(field, param_flag) \
1723do { \
1724 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001725 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05001726 count++; \
1727 } \
1728} while (0)
1729
Alex Aizman0896b752005-08-04 19:33:07 -07001730static int iscsi_session_match(struct attribute_container *cont,
1731 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Mike Christie7b8631b2006-01-13 18:05:50 -06001733 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001735 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Alex Aizman0896b752005-08-04 19:33:07 -07001737 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return 0;
1739
Mike Christie7b8631b2006-01-13 18:05:50 -06001740 session = iscsi_dev_to_session(dev);
1741 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001742 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return 0;
1744
Alex Aizman0896b752005-08-04 19:33:07 -07001745 priv = to_iscsi_internal(shost->transportt);
1746 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1747 return 0;
1748
1749 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Alex Aizman0896b752005-08-04 19:33:07 -07001752static int iscsi_conn_match(struct attribute_container *cont,
1753 struct device *dev)
1754{
Mike Christie7b8631b2006-01-13 18:05:50 -06001755 struct iscsi_cls_session *session;
1756 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001757 struct Scsi_Host *shost;
1758 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Alex Aizman0896b752005-08-04 19:33:07 -07001760 if (!iscsi_is_conn_dev(dev))
1761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Mike Christie7b8631b2006-01-13 18:05:50 -06001763 conn = iscsi_dev_to_conn(dev);
1764 session = iscsi_dev_to_session(conn->dev.parent);
1765 shost = iscsi_session_to_shost(session);
1766
Alex Aizman0896b752005-08-04 19:33:07 -07001767 if (!shost->transportt)
1768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Alex Aizman0896b752005-08-04 19:33:07 -07001770 priv = to_iscsi_internal(shost->transportt);
1771 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1772 return 0;
1773
1774 return &priv->conn_cont.ac == cont;
1775}
1776
Mike Christie30a6c652006-04-06 21:13:39 -05001777static int iscsi_host_match(struct attribute_container *cont,
1778 struct device *dev)
1779{
1780 struct Scsi_Host *shost;
1781 struct iscsi_internal *priv;
1782
1783 if (!scsi_is_host_device(dev))
1784 return 0;
1785
1786 shost = dev_to_shost(dev);
1787 if (!shost->transportt ||
1788 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1789 return 0;
1790
1791 priv = to_iscsi_internal(shost->transportt);
1792 return &priv->t.host_attrs.ac == cont;
1793}
1794
Mike Christie7b8631b2006-01-13 18:05:50 -06001795struct scsi_transport_template *
1796iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001797{
1798 struct iscsi_internal *priv;
1799 unsigned long flags;
1800 int count = 0, err;
1801
1802 BUG_ON(!tt);
1803
1804 priv = iscsi_if_transport_lookup(tt);
1805 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001806 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001807
Jes Sorensen24669f752006-01-16 10:31:18 -05001808 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001809 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001810 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001811 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001812 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001813 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001814 priv->t.user_scan = iscsi_user_scan;
Mike Christiea4804cd2008-05-21 15:54:00 -05001815 if (!(tt->caps & CAP_DATA_PATH_OFFLOAD))
1816 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07001817
Tony Jonesee959b02008-02-22 00:13:36 +01001818 priv->dev.class = &iscsi_transport_class;
1819 snprintf(priv->dev.bus_id, BUS_ID_SIZE, "%s", tt->name);
1820 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001821 if (err)
1822 goto free_priv;
1823
Tony Jonesee959b02008-02-22 00:13:36 +01001824 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07001825 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01001826 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07001827
Mike Christie30a6c652006-04-06 21:13:39 -05001828 /* host parameters */
1829 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1830 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1831 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05001832 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001833 transport_container_register(&priv->t.host_attrs);
1834
Mike Christied8196ed2007-05-30 12:57:25 -05001835 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001836 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001837 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001838 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001839 BUG_ON(count > ISCSI_HOST_ATTRS);
1840 priv->host_attrs[count] = NULL;
1841 count = 0;
1842
Alex Aizman0896b752005-08-04 19:33:07 -07001843 /* connection parameters */
1844 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1845 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1846 priv->conn_cont.ac.match = iscsi_conn_match;
1847 transport_container_register(&priv->conn_cont);
1848
Mike Christiefd7255f2006-04-06 21:13:36 -05001849 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1850 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1851 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1852 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1853 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1854 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1855 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1856 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001857 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001858 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1859 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001860 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1861 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001862
1863 BUG_ON(count > ISCSI_CONN_ATTRS);
1864 priv->conn_attrs[count] = NULL;
1865 count = 0;
1866
1867 /* session parameters */
1868 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1869 priv->session_cont.ac.class = &iscsi_session_class.class;
1870 priv->session_cont.ac.match = iscsi_session_match;
1871 transport_container_register(&priv->session_cont);
1872
Mike Christiefd7255f2006-04-06 21:13:36 -05001873 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1874 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1875 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1876 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1877 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1878 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1879 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1880 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001881 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1882 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001883 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1884 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1885 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1886 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001887 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001888 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1889 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie88dfd342008-05-21 15:54:16 -05001890 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
1891 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
Mike Christie30a6c652006-04-06 21:13:39 -05001892 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06001893 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001896 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Alex Aizman0896b752005-08-04 19:33:07 -07001898 spin_lock_irqsave(&iscsi_transport_lock, flags);
1899 list_add(&priv->list, &iscsi_transports);
1900 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Alex Aizman0896b752005-08-04 19:33:07 -07001902 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001903 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Tony Jonesee959b02008-02-22 00:13:36 +01001905unregister_dev:
1906 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05001907 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001908free_priv:
1909 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001910 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
Alex Aizman0896b752005-08-04 19:33:07 -07001912EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Alex Aizman0896b752005-08-04 19:33:07 -07001914int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Alex Aizman0896b752005-08-04 19:33:07 -07001916 struct iscsi_internal *priv;
1917 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Alex Aizman0896b752005-08-04 19:33:07 -07001919 BUG_ON(!tt);
1920
Arjan van de Ven0b950672006-01-11 13:16:10 +01001921 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001922
1923 priv = iscsi_if_transport_lookup(tt);
1924 BUG_ON (!priv);
1925
Alex Aizman0896b752005-08-04 19:33:07 -07001926 spin_lock_irqsave(&iscsi_transport_lock, flags);
1927 list_del(&priv->list);
1928 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1929
1930 transport_container_unregister(&priv->conn_cont);
1931 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001932 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001933
Tony Jonesee959b02008-02-22 00:13:36 +01001934 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1935 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001936 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001937
1938 return 0;
1939}
1940EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942static __init int iscsi_transport_init(void)
1943{
Alex Aizman0896b752005-08-04 19:33:07 -07001944 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Meelis Roos09492602006-12-17 12:10:26 -06001946 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001947 ISCSI_TRANSPORT_VERSION);
1948
Mike Christie41be1442007-02-28 17:32:18 -06001949 atomic_set(&iscsi_session_nr, 0);
1950
Alex Aizman0896b752005-08-04 19:33:07 -07001951 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (err)
1953 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001954
Mike Christied82ff9be2008-05-21 15:54:13 -05001955 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001956 if (err)
1957 goto unregister_transport_class;
1958
Mike Christied82ff9be2008-05-21 15:54:13 -05001959 err = transport_class_register(&iscsi_host_class);
1960 if (err)
1961 goto unregister_endpoint_class;
1962
Mike Christie30a6c652006-04-06 21:13:39 -05001963 err = transport_class_register(&iscsi_connection_class);
1964 if (err)
1965 goto unregister_host_class;
1966
Alex Aizman0896b752005-08-04 19:33:07 -07001967 err = transport_class_register(&iscsi_session_class);
1968 if (err)
1969 goto unregister_conn_class;
1970
Mike Christied82ff9be2008-05-21 15:54:13 -05001971 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
1972 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001973 if (!nls) {
1974 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001975 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001976 }
1977
Mike Christied8bf5412007-12-13 12:43:27 -06001978 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1979 if (!iscsi_eh_timer_workq)
1980 goto release_nls;
1981
Mike Christie43a145a2006-10-16 18:09:38 -04001982 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001983
Mike Christied8bf5412007-12-13 12:43:27 -06001984release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001985 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001986unregister_session_class:
1987 transport_class_unregister(&iscsi_session_class);
1988unregister_conn_class:
1989 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001990unregister_host_class:
1991 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05001992unregister_endpoint_class:
1993 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001994unregister_transport_class:
1995 class_unregister(&iscsi_transport_class);
1996 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999static void __exit iscsi_transport_exit(void)
2000{
Mike Christied8bf5412007-12-13 12:43:27 -06002001 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002002 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002003 transport_class_unregister(&iscsi_connection_class);
2004 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002005 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002006 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002007 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010module_init(iscsi_transport_init);
2011module_exit(iscsi_transport_exit);
2012
Alex Aizman0896b752005-08-04 19:33:07 -07002013MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2014 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2015 "Alex Aizman <itn780@yahoo.com>");
2016MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002018MODULE_VERSION(ISCSI_TRANSPORT_VERSION);