blob: 5569fdcfd621d3cee2363bc6415a7409dc501380 [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);
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
Or Gerlitzbe2df722006-05-02 19:46:43 -0500269 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
270 "out after %d secs\n", session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500271
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/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600475 * iscsi interface functions
476 */
Alex Aizman0896b752005-08-04 19:33:07 -0700477static struct iscsi_internal *
478iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Alex Aizman0896b752005-08-04 19:33:07 -0700480 struct iscsi_internal *priv;
481 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Alex Aizman0896b752005-08-04 19:33:07 -0700483 spin_lock_irqsave(&iscsi_transport_lock, flags);
484 list_for_each_entry(priv, &iscsi_transports, list) {
485 if (tt == priv->iscsi_transport) {
486 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
487 return priv;
488 }
489 }
490 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
491 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Alex Aizman0896b752005-08-04 19:33:07 -0700494static inline struct list_head *skb_to_lh(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Alex Aizman0896b752005-08-04 19:33:07 -0700496 return (struct list_head *)&skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
Alex Aizman0896b752005-08-04 19:33:07 -0700498
499static void*
Mike Christie28e55542006-02-01 21:07:11 -0600500mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
Alex Aizman0896b752005-08-04 19:33:07 -0700501{
502 struct mempool_zone *zone = pool_data;
503
504 return alloc_skb(zone->size, gfp_mask);
505}
506
507static void
508mempool_zone_free_skb(void *element, void *pool_data)
509{
510 kfree_skb(element);
511}
512
513static void
514mempool_zone_complete(struct mempool_zone *zone)
515{
516 unsigned long flags;
517 struct list_head *lh, *n;
518
519 spin_lock_irqsave(&zone->freelock, flags);
520 list_for_each_safe(lh, n, &zone->freequeue) {
521 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
522 offsetof(struct sk_buff, cb));
523 if (!skb_shared(skb)) {
524 list_del(skb_to_lh(skb));
525 mempool_free(skb, zone->pool);
526 atomic_dec(&zone->allocated);
527 }
528 }
529 spin_unlock_irqrestore(&zone->freelock, flags);
530}
531
Mike Christie7b8631b2006-01-13 18:05:50 -0600532static struct mempool_zone *
533mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700534{
Mike Christie7b8631b2006-01-13 18:05:50 -0600535 struct mempool_zone *zp;
536
537 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
538 if (!zp)
539 return NULL;
540
Mike Christie142e3012006-02-01 21:06:58 -0600541 zp->size = size;
542 zp->hiwat = hiwat;
543 INIT_LIST_HEAD(&zp->freequeue);
544 spin_lock_init(&zp->freelock);
545 atomic_set(&zp->allocated, 0);
546
Alex Aizman0896b752005-08-04 19:33:07 -0700547 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
548 mempool_zone_free_skb, zp);
Mike Christie7b8631b2006-01-13 18:05:50 -0600549 if (!zp->pool) {
550 kfree(zp);
551 return NULL;
552 }
Alex Aizman0896b752005-08-04 19:33:07 -0700553
Mike Christie7b8631b2006-01-13 18:05:50 -0600554 return zp;
Alex Aizman0896b752005-08-04 19:33:07 -0700555}
556
Mike Christie7b8631b2006-01-13 18:05:50 -0600557static void mempool_zone_destroy(struct mempool_zone *zp)
558{
559 mempool_destroy(zp->pool);
560 kfree(zp);
561}
Alex Aizman0896b752005-08-04 19:33:07 -0700562
563static struct sk_buff*
564mempool_zone_get_skb(struct mempool_zone *zone)
565{
566 struct sk_buff *skb;
567
568 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
569 if (skb)
570 atomic_inc(&zone->allocated);
571 return skb;
572}
573
574static int
Mike Christie790f39a2006-05-18 20:31:39 -0500575iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700576{
577 unsigned long flags;
578 int rc;
579
580 skb_get(skb);
Mike Christie790f39a2006-05-18 20:31:39 -0500581 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700582 if (rc < 0) {
583 mempool_free(skb, zone->pool);
584 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
585 return rc;
586 }
587
588 spin_lock_irqsave(&zone->freelock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500589 INIT_LIST_HEAD(skb_to_lh(skb));
Alex Aizman0896b752005-08-04 19:33:07 -0700590 list_add(skb_to_lh(skb), &zone->freequeue);
591 spin_unlock_irqrestore(&zone->freelock, flags);
592
593 return 0;
594}
595
Mike Christie7b7232f2006-02-01 21:06:49 -0600596int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700597 char *data, uint32_t data_size)
598{
599 struct nlmsghdr *nlh;
600 struct sk_buff *skb;
601 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700602 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500603 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700604 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
605 data_size);
606
Mike Christie790f39a2006-05-18 20:31:39 -0500607 priv = iscsi_if_transport_lookup(conn->transport);
608 if (!priv)
609 return -EINVAL;
610
Mike Christie7b8631b2006-01-13 18:05:50 -0600611 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700612
Mike Christie7b8631b2006-01-13 18:05:50 -0600613 skb = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700614 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600615 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600616 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
617 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700618 return -ENOMEM;
619 }
620
Mike Christie790f39a2006-05-18 20:31:39 -0500621 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700622 ev = NLMSG_DATA(nlh);
623 memset(ev, 0, sizeof(*ev));
624 ev->transport_handle = iscsi_handle(conn->transport);
625 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christie7b8631b2006-01-13 18:05:50 -0600626 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700627 ev->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500628 ev->r.recv_req.cid = conn->cid;
629 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700630 pdu = (char*)ev + sizeof(*ev);
631 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
632 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
633
Mike Christie790f39a2006-05-18 20:31:39 -0500634 return iscsi_unicast_skb(conn->z_pdu, skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700635}
636EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
637
Mike Christie7b7232f2006-02-01 21:06:49 -0600638void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700639{
640 struct nlmsghdr *nlh;
641 struct sk_buff *skb;
642 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500643 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700644 int len = NLMSG_SPACE(sizeof(*ev));
645
Mike Christie790f39a2006-05-18 20:31:39 -0500646 priv = iscsi_if_transport_lookup(conn->transport);
647 if (!priv)
648 return;
649
Mike Christie7b8631b2006-01-13 18:05:50 -0600650 mempool_zone_complete(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700651
Mike Christie7b8631b2006-01-13 18:05:50 -0600652 skb = mempool_zone_get_skb(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700653 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600654 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
655 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700656 return;
657 }
658
Mike Christie790f39a2006-05-18 20:31:39 -0500659 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700660 ev = NLMSG_DATA(nlh);
661 ev->transport_handle = iscsi_handle(conn->transport);
662 ev->type = ISCSI_KEVENT_CONN_ERROR;
Mike Christie7b8631b2006-01-13 18:05:50 -0600663 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700664 ev->iferror = -ENOMEM;
665 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500666 ev->r.connerror.cid = conn->cid;
667 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700668
Mike Christie790f39a2006-05-18 20:31:39 -0500669 iscsi_unicast_skb(conn->z_error, skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700670
Mike Christie7b8631b2006-01-13 18:05:50 -0600671 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
672 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700673}
674EXPORT_SYMBOL_GPL(iscsi_conn_error);
675
676static int
677iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
678 void *payload, int size)
679{
680 struct sk_buff *skb;
681 struct nlmsghdr *nlh;
682 int len = NLMSG_SPACE(size);
683 int flags = multi ? NLM_F_MULTI : 0;
684 int t = done ? NLMSG_DONE : type;
685
Mike Christie7b8631b2006-01-13 18:05:50 -0600686 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700687
Mike Christie7b8631b2006-01-13 18:05:50 -0600688 skb = mempool_zone_get_skb(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700689 /*
690 * FIXME:
691 * user is supposed to react on iferror == -ENOMEM;
692 * see iscsi_if_rx().
693 */
694 BUG_ON(!skb);
695
696 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
697 nlh->nlmsg_flags = flags;
698 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie790f39a2006-05-18 20:31:39 -0500699 return iscsi_unicast_skb(z_reply, skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700700}
701
702static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600703iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700704{
705 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
706 struct iscsi_stats *stats;
707 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600708 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700709 struct nlmsghdr *nlhstat;
710 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500711 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700712 int len = NLMSG_SPACE(sizeof(*ev) +
713 sizeof(struct iscsi_stats) +
714 sizeof(struct iscsi_stats_custom) *
715 ISCSI_STATS_CUSTOM_MAX);
716 int err = 0;
717
Mike Christie790f39a2006-05-18 20:31:39 -0500718 priv = iscsi_if_transport_lookup(transport);
719 if (!priv)
720 return -EINVAL;
721
Mike Christieb5c7a122006-04-06 21:13:33 -0500722 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700723 if (!conn)
724 return -EEXIST;
725
726 do {
727 int actual_size;
728
Mike Christie7b8631b2006-01-13 18:05:50 -0600729 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700730
Mike Christie7b8631b2006-01-13 18:05:50 -0600731 skbstat = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700732 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600733 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
734 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700735 return -ENOMEM;
736 }
737
Mike Christie790f39a2006-05-18 20:31:39 -0500738 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -0700739 (len - sizeof(*nlhstat)), 0);
740 evstat = NLMSG_DATA(nlhstat);
741 memset(evstat, 0, sizeof(*evstat));
742 evstat->transport_handle = iscsi_handle(conn->transport);
743 evstat->type = nlh->nlmsg_type;
Mike Christie7b8631b2006-01-13 18:05:50 -0600744 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700745 evstat->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500746 evstat->u.get_stats.cid =
747 ev->u.get_stats.cid;
748 evstat->u.get_stats.sid =
749 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700750 stats = (struct iscsi_stats *)
751 ((char*)evstat + sizeof(*evstat));
752 memset(stats, 0, sizeof(*stats));
753
Mike Christie7b7232f2006-02-01 21:06:49 -0600754 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700755 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
756 sizeof(struct iscsi_stats) +
757 sizeof(struct iscsi_stats_custom) *
758 stats->custom_length);
759 actual_size -= sizeof(*nlhstat);
760 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600761 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700762 nlhstat->nlmsg_len = actual_size;
763
Mike Christie790f39a2006-05-18 20:31:39 -0500764 err = iscsi_unicast_skb(conn->z_pdu, skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700765 } while (err < 0 && err != -ECONNREFUSED);
766
767 return err;
768}
769
770static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600771iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
772{
773 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600774 struct iscsi_cls_session *session;
Mike Christie7996a772006-04-06 21:13:41 -0500775 unsigned long flags;
Mike Christieb5c7a122006-04-06 21:13:33 -0500776 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -0600777
Mike Christie7996a772006-04-06 21:13:41 -0500778 session = transport->create_session(transport, &priv->t,
Mike Christie7b7232f2006-02-01 21:06:49 -0600779 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -0500780 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -0600781 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600782 return -ENOMEM;
783
Mike Christie7996a772006-04-06 21:13:41 -0500784 spin_lock_irqsave(&sesslock, flags);
785 list_add(&session->sess_list, &sesslist);
786 spin_unlock_irqrestore(&sesslock, flags);
787
Mike Christieb5c7a122006-04-06 21:13:33 -0500788 ev->r.c_session_ret.host_no = hostno;
789 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600790 return 0;
791}
792
793static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600794iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600795{
Mike Christie7b8631b2006-01-13 18:05:50 -0600796 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600797 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600798 unsigned long flags;
799
Mike Christieb5c7a122006-04-06 21:13:33 -0500800 session = iscsi_session_lookup(ev->u.c_conn.sid);
801 if (!session) {
802 printk(KERN_ERR "iscsi: invalid session %d\n",
803 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600804 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -0500805 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600806
Mike Christie7b7232f2006-02-01 21:06:49 -0600807 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -0500808 if (!conn) {
809 printk(KERN_ERR "iscsi: couldn't create a new "
810 "connection for session %d\n",
811 session->sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600812 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500813 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600814
815 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
816 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
817 sizeof(struct iscsi_hdr) +
818 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
819 Z_HIWAT_PDU);
820 if (!conn->z_pdu) {
821 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
822 "pdu zone for new conn\n");
823 goto destroy_conn;
824 }
825
826 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
827 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
828 Z_HIWAT_ERROR);
829 if (!conn->z_error) {
830 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
831 "error zone for new conn\n");
832 goto free_pdu_pool;
833 }
834
Mike Christieb5c7a122006-04-06 21:13:33 -0500835 ev->r.c_conn_ret.sid = session->sid;
836 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600837
838 spin_lock_irqsave(&connlock, flags);
839 list_add(&conn->conn_list, &connlist);
840 conn->active = 1;
841 spin_unlock_irqrestore(&connlock, flags);
842
Mike Christie7b8631b2006-01-13 18:05:50 -0600843 return 0;
844
845free_pdu_pool:
846 mempool_zone_destroy(conn->z_pdu);
847destroy_conn:
848 if (transport->destroy_conn)
849 transport->destroy_conn(conn->dd_data);
Mike Christie7b8631b2006-01-13 18:05:50 -0600850 return -ENOMEM;
851}
852
853static int
854iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
855{
856 unsigned long flags;
857 struct iscsi_cls_conn *conn;
858 struct mempool_zone *z_error, *z_pdu;
859
Mike Christieb5c7a122006-04-06 21:13:33 -0500860 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600861 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -0600862 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -0600863 spin_lock_irqsave(&connlock, flags);
864 conn->active = 0;
865 list_del(&conn->conn_list);
866 spin_unlock_irqrestore(&connlock, flags);
867
868 z_pdu = conn->z_pdu;
869 z_error = conn->z_error;
870
871 if (transport->destroy_conn)
872 transport->destroy_conn(conn);
873
874 mempool_zone_destroy(z_pdu);
875 mempool_zone_destroy(z_error);
876
877 return 0;
878}
879
Mike Christiefd7255f2006-04-06 21:13:36 -0500880static void
881iscsi_copy_param(struct iscsi_uevent *ev, uint32_t *value, char *data)
882{
883 if (ev->u.set_param.len != sizeof(uint32_t))
884 BUG();
885 memcpy(value, data, min_t(uint32_t, sizeof(uint32_t),
886 ev->u.set_param.len));
887}
888
889static int
890iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
891{
892 char *data = (char*)ev + sizeof(*ev);
893 struct iscsi_cls_conn *conn;
894 struct iscsi_cls_session *session;
895 int err = 0;
896 uint32_t value = 0;
897
898 session = iscsi_session_lookup(ev->u.set_param.sid);
899 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
900 if (!conn || !session)
901 return -EINVAL;
902
903 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -0500904 case ISCSI_PARAM_SESS_RECOVERY_TMO:
905 iscsi_copy_param(ev, &value, data);
906 if (value != 0)
907 session->recovery_tmo = value;
908 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500909 case ISCSI_PARAM_TARGET_NAME:
910 /* this should not change between logins */
911 if (session->targetname)
912 return 0;
913
914 session->targetname = kstrdup(data, GFP_KERNEL);
915 if (!session->targetname)
916 return -ENOMEM;
917 break;
918 case ISCSI_PARAM_TPGT:
919 iscsi_copy_param(ev, &value, data);
920 session->tpgt = value;
921 break;
922 case ISCSI_PARAM_PERSISTENT_PORT:
923 iscsi_copy_param(ev, &value, data);
924 conn->persistent_port = value;
925 break;
926 case ISCSI_PARAM_PERSISTENT_ADDRESS:
927 /*
928 * this is the address returned in discovery so it should
929 * not change between logins.
930 */
931 if (conn->persistent_address)
932 return 0;
933
934 conn->persistent_address = kstrdup(data, GFP_KERNEL);
935 if (!conn->persistent_address)
936 return -ENOMEM;
937 break;
938 default:
939 iscsi_copy_param(ev, &value, data);
940 err = transport->set_param(conn, ev->u.set_param.param, value);
941 }
942
943 return err;
944}
945
Mike Christie7b8631b2006-01-13 18:05:50 -0600946static int
Or Gerlitz264faaa2006-05-02 19:46:36 -0500947iscsi_if_transport_ep(struct iscsi_transport *transport,
948 struct iscsi_uevent *ev, int msg_type)
949{
950 struct sockaddr *dst_addr;
951 int rc = 0;
952
953 switch (msg_type) {
954 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
955 if (!transport->ep_connect)
956 return -EINVAL;
957
958 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
959 rc = transport->ep_connect(dst_addr,
960 ev->u.ep_connect.non_blocking,
961 &ev->r.ep_connect_ret.handle);
962 break;
963 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
964 if (!transport->ep_poll)
965 return -EINVAL;
966
967 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
968 ev->u.ep_poll.timeout_ms);
969 break;
970 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
971 if (!transport->ep_disconnect)
972 return -EINVAL;
973
974 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
975 break;
976 }
977 return rc;
978}
979
980static int
Alex Aizman0896b752005-08-04 19:33:07 -0700981iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
982{
983 int err = 0;
984 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
985 struct iscsi_transport *transport = NULL;
986 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -0600987 struct iscsi_cls_session *session;
988 struct iscsi_cls_conn *conn;
Mike Christie7996a772006-04-06 21:13:41 -0500989 unsigned long flags;
Alex Aizman0896b752005-08-04 19:33:07 -0700990
Alex Aizman0896b752005-08-04 19:33:07 -0700991 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
992 if (!priv)
993 return -EINVAL;
994 transport = priv->iscsi_transport;
995
Mike Christie7b7232f2006-02-01 21:06:49 -0600996 if (!try_module_get(transport->owner))
997 return -EINVAL;
998
Mike Christie790f39a2006-05-18 20:31:39 -0500999 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1000
Alex Aizman0896b752005-08-04 19:33:07 -07001001 switch (nlh->nlmsg_type) {
1002 case ISCSI_UEVENT_CREATE_SESSION:
1003 err = iscsi_if_create_session(priv, ev);
1004 break;
1005 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001006 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie7996a772006-04-06 21:13:41 -05001007 if (session) {
1008 spin_lock_irqsave(&sesslock, flags);
1009 list_del(&session->sess_list);
1010 spin_unlock_irqrestore(&sesslock, flags);
1011
Mike Christie7b7232f2006-02-01 21:06:49 -06001012 transport->destroy_session(session);
Mike Christie7996a772006-04-06 21:13:41 -05001013 } else
Mike Christie7b7232f2006-02-01 21:06:49 -06001014 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001015 break;
1016 case ISCSI_UEVENT_CREATE_CONN:
1017 err = iscsi_if_create_conn(transport, ev);
1018 break;
1019 case ISCSI_UEVENT_DESTROY_CONN:
1020 err = iscsi_if_destroy_conn(transport, ev);
1021 break;
1022 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001023 session = iscsi_session_lookup(ev->u.b_conn.sid);
1024 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001025
1026 if (session && conn)
1027 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001028 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001029 ev->u.b_conn.is_leading);
1030 else
1031 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001032 break;
1033 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001034 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001035 break;
1036 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001037 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001038 if (conn)
1039 ev->r.retcode = transport->start_conn(conn);
1040 else
1041 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001042 break;
1043 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001044 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001045 if (conn)
1046 transport->stop_conn(conn, ev->u.stop_conn.flag);
1047 else
1048 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001049 break;
1050 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001051 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001052 if (conn)
1053 ev->r.retcode = transport->send_pdu(conn,
1054 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1055 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1056 ev->u.send_pdu.data_size);
1057 else
1058 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001059 break;
1060 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001061 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001062 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001063 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1064 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1065 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1066 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1067 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001068 default:
1069 err = -EINVAL;
1070 break;
1071 }
1072
Mike Christie7b7232f2006-02-01 21:06:49 -06001073 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001074 return err;
1075}
1076
Mike Christieb5c7a122006-04-06 21:13:33 -05001077/*
1078 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1079 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1080 * invalid creds are discarded silently.
1081 */
Alex Aizman0896b752005-08-04 19:33:07 -07001082static void
1083iscsi_if_rx(struct sock *sk, int len)
1084{
1085 struct sk_buff *skb;
1086
Arjan van de Ven0b950672006-01-11 13:16:10 +01001087 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001088 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Mike Christieee7f8e42006-02-01 21:07:01 -06001089 if (NETLINK_CREDS(skb)->uid) {
1090 skb_pull(skb, skb->len);
1091 goto free_skb;
1092 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001093
Alex Aizman0896b752005-08-04 19:33:07 -07001094 while (skb->len >= NLMSG_SPACE(0)) {
1095 int err;
1096 uint32_t rlen;
1097 struct nlmsghdr *nlh;
1098 struct iscsi_uevent *ev;
1099
1100 nlh = (struct nlmsghdr *)skb->data;
1101 if (nlh->nlmsg_len < sizeof(*nlh) ||
1102 skb->len < nlh->nlmsg_len) {
1103 break;
1104 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001105
Alex Aizman0896b752005-08-04 19:33:07 -07001106 ev = NLMSG_DATA(nlh);
1107 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1108 if (rlen > skb->len)
1109 rlen = skb->len;
Mike Christieee7f8e42006-02-01 21:07:01 -06001110
Alex Aizman0896b752005-08-04 19:33:07 -07001111 err = iscsi_if_recv_msg(skb, nlh);
1112 if (err) {
1113 ev->type = ISCSI_KEVENT_IF_ERROR;
1114 ev->iferror = err;
1115 }
1116 do {
1117 /*
1118 * special case for GET_STATS:
1119 * on success - sending reply and stats from
1120 * inside of if_recv_msg(),
1121 * on error - fall through.
1122 */
1123 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1124 break;
1125 err = iscsi_if_send_reply(
1126 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1127 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Mike Christie7b8631b2006-01-13 18:05:50 -06001128 if (atomic_read(&z_reply->allocated) >=
1129 z_reply->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -07001130 ev->iferror = -ENOMEM;
1131 } while (err < 0 && err != -ECONNREFUSED);
1132 skb_pull(skb, rlen);
1133 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001134free_skb:
Alex Aizman0896b752005-08-04 19:33:07 -07001135 kfree_skb(skb);
1136 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001137 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001138}
1139
Mike Christie7b8631b2006-01-13 18:05:50 -06001140#define iscsi_cdev_to_conn(_cdev) \
1141 iscsi_dev_to_conn(_cdev->dev)
1142
Mike Christiefd7255f2006-04-06 21:13:36 -05001143#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1144struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1145 __ATTR(_name,_mode,_show,_store)
1146
Alex Aizman0896b752005-08-04 19:33:07 -07001147/*
1148 * iSCSI connection attrs
1149 */
1150#define iscsi_conn_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -07001152show_conn_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001154 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001155 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1156 struct iscsi_transport *t = conn->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001158 t->get_conn_param(conn, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001159 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Alex Aizman0896b752005-08-04 19:33:07 -07001162#define iscsi_conn_int_attr(field, param, format) \
1163 iscsi_conn_int_attr_show(param, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001164static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_int_param_##param, \
1165 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Alex Aizman0896b752005-08-04 19:33:07 -07001167iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1168iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
1169iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1170iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1171iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1172iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001173iscsi_conn_int_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT, "%d");
1174iscsi_conn_int_attr(port, ISCSI_PARAM_CONN_PORT, "%d");
Mike Christie8d2860b2006-05-02 19:46:47 -05001175iscsi_conn_int_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN, "%u");
Mike Christiefd7255f2006-04-06 21:13:36 -05001176
1177#define iscsi_conn_str_attr_show(param) \
1178static ssize_t \
1179show_conn_str_param_##param(struct class_device *cdev, char *buf) \
1180{ \
1181 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1182 struct iscsi_transport *t = conn->transport; \
1183 return t->get_conn_str_param(conn, param, buf); \
1184}
1185
1186#define iscsi_conn_str_attr(field, param) \
1187 iscsi_conn_str_attr_show(param) \
1188static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_str_param_##param, \
1189 NULL);
1190
1191iscsi_conn_str_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1192iscsi_conn_str_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Mike Christie7b8631b2006-01-13 18:05:50 -06001194#define iscsi_cdev_to_session(_cdev) \
1195 iscsi_dev_to_session(_cdev->dev)
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/*
Alex Aizman0896b752005-08-04 19:33:07 -07001198 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Alex Aizman0896b752005-08-04 19:33:07 -07001200#define iscsi_session_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -07001202show_session_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001204 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001205 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
Mike Christie7b8631b2006-01-13 18:05:50 -06001206 struct iscsi_transport *t = session->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001208 t->get_session_param(session, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001209 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Alex Aizman0896b752005-08-04 19:33:07 -07001212#define iscsi_session_int_attr(field, param, format) \
1213 iscsi_session_int_attr_show(param, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001214static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_int_param_##param, \
1215 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Alex Aizman0896b752005-08-04 19:33:07 -07001217iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1218iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
1219iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
1220iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
1221iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1222iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1223iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1224iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001225iscsi_session_int_attr(tpgt, ISCSI_PARAM_TPGT, "%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Mike Christiefd7255f2006-04-06 21:13:36 -05001227#define iscsi_session_str_attr_show(param) \
1228static ssize_t \
1229show_session_str_param_##param(struct class_device *cdev, char *buf) \
1230{ \
1231 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1232 struct iscsi_transport *t = session->transport; \
1233 return t->get_session_str_param(session, param, buf); \
1234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Mike Christiefd7255f2006-04-06 21:13:36 -05001236#define iscsi_session_str_attr(field, param) \
1237 iscsi_session_str_attr_show(param) \
1238static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_str_param_##param, \
1239 NULL);
1240
1241iscsi_session_str_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1242
1243/*
1244 * Private session and conn attrs. userspace uses several iscsi values
1245 * to identify each session between reboots. Some of these values may not
1246 * be present in the iscsi_transport/LLD driver becuase userspace handles
1247 * login (and failback for login redirect) so for these type of drivers
1248 * the class manages the attrs and values for the iscsi_transport/LLD
1249 */
1250#define iscsi_priv_session_attr_show(field, format) \
1251static ssize_t \
1252show_priv_session_##field(struct class_device *cdev, char *buf) \
1253{ \
1254 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1255 return sprintf(buf, format"\n", session->field); \
1256}
1257
1258#define iscsi_priv_session_attr(field, format) \
1259 iscsi_priv_session_attr_show(field, format) \
1260static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1261 NULL)
1262iscsi_priv_session_attr(targetname, "%s");
1263iscsi_priv_session_attr(tpgt, "%d");
Mike Christie30a6c652006-04-06 21:13:39 -05001264iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001265
1266#define iscsi_priv_conn_attr_show(field, format) \
1267static ssize_t \
1268show_priv_conn_##field(struct class_device *cdev, char *buf) \
1269{ \
1270 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1271 return sprintf(buf, format"\n", conn->field); \
1272}
1273
1274#define iscsi_priv_conn_attr(field, format) \
1275 iscsi_priv_conn_attr_show(field, format) \
1276static ISCSI_CLASS_ATTR(priv_conn, field, S_IRUGO, show_priv_conn_##field, \
1277 NULL)
1278iscsi_priv_conn_attr(persistent_address, "%s");
1279iscsi_priv_conn_attr(persistent_port, "%d");
1280
1281#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1282do { \
1283 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1284 count++; \
1285} while (0)
1286
1287#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1288do { \
1289 if (tt->param_mask & param_flag) { \
1290 priv->session_attrs[count] = &class_device_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001292 } \
1293} while (0)
1294
1295#define SETUP_PRIV_CONN_RD_ATTR(field) \
1296do { \
1297 priv->conn_attrs[count] = &class_device_attr_priv_conn_##field; \
1298 count++; \
1299} while (0)
1300
1301#define SETUP_CONN_RD_ATTR(field, param_flag) \
1302do { \
1303 if (tt->param_mask & param_flag) { \
1304 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1305 count++; \
1306 } \
1307} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Alex Aizman0896b752005-08-04 19:33:07 -07001309static int iscsi_session_match(struct attribute_container *cont,
1310 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Mike Christie7b8631b2006-01-13 18:05:50 -06001312 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001314 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Alex Aizman0896b752005-08-04 19:33:07 -07001316 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 0;
1318
Mike Christie7b8631b2006-01-13 18:05:50 -06001319 session = iscsi_dev_to_session(dev);
1320 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001321 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return 0;
1323
Alex Aizman0896b752005-08-04 19:33:07 -07001324 priv = to_iscsi_internal(shost->transportt);
1325 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1326 return 0;
1327
1328 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Alex Aizman0896b752005-08-04 19:33:07 -07001331static int iscsi_conn_match(struct attribute_container *cont,
1332 struct device *dev)
1333{
Mike Christie7b8631b2006-01-13 18:05:50 -06001334 struct iscsi_cls_session *session;
1335 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001336 struct Scsi_Host *shost;
1337 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Alex Aizman0896b752005-08-04 19:33:07 -07001339 if (!iscsi_is_conn_dev(dev))
1340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Mike Christie7b8631b2006-01-13 18:05:50 -06001342 conn = iscsi_dev_to_conn(dev);
1343 session = iscsi_dev_to_session(conn->dev.parent);
1344 shost = iscsi_session_to_shost(session);
1345
Alex Aizman0896b752005-08-04 19:33:07 -07001346 if (!shost->transportt)
1347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Alex Aizman0896b752005-08-04 19:33:07 -07001349 priv = to_iscsi_internal(shost->transportt);
1350 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1351 return 0;
1352
1353 return &priv->conn_cont.ac == cont;
1354}
1355
Mike Christie30a6c652006-04-06 21:13:39 -05001356static int iscsi_host_match(struct attribute_container *cont,
1357 struct device *dev)
1358{
1359 struct Scsi_Host *shost;
1360 struct iscsi_internal *priv;
1361
1362 if (!scsi_is_host_device(dev))
1363 return 0;
1364
1365 shost = dev_to_shost(dev);
1366 if (!shost->transportt ||
1367 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1368 return 0;
1369
1370 priv = to_iscsi_internal(shost->transportt);
1371 return &priv->t.host_attrs.ac == cont;
1372}
1373
Mike Christie7b8631b2006-01-13 18:05:50 -06001374struct scsi_transport_template *
1375iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001376{
1377 struct iscsi_internal *priv;
1378 unsigned long flags;
1379 int count = 0, err;
1380
1381 BUG_ON(!tt);
1382
1383 priv = iscsi_if_transport_lookup(tt);
1384 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001385 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001386
Jes Sorensen24669f752006-01-16 10:31:18 -05001387 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001388 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001389 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001390 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07001391 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001392 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001393
1394 priv->cdev.class = &iscsi_transport_class;
1395 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1396 err = class_device_register(&priv->cdev);
1397 if (err)
1398 goto free_priv;
1399
1400 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1401 if (err)
1402 goto unregister_cdev;
1403
Mike Christie30a6c652006-04-06 21:13:39 -05001404 /* host parameters */
1405 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1406 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1407 priv->t.host_attrs.ac.match = iscsi_host_match;
1408 priv->t.host_size = sizeof(struct iscsi_host);
1409 priv->host_attrs[0] = NULL;
1410 transport_container_register(&priv->t.host_attrs);
1411
Alex Aizman0896b752005-08-04 19:33:07 -07001412 /* connection parameters */
1413 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1414 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1415 priv->conn_cont.ac.match = iscsi_conn_match;
1416 transport_container_register(&priv->conn_cont);
1417
Mike Christiefd7255f2006-04-06 21:13:36 -05001418 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1419 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1420 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1421 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1422 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1423 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1424 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1425 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001426 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiefd7255f2006-04-06 21:13:36 -05001427
1428 if (tt->param_mask & ISCSI_PERSISTENT_ADDRESS)
1429 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1430 else
1431 SETUP_PRIV_CONN_RD_ATTR(persistent_address);
1432
1433 if (tt->param_mask & ISCSI_PERSISTENT_PORT)
1434 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1435 else
1436 SETUP_PRIV_CONN_RD_ATTR(persistent_port);
Alex Aizman0896b752005-08-04 19:33:07 -07001437
1438 BUG_ON(count > ISCSI_CONN_ATTRS);
1439 priv->conn_attrs[count] = NULL;
1440 count = 0;
1441
1442 /* session parameters */
1443 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1444 priv->session_cont.ac.class = &iscsi_session_class.class;
1445 priv->session_cont.ac.match = iscsi_session_match;
1446 transport_container_register(&priv->session_cont);
1447
Mike Christiefd7255f2006-04-06 21:13:36 -05001448 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1449 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1450 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1451 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1452 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1453 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1454 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1455 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christie30a6c652006-04-06 21:13:39 -05001456 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christiefd7255f2006-04-06 21:13:36 -05001457
1458 if (tt->param_mask & ISCSI_TARGET_NAME)
1459 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1460 else
1461 SETUP_PRIV_SESSION_RD_ATTR(targetname);
1462
1463 if (tt->param_mask & ISCSI_TPGT)
1464 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1465 else
1466 SETUP_PRIV_SESSION_RD_ATTR(tpgt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001469 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Alex Aizman0896b752005-08-04 19:33:07 -07001471 spin_lock_irqsave(&iscsi_transport_lock, flags);
1472 list_add(&priv->list, &iscsi_transports);
1473 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Alex Aizman0896b752005-08-04 19:33:07 -07001475 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001476 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Alex Aizman0896b752005-08-04 19:33:07 -07001478unregister_cdev:
1479 class_device_unregister(&priv->cdev);
1480free_priv:
1481 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001482 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
Alex Aizman0896b752005-08-04 19:33:07 -07001484EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alex Aizman0896b752005-08-04 19:33:07 -07001486int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Alex Aizman0896b752005-08-04 19:33:07 -07001488 struct iscsi_internal *priv;
1489 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Alex Aizman0896b752005-08-04 19:33:07 -07001491 BUG_ON(!tt);
1492
Arjan van de Ven0b950672006-01-11 13:16:10 +01001493 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001494
1495 priv = iscsi_if_transport_lookup(tt);
1496 BUG_ON (!priv);
1497
Alex Aizman0896b752005-08-04 19:33:07 -07001498 spin_lock_irqsave(&iscsi_transport_lock, flags);
1499 list_del(&priv->list);
1500 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1501
1502 transport_container_unregister(&priv->conn_cont);
1503 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001504 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001505
1506 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1507 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001508 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001509
1510 return 0;
1511}
1512EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1513
1514static int
1515iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1516{
1517 struct netlink_notify *n = ptr;
1518
1519 if (event == NETLINK_URELEASE &&
1520 n->protocol == NETLINK_ISCSI && n->pid) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001521 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001522 unsigned long flags;
1523
Mike Christie7b8631b2006-01-13 18:05:50 -06001524 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001525 spin_lock_irqsave(&connlock, flags);
1526 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001527 mempool_zone_complete(conn->z_error);
1528 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -07001529 }
1530 spin_unlock_irqrestore(&connlock, flags);
1531 }
1532
1533 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Alex Aizman0896b752005-08-04 19:33:07 -07001536static struct notifier_block iscsi_nl_notifier = {
1537 .notifier_call = iscsi_rcv_nl_event,
1538};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540static __init int iscsi_transport_init(void)
1541{
Alex Aizman0896b752005-08-04 19:33:07 -07001542 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Alex Aizman0896b752005-08-04 19:33:07 -07001544 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (err)
1546 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001547
Mike Christie30a6c652006-04-06 21:13:39 -05001548 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001549 if (err)
1550 goto unregister_transport_class;
1551
Mike Christie30a6c652006-04-06 21:13:39 -05001552 err = transport_class_register(&iscsi_connection_class);
1553 if (err)
1554 goto unregister_host_class;
1555
Alex Aizman0896b752005-08-04 19:33:07 -07001556 err = transport_class_register(&iscsi_session_class);
1557 if (err)
1558 goto unregister_conn_class;
1559
1560 err = netlink_register_notifier(&iscsi_nl_notifier);
1561 if (err)
1562 goto unregister_session_class;
1563
James Bottomley2290d2b2005-09-08 10:14:11 -05001564 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
Mike Christie7b8631b2006-01-13 18:05:50 -06001565 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001566 if (!nls) {
1567 err = -ENOBUFS;
1568 goto unregister_notifier;
1569 }
1570
Mike Christie7b8631b2006-01-13 18:05:50 -06001571 z_reply = mempool_zone_init(Z_MAX_REPLY,
Alex Aizman0896b752005-08-04 19:33:07 -07001572 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
Mike Christie7b8631b2006-01-13 18:05:50 -06001573 if (z_reply)
Alex Aizman0896b752005-08-04 19:33:07 -07001574 return 0;
1575
1576 sock_release(nls->sk_socket);
1577unregister_notifier:
1578 netlink_unregister_notifier(&iscsi_nl_notifier);
1579unregister_session_class:
1580 transport_class_unregister(&iscsi_session_class);
1581unregister_conn_class:
1582 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001583unregister_host_class:
1584 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001585unregister_transport_class:
1586 class_unregister(&iscsi_transport_class);
1587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590static void __exit iscsi_transport_exit(void)
1591{
Mike Christie7b8631b2006-01-13 18:05:50 -06001592 mempool_zone_destroy(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001593 sock_release(nls->sk_socket);
1594 netlink_unregister_notifier(&iscsi_nl_notifier);
1595 transport_class_unregister(&iscsi_connection_class);
1596 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001597 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001598 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601module_init(iscsi_transport_init);
1602module_exit(iscsi_transport_exit);
1603
Alex Aizman0896b752005-08-04 19:33:07 -07001604MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1605 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1606 "Alex Aizman <itn780@yahoo.com>");
1607MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608MODULE_LICENSE("GPL");