blob: e2b67e34d92ee37f699ff72335a0ba62a9dcc10f [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>
Alex Aizman0896b752005-08-04 19:33:07 -070024#include <linux/mempool.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010025#include <linux/mutex.h>
Alex Aizman0896b752005-08-04 19:33:07 -070026#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <scsi/scsi.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070032#include <scsi/iscsi_if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Mike Christie30a6c652006-04-06 21:13:39 -050034#define ISCSI_SESSION_ATTRS 11
Mike Christiefd7255f2006-04-06 21:13:36 -050035#define ISCSI_CONN_ATTRS 10
Mike Christie30a6c652006-04-06 21:13:39 -050036#define ISCSI_HOST_ATTRS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct iscsi_internal {
39 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070040 struct iscsi_transport *iscsi_transport;
41 struct list_head list;
Alex Aizman0896b752005-08-04 19:33:07 -070042 struct class_device cdev;
Mike Christie30a6c652006-04-06 21:13:39 -050043
44 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070045 struct transport_container conn_cont;
46 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
47 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Mike Christieb5c7a122006-04-06 21:13:33 -050051static int iscsi_session_nr; /* sysfs session id for next new session */
52
Alex Aizman0896b752005-08-04 19:33:07 -070053/*
54 * list of registered transports and lock that must
55 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010056 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070057 */
58static LIST_HEAD(iscsi_transports);
59static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Alex Aizman0896b752005-08-04 19:33:07 -070061#define to_iscsi_internal(tmpl) \
62 container_of(tmpl, struct iscsi_internal, t)
63
64#define cdev_to_iscsi_internal(_cdev) \
65 container_of(_cdev, struct iscsi_internal, cdev)
66
67static void iscsi_transport_release(struct class_device *cdev)
68{
69 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
70 kfree(priv);
71}
72
73/*
74 * iscsi_transport_class represents the iscsi_transports that are
75 * registered.
76 */
77static struct class iscsi_transport_class = {
78 .name = "iscsi_transport",
79 .release = iscsi_transport_release,
80};
81
82static ssize_t
83show_transport_handle(struct class_device *cdev, char *buf)
84{
85 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
Mike Christie762e2bf2005-09-12 21:01:46 -050086 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070087}
88static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
89
90#define show_transport_attr(name, format) \
91static ssize_t \
92show_transport_##name(struct class_device *cdev, char *buf) \
93{ \
94 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
95 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
96} \
97static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
98
99show_transport_attr(caps, "0x%x");
100show_transport_attr(max_lun, "%d");
101show_transport_attr(max_conn, "%d");
102show_transport_attr(max_cmd_len, "%d");
103
104static struct attribute *iscsi_transport_attrs[] = {
105 &class_device_attr_handle.attr,
106 &class_device_attr_caps.attr,
107 &class_device_attr_max_lun.attr,
108 &class_device_attr_max_conn.attr,
109 &class_device_attr_max_cmd_len.attr,
110 NULL,
111};
112
113static struct attribute_group iscsi_transport_group = {
114 .attrs = iscsi_transport_attrs,
115};
116
Mike Christie30a6c652006-04-06 21:13:39 -0500117static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
118 struct class_device *cdev)
119{
120 struct Scsi_Host *shost = dev_to_shost(dev);
121 struct iscsi_host *ihost = shost->shost_data;
122
123 memset(ihost, 0, sizeof(*ihost));
124 INIT_LIST_HEAD(&ihost->sessions);
125 mutex_init(&ihost->mutex);
126 return 0;
127}
128
129static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
130 "iscsi_host",
131 iscsi_setup_host,
132 NULL,
133 NULL);
134
Alex Aizman0896b752005-08-04 19:33:07 -0700135static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
136 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 NULL,
138 NULL,
139 NULL);
140
Alex Aizman0896b752005-08-04 19:33:07 -0700141static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
142 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 NULL,
144 NULL,
145 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700146
147static struct sock *nls;
148static int daemon_pid;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100149static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700150
151struct mempool_zone {
152 mempool_t *pool;
153 atomic_t allocated;
154 int size;
155 int hiwat;
156 struct list_head freequeue;
157 spinlock_t freelock;
158};
159
Mike Christie7b8631b2006-01-13 18:05:50 -0600160static struct mempool_zone *z_reply;
Alex Aizman0896b752005-08-04 19:33:07 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
Alex Aizman0896b752005-08-04 19:33:07 -0700163 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
164 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
165 * so daemon will notice OOM on NETLINK tranposrt level and will
166 * be able to predict or change operational behavior
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Alex Aizman0896b752005-08-04 19:33:07 -0700168#define Z_MAX_REPLY 8
169#define Z_HIWAT_REPLY 6
170#define Z_MAX_PDU 8
171#define Z_HIWAT_PDU 6
172#define Z_MAX_ERROR 16
173#define Z_HIWAT_ERROR 12
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Mike Christie7b7232f2006-02-01 21:06:49 -0600175static LIST_HEAD(sesslist);
176static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700177static LIST_HEAD(connlist);
178static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Mike Christieb5c7a122006-04-06 21:13:33 -0500180static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
181{
182 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
183 return sess->sid;
184}
185
186/*
187 * Returns the matching session to a given sid
188 */
189static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600190{
191 unsigned long flags;
192 struct iscsi_cls_session *sess;
193
194 spin_lock_irqsave(&sesslock, flags);
195 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500196 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600197 spin_unlock_irqrestore(&sesslock, flags);
198 return sess;
199 }
200 }
201 spin_unlock_irqrestore(&sesslock, flags);
202 return NULL;
203}
204
Mike Christieb5c7a122006-04-06 21:13:33 -0500205/*
206 * Returns the matching connection to a given sid / cid tuple
207 */
208static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600209{
210 unsigned long flags;
211 struct iscsi_cls_conn *conn;
212
213 spin_lock_irqsave(&connlock, flags);
214 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500215 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600216 spin_unlock_irqrestore(&connlock, flags);
217 return conn;
218 }
219 }
220 spin_unlock_irqrestore(&connlock, flags);
221 return NULL;
222}
223
Mike Christie7b8631b2006-01-13 18:05:50 -0600224/*
225 * The following functions can be used by LLDs that allocate
226 * their own scsi_hosts or by software iscsi LLDs
227 */
228static void iscsi_session_release(struct device *dev)
229{
230 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
231 struct iscsi_transport *transport = session->transport;
232 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Mike Christie7b8631b2006-01-13 18:05:50 -0600234 shost = iscsi_session_to_shost(session);
235 scsi_host_put(shost);
Mike Christiefd7255f2006-04-06 21:13:36 -0500236 kfree(session->targetname);
Mike Christie7b8631b2006-01-13 18:05:50 -0600237 kfree(session);
238 module_put(transport->owner);
239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Mike Christie7b8631b2006-01-13 18:05:50 -0600241static int iscsi_is_session_dev(const struct device *dev)
242{
243 return dev->release == iscsi_session_release;
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Mike Christie30a6c652006-04-06 21:13:39 -0500246static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
247 uint id, uint lun)
248{
249 struct iscsi_host *ihost = shost->shost_data;
250 struct iscsi_cls_session *session;
251
252 mutex_lock(&ihost->mutex);
253 list_for_each_entry(session, &ihost->sessions, host_list) {
254 if ((channel == SCAN_WILD_CARD ||
255 channel == session->channel) &&
256 (id == SCAN_WILD_CARD || id == session->target_id))
257 scsi_scan_target(&session->dev, session->channel,
258 session->target_id, lun, 1);
259 }
260 mutex_unlock(&ihost->mutex);
261
262 return 0;
263}
264
265static void session_recovery_timedout(void *data)
266{
267 struct iscsi_cls_session *session = data;
268
269 dev_printk(KERN_INFO, &session->dev, "session recovery timed out "
270 "after %d secs\n", session->recovery_tmo);
271
272 if (session->transport->session_recovery_timedout)
273 session->transport->session_recovery_timedout(session);
274
275 scsi_target_unblock(&session->dev);
276}
277
278void iscsi_unblock_session(struct iscsi_cls_session *session)
279{
280 if (!cancel_delayed_work(&session->recovery_work))
281 flush_scheduled_work();
282 scsi_target_unblock(&session->dev);
283}
284EXPORT_SYMBOL_GPL(iscsi_unblock_session);
285
286void iscsi_block_session(struct iscsi_cls_session *session)
287{
288 scsi_target_block(&session->dev);
289 schedule_delayed_work(&session->recovery_work,
290 session->recovery_tmo * HZ);
291}
292EXPORT_SYMBOL_GPL(iscsi_block_session);
293
Mike Christie7b8631b2006-01-13 18:05:50 -0600294/**
295 * iscsi_create_session - create iscsi class session
296 * @shost: scsi host
297 * @transport: iscsi transport
298 *
Mike Christieb5c7a122006-04-06 21:13:33 -0500299 * This can be called from a LLD or iscsi_transport.
Mike Christie7b8631b2006-01-13 18:05:50 -0600300 **/
301struct iscsi_cls_session *
Mike Christie30a6c652006-04-06 21:13:39 -0500302iscsi_create_session(struct Scsi_Host *shost,
303 struct iscsi_transport *transport, int channel)
Mike Christie7b8631b2006-01-13 18:05:50 -0600304{
Mike Christie30a6c652006-04-06 21:13:39 -0500305 struct iscsi_host *ihost;
Mike Christie7b8631b2006-01-13 18:05:50 -0600306 struct iscsi_cls_session *session;
307 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Mike Christie7b8631b2006-01-13 18:05:50 -0600309 if (!try_module_get(transport->owner))
310 return NULL;
311
Mike Christieb5c7a122006-04-06 21:13:33 -0500312 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
313 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600314 if (!session)
315 goto module_put;
316 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500317 session->recovery_tmo = 120;
318 INIT_WORK(&session->recovery_work, session_recovery_timedout, session);
319 INIT_LIST_HEAD(&session->host_list);
320 INIT_LIST_HEAD(&session->sess_list);
Mike Christie7b8631b2006-01-13 18:05:50 -0600321
Mike Christieb5c7a122006-04-06 21:13:33 -0500322 if (transport->sessiondata_size)
323 session->dd_data = &session[1];
324
Mike Christie7b8631b2006-01-13 18:05:50 -0600325 /* this is released in the dev's release function */
326 scsi_host_get(shost);
Mike Christie30a6c652006-04-06 21:13:39 -0500327 ihost = shost->shost_data;
328
Mike Christieb5c7a122006-04-06 21:13:33 -0500329 session->sid = iscsi_session_nr++;
Mike Christie30a6c652006-04-06 21:13:39 -0500330 session->channel = channel;
331 session->target_id = ihost->next_target_id++;
332
Mike Christieb5c7a122006-04-06 21:13:33 -0500333 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
334 session->sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600335 session->dev.parent = &shost->shost_gendev;
336 session->dev.release = iscsi_session_release;
337 err = device_register(&session->dev);
338 if (err) {
339 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
340 "register session's dev\n");
341 goto free_session;
342 }
343 transport_register_device(&session->dev);
344
Mike Christie30a6c652006-04-06 21:13:39 -0500345 mutex_lock(&ihost->mutex);
346 list_add(&session->host_list, &ihost->sessions);
347 mutex_unlock(&ihost->mutex);
348
Mike Christie7b8631b2006-01-13 18:05:50 -0600349 return session;
350
351free_session:
352 kfree(session);
353module_put:
354 module_put(transport->owner);
355 return NULL;
356}
357
358EXPORT_SYMBOL_GPL(iscsi_create_session);
359
360/**
361 * iscsi_destroy_session - destroy iscsi session
362 * @session: iscsi_session
363 *
364 * Can be called by a LLD or iscsi_transport. There must not be
365 * any running connections.
366 **/
367int iscsi_destroy_session(struct iscsi_cls_session *session)
368{
Mike Christie30a6c652006-04-06 21:13:39 -0500369 struct Scsi_Host *shost = iscsi_session_to_shost(session);
370 struct iscsi_host *ihost = shost->shost_data;
371
372 if (!cancel_delayed_work(&session->recovery_work))
373 flush_scheduled_work();
374
375 mutex_lock(&ihost->mutex);
376 list_del(&session->host_list);
377 mutex_unlock(&ihost->mutex);
378
Mike Christie7b8631b2006-01-13 18:05:50 -0600379 transport_unregister_device(&session->dev);
380 device_unregister(&session->dev);
381 return 0;
382}
383
384EXPORT_SYMBOL_GPL(iscsi_destroy_session);
385
386static void iscsi_conn_release(struct device *dev)
387{
388 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
389 struct device *parent = conn->dev.parent;
390
Mike Christiefd7255f2006-04-06 21:13:36 -0500391 kfree(conn->persistent_address);
Mike Christie7b8631b2006-01-13 18:05:50 -0600392 kfree(conn);
393 put_device(parent);
394}
395
396static int iscsi_is_conn_dev(const struct device *dev)
397{
398 return dev->release == iscsi_conn_release;
399}
400
401/**
402 * iscsi_create_conn - create iscsi class connection
403 * @session: iscsi cls session
404 * @cid: connection id
405 *
406 * This can be called from a LLD or iscsi_transport. The connection
407 * is child of the session so cid must be unique for all connections
408 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500409 *
410 * Since we do not support MCS, cid will normally be zero. In some cases
411 * for software iscsi we could be trying to preallocate a connection struct
412 * in which case there could be two connection structs and cid would be
413 * non-zero.
Mike Christie7b8631b2006-01-13 18:05:50 -0600414 **/
415struct iscsi_cls_conn *
416iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
417{
418 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600419 struct iscsi_cls_conn *conn;
420 int err;
421
422 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
423 if (!conn)
424 return NULL;
425
426 if (transport->conndata_size)
427 conn->dd_data = &conn[1];
428
429 INIT_LIST_HEAD(&conn->conn_list);
430 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500431 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600432
433 /* this is released in the dev's release function */
434 if (!get_device(&session->dev))
435 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500436
Mike Christie7b8631b2006-01-13 18:05:50 -0600437 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500438 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600439 conn->dev.parent = &session->dev;
440 conn->dev.release = iscsi_conn_release;
441 err = device_register(&conn->dev);
442 if (err) {
443 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
444 "connection's dev\n");
445 goto release_parent_ref;
446 }
447 transport_register_device(&conn->dev);
448 return conn;
449
450release_parent_ref:
451 put_device(&session->dev);
452free_conn:
453 kfree(conn);
454 return NULL;
455}
456
457EXPORT_SYMBOL_GPL(iscsi_create_conn);
458
459/**
460 * iscsi_destroy_conn - destroy iscsi class connection
461 * @session: iscsi cls session
462 *
463 * This can be called from a LLD or iscsi_transport.
464 **/
465int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
466{
467 transport_unregister_device(&conn->dev);
468 device_unregister(&conn->dev);
469 return 0;
470}
471
472EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
473
474/*
475 * These functions are used only by software iscsi_transports
476 * which do not allocate and more their scsi_hosts since this
477 * is initiated from userspace.
478 */
479
480/*
481 * iSCSI Session's hostdata organization:
482 *
483 * *------------------* <== hostdata_session(host->hostdata)
484 * | ptr to class sess|
485 * |------------------| <== iscsi_hostdata(host->hostdata)
486 * | transport's data |
487 * *------------------*
488 */
489
490#define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
491 _t->hostdata_size % sizeof(unsigned long))
492
493#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
494
495/**
496 * iscsi_transport_create_session - create iscsi cls session and host
497 * scsit: scsi transport template
498 * transport: iscsi transport template
499 *
500 * This can be used by software iscsi_transports that allocate
501 * a session per scsi host.
502 **/
503struct Scsi_Host *
504iscsi_transport_create_session(struct scsi_transport_template *scsit,
505 struct iscsi_transport *transport)
506{
507 struct iscsi_cls_session *session;
508 struct Scsi_Host *shost;
Mike Christie7b7232f2006-02-01 21:06:49 -0600509 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600510
511 shost = scsi_host_alloc(transport->host_template,
512 hostdata_privsize(transport));
513 if (!shost) {
514 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
515 "session\n");
516 return NULL;
517 }
518
519 shost->max_id = 1;
520 shost->max_channel = 0;
521 shost->max_lun = transport->max_lun;
522 shost->max_cmd_len = transport->max_cmd_len;
523 shost->transportt = scsit;
Mike Christie55e32992006-01-13 18:05:53 -0600524 shost->transportt->create_work_queue = 1;
Mike Christie7b8631b2006-01-13 18:05:50 -0600525
526 if (scsi_add_host(shost, NULL))
527 goto free_host;
528
Mike Christie30a6c652006-04-06 21:13:39 -0500529 session = iscsi_create_session(shost, transport, 0);
Mike Christie7b8631b2006-01-13 18:05:50 -0600530 if (!session)
531 goto remove_host;
532
533 *(unsigned long*)shost->hostdata = (unsigned long)session;
Mike Christie7b7232f2006-02-01 21:06:49 -0600534 spin_lock_irqsave(&sesslock, flags);
535 list_add(&session->sess_list, &sesslist);
536 spin_unlock_irqrestore(&sesslock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600537 return shost;
538
539remove_host:
540 scsi_remove_host(shost);
541free_host:
542 scsi_host_put(shost);
543 return NULL;
544}
545
546EXPORT_SYMBOL_GPL(iscsi_transport_create_session);
547
548/**
549 * iscsi_transport_destroy_session - destroy session and scsi host
550 * shost: scsi host
551 *
552 * This can be used by software iscsi_transports that allocate
553 * a session per scsi host.
554 **/
555int iscsi_transport_destroy_session(struct Scsi_Host *shost)
556{
557 struct iscsi_cls_session *session;
Mike Christie7b7232f2006-02-01 21:06:49 -0600558 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600559
Mike Christie7b8631b2006-01-13 18:05:50 -0600560 session = hostdata_session(shost->hostdata);
Mike Christie7b7232f2006-02-01 21:06:49 -0600561 spin_lock_irqsave(&sesslock, flags);
562 list_del(&session->sess_list);
563 spin_unlock_irqrestore(&sesslock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600564 iscsi_destroy_session(session);
Mike Christie30a6c652006-04-06 21:13:39 -0500565
566 scsi_remove_host(shost);
Mike Christie7b8631b2006-01-13 18:05:50 -0600567 /* ref from host alloc */
568 scsi_host_put(shost);
569 return 0;
570}
571
572EXPORT_SYMBOL_GPL(iscsi_transport_destroy_session);
573
574/*
575 * iscsi interface functions
576 */
Alex Aizman0896b752005-08-04 19:33:07 -0700577static struct iscsi_internal *
578iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Alex Aizman0896b752005-08-04 19:33:07 -0700580 struct iscsi_internal *priv;
581 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Alex Aizman0896b752005-08-04 19:33:07 -0700583 spin_lock_irqsave(&iscsi_transport_lock, flags);
584 list_for_each_entry(priv, &iscsi_transports, list) {
585 if (tt == priv->iscsi_transport) {
586 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
587 return priv;
588 }
589 }
590 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
591 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Alex Aizman0896b752005-08-04 19:33:07 -0700594static inline struct list_head *skb_to_lh(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Alex Aizman0896b752005-08-04 19:33:07 -0700596 return (struct list_head *)&skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Alex Aizman0896b752005-08-04 19:33:07 -0700598
599static void*
Mike Christie28e55542006-02-01 21:07:11 -0600600mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
Alex Aizman0896b752005-08-04 19:33:07 -0700601{
602 struct mempool_zone *zone = pool_data;
603
604 return alloc_skb(zone->size, gfp_mask);
605}
606
607static void
608mempool_zone_free_skb(void *element, void *pool_data)
609{
610 kfree_skb(element);
611}
612
613static void
614mempool_zone_complete(struct mempool_zone *zone)
615{
616 unsigned long flags;
617 struct list_head *lh, *n;
618
619 spin_lock_irqsave(&zone->freelock, flags);
620 list_for_each_safe(lh, n, &zone->freequeue) {
621 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
622 offsetof(struct sk_buff, cb));
623 if (!skb_shared(skb)) {
624 list_del(skb_to_lh(skb));
625 mempool_free(skb, zone->pool);
626 atomic_dec(&zone->allocated);
627 }
628 }
629 spin_unlock_irqrestore(&zone->freelock, flags);
630}
631
Mike Christie7b8631b2006-01-13 18:05:50 -0600632static struct mempool_zone *
633mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700634{
Mike Christie7b8631b2006-01-13 18:05:50 -0600635 struct mempool_zone *zp;
636
637 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
638 if (!zp)
639 return NULL;
640
Mike Christie142e3012006-02-01 21:06:58 -0600641 zp->size = size;
642 zp->hiwat = hiwat;
643 INIT_LIST_HEAD(&zp->freequeue);
644 spin_lock_init(&zp->freelock);
645 atomic_set(&zp->allocated, 0);
646
Alex Aizman0896b752005-08-04 19:33:07 -0700647 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
648 mempool_zone_free_skb, zp);
Mike Christie7b8631b2006-01-13 18:05:50 -0600649 if (!zp->pool) {
650 kfree(zp);
651 return NULL;
652 }
Alex Aizman0896b752005-08-04 19:33:07 -0700653
Mike Christie7b8631b2006-01-13 18:05:50 -0600654 return zp;
Alex Aizman0896b752005-08-04 19:33:07 -0700655}
656
Mike Christie7b8631b2006-01-13 18:05:50 -0600657static void mempool_zone_destroy(struct mempool_zone *zp)
658{
659 mempool_destroy(zp->pool);
660 kfree(zp);
661}
Alex Aizman0896b752005-08-04 19:33:07 -0700662
663static struct sk_buff*
664mempool_zone_get_skb(struct mempool_zone *zone)
665{
666 struct sk_buff *skb;
667
668 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
669 if (skb)
670 atomic_inc(&zone->allocated);
671 return skb;
672}
673
674static int
675iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
676{
677 unsigned long flags;
678 int rc;
679
680 skb_get(skb);
681 rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT);
682 if (rc < 0) {
683 mempool_free(skb, zone->pool);
684 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
685 return rc;
686 }
687
688 spin_lock_irqsave(&zone->freelock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500689 INIT_LIST_HEAD(skb_to_lh(skb));
Alex Aizman0896b752005-08-04 19:33:07 -0700690 list_add(skb_to_lh(skb), &zone->freequeue);
691 spin_unlock_irqrestore(&zone->freelock, flags);
692
693 return 0;
694}
695
Mike Christie7b7232f2006-02-01 21:06:49 -0600696int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700697 char *data, uint32_t data_size)
698{
699 struct nlmsghdr *nlh;
700 struct sk_buff *skb;
701 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700702 char *pdu;
703 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
704 data_size);
705
Mike Christie7b8631b2006-01-13 18:05:50 -0600706 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700707
Mike Christie7b8631b2006-01-13 18:05:50 -0600708 skb = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700709 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600710 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600711 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
712 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700713 return -ENOMEM;
714 }
715
716 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
717 ev = NLMSG_DATA(nlh);
718 memset(ev, 0, sizeof(*ev));
719 ev->transport_handle = iscsi_handle(conn->transport);
720 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christie7b8631b2006-01-13 18:05:50 -0600721 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700722 ev->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500723 ev->r.recv_req.cid = conn->cid;
724 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700725 pdu = (char*)ev + sizeof(*ev);
726 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
727 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
728
Mike Christie7b8631b2006-01-13 18:05:50 -0600729 return iscsi_unicast_skb(conn->z_pdu, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700730}
731EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
732
Mike Christie7b7232f2006-02-01 21:06:49 -0600733void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700734{
735 struct nlmsghdr *nlh;
736 struct sk_buff *skb;
737 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700738 int len = NLMSG_SPACE(sizeof(*ev));
739
Mike Christie7b8631b2006-01-13 18:05:50 -0600740 mempool_zone_complete(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700741
Mike Christie7b8631b2006-01-13 18:05:50 -0600742 skb = mempool_zone_get_skb(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700743 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600744 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
745 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700746 return;
747 }
748
749 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
750 ev = NLMSG_DATA(nlh);
751 ev->transport_handle = iscsi_handle(conn->transport);
752 ev->type = ISCSI_KEVENT_CONN_ERROR;
Mike Christie7b8631b2006-01-13 18:05:50 -0600753 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700754 ev->iferror = -ENOMEM;
755 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500756 ev->r.connerror.cid = conn->cid;
757 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700758
Mike Christie7b8631b2006-01-13 18:05:50 -0600759 iscsi_unicast_skb(conn->z_error, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700760
Mike Christie7b8631b2006-01-13 18:05:50 -0600761 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
762 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700763}
764EXPORT_SYMBOL_GPL(iscsi_conn_error);
765
766static int
767iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
768 void *payload, int size)
769{
770 struct sk_buff *skb;
771 struct nlmsghdr *nlh;
772 int len = NLMSG_SPACE(size);
773 int flags = multi ? NLM_F_MULTI : 0;
774 int t = done ? NLMSG_DONE : type;
775
Mike Christie7b8631b2006-01-13 18:05:50 -0600776 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700777
Mike Christie7b8631b2006-01-13 18:05:50 -0600778 skb = mempool_zone_get_skb(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700779 /*
780 * FIXME:
781 * user is supposed to react on iferror == -ENOMEM;
782 * see iscsi_if_rx().
783 */
784 BUG_ON(!skb);
785
786 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
787 nlh->nlmsg_flags = flags;
788 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie7b8631b2006-01-13 18:05:50 -0600789 return iscsi_unicast_skb(z_reply, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700790}
791
792static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600793iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700794{
795 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
796 struct iscsi_stats *stats;
797 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600798 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700799 struct nlmsghdr *nlhstat;
800 struct iscsi_uevent *evstat;
801 int len = NLMSG_SPACE(sizeof(*ev) +
802 sizeof(struct iscsi_stats) +
803 sizeof(struct iscsi_stats_custom) *
804 ISCSI_STATS_CUSTOM_MAX);
805 int err = 0;
806
Mike Christieb5c7a122006-04-06 21:13:33 -0500807 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700808 if (!conn)
809 return -EEXIST;
810
811 do {
812 int actual_size;
813
Mike Christie7b8631b2006-01-13 18:05:50 -0600814 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700815
Mike Christie7b8631b2006-01-13 18:05:50 -0600816 skbstat = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700817 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600818 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
819 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700820 return -ENOMEM;
821 }
822
823 nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0,
824 (len - sizeof(*nlhstat)), 0);
825 evstat = NLMSG_DATA(nlhstat);
826 memset(evstat, 0, sizeof(*evstat));
827 evstat->transport_handle = iscsi_handle(conn->transport);
828 evstat->type = nlh->nlmsg_type;
Mike Christie7b8631b2006-01-13 18:05:50 -0600829 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700830 evstat->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500831 evstat->u.get_stats.cid =
832 ev->u.get_stats.cid;
833 evstat->u.get_stats.sid =
834 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700835 stats = (struct iscsi_stats *)
836 ((char*)evstat + sizeof(*evstat));
837 memset(stats, 0, sizeof(*stats));
838
Mike Christie7b7232f2006-02-01 21:06:49 -0600839 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700840 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
841 sizeof(struct iscsi_stats) +
842 sizeof(struct iscsi_stats_custom) *
843 stats->custom_length);
844 actual_size -= sizeof(*nlhstat);
845 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600846 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700847 nlhstat->nlmsg_len = actual_size;
848
Mike Christie7b8631b2006-01-13 18:05:50 -0600849 err = iscsi_unicast_skb(conn->z_pdu, skbstat);
Alex Aizman0896b752005-08-04 19:33:07 -0700850 } while (err < 0 && err != -ECONNREFUSED);
851
852 return err;
853}
854
855static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600856iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
857{
858 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600859 struct iscsi_cls_session *session;
Mike Christieb5c7a122006-04-06 21:13:33 -0500860 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -0600861
Mike Christie7b7232f2006-02-01 21:06:49 -0600862 session = transport->create_session(&priv->t,
863 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -0500864 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -0600865 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600866 return -ENOMEM;
867
Mike Christieb5c7a122006-04-06 21:13:33 -0500868 ev->r.c_session_ret.host_no = hostno;
869 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600870 return 0;
871}
872
873static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600874iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600875{
Mike Christie7b8631b2006-01-13 18:05:50 -0600876 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600877 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600878 unsigned long flags;
879
Mike Christieb5c7a122006-04-06 21:13:33 -0500880 session = iscsi_session_lookup(ev->u.c_conn.sid);
881 if (!session) {
882 printk(KERN_ERR "iscsi: invalid session %d\n",
883 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600884 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -0500885 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600886
Mike Christie7b7232f2006-02-01 21:06:49 -0600887 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -0500888 if (!conn) {
889 printk(KERN_ERR "iscsi: couldn't create a new "
890 "connection for session %d\n",
891 session->sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600892 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500893 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600894
895 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
896 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
897 sizeof(struct iscsi_hdr) +
898 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
899 Z_HIWAT_PDU);
900 if (!conn->z_pdu) {
901 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
902 "pdu zone for new conn\n");
903 goto destroy_conn;
904 }
905
906 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
907 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
908 Z_HIWAT_ERROR);
909 if (!conn->z_error) {
910 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
911 "error zone for new conn\n");
912 goto free_pdu_pool;
913 }
914
Mike Christieb5c7a122006-04-06 21:13:33 -0500915 ev->r.c_conn_ret.sid = session->sid;
916 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600917
918 spin_lock_irqsave(&connlock, flags);
919 list_add(&conn->conn_list, &connlist);
920 conn->active = 1;
921 spin_unlock_irqrestore(&connlock, flags);
922
Mike Christie7b8631b2006-01-13 18:05:50 -0600923 return 0;
924
925free_pdu_pool:
926 mempool_zone_destroy(conn->z_pdu);
927destroy_conn:
928 if (transport->destroy_conn)
929 transport->destroy_conn(conn->dd_data);
Mike Christie7b8631b2006-01-13 18:05:50 -0600930 return -ENOMEM;
931}
932
933static int
934iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
935{
936 unsigned long flags;
937 struct iscsi_cls_conn *conn;
938 struct mempool_zone *z_error, *z_pdu;
939
Mike Christieb5c7a122006-04-06 21:13:33 -0500940 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600941 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -0600942 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -0600943 spin_lock_irqsave(&connlock, flags);
944 conn->active = 0;
945 list_del(&conn->conn_list);
946 spin_unlock_irqrestore(&connlock, flags);
947
948 z_pdu = conn->z_pdu;
949 z_error = conn->z_error;
950
951 if (transport->destroy_conn)
952 transport->destroy_conn(conn);
953
954 mempool_zone_destroy(z_pdu);
955 mempool_zone_destroy(z_error);
956
957 return 0;
958}
959
Mike Christiefd7255f2006-04-06 21:13:36 -0500960static void
961iscsi_copy_param(struct iscsi_uevent *ev, uint32_t *value, char *data)
962{
963 if (ev->u.set_param.len != sizeof(uint32_t))
964 BUG();
965 memcpy(value, data, min_t(uint32_t, sizeof(uint32_t),
966 ev->u.set_param.len));
967}
968
969static int
970iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
971{
972 char *data = (char*)ev + sizeof(*ev);
973 struct iscsi_cls_conn *conn;
974 struct iscsi_cls_session *session;
975 int err = 0;
976 uint32_t value = 0;
977
978 session = iscsi_session_lookup(ev->u.set_param.sid);
979 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
980 if (!conn || !session)
981 return -EINVAL;
982
983 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -0500984 case ISCSI_PARAM_SESS_RECOVERY_TMO:
985 iscsi_copy_param(ev, &value, data);
986 if (value != 0)
987 session->recovery_tmo = value;
988 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500989 case ISCSI_PARAM_TARGET_NAME:
990 /* this should not change between logins */
991 if (session->targetname)
992 return 0;
993
994 session->targetname = kstrdup(data, GFP_KERNEL);
995 if (!session->targetname)
996 return -ENOMEM;
997 break;
998 case ISCSI_PARAM_TPGT:
999 iscsi_copy_param(ev, &value, data);
1000 session->tpgt = value;
1001 break;
1002 case ISCSI_PARAM_PERSISTENT_PORT:
1003 iscsi_copy_param(ev, &value, data);
1004 conn->persistent_port = value;
1005 break;
1006 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1007 /*
1008 * this is the address returned in discovery so it should
1009 * not change between logins.
1010 */
1011 if (conn->persistent_address)
1012 return 0;
1013
1014 conn->persistent_address = kstrdup(data, GFP_KERNEL);
1015 if (!conn->persistent_address)
1016 return -ENOMEM;
1017 break;
1018 default:
1019 iscsi_copy_param(ev, &value, data);
1020 err = transport->set_param(conn, ev->u.set_param.param, value);
1021 }
1022
1023 return err;
1024}
1025
Mike Christie7b8631b2006-01-13 18:05:50 -06001026static int
Alex Aizman0896b752005-08-04 19:33:07 -07001027iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1028{
1029 int err = 0;
1030 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1031 struct iscsi_transport *transport = NULL;
1032 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001033 struct iscsi_cls_session *session;
1034 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001035
Alex Aizman0896b752005-08-04 19:33:07 -07001036 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1037 if (!priv)
1038 return -EINVAL;
1039 transport = priv->iscsi_transport;
1040
Mike Christie7b7232f2006-02-01 21:06:49 -06001041 if (!try_module_get(transport->owner))
1042 return -EINVAL;
1043
Alex Aizman0896b752005-08-04 19:33:07 -07001044 switch (nlh->nlmsg_type) {
1045 case ISCSI_UEVENT_CREATE_SESSION:
1046 err = iscsi_if_create_session(priv, ev);
1047 break;
1048 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001049 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001050 if (session)
1051 transport->destroy_session(session);
1052 else
1053 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001054 break;
1055 case ISCSI_UEVENT_CREATE_CONN:
1056 err = iscsi_if_create_conn(transport, ev);
1057 break;
1058 case ISCSI_UEVENT_DESTROY_CONN:
1059 err = iscsi_if_destroy_conn(transport, ev);
1060 break;
1061 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001062 session = iscsi_session_lookup(ev->u.b_conn.sid);
1063 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001064
1065 if (session && conn)
1066 ev->r.retcode = transport->bind_conn(session, conn,
1067 ev->u.b_conn.transport_fd,
1068 ev->u.b_conn.is_leading);
1069 else
1070 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001071 break;
1072 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001073 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001074 break;
1075 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001076 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001077 if (conn)
1078 ev->r.retcode = transport->start_conn(conn);
1079 else
1080 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001081 break;
1082 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001083 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001084 if (conn)
1085 transport->stop_conn(conn, ev->u.stop_conn.flag);
1086 else
1087 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001088 break;
1089 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001090 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001091 if (conn)
1092 ev->r.retcode = transport->send_pdu(conn,
1093 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1094 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1095 ev->u.send_pdu.data_size);
1096 else
1097 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001098 break;
1099 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001100 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001101 break;
1102 default:
1103 err = -EINVAL;
1104 break;
1105 }
1106
Mike Christie7b7232f2006-02-01 21:06:49 -06001107 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001108 return err;
1109}
1110
Mike Christieb5c7a122006-04-06 21:13:33 -05001111/*
1112 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1113 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1114 * invalid creds are discarded silently.
1115 */
Alex Aizman0896b752005-08-04 19:33:07 -07001116static void
1117iscsi_if_rx(struct sock *sk, int len)
1118{
1119 struct sk_buff *skb;
1120
Arjan van de Ven0b950672006-01-11 13:16:10 +01001121 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001122 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Mike Christieee7f8e42006-02-01 21:07:01 -06001123 if (NETLINK_CREDS(skb)->uid) {
1124 skb_pull(skb, skb->len);
1125 goto free_skb;
1126 }
1127 daemon_pid = NETLINK_CREDS(skb)->pid;
1128
Alex Aizman0896b752005-08-04 19:33:07 -07001129 while (skb->len >= NLMSG_SPACE(0)) {
1130 int err;
1131 uint32_t rlen;
1132 struct nlmsghdr *nlh;
1133 struct iscsi_uevent *ev;
1134
1135 nlh = (struct nlmsghdr *)skb->data;
1136 if (nlh->nlmsg_len < sizeof(*nlh) ||
1137 skb->len < nlh->nlmsg_len) {
1138 break;
1139 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001140
Alex Aizman0896b752005-08-04 19:33:07 -07001141 ev = NLMSG_DATA(nlh);
1142 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1143 if (rlen > skb->len)
1144 rlen = skb->len;
Mike Christieee7f8e42006-02-01 21:07:01 -06001145
Alex Aizman0896b752005-08-04 19:33:07 -07001146 err = iscsi_if_recv_msg(skb, nlh);
1147 if (err) {
1148 ev->type = ISCSI_KEVENT_IF_ERROR;
1149 ev->iferror = err;
1150 }
1151 do {
1152 /*
1153 * special case for GET_STATS:
1154 * on success - sending reply and stats from
1155 * inside of if_recv_msg(),
1156 * on error - fall through.
1157 */
1158 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1159 break;
1160 err = iscsi_if_send_reply(
1161 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1162 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Mike Christie7b8631b2006-01-13 18:05:50 -06001163 if (atomic_read(&z_reply->allocated) >=
1164 z_reply->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -07001165 ev->iferror = -ENOMEM;
1166 } while (err < 0 && err != -ECONNREFUSED);
1167 skb_pull(skb, rlen);
1168 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001169free_skb:
Alex Aizman0896b752005-08-04 19:33:07 -07001170 kfree_skb(skb);
1171 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001172 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001173}
1174
Mike Christie7b8631b2006-01-13 18:05:50 -06001175#define iscsi_cdev_to_conn(_cdev) \
1176 iscsi_dev_to_conn(_cdev->dev)
1177
Mike Christiefd7255f2006-04-06 21:13:36 -05001178#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1179struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1180 __ATTR(_name,_mode,_show,_store)
1181
Alex Aizman0896b752005-08-04 19:33:07 -07001182/*
1183 * iSCSI connection attrs
1184 */
1185#define iscsi_conn_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -07001187show_conn_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001189 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001190 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1191 struct iscsi_transport *t = conn->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001193 t->get_conn_param(conn, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001194 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Alex Aizman0896b752005-08-04 19:33:07 -07001197#define iscsi_conn_int_attr(field, param, format) \
1198 iscsi_conn_int_attr_show(param, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001199static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_int_param_##param, \
1200 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Alex Aizman0896b752005-08-04 19:33:07 -07001202iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1203iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
1204iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1205iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1206iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1207iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001208iscsi_conn_int_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT, "%d");
1209iscsi_conn_int_attr(port, ISCSI_PARAM_CONN_PORT, "%d");
1210
1211#define iscsi_conn_str_attr_show(param) \
1212static ssize_t \
1213show_conn_str_param_##param(struct class_device *cdev, char *buf) \
1214{ \
1215 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1216 struct iscsi_transport *t = conn->transport; \
1217 return t->get_conn_str_param(conn, param, buf); \
1218}
1219
1220#define iscsi_conn_str_attr(field, param) \
1221 iscsi_conn_str_attr_show(param) \
1222static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_str_param_##param, \
1223 NULL);
1224
1225iscsi_conn_str_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1226iscsi_conn_str_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Mike Christie7b8631b2006-01-13 18:05:50 -06001228#define iscsi_cdev_to_session(_cdev) \
1229 iscsi_dev_to_session(_cdev->dev)
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
Alex Aizman0896b752005-08-04 19:33:07 -07001232 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Alex Aizman0896b752005-08-04 19:33:07 -07001234#define iscsi_session_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -07001236show_session_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001238 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001239 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
Mike Christie7b8631b2006-01-13 18:05:50 -06001240 struct iscsi_transport *t = session->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001242 t->get_session_param(session, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001243 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Alex Aizman0896b752005-08-04 19:33:07 -07001246#define iscsi_session_int_attr(field, param, format) \
1247 iscsi_session_int_attr_show(param, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001248static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_int_param_##param, \
1249 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Alex Aizman0896b752005-08-04 19:33:07 -07001251iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1252iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
1253iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
1254iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
1255iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1256iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1257iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1258iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001259iscsi_session_int_attr(tpgt, ISCSI_PARAM_TPGT, "%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Mike Christiefd7255f2006-04-06 21:13:36 -05001261#define iscsi_session_str_attr_show(param) \
1262static ssize_t \
1263show_session_str_param_##param(struct class_device *cdev, char *buf) \
1264{ \
1265 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1266 struct iscsi_transport *t = session->transport; \
1267 return t->get_session_str_param(session, param, buf); \
1268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Mike Christiefd7255f2006-04-06 21:13:36 -05001270#define iscsi_session_str_attr(field, param) \
1271 iscsi_session_str_attr_show(param) \
1272static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_str_param_##param, \
1273 NULL);
1274
1275iscsi_session_str_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1276
1277/*
1278 * Private session and conn attrs. userspace uses several iscsi values
1279 * to identify each session between reboots. Some of these values may not
1280 * be present in the iscsi_transport/LLD driver becuase userspace handles
1281 * login (and failback for login redirect) so for these type of drivers
1282 * the class manages the attrs and values for the iscsi_transport/LLD
1283 */
1284#define iscsi_priv_session_attr_show(field, format) \
1285static ssize_t \
1286show_priv_session_##field(struct class_device *cdev, char *buf) \
1287{ \
1288 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1289 return sprintf(buf, format"\n", session->field); \
1290}
1291
1292#define iscsi_priv_session_attr(field, format) \
1293 iscsi_priv_session_attr_show(field, format) \
1294static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1295 NULL)
1296iscsi_priv_session_attr(targetname, "%s");
1297iscsi_priv_session_attr(tpgt, "%d");
Mike Christie30a6c652006-04-06 21:13:39 -05001298iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001299
1300#define iscsi_priv_conn_attr_show(field, format) \
1301static ssize_t \
1302show_priv_conn_##field(struct class_device *cdev, char *buf) \
1303{ \
1304 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1305 return sprintf(buf, format"\n", conn->field); \
1306}
1307
1308#define iscsi_priv_conn_attr(field, format) \
1309 iscsi_priv_conn_attr_show(field, format) \
1310static ISCSI_CLASS_ATTR(priv_conn, field, S_IRUGO, show_priv_conn_##field, \
1311 NULL)
1312iscsi_priv_conn_attr(persistent_address, "%s");
1313iscsi_priv_conn_attr(persistent_port, "%d");
1314
1315#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1316do { \
1317 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1318 count++; \
1319} while (0)
1320
1321#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1322do { \
1323 if (tt->param_mask & param_flag) { \
1324 priv->session_attrs[count] = &class_device_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001326 } \
1327} while (0)
1328
1329#define SETUP_PRIV_CONN_RD_ATTR(field) \
1330do { \
1331 priv->conn_attrs[count] = &class_device_attr_priv_conn_##field; \
1332 count++; \
1333} while (0)
1334
1335#define SETUP_CONN_RD_ATTR(field, param_flag) \
1336do { \
1337 if (tt->param_mask & param_flag) { \
1338 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1339 count++; \
1340 } \
1341} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Alex Aizman0896b752005-08-04 19:33:07 -07001343static int iscsi_session_match(struct attribute_container *cont,
1344 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Mike Christie7b8631b2006-01-13 18:05:50 -06001346 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001348 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Alex Aizman0896b752005-08-04 19:33:07 -07001350 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352
Mike Christie7b8631b2006-01-13 18:05:50 -06001353 session = iscsi_dev_to_session(dev);
1354 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001355 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return 0;
1357
Alex Aizman0896b752005-08-04 19:33:07 -07001358 priv = to_iscsi_internal(shost->transportt);
1359 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1360 return 0;
1361
1362 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Alex Aizman0896b752005-08-04 19:33:07 -07001365static int iscsi_conn_match(struct attribute_container *cont,
1366 struct device *dev)
1367{
Mike Christie7b8631b2006-01-13 18:05:50 -06001368 struct iscsi_cls_session *session;
1369 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001370 struct Scsi_Host *shost;
1371 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Alex Aizman0896b752005-08-04 19:33:07 -07001373 if (!iscsi_is_conn_dev(dev))
1374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Mike Christie7b8631b2006-01-13 18:05:50 -06001376 conn = iscsi_dev_to_conn(dev);
1377 session = iscsi_dev_to_session(conn->dev.parent);
1378 shost = iscsi_session_to_shost(session);
1379
Alex Aizman0896b752005-08-04 19:33:07 -07001380 if (!shost->transportt)
1381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Alex Aizman0896b752005-08-04 19:33:07 -07001383 priv = to_iscsi_internal(shost->transportt);
1384 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1385 return 0;
1386
1387 return &priv->conn_cont.ac == cont;
1388}
1389
Mike Christie30a6c652006-04-06 21:13:39 -05001390static int iscsi_host_match(struct attribute_container *cont,
1391 struct device *dev)
1392{
1393 struct Scsi_Host *shost;
1394 struct iscsi_internal *priv;
1395
1396 if (!scsi_is_host_device(dev))
1397 return 0;
1398
1399 shost = dev_to_shost(dev);
1400 if (!shost->transportt ||
1401 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1402 return 0;
1403
1404 priv = to_iscsi_internal(shost->transportt);
1405 return &priv->t.host_attrs.ac == cont;
1406}
1407
Mike Christie7b8631b2006-01-13 18:05:50 -06001408struct scsi_transport_template *
1409iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001410{
1411 struct iscsi_internal *priv;
1412 unsigned long flags;
1413 int count = 0, err;
1414
1415 BUG_ON(!tt);
1416
1417 priv = iscsi_if_transport_lookup(tt);
1418 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001419 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001420
Jes Sorensen24669f752006-01-16 10:31:18 -05001421 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001422 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001423 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001424 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07001425 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001426 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001427
1428 priv->cdev.class = &iscsi_transport_class;
1429 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1430 err = class_device_register(&priv->cdev);
1431 if (err)
1432 goto free_priv;
1433
1434 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1435 if (err)
1436 goto unregister_cdev;
1437
Mike Christie30a6c652006-04-06 21:13:39 -05001438 /* host parameters */
1439 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1440 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1441 priv->t.host_attrs.ac.match = iscsi_host_match;
1442 priv->t.host_size = sizeof(struct iscsi_host);
1443 priv->host_attrs[0] = NULL;
1444 transport_container_register(&priv->t.host_attrs);
1445
Alex Aizman0896b752005-08-04 19:33:07 -07001446 /* connection parameters */
1447 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1448 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1449 priv->conn_cont.ac.match = iscsi_conn_match;
1450 transport_container_register(&priv->conn_cont);
1451
Mike Christiefd7255f2006-04-06 21:13:36 -05001452 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1453 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1454 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1455 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1456 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1457 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1458 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1459 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1460
1461 if (tt->param_mask & ISCSI_PERSISTENT_ADDRESS)
1462 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1463 else
1464 SETUP_PRIV_CONN_RD_ATTR(persistent_address);
1465
1466 if (tt->param_mask & ISCSI_PERSISTENT_PORT)
1467 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1468 else
1469 SETUP_PRIV_CONN_RD_ATTR(persistent_port);
Alex Aizman0896b752005-08-04 19:33:07 -07001470
1471 BUG_ON(count > ISCSI_CONN_ATTRS);
1472 priv->conn_attrs[count] = NULL;
1473 count = 0;
1474
1475 /* session parameters */
1476 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1477 priv->session_cont.ac.class = &iscsi_session_class.class;
1478 priv->session_cont.ac.match = iscsi_session_match;
1479 transport_container_register(&priv->session_cont);
1480
Mike Christiefd7255f2006-04-06 21:13:36 -05001481 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1482 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1483 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1484 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1485 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1486 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1487 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1488 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christie30a6c652006-04-06 21:13:39 -05001489 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christiefd7255f2006-04-06 21:13:36 -05001490
1491 if (tt->param_mask & ISCSI_TARGET_NAME)
1492 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1493 else
1494 SETUP_PRIV_SESSION_RD_ATTR(targetname);
1495
1496 if (tt->param_mask & ISCSI_TPGT)
1497 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1498 else
1499 SETUP_PRIV_SESSION_RD_ATTR(tpgt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001502 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Alex Aizman0896b752005-08-04 19:33:07 -07001504 spin_lock_irqsave(&iscsi_transport_lock, flags);
1505 list_add(&priv->list, &iscsi_transports);
1506 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Alex Aizman0896b752005-08-04 19:33:07 -07001508 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001509 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Alex Aizman0896b752005-08-04 19:33:07 -07001511unregister_cdev:
1512 class_device_unregister(&priv->cdev);
1513free_priv:
1514 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001515 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
Alex Aizman0896b752005-08-04 19:33:07 -07001517EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Alex Aizman0896b752005-08-04 19:33:07 -07001519int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Alex Aizman0896b752005-08-04 19:33:07 -07001521 struct iscsi_internal *priv;
1522 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Alex Aizman0896b752005-08-04 19:33:07 -07001524 BUG_ON(!tt);
1525
Arjan van de Ven0b950672006-01-11 13:16:10 +01001526 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001527
1528 priv = iscsi_if_transport_lookup(tt);
1529 BUG_ON (!priv);
1530
Alex Aizman0896b752005-08-04 19:33:07 -07001531 spin_lock_irqsave(&iscsi_transport_lock, flags);
1532 list_del(&priv->list);
1533 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1534
1535 transport_container_unregister(&priv->conn_cont);
1536 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001537 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001538
1539 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1540 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001541 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001542
1543 return 0;
1544}
1545EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1546
1547static int
1548iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1549{
1550 struct netlink_notify *n = ptr;
1551
1552 if (event == NETLINK_URELEASE &&
1553 n->protocol == NETLINK_ISCSI && n->pid) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001554 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001555 unsigned long flags;
1556
Mike Christie7b8631b2006-01-13 18:05:50 -06001557 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001558 spin_lock_irqsave(&connlock, flags);
1559 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001560 mempool_zone_complete(conn->z_error);
1561 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -07001562 }
1563 spin_unlock_irqrestore(&connlock, flags);
1564 }
1565
1566 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Alex Aizman0896b752005-08-04 19:33:07 -07001569static struct notifier_block iscsi_nl_notifier = {
1570 .notifier_call = iscsi_rcv_nl_event,
1571};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573static __init int iscsi_transport_init(void)
1574{
Alex Aizman0896b752005-08-04 19:33:07 -07001575 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Alex Aizman0896b752005-08-04 19:33:07 -07001577 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (err)
1579 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001580
Mike Christie30a6c652006-04-06 21:13:39 -05001581 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001582 if (err)
1583 goto unregister_transport_class;
1584
Mike Christie30a6c652006-04-06 21:13:39 -05001585 err = transport_class_register(&iscsi_connection_class);
1586 if (err)
1587 goto unregister_host_class;
1588
Alex Aizman0896b752005-08-04 19:33:07 -07001589 err = transport_class_register(&iscsi_session_class);
1590 if (err)
1591 goto unregister_conn_class;
1592
1593 err = netlink_register_notifier(&iscsi_nl_notifier);
1594 if (err)
1595 goto unregister_session_class;
1596
James Bottomley2290d2b2005-09-08 10:14:11 -05001597 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
Mike Christie7b8631b2006-01-13 18:05:50 -06001598 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001599 if (!nls) {
1600 err = -ENOBUFS;
1601 goto unregister_notifier;
1602 }
1603
Mike Christie7b8631b2006-01-13 18:05:50 -06001604 z_reply = mempool_zone_init(Z_MAX_REPLY,
Alex Aizman0896b752005-08-04 19:33:07 -07001605 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
Mike Christie7b8631b2006-01-13 18:05:50 -06001606 if (z_reply)
Alex Aizman0896b752005-08-04 19:33:07 -07001607 return 0;
1608
1609 sock_release(nls->sk_socket);
1610unregister_notifier:
1611 netlink_unregister_notifier(&iscsi_nl_notifier);
1612unregister_session_class:
1613 transport_class_unregister(&iscsi_session_class);
1614unregister_conn_class:
1615 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001616unregister_host_class:
1617 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001618unregister_transport_class:
1619 class_unregister(&iscsi_transport_class);
1620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623static void __exit iscsi_transport_exit(void)
1624{
Mike Christie7b8631b2006-01-13 18:05:50 -06001625 mempool_zone_destroy(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001626 sock_release(nls->sk_socket);
1627 netlink_unregister_notifier(&iscsi_nl_notifier);
1628 transport_class_unregister(&iscsi_connection_class);
1629 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001630 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001631 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634module_init(iscsi_transport_init);
1635module_exit(iscsi_transport_exit);
1636
Alex Aizman0896b752005-08-04 19:33:07 -07001637MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1638 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1639 "Alex Aizman <itn780@yahoo.com>");
1640MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641MODULE_LICENSE("GPL");