blob: dfb026b95a6a04c31e425f5e5e049ef44dc48242 [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 Christie6eabafb2008-01-31 13:36:43 -060033#define ISCSI_SESSION_ATTRS 19
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 Christie31ed0bf2008-02-26 12:35:23 -060036#define ISCSI_TRANSPORT_VERSION "2.0-869"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct iscsi_internal {
Mike Christie790f39a2006-05-18 20:31:39 -050039 int daemon_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070041 struct iscsi_transport *iscsi_transport;
42 struct list_head list;
Alex Aizman0896b752005-08-04 19:33:07 -070043 struct class_device cdev;
Mike Christie30a6c652006-04-06 21:13:39 -050044
45 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070046 struct transport_container conn_cont;
47 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
48 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Mike Christie41be1442007-02-28 17:32:18 -060052static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060053static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050054
Alex Aizman0896b752005-08-04 19:33:07 -070055/*
56 * list of registered transports and lock that must
57 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010058 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070059 */
60static LIST_HEAD(iscsi_transports);
61static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Alex Aizman0896b752005-08-04 19:33:07 -070063#define to_iscsi_internal(tmpl) \
64 container_of(tmpl, struct iscsi_internal, t)
65
66#define cdev_to_iscsi_internal(_cdev) \
67 container_of(_cdev, struct iscsi_internal, cdev)
68
69static void iscsi_transport_release(struct class_device *cdev)
70{
71 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
72 kfree(priv);
73}
74
75/*
76 * iscsi_transport_class represents the iscsi_transports that are
77 * registered.
78 */
79static struct class iscsi_transport_class = {
80 .name = "iscsi_transport",
81 .release = iscsi_transport_release,
82};
83
84static ssize_t
85show_transport_handle(struct class_device *cdev, char *buf)
86{
87 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
Mike Christie762e2bf2005-09-12 21:01:46 -050088 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070089}
90static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
91
92#define show_transport_attr(name, format) \
93static ssize_t \
94show_transport_##name(struct class_device *cdev, char *buf) \
95{ \
96 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
97 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
98} \
99static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
100
101show_transport_attr(caps, "0x%x");
102show_transport_attr(max_lun, "%d");
103show_transport_attr(max_conn, "%d");
104show_transport_attr(max_cmd_len, "%d");
105
106static struct attribute *iscsi_transport_attrs[] = {
107 &class_device_attr_handle.attr,
108 &class_device_attr_caps.attr,
109 &class_device_attr_max_lun.attr,
110 &class_device_attr_max_conn.attr,
111 &class_device_attr_max_cmd_len.attr,
112 NULL,
113};
114
115static struct attribute_group iscsi_transport_group = {
116 .attrs = iscsi_transport_attrs,
117};
118
Mike Christie26974782007-12-13 12:43:29 -0600119
120
Mike Christie30a6c652006-04-06 21:13:39 -0500121static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
122 struct class_device *cdev)
123{
124 struct Scsi_Host *shost = dev_to_shost(dev);
125 struct iscsi_host *ihost = shost->shost_data;
126
127 memset(ihost, 0, sizeof(*ihost));
128 INIT_LIST_HEAD(&ihost->sessions);
129 mutex_init(&ihost->mutex);
Mike Christie8aae18a2008-01-31 13:36:48 -0600130 atomic_set(&ihost->nr_scans, 0);
Mike Christie26974782007-12-13 12:43:29 -0600131
Mike Christiebd976f62008-01-31 13:36:46 -0600132 snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
Mike Christie26974782007-12-13 12:43:29 -0600133 shost->host_no);
Mike Christiebd976f62008-01-31 13:36:46 -0600134 ihost->scan_workq = create_singlethread_workqueue(
135 ihost->scan_workq_name);
136 if (!ihost->scan_workq)
Mike Christie26974782007-12-13 12:43:29 -0600137 return -ENOMEM;
138 return 0;
139}
140
141static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
142 struct class_device *cdev)
143{
144 struct Scsi_Host *shost = dev_to_shost(dev);
145 struct iscsi_host *ihost = shost->shost_data;
146
Mike Christiebd976f62008-01-31 13:36:46 -0600147 destroy_workqueue(ihost->scan_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500148 return 0;
149}
150
151static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
152 "iscsi_host",
153 iscsi_setup_host,
Mike Christie26974782007-12-13 12:43:29 -0600154 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500155 NULL);
156
Alex Aizman0896b752005-08-04 19:33:07 -0700157static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
158 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 NULL,
160 NULL,
161 NULL);
162
Alex Aizman0896b752005-08-04 19:33:07 -0700163static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
164 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 NULL,
166 NULL,
167 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700168
169static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100170static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700171
Mike Christie7b7232f2006-02-01 21:06:49 -0600172static LIST_HEAD(sesslist);
173static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700174static LIST_HEAD(connlist);
175static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Mike Christieb5c7a122006-04-06 21:13:33 -0500177static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
178{
179 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
180 return sess->sid;
181}
182
183/*
184 * Returns the matching session to a given sid
185 */
186static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600187{
188 unsigned long flags;
189 struct iscsi_cls_session *sess;
190
191 spin_lock_irqsave(&sesslock, flags);
192 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500193 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600194 spin_unlock_irqrestore(&sesslock, flags);
195 return sess;
196 }
197 }
198 spin_unlock_irqrestore(&sesslock, flags);
199 return NULL;
200}
201
Mike Christieb5c7a122006-04-06 21:13:33 -0500202/*
203 * Returns the matching connection to a given sid / cid tuple
204 */
205static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600206{
207 unsigned long flags;
208 struct iscsi_cls_conn *conn;
209
210 spin_lock_irqsave(&connlock, flags);
211 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500212 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600213 spin_unlock_irqrestore(&connlock, flags);
214 return conn;
215 }
216 }
217 spin_unlock_irqrestore(&connlock, flags);
218 return NULL;
219}
220
Mike Christie7b8631b2006-01-13 18:05:50 -0600221/*
222 * The following functions can be used by LLDs that allocate
223 * their own scsi_hosts or by software iscsi LLDs
224 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600225static struct {
226 int value;
227 char *name;
228} iscsi_session_state_names[] = {
229 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
230 { ISCSI_SESSION_FAILED, "FAILED" },
231 { ISCSI_SESSION_FREE, "FREE" },
232};
233
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200234static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600235{
236 int i;
237 char *name = NULL;
238
239 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
240 if (iscsi_session_state_names[i].value == state) {
241 name = iscsi_session_state_names[i].name;
242 break;
243 }
244 }
245 return name;
246}
247
248int iscsi_session_chkready(struct iscsi_cls_session *session)
249{
250 unsigned long flags;
251 int err;
252
253 spin_lock_irqsave(&session->lock, flags);
254 switch (session->state) {
255 case ISCSI_SESSION_LOGGED_IN:
256 err = 0;
257 break;
258 case ISCSI_SESSION_FAILED:
259 err = DID_IMM_RETRY << 16;
260 break;
261 case ISCSI_SESSION_FREE:
262 err = DID_NO_CONNECT << 16;
263 break;
264 default:
265 err = DID_NO_CONNECT << 16;
266 break;
267 }
268 spin_unlock_irqrestore(&session->lock, flags);
269 return err;
270}
271EXPORT_SYMBOL_GPL(iscsi_session_chkready);
272
Mike Christie7b8631b2006-01-13 18:05:50 -0600273static void iscsi_session_release(struct device *dev)
274{
275 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600276 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Mike Christie7b8631b2006-01-13 18:05:50 -0600278 shost = iscsi_session_to_shost(session);
279 scsi_host_put(shost);
280 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Mike Christie7b8631b2006-01-13 18:05:50 -0600283static int iscsi_is_session_dev(const struct device *dev)
284{
285 return dev->release == iscsi_session_release;
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Mike Christie8aae18a2008-01-31 13:36:48 -0600288/**
289 * iscsi_scan_finished - helper to report when running scans are done
290 * @shost: scsi host
291 * @time: scan run time
292 *
293 * This function can be used by drives like qla4xxx to report to the scsi
294 * layer when the scans it kicked off at module load time are done.
295 */
296int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
297{
298 struct iscsi_host *ihost = shost->shost_data;
299 /*
300 * qla4xxx will have kicked off some session unblocks before calling
301 * scsi_scan_host, so just wait for them to complete.
302 */
303 return !atomic_read(&ihost->nr_scans);
304}
305EXPORT_SYMBOL_GPL(iscsi_scan_finished);
306
Mike Christie30a6c652006-04-06 21:13:39 -0500307static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
308 uint id, uint lun)
309{
310 struct iscsi_host *ihost = shost->shost_data;
311 struct iscsi_cls_session *session;
312
313 mutex_lock(&ihost->mutex);
314 list_for_each_entry(session, &ihost->sessions, host_list) {
Mike Christiee6f3b632006-06-28 12:00:29 -0500315 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Mike Christie30a6c652006-04-06 21:13:39 -0500316 (id == SCAN_WILD_CARD || id == session->target_id))
Mike Christiee6f3b632006-06-28 12:00:29 -0500317 scsi_scan_target(&session->dev, 0,
Mike Christie30a6c652006-04-06 21:13:39 -0500318 session->target_id, lun, 1);
319 }
320 mutex_unlock(&ihost->mutex);
321
322 return 0;
323}
324
Mike Christiebd976f62008-01-31 13:36:46 -0600325static void iscsi_scan_session(struct work_struct *work)
326{
327 struct iscsi_cls_session *session =
328 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600329 struct Scsi_Host *shost = iscsi_session_to_shost(session);
330 struct iscsi_host *ihost = shost->shost_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600331 unsigned long flags;
332
333 spin_lock_irqsave(&session->lock, flags);
334 if (session->state != ISCSI_SESSION_LOGGED_IN) {
335 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie8aae18a2008-01-31 13:36:48 -0600336 goto done;
Mike Christiebd976f62008-01-31 13:36:46 -0600337 }
338 spin_unlock_irqrestore(&session->lock, flags);
339
340 scsi_scan_target(&session->dev, 0, session->target_id,
341 SCAN_WILD_CARD, 1);
Mike Christie8aae18a2008-01-31 13:36:48 -0600342done:
343 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600344}
345
David Howellsc4028952006-11-22 14:57:56 +0000346static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500347{
David Howellsc4028952006-11-22 14:57:56 +0000348 struct iscsi_cls_session *session =
349 container_of(work, struct iscsi_cls_session,
350 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600351 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500352
Mike Christie322d7392008-01-31 13:36:52 -0600353 iscsi_cls_session_printk(KERN_INFO, session,
354 "session recovery timed out after %d secs\n",
355 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500356
Mike Christie6eabafb2008-01-31 13:36:43 -0600357 spin_lock_irqsave(&session->lock, flags);
358 switch (session->state) {
359 case ISCSI_SESSION_FAILED:
360 session->state = ISCSI_SESSION_FREE;
361 break;
362 case ISCSI_SESSION_LOGGED_IN:
363 case ISCSI_SESSION_FREE:
364 /* we raced with the unblock's flush */
365 spin_unlock_irqrestore(&session->lock, flags);
366 return;
367 }
368 spin_unlock_irqrestore(&session->lock, flags);
369
Mike Christie30a6c652006-04-06 21:13:39 -0500370 if (session->transport->session_recovery_timedout)
371 session->transport->session_recovery_timedout(session);
372
373 scsi_target_unblock(&session->dev);
374}
375
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200376static void __iscsi_unblock_session(struct iscsi_cls_session *session)
Mike Christie30a6c652006-04-06 21:13:39 -0500377{
378 if (!cancel_delayed_work(&session->recovery_work))
Mike Christied8bf5412007-12-13 12:43:27 -0600379 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500380 scsi_target_unblock(&session->dev);
381}
Mike Christie6eabafb2008-01-31 13:36:43 -0600382
383void iscsi_unblock_session(struct iscsi_cls_session *session)
384{
Mike Christiebd976f62008-01-31 13:36:46 -0600385 struct Scsi_Host *shost = iscsi_session_to_shost(session);
386 struct iscsi_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600387 unsigned long flags;
388
389 spin_lock_irqsave(&session->lock, flags);
390 session->state = ISCSI_SESSION_LOGGED_IN;
391 spin_unlock_irqrestore(&session->lock, flags);
392
393 __iscsi_unblock_session(session);
Mike Christie8aae18a2008-01-31 13:36:48 -0600394 /*
395 * Only do kernel scanning if the driver is properly hooked into
396 * the async scanning code (drivers like iscsi_tcp do login and
397 * scanning from userspace).
398 */
399 if (shost->hostt->scan_finished) {
400 if (queue_work(ihost->scan_workq, &session->scan_work))
401 atomic_inc(&ihost->nr_scans);
402 }
Mike Christie6eabafb2008-01-31 13:36:43 -0600403}
Mike Christie30a6c652006-04-06 21:13:39 -0500404EXPORT_SYMBOL_GPL(iscsi_unblock_session);
405
406void iscsi_block_session(struct iscsi_cls_session *session)
407{
Mike Christie6eabafb2008-01-31 13:36:43 -0600408 unsigned long flags;
409
410 spin_lock_irqsave(&session->lock, flags);
411 session->state = ISCSI_SESSION_FAILED;
412 spin_unlock_irqrestore(&session->lock, flags);
413
Mike Christie30a6c652006-04-06 21:13:39 -0500414 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600415 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
416 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500417}
418EXPORT_SYMBOL_GPL(iscsi_block_session);
419
Mike Christie26974782007-12-13 12:43:29 -0600420static void __iscsi_unbind_session(struct work_struct *work)
421{
422 struct iscsi_cls_session *session =
423 container_of(work, struct iscsi_cls_session,
424 unbind_work);
425 struct Scsi_Host *shost = iscsi_session_to_shost(session);
426 struct iscsi_host *ihost = shost->shost_data;
427
428 /* Prevent new scans and make sure scanning is not in progress */
429 mutex_lock(&ihost->mutex);
430 if (list_empty(&session->host_list)) {
431 mutex_unlock(&ihost->mutex);
432 return;
433 }
434 list_del_init(&session->host_list);
435 mutex_unlock(&ihost->mutex);
436
437 scsi_remove_target(&session->dev);
438 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
439}
440
441static int iscsi_unbind_session(struct iscsi_cls_session *session)
442{
443 struct Scsi_Host *shost = iscsi_session_to_shost(session);
444 struct iscsi_host *ihost = shost->shost_data;
445
Mike Christiebd976f62008-01-31 13:36:46 -0600446 return queue_work(ihost->scan_workq, &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -0600447}
448
Mike Christie7b8631b2006-01-13 18:05:50 -0600449struct iscsi_cls_session *
Mike Christie8434aa82006-06-28 12:00:30 -0500450iscsi_alloc_session(struct Scsi_Host *shost,
451 struct iscsi_transport *transport)
Mike Christie7b8631b2006-01-13 18:05:50 -0600452{
453 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Mike Christieb5c7a122006-04-06 21:13:33 -0500455 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
456 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600457 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500458 return NULL;
459
Mike Christie7b8631b2006-01-13 18:05:50 -0600460 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500461 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600462 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000463 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500464 INIT_LIST_HEAD(&session->host_list);
465 INIT_LIST_HEAD(&session->sess_list);
Mike Christie26974782007-12-13 12:43:29 -0600466 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600467 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600468 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600469
Mike Christie6a8a0d32006-06-28 12:00:31 -0500470 /* this is released in the dev's release function */
471 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500472 session->dev.parent = &shost->shost_gendev;
473 session->dev.release = iscsi_session_release;
474 device_initialize(&session->dev);
Mike Christieb5c7a122006-04-06 21:13:33 -0500475 if (transport->sessiondata_size)
476 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500477 return session;
478}
479EXPORT_SYMBOL_GPL(iscsi_alloc_session);
480
Mike Christie6a8a0d32006-06-28 12:00:31 -0500481int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500482{
483 struct Scsi_Host *shost = iscsi_session_to_shost(session);
484 struct iscsi_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600485 unsigned long flags;
Mike Christie8434aa82006-06-28 12:00:30 -0500486 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500487
Mike Christie30a6c652006-04-06 21:13:39 -0500488 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600489 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie6a8a0d32006-06-28 12:00:31 -0500490 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -0500491
Mike Christieb5c7a122006-04-06 21:13:33 -0500492 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
493 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500494 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600495 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600496 iscsi_cls_session_printk(KERN_ERR, session,
497 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500498 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600499 }
500 transport_register_device(&session->dev);
501
Mike Christie26974782007-12-13 12:43:29 -0600502 spin_lock_irqsave(&sesslock, flags);
503 list_add(&session->sess_list, &sesslist);
504 spin_unlock_irqrestore(&sesslock, flags);
505
Mike Christie30a6c652006-04-06 21:13:39 -0500506 mutex_lock(&ihost->mutex);
507 list_add(&session->host_list, &ihost->sessions);
508 mutex_unlock(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600509
510 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500511 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500512
Mike Christie8434aa82006-06-28 12:00:30 -0500513release_host:
514 scsi_host_put(shost);
515 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600516}
Mike Christie8434aa82006-06-28 12:00:30 -0500517EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600518
519/**
Mike Christie8434aa82006-06-28 12:00:30 -0500520 * iscsi_create_session - create iscsi class session
521 * @shost: scsi host
522 * @transport: iscsi transport
Rob Landleyeb448202007-11-03 13:30:39 -0500523 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600524 *
Mike Christie8434aa82006-06-28 12:00:30 -0500525 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500526 */
Mike Christie8434aa82006-06-28 12:00:30 -0500527struct iscsi_cls_session *
528iscsi_create_session(struct Scsi_Host *shost,
Mike Christie6a8a0d32006-06-28 12:00:31 -0500529 struct iscsi_transport *transport,
530 unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500531{
532 struct iscsi_cls_session *session;
533
534 session = iscsi_alloc_session(shost, transport);
535 if (!session)
536 return NULL;
537
Mike Christie6a8a0d32006-06-28 12:00:31 -0500538 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500539 iscsi_free_session(session);
540 return NULL;
541 }
542 return session;
543}
544EXPORT_SYMBOL_GPL(iscsi_create_session);
545
Mike Christie26974782007-12-13 12:43:29 -0600546static void iscsi_conn_release(struct device *dev)
547{
548 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
549 struct device *parent = conn->dev.parent;
550
551 kfree(conn);
552 put_device(parent);
553}
554
555static int iscsi_is_conn_dev(const struct device *dev)
556{
557 return dev->release == iscsi_conn_release;
558}
559
560static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
561{
562 if (!iscsi_is_conn_dev(dev))
563 return 0;
564 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
565}
566
Mike Christie8434aa82006-06-28 12:00:30 -0500567void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600568{
Mike Christie30a6c652006-04-06 21:13:39 -0500569 struct Scsi_Host *shost = iscsi_session_to_shost(session);
570 struct iscsi_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600571 unsigned long flags;
572 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500573
Mike Christie26974782007-12-13 12:43:29 -0600574 spin_lock_irqsave(&sesslock, flags);
575 list_del(&session->sess_list);
576 spin_unlock_irqrestore(&sesslock, flags);
577
578 /*
579 * If we are blocked let commands flow again. The lld or iscsi
580 * layer should set up the queuecommand to fail commands.
581 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600582 spin_lock_irqsave(&session->lock, flags);
583 session->state = ISCSI_SESSION_FREE;
584 spin_unlock_irqrestore(&session->lock, flags);
585 __iscsi_unblock_session(session);
Mike Christie8aae18a2008-01-31 13:36:48 -0600586 __iscsi_unbind_session(&session->unbind_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600587
588 /* flush running scans */
589 flush_workqueue(ihost->scan_workq);
Mike Christie26974782007-12-13 12:43:29 -0600590 /*
591 * If the session dropped while removing devices then we need to make
592 * sure it is not blocked
593 */
Mike Christie30a6c652006-04-06 21:13:39 -0500594 if (!cancel_delayed_work(&session->recovery_work))
Mike Christied8bf5412007-12-13 12:43:27 -0600595 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500596
Mike Christie26974782007-12-13 12:43:29 -0600597 /* hw iscsi may not have removed all connections from session */
598 err = device_for_each_child(&session->dev, NULL,
599 iscsi_iter_destroy_conn_fn);
600 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600601 iscsi_cls_session_printk(KERN_ERR, session,
602 "Could not delete all connections "
603 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500604
Mike Christie7b8631b2006-01-13 18:05:50 -0600605 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500606 device_del(&session->dev);
607}
608EXPORT_SYMBOL_GPL(iscsi_remove_session);
609
610void iscsi_free_session(struct iscsi_cls_session *session)
611{
Mike Christie26974782007-12-13 12:43:29 -0600612 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500613 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600614}
Mike Christie8434aa82006-06-28 12:00:30 -0500615EXPORT_SYMBOL_GPL(iscsi_free_session);
616
617/**
618 * iscsi_destroy_session - destroy iscsi session
619 * @session: iscsi_session
620 *
621 * Can be called by a LLD or iscsi_transport. There must not be
622 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500623 */
Mike Christie8434aa82006-06-28 12:00:30 -0500624int iscsi_destroy_session(struct iscsi_cls_session *session)
625{
626 iscsi_remove_session(session);
627 iscsi_free_session(session);
628 return 0;
629}
Mike Christie7b8631b2006-01-13 18:05:50 -0600630EXPORT_SYMBOL_GPL(iscsi_destroy_session);
631
Mike Christie7b8631b2006-01-13 18:05:50 -0600632/**
633 * iscsi_create_conn - create iscsi class connection
634 * @session: iscsi cls session
635 * @cid: connection id
636 *
637 * This can be called from a LLD or iscsi_transport. The connection
638 * is child of the session so cid must be unique for all connections
639 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500640 *
641 * Since we do not support MCS, cid will normally be zero. In some cases
642 * for software iscsi we could be trying to preallocate a connection struct
643 * in which case there could be two connection structs and cid would be
644 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500645 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600646struct iscsi_cls_conn *
647iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
648{
649 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600650 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600651 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600652 int err;
653
654 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
655 if (!conn)
656 return NULL;
657
658 if (transport->conndata_size)
659 conn->dd_data = &conn[1];
660
661 INIT_LIST_HEAD(&conn->conn_list);
662 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500663 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600664
665 /* this is released in the dev's release function */
666 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400667 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500668
Mike Christie7b8631b2006-01-13 18:05:50 -0600669 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500670 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600671 conn->dev.parent = &session->dev;
672 conn->dev.release = iscsi_conn_release;
673 err = device_register(&conn->dev);
674 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600675 iscsi_cls_session_printk(KERN_ERR, session, "could not "
676 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600677 goto release_parent_ref;
678 }
679 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600680
681 spin_lock_irqsave(&connlock, flags);
682 list_add(&conn->conn_list, &connlist);
683 conn->active = 1;
684 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600685 return conn;
686
687release_parent_ref:
688 put_device(&session->dev);
689free_conn:
690 kfree(conn);
691 return NULL;
692}
693
694EXPORT_SYMBOL_GPL(iscsi_create_conn);
695
696/**
697 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500698 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600699 *
Mike Christie26974782007-12-13 12:43:29 -0600700 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500701 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600702int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
703{
Mike Christie26974782007-12-13 12:43:29 -0600704 unsigned long flags;
705
706 spin_lock_irqsave(&connlock, flags);
707 conn->active = 0;
708 list_del(&conn->conn_list);
709 spin_unlock_irqrestore(&connlock, flags);
710
Mike Christie7b8631b2006-01-13 18:05:50 -0600711 transport_unregister_device(&conn->dev);
712 device_unregister(&conn->dev);
713 return 0;
714}
Mike Christie7b8631b2006-01-13 18:05:50 -0600715EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
716
717/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600718 * iscsi interface functions
719 */
Alex Aizman0896b752005-08-04 19:33:07 -0700720static struct iscsi_internal *
721iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Alex Aizman0896b752005-08-04 19:33:07 -0700723 struct iscsi_internal *priv;
724 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Alex Aizman0896b752005-08-04 19:33:07 -0700726 spin_lock_irqsave(&iscsi_transport_lock, flags);
727 list_for_each_entry(priv, &iscsi_transports, list) {
728 if (tt == priv->iscsi_transport) {
729 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
730 return priv;
731 }
732 }
733 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
734 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Alex Aizman0896b752005-08-04 19:33:07 -0700737static int
Mike Christie43a145a2006-10-16 18:09:38 -0400738iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500739{
Mike Christie53cb8a12006-06-28 12:00:32 -0500740 int rc;
741
Mike Christie9aaa2b42006-07-24 15:47:34 -0500742 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500743 if (rc < 0) {
Mike Christie53cb8a12006-06-28 12:00:32 -0500744 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
745 return rc;
746 }
747
Mike Christie53cb8a12006-06-28 12:00:32 -0500748 return 0;
749}
750
751static int
Mike Christie43a145a2006-10-16 18:09:38 -0400752iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700753{
Alex Aizman0896b752005-08-04 19:33:07 -0700754 int rc;
755
Mike Christie790f39a2006-05-18 20:31:39 -0500756 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700757 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700758 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
759 return rc;
760 }
761
Alex Aizman0896b752005-08-04 19:33:07 -0700762 return 0;
763}
764
Mike Christie7b7232f2006-02-01 21:06:49 -0600765int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700766 char *data, uint32_t data_size)
767{
768 struct nlmsghdr *nlh;
769 struct sk_buff *skb;
770 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700771 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500772 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700773 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
774 data_size);
775
Mike Christie790f39a2006-05-18 20:31:39 -0500776 priv = iscsi_if_transport_lookup(conn->transport);
777 if (!priv)
778 return -EINVAL;
779
Mike Christie43a145a2006-10-16 18:09:38 -0400780 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700781 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600782 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -0600783 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
784 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700785 return -ENOMEM;
786 }
787
Mike Christie790f39a2006-05-18 20:31:39 -0500788 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700789 ev = NLMSG_DATA(nlh);
790 memset(ev, 0, sizeof(*ev));
791 ev->transport_handle = iscsi_handle(conn->transport);
792 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -0500793 ev->r.recv_req.cid = conn->cid;
794 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700795 pdu = (char*)ev + sizeof(*ev);
796 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
797 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
798
Mike Christie43a145a2006-10-16 18:09:38 -0400799 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700800}
801EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
802
Mike Christie7b7232f2006-02-01 21:06:49 -0600803void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700804{
Mike Christie6eabafb2008-01-31 13:36:43 -0600805 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700806 struct nlmsghdr *nlh;
807 struct sk_buff *skb;
808 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500809 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700810 int len = NLMSG_SPACE(sizeof(*ev));
Mike Christie6eabafb2008-01-31 13:36:43 -0600811 unsigned long flags;
Alex Aizman0896b752005-08-04 19:33:07 -0700812
Mike Christie790f39a2006-05-18 20:31:39 -0500813 priv = iscsi_if_transport_lookup(conn->transport);
814 if (!priv)
815 return;
816
Mike Christie6eabafb2008-01-31 13:36:43 -0600817 spin_lock_irqsave(&session->lock, flags);
818 if (session->state == ISCSI_SESSION_LOGGED_IN)
819 session->state = ISCSI_SESSION_FAILED;
820 spin_unlock_irqrestore(&session->lock, flags);
821
Mike Christie43a145a2006-10-16 18:09:38 -0400822 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700823 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -0600824 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
825 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700826 return;
827 }
828
Mike Christie790f39a2006-05-18 20:31:39 -0500829 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700830 ev = NLMSG_DATA(nlh);
831 ev->transport_handle = iscsi_handle(conn->transport);
832 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -0700833 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500834 ev->r.connerror.cid = conn->cid;
835 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700836
Mike Christie43a145a2006-10-16 18:09:38 -0400837 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700838
Mike Christie322d7392008-01-31 13:36:52 -0600839 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
840 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700841}
842EXPORT_SYMBOL_GPL(iscsi_conn_error);
843
844static int
845iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
846 void *payload, int size)
847{
848 struct sk_buff *skb;
849 struct nlmsghdr *nlh;
850 int len = NLMSG_SPACE(size);
851 int flags = multi ? NLM_F_MULTI : 0;
852 int t = done ? NLMSG_DONE : type;
853
Mike Christie43a145a2006-10-16 18:09:38 -0400854 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -0500855 if (!skb) {
856 printk(KERN_ERR "Could not allocate skb to send reply.\n");
857 return -ENOMEM;
858 }
Alex Aizman0896b752005-08-04 19:33:07 -0700859
860 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
861 nlh->nlmsg_flags = flags;
862 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -0400863 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700864}
865
866static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600867iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700868{
869 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
870 struct iscsi_stats *stats;
871 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600872 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700873 struct nlmsghdr *nlhstat;
874 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500875 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700876 int len = NLMSG_SPACE(sizeof(*ev) +
877 sizeof(struct iscsi_stats) +
878 sizeof(struct iscsi_stats_custom) *
879 ISCSI_STATS_CUSTOM_MAX);
880 int err = 0;
881
Mike Christie790f39a2006-05-18 20:31:39 -0500882 priv = iscsi_if_transport_lookup(transport);
883 if (!priv)
884 return -EINVAL;
885
Mike Christieb5c7a122006-04-06 21:13:33 -0500886 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700887 if (!conn)
888 return -EEXIST;
889
890 do {
891 int actual_size;
892
Mike Christie43a145a2006-10-16 18:09:38 -0400893 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700894 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -0600895 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
896 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700897 return -ENOMEM;
898 }
899
Mike Christie790f39a2006-05-18 20:31:39 -0500900 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -0700901 (len - sizeof(*nlhstat)), 0);
902 evstat = NLMSG_DATA(nlhstat);
903 memset(evstat, 0, sizeof(*evstat));
904 evstat->transport_handle = iscsi_handle(conn->transport);
905 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -0500906 evstat->u.get_stats.cid =
907 ev->u.get_stats.cid;
908 evstat->u.get_stats.sid =
909 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700910 stats = (struct iscsi_stats *)
911 ((char*)evstat + sizeof(*evstat));
912 memset(stats, 0, sizeof(*stats));
913
Mike Christie7b7232f2006-02-01 21:06:49 -0600914 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700915 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
916 sizeof(struct iscsi_stats) +
917 sizeof(struct iscsi_stats_custom) *
918 stats->custom_length);
919 actual_size -= sizeof(*nlhstat);
920 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600921 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700922 nlhstat->nlmsg_len = actual_size;
923
Mike Christie43a145a2006-10-16 18:09:38 -0400924 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700925 } while (err < 0 && err != -ECONNREFUSED);
926
927 return err;
928}
929
Mike Christie53cb8a12006-06-28 12:00:32 -0500930/**
Mike Christie26974782007-12-13 12:43:29 -0600931 * iscsi_session_event - send session destr. completion event
932 * @session: iscsi class session
933 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -0500934 */
Mike Christie26974782007-12-13 12:43:29 -0600935int iscsi_session_event(struct iscsi_cls_session *session,
936 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -0500937{
938 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -0500939 struct Scsi_Host *shost;
940 struct iscsi_uevent *ev;
941 struct sk_buff *skb;
942 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -0500943 int rc, len = NLMSG_SPACE(sizeof(*ev));
944
Mike Christie26974782007-12-13 12:43:29 -0600945 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -0500946 if (!priv)
947 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -0500948 shost = iscsi_session_to_shost(session);
949
Mike Christie43a145a2006-10-16 18:09:38 -0400950 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500951 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -0600952 iscsi_cls_session_printk(KERN_ERR, session,
953 "Cannot notify userspace of session "
954 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500955 return -ENOMEM;
956 }
957
958 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
959 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -0600960 ev->transport_handle = iscsi_handle(session->transport);
961
962 ev->type = event;
963 switch (event) {
964 case ISCSI_KEVENT_DESTROY_SESSION:
965 ev->r.d_session.host_no = shost->host_no;
966 ev->r.d_session.sid = session->sid;
967 break;
968 case ISCSI_KEVENT_CREATE_SESSION:
969 ev->r.c_session_ret.host_no = shost->host_no;
970 ev->r.c_session_ret.sid = session->sid;
971 break;
972 case ISCSI_KEVENT_UNBIND_SESSION:
973 ev->r.unbind_session.host_no = shost->host_no;
974 ev->r.unbind_session.sid = session->sid;
975 break;
976 default:
Mike Christie322d7392008-01-31 13:36:52 -0600977 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
978 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -0600979 kfree_skb(skb);
980 return -EINVAL;
981 }
Mike Christie53cb8a12006-06-28 12:00:32 -0500982
983 /*
984 * this will occur if the daemon is not up, so we just warn
985 * the user and when the daemon is restarted it will handle it
986 */
Mike Christie43a145a2006-10-16 18:09:38 -0400987 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500988 if (rc < 0)
Mike Christie322d7392008-01-31 13:36:52 -0600989 iscsi_cls_session_printk(KERN_ERR, session,
990 "Cannot notify userspace of session "
991 "event %u. Check iscsi daemon\n",
992 event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500993 return rc;
994}
Mike Christie26974782007-12-13 12:43:29 -0600995EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500996
Alex Aizman0896b752005-08-04 19:33:07 -0700997static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600998iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
999{
1000 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001001 struct iscsi_cls_session *session;
Mike Christieb5c7a122006-04-06 21:13:33 -05001002 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -06001003
Mike Christie7996a772006-04-06 21:13:41 -05001004 session = transport->create_session(transport, &priv->t,
Mike Christie15482712007-05-30 12:57:19 -05001005 ev->u.c_session.cmds_max,
1006 ev->u.c_session.queue_depth,
Mike Christie7b7232f2006-02-01 21:06:49 -06001007 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -05001008 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -06001009 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001010 return -ENOMEM;
1011
Mike Christieb5c7a122006-04-06 21:13:33 -05001012 ev->r.c_session_ret.host_no = hostno;
1013 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001014 return 0;
1015}
1016
1017static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001018iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001019{
Mike Christie7b8631b2006-01-13 18:05:50 -06001020 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001021 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001022
Mike Christieb5c7a122006-04-06 21:13:33 -05001023 session = iscsi_session_lookup(ev->u.c_conn.sid);
1024 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001025 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001026 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001027 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001028 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001029
Mike Christie7b7232f2006-02-01 21:06:49 -06001030 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001031 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001032 iscsi_cls_session_printk(KERN_ERR, session,
1033 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001034 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001035 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001036
Mike Christieb5c7a122006-04-06 21:13:33 -05001037 ev->r.c_conn_ret.sid = session->sid;
1038 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001039 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001040}
1041
1042static int
1043iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1044{
Mike Christie7b8631b2006-01-13 18:05:50 -06001045 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001046
Mike Christieb5c7a122006-04-06 21:13:33 -05001047 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001048 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001049 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001050
Mike Christie7b8631b2006-01-13 18:05:50 -06001051 if (transport->destroy_conn)
1052 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001053 return 0;
1054}
1055
Mike Christiefd7255f2006-04-06 21:13:36 -05001056static int
1057iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1058{
1059 char *data = (char*)ev + sizeof(*ev);
1060 struct iscsi_cls_conn *conn;
1061 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001062 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001063
1064 session = iscsi_session_lookup(ev->u.set_param.sid);
1065 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1066 if (!conn || !session)
1067 return -EINVAL;
1068
1069 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001070 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001071 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001072 if (value != 0)
1073 session->recovery_tmo = value;
1074 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001075 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001076 err = transport->set_param(conn, ev->u.set_param.param,
1077 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001078 }
1079
1080 return err;
1081}
1082
Mike Christie7b8631b2006-01-13 18:05:50 -06001083static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001084iscsi_if_transport_ep(struct iscsi_transport *transport,
1085 struct iscsi_uevent *ev, int msg_type)
1086{
1087 struct sockaddr *dst_addr;
1088 int rc = 0;
1089
1090 switch (msg_type) {
1091 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1092 if (!transport->ep_connect)
1093 return -EINVAL;
1094
1095 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1096 rc = transport->ep_connect(dst_addr,
1097 ev->u.ep_connect.non_blocking,
1098 &ev->r.ep_connect_ret.handle);
1099 break;
1100 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1101 if (!transport->ep_poll)
1102 return -EINVAL;
1103
1104 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1105 ev->u.ep_poll.timeout_ms);
1106 break;
1107 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1108 if (!transport->ep_disconnect)
1109 return -EINVAL;
1110
1111 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1112 break;
1113 }
1114 return rc;
1115}
1116
1117static int
Mike Christie01cb2252006-06-28 12:00:22 -05001118iscsi_tgt_dscvr(struct iscsi_transport *transport,
1119 struct iscsi_uevent *ev)
1120{
Mike Christie2174a042007-05-30 12:57:10 -05001121 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001122 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001123 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001124
1125 if (!transport->tgt_dscvr)
1126 return -EINVAL;
1127
Mike Christie2174a042007-05-30 12:57:10 -05001128 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1129 if (IS_ERR(shost)) {
1130 printk(KERN_ERR "target discovery could not find host no %u\n",
1131 ev->u.tgt_dscvr.host_no);
1132 return -ENODEV;
1133 }
1134
1135
Mike Christie01cb2252006-06-28 12:00:22 -05001136 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001137 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1138 ev->u.tgt_dscvr.enable, dst_addr);
1139 scsi_host_put(shost);
1140 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001141}
1142
1143static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001144iscsi_set_host_param(struct iscsi_transport *transport,
1145 struct iscsi_uevent *ev)
1146{
1147 char *data = (char*)ev + sizeof(*ev);
1148 struct Scsi_Host *shost;
1149 int err;
1150
1151 if (!transport->set_host_param)
1152 return -ENOSYS;
1153
1154 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1155 if (IS_ERR(shost)) {
1156 printk(KERN_ERR "set_host_param could not find host no %u\n",
1157 ev->u.set_host_param.host_no);
1158 return -ENODEV;
1159 }
1160
1161 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1162 data, ev->u.set_host_param.len);
1163 scsi_host_put(shost);
1164 return err;
1165}
1166
1167static int
Alex Aizman0896b752005-08-04 19:33:07 -07001168iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1169{
1170 int err = 0;
1171 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1172 struct iscsi_transport *transport = NULL;
1173 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001174 struct iscsi_cls_session *session;
1175 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001176
Alex Aizman0896b752005-08-04 19:33:07 -07001177 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1178 if (!priv)
1179 return -EINVAL;
1180 transport = priv->iscsi_transport;
1181
Mike Christie7b7232f2006-02-01 21:06:49 -06001182 if (!try_module_get(transport->owner))
1183 return -EINVAL;
1184
Mike Christie790f39a2006-05-18 20:31:39 -05001185 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1186
Alex Aizman0896b752005-08-04 19:33:07 -07001187 switch (nlh->nlmsg_type) {
1188 case ISCSI_UEVENT_CREATE_SESSION:
1189 err = iscsi_if_create_session(priv, ev);
1190 break;
1191 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001192 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001193 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001194 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001195 else
1196 err = -EINVAL;
1197 break;
1198 case ISCSI_UEVENT_UNBIND_SESSION:
1199 session = iscsi_session_lookup(ev->u.d_session.sid);
1200 if (session)
1201 iscsi_unbind_session(session);
1202 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001203 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001204 break;
1205 case ISCSI_UEVENT_CREATE_CONN:
1206 err = iscsi_if_create_conn(transport, ev);
1207 break;
1208 case ISCSI_UEVENT_DESTROY_CONN:
1209 err = iscsi_if_destroy_conn(transport, ev);
1210 break;
1211 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001212 session = iscsi_session_lookup(ev->u.b_conn.sid);
1213 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001214
1215 if (session && conn)
1216 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001217 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001218 ev->u.b_conn.is_leading);
1219 else
1220 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001221 break;
1222 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001223 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001224 break;
1225 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001226 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001227 if (conn)
1228 ev->r.retcode = transport->start_conn(conn);
1229 else
1230 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001231 break;
1232 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001233 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001234 if (conn)
1235 transport->stop_conn(conn, ev->u.stop_conn.flag);
1236 else
1237 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001238 break;
1239 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001240 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001241 if (conn)
1242 ev->r.retcode = transport->send_pdu(conn,
1243 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1244 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1245 ev->u.send_pdu.data_size);
1246 else
1247 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001248 break;
1249 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001250 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001251 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001252 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1253 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1254 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1255 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1256 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001257 case ISCSI_UEVENT_TGT_DSCVR:
1258 err = iscsi_tgt_dscvr(transport, ev);
1259 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001260 case ISCSI_UEVENT_SET_HOST_PARAM:
1261 err = iscsi_set_host_param(transport, ev);
1262 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001263 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001264 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001265 break;
1266 }
1267
Mike Christie7b7232f2006-02-01 21:06:49 -06001268 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001269 return err;
1270}
1271
Mike Christieb5c7a122006-04-06 21:13:33 -05001272/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001273 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1274 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001275 */
Alex Aizman0896b752005-08-04 19:33:07 -07001276static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001277iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001278{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001279 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001280 while (skb->len >= NLMSG_SPACE(0)) {
1281 int err;
1282 uint32_t rlen;
1283 struct nlmsghdr *nlh;
1284 struct iscsi_uevent *ev;
1285
1286 nlh = nlmsg_hdr(skb);
1287 if (nlh->nlmsg_len < sizeof(*nlh) ||
1288 skb->len < nlh->nlmsg_len) {
1289 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001290 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001291
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001292 ev = NLMSG_DATA(nlh);
1293 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1294 if (rlen > skb->len)
1295 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001296
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001297 err = iscsi_if_recv_msg(skb, nlh);
1298 if (err) {
1299 ev->type = ISCSI_KEVENT_IF_ERROR;
1300 ev->iferror = err;
1301 }
1302 do {
1303 /*
1304 * special case for GET_STATS:
1305 * on success - sending reply and stats from
1306 * inside of if_recv_msg(),
1307 * on error - fall through.
1308 */
1309 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001310 break;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001311 err = iscsi_if_send_reply(
1312 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1313 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1314 } while (err < 0 && err != -ECONNREFUSED);
1315 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001316 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001317 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001318}
1319
Mike Christie7b8631b2006-01-13 18:05:50 -06001320#define iscsi_cdev_to_conn(_cdev) \
1321 iscsi_dev_to_conn(_cdev->dev)
1322
Mike Christiefd7255f2006-04-06 21:13:36 -05001323#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1324struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1325 __ATTR(_name,_mode,_show,_store)
1326
Alex Aizman0896b752005-08-04 19:33:07 -07001327/*
1328 * iSCSI connection attrs
1329 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001330#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001332show_conn_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001333{ \
1334 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1335 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001336 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001337}
1338
Mike Christiea54a52c2006-06-28 12:00:23 -05001339#define iscsi_conn_attr(field, param) \
1340 iscsi_conn_attr_show(param) \
1341static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001342 NULL);
1343
Mike Christiea54a52c2006-06-28 12:00:23 -05001344iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1345iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1346iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1347iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1348iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1349iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1350iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1351iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1352iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1353iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1354iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001355iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1356iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Mike Christie7b8631b2006-01-13 18:05:50 -06001358#define iscsi_cdev_to_session(_cdev) \
1359 iscsi_dev_to_session(_cdev->dev)
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/*
Alex Aizman0896b752005-08-04 19:33:07 -07001362 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
Mike Christieb2c64162007-05-30 12:57:16 -05001364#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001366show_session_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001367{ \
1368 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1369 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001370 \
1371 if (perm && !capable(CAP_SYS_ADMIN)) \
1372 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001373 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Mike Christieb2c64162007-05-30 12:57:16 -05001376#define iscsi_session_attr(field, param, perm) \
1377 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001378static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001379 NULL);
1380
Mike Christieb2c64162007-05-30 12:57:16 -05001381iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1382iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1383iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1384iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1385iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1386iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1387iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1388iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1389iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1390iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1391iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1392iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1393iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1394iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001395iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1396iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1397iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05001398
Mike Christie6eabafb2008-01-31 13:36:43 -06001399static ssize_t
1400show_priv_session_state(struct class_device *cdev, char *buf)
1401{
1402 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);
1403 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1404}
1405static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1406 NULL);
1407
Mike Christiefd7255f2006-04-06 21:13:36 -05001408#define iscsi_priv_session_attr_show(field, format) \
1409static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001410show_priv_session_##field(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001411{ \
Mike Christiea54a52c2006-06-28 12:00:23 -05001412 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
Mike Christiefd7255f2006-04-06 21:13:36 -05001413 return sprintf(buf, format"\n", session->field); \
1414}
1415
1416#define iscsi_priv_session_attr(field, format) \
1417 iscsi_priv_session_attr_show(field, format) \
1418static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1419 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001420iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001421
Mike Christie1819dc82007-05-30 12:57:08 -05001422/*
1423 * iSCSI host attrs
1424 */
1425#define iscsi_host_attr_show(param) \
1426static ssize_t \
1427show_host_param_##param(struct class_device *cdev, char *buf) \
1428{ \
1429 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1430 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1431 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1432}
1433
1434#define iscsi_host_attr(field, param) \
1435 iscsi_host_attr_show(param) \
1436static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1437 NULL);
1438
Mike Christied8196ed2007-05-30 12:57:25 -05001439iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001440iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001441iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001442iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001443
Mike Christiefd7255f2006-04-06 21:13:36 -05001444#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1445do { \
1446 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1447 count++; \
1448} while (0)
1449
Mike Christiea54a52c2006-06-28 12:00:23 -05001450
Mike Christiefd7255f2006-04-06 21:13:36 -05001451#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1452do { \
1453 if (tt->param_mask & param_flag) { \
1454 priv->session_attrs[count] = &class_device_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001456 } \
1457} while (0)
1458
Mike Christiefd7255f2006-04-06 21:13:36 -05001459#define SETUP_CONN_RD_ATTR(field, param_flag) \
1460do { \
1461 if (tt->param_mask & param_flag) { \
1462 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1463 count++; \
1464 } \
1465} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Mike Christie1819dc82007-05-30 12:57:08 -05001467#define SETUP_HOST_RD_ATTR(field, param_flag) \
1468do { \
1469 if (tt->host_param_mask & param_flag) { \
1470 priv->host_attrs[count] = &class_device_attr_host_##field; \
1471 count++; \
1472 } \
1473} while (0)
1474
Alex Aizman0896b752005-08-04 19:33:07 -07001475static int iscsi_session_match(struct attribute_container *cont,
1476 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Mike Christie7b8631b2006-01-13 18:05:50 -06001478 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001480 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Alex Aizman0896b752005-08-04 19:33:07 -07001482 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
1484
Mike Christie7b8631b2006-01-13 18:05:50 -06001485 session = iscsi_dev_to_session(dev);
1486 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001487 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489
Alex Aizman0896b752005-08-04 19:33:07 -07001490 priv = to_iscsi_internal(shost->transportt);
1491 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1492 return 0;
1493
1494 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Alex Aizman0896b752005-08-04 19:33:07 -07001497static int iscsi_conn_match(struct attribute_container *cont,
1498 struct device *dev)
1499{
Mike Christie7b8631b2006-01-13 18:05:50 -06001500 struct iscsi_cls_session *session;
1501 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001502 struct Scsi_Host *shost;
1503 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Alex Aizman0896b752005-08-04 19:33:07 -07001505 if (!iscsi_is_conn_dev(dev))
1506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Mike Christie7b8631b2006-01-13 18:05:50 -06001508 conn = iscsi_dev_to_conn(dev);
1509 session = iscsi_dev_to_session(conn->dev.parent);
1510 shost = iscsi_session_to_shost(session);
1511
Alex Aizman0896b752005-08-04 19:33:07 -07001512 if (!shost->transportt)
1513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Alex Aizman0896b752005-08-04 19:33:07 -07001515 priv = to_iscsi_internal(shost->transportt);
1516 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1517 return 0;
1518
1519 return &priv->conn_cont.ac == cont;
1520}
1521
Mike Christie30a6c652006-04-06 21:13:39 -05001522static int iscsi_host_match(struct attribute_container *cont,
1523 struct device *dev)
1524{
1525 struct Scsi_Host *shost;
1526 struct iscsi_internal *priv;
1527
1528 if (!scsi_is_host_device(dev))
1529 return 0;
1530
1531 shost = dev_to_shost(dev);
1532 if (!shost->transportt ||
1533 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1534 return 0;
1535
1536 priv = to_iscsi_internal(shost->transportt);
1537 return &priv->t.host_attrs.ac == cont;
1538}
1539
Mike Christie7b8631b2006-01-13 18:05:50 -06001540struct scsi_transport_template *
1541iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001542{
1543 struct iscsi_internal *priv;
1544 unsigned long flags;
1545 int count = 0, err;
1546
1547 BUG_ON(!tt);
1548
1549 priv = iscsi_if_transport_lookup(tt);
1550 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001551 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001552
Jes Sorensen24669f752006-01-16 10:31:18 -05001553 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001554 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001555 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001556 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001557 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001558 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001559 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001560
1561 priv->cdev.class = &iscsi_transport_class;
1562 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1563 err = class_device_register(&priv->cdev);
1564 if (err)
1565 goto free_priv;
1566
1567 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1568 if (err)
1569 goto unregister_cdev;
1570
Mike Christie30a6c652006-04-06 21:13:39 -05001571 /* host parameters */
1572 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1573 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1574 priv->t.host_attrs.ac.match = iscsi_host_match;
1575 priv->t.host_size = sizeof(struct iscsi_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001576 transport_container_register(&priv->t.host_attrs);
1577
Mike Christied8196ed2007-05-30 12:57:25 -05001578 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001579 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001580 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001581 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001582 BUG_ON(count > ISCSI_HOST_ATTRS);
1583 priv->host_attrs[count] = NULL;
1584 count = 0;
1585
Alex Aizman0896b752005-08-04 19:33:07 -07001586 /* connection parameters */
1587 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1588 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1589 priv->conn_cont.ac.match = iscsi_conn_match;
1590 transport_container_register(&priv->conn_cont);
1591
Mike Christiefd7255f2006-04-06 21:13:36 -05001592 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1593 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1594 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1595 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1596 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1597 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1598 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1599 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001600 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001601 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1602 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001603 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1604 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001605
1606 BUG_ON(count > ISCSI_CONN_ATTRS);
1607 priv->conn_attrs[count] = NULL;
1608 count = 0;
1609
1610 /* session parameters */
1611 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1612 priv->session_cont.ac.class = &iscsi_session_class.class;
1613 priv->session_cont.ac.match = iscsi_session_match;
1614 transport_container_register(&priv->session_cont);
1615
Mike Christiefd7255f2006-04-06 21:13:36 -05001616 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1617 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1618 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1619 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1620 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1621 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1622 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1623 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001624 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1625 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001626 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1627 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1628 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1629 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001630 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001631 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1632 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie30a6c652006-04-06 21:13:39 -05001633 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06001634 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001637 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Alex Aizman0896b752005-08-04 19:33:07 -07001639 spin_lock_irqsave(&iscsi_transport_lock, flags);
1640 list_add(&priv->list, &iscsi_transports);
1641 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Alex Aizman0896b752005-08-04 19:33:07 -07001643 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001644 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Alex Aizman0896b752005-08-04 19:33:07 -07001646unregister_cdev:
1647 class_device_unregister(&priv->cdev);
1648free_priv:
1649 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001650 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
Alex Aizman0896b752005-08-04 19:33:07 -07001652EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Alex Aizman0896b752005-08-04 19:33:07 -07001654int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Alex Aizman0896b752005-08-04 19:33:07 -07001656 struct iscsi_internal *priv;
1657 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Alex Aizman0896b752005-08-04 19:33:07 -07001659 BUG_ON(!tt);
1660
Arjan van de Ven0b950672006-01-11 13:16:10 +01001661 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001662
1663 priv = iscsi_if_transport_lookup(tt);
1664 BUG_ON (!priv);
1665
Alex Aizman0896b752005-08-04 19:33:07 -07001666 spin_lock_irqsave(&iscsi_transport_lock, flags);
1667 list_del(&priv->list);
1668 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1669
1670 transport_container_unregister(&priv->conn_cont);
1671 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001672 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001673
1674 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1675 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001676 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001677
1678 return 0;
1679}
1680EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682static __init int iscsi_transport_init(void)
1683{
Alex Aizman0896b752005-08-04 19:33:07 -07001684 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Meelis Roos09492602006-12-17 12:10:26 -06001686 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001687 ISCSI_TRANSPORT_VERSION);
1688
Mike Christie41be1442007-02-28 17:32:18 -06001689 atomic_set(&iscsi_session_nr, 0);
1690
Alex Aizman0896b752005-08-04 19:33:07 -07001691 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (err)
1693 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001694
Mike Christie30a6c652006-04-06 21:13:39 -05001695 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001696 if (err)
1697 goto unregister_transport_class;
1698
Mike Christie30a6c652006-04-06 21:13:39 -05001699 err = transport_class_register(&iscsi_connection_class);
1700 if (err)
1701 goto unregister_host_class;
1702
Alex Aizman0896b752005-08-04 19:33:07 -07001703 err = transport_class_register(&iscsi_session_class);
1704 if (err)
1705 goto unregister_conn_class;
1706
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001707 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
Mike Christie7b8631b2006-01-13 18:05:50 -06001708 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001709 if (!nls) {
1710 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001711 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001712 }
1713
Mike Christied8bf5412007-12-13 12:43:27 -06001714 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1715 if (!iscsi_eh_timer_workq)
1716 goto release_nls;
1717
Mike Christie43a145a2006-10-16 18:09:38 -04001718 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001719
Mike Christied8bf5412007-12-13 12:43:27 -06001720release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001721 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001722unregister_session_class:
1723 transport_class_unregister(&iscsi_session_class);
1724unregister_conn_class:
1725 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001726unregister_host_class:
1727 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001728unregister_transport_class:
1729 class_unregister(&iscsi_transport_class);
1730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733static void __exit iscsi_transport_exit(void)
1734{
Mike Christied8bf5412007-12-13 12:43:27 -06001735 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001736 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001737 transport_class_unregister(&iscsi_connection_class);
1738 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001739 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001740 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743module_init(iscsi_transport_init);
1744module_exit(iscsi_transport_exit);
1745
Alex Aizman0896b752005-08-04 19:33:07 -07001746MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1747 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1748 "Alex Aizman <itn780@yahoo.com>");
1749MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05001751MODULE_VERSION(ISCSI_TRANSPORT_VERSION);