blob: 7963c0538de5155ee5bf69856e3a32e4db18f81c [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 Christie8d2860b2006-05-02 19:46:47 -050035#define ISCSI_CONN_ATTRS 11
Mike Christie30a6c652006-04-06 21:13:39 -050036#define ISCSI_HOST_ATTRS 0
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 Christieb5c7a122006-04-06 21:13:33 -050052static int iscsi_session_nr; /* sysfs session id for next new session */
53
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
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);
Mike Christie7b8631b2006-01-13 18:05:50 -0600231 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Mike Christie7b8631b2006-01-13 18:05:50 -0600233 shost = iscsi_session_to_shost(session);
234 scsi_host_put(shost);
235 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Mike Christie7b8631b2006-01-13 18:05:50 -0600238static int iscsi_is_session_dev(const struct device *dev)
239{
240 return dev->release == iscsi_session_release;
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Mike Christie30a6c652006-04-06 21:13:39 -0500243static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
244 uint id, uint lun)
245{
246 struct iscsi_host *ihost = shost->shost_data;
247 struct iscsi_cls_session *session;
248
249 mutex_lock(&ihost->mutex);
250 list_for_each_entry(session, &ihost->sessions, host_list) {
Mike Christiee6f3b632006-06-28 12:00:29 -0500251 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Mike Christie30a6c652006-04-06 21:13:39 -0500252 (id == SCAN_WILD_CARD || id == session->target_id))
Mike Christiee6f3b632006-06-28 12:00:29 -0500253 scsi_scan_target(&session->dev, 0,
Mike Christie30a6c652006-04-06 21:13:39 -0500254 session->target_id, lun, 1);
255 }
256 mutex_unlock(&ihost->mutex);
257
258 return 0;
259}
260
261static void session_recovery_timedout(void *data)
262{
263 struct iscsi_cls_session *session = data;
264
Or Gerlitzbe2df722006-05-02 19:46:43 -0500265 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
266 "out after %d secs\n", session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500267
268 if (session->transport->session_recovery_timedout)
269 session->transport->session_recovery_timedout(session);
270
271 scsi_target_unblock(&session->dev);
272}
273
274void iscsi_unblock_session(struct iscsi_cls_session *session)
275{
276 if (!cancel_delayed_work(&session->recovery_work))
277 flush_scheduled_work();
278 scsi_target_unblock(&session->dev);
279}
280EXPORT_SYMBOL_GPL(iscsi_unblock_session);
281
282void iscsi_block_session(struct iscsi_cls_session *session)
283{
284 scsi_target_block(&session->dev);
285 schedule_delayed_work(&session->recovery_work,
286 session->recovery_tmo * HZ);
287}
288EXPORT_SYMBOL_GPL(iscsi_block_session);
289
Mike Christie7b8631b2006-01-13 18:05:50 -0600290struct iscsi_cls_session *
Mike Christie8434aa82006-06-28 12:00:30 -0500291iscsi_alloc_session(struct Scsi_Host *shost,
292 struct iscsi_transport *transport)
Mike Christie7b8631b2006-01-13 18:05:50 -0600293{
294 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Mike Christieb5c7a122006-04-06 21:13:33 -0500296 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
297 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600298 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500299 return NULL;
300
Mike Christie7b8631b2006-01-13 18:05:50 -0600301 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500302 session->recovery_tmo = 120;
303 INIT_WORK(&session->recovery_work, session_recovery_timedout, session);
304 INIT_LIST_HEAD(&session->host_list);
305 INIT_LIST_HEAD(&session->sess_list);
Mike Christie7b8631b2006-01-13 18:05:50 -0600306
Mike Christie6a8a0d32006-06-28 12:00:31 -0500307 /* this is released in the dev's release function */
308 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500309 session->dev.parent = &shost->shost_gendev;
310 session->dev.release = iscsi_session_release;
311 device_initialize(&session->dev);
Mike Christieb5c7a122006-04-06 21:13:33 -0500312 if (transport->sessiondata_size)
313 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500314 return session;
315}
316EXPORT_SYMBOL_GPL(iscsi_alloc_session);
317
Mike Christie6a8a0d32006-06-28 12:00:31 -0500318int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500319{
320 struct Scsi_Host *shost = iscsi_session_to_shost(session);
321 struct iscsi_host *ihost;
322 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500323
Mike Christie30a6c652006-04-06 21:13:39 -0500324 ihost = shost->shost_data;
Mike Christieb5c7a122006-04-06 21:13:33 -0500325 session->sid = iscsi_session_nr++;
Mike Christie6a8a0d32006-06-28 12:00:31 -0500326 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -0500327
Mike Christieb5c7a122006-04-06 21:13:33 -0500328 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
329 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500330 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600331 if (err) {
332 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
333 "register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500334 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600335 }
336 transport_register_device(&session->dev);
337
Mike Christie30a6c652006-04-06 21:13:39 -0500338 mutex_lock(&ihost->mutex);
339 list_add(&session->host_list, &ihost->sessions);
340 mutex_unlock(&ihost->mutex);
Mike Christie8434aa82006-06-28 12:00:30 -0500341 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500342
Mike Christie8434aa82006-06-28 12:00:30 -0500343release_host:
344 scsi_host_put(shost);
345 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600346}
Mike Christie8434aa82006-06-28 12:00:30 -0500347EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600348
349/**
Mike Christie8434aa82006-06-28 12:00:30 -0500350 * iscsi_create_session - create iscsi class session
351 * @shost: scsi host
352 * @transport: iscsi transport
Mike Christie7b8631b2006-01-13 18:05:50 -0600353 *
Mike Christie8434aa82006-06-28 12:00:30 -0500354 * This can be called from a LLD or iscsi_transport.
Mike Christie7b8631b2006-01-13 18:05:50 -0600355 **/
Mike Christie8434aa82006-06-28 12:00:30 -0500356struct iscsi_cls_session *
357iscsi_create_session(struct Scsi_Host *shost,
Mike Christie6a8a0d32006-06-28 12:00:31 -0500358 struct iscsi_transport *transport,
359 unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500360{
361 struct iscsi_cls_session *session;
362
363 session = iscsi_alloc_session(shost, transport);
364 if (!session)
365 return NULL;
366
Mike Christie6a8a0d32006-06-28 12:00:31 -0500367 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500368 iscsi_free_session(session);
369 return NULL;
370 }
371 return session;
372}
373EXPORT_SYMBOL_GPL(iscsi_create_session);
374
375void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600376{
Mike Christie30a6c652006-04-06 21:13:39 -0500377 struct Scsi_Host *shost = iscsi_session_to_shost(session);
378 struct iscsi_host *ihost = shost->shost_data;
379
380 if (!cancel_delayed_work(&session->recovery_work))
381 flush_scheduled_work();
382
383 mutex_lock(&ihost->mutex);
384 list_del(&session->host_list);
385 mutex_unlock(&ihost->mutex);
386
Mike Christie8434aa82006-06-28 12:00:30 -0500387 scsi_remove_target(&session->dev);
388
Mike Christie7b8631b2006-01-13 18:05:50 -0600389 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500390 device_del(&session->dev);
391}
392EXPORT_SYMBOL_GPL(iscsi_remove_session);
393
394void iscsi_free_session(struct iscsi_cls_session *session)
395{
396 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600397}
398
Mike Christie8434aa82006-06-28 12:00:30 -0500399EXPORT_SYMBOL_GPL(iscsi_free_session);
400
401/**
402 * iscsi_destroy_session - destroy iscsi session
403 * @session: iscsi_session
404 *
405 * Can be called by a LLD or iscsi_transport. There must not be
406 * any running connections.
407 **/
408int iscsi_destroy_session(struct iscsi_cls_session *session)
409{
410 iscsi_remove_session(session);
411 iscsi_free_session(session);
412 return 0;
413}
Mike Christie7b8631b2006-01-13 18:05:50 -0600414EXPORT_SYMBOL_GPL(iscsi_destroy_session);
415
416static void iscsi_conn_release(struct device *dev)
417{
418 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
419 struct device *parent = conn->dev.parent;
420
421 kfree(conn);
422 put_device(parent);
423}
424
425static int iscsi_is_conn_dev(const struct device *dev)
426{
427 return dev->release == iscsi_conn_release;
428}
429
430/**
431 * iscsi_create_conn - create iscsi class connection
432 * @session: iscsi cls session
433 * @cid: connection id
434 *
435 * This can be called from a LLD or iscsi_transport. The connection
436 * is child of the session so cid must be unique for all connections
437 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500438 *
439 * Since we do not support MCS, cid will normally be zero. In some cases
440 * for software iscsi we could be trying to preallocate a connection struct
441 * in which case there could be two connection structs and cid would be
442 * non-zero.
Mike Christie7b8631b2006-01-13 18:05:50 -0600443 **/
444struct iscsi_cls_conn *
445iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
446{
447 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600448 struct iscsi_cls_conn *conn;
449 int err;
450
451 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
452 if (!conn)
453 return NULL;
454
455 if (transport->conndata_size)
456 conn->dd_data = &conn[1];
457
458 INIT_LIST_HEAD(&conn->conn_list);
459 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500460 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600461
462 /* this is released in the dev's release function */
463 if (!get_device(&session->dev))
464 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500465
Mike Christie7b8631b2006-01-13 18:05:50 -0600466 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500467 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600468 conn->dev.parent = &session->dev;
469 conn->dev.release = iscsi_conn_release;
470 err = device_register(&conn->dev);
471 if (err) {
472 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
473 "connection's dev\n");
474 goto release_parent_ref;
475 }
476 transport_register_device(&conn->dev);
477 return conn;
478
479release_parent_ref:
480 put_device(&session->dev);
481free_conn:
482 kfree(conn);
483 return NULL;
484}
485
486EXPORT_SYMBOL_GPL(iscsi_create_conn);
487
488/**
489 * iscsi_destroy_conn - destroy iscsi class connection
490 * @session: iscsi cls session
491 *
492 * This can be called from a LLD or iscsi_transport.
493 **/
494int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
495{
496 transport_unregister_device(&conn->dev);
497 device_unregister(&conn->dev);
498 return 0;
499}
500
501EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
502
503/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600504 * iscsi interface functions
505 */
Alex Aizman0896b752005-08-04 19:33:07 -0700506static struct iscsi_internal *
507iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Alex Aizman0896b752005-08-04 19:33:07 -0700509 struct iscsi_internal *priv;
510 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Alex Aizman0896b752005-08-04 19:33:07 -0700512 spin_lock_irqsave(&iscsi_transport_lock, flags);
513 list_for_each_entry(priv, &iscsi_transports, list) {
514 if (tt == priv->iscsi_transport) {
515 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
516 return priv;
517 }
518 }
519 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
520 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Alex Aizman0896b752005-08-04 19:33:07 -0700523static inline struct list_head *skb_to_lh(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Alex Aizman0896b752005-08-04 19:33:07 -0700525 return (struct list_head *)&skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
Alex Aizman0896b752005-08-04 19:33:07 -0700527
528static void*
Mike Christie28e55542006-02-01 21:07:11 -0600529mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
Alex Aizman0896b752005-08-04 19:33:07 -0700530{
531 struct mempool_zone *zone = pool_data;
532
533 return alloc_skb(zone->size, gfp_mask);
534}
535
536static void
537mempool_zone_free_skb(void *element, void *pool_data)
538{
539 kfree_skb(element);
540}
541
542static void
543mempool_zone_complete(struct mempool_zone *zone)
544{
545 unsigned long flags;
546 struct list_head *lh, *n;
547
548 spin_lock_irqsave(&zone->freelock, flags);
549 list_for_each_safe(lh, n, &zone->freequeue) {
550 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
551 offsetof(struct sk_buff, cb));
552 if (!skb_shared(skb)) {
553 list_del(skb_to_lh(skb));
554 mempool_free(skb, zone->pool);
555 atomic_dec(&zone->allocated);
556 }
557 }
558 spin_unlock_irqrestore(&zone->freelock, flags);
559}
560
Mike Christie7b8631b2006-01-13 18:05:50 -0600561static struct mempool_zone *
562mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700563{
Mike Christie7b8631b2006-01-13 18:05:50 -0600564 struct mempool_zone *zp;
565
566 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
567 if (!zp)
568 return NULL;
569
Mike Christie142e3012006-02-01 21:06:58 -0600570 zp->size = size;
571 zp->hiwat = hiwat;
572 INIT_LIST_HEAD(&zp->freequeue);
573 spin_lock_init(&zp->freelock);
574 atomic_set(&zp->allocated, 0);
575
Alex Aizman0896b752005-08-04 19:33:07 -0700576 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
577 mempool_zone_free_skb, zp);
Mike Christie7b8631b2006-01-13 18:05:50 -0600578 if (!zp->pool) {
579 kfree(zp);
580 return NULL;
581 }
Alex Aizman0896b752005-08-04 19:33:07 -0700582
Mike Christie7b8631b2006-01-13 18:05:50 -0600583 return zp;
Alex Aizman0896b752005-08-04 19:33:07 -0700584}
585
Mike Christie7b8631b2006-01-13 18:05:50 -0600586static void mempool_zone_destroy(struct mempool_zone *zp)
587{
588 mempool_destroy(zp->pool);
589 kfree(zp);
590}
Alex Aizman0896b752005-08-04 19:33:07 -0700591
592static struct sk_buff*
593mempool_zone_get_skb(struct mempool_zone *zone)
594{
595 struct sk_buff *skb;
596
597 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
598 if (skb)
599 atomic_inc(&zone->allocated);
600 return skb;
601}
602
603static int
Mike Christie790f39a2006-05-18 20:31:39 -0500604iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700605{
606 unsigned long flags;
607 int rc;
608
609 skb_get(skb);
Mike Christie790f39a2006-05-18 20:31:39 -0500610 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700611 if (rc < 0) {
612 mempool_free(skb, zone->pool);
613 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
614 return rc;
615 }
616
617 spin_lock_irqsave(&zone->freelock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500618 INIT_LIST_HEAD(skb_to_lh(skb));
Alex Aizman0896b752005-08-04 19:33:07 -0700619 list_add(skb_to_lh(skb), &zone->freequeue);
620 spin_unlock_irqrestore(&zone->freelock, flags);
621
622 return 0;
623}
624
Mike Christie7b7232f2006-02-01 21:06:49 -0600625int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700626 char *data, uint32_t data_size)
627{
628 struct nlmsghdr *nlh;
629 struct sk_buff *skb;
630 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700631 char *pdu;
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) + sizeof(struct iscsi_hdr) +
634 data_size);
635
Mike Christie790f39a2006-05-18 20:31:39 -0500636 priv = iscsi_if_transport_lookup(conn->transport);
637 if (!priv)
638 return -EINVAL;
639
Mike Christie7b8631b2006-01-13 18:05:50 -0600640 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700641
Mike Christie7b8631b2006-01-13 18:05:50 -0600642 skb = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700643 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600644 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600645 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
646 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700647 return -ENOMEM;
648 }
649
Mike Christie790f39a2006-05-18 20:31:39 -0500650 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700651 ev = NLMSG_DATA(nlh);
652 memset(ev, 0, sizeof(*ev));
653 ev->transport_handle = iscsi_handle(conn->transport);
654 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christie7b8631b2006-01-13 18:05:50 -0600655 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700656 ev->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500657 ev->r.recv_req.cid = conn->cid;
658 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700659 pdu = (char*)ev + sizeof(*ev);
660 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
661 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
662
Mike Christie790f39a2006-05-18 20:31:39 -0500663 return iscsi_unicast_skb(conn->z_pdu, skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700664}
665EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
666
Mike Christie7b7232f2006-02-01 21:06:49 -0600667void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700668{
669 struct nlmsghdr *nlh;
670 struct sk_buff *skb;
671 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500672 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700673 int len = NLMSG_SPACE(sizeof(*ev));
674
Mike Christie790f39a2006-05-18 20:31:39 -0500675 priv = iscsi_if_transport_lookup(conn->transport);
676 if (!priv)
677 return;
678
Mike Christie7b8631b2006-01-13 18:05:50 -0600679 mempool_zone_complete(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700680
Mike Christie7b8631b2006-01-13 18:05:50 -0600681 skb = mempool_zone_get_skb(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700682 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600683 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
684 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700685 return;
686 }
687
Mike Christie790f39a2006-05-18 20:31:39 -0500688 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700689 ev = NLMSG_DATA(nlh);
690 ev->transport_handle = iscsi_handle(conn->transport);
691 ev->type = ISCSI_KEVENT_CONN_ERROR;
Mike Christie7b8631b2006-01-13 18:05:50 -0600692 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700693 ev->iferror = -ENOMEM;
694 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500695 ev->r.connerror.cid = conn->cid;
696 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700697
Mike Christie790f39a2006-05-18 20:31:39 -0500698 iscsi_unicast_skb(conn->z_error, skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700699
Mike Christie7b8631b2006-01-13 18:05:50 -0600700 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
701 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700702}
703EXPORT_SYMBOL_GPL(iscsi_conn_error);
704
705static int
706iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
707 void *payload, int size)
708{
709 struct sk_buff *skb;
710 struct nlmsghdr *nlh;
711 int len = NLMSG_SPACE(size);
712 int flags = multi ? NLM_F_MULTI : 0;
713 int t = done ? NLMSG_DONE : type;
714
Mike Christie7b8631b2006-01-13 18:05:50 -0600715 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700716
Mike Christie7b8631b2006-01-13 18:05:50 -0600717 skb = mempool_zone_get_skb(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700718 /*
719 * FIXME:
720 * user is supposed to react on iferror == -ENOMEM;
721 * see iscsi_if_rx().
722 */
723 BUG_ON(!skb);
724
725 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
726 nlh->nlmsg_flags = flags;
727 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie790f39a2006-05-18 20:31:39 -0500728 return iscsi_unicast_skb(z_reply, skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700729}
730
731static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600732iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700733{
734 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
735 struct iscsi_stats *stats;
736 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600737 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700738 struct nlmsghdr *nlhstat;
739 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500740 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700741 int len = NLMSG_SPACE(sizeof(*ev) +
742 sizeof(struct iscsi_stats) +
743 sizeof(struct iscsi_stats_custom) *
744 ISCSI_STATS_CUSTOM_MAX);
745 int err = 0;
746
Mike Christie790f39a2006-05-18 20:31:39 -0500747 priv = iscsi_if_transport_lookup(transport);
748 if (!priv)
749 return -EINVAL;
750
Mike Christieb5c7a122006-04-06 21:13:33 -0500751 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700752 if (!conn)
753 return -EEXIST;
754
755 do {
756 int actual_size;
757
Mike Christie7b8631b2006-01-13 18:05:50 -0600758 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700759
Mike Christie7b8631b2006-01-13 18:05:50 -0600760 skbstat = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700761 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600762 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
763 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700764 return -ENOMEM;
765 }
766
Mike Christie790f39a2006-05-18 20:31:39 -0500767 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -0700768 (len - sizeof(*nlhstat)), 0);
769 evstat = NLMSG_DATA(nlhstat);
770 memset(evstat, 0, sizeof(*evstat));
771 evstat->transport_handle = iscsi_handle(conn->transport);
772 evstat->type = nlh->nlmsg_type;
Mike Christie7b8631b2006-01-13 18:05:50 -0600773 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700774 evstat->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500775 evstat->u.get_stats.cid =
776 ev->u.get_stats.cid;
777 evstat->u.get_stats.sid =
778 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700779 stats = (struct iscsi_stats *)
780 ((char*)evstat + sizeof(*evstat));
781 memset(stats, 0, sizeof(*stats));
782
Mike Christie7b7232f2006-02-01 21:06:49 -0600783 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700784 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
785 sizeof(struct iscsi_stats) +
786 sizeof(struct iscsi_stats_custom) *
787 stats->custom_length);
788 actual_size -= sizeof(*nlhstat);
789 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600790 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700791 nlhstat->nlmsg_len = actual_size;
792
Mike Christie790f39a2006-05-18 20:31:39 -0500793 err = iscsi_unicast_skb(conn->z_pdu, skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700794 } while (err < 0 && err != -ECONNREFUSED);
795
796 return err;
797}
798
799static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600800iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
801{
802 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600803 struct iscsi_cls_session *session;
Mike Christie7996a772006-04-06 21:13:41 -0500804 unsigned long flags;
Mike Christieb5c7a122006-04-06 21:13:33 -0500805 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -0600806
Mike Christie7996a772006-04-06 21:13:41 -0500807 session = transport->create_session(transport, &priv->t,
Mike Christie7b7232f2006-02-01 21:06:49 -0600808 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -0500809 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -0600810 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600811 return -ENOMEM;
812
Mike Christie7996a772006-04-06 21:13:41 -0500813 spin_lock_irqsave(&sesslock, flags);
814 list_add(&session->sess_list, &sesslist);
815 spin_unlock_irqrestore(&sesslock, flags);
816
Mike Christieb5c7a122006-04-06 21:13:33 -0500817 ev->r.c_session_ret.host_no = hostno;
818 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600819 return 0;
820}
821
822static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600823iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600824{
Mike Christie7b8631b2006-01-13 18:05:50 -0600825 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600826 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600827 unsigned long flags;
828
Mike Christieb5c7a122006-04-06 21:13:33 -0500829 session = iscsi_session_lookup(ev->u.c_conn.sid);
830 if (!session) {
831 printk(KERN_ERR "iscsi: invalid session %d\n",
832 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600833 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -0500834 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600835
Mike Christie7b7232f2006-02-01 21:06:49 -0600836 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -0500837 if (!conn) {
838 printk(KERN_ERR "iscsi: couldn't create a new "
839 "connection for session %d\n",
840 session->sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600841 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500842 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600843
844 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
845 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
846 sizeof(struct iscsi_hdr) +
847 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
848 Z_HIWAT_PDU);
849 if (!conn->z_pdu) {
850 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
851 "pdu zone for new conn\n");
852 goto destroy_conn;
853 }
854
855 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
856 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
857 Z_HIWAT_ERROR);
858 if (!conn->z_error) {
859 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
860 "error zone for new conn\n");
861 goto free_pdu_pool;
862 }
863
Mike Christieb5c7a122006-04-06 21:13:33 -0500864 ev->r.c_conn_ret.sid = session->sid;
865 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600866
867 spin_lock_irqsave(&connlock, flags);
868 list_add(&conn->conn_list, &connlist);
869 conn->active = 1;
870 spin_unlock_irqrestore(&connlock, flags);
871
Mike Christie7b8631b2006-01-13 18:05:50 -0600872 return 0;
873
874free_pdu_pool:
875 mempool_zone_destroy(conn->z_pdu);
876destroy_conn:
877 if (transport->destroy_conn)
878 transport->destroy_conn(conn->dd_data);
Mike Christie7b8631b2006-01-13 18:05:50 -0600879 return -ENOMEM;
880}
881
882static int
883iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
884{
885 unsigned long flags;
886 struct iscsi_cls_conn *conn;
887 struct mempool_zone *z_error, *z_pdu;
888
Mike Christieb5c7a122006-04-06 21:13:33 -0500889 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600890 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -0600891 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -0600892 spin_lock_irqsave(&connlock, flags);
893 conn->active = 0;
894 list_del(&conn->conn_list);
895 spin_unlock_irqrestore(&connlock, flags);
896
897 z_pdu = conn->z_pdu;
898 z_error = conn->z_error;
899
900 if (transport->destroy_conn)
901 transport->destroy_conn(conn);
902
903 mempool_zone_destroy(z_pdu);
904 mempool_zone_destroy(z_error);
905
906 return 0;
907}
908
Mike Christiefd7255f2006-04-06 21:13:36 -0500909static int
910iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
911{
912 char *data = (char*)ev + sizeof(*ev);
913 struct iscsi_cls_conn *conn;
914 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -0500915 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -0500916
917 session = iscsi_session_lookup(ev->u.set_param.sid);
918 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
919 if (!conn || !session)
920 return -EINVAL;
921
922 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -0500923 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -0500924 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -0500925 if (value != 0)
926 session->recovery_tmo = value;
927 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500928 default:
Mike Christiea54a52c2006-06-28 12:00:23 -0500929 err = transport->set_param(conn, ev->u.set_param.param,
930 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -0500931 }
932
933 return err;
934}
935
Mike Christie7b8631b2006-01-13 18:05:50 -0600936static int
Or Gerlitz264faaa2006-05-02 19:46:36 -0500937iscsi_if_transport_ep(struct iscsi_transport *transport,
938 struct iscsi_uevent *ev, int msg_type)
939{
940 struct sockaddr *dst_addr;
941 int rc = 0;
942
943 switch (msg_type) {
944 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
945 if (!transport->ep_connect)
946 return -EINVAL;
947
948 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
949 rc = transport->ep_connect(dst_addr,
950 ev->u.ep_connect.non_blocking,
951 &ev->r.ep_connect_ret.handle);
952 break;
953 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
954 if (!transport->ep_poll)
955 return -EINVAL;
956
957 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
958 ev->u.ep_poll.timeout_ms);
959 break;
960 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
961 if (!transport->ep_disconnect)
962 return -EINVAL;
963
964 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
965 break;
966 }
967 return rc;
968}
969
970static int
Mike Christie01cb2252006-06-28 12:00:22 -0500971iscsi_tgt_dscvr(struct iscsi_transport *transport,
972 struct iscsi_uevent *ev)
973{
974 struct sockaddr *dst_addr;
975
976 if (!transport->tgt_dscvr)
977 return -EINVAL;
978
979 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
980 return transport->tgt_dscvr(ev->u.tgt_dscvr.type,
981 ev->u.tgt_dscvr.host_no,
982 ev->u.tgt_dscvr.enable, dst_addr);
983}
984
985static int
Alex Aizman0896b752005-08-04 19:33:07 -0700986iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
987{
988 int err = 0;
989 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
990 struct iscsi_transport *transport = NULL;
991 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -0600992 struct iscsi_cls_session *session;
993 struct iscsi_cls_conn *conn;
Mike Christie7996a772006-04-06 21:13:41 -0500994 unsigned long flags;
Alex Aizman0896b752005-08-04 19:33:07 -0700995
Alex Aizman0896b752005-08-04 19:33:07 -0700996 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
997 if (!priv)
998 return -EINVAL;
999 transport = priv->iscsi_transport;
1000
Mike Christie7b7232f2006-02-01 21:06:49 -06001001 if (!try_module_get(transport->owner))
1002 return -EINVAL;
1003
Mike Christie790f39a2006-05-18 20:31:39 -05001004 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1005
Alex Aizman0896b752005-08-04 19:33:07 -07001006 switch (nlh->nlmsg_type) {
1007 case ISCSI_UEVENT_CREATE_SESSION:
1008 err = iscsi_if_create_session(priv, ev);
1009 break;
1010 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001011 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie7996a772006-04-06 21:13:41 -05001012 if (session) {
1013 spin_lock_irqsave(&sesslock, flags);
1014 list_del(&session->sess_list);
1015 spin_unlock_irqrestore(&sesslock, flags);
1016
Mike Christie7b7232f2006-02-01 21:06:49 -06001017 transport->destroy_session(session);
Mike Christie7996a772006-04-06 21:13:41 -05001018 } else
Mike Christie7b7232f2006-02-01 21:06:49 -06001019 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001020 break;
1021 case ISCSI_UEVENT_CREATE_CONN:
1022 err = iscsi_if_create_conn(transport, ev);
1023 break;
1024 case ISCSI_UEVENT_DESTROY_CONN:
1025 err = iscsi_if_destroy_conn(transport, ev);
1026 break;
1027 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001028 session = iscsi_session_lookup(ev->u.b_conn.sid);
1029 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001030
1031 if (session && conn)
1032 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001033 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001034 ev->u.b_conn.is_leading);
1035 else
1036 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001037 break;
1038 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001039 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001040 break;
1041 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001042 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001043 if (conn)
1044 ev->r.retcode = transport->start_conn(conn);
1045 else
1046 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001047 break;
1048 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001049 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001050 if (conn)
1051 transport->stop_conn(conn, ev->u.stop_conn.flag);
1052 else
1053 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001054 break;
1055 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001056 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001057 if (conn)
1058 ev->r.retcode = transport->send_pdu(conn,
1059 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1060 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1061 ev->u.send_pdu.data_size);
1062 else
1063 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001064 break;
1065 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001066 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001067 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001068 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1069 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1070 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1071 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1072 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001073 case ISCSI_UEVENT_TGT_DSCVR:
1074 err = iscsi_tgt_dscvr(transport, ev);
1075 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001076 default:
1077 err = -EINVAL;
1078 break;
1079 }
1080
Mike Christie7b7232f2006-02-01 21:06:49 -06001081 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001082 return err;
1083}
1084
Mike Christieb5c7a122006-04-06 21:13:33 -05001085/*
1086 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1087 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1088 * invalid creds are discarded silently.
1089 */
Alex Aizman0896b752005-08-04 19:33:07 -07001090static void
1091iscsi_if_rx(struct sock *sk, int len)
1092{
1093 struct sk_buff *skb;
1094
Arjan van de Ven0b950672006-01-11 13:16:10 +01001095 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001096 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Mike Christieee7f8e42006-02-01 21:07:01 -06001097 if (NETLINK_CREDS(skb)->uid) {
1098 skb_pull(skb, skb->len);
1099 goto free_skb;
1100 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001101
Alex Aizman0896b752005-08-04 19:33:07 -07001102 while (skb->len >= NLMSG_SPACE(0)) {
1103 int err;
1104 uint32_t rlen;
1105 struct nlmsghdr *nlh;
1106 struct iscsi_uevent *ev;
1107
1108 nlh = (struct nlmsghdr *)skb->data;
1109 if (nlh->nlmsg_len < sizeof(*nlh) ||
1110 skb->len < nlh->nlmsg_len) {
1111 break;
1112 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001113
Alex Aizman0896b752005-08-04 19:33:07 -07001114 ev = NLMSG_DATA(nlh);
1115 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1116 if (rlen > skb->len)
1117 rlen = skb->len;
Mike Christieee7f8e42006-02-01 21:07:01 -06001118
Alex Aizman0896b752005-08-04 19:33:07 -07001119 err = iscsi_if_recv_msg(skb, nlh);
1120 if (err) {
1121 ev->type = ISCSI_KEVENT_IF_ERROR;
1122 ev->iferror = err;
1123 }
1124 do {
1125 /*
1126 * special case for GET_STATS:
1127 * on success - sending reply and stats from
1128 * inside of if_recv_msg(),
1129 * on error - fall through.
1130 */
1131 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1132 break;
1133 err = iscsi_if_send_reply(
1134 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1135 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Mike Christie7b8631b2006-01-13 18:05:50 -06001136 if (atomic_read(&z_reply->allocated) >=
1137 z_reply->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -07001138 ev->iferror = -ENOMEM;
1139 } while (err < 0 && err != -ECONNREFUSED);
1140 skb_pull(skb, rlen);
1141 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001142free_skb:
Alex Aizman0896b752005-08-04 19:33:07 -07001143 kfree_skb(skb);
1144 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001145 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001146}
1147
Mike Christie7b8631b2006-01-13 18:05:50 -06001148#define iscsi_cdev_to_conn(_cdev) \
1149 iscsi_dev_to_conn(_cdev->dev)
1150
Mike Christiefd7255f2006-04-06 21:13:36 -05001151#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1152struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1153 __ATTR(_name,_mode,_show,_store)
1154
Alex Aizman0896b752005-08-04 19:33:07 -07001155/*
1156 * iSCSI connection attrs
1157 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001158#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001160show_conn_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001161{ \
1162 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1163 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001164 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001165}
1166
Mike Christiea54a52c2006-06-28 12:00:23 -05001167#define iscsi_conn_attr(field, param) \
1168 iscsi_conn_attr_show(param) \
1169static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001170 NULL);
1171
Mike Christiea54a52c2006-06-28 12:00:23 -05001172iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1173iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1174iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1175iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1176iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1177iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1178iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1179iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1180iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1181iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1182iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Mike Christie7b8631b2006-01-13 18:05:50 -06001184#define iscsi_cdev_to_session(_cdev) \
1185 iscsi_dev_to_session(_cdev->dev)
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/*
Alex Aizman0896b752005-08-04 19:33:07 -07001188 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001190#define iscsi_session_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001192show_session_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001193{ \
1194 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1195 struct iscsi_transport *t = session->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001196 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001197}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Mike Christiea54a52c2006-06-28 12:00:23 -05001199#define iscsi_session_attr(field, param) \
1200 iscsi_session_attr_show(param) \
1201static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001202 NULL);
1203
Mike Christiea54a52c2006-06-28 12:00:23 -05001204iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1205iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1206iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1207iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1208iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1209iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST);
1210iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1211iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN);
1212iscsi_session_attr(erl, ISCSI_PARAM_ERL);
1213iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT);
Mike Christiefd7255f2006-04-06 21:13:36 -05001214
Mike Christiefd7255f2006-04-06 21:13:36 -05001215#define iscsi_priv_session_attr_show(field, format) \
1216static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001217show_priv_session_##field(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001218{ \
Mike Christiea54a52c2006-06-28 12:00:23 -05001219 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
Mike Christiefd7255f2006-04-06 21:13:36 -05001220 return sprintf(buf, format"\n", session->field); \
1221}
1222
1223#define iscsi_priv_session_attr(field, format) \
1224 iscsi_priv_session_attr_show(field, format) \
1225static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1226 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001227iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001228
Mike Christiefd7255f2006-04-06 21:13:36 -05001229#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1230do { \
1231 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1232 count++; \
1233} while (0)
1234
Mike Christiea54a52c2006-06-28 12:00:23 -05001235
Mike Christiefd7255f2006-04-06 21:13:36 -05001236#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1237do { \
1238 if (tt->param_mask & param_flag) { \
1239 priv->session_attrs[count] = &class_device_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001241 } \
1242} while (0)
1243
Mike Christiefd7255f2006-04-06 21:13:36 -05001244#define SETUP_CONN_RD_ATTR(field, param_flag) \
1245do { \
1246 if (tt->param_mask & param_flag) { \
1247 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1248 count++; \
1249 } \
1250} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Alex Aizman0896b752005-08-04 19:33:07 -07001252static int iscsi_session_match(struct attribute_container *cont,
1253 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Mike Christie7b8631b2006-01-13 18:05:50 -06001255 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001257 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Alex Aizman0896b752005-08-04 19:33:07 -07001259 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return 0;
1261
Mike Christie7b8631b2006-01-13 18:05:50 -06001262 session = iscsi_dev_to_session(dev);
1263 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001264 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 0;
1266
Alex Aizman0896b752005-08-04 19:33:07 -07001267 priv = to_iscsi_internal(shost->transportt);
1268 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1269 return 0;
1270
1271 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Alex Aizman0896b752005-08-04 19:33:07 -07001274static int iscsi_conn_match(struct attribute_container *cont,
1275 struct device *dev)
1276{
Mike Christie7b8631b2006-01-13 18:05:50 -06001277 struct iscsi_cls_session *session;
1278 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001279 struct Scsi_Host *shost;
1280 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Alex Aizman0896b752005-08-04 19:33:07 -07001282 if (!iscsi_is_conn_dev(dev))
1283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Mike Christie7b8631b2006-01-13 18:05:50 -06001285 conn = iscsi_dev_to_conn(dev);
1286 session = iscsi_dev_to_session(conn->dev.parent);
1287 shost = iscsi_session_to_shost(session);
1288
Alex Aizman0896b752005-08-04 19:33:07 -07001289 if (!shost->transportt)
1290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Alex Aizman0896b752005-08-04 19:33:07 -07001292 priv = to_iscsi_internal(shost->transportt);
1293 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1294 return 0;
1295
1296 return &priv->conn_cont.ac == cont;
1297}
1298
Mike Christie30a6c652006-04-06 21:13:39 -05001299static int iscsi_host_match(struct attribute_container *cont,
1300 struct device *dev)
1301{
1302 struct Scsi_Host *shost;
1303 struct iscsi_internal *priv;
1304
1305 if (!scsi_is_host_device(dev))
1306 return 0;
1307
1308 shost = dev_to_shost(dev);
1309 if (!shost->transportt ||
1310 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1311 return 0;
1312
1313 priv = to_iscsi_internal(shost->transportt);
1314 return &priv->t.host_attrs.ac == cont;
1315}
1316
Mike Christie7b8631b2006-01-13 18:05:50 -06001317struct scsi_transport_template *
1318iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001319{
1320 struct iscsi_internal *priv;
1321 unsigned long flags;
1322 int count = 0, err;
1323
1324 BUG_ON(!tt);
1325
1326 priv = iscsi_if_transport_lookup(tt);
1327 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001328 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001329
Jes Sorensen24669f752006-01-16 10:31:18 -05001330 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001331 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001332 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001333 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07001334 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001335 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001336
1337 priv->cdev.class = &iscsi_transport_class;
1338 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1339 err = class_device_register(&priv->cdev);
1340 if (err)
1341 goto free_priv;
1342
1343 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1344 if (err)
1345 goto unregister_cdev;
1346
Mike Christie30a6c652006-04-06 21:13:39 -05001347 /* host parameters */
1348 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1349 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1350 priv->t.host_attrs.ac.match = iscsi_host_match;
1351 priv->t.host_size = sizeof(struct iscsi_host);
1352 priv->host_attrs[0] = NULL;
1353 transport_container_register(&priv->t.host_attrs);
1354
Alex Aizman0896b752005-08-04 19:33:07 -07001355 /* connection parameters */
1356 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1357 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1358 priv->conn_cont.ac.match = iscsi_conn_match;
1359 transport_container_register(&priv->conn_cont);
1360
Mike Christiefd7255f2006-04-06 21:13:36 -05001361 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1362 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1363 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1364 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1365 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1366 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1367 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1368 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001369 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001370 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1371 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Alex Aizman0896b752005-08-04 19:33:07 -07001372
1373 BUG_ON(count > ISCSI_CONN_ATTRS);
1374 priv->conn_attrs[count] = NULL;
1375 count = 0;
1376
1377 /* session parameters */
1378 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1379 priv->session_cont.ac.class = &iscsi_session_class.class;
1380 priv->session_cont.ac.match = iscsi_session_match;
1381 transport_container_register(&priv->session_cont);
1382
Mike Christiefd7255f2006-04-06 21:13:36 -05001383 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1384 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1385 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1386 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1387 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1388 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1389 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1390 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001391 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1392 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christie30a6c652006-04-06 21:13:39 -05001393 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christiefd7255f2006-04-06 21:13:36 -05001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001396 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Alex Aizman0896b752005-08-04 19:33:07 -07001398 spin_lock_irqsave(&iscsi_transport_lock, flags);
1399 list_add(&priv->list, &iscsi_transports);
1400 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Alex Aizman0896b752005-08-04 19:33:07 -07001402 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001403 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Alex Aizman0896b752005-08-04 19:33:07 -07001405unregister_cdev:
1406 class_device_unregister(&priv->cdev);
1407free_priv:
1408 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001409 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
Alex Aizman0896b752005-08-04 19:33:07 -07001411EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Alex Aizman0896b752005-08-04 19:33:07 -07001413int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Alex Aizman0896b752005-08-04 19:33:07 -07001415 struct iscsi_internal *priv;
1416 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Alex Aizman0896b752005-08-04 19:33:07 -07001418 BUG_ON(!tt);
1419
Arjan van de Ven0b950672006-01-11 13:16:10 +01001420 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001421
1422 priv = iscsi_if_transport_lookup(tt);
1423 BUG_ON (!priv);
1424
Alex Aizman0896b752005-08-04 19:33:07 -07001425 spin_lock_irqsave(&iscsi_transport_lock, flags);
1426 list_del(&priv->list);
1427 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1428
1429 transport_container_unregister(&priv->conn_cont);
1430 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001431 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001432
1433 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1434 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001435 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001436
1437 return 0;
1438}
1439EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1440
1441static int
1442iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1443{
1444 struct netlink_notify *n = ptr;
1445
1446 if (event == NETLINK_URELEASE &&
1447 n->protocol == NETLINK_ISCSI && n->pid) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001448 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001449 unsigned long flags;
1450
Mike Christie7b8631b2006-01-13 18:05:50 -06001451 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001452 spin_lock_irqsave(&connlock, flags);
1453 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001454 mempool_zone_complete(conn->z_error);
1455 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -07001456 }
1457 spin_unlock_irqrestore(&connlock, flags);
1458 }
1459
1460 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Alex Aizman0896b752005-08-04 19:33:07 -07001463static struct notifier_block iscsi_nl_notifier = {
1464 .notifier_call = iscsi_rcv_nl_event,
1465};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467static __init int iscsi_transport_init(void)
1468{
Alex Aizman0896b752005-08-04 19:33:07 -07001469 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Alex Aizman0896b752005-08-04 19:33:07 -07001471 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (err)
1473 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001474
Mike Christie30a6c652006-04-06 21:13:39 -05001475 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001476 if (err)
1477 goto unregister_transport_class;
1478
Mike Christie30a6c652006-04-06 21:13:39 -05001479 err = transport_class_register(&iscsi_connection_class);
1480 if (err)
1481 goto unregister_host_class;
1482
Alex Aizman0896b752005-08-04 19:33:07 -07001483 err = transport_class_register(&iscsi_session_class);
1484 if (err)
1485 goto unregister_conn_class;
1486
1487 err = netlink_register_notifier(&iscsi_nl_notifier);
1488 if (err)
1489 goto unregister_session_class;
1490
James Bottomley2290d2b2005-09-08 10:14:11 -05001491 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
Mike Christie7b8631b2006-01-13 18:05:50 -06001492 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001493 if (!nls) {
1494 err = -ENOBUFS;
1495 goto unregister_notifier;
1496 }
1497
Mike Christie7b8631b2006-01-13 18:05:50 -06001498 z_reply = mempool_zone_init(Z_MAX_REPLY,
Alex Aizman0896b752005-08-04 19:33:07 -07001499 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
Mike Christie7b8631b2006-01-13 18:05:50 -06001500 if (z_reply)
Alex Aizman0896b752005-08-04 19:33:07 -07001501 return 0;
1502
1503 sock_release(nls->sk_socket);
1504unregister_notifier:
1505 netlink_unregister_notifier(&iscsi_nl_notifier);
1506unregister_session_class:
1507 transport_class_unregister(&iscsi_session_class);
1508unregister_conn_class:
1509 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001510unregister_host_class:
1511 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001512unregister_transport_class:
1513 class_unregister(&iscsi_transport_class);
1514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
1517static void __exit iscsi_transport_exit(void)
1518{
Mike Christie7b8631b2006-01-13 18:05:50 -06001519 mempool_zone_destroy(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001520 sock_release(nls->sk_socket);
1521 netlink_unregister_notifier(&iscsi_nl_notifier);
1522 transport_class_unregister(&iscsi_connection_class);
1523 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001524 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001525 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528module_init(iscsi_transport_init);
1529module_exit(iscsi_transport_exit);
1530
Alex Aizman0896b752005-08-04 19:33:07 -07001531MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1532 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1533 "Alex Aizman <itn780@yahoo.com>");
1534MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535MODULE_LICENSE("GPL");