blob: 10ff0f0210ba27b0e2b4a7bde60e49788ab7e8f7 [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
Alex Aizman0896b752005-08-04 19:33:07 -070034#define ISCSI_SESSION_ATTRS 8
35#define ISCSI_CONN_ATTRS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct iscsi_internal {
38 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070039 struct iscsi_transport *iscsi_transport;
40 struct list_head list;
41 /*
Alex Aizman0896b752005-08-04 19:33:07 -070042 * based on transport capabilities, at register time we set these
43 * bits to tell the transport class it wants attributes displayed
44 * in sysfs or that it can support different iSCSI Data-Path
45 * capabilities
46 */
47 uint32_t param_mask;
48
49 struct class_device cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 /*
51 * We do not have any private or other attrs.
52 */
Alex Aizman0896b752005-08-04 19:33:07 -070053 struct transport_container conn_cont;
54 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
55 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
Mike Christieb5c7a122006-04-06 21:13:33 -050059static int iscsi_session_nr; /* sysfs session id for next new session */
60
Alex Aizman0896b752005-08-04 19:33:07 -070061/*
62 * list of registered transports and lock that must
63 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010064 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070065 */
66static LIST_HEAD(iscsi_transports);
67static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Alex Aizman0896b752005-08-04 19:33:07 -070069#define to_iscsi_internal(tmpl) \
70 container_of(tmpl, struct iscsi_internal, t)
71
72#define cdev_to_iscsi_internal(_cdev) \
73 container_of(_cdev, struct iscsi_internal, cdev)
74
75static void iscsi_transport_release(struct class_device *cdev)
76{
77 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
78 kfree(priv);
79}
80
81/*
82 * iscsi_transport_class represents the iscsi_transports that are
83 * registered.
84 */
85static struct class iscsi_transport_class = {
86 .name = "iscsi_transport",
87 .release = iscsi_transport_release,
88};
89
90static ssize_t
91show_transport_handle(struct class_device *cdev, char *buf)
92{
93 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
Mike Christie762e2bf2005-09-12 21:01:46 -050094 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070095}
96static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
97
98#define show_transport_attr(name, format) \
99static ssize_t \
100show_transport_##name(struct class_device *cdev, char *buf) \
101{ \
102 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
103 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
104} \
105static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
106
107show_transport_attr(caps, "0x%x");
108show_transport_attr(max_lun, "%d");
109show_transport_attr(max_conn, "%d");
110show_transport_attr(max_cmd_len, "%d");
111
112static struct attribute *iscsi_transport_attrs[] = {
113 &class_device_attr_handle.attr,
114 &class_device_attr_caps.attr,
115 &class_device_attr_max_lun.attr,
116 &class_device_attr_max_conn.attr,
117 &class_device_attr_max_cmd_len.attr,
118 NULL,
119};
120
121static struct attribute_group iscsi_transport_group = {
122 .attrs = iscsi_transport_attrs,
123};
124
125static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
126 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 NULL,
128 NULL,
129 NULL);
130
Alex Aizman0896b752005-08-04 19:33:07 -0700131static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
132 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 NULL,
134 NULL,
135 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700136
137static struct sock *nls;
138static int daemon_pid;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100139static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700140
141struct mempool_zone {
142 mempool_t *pool;
143 atomic_t allocated;
144 int size;
145 int hiwat;
146 struct list_head freequeue;
147 spinlock_t freelock;
148};
149
Mike Christie7b8631b2006-01-13 18:05:50 -0600150static struct mempool_zone *z_reply;
Alex Aizman0896b752005-08-04 19:33:07 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Alex Aizman0896b752005-08-04 19:33:07 -0700153 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
154 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
155 * so daemon will notice OOM on NETLINK tranposrt level and will
156 * be able to predict or change operational behavior
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
Alex Aizman0896b752005-08-04 19:33:07 -0700158#define Z_MAX_REPLY 8
159#define Z_HIWAT_REPLY 6
160#define Z_MAX_PDU 8
161#define Z_HIWAT_PDU 6
162#define Z_MAX_ERROR 16
163#define Z_HIWAT_ERROR 12
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Mike Christie7b7232f2006-02-01 21:06:49 -0600165static LIST_HEAD(sesslist);
166static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700167static LIST_HEAD(connlist);
168static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Mike Christieb5c7a122006-04-06 21:13:33 -0500170static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
171{
172 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
173 return sess->sid;
174}
175
176/*
177 * Returns the matching session to a given sid
178 */
179static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600180{
181 unsigned long flags;
182 struct iscsi_cls_session *sess;
183
184 spin_lock_irqsave(&sesslock, flags);
185 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500186 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600187 spin_unlock_irqrestore(&sesslock, flags);
188 return sess;
189 }
190 }
191 spin_unlock_irqrestore(&sesslock, flags);
192 return NULL;
193}
194
Mike Christieb5c7a122006-04-06 21:13:33 -0500195/*
196 * Returns the matching connection to a given sid / cid tuple
197 */
198static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600199{
200 unsigned long flags;
201 struct iscsi_cls_conn *conn;
202
203 spin_lock_irqsave(&connlock, flags);
204 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500205 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600206 spin_unlock_irqrestore(&connlock, flags);
207 return conn;
208 }
209 }
210 spin_unlock_irqrestore(&connlock, flags);
211 return NULL;
212}
213
Mike Christie7b8631b2006-01-13 18:05:50 -0600214/*
215 * The following functions can be used by LLDs that allocate
216 * their own scsi_hosts or by software iscsi LLDs
217 */
218static void iscsi_session_release(struct device *dev)
219{
220 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
221 struct iscsi_transport *transport = session->transport;
222 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Mike Christie7b8631b2006-01-13 18:05:50 -0600224 shost = iscsi_session_to_shost(session);
225 scsi_host_put(shost);
226 kfree(session);
227 module_put(transport->owner);
228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Mike Christie7b8631b2006-01-13 18:05:50 -0600230static int iscsi_is_session_dev(const struct device *dev)
231{
232 return dev->release == iscsi_session_release;
233}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Mike Christie7b8631b2006-01-13 18:05:50 -0600235/**
236 * iscsi_create_session - create iscsi class session
237 * @shost: scsi host
238 * @transport: iscsi transport
239 *
Mike Christieb5c7a122006-04-06 21:13:33 -0500240 * This can be called from a LLD or iscsi_transport.
Mike Christie7b8631b2006-01-13 18:05:50 -0600241 **/
242struct iscsi_cls_session *
243iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport)
244{
245 struct iscsi_cls_session *session;
246 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Mike Christie7b8631b2006-01-13 18:05:50 -0600248 if (!try_module_get(transport->owner))
249 return NULL;
250
Mike Christieb5c7a122006-04-06 21:13:33 -0500251 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
252 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600253 if (!session)
254 goto module_put;
255 session->transport = transport;
256
Mike Christieb5c7a122006-04-06 21:13:33 -0500257 if (transport->sessiondata_size)
258 session->dd_data = &session[1];
259
Mike Christie7b8631b2006-01-13 18:05:50 -0600260 /* this is released in the dev's release function */
261 scsi_host_get(shost);
Mike Christieb5c7a122006-04-06 21:13:33 -0500262 session->sid = iscsi_session_nr++;
263 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
264 session->sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600265 session->dev.parent = &shost->shost_gendev;
266 session->dev.release = iscsi_session_release;
267 err = device_register(&session->dev);
268 if (err) {
269 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
270 "register session's dev\n");
271 goto free_session;
272 }
273 transport_register_device(&session->dev);
274
275 return session;
276
277free_session:
278 kfree(session);
279module_put:
280 module_put(transport->owner);
281 return NULL;
282}
283
284EXPORT_SYMBOL_GPL(iscsi_create_session);
285
286/**
287 * iscsi_destroy_session - destroy iscsi session
288 * @session: iscsi_session
289 *
290 * Can be called by a LLD or iscsi_transport. There must not be
291 * any running connections.
292 **/
293int iscsi_destroy_session(struct iscsi_cls_session *session)
294{
295 transport_unregister_device(&session->dev);
296 device_unregister(&session->dev);
297 return 0;
298}
299
300EXPORT_SYMBOL_GPL(iscsi_destroy_session);
301
302static void iscsi_conn_release(struct device *dev)
303{
304 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
305 struct device *parent = conn->dev.parent;
306
307 kfree(conn);
308 put_device(parent);
309}
310
311static int iscsi_is_conn_dev(const struct device *dev)
312{
313 return dev->release == iscsi_conn_release;
314}
315
316/**
317 * iscsi_create_conn - create iscsi class connection
318 * @session: iscsi cls session
319 * @cid: connection id
320 *
321 * This can be called from a LLD or iscsi_transport. The connection
322 * is child of the session so cid must be unique for all connections
323 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500324 *
325 * Since we do not support MCS, cid will normally be zero. In some cases
326 * for software iscsi we could be trying to preallocate a connection struct
327 * in which case there could be two connection structs and cid would be
328 * non-zero.
Mike Christie7b8631b2006-01-13 18:05:50 -0600329 **/
330struct iscsi_cls_conn *
331iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
332{
333 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600334 struct iscsi_cls_conn *conn;
335 int err;
336
337 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
338 if (!conn)
339 return NULL;
340
341 if (transport->conndata_size)
342 conn->dd_data = &conn[1];
343
344 INIT_LIST_HEAD(&conn->conn_list);
345 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500346 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600347
348 /* this is released in the dev's release function */
349 if (!get_device(&session->dev))
350 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500351
Mike Christie7b8631b2006-01-13 18:05:50 -0600352 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500353 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600354 conn->dev.parent = &session->dev;
355 conn->dev.release = iscsi_conn_release;
356 err = device_register(&conn->dev);
357 if (err) {
358 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
359 "connection's dev\n");
360 goto release_parent_ref;
361 }
362 transport_register_device(&conn->dev);
363 return conn;
364
365release_parent_ref:
366 put_device(&session->dev);
367free_conn:
368 kfree(conn);
369 return NULL;
370}
371
372EXPORT_SYMBOL_GPL(iscsi_create_conn);
373
374/**
375 * iscsi_destroy_conn - destroy iscsi class connection
376 * @session: iscsi cls session
377 *
378 * This can be called from a LLD or iscsi_transport.
379 **/
380int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
381{
382 transport_unregister_device(&conn->dev);
383 device_unregister(&conn->dev);
384 return 0;
385}
386
387EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
388
389/*
390 * These functions are used only by software iscsi_transports
391 * which do not allocate and more their scsi_hosts since this
392 * is initiated from userspace.
393 */
394
395/*
396 * iSCSI Session's hostdata organization:
397 *
398 * *------------------* <== hostdata_session(host->hostdata)
399 * | ptr to class sess|
400 * |------------------| <== iscsi_hostdata(host->hostdata)
401 * | transport's data |
402 * *------------------*
403 */
404
405#define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
406 _t->hostdata_size % sizeof(unsigned long))
407
408#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
409
410/**
411 * iscsi_transport_create_session - create iscsi cls session and host
412 * scsit: scsi transport template
413 * transport: iscsi transport template
414 *
415 * This can be used by software iscsi_transports that allocate
416 * a session per scsi host.
417 **/
418struct Scsi_Host *
419iscsi_transport_create_session(struct scsi_transport_template *scsit,
420 struct iscsi_transport *transport)
421{
422 struct iscsi_cls_session *session;
423 struct Scsi_Host *shost;
Mike Christie7b7232f2006-02-01 21:06:49 -0600424 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600425
426 shost = scsi_host_alloc(transport->host_template,
427 hostdata_privsize(transport));
428 if (!shost) {
429 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
430 "session\n");
431 return NULL;
432 }
433
434 shost->max_id = 1;
435 shost->max_channel = 0;
436 shost->max_lun = transport->max_lun;
437 shost->max_cmd_len = transport->max_cmd_len;
438 shost->transportt = scsit;
Mike Christie55e32992006-01-13 18:05:53 -0600439 shost->transportt->create_work_queue = 1;
Mike Christie7b8631b2006-01-13 18:05:50 -0600440
441 if (scsi_add_host(shost, NULL))
442 goto free_host;
443
444 session = iscsi_create_session(shost, transport);
445 if (!session)
446 goto remove_host;
447
448 *(unsigned long*)shost->hostdata = (unsigned long)session;
Mike Christie7b7232f2006-02-01 21:06:49 -0600449 spin_lock_irqsave(&sesslock, flags);
450 list_add(&session->sess_list, &sesslist);
451 spin_unlock_irqrestore(&sesslock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600452 return shost;
453
454remove_host:
455 scsi_remove_host(shost);
456free_host:
457 scsi_host_put(shost);
458 return NULL;
459}
460
461EXPORT_SYMBOL_GPL(iscsi_transport_create_session);
462
463/**
464 * iscsi_transport_destroy_session - destroy session and scsi host
465 * shost: scsi host
466 *
467 * This can be used by software iscsi_transports that allocate
468 * a session per scsi host.
469 **/
470int iscsi_transport_destroy_session(struct Scsi_Host *shost)
471{
472 struct iscsi_cls_session *session;
Mike Christie7b7232f2006-02-01 21:06:49 -0600473 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600474
475 scsi_remove_host(shost);
476 session = hostdata_session(shost->hostdata);
Mike Christie7b7232f2006-02-01 21:06:49 -0600477 spin_lock_irqsave(&sesslock, flags);
478 list_del(&session->sess_list);
479 spin_unlock_irqrestore(&sesslock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600480 iscsi_destroy_session(session);
481 /* ref from host alloc */
482 scsi_host_put(shost);
483 return 0;
484}
485
486EXPORT_SYMBOL_GPL(iscsi_transport_destroy_session);
487
488/*
489 * iscsi interface functions
490 */
Alex Aizman0896b752005-08-04 19:33:07 -0700491static struct iscsi_internal *
492iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Alex Aizman0896b752005-08-04 19:33:07 -0700494 struct iscsi_internal *priv;
495 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Alex Aizman0896b752005-08-04 19:33:07 -0700497 spin_lock_irqsave(&iscsi_transport_lock, flags);
498 list_for_each_entry(priv, &iscsi_transports, list) {
499 if (tt == priv->iscsi_transport) {
500 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
501 return priv;
502 }
503 }
504 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
505 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Alex Aizman0896b752005-08-04 19:33:07 -0700508static inline struct list_head *skb_to_lh(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Alex Aizman0896b752005-08-04 19:33:07 -0700510 return (struct list_head *)&skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
Alex Aizman0896b752005-08-04 19:33:07 -0700512
513static void*
Mike Christie28e55542006-02-01 21:07:11 -0600514mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
Alex Aizman0896b752005-08-04 19:33:07 -0700515{
516 struct mempool_zone *zone = pool_data;
517
518 return alloc_skb(zone->size, gfp_mask);
519}
520
521static void
522mempool_zone_free_skb(void *element, void *pool_data)
523{
524 kfree_skb(element);
525}
526
527static void
528mempool_zone_complete(struct mempool_zone *zone)
529{
530 unsigned long flags;
531 struct list_head *lh, *n;
532
533 spin_lock_irqsave(&zone->freelock, flags);
534 list_for_each_safe(lh, n, &zone->freequeue) {
535 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
536 offsetof(struct sk_buff, cb));
537 if (!skb_shared(skb)) {
538 list_del(skb_to_lh(skb));
539 mempool_free(skb, zone->pool);
540 atomic_dec(&zone->allocated);
541 }
542 }
543 spin_unlock_irqrestore(&zone->freelock, flags);
544}
545
Mike Christie7b8631b2006-01-13 18:05:50 -0600546static struct mempool_zone *
547mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700548{
Mike Christie7b8631b2006-01-13 18:05:50 -0600549 struct mempool_zone *zp;
550
551 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
552 if (!zp)
553 return NULL;
554
Mike Christie142e3012006-02-01 21:06:58 -0600555 zp->size = size;
556 zp->hiwat = hiwat;
557 INIT_LIST_HEAD(&zp->freequeue);
558 spin_lock_init(&zp->freelock);
559 atomic_set(&zp->allocated, 0);
560
Alex Aizman0896b752005-08-04 19:33:07 -0700561 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
562 mempool_zone_free_skb, zp);
Mike Christie7b8631b2006-01-13 18:05:50 -0600563 if (!zp->pool) {
564 kfree(zp);
565 return NULL;
566 }
Alex Aizman0896b752005-08-04 19:33:07 -0700567
Mike Christie7b8631b2006-01-13 18:05:50 -0600568 return zp;
Alex Aizman0896b752005-08-04 19:33:07 -0700569}
570
Mike Christie7b8631b2006-01-13 18:05:50 -0600571static void mempool_zone_destroy(struct mempool_zone *zp)
572{
573 mempool_destroy(zp->pool);
574 kfree(zp);
575}
Alex Aizman0896b752005-08-04 19:33:07 -0700576
577static struct sk_buff*
578mempool_zone_get_skb(struct mempool_zone *zone)
579{
580 struct sk_buff *skb;
581
582 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
583 if (skb)
584 atomic_inc(&zone->allocated);
585 return skb;
586}
587
588static int
589iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
590{
591 unsigned long flags;
592 int rc;
593
594 skb_get(skb);
595 rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT);
596 if (rc < 0) {
597 mempool_free(skb, zone->pool);
598 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
599 return rc;
600 }
601
602 spin_lock_irqsave(&zone->freelock, flags);
603 list_add(skb_to_lh(skb), &zone->freequeue);
604 spin_unlock_irqrestore(&zone->freelock, flags);
605
606 return 0;
607}
608
Mike Christie7b7232f2006-02-01 21:06:49 -0600609int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700610 char *data, uint32_t data_size)
611{
612 struct nlmsghdr *nlh;
613 struct sk_buff *skb;
614 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700615 char *pdu;
616 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
617 data_size);
618
Mike Christie7b8631b2006-01-13 18:05:50 -0600619 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700620
Mike Christie7b8631b2006-01-13 18:05:50 -0600621 skb = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700622 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600623 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600624 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
625 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700626 return -ENOMEM;
627 }
628
629 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
630 ev = NLMSG_DATA(nlh);
631 memset(ev, 0, sizeof(*ev));
632 ev->transport_handle = iscsi_handle(conn->transport);
633 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christie7b8631b2006-01-13 18:05:50 -0600634 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700635 ev->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500636 ev->r.recv_req.cid = conn->cid;
637 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700638 pdu = (char*)ev + sizeof(*ev);
639 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
640 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
641
Mike Christie7b8631b2006-01-13 18:05:50 -0600642 return iscsi_unicast_skb(conn->z_pdu, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700643}
644EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
645
Mike Christie7b7232f2006-02-01 21:06:49 -0600646void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700647{
648 struct nlmsghdr *nlh;
649 struct sk_buff *skb;
650 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700651 int len = NLMSG_SPACE(sizeof(*ev));
652
Mike Christie7b8631b2006-01-13 18:05:50 -0600653 mempool_zone_complete(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700654
Mike Christie7b8631b2006-01-13 18:05:50 -0600655 skb = mempool_zone_get_skb(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700656 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600657 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
658 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700659 return;
660 }
661
662 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
663 ev = NLMSG_DATA(nlh);
664 ev->transport_handle = iscsi_handle(conn->transport);
665 ev->type = ISCSI_KEVENT_CONN_ERROR;
Mike Christie7b8631b2006-01-13 18:05:50 -0600666 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700667 ev->iferror = -ENOMEM;
668 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500669 ev->r.connerror.cid = conn->cid;
670 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700671
Mike Christie7b8631b2006-01-13 18:05:50 -0600672 iscsi_unicast_skb(conn->z_error, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700673
Mike Christie7b8631b2006-01-13 18:05:50 -0600674 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
675 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700676}
677EXPORT_SYMBOL_GPL(iscsi_conn_error);
678
679static int
680iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
681 void *payload, int size)
682{
683 struct sk_buff *skb;
684 struct nlmsghdr *nlh;
685 int len = NLMSG_SPACE(size);
686 int flags = multi ? NLM_F_MULTI : 0;
687 int t = done ? NLMSG_DONE : type;
688
Mike Christie7b8631b2006-01-13 18:05:50 -0600689 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700690
Mike Christie7b8631b2006-01-13 18:05:50 -0600691 skb = mempool_zone_get_skb(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700692 /*
693 * FIXME:
694 * user is supposed to react on iferror == -ENOMEM;
695 * see iscsi_if_rx().
696 */
697 BUG_ON(!skb);
698
699 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
700 nlh->nlmsg_flags = flags;
701 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie7b8631b2006-01-13 18:05:50 -0600702 return iscsi_unicast_skb(z_reply, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700703}
704
705static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600706iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700707{
708 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
709 struct iscsi_stats *stats;
710 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600711 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700712 struct nlmsghdr *nlhstat;
713 struct iscsi_uevent *evstat;
714 int len = NLMSG_SPACE(sizeof(*ev) +
715 sizeof(struct iscsi_stats) +
716 sizeof(struct iscsi_stats_custom) *
717 ISCSI_STATS_CUSTOM_MAX);
718 int err = 0;
719
Mike Christieb5c7a122006-04-06 21:13:33 -0500720 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700721 if (!conn)
722 return -EEXIST;
723
724 do {
725 int actual_size;
726
Mike Christie7b8631b2006-01-13 18:05:50 -0600727 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700728
Mike Christie7b8631b2006-01-13 18:05:50 -0600729 skbstat = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700730 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600731 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
732 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700733 return -ENOMEM;
734 }
735
736 nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0,
737 (len - sizeof(*nlhstat)), 0);
738 evstat = NLMSG_DATA(nlhstat);
739 memset(evstat, 0, sizeof(*evstat));
740 evstat->transport_handle = iscsi_handle(conn->transport);
741 evstat->type = nlh->nlmsg_type;
Mike Christie7b8631b2006-01-13 18:05:50 -0600742 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700743 evstat->iferror = -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500744 evstat->u.get_stats.cid =
745 ev->u.get_stats.cid;
746 evstat->u.get_stats.sid =
747 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700748 stats = (struct iscsi_stats *)
749 ((char*)evstat + sizeof(*evstat));
750 memset(stats, 0, sizeof(*stats));
751
Mike Christie7b7232f2006-02-01 21:06:49 -0600752 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700753 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
754 sizeof(struct iscsi_stats) +
755 sizeof(struct iscsi_stats_custom) *
756 stats->custom_length);
757 actual_size -= sizeof(*nlhstat);
758 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600759 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700760 nlhstat->nlmsg_len = actual_size;
761
Mike Christie7b8631b2006-01-13 18:05:50 -0600762 err = iscsi_unicast_skb(conn->z_pdu, skbstat);
Alex Aizman0896b752005-08-04 19:33:07 -0700763 } while (err < 0 && err != -ECONNREFUSED);
764
765 return err;
766}
767
768static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600769iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
770{
771 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600772 struct iscsi_cls_session *session;
Mike Christieb5c7a122006-04-06 21:13:33 -0500773 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -0600774
Mike Christie7b7232f2006-02-01 21:06:49 -0600775 session = transport->create_session(&priv->t,
776 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -0500777 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -0600778 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600779 return -ENOMEM;
780
Mike Christieb5c7a122006-04-06 21:13:33 -0500781 ev->r.c_session_ret.host_no = hostno;
782 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600783 return 0;
784}
785
786static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600787iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600788{
Mike Christie7b8631b2006-01-13 18:05:50 -0600789 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600790 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600791 unsigned long flags;
792
Mike Christieb5c7a122006-04-06 21:13:33 -0500793 session = iscsi_session_lookup(ev->u.c_conn.sid);
794 if (!session) {
795 printk(KERN_ERR "iscsi: invalid session %d\n",
796 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600797 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -0500798 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600799
Mike Christie7b7232f2006-02-01 21:06:49 -0600800 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -0500801 if (!conn) {
802 printk(KERN_ERR "iscsi: couldn't create a new "
803 "connection for session %d\n",
804 session->sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600805 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500806 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600807
808 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
809 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
810 sizeof(struct iscsi_hdr) +
811 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
812 Z_HIWAT_PDU);
813 if (!conn->z_pdu) {
814 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
815 "pdu zone for new conn\n");
816 goto destroy_conn;
817 }
818
819 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
820 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
821 Z_HIWAT_ERROR);
822 if (!conn->z_error) {
823 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
824 "error zone for new conn\n");
825 goto free_pdu_pool;
826 }
827
Mike Christieb5c7a122006-04-06 21:13:33 -0500828 ev->r.c_conn_ret.sid = session->sid;
829 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600830
831 spin_lock_irqsave(&connlock, flags);
832 list_add(&conn->conn_list, &connlist);
833 conn->active = 1;
834 spin_unlock_irqrestore(&connlock, flags);
835
Mike Christie7b8631b2006-01-13 18:05:50 -0600836 return 0;
837
838free_pdu_pool:
839 mempool_zone_destroy(conn->z_pdu);
840destroy_conn:
841 if (transport->destroy_conn)
842 transport->destroy_conn(conn->dd_data);
Mike Christie7b8631b2006-01-13 18:05:50 -0600843 return -ENOMEM;
844}
845
846static int
847iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
848{
849 unsigned long flags;
850 struct iscsi_cls_conn *conn;
851 struct mempool_zone *z_error, *z_pdu;
852
Mike Christieb5c7a122006-04-06 21:13:33 -0500853 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600854 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -0600855 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -0600856 spin_lock_irqsave(&connlock, flags);
857 conn->active = 0;
858 list_del(&conn->conn_list);
859 spin_unlock_irqrestore(&connlock, flags);
860
861 z_pdu = conn->z_pdu;
862 z_error = conn->z_error;
863
864 if (transport->destroy_conn)
865 transport->destroy_conn(conn);
866
867 mempool_zone_destroy(z_pdu);
868 mempool_zone_destroy(z_error);
869
870 return 0;
871}
872
873static int
Alex Aizman0896b752005-08-04 19:33:07 -0700874iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
875{
876 int err = 0;
877 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
878 struct iscsi_transport *transport = NULL;
879 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -0600880 struct iscsi_cls_session *session;
881 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700882
Alex Aizman0896b752005-08-04 19:33:07 -0700883 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
884 if (!priv)
885 return -EINVAL;
886 transport = priv->iscsi_transport;
887
Mike Christie7b7232f2006-02-01 21:06:49 -0600888 if (!try_module_get(transport->owner))
889 return -EINVAL;
890
Alex Aizman0896b752005-08-04 19:33:07 -0700891 switch (nlh->nlmsg_type) {
892 case ISCSI_UEVENT_CREATE_SESSION:
893 err = iscsi_if_create_session(priv, ev);
894 break;
895 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -0500896 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600897 if (session)
898 transport->destroy_session(session);
899 else
900 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700901 break;
902 case ISCSI_UEVENT_CREATE_CONN:
903 err = iscsi_if_create_conn(transport, ev);
904 break;
905 case ISCSI_UEVENT_DESTROY_CONN:
906 err = iscsi_if_destroy_conn(transport, ev);
907 break;
908 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -0500909 session = iscsi_session_lookup(ev->u.b_conn.sid);
910 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600911
912 if (session && conn)
913 ev->r.retcode = transport->bind_conn(session, conn,
914 ev->u.b_conn.transport_fd,
915 ev->u.b_conn.is_leading);
916 else
917 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700918 break;
919 case ISCSI_UEVENT_SET_PARAM:
Mike Christieb5c7a122006-04-06 21:13:33 -0500920 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600921 if (conn)
922 ev->r.retcode = transport->set_param(conn,
923 ev->u.set_param.param, ev->u.set_param.value);
924 else
925 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700926 break;
927 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -0500928 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600929 if (conn)
930 ev->r.retcode = transport->start_conn(conn);
931 else
932 err = -EINVAL;
933
Alex Aizman0896b752005-08-04 19:33:07 -0700934 break;
935 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -0500936 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600937 if (conn)
938 transport->stop_conn(conn, ev->u.stop_conn.flag);
939 else
940 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700941 break;
942 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -0500943 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600944 if (conn)
945 ev->r.retcode = transport->send_pdu(conn,
946 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
947 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
948 ev->u.send_pdu.data_size);
949 else
950 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700951 break;
952 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -0600953 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -0700954 break;
955 default:
956 err = -EINVAL;
957 break;
958 }
959
Mike Christie7b7232f2006-02-01 21:06:49 -0600960 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -0700961 return err;
962}
963
Mike Christieb5c7a122006-04-06 21:13:33 -0500964/*
965 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
966 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
967 * invalid creds are discarded silently.
968 */
Alex Aizman0896b752005-08-04 19:33:07 -0700969static void
970iscsi_if_rx(struct sock *sk, int len)
971{
972 struct sk_buff *skb;
973
Arjan van de Ven0b950672006-01-11 13:16:10 +0100974 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700975 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Mike Christieee7f8e42006-02-01 21:07:01 -0600976 if (NETLINK_CREDS(skb)->uid) {
977 skb_pull(skb, skb->len);
978 goto free_skb;
979 }
980 daemon_pid = NETLINK_CREDS(skb)->pid;
981
Alex Aizman0896b752005-08-04 19:33:07 -0700982 while (skb->len >= NLMSG_SPACE(0)) {
983 int err;
984 uint32_t rlen;
985 struct nlmsghdr *nlh;
986 struct iscsi_uevent *ev;
987
988 nlh = (struct nlmsghdr *)skb->data;
989 if (nlh->nlmsg_len < sizeof(*nlh) ||
990 skb->len < nlh->nlmsg_len) {
991 break;
992 }
Mike Christieee7f8e42006-02-01 21:07:01 -0600993
Alex Aizman0896b752005-08-04 19:33:07 -0700994 ev = NLMSG_DATA(nlh);
995 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
996 if (rlen > skb->len)
997 rlen = skb->len;
Mike Christieee7f8e42006-02-01 21:07:01 -0600998
Alex Aizman0896b752005-08-04 19:33:07 -0700999 err = iscsi_if_recv_msg(skb, nlh);
1000 if (err) {
1001 ev->type = ISCSI_KEVENT_IF_ERROR;
1002 ev->iferror = err;
1003 }
1004 do {
1005 /*
1006 * special case for GET_STATS:
1007 * on success - sending reply and stats from
1008 * inside of if_recv_msg(),
1009 * on error - fall through.
1010 */
1011 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1012 break;
1013 err = iscsi_if_send_reply(
1014 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1015 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Mike Christie7b8631b2006-01-13 18:05:50 -06001016 if (atomic_read(&z_reply->allocated) >=
1017 z_reply->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -07001018 ev->iferror = -ENOMEM;
1019 } while (err < 0 && err != -ECONNREFUSED);
1020 skb_pull(skb, rlen);
1021 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001022free_skb:
Alex Aizman0896b752005-08-04 19:33:07 -07001023 kfree_skb(skb);
1024 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001025 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001026}
1027
Mike Christie7b8631b2006-01-13 18:05:50 -06001028#define iscsi_cdev_to_conn(_cdev) \
1029 iscsi_dev_to_conn(_cdev->dev)
1030
Alex Aizman0896b752005-08-04 19:33:07 -07001031/*
1032 * iSCSI connection attrs
1033 */
1034#define iscsi_conn_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -07001036show_conn_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001038 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001039 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1040 struct iscsi_transport *t = conn->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001042 t->get_conn_param(conn, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001043 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Alex Aizman0896b752005-08-04 19:33:07 -07001046#define iscsi_conn_int_attr(field, param, format) \
1047 iscsi_conn_int_attr_show(param, format) \
1048static CLASS_DEVICE_ATTR(field, S_IRUGO, show_conn_int_param_##param, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Alex Aizman0896b752005-08-04 19:33:07 -07001050iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1051iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
1052iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1053iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1054iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1055iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Mike Christie7b8631b2006-01-13 18:05:50 -06001057#define iscsi_cdev_to_session(_cdev) \
1058 iscsi_dev_to_session(_cdev->dev)
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/*
Alex Aizman0896b752005-08-04 19:33:07 -07001061 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Alex Aizman0896b752005-08-04 19:33:07 -07001063#define iscsi_session_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -07001065show_session_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001067 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001068 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
Mike Christie7b8631b2006-01-13 18:05:50 -06001069 struct iscsi_transport *t = session->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001071 t->get_session_param(session, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001072 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Alex Aizman0896b752005-08-04 19:33:07 -07001075#define iscsi_session_int_attr(field, param, format) \
1076 iscsi_session_int_attr_show(param, format) \
1077static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_int_param_##param, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Alex Aizman0896b752005-08-04 19:33:07 -07001079iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1080iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
1081iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
1082iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
1083iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1084iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1085iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1086iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Alex Aizman0896b752005-08-04 19:33:07 -07001088#define SETUP_SESSION_RD_ATTR(field, param) \
1089 if (priv->param_mask & (1 << param)) { \
1090 priv->session_attrs[count] = &class_device_attr_##field;\
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 count++; \
1092 }
1093
Alex Aizman0896b752005-08-04 19:33:07 -07001094#define SETUP_CONN_RD_ATTR(field, param) \
1095 if (priv->param_mask & (1 << param)) { \
1096 priv->conn_attrs[count] = &class_device_attr_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 count++; \
1098 }
1099
Alex Aizman0896b752005-08-04 19:33:07 -07001100static int iscsi_session_match(struct attribute_container *cont,
1101 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Mike Christie7b8631b2006-01-13 18:05:50 -06001103 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001105 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Alex Aizman0896b752005-08-04 19:33:07 -07001107 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return 0;
1109
Mike Christie7b8631b2006-01-13 18:05:50 -06001110 session = iscsi_dev_to_session(dev);
1111 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001112 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return 0;
1114
Alex Aizman0896b752005-08-04 19:33:07 -07001115 priv = to_iscsi_internal(shost->transportt);
1116 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1117 return 0;
1118
1119 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Alex Aizman0896b752005-08-04 19:33:07 -07001122static int iscsi_conn_match(struct attribute_container *cont,
1123 struct device *dev)
1124{
Mike Christie7b8631b2006-01-13 18:05:50 -06001125 struct iscsi_cls_session *session;
1126 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001127 struct Scsi_Host *shost;
1128 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Alex Aizman0896b752005-08-04 19:33:07 -07001130 if (!iscsi_is_conn_dev(dev))
1131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Mike Christie7b8631b2006-01-13 18:05:50 -06001133 conn = iscsi_dev_to_conn(dev);
1134 session = iscsi_dev_to_session(conn->dev.parent);
1135 shost = iscsi_session_to_shost(session);
1136
Alex Aizman0896b752005-08-04 19:33:07 -07001137 if (!shost->transportt)
1138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Alex Aizman0896b752005-08-04 19:33:07 -07001140 priv = to_iscsi_internal(shost->transportt);
1141 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1142 return 0;
1143
1144 return &priv->conn_cont.ac == cont;
1145}
1146
Mike Christie7b8631b2006-01-13 18:05:50 -06001147struct scsi_transport_template *
1148iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001149{
1150 struct iscsi_internal *priv;
1151 unsigned long flags;
1152 int count = 0, err;
1153
1154 BUG_ON(!tt);
1155
1156 priv = iscsi_if_transport_lookup(tt);
1157 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001158 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001159
Jes Sorensen24669f752006-01-16 10:31:18 -05001160 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001161 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001162 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001163 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07001164 priv->iscsi_transport = tt;
1165
1166 priv->cdev.class = &iscsi_transport_class;
1167 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1168 err = class_device_register(&priv->cdev);
1169 if (err)
1170 goto free_priv;
1171
1172 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1173 if (err)
1174 goto unregister_cdev;
1175
1176 /* setup parameters mask */
1177 priv->param_mask = 0xFFFFFFFF;
1178 if (!(tt->caps & CAP_MULTI_R2T))
1179 priv->param_mask &= ~(1 << ISCSI_PARAM_MAX_R2T);
1180 if (!(tt->caps & CAP_HDRDGST))
1181 priv->param_mask &= ~(1 << ISCSI_PARAM_HDRDGST_EN);
1182 if (!(tt->caps & CAP_DATADGST))
1183 priv->param_mask &= ~(1 << ISCSI_PARAM_DATADGST_EN);
1184 if (!(tt->caps & CAP_MARKERS)) {
1185 priv->param_mask &= ~(1 << ISCSI_PARAM_IFMARKER_EN);
1186 priv->param_mask &= ~(1 << ISCSI_PARAM_OFMARKER_EN);
1187 }
1188
1189 /* connection parameters */
1190 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1191 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1192 priv->conn_cont.ac.match = iscsi_conn_match;
1193 transport_container_register(&priv->conn_cont);
1194
1195 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1196 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1197 SETUP_CONN_RD_ATTR(header_digest, ISCSI_PARAM_HDRDGST_EN);
1198 SETUP_CONN_RD_ATTR(data_digest, ISCSI_PARAM_DATADGST_EN);
1199 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1200 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1201
1202 BUG_ON(count > ISCSI_CONN_ATTRS);
1203 priv->conn_attrs[count] = NULL;
1204 count = 0;
1205
1206 /* session parameters */
1207 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1208 priv->session_cont.ac.class = &iscsi_session_class.class;
1209 priv->session_cont.ac.match = iscsi_session_match;
1210 transport_container_register(&priv->session_cont);
1211
1212 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1213 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1214 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1215 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1216 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_PARAM_MAX_BURST);
1217 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1218 SETUP_SESSION_RD_ATTR(data_seq_in_order,ISCSI_PARAM_DATASEQ_INORDER_EN)
1219 SETUP_SESSION_RD_ATTR(erl, ISCSI_PARAM_ERL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001222 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Alex Aizman0896b752005-08-04 19:33:07 -07001224 spin_lock_irqsave(&iscsi_transport_lock, flags);
1225 list_add(&priv->list, &iscsi_transports);
1226 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Alex Aizman0896b752005-08-04 19:33:07 -07001228 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001229 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Alex Aizman0896b752005-08-04 19:33:07 -07001231unregister_cdev:
1232 class_device_unregister(&priv->cdev);
1233free_priv:
1234 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001235 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
Alex Aizman0896b752005-08-04 19:33:07 -07001237EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Alex Aizman0896b752005-08-04 19:33:07 -07001239int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Alex Aizman0896b752005-08-04 19:33:07 -07001241 struct iscsi_internal *priv;
1242 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Alex Aizman0896b752005-08-04 19:33:07 -07001244 BUG_ON(!tt);
1245
Arjan van de Ven0b950672006-01-11 13:16:10 +01001246 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001247
1248 priv = iscsi_if_transport_lookup(tt);
1249 BUG_ON (!priv);
1250
Alex Aizman0896b752005-08-04 19:33:07 -07001251 spin_lock_irqsave(&iscsi_transport_lock, flags);
1252 list_del(&priv->list);
1253 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1254
1255 transport_container_unregister(&priv->conn_cont);
1256 transport_container_unregister(&priv->session_cont);
1257
1258 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1259 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001260 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001261
1262 return 0;
1263}
1264EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1265
1266static int
1267iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1268{
1269 struct netlink_notify *n = ptr;
1270
1271 if (event == NETLINK_URELEASE &&
1272 n->protocol == NETLINK_ISCSI && n->pid) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001273 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001274 unsigned long flags;
1275
Mike Christie7b8631b2006-01-13 18:05:50 -06001276 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001277 spin_lock_irqsave(&connlock, flags);
1278 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001279 mempool_zone_complete(conn->z_error);
1280 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -07001281 }
1282 spin_unlock_irqrestore(&connlock, flags);
1283 }
1284
1285 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Alex Aizman0896b752005-08-04 19:33:07 -07001288static struct notifier_block iscsi_nl_notifier = {
1289 .notifier_call = iscsi_rcv_nl_event,
1290};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292static __init int iscsi_transport_init(void)
1293{
Alex Aizman0896b752005-08-04 19:33:07 -07001294 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Alex Aizman0896b752005-08-04 19:33:07 -07001296 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (err)
1298 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001299
1300 err = transport_class_register(&iscsi_connection_class);
1301 if (err)
1302 goto unregister_transport_class;
1303
1304 err = transport_class_register(&iscsi_session_class);
1305 if (err)
1306 goto unregister_conn_class;
1307
1308 err = netlink_register_notifier(&iscsi_nl_notifier);
1309 if (err)
1310 goto unregister_session_class;
1311
James Bottomley2290d2b2005-09-08 10:14:11 -05001312 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
Mike Christie7b8631b2006-01-13 18:05:50 -06001313 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001314 if (!nls) {
1315 err = -ENOBUFS;
1316 goto unregister_notifier;
1317 }
1318
Mike Christie7b8631b2006-01-13 18:05:50 -06001319 z_reply = mempool_zone_init(Z_MAX_REPLY,
Alex Aizman0896b752005-08-04 19:33:07 -07001320 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
Mike Christie7b8631b2006-01-13 18:05:50 -06001321 if (z_reply)
Alex Aizman0896b752005-08-04 19:33:07 -07001322 return 0;
1323
1324 sock_release(nls->sk_socket);
1325unregister_notifier:
1326 netlink_unregister_notifier(&iscsi_nl_notifier);
1327unregister_session_class:
1328 transport_class_unregister(&iscsi_session_class);
1329unregister_conn_class:
1330 transport_class_unregister(&iscsi_connection_class);
1331unregister_transport_class:
1332 class_unregister(&iscsi_transport_class);
1333 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336static void __exit iscsi_transport_exit(void)
1337{
Mike Christie7b8631b2006-01-13 18:05:50 -06001338 mempool_zone_destroy(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001339 sock_release(nls->sk_socket);
1340 netlink_unregister_notifier(&iscsi_nl_notifier);
1341 transport_class_unregister(&iscsi_connection_class);
1342 transport_class_unregister(&iscsi_session_class);
1343 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346module_init(iscsi_transport_init);
1347module_exit(iscsi_transport_exit);
1348
Alex Aizman0896b752005-08-04 19:33:07 -07001349MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1350 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1351 "Alex Aizman <itn780@yahoo.com>");
1352MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353MODULE_LICENSE("GPL");