blob: 5ec2fbea8937abdf81bd7f137ac4e1b287161d42 [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 Christie30a6c652006-04-06 21:13:39 -050033#define ISCSI_SESSION_ATTRS 11
Mike Christie8d2860b2006-05-02 19:46:47 -050034#define ISCSI_CONN_ATTRS 11
Mike Christie8ad57812007-05-30 12:57:13 -050035#define ISCSI_HOST_ATTRS 2
Mike Christie82a0d7b2006-11-08 15:58:34 -060036#define ISCSI_TRANSPORT_VERSION "2.0-724"
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 Christieb5c7a122006-04-06 21:13:33 -050053
Alex Aizman0896b752005-08-04 19:33:07 -070054/*
55 * list of registered transports and lock that must
56 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010057 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070058 */
59static LIST_HEAD(iscsi_transports);
60static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Alex Aizman0896b752005-08-04 19:33:07 -070062#define to_iscsi_internal(tmpl) \
63 container_of(tmpl, struct iscsi_internal, t)
64
65#define cdev_to_iscsi_internal(_cdev) \
66 container_of(_cdev, struct iscsi_internal, cdev)
67
68static void iscsi_transport_release(struct class_device *cdev)
69{
70 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
71 kfree(priv);
72}
73
74/*
75 * iscsi_transport_class represents the iscsi_transports that are
76 * registered.
77 */
78static struct class iscsi_transport_class = {
79 .name = "iscsi_transport",
80 .release = iscsi_transport_release,
81};
82
83static ssize_t
84show_transport_handle(struct class_device *cdev, char *buf)
85{
86 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
Mike Christie762e2bf2005-09-12 21:01:46 -050087 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070088}
89static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
90
91#define show_transport_attr(name, format) \
92static ssize_t \
93show_transport_##name(struct class_device *cdev, char *buf) \
94{ \
95 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
96 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
97} \
98static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
99
100show_transport_attr(caps, "0x%x");
101show_transport_attr(max_lun, "%d");
102show_transport_attr(max_conn, "%d");
103show_transport_attr(max_cmd_len, "%d");
104
105static struct attribute *iscsi_transport_attrs[] = {
106 &class_device_attr_handle.attr,
107 &class_device_attr_caps.attr,
108 &class_device_attr_max_lun.attr,
109 &class_device_attr_max_conn.attr,
110 &class_device_attr_max_cmd_len.attr,
111 NULL,
112};
113
114static struct attribute_group iscsi_transport_group = {
115 .attrs = iscsi_transport_attrs,
116};
117
Mike Christie30a6c652006-04-06 21:13:39 -0500118static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
119 struct class_device *cdev)
120{
121 struct Scsi_Host *shost = dev_to_shost(dev);
122 struct iscsi_host *ihost = shost->shost_data;
123
124 memset(ihost, 0, sizeof(*ihost));
125 INIT_LIST_HEAD(&ihost->sessions);
126 mutex_init(&ihost->mutex);
127 return 0;
128}
129
130static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
131 "iscsi_host",
132 iscsi_setup_host,
133 NULL,
134 NULL);
135
Alex Aizman0896b752005-08-04 19:33:07 -0700136static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
137 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 NULL,
139 NULL,
140 NULL);
141
Alex Aizman0896b752005-08-04 19:33:07 -0700142static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
143 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 NULL,
145 NULL,
146 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700147
148static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100149static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700150
Mike Christie7b7232f2006-02-01 21:06:49 -0600151static LIST_HEAD(sesslist);
152static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700153static LIST_HEAD(connlist);
154static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Mike Christieb5c7a122006-04-06 21:13:33 -0500156static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
157{
158 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
159 return sess->sid;
160}
161
162/*
163 * Returns the matching session to a given sid
164 */
165static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600166{
167 unsigned long flags;
168 struct iscsi_cls_session *sess;
169
170 spin_lock_irqsave(&sesslock, flags);
171 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500172 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600173 spin_unlock_irqrestore(&sesslock, flags);
174 return sess;
175 }
176 }
177 spin_unlock_irqrestore(&sesslock, flags);
178 return NULL;
179}
180
Mike Christieb5c7a122006-04-06 21:13:33 -0500181/*
182 * Returns the matching connection to a given sid / cid tuple
183 */
184static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600185{
186 unsigned long flags;
187 struct iscsi_cls_conn *conn;
188
189 spin_lock_irqsave(&connlock, flags);
190 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500191 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600192 spin_unlock_irqrestore(&connlock, flags);
193 return conn;
194 }
195 }
196 spin_unlock_irqrestore(&connlock, flags);
197 return NULL;
198}
199
Mike Christie7b8631b2006-01-13 18:05:50 -0600200/*
201 * The following functions can be used by LLDs that allocate
202 * their own scsi_hosts or by software iscsi LLDs
203 */
204static void iscsi_session_release(struct device *dev)
205{
206 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600207 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Mike Christie7b8631b2006-01-13 18:05:50 -0600209 shost = iscsi_session_to_shost(session);
210 scsi_host_put(shost);
211 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Mike Christie7b8631b2006-01-13 18:05:50 -0600214static int iscsi_is_session_dev(const struct device *dev)
215{
216 return dev->release == iscsi_session_release;
217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Mike Christie30a6c652006-04-06 21:13:39 -0500219static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
220 uint id, uint lun)
221{
222 struct iscsi_host *ihost = shost->shost_data;
223 struct iscsi_cls_session *session;
224
225 mutex_lock(&ihost->mutex);
226 list_for_each_entry(session, &ihost->sessions, host_list) {
Mike Christiee6f3b632006-06-28 12:00:29 -0500227 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Mike Christie30a6c652006-04-06 21:13:39 -0500228 (id == SCAN_WILD_CARD || id == session->target_id))
Mike Christiee6f3b632006-06-28 12:00:29 -0500229 scsi_scan_target(&session->dev, 0,
Mike Christie30a6c652006-04-06 21:13:39 -0500230 session->target_id, lun, 1);
231 }
232 mutex_unlock(&ihost->mutex);
233
234 return 0;
235}
236
David Howellsc4028952006-11-22 14:57:56 +0000237static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500238{
David Howellsc4028952006-11-22 14:57:56 +0000239 struct iscsi_cls_session *session =
240 container_of(work, struct iscsi_cls_session,
241 recovery_work.work);
Mike Christie30a6c652006-04-06 21:13:39 -0500242
Or Gerlitzbe2df722006-05-02 19:46:43 -0500243 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
244 "out after %d secs\n", session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500245
246 if (session->transport->session_recovery_timedout)
247 session->transport->session_recovery_timedout(session);
248
249 scsi_target_unblock(&session->dev);
250}
251
252void iscsi_unblock_session(struct iscsi_cls_session *session)
253{
254 if (!cancel_delayed_work(&session->recovery_work))
255 flush_scheduled_work();
256 scsi_target_unblock(&session->dev);
257}
258EXPORT_SYMBOL_GPL(iscsi_unblock_session);
259
260void iscsi_block_session(struct iscsi_cls_session *session)
261{
262 scsi_target_block(&session->dev);
263 schedule_delayed_work(&session->recovery_work,
264 session->recovery_tmo * HZ);
265}
266EXPORT_SYMBOL_GPL(iscsi_block_session);
267
Mike Christie7b8631b2006-01-13 18:05:50 -0600268struct iscsi_cls_session *
Mike Christie8434aa82006-06-28 12:00:30 -0500269iscsi_alloc_session(struct Scsi_Host *shost,
270 struct iscsi_transport *transport)
Mike Christie7b8631b2006-01-13 18:05:50 -0600271{
272 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Mike Christieb5c7a122006-04-06 21:13:33 -0500274 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
275 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600276 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500277 return NULL;
278
Mike Christie7b8631b2006-01-13 18:05:50 -0600279 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500280 session->recovery_tmo = 120;
David Howellsc4028952006-11-22 14:57:56 +0000281 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500282 INIT_LIST_HEAD(&session->host_list);
283 INIT_LIST_HEAD(&session->sess_list);
Mike Christie7b8631b2006-01-13 18:05:50 -0600284
Mike Christie6a8a0d32006-06-28 12:00:31 -0500285 /* this is released in the dev's release function */
286 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500287 session->dev.parent = &shost->shost_gendev;
288 session->dev.release = iscsi_session_release;
289 device_initialize(&session->dev);
Mike Christieb5c7a122006-04-06 21:13:33 -0500290 if (transport->sessiondata_size)
291 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500292 return session;
293}
294EXPORT_SYMBOL_GPL(iscsi_alloc_session);
295
Mike Christie6a8a0d32006-06-28 12:00:31 -0500296int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500297{
298 struct Scsi_Host *shost = iscsi_session_to_shost(session);
299 struct iscsi_host *ihost;
300 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500301
Mike Christie30a6c652006-04-06 21:13:39 -0500302 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600303 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie6a8a0d32006-06-28 12:00:31 -0500304 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -0500305
Mike Christieb5c7a122006-04-06 21:13:33 -0500306 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
307 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500308 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600309 if (err) {
310 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
311 "register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500312 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600313 }
314 transport_register_device(&session->dev);
315
Mike Christie30a6c652006-04-06 21:13:39 -0500316 mutex_lock(&ihost->mutex);
317 list_add(&session->host_list, &ihost->sessions);
318 mutex_unlock(&ihost->mutex);
Mike Christie8434aa82006-06-28 12:00:30 -0500319 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500320
Mike Christie8434aa82006-06-28 12:00:30 -0500321release_host:
322 scsi_host_put(shost);
323 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600324}
Mike Christie8434aa82006-06-28 12:00:30 -0500325EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600326
327/**
Mike Christie8434aa82006-06-28 12:00:30 -0500328 * iscsi_create_session - create iscsi class session
329 * @shost: scsi host
330 * @transport: iscsi transport
Mike Christie7b8631b2006-01-13 18:05:50 -0600331 *
Mike Christie8434aa82006-06-28 12:00:30 -0500332 * This can be called from a LLD or iscsi_transport.
Mike Christie7b8631b2006-01-13 18:05:50 -0600333 **/
Mike Christie8434aa82006-06-28 12:00:30 -0500334struct iscsi_cls_session *
335iscsi_create_session(struct Scsi_Host *shost,
Mike Christie6a8a0d32006-06-28 12:00:31 -0500336 struct iscsi_transport *transport,
337 unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500338{
339 struct iscsi_cls_session *session;
340
341 session = iscsi_alloc_session(shost, transport);
342 if (!session)
343 return NULL;
344
Mike Christie6a8a0d32006-06-28 12:00:31 -0500345 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500346 iscsi_free_session(session);
347 return NULL;
348 }
349 return session;
350}
351EXPORT_SYMBOL_GPL(iscsi_create_session);
352
353void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600354{
Mike Christie30a6c652006-04-06 21:13:39 -0500355 struct Scsi_Host *shost = iscsi_session_to_shost(session);
356 struct iscsi_host *ihost = shost->shost_data;
357
358 if (!cancel_delayed_work(&session->recovery_work))
359 flush_scheduled_work();
360
361 mutex_lock(&ihost->mutex);
362 list_del(&session->host_list);
363 mutex_unlock(&ihost->mutex);
364
Mike Christie8434aa82006-06-28 12:00:30 -0500365 scsi_remove_target(&session->dev);
366
Mike Christie7b8631b2006-01-13 18:05:50 -0600367 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500368 device_del(&session->dev);
369}
370EXPORT_SYMBOL_GPL(iscsi_remove_session);
371
372void iscsi_free_session(struct iscsi_cls_session *session)
373{
374 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600375}
376
Mike Christie8434aa82006-06-28 12:00:30 -0500377EXPORT_SYMBOL_GPL(iscsi_free_session);
378
379/**
380 * iscsi_destroy_session - destroy iscsi session
381 * @session: iscsi_session
382 *
383 * Can be called by a LLD or iscsi_transport. There must not be
384 * any running connections.
385 **/
386int iscsi_destroy_session(struct iscsi_cls_session *session)
387{
388 iscsi_remove_session(session);
389 iscsi_free_session(session);
390 return 0;
391}
Mike Christie7b8631b2006-01-13 18:05:50 -0600392EXPORT_SYMBOL_GPL(iscsi_destroy_session);
393
394static void iscsi_conn_release(struct device *dev)
395{
396 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
397 struct device *parent = conn->dev.parent;
398
399 kfree(conn);
400 put_device(parent);
401}
402
403static int iscsi_is_conn_dev(const struct device *dev)
404{
405 return dev->release == iscsi_conn_release;
406}
407
408/**
409 * iscsi_create_conn - create iscsi class connection
410 * @session: iscsi cls session
411 * @cid: connection id
412 *
413 * This can be called from a LLD or iscsi_transport. The connection
414 * is child of the session so cid must be unique for all connections
415 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500416 *
417 * Since we do not support MCS, cid will normally be zero. In some cases
418 * for software iscsi we could be trying to preallocate a connection struct
419 * in which case there could be two connection structs and cid would be
420 * non-zero.
Mike Christie7b8631b2006-01-13 18:05:50 -0600421 **/
422struct iscsi_cls_conn *
423iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
424{
425 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600426 struct iscsi_cls_conn *conn;
427 int err;
428
429 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
430 if (!conn)
431 return NULL;
432
433 if (transport->conndata_size)
434 conn->dd_data = &conn[1];
435
436 INIT_LIST_HEAD(&conn->conn_list);
437 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500438 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600439
440 /* this is released in the dev's release function */
441 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400442 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500443
Mike Christie7b8631b2006-01-13 18:05:50 -0600444 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500445 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600446 conn->dev.parent = &session->dev;
447 conn->dev.release = iscsi_conn_release;
448 err = device_register(&conn->dev);
449 if (err) {
450 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
451 "connection's dev\n");
452 goto release_parent_ref;
453 }
454 transport_register_device(&conn->dev);
455 return conn;
456
457release_parent_ref:
458 put_device(&session->dev);
459free_conn:
460 kfree(conn);
461 return NULL;
462}
463
464EXPORT_SYMBOL_GPL(iscsi_create_conn);
465
466/**
467 * iscsi_destroy_conn - destroy iscsi class connection
468 * @session: iscsi cls session
469 *
470 * This can be called from a LLD or iscsi_transport.
471 **/
472int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
473{
474 transport_unregister_device(&conn->dev);
475 device_unregister(&conn->dev);
476 return 0;
477}
478
479EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
480
481/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600482 * iscsi interface functions
483 */
Alex Aizman0896b752005-08-04 19:33:07 -0700484static struct iscsi_internal *
485iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Alex Aizman0896b752005-08-04 19:33:07 -0700487 struct iscsi_internal *priv;
488 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Alex Aizman0896b752005-08-04 19:33:07 -0700490 spin_lock_irqsave(&iscsi_transport_lock, flags);
491 list_for_each_entry(priv, &iscsi_transports, list) {
492 if (tt == priv->iscsi_transport) {
493 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
494 return priv;
495 }
496 }
497 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
498 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Alex Aizman0896b752005-08-04 19:33:07 -0700501static int
Mike Christie43a145a2006-10-16 18:09:38 -0400502iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500503{
Mike Christie53cb8a12006-06-28 12:00:32 -0500504 int rc;
505
Mike Christie9aaa2b42006-07-24 15:47:34 -0500506 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500507 if (rc < 0) {
Mike Christie53cb8a12006-06-28 12:00:32 -0500508 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
509 return rc;
510 }
511
Mike Christie53cb8a12006-06-28 12:00:32 -0500512 return 0;
513}
514
515static int
Mike Christie43a145a2006-10-16 18:09:38 -0400516iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700517{
Alex Aizman0896b752005-08-04 19:33:07 -0700518 int rc;
519
Mike Christie790f39a2006-05-18 20:31:39 -0500520 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700521 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700522 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
523 return rc;
524 }
525
Alex Aizman0896b752005-08-04 19:33:07 -0700526 return 0;
527}
528
Mike Christie7b7232f2006-02-01 21:06:49 -0600529int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700530 char *data, uint32_t data_size)
531{
532 struct nlmsghdr *nlh;
533 struct sk_buff *skb;
534 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700535 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500536 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700537 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
538 data_size);
539
Mike Christie790f39a2006-05-18 20:31:39 -0500540 priv = iscsi_if_transport_lookup(conn->transport);
541 if (!priv)
542 return -EINVAL;
543
Mike Christie43a145a2006-10-16 18:09:38 -0400544 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700545 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600546 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600547 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
548 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700549 return -ENOMEM;
550 }
551
Mike Christie790f39a2006-05-18 20:31:39 -0500552 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700553 ev = NLMSG_DATA(nlh);
554 memset(ev, 0, sizeof(*ev));
555 ev->transport_handle = iscsi_handle(conn->transport);
556 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -0500557 ev->r.recv_req.cid = conn->cid;
558 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700559 pdu = (char*)ev + sizeof(*ev);
560 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
561 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
562
Mike Christie43a145a2006-10-16 18:09:38 -0400563 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700564}
565EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
566
Mike Christie7b7232f2006-02-01 21:06:49 -0600567void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700568{
569 struct nlmsghdr *nlh;
570 struct sk_buff *skb;
571 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500572 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700573 int len = NLMSG_SPACE(sizeof(*ev));
574
Mike Christie790f39a2006-05-18 20:31:39 -0500575 priv = iscsi_if_transport_lookup(conn->transport);
576 if (!priv)
577 return;
578
Mike Christie43a145a2006-10-16 18:09:38 -0400579 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700580 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600581 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
582 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700583 return;
584 }
585
Mike Christie790f39a2006-05-18 20:31:39 -0500586 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700587 ev = NLMSG_DATA(nlh);
588 ev->transport_handle = iscsi_handle(conn->transport);
589 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -0700590 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500591 ev->r.connerror.cid = conn->cid;
592 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700593
Mike Christie43a145a2006-10-16 18:09:38 -0400594 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700595
Mike Christie7b8631b2006-01-13 18:05:50 -0600596 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
597 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700598}
599EXPORT_SYMBOL_GPL(iscsi_conn_error);
600
601static int
602iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
603 void *payload, int size)
604{
605 struct sk_buff *skb;
606 struct nlmsghdr *nlh;
607 int len = NLMSG_SPACE(size);
608 int flags = multi ? NLM_F_MULTI : 0;
609 int t = done ? NLMSG_DONE : type;
610
Mike Christie43a145a2006-10-16 18:09:38 -0400611 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -0500612 if (!skb) {
613 printk(KERN_ERR "Could not allocate skb to send reply.\n");
614 return -ENOMEM;
615 }
Alex Aizman0896b752005-08-04 19:33:07 -0700616
617 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
618 nlh->nlmsg_flags = flags;
619 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -0400620 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700621}
622
623static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600624iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700625{
626 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
627 struct iscsi_stats *stats;
628 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600629 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700630 struct nlmsghdr *nlhstat;
631 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500632 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700633 int len = NLMSG_SPACE(sizeof(*ev) +
634 sizeof(struct iscsi_stats) +
635 sizeof(struct iscsi_stats_custom) *
636 ISCSI_STATS_CUSTOM_MAX);
637 int err = 0;
638
Mike Christie790f39a2006-05-18 20:31:39 -0500639 priv = iscsi_if_transport_lookup(transport);
640 if (!priv)
641 return -EINVAL;
642
Mike Christieb5c7a122006-04-06 21:13:33 -0500643 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700644 if (!conn)
645 return -EEXIST;
646
647 do {
648 int actual_size;
649
Mike Christie43a145a2006-10-16 18:09:38 -0400650 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700651 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600652 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
653 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700654 return -ENOMEM;
655 }
656
Mike Christie790f39a2006-05-18 20:31:39 -0500657 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -0700658 (len - sizeof(*nlhstat)), 0);
659 evstat = NLMSG_DATA(nlhstat);
660 memset(evstat, 0, sizeof(*evstat));
661 evstat->transport_handle = iscsi_handle(conn->transport);
662 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -0500663 evstat->u.get_stats.cid =
664 ev->u.get_stats.cid;
665 evstat->u.get_stats.sid =
666 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700667 stats = (struct iscsi_stats *)
668 ((char*)evstat + sizeof(*evstat));
669 memset(stats, 0, sizeof(*stats));
670
Mike Christie7b7232f2006-02-01 21:06:49 -0600671 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700672 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
673 sizeof(struct iscsi_stats) +
674 sizeof(struct iscsi_stats_custom) *
675 stats->custom_length);
676 actual_size -= sizeof(*nlhstat);
677 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600678 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700679 nlhstat->nlmsg_len = actual_size;
680
Mike Christie43a145a2006-10-16 18:09:38 -0400681 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700682 } while (err < 0 && err != -ECONNREFUSED);
683
684 return err;
685}
686
Mike Christie53cb8a12006-06-28 12:00:32 -0500687/**
688 * iscsi_if_destroy_session_done - send session destr. completion event
689 * @conn: last connection for session
690 *
691 * This is called by HW iscsi LLDs to notify userpsace that its HW has
692 * removed a session.
693 **/
694int iscsi_if_destroy_session_done(struct iscsi_cls_conn *conn)
695{
696 struct iscsi_internal *priv;
697 struct iscsi_cls_session *session;
698 struct Scsi_Host *shost;
699 struct iscsi_uevent *ev;
700 struct sk_buff *skb;
701 struct nlmsghdr *nlh;
702 unsigned long flags;
703 int rc, len = NLMSG_SPACE(sizeof(*ev));
704
705 priv = iscsi_if_transport_lookup(conn->transport);
706 if (!priv)
707 return -EINVAL;
708
709 session = iscsi_dev_to_session(conn->dev.parent);
710 shost = iscsi_session_to_shost(session);
711
Mike Christie43a145a2006-10-16 18:09:38 -0400712 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500713 if (!skb) {
714 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
715 "session creation event\n");
716 return -ENOMEM;
717 }
718
719 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
720 ev = NLMSG_DATA(nlh);
721 ev->transport_handle = iscsi_handle(conn->transport);
722 ev->type = ISCSI_KEVENT_DESTROY_SESSION;
723 ev->r.d_session.host_no = shost->host_no;
724 ev->r.d_session.sid = session->sid;
725
726 /*
727 * this will occur if the daemon is not up, so we just warn
728 * the user and when the daemon is restarted it will handle it
729 */
Mike Christie43a145a2006-10-16 18:09:38 -0400730 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500731 if (rc < 0)
732 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
733 "session destruction event. Check iscsi daemon\n");
734
735 spin_lock_irqsave(&sesslock, flags);
736 list_del(&session->sess_list);
737 spin_unlock_irqrestore(&sesslock, flags);
738
739 spin_lock_irqsave(&connlock, flags);
740 conn->active = 0;
741 list_del(&conn->conn_list);
742 spin_unlock_irqrestore(&connlock, flags);
743
744 return rc;
745}
746EXPORT_SYMBOL_GPL(iscsi_if_destroy_session_done);
747
748/**
749 * iscsi_if_create_session_done - send session creation completion event
750 * @conn: leading connection for session
751 *
752 * This is called by HW iscsi LLDs to notify userpsace that its HW has
753 * created a session or a existing session is back in the logged in state.
754 **/
755int iscsi_if_create_session_done(struct iscsi_cls_conn *conn)
756{
757 struct iscsi_internal *priv;
758 struct iscsi_cls_session *session;
759 struct Scsi_Host *shost;
760 struct iscsi_uevent *ev;
761 struct sk_buff *skb;
762 struct nlmsghdr *nlh;
763 unsigned long flags;
764 int rc, len = NLMSG_SPACE(sizeof(*ev));
765
766 priv = iscsi_if_transport_lookup(conn->transport);
767 if (!priv)
768 return -EINVAL;
769
770 session = iscsi_dev_to_session(conn->dev.parent);
771 shost = iscsi_session_to_shost(session);
772
Mike Christie43a145a2006-10-16 18:09:38 -0400773 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500774 if (!skb) {
775 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
776 "session creation event\n");
777 return -ENOMEM;
778 }
779
780 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
781 ev = NLMSG_DATA(nlh);
782 ev->transport_handle = iscsi_handle(conn->transport);
783 ev->type = ISCSI_UEVENT_CREATE_SESSION;
784 ev->r.c_session_ret.host_no = shost->host_no;
785 ev->r.c_session_ret.sid = session->sid;
786
787 /*
788 * this will occur if the daemon is not up, so we just warn
789 * the user and when the daemon is restarted it will handle it
790 */
Mike Christie43a145a2006-10-16 18:09:38 -0400791 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500792 if (rc < 0)
793 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
794 "session creation event. Check iscsi daemon\n");
795
796 spin_lock_irqsave(&sesslock, flags);
797 list_add(&session->sess_list, &sesslist);
798 spin_unlock_irqrestore(&sesslock, flags);
799
800 spin_lock_irqsave(&connlock, flags);
801 list_add(&conn->conn_list, &connlist);
802 conn->active = 1;
803 spin_unlock_irqrestore(&connlock, flags);
804 return rc;
805}
806EXPORT_SYMBOL_GPL(iscsi_if_create_session_done);
807
Alex Aizman0896b752005-08-04 19:33:07 -0700808static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600809iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
810{
811 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600812 struct iscsi_cls_session *session;
Mike Christie7996a772006-04-06 21:13:41 -0500813 unsigned long flags;
Mike Christieb5c7a122006-04-06 21:13:33 -0500814 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -0600815
Mike Christie7996a772006-04-06 21:13:41 -0500816 session = transport->create_session(transport, &priv->t,
Mike Christie7b7232f2006-02-01 21:06:49 -0600817 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -0500818 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -0600819 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600820 return -ENOMEM;
821
Mike Christie7996a772006-04-06 21:13:41 -0500822 spin_lock_irqsave(&sesslock, flags);
823 list_add(&session->sess_list, &sesslist);
824 spin_unlock_irqrestore(&sesslock, flags);
825
Mike Christieb5c7a122006-04-06 21:13:33 -0500826 ev->r.c_session_ret.host_no = hostno;
827 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600828 return 0;
829}
830
831static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600832iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600833{
Mike Christie7b8631b2006-01-13 18:05:50 -0600834 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600835 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600836 unsigned long flags;
837
Mike Christieb5c7a122006-04-06 21:13:33 -0500838 session = iscsi_session_lookup(ev->u.c_conn.sid);
839 if (!session) {
840 printk(KERN_ERR "iscsi: invalid session %d\n",
841 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600842 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -0500843 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600844
Mike Christie7b7232f2006-02-01 21:06:49 -0600845 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -0500846 if (!conn) {
847 printk(KERN_ERR "iscsi: couldn't create a new "
848 "connection for session %d\n",
849 session->sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600850 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500851 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600852
Mike Christieb5c7a122006-04-06 21:13:33 -0500853 ev->r.c_conn_ret.sid = session->sid;
854 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600855
856 spin_lock_irqsave(&connlock, flags);
857 list_add(&conn->conn_list, &connlist);
858 conn->active = 1;
859 spin_unlock_irqrestore(&connlock, flags);
860
Mike Christie7b8631b2006-01-13 18:05:50 -0600861 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -0600862}
863
864static int
865iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
866{
867 unsigned long flags;
868 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -0600869
Mike Christieb5c7a122006-04-06 21:13:33 -0500870 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600871 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -0600872 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -0600873 spin_lock_irqsave(&connlock, flags);
874 conn->active = 0;
875 list_del(&conn->conn_list);
876 spin_unlock_irqrestore(&connlock, flags);
877
Mike Christie7b8631b2006-01-13 18:05:50 -0600878 if (transport->destroy_conn)
879 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -0600880 return 0;
881}
882
Mike Christiefd7255f2006-04-06 21:13:36 -0500883static int
884iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
885{
886 char *data = (char*)ev + sizeof(*ev);
887 struct iscsi_cls_conn *conn;
888 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -0500889 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -0500890
891 session = iscsi_session_lookup(ev->u.set_param.sid);
892 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
893 if (!conn || !session)
894 return -EINVAL;
895
896 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -0500897 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -0500898 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -0500899 if (value != 0)
900 session->recovery_tmo = value;
901 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500902 default:
Mike Christiea54a52c2006-06-28 12:00:23 -0500903 err = transport->set_param(conn, ev->u.set_param.param,
904 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -0500905 }
906
907 return err;
908}
909
Mike Christie7b8631b2006-01-13 18:05:50 -0600910static int
Or Gerlitz264faaa2006-05-02 19:46:36 -0500911iscsi_if_transport_ep(struct iscsi_transport *transport,
912 struct iscsi_uevent *ev, int msg_type)
913{
914 struct sockaddr *dst_addr;
915 int rc = 0;
916
917 switch (msg_type) {
918 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
919 if (!transport->ep_connect)
920 return -EINVAL;
921
922 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
923 rc = transport->ep_connect(dst_addr,
924 ev->u.ep_connect.non_blocking,
925 &ev->r.ep_connect_ret.handle);
926 break;
927 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
928 if (!transport->ep_poll)
929 return -EINVAL;
930
931 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
932 ev->u.ep_poll.timeout_ms);
933 break;
934 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
935 if (!transport->ep_disconnect)
936 return -EINVAL;
937
938 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
939 break;
940 }
941 return rc;
942}
943
944static int
Mike Christie01cb2252006-06-28 12:00:22 -0500945iscsi_tgt_dscvr(struct iscsi_transport *transport,
946 struct iscsi_uevent *ev)
947{
Mike Christie2174a042007-05-30 12:57:10 -0500948 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -0500949 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -0500950 int err;
Mike Christie01cb2252006-06-28 12:00:22 -0500951
952 if (!transport->tgt_dscvr)
953 return -EINVAL;
954
Mike Christie2174a042007-05-30 12:57:10 -0500955 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
956 if (IS_ERR(shost)) {
957 printk(KERN_ERR "target discovery could not find host no %u\n",
958 ev->u.tgt_dscvr.host_no);
959 return -ENODEV;
960 }
961
962
Mike Christie01cb2252006-06-28 12:00:22 -0500963 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -0500964 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
965 ev->u.tgt_dscvr.enable, dst_addr);
966 scsi_host_put(shost);
967 return err;
Mike Christie01cb2252006-06-28 12:00:22 -0500968}
969
970static int
Mike Christie1d9bf132007-05-30 12:57:11 -0500971iscsi_set_host_param(struct iscsi_transport *transport,
972 struct iscsi_uevent *ev)
973{
974 char *data = (char*)ev + sizeof(*ev);
975 struct Scsi_Host *shost;
976 int err;
977
978 if (!transport->set_host_param)
979 return -ENOSYS;
980
981 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
982 if (IS_ERR(shost)) {
983 printk(KERN_ERR "set_host_param could not find host no %u\n",
984 ev->u.set_host_param.host_no);
985 return -ENODEV;
986 }
987
988 err = transport->set_host_param(shost, ev->u.set_host_param.param,
989 data, ev->u.set_host_param.len);
990 scsi_host_put(shost);
991 return err;
992}
993
994static int
Alex Aizman0896b752005-08-04 19:33:07 -0700995iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
996{
997 int err = 0;
998 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
999 struct iscsi_transport *transport = NULL;
1000 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001001 struct iscsi_cls_session *session;
1002 struct iscsi_cls_conn *conn;
Mike Christie7996a772006-04-06 21:13:41 -05001003 unsigned long flags;
Alex Aizman0896b752005-08-04 19:33:07 -07001004
Alex Aizman0896b752005-08-04 19:33:07 -07001005 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1006 if (!priv)
1007 return -EINVAL;
1008 transport = priv->iscsi_transport;
1009
Mike Christie7b7232f2006-02-01 21:06:49 -06001010 if (!try_module_get(transport->owner))
1011 return -EINVAL;
1012
Mike Christie790f39a2006-05-18 20:31:39 -05001013 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1014
Alex Aizman0896b752005-08-04 19:33:07 -07001015 switch (nlh->nlmsg_type) {
1016 case ISCSI_UEVENT_CREATE_SESSION:
1017 err = iscsi_if_create_session(priv, ev);
1018 break;
1019 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001020 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie7996a772006-04-06 21:13:41 -05001021 if (session) {
1022 spin_lock_irqsave(&sesslock, flags);
1023 list_del(&session->sess_list);
1024 spin_unlock_irqrestore(&sesslock, flags);
1025
Mike Christie7b7232f2006-02-01 21:06:49 -06001026 transport->destroy_session(session);
Mike Christie7996a772006-04-06 21:13:41 -05001027 } else
Mike Christie7b7232f2006-02-01 21:06:49 -06001028 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001029 break;
1030 case ISCSI_UEVENT_CREATE_CONN:
1031 err = iscsi_if_create_conn(transport, ev);
1032 break;
1033 case ISCSI_UEVENT_DESTROY_CONN:
1034 err = iscsi_if_destroy_conn(transport, ev);
1035 break;
1036 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001037 session = iscsi_session_lookup(ev->u.b_conn.sid);
1038 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001039
1040 if (session && conn)
1041 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001042 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001043 ev->u.b_conn.is_leading);
1044 else
1045 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001046 break;
1047 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001048 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001049 break;
1050 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001051 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001052 if (conn)
1053 ev->r.retcode = transport->start_conn(conn);
1054 else
1055 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001056 break;
1057 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001058 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001059 if (conn)
1060 transport->stop_conn(conn, ev->u.stop_conn.flag);
1061 else
1062 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001063 break;
1064 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001065 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001066 if (conn)
1067 ev->r.retcode = transport->send_pdu(conn,
1068 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1069 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1070 ev->u.send_pdu.data_size);
1071 else
1072 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001073 break;
1074 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001075 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001076 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001077 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1078 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1079 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1080 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1081 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001082 case ISCSI_UEVENT_TGT_DSCVR:
1083 err = iscsi_tgt_dscvr(transport, ev);
1084 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001085 case ISCSI_UEVENT_SET_HOST_PARAM:
1086 err = iscsi_set_host_param(transport, ev);
1087 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001088 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001089 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001090 break;
1091 }
1092
Mike Christie7b7232f2006-02-01 21:06:49 -06001093 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001094 return err;
1095}
1096
Mike Christieb5c7a122006-04-06 21:13:33 -05001097/*
1098 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1099 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1100 * invalid creds are discarded silently.
1101 */
Alex Aizman0896b752005-08-04 19:33:07 -07001102static void
1103iscsi_if_rx(struct sock *sk, int len)
1104{
1105 struct sk_buff *skb;
1106
Arjan van de Ven0b950672006-01-11 13:16:10 +01001107 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001108 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Mike Christieee7f8e42006-02-01 21:07:01 -06001109 if (NETLINK_CREDS(skb)->uid) {
1110 skb_pull(skb, skb->len);
1111 goto free_skb;
1112 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001113
Alex Aizman0896b752005-08-04 19:33:07 -07001114 while (skb->len >= NLMSG_SPACE(0)) {
1115 int err;
1116 uint32_t rlen;
1117 struct nlmsghdr *nlh;
1118 struct iscsi_uevent *ev;
1119
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001120 nlh = nlmsg_hdr(skb);
Alex Aizman0896b752005-08-04 19:33:07 -07001121 if (nlh->nlmsg_len < sizeof(*nlh) ||
1122 skb->len < nlh->nlmsg_len) {
1123 break;
1124 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001125
Alex Aizman0896b752005-08-04 19:33:07 -07001126 ev = NLMSG_DATA(nlh);
1127 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1128 if (rlen > skb->len)
1129 rlen = skb->len;
Mike Christieee7f8e42006-02-01 21:07:01 -06001130
Alex Aizman0896b752005-08-04 19:33:07 -07001131 err = iscsi_if_recv_msg(skb, nlh);
1132 if (err) {
1133 ev->type = ISCSI_KEVENT_IF_ERROR;
1134 ev->iferror = err;
1135 }
1136 do {
1137 /*
1138 * special case for GET_STATS:
1139 * on success - sending reply and stats from
1140 * inside of if_recv_msg(),
1141 * on error - fall through.
1142 */
1143 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1144 break;
1145 err = iscsi_if_send_reply(
1146 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1147 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Alex Aizman0896b752005-08-04 19:33:07 -07001148 } while (err < 0 && err != -ECONNREFUSED);
1149 skb_pull(skb, rlen);
1150 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001151free_skb:
Alex Aizman0896b752005-08-04 19:33:07 -07001152 kfree_skb(skb);
1153 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001154 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001155}
1156
Mike Christie7b8631b2006-01-13 18:05:50 -06001157#define iscsi_cdev_to_conn(_cdev) \
1158 iscsi_dev_to_conn(_cdev->dev)
1159
Mike Christiefd7255f2006-04-06 21:13:36 -05001160#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1161struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1162 __ATTR(_name,_mode,_show,_store)
1163
Alex Aizman0896b752005-08-04 19:33:07 -07001164/*
1165 * iSCSI connection attrs
1166 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001167#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001169show_conn_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001170{ \
1171 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1172 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001173 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001174}
1175
Mike Christiea54a52c2006-06-28 12:00:23 -05001176#define iscsi_conn_attr(field, param) \
1177 iscsi_conn_attr_show(param) \
1178static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001179 NULL);
1180
Mike Christiea54a52c2006-06-28 12:00:23 -05001181iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1182iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1183iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1184iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1185iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1186iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1187iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1188iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1189iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1190iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1191iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Mike Christie7b8631b2006-01-13 18:05:50 -06001193#define iscsi_cdev_to_session(_cdev) \
1194 iscsi_dev_to_session(_cdev->dev)
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196/*
Alex Aizman0896b752005-08-04 19:33:07 -07001197 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001199#define iscsi_session_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001201show_session_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001202{ \
1203 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1204 struct iscsi_transport *t = session->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001205 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Mike Christiea54a52c2006-06-28 12:00:23 -05001208#define iscsi_session_attr(field, param) \
1209 iscsi_session_attr_show(param) \
1210static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001211 NULL);
1212
Mike Christiea54a52c2006-06-28 12:00:23 -05001213iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1214iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1215iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1216iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1217iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1218iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST);
1219iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1220iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN);
1221iscsi_session_attr(erl, ISCSI_PARAM_ERL);
1222iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT);
Mike Christiefd7255f2006-04-06 21:13:36 -05001223
Mike Christiefd7255f2006-04-06 21:13:36 -05001224#define iscsi_priv_session_attr_show(field, format) \
1225static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001226show_priv_session_##field(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001227{ \
Mike Christiea54a52c2006-06-28 12:00:23 -05001228 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
Mike Christiefd7255f2006-04-06 21:13:36 -05001229 return sprintf(buf, format"\n", session->field); \
1230}
1231
1232#define iscsi_priv_session_attr(field, format) \
1233 iscsi_priv_session_attr_show(field, format) \
1234static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1235 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001236iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001237
Mike Christie1819dc82007-05-30 12:57:08 -05001238/*
1239 * iSCSI host attrs
1240 */
1241#define iscsi_host_attr_show(param) \
1242static ssize_t \
1243show_host_param_##param(struct class_device *cdev, char *buf) \
1244{ \
1245 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1246 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1247 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1248}
1249
1250#define iscsi_host_attr(field, param) \
1251 iscsi_host_attr_show(param) \
1252static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1253 NULL);
1254
1255iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001256iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001257
Mike Christiefd7255f2006-04-06 21:13:36 -05001258#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1259do { \
1260 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1261 count++; \
1262} while (0)
1263
Mike Christiea54a52c2006-06-28 12:00:23 -05001264
Mike Christiefd7255f2006-04-06 21:13:36 -05001265#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1266do { \
1267 if (tt->param_mask & param_flag) { \
1268 priv->session_attrs[count] = &class_device_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001270 } \
1271} while (0)
1272
Mike Christiefd7255f2006-04-06 21:13:36 -05001273#define SETUP_CONN_RD_ATTR(field, param_flag) \
1274do { \
1275 if (tt->param_mask & param_flag) { \
1276 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1277 count++; \
1278 } \
1279} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Mike Christie1819dc82007-05-30 12:57:08 -05001281#define SETUP_HOST_RD_ATTR(field, param_flag) \
1282do { \
1283 if (tt->host_param_mask & param_flag) { \
1284 priv->host_attrs[count] = &class_device_attr_host_##field; \
1285 count++; \
1286 } \
1287} while (0)
1288
Alex Aizman0896b752005-08-04 19:33:07 -07001289static int iscsi_session_match(struct attribute_container *cont,
1290 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Mike Christie7b8631b2006-01-13 18:05:50 -06001292 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001294 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Alex Aizman0896b752005-08-04 19:33:07 -07001296 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 0;
1298
Mike Christie7b8631b2006-01-13 18:05:50 -06001299 session = iscsi_dev_to_session(dev);
1300 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001301 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303
Alex Aizman0896b752005-08-04 19:33:07 -07001304 priv = to_iscsi_internal(shost->transportt);
1305 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1306 return 0;
1307
1308 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Alex Aizman0896b752005-08-04 19:33:07 -07001311static int iscsi_conn_match(struct attribute_container *cont,
1312 struct device *dev)
1313{
Mike Christie7b8631b2006-01-13 18:05:50 -06001314 struct iscsi_cls_session *session;
1315 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001316 struct Scsi_Host *shost;
1317 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Alex Aizman0896b752005-08-04 19:33:07 -07001319 if (!iscsi_is_conn_dev(dev))
1320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Mike Christie7b8631b2006-01-13 18:05:50 -06001322 conn = iscsi_dev_to_conn(dev);
1323 session = iscsi_dev_to_session(conn->dev.parent);
1324 shost = iscsi_session_to_shost(session);
1325
Alex Aizman0896b752005-08-04 19:33:07 -07001326 if (!shost->transportt)
1327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Alex Aizman0896b752005-08-04 19:33:07 -07001329 priv = to_iscsi_internal(shost->transportt);
1330 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1331 return 0;
1332
1333 return &priv->conn_cont.ac == cont;
1334}
1335
Mike Christie30a6c652006-04-06 21:13:39 -05001336static int iscsi_host_match(struct attribute_container *cont,
1337 struct device *dev)
1338{
1339 struct Scsi_Host *shost;
1340 struct iscsi_internal *priv;
1341
1342 if (!scsi_is_host_device(dev))
1343 return 0;
1344
1345 shost = dev_to_shost(dev);
1346 if (!shost->transportt ||
1347 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1348 return 0;
1349
1350 priv = to_iscsi_internal(shost->transportt);
1351 return &priv->t.host_attrs.ac == cont;
1352}
1353
Mike Christie7b8631b2006-01-13 18:05:50 -06001354struct scsi_transport_template *
1355iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001356{
1357 struct iscsi_internal *priv;
1358 unsigned long flags;
1359 int count = 0, err;
1360
1361 BUG_ON(!tt);
1362
1363 priv = iscsi_if_transport_lookup(tt);
1364 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001365 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001366
Jes Sorensen24669f752006-01-16 10:31:18 -05001367 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001368 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001369 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001370 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001371 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001372 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001373 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001374
1375 priv->cdev.class = &iscsi_transport_class;
1376 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1377 err = class_device_register(&priv->cdev);
1378 if (err)
1379 goto free_priv;
1380
1381 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1382 if (err)
1383 goto unregister_cdev;
1384
Mike Christie30a6c652006-04-06 21:13:39 -05001385 /* host parameters */
1386 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1387 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1388 priv->t.host_attrs.ac.match = iscsi_host_match;
1389 priv->t.host_size = sizeof(struct iscsi_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001390 transport_container_register(&priv->t.host_attrs);
1391
Mike Christie1819dc82007-05-30 12:57:08 -05001392 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001393 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001394 BUG_ON(count > ISCSI_HOST_ATTRS);
1395 priv->host_attrs[count] = NULL;
1396 count = 0;
1397
Alex Aizman0896b752005-08-04 19:33:07 -07001398 /* connection parameters */
1399 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1400 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1401 priv->conn_cont.ac.match = iscsi_conn_match;
1402 transport_container_register(&priv->conn_cont);
1403
Mike Christiefd7255f2006-04-06 21:13:36 -05001404 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1405 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1406 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1407 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1408 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1409 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1410 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1411 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001412 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001413 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1414 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Alex Aizman0896b752005-08-04 19:33:07 -07001415
1416 BUG_ON(count > ISCSI_CONN_ATTRS);
1417 priv->conn_attrs[count] = NULL;
1418 count = 0;
1419
1420 /* session parameters */
1421 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1422 priv->session_cont.ac.class = &iscsi_session_class.class;
1423 priv->session_cont.ac.match = iscsi_session_match;
1424 transport_container_register(&priv->session_cont);
1425
Mike Christiefd7255f2006-04-06 21:13:36 -05001426 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1427 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1428 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1429 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1430 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1431 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1432 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1433 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001434 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1435 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christie30a6c652006-04-06 21:13:39 -05001436 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christiefd7255f2006-04-06 21:13:36 -05001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001439 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Alex Aizman0896b752005-08-04 19:33:07 -07001441 spin_lock_irqsave(&iscsi_transport_lock, flags);
1442 list_add(&priv->list, &iscsi_transports);
1443 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Alex Aizman0896b752005-08-04 19:33:07 -07001445 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001446 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Alex Aizman0896b752005-08-04 19:33:07 -07001448unregister_cdev:
1449 class_device_unregister(&priv->cdev);
1450free_priv:
1451 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001452 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
Alex Aizman0896b752005-08-04 19:33:07 -07001454EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Alex Aizman0896b752005-08-04 19:33:07 -07001456int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Alex Aizman0896b752005-08-04 19:33:07 -07001458 struct iscsi_internal *priv;
1459 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Alex Aizman0896b752005-08-04 19:33:07 -07001461 BUG_ON(!tt);
1462
Arjan van de Ven0b950672006-01-11 13:16:10 +01001463 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001464
1465 priv = iscsi_if_transport_lookup(tt);
1466 BUG_ON (!priv);
1467
Alex Aizman0896b752005-08-04 19:33:07 -07001468 spin_lock_irqsave(&iscsi_transport_lock, flags);
1469 list_del(&priv->list);
1470 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1471
1472 transport_container_unregister(&priv->conn_cont);
1473 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001474 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001475
1476 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1477 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001478 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001479
1480 return 0;
1481}
1482EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484static __init int iscsi_transport_init(void)
1485{
Alex Aizman0896b752005-08-04 19:33:07 -07001486 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Meelis Roos09492602006-12-17 12:10:26 -06001488 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001489 ISCSI_TRANSPORT_VERSION);
1490
Mike Christie41be1442007-02-28 17:32:18 -06001491 atomic_set(&iscsi_session_nr, 0);
1492
Alex Aizman0896b752005-08-04 19:33:07 -07001493 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (err)
1495 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001496
Mike Christie30a6c652006-04-06 21:13:39 -05001497 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001498 if (err)
1499 goto unregister_transport_class;
1500
Mike Christie30a6c652006-04-06 21:13:39 -05001501 err = transport_class_register(&iscsi_connection_class);
1502 if (err)
1503 goto unregister_host_class;
1504
Alex Aizman0896b752005-08-04 19:33:07 -07001505 err = transport_class_register(&iscsi_session_class);
1506 if (err)
1507 goto unregister_conn_class;
1508
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001509 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
Mike Christie7b8631b2006-01-13 18:05:50 -06001510 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001511 if (!nls) {
1512 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001513 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001514 }
1515
Mike Christie43a145a2006-10-16 18:09:38 -04001516 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001517
Alex Aizman0896b752005-08-04 19:33:07 -07001518unregister_session_class:
1519 transport_class_unregister(&iscsi_session_class);
1520unregister_conn_class:
1521 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001522unregister_host_class:
1523 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001524unregister_transport_class:
1525 class_unregister(&iscsi_transport_class);
1526 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529static void __exit iscsi_transport_exit(void)
1530{
Alex Aizman0896b752005-08-04 19:33:07 -07001531 sock_release(nls->sk_socket);
Alex Aizman0896b752005-08-04 19:33:07 -07001532 transport_class_unregister(&iscsi_connection_class);
1533 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001534 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001535 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538module_init(iscsi_transport_init);
1539module_exit(iscsi_transport_exit);
1540
Alex Aizman0896b752005-08-04 19:33:07 -07001541MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1542 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1543 "Alex Aizman <itn780@yahoo.com>");
1544MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05001546MODULE_VERSION(ISCSI_TRANSPORT_VERSION);