blob: b61868587dca21fd163934ac92f959e7a6343f4a [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 /*
42 * List of sessions for this transport
43 */
44 struct list_head sessions;
45 /*
Alex Aizman0896b752005-08-04 19:33:07 -070046 * based on transport capabilities, at register time we set these
47 * bits to tell the transport class it wants attributes displayed
48 * in sysfs or that it can support different iSCSI Data-Path
49 * capabilities
50 */
51 uint32_t param_mask;
52
53 struct class_device cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /*
55 * We do not have any private or other attrs.
56 */
Alex Aizman0896b752005-08-04 19:33:07 -070057 struct transport_container conn_cont;
58 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
59 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Alex Aizman0896b752005-08-04 19:33:07 -070063/*
64 * list of registered transports and lock that must
65 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010066 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070067 */
68static LIST_HEAD(iscsi_transports);
69static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Alex Aizman0896b752005-08-04 19:33:07 -070071#define to_iscsi_internal(tmpl) \
72 container_of(tmpl, struct iscsi_internal, t)
73
74#define cdev_to_iscsi_internal(_cdev) \
75 container_of(_cdev, struct iscsi_internal, cdev)
76
77static void iscsi_transport_release(struct class_device *cdev)
78{
79 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
80 kfree(priv);
81}
82
83/*
84 * iscsi_transport_class represents the iscsi_transports that are
85 * registered.
86 */
87static struct class iscsi_transport_class = {
88 .name = "iscsi_transport",
89 .release = iscsi_transport_release,
90};
91
92static ssize_t
93show_transport_handle(struct class_device *cdev, char *buf)
94{
95 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
Mike Christie762e2bf2005-09-12 21:01:46 -050096 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070097}
98static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
99
100#define show_transport_attr(name, format) \
101static ssize_t \
102show_transport_##name(struct class_device *cdev, char *buf) \
103{ \
104 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
105 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
106} \
107static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
108
109show_transport_attr(caps, "0x%x");
110show_transport_attr(max_lun, "%d");
111show_transport_attr(max_conn, "%d");
112show_transport_attr(max_cmd_len, "%d");
113
114static struct attribute *iscsi_transport_attrs[] = {
115 &class_device_attr_handle.attr,
116 &class_device_attr_caps.attr,
117 &class_device_attr_max_lun.attr,
118 &class_device_attr_max_conn.attr,
119 &class_device_attr_max_cmd_len.attr,
120 NULL,
121};
122
123static struct attribute_group iscsi_transport_group = {
124 .attrs = iscsi_transport_attrs,
125};
126
127static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
128 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 NULL,
130 NULL,
131 NULL);
132
Alex Aizman0896b752005-08-04 19:33:07 -0700133static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
134 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 NULL,
136 NULL,
137 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700138
139static struct sock *nls;
140static int daemon_pid;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100141static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700142
143struct mempool_zone {
144 mempool_t *pool;
145 atomic_t allocated;
146 int size;
147 int hiwat;
148 struct list_head freequeue;
149 spinlock_t freelock;
150};
151
Mike Christie7b8631b2006-01-13 18:05:50 -0600152static struct mempool_zone *z_reply;
Alex Aizman0896b752005-08-04 19:33:07 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Alex Aizman0896b752005-08-04 19:33:07 -0700155 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
156 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
157 * so daemon will notice OOM on NETLINK tranposrt level and will
158 * be able to predict or change operational behavior
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Alex Aizman0896b752005-08-04 19:33:07 -0700160#define Z_MAX_REPLY 8
161#define Z_HIWAT_REPLY 6
162#define Z_MAX_PDU 8
163#define Z_HIWAT_PDU 6
164#define Z_MAX_ERROR 16
165#define Z_HIWAT_ERROR 12
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Mike Christie7b7232f2006-02-01 21:06:49 -0600167static LIST_HEAD(sesslist);
168static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700169static LIST_HEAD(connlist);
170static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Mike Christie7b7232f2006-02-01 21:06:49 -0600172static struct iscsi_cls_session *iscsi_session_lookup(uint64_t handle)
173{
174 unsigned long flags;
175 struct iscsi_cls_session *sess;
176
177 spin_lock_irqsave(&sesslock, flags);
178 list_for_each_entry(sess, &sesslist, sess_list) {
179 if (sess == iscsi_ptr(handle)) {
180 spin_unlock_irqrestore(&sesslock, flags);
181 return sess;
182 }
183 }
184 spin_unlock_irqrestore(&sesslock, flags);
185 return NULL;
186}
187
188static struct iscsi_cls_conn *iscsi_conn_lookup(uint64_t handle)
189{
190 unsigned long flags;
191 struct iscsi_cls_conn *conn;
192
193 spin_lock_irqsave(&connlock, flags);
194 list_for_each_entry(conn, &connlist, conn_list) {
195 if (conn == iscsi_ptr(handle)) {
196 spin_unlock_irqrestore(&connlock, flags);
197 return conn;
198 }
199 }
200 spin_unlock_irqrestore(&connlock, flags);
201 return NULL;
202}
203
Mike Christie7b8631b2006-01-13 18:05:50 -0600204/*
205 * The following functions can be used by LLDs that allocate
206 * their own scsi_hosts or by software iscsi LLDs
207 */
208static void iscsi_session_release(struct device *dev)
209{
210 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
211 struct iscsi_transport *transport = session->transport;
212 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Mike Christie7b8631b2006-01-13 18:05:50 -0600214 shost = iscsi_session_to_shost(session);
215 scsi_host_put(shost);
216 kfree(session);
217 module_put(transport->owner);
218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Mike Christie7b8631b2006-01-13 18:05:50 -0600220static int iscsi_is_session_dev(const struct device *dev)
221{
222 return dev->release == iscsi_session_release;
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Mike Christie7b8631b2006-01-13 18:05:50 -0600225/**
226 * iscsi_create_session - create iscsi class session
227 * @shost: scsi host
228 * @transport: iscsi transport
229 *
230 * This can be called from a LLD or iscsi_transport
231 **/
232struct iscsi_cls_session *
233iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport)
234{
235 struct iscsi_cls_session *session;
236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Mike Christie7b8631b2006-01-13 18:05:50 -0600238 if (!try_module_get(transport->owner))
239 return NULL;
240
241 session = kzalloc(sizeof(*session), GFP_KERNEL);
242 if (!session)
243 goto module_put;
244 session->transport = transport;
245
246 /* this is released in the dev's release function */
247 scsi_host_get(shost);
248 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", shost->host_no);
249 session->dev.parent = &shost->shost_gendev;
250 session->dev.release = iscsi_session_release;
251 err = device_register(&session->dev);
252 if (err) {
253 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
254 "register session's dev\n");
255 goto free_session;
256 }
257 transport_register_device(&session->dev);
258
259 return session;
260
261free_session:
262 kfree(session);
263module_put:
264 module_put(transport->owner);
265 return NULL;
266}
267
268EXPORT_SYMBOL_GPL(iscsi_create_session);
269
270/**
271 * iscsi_destroy_session - destroy iscsi session
272 * @session: iscsi_session
273 *
274 * Can be called by a LLD or iscsi_transport. There must not be
275 * any running connections.
276 **/
277int iscsi_destroy_session(struct iscsi_cls_session *session)
278{
279 transport_unregister_device(&session->dev);
280 device_unregister(&session->dev);
281 return 0;
282}
283
284EXPORT_SYMBOL_GPL(iscsi_destroy_session);
285
286static void iscsi_conn_release(struct device *dev)
287{
288 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
289 struct device *parent = conn->dev.parent;
290
291 kfree(conn);
292 put_device(parent);
293}
294
295static int iscsi_is_conn_dev(const struct device *dev)
296{
297 return dev->release == iscsi_conn_release;
298}
299
300/**
301 * iscsi_create_conn - create iscsi class connection
302 * @session: iscsi cls session
303 * @cid: connection id
304 *
305 * This can be called from a LLD or iscsi_transport. The connection
306 * is child of the session so cid must be unique for all connections
307 * on the session.
308 **/
309struct iscsi_cls_conn *
310iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
311{
312 struct iscsi_transport *transport = session->transport;
313 struct Scsi_Host *shost = iscsi_session_to_shost(session);
314 struct iscsi_cls_conn *conn;
315 int err;
316
317 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
318 if (!conn)
319 return NULL;
320
321 if (transport->conndata_size)
322 conn->dd_data = &conn[1];
323
324 INIT_LIST_HEAD(&conn->conn_list);
325 conn->transport = transport;
326
327 /* this is released in the dev's release function */
328 if (!get_device(&session->dev))
329 goto free_conn;
330 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
331 shost->host_no, cid);
332 conn->dev.parent = &session->dev;
333 conn->dev.release = iscsi_conn_release;
334 err = device_register(&conn->dev);
335 if (err) {
336 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
337 "connection's dev\n");
338 goto release_parent_ref;
339 }
340 transport_register_device(&conn->dev);
341 return conn;
342
343release_parent_ref:
344 put_device(&session->dev);
345free_conn:
346 kfree(conn);
347 return NULL;
348}
349
350EXPORT_SYMBOL_GPL(iscsi_create_conn);
351
352/**
353 * iscsi_destroy_conn - destroy iscsi class connection
354 * @session: iscsi cls session
355 *
356 * This can be called from a LLD or iscsi_transport.
357 **/
358int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
359{
360 transport_unregister_device(&conn->dev);
361 device_unregister(&conn->dev);
362 return 0;
363}
364
365EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
366
367/*
368 * These functions are used only by software iscsi_transports
369 * which do not allocate and more their scsi_hosts since this
370 * is initiated from userspace.
371 */
372
373/*
374 * iSCSI Session's hostdata organization:
375 *
376 * *------------------* <== hostdata_session(host->hostdata)
377 * | ptr to class sess|
378 * |------------------| <== iscsi_hostdata(host->hostdata)
379 * | transport's data |
380 * *------------------*
381 */
382
383#define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
384 _t->hostdata_size % sizeof(unsigned long))
385
386#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
387
388/**
389 * iscsi_transport_create_session - create iscsi cls session and host
390 * scsit: scsi transport template
391 * transport: iscsi transport template
392 *
393 * This can be used by software iscsi_transports that allocate
394 * a session per scsi host.
395 **/
396struct Scsi_Host *
397iscsi_transport_create_session(struct scsi_transport_template *scsit,
398 struct iscsi_transport *transport)
399{
400 struct iscsi_cls_session *session;
401 struct Scsi_Host *shost;
Mike Christie7b7232f2006-02-01 21:06:49 -0600402 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600403
404 shost = scsi_host_alloc(transport->host_template,
405 hostdata_privsize(transport));
406 if (!shost) {
407 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
408 "session\n");
409 return NULL;
410 }
411
412 shost->max_id = 1;
413 shost->max_channel = 0;
414 shost->max_lun = transport->max_lun;
415 shost->max_cmd_len = transport->max_cmd_len;
416 shost->transportt = scsit;
Mike Christie55e32992006-01-13 18:05:53 -0600417 shost->transportt->create_work_queue = 1;
Mike Christie7b8631b2006-01-13 18:05:50 -0600418
419 if (scsi_add_host(shost, NULL))
420 goto free_host;
421
422 session = iscsi_create_session(shost, transport);
423 if (!session)
424 goto remove_host;
425
426 *(unsigned long*)shost->hostdata = (unsigned long)session;
Mike Christie7b7232f2006-02-01 21:06:49 -0600427 spin_lock_irqsave(&sesslock, flags);
428 list_add(&session->sess_list, &sesslist);
429 spin_unlock_irqrestore(&sesslock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600430 return shost;
431
432remove_host:
433 scsi_remove_host(shost);
434free_host:
435 scsi_host_put(shost);
436 return NULL;
437}
438
439EXPORT_SYMBOL_GPL(iscsi_transport_create_session);
440
441/**
442 * iscsi_transport_destroy_session - destroy session and scsi host
443 * shost: scsi host
444 *
445 * This can be used by software iscsi_transports that allocate
446 * a session per scsi host.
447 **/
448int iscsi_transport_destroy_session(struct Scsi_Host *shost)
449{
450 struct iscsi_cls_session *session;
Mike Christie7b7232f2006-02-01 21:06:49 -0600451 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600452
453 scsi_remove_host(shost);
454 session = hostdata_session(shost->hostdata);
Mike Christie7b7232f2006-02-01 21:06:49 -0600455 spin_lock_irqsave(&sesslock, flags);
456 list_del(&session->sess_list);
457 spin_unlock_irqrestore(&sesslock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600458 iscsi_destroy_session(session);
459 /* ref from host alloc */
460 scsi_host_put(shost);
461 return 0;
462}
463
464EXPORT_SYMBOL_GPL(iscsi_transport_destroy_session);
465
466/*
467 * iscsi interface functions
468 */
Alex Aizman0896b752005-08-04 19:33:07 -0700469static struct iscsi_internal *
470iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Alex Aizman0896b752005-08-04 19:33:07 -0700472 struct iscsi_internal *priv;
473 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alex Aizman0896b752005-08-04 19:33:07 -0700475 spin_lock_irqsave(&iscsi_transport_lock, flags);
476 list_for_each_entry(priv, &iscsi_transports, list) {
477 if (tt == priv->iscsi_transport) {
478 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
479 return priv;
480 }
481 }
482 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
483 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Alex Aizman0896b752005-08-04 19:33:07 -0700486static inline struct list_head *skb_to_lh(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Alex Aizman0896b752005-08-04 19:33:07 -0700488 return (struct list_head *)&skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Alex Aizman0896b752005-08-04 19:33:07 -0700490
491static void*
Mike Christie7b8631b2006-01-13 18:05:50 -0600492mempool_zone_alloc_skb(unsigned int gfp_mask, void *pool_data)
Alex Aizman0896b752005-08-04 19:33:07 -0700493{
494 struct mempool_zone *zone = pool_data;
495
496 return alloc_skb(zone->size, gfp_mask);
497}
498
499static void
500mempool_zone_free_skb(void *element, void *pool_data)
501{
502 kfree_skb(element);
503}
504
505static void
506mempool_zone_complete(struct mempool_zone *zone)
507{
508 unsigned long flags;
509 struct list_head *lh, *n;
510
511 spin_lock_irqsave(&zone->freelock, flags);
512 list_for_each_safe(lh, n, &zone->freequeue) {
513 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
514 offsetof(struct sk_buff, cb));
515 if (!skb_shared(skb)) {
516 list_del(skb_to_lh(skb));
517 mempool_free(skb, zone->pool);
518 atomic_dec(&zone->allocated);
519 }
520 }
521 spin_unlock_irqrestore(&zone->freelock, flags);
522}
523
Mike Christie7b8631b2006-01-13 18:05:50 -0600524static struct mempool_zone *
525mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700526{
Mike Christie7b8631b2006-01-13 18:05:50 -0600527 struct mempool_zone *zp;
528
529 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
530 if (!zp)
531 return NULL;
532
Alex Aizman0896b752005-08-04 19:33:07 -0700533 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
534 mempool_zone_free_skb, zp);
Mike Christie7b8631b2006-01-13 18:05:50 -0600535 if (!zp->pool) {
536 kfree(zp);
537 return NULL;
538 }
Alex Aizman0896b752005-08-04 19:33:07 -0700539
540 zp->size = size;
541 zp->hiwat = hiwat;
542
543 INIT_LIST_HEAD(&zp->freequeue);
544 spin_lock_init(&zp->freelock);
545 atomic_set(&zp->allocated, 0);
546
Mike Christie7b8631b2006-01-13 18:05:50 -0600547 return zp;
Alex Aizman0896b752005-08-04 19:33:07 -0700548}
549
Mike Christie7b8631b2006-01-13 18:05:50 -0600550static void mempool_zone_destroy(struct mempool_zone *zp)
551{
552 mempool_destroy(zp->pool);
553 kfree(zp);
554}
Alex Aizman0896b752005-08-04 19:33:07 -0700555
556static struct sk_buff*
557mempool_zone_get_skb(struct mempool_zone *zone)
558{
559 struct sk_buff *skb;
560
561 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
562 if (skb)
563 atomic_inc(&zone->allocated);
564 return skb;
565}
566
567static int
568iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
569{
570 unsigned long flags;
571 int rc;
572
573 skb_get(skb);
574 rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT);
575 if (rc < 0) {
576 mempool_free(skb, zone->pool);
577 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
578 return rc;
579 }
580
581 spin_lock_irqsave(&zone->freelock, flags);
582 list_add(skb_to_lh(skb), &zone->freequeue);
583 spin_unlock_irqrestore(&zone->freelock, flags);
584
585 return 0;
586}
587
Mike Christie7b7232f2006-02-01 21:06:49 -0600588int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700589 char *data, uint32_t data_size)
590{
591 struct nlmsghdr *nlh;
592 struct sk_buff *skb;
593 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700594 char *pdu;
595 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
596 data_size);
597
Mike Christie7b8631b2006-01-13 18:05:50 -0600598 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700599
Mike Christie7b8631b2006-01-13 18:05:50 -0600600 skb = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700601 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600602 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600603 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
604 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700605 return -ENOMEM;
606 }
607
608 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
609 ev = NLMSG_DATA(nlh);
610 memset(ev, 0, sizeof(*ev));
611 ev->transport_handle = iscsi_handle(conn->transport);
612 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christie7b8631b2006-01-13 18:05:50 -0600613 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700614 ev->iferror = -ENOMEM;
Mike Christie7b7232f2006-02-01 21:06:49 -0600615 ev->r.recv_req.conn_handle = iscsi_handle(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700616 pdu = (char*)ev + sizeof(*ev);
617 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
618 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
619
Mike Christie7b8631b2006-01-13 18:05:50 -0600620 return iscsi_unicast_skb(conn->z_pdu, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700621}
622EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
623
Mike Christie7b7232f2006-02-01 21:06:49 -0600624void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700625{
626 struct nlmsghdr *nlh;
627 struct sk_buff *skb;
628 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700629 int len = NLMSG_SPACE(sizeof(*ev));
630
Mike Christie7b8631b2006-01-13 18:05:50 -0600631 mempool_zone_complete(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700632
Mike Christie7b8631b2006-01-13 18:05:50 -0600633 skb = mempool_zone_get_skb(conn->z_error);
Alex Aizman0896b752005-08-04 19:33:07 -0700634 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600635 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
636 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700637 return;
638 }
639
640 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
641 ev = NLMSG_DATA(nlh);
642 ev->transport_handle = iscsi_handle(conn->transport);
643 ev->type = ISCSI_KEVENT_CONN_ERROR;
Mike Christie7b8631b2006-01-13 18:05:50 -0600644 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700645 ev->iferror = -ENOMEM;
646 ev->r.connerror.error = error;
Mike Christie7b7232f2006-02-01 21:06:49 -0600647 ev->r.connerror.conn_handle = iscsi_handle(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700648
Mike Christie7b8631b2006-01-13 18:05:50 -0600649 iscsi_unicast_skb(conn->z_error, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700650
Mike Christie7b8631b2006-01-13 18:05:50 -0600651 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
652 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700653}
654EXPORT_SYMBOL_GPL(iscsi_conn_error);
655
656static int
657iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
658 void *payload, int size)
659{
660 struct sk_buff *skb;
661 struct nlmsghdr *nlh;
662 int len = NLMSG_SPACE(size);
663 int flags = multi ? NLM_F_MULTI : 0;
664 int t = done ? NLMSG_DONE : type;
665
Mike Christie7b8631b2006-01-13 18:05:50 -0600666 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700667
Mike Christie7b8631b2006-01-13 18:05:50 -0600668 skb = mempool_zone_get_skb(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -0700669 /*
670 * FIXME:
671 * user is supposed to react on iferror == -ENOMEM;
672 * see iscsi_if_rx().
673 */
674 BUG_ON(!skb);
675
676 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
677 nlh->nlmsg_flags = flags;
678 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie7b8631b2006-01-13 18:05:50 -0600679 return iscsi_unicast_skb(z_reply, skb);
Alex Aizman0896b752005-08-04 19:33:07 -0700680}
681
682static int
683iscsi_if_get_stats(struct iscsi_transport *transport, struct sk_buff *skb,
684 struct nlmsghdr *nlh)
685{
686 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
687 struct iscsi_stats *stats;
688 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600689 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700690 struct nlmsghdr *nlhstat;
691 struct iscsi_uevent *evstat;
692 int len = NLMSG_SPACE(sizeof(*ev) +
693 sizeof(struct iscsi_stats) +
694 sizeof(struct iscsi_stats_custom) *
695 ISCSI_STATS_CUSTOM_MAX);
696 int err = 0;
697
Mike Christie7b7232f2006-02-01 21:06:49 -0600698 conn = iscsi_conn_lookup(ev->u.get_stats.conn_handle);
Alex Aizman0896b752005-08-04 19:33:07 -0700699 if (!conn)
700 return -EEXIST;
701
702 do {
703 int actual_size;
704
Mike Christie7b8631b2006-01-13 18:05:50 -0600705 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700706
Mike Christie7b8631b2006-01-13 18:05:50 -0600707 skbstat = mempool_zone_get_skb(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -0700708 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600709 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
710 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700711 return -ENOMEM;
712 }
713
714 nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0,
715 (len - sizeof(*nlhstat)), 0);
716 evstat = NLMSG_DATA(nlhstat);
717 memset(evstat, 0, sizeof(*evstat));
718 evstat->transport_handle = iscsi_handle(conn->transport);
719 evstat->type = nlh->nlmsg_type;
Mike Christie7b8631b2006-01-13 18:05:50 -0600720 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700721 evstat->iferror = -ENOMEM;
722 evstat->u.get_stats.conn_handle =
723 ev->u.get_stats.conn_handle;
724 stats = (struct iscsi_stats *)
725 ((char*)evstat + sizeof(*evstat));
726 memset(stats, 0, sizeof(*stats));
727
Mike Christie7b7232f2006-02-01 21:06:49 -0600728 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700729 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
730 sizeof(struct iscsi_stats) +
731 sizeof(struct iscsi_stats_custom) *
732 stats->custom_length);
733 actual_size -= sizeof(*nlhstat);
734 actual_size = NLMSG_LENGTH(actual_size);
735 skb_trim(skb, NLMSG_ALIGN(actual_size));
736 nlhstat->nlmsg_len = actual_size;
737
Mike Christie7b8631b2006-01-13 18:05:50 -0600738 err = iscsi_unicast_skb(conn->z_pdu, skbstat);
Alex Aizman0896b752005-08-04 19:33:07 -0700739 } while (err < 0 && err != -ECONNREFUSED);
740
741 return err;
742}
743
744static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600745iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
746{
747 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600748 struct iscsi_cls_session *session;
749 uint32_t sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600750
Mike Christie7b7232f2006-02-01 21:06:49 -0600751 session = transport->create_session(&priv->t,
752 ev->u.c_session.initial_cmdsn,
753 &sid);
754 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600755 return -ENOMEM;
756
Mike Christie7b7232f2006-02-01 21:06:49 -0600757 ev->r.c_session_ret.session_handle = iscsi_handle(session);
758 ev->r.c_session_ret.sid = sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600759 return 0;
760}
761
762static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600763iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600764{
Mike Christie7b8631b2006-01-13 18:05:50 -0600765 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600766 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600767 unsigned long flags;
768
Mike Christie7b7232f2006-02-01 21:06:49 -0600769 session = iscsi_session_lookup(ev->u.c_conn.session_handle);
770 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600771 return -EINVAL;
772
Mike Christie7b7232f2006-02-01 21:06:49 -0600773 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600774 if (!conn)
Mike Christie7b7232f2006-02-01 21:06:49 -0600775 return -ENOMEM;
Mike Christie7b8631b2006-01-13 18:05:50 -0600776
777 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
778 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
779 sizeof(struct iscsi_hdr) +
780 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
781 Z_HIWAT_PDU);
782 if (!conn->z_pdu) {
783 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
784 "pdu zone for new conn\n");
785 goto destroy_conn;
786 }
787
788 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
789 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
790 Z_HIWAT_ERROR);
791 if (!conn->z_error) {
792 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
793 "error zone for new conn\n");
794 goto free_pdu_pool;
795 }
796
Mike Christie7b7232f2006-02-01 21:06:49 -0600797 ev->r.handle = iscsi_handle(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -0600798
799 spin_lock_irqsave(&connlock, flags);
800 list_add(&conn->conn_list, &connlist);
801 conn->active = 1;
802 spin_unlock_irqrestore(&connlock, flags);
803
Mike Christie7b8631b2006-01-13 18:05:50 -0600804 return 0;
805
806free_pdu_pool:
807 mempool_zone_destroy(conn->z_pdu);
808destroy_conn:
809 if (transport->destroy_conn)
810 transport->destroy_conn(conn->dd_data);
Mike Christie7b8631b2006-01-13 18:05:50 -0600811 return -ENOMEM;
812}
813
814static int
815iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
816{
817 unsigned long flags;
818 struct iscsi_cls_conn *conn;
819 struct mempool_zone *z_error, *z_pdu;
820
Mike Christie7b7232f2006-02-01 21:06:49 -0600821 conn = iscsi_conn_lookup(ev->u.d_conn.conn_handle);
Mike Christie7b8631b2006-01-13 18:05:50 -0600822 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -0600823 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -0600824 spin_lock_irqsave(&connlock, flags);
825 conn->active = 0;
826 list_del(&conn->conn_list);
827 spin_unlock_irqrestore(&connlock, flags);
828
829 z_pdu = conn->z_pdu;
830 z_error = conn->z_error;
831
832 if (transport->destroy_conn)
833 transport->destroy_conn(conn);
834
835 mempool_zone_destroy(z_pdu);
836 mempool_zone_destroy(z_error);
837
838 return 0;
839}
840
841static int
Alex Aizman0896b752005-08-04 19:33:07 -0700842iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
843{
844 int err = 0;
845 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
846 struct iscsi_transport *transport = NULL;
847 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -0600848 struct iscsi_cls_session *session;
849 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700850
851 if (NETLINK_CREDS(skb)->uid)
852 return -EPERM;
853
854 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
855 if (!priv)
856 return -EINVAL;
857 transport = priv->iscsi_transport;
858
Mike Christie7b7232f2006-02-01 21:06:49 -0600859 if (!try_module_get(transport->owner))
860 return -EINVAL;
861
Alex Aizman0896b752005-08-04 19:33:07 -0700862 daemon_pid = NETLINK_CREDS(skb)->pid;
863
864 switch (nlh->nlmsg_type) {
865 case ISCSI_UEVENT_CREATE_SESSION:
866 err = iscsi_if_create_session(priv, ev);
867 break;
868 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christie7b7232f2006-02-01 21:06:49 -0600869 session = iscsi_session_lookup(ev->u.d_session.session_handle);
870 if (session)
871 transport->destroy_session(session);
872 else
873 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700874 break;
875 case ISCSI_UEVENT_CREATE_CONN:
876 err = iscsi_if_create_conn(transport, ev);
877 break;
878 case ISCSI_UEVENT_DESTROY_CONN:
879 err = iscsi_if_destroy_conn(transport, ev);
880 break;
881 case ISCSI_UEVENT_BIND_CONN:
Mike Christie7b7232f2006-02-01 21:06:49 -0600882 session = iscsi_session_lookup(ev->u.b_conn.session_handle);
883 conn = iscsi_conn_lookup(ev->u.b_conn.conn_handle);
884
885 if (session && conn)
886 ev->r.retcode = transport->bind_conn(session, conn,
887 ev->u.b_conn.transport_fd,
888 ev->u.b_conn.is_leading);
889 else
890 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700891 break;
892 case ISCSI_UEVENT_SET_PARAM:
Mike Christie7b7232f2006-02-01 21:06:49 -0600893 conn = iscsi_conn_lookup(ev->u.set_param.conn_handle);
894 if (conn)
895 ev->r.retcode = transport->set_param(conn,
896 ev->u.set_param.param, ev->u.set_param.value);
897 else
898 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700899 break;
900 case ISCSI_UEVENT_START_CONN:
Mike Christie7b7232f2006-02-01 21:06:49 -0600901 conn = iscsi_conn_lookup(ev->u.start_conn.conn_handle);
902 if (conn)
903 ev->r.retcode = transport->start_conn(conn);
904 else
905 err = -EINVAL;
906
Alex Aizman0896b752005-08-04 19:33:07 -0700907 break;
908 case ISCSI_UEVENT_STOP_CONN:
Mike Christie7b7232f2006-02-01 21:06:49 -0600909 conn = iscsi_conn_lookup(ev->u.stop_conn.conn_handle);
910 if (conn)
911 transport->stop_conn(conn, ev->u.stop_conn.flag);
912 else
913 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700914 break;
915 case ISCSI_UEVENT_SEND_PDU:
Mike Christie7b7232f2006-02-01 21:06:49 -0600916 conn = iscsi_conn_lookup(ev->u.send_pdu.conn_handle);
917 if (conn)
918 ev->r.retcode = transport->send_pdu(conn,
919 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
920 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
921 ev->u.send_pdu.data_size);
922 else
923 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -0700924 break;
925 case ISCSI_UEVENT_GET_STATS:
926 err = iscsi_if_get_stats(transport, skb, nlh);
927 break;
928 default:
929 err = -EINVAL;
930 break;
931 }
932
Mike Christie7b7232f2006-02-01 21:06:49 -0600933 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -0700934 return err;
935}
936
937/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
938 * processed by iscsi_if_recv_msg. Malformed skbs with wrong length are
939 * discarded silently. */
940static void
941iscsi_if_rx(struct sock *sk, int len)
942{
943 struct sk_buff *skb;
944
Arjan van de Ven0b950672006-01-11 13:16:10 +0100945 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700946 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
947 while (skb->len >= NLMSG_SPACE(0)) {
948 int err;
949 uint32_t rlen;
950 struct nlmsghdr *nlh;
951 struct iscsi_uevent *ev;
952
953 nlh = (struct nlmsghdr *)skb->data;
954 if (nlh->nlmsg_len < sizeof(*nlh) ||
955 skb->len < nlh->nlmsg_len) {
956 break;
957 }
958 ev = NLMSG_DATA(nlh);
959 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
960 if (rlen > skb->len)
961 rlen = skb->len;
962 err = iscsi_if_recv_msg(skb, nlh);
963 if (err) {
964 ev->type = ISCSI_KEVENT_IF_ERROR;
965 ev->iferror = err;
966 }
967 do {
968 /*
969 * special case for GET_STATS:
970 * on success - sending reply and stats from
971 * inside of if_recv_msg(),
972 * on error - fall through.
973 */
974 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
975 break;
976 err = iscsi_if_send_reply(
977 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
978 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Mike Christie7b8631b2006-01-13 18:05:50 -0600979 if (atomic_read(&z_reply->allocated) >=
980 z_reply->hiwat)
Alex Aizman0896b752005-08-04 19:33:07 -0700981 ev->iferror = -ENOMEM;
982 } while (err < 0 && err != -ECONNREFUSED);
983 skb_pull(skb, rlen);
984 }
985 kfree_skb(skb);
986 }
Arjan van de Ven0b950672006-01-11 13:16:10 +0100987 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700988}
989
Mike Christie7b8631b2006-01-13 18:05:50 -0600990#define iscsi_cdev_to_conn(_cdev) \
991 iscsi_dev_to_conn(_cdev->dev)
992
Alex Aizman0896b752005-08-04 19:33:07 -0700993/*
994 * iSCSI connection attrs
995 */
996#define iscsi_conn_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -0700998show_conn_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001000 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001001 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1002 struct iscsi_transport *t = conn->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001004 t->get_conn_param(conn, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001005 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Alex Aizman0896b752005-08-04 19:33:07 -07001008#define iscsi_conn_int_attr(field, param, format) \
1009 iscsi_conn_int_attr_show(param, format) \
1010static CLASS_DEVICE_ATTR(field, S_IRUGO, show_conn_int_param_##param, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Alex Aizman0896b752005-08-04 19:33:07 -07001012iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1013iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
1014iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1015iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1016iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1017iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Mike Christie7b8631b2006-01-13 18:05:50 -06001019#define iscsi_cdev_to_session(_cdev) \
1020 iscsi_dev_to_session(_cdev->dev)
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
Alex Aizman0896b752005-08-04 19:33:07 -07001023 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
Alex Aizman0896b752005-08-04 19:33:07 -07001025#define iscsi_session_int_attr_show(param, format) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026static ssize_t \
Alex Aizman0896b752005-08-04 19:33:07 -07001027show_session_int_param_##param(struct class_device *cdev, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{ \
Alex Aizman0896b752005-08-04 19:33:07 -07001029 uint32_t value = 0; \
Mike Christie7b8631b2006-01-13 18:05:50 -06001030 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
Mike Christie7b8631b2006-01-13 18:05:50 -06001031 struct iscsi_transport *t = session->transport; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 \
Mike Christie7b7232f2006-02-01 21:06:49 -06001033 t->get_session_param(session, param, &value); \
Alex Aizman0896b752005-08-04 19:33:07 -07001034 return snprintf(buf, 20, format"\n", value); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Alex Aizman0896b752005-08-04 19:33:07 -07001037#define iscsi_session_int_attr(field, param, format) \
1038 iscsi_session_int_attr_show(param, format) \
1039static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_int_param_##param, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Alex Aizman0896b752005-08-04 19:33:07 -07001041iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1042iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
1043iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
1044iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
1045iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1046iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1047iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1048iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Alex Aizman0896b752005-08-04 19:33:07 -07001050#define SETUP_SESSION_RD_ATTR(field, param) \
1051 if (priv->param_mask & (1 << param)) { \
1052 priv->session_attrs[count] = &class_device_attr_##field;\
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 count++; \
1054 }
1055
Alex Aizman0896b752005-08-04 19:33:07 -07001056#define SETUP_CONN_RD_ATTR(field, param) \
1057 if (priv->param_mask & (1 << param)) { \
1058 priv->conn_attrs[count] = &class_device_attr_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 count++; \
1060 }
1061
Alex Aizman0896b752005-08-04 19:33:07 -07001062static int iscsi_session_match(struct attribute_container *cont,
1063 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Mike Christie7b8631b2006-01-13 18:05:50 -06001065 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001067 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Alex Aizman0896b752005-08-04 19:33:07 -07001069 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return 0;
1071
Mike Christie7b8631b2006-01-13 18:05:50 -06001072 session = iscsi_dev_to_session(dev);
1073 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001074 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076
Alex Aizman0896b752005-08-04 19:33:07 -07001077 priv = to_iscsi_internal(shost->transportt);
1078 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1079 return 0;
1080
1081 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Alex Aizman0896b752005-08-04 19:33:07 -07001084static int iscsi_conn_match(struct attribute_container *cont,
1085 struct device *dev)
1086{
Mike Christie7b8631b2006-01-13 18:05:50 -06001087 struct iscsi_cls_session *session;
1088 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001089 struct Scsi_Host *shost;
1090 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Alex Aizman0896b752005-08-04 19:33:07 -07001092 if (!iscsi_is_conn_dev(dev))
1093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Mike Christie7b8631b2006-01-13 18:05:50 -06001095 conn = iscsi_dev_to_conn(dev);
1096 session = iscsi_dev_to_session(conn->dev.parent);
1097 shost = iscsi_session_to_shost(session);
1098
Alex Aizman0896b752005-08-04 19:33:07 -07001099 if (!shost->transportt)
1100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Alex Aizman0896b752005-08-04 19:33:07 -07001102 priv = to_iscsi_internal(shost->transportt);
1103 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1104 return 0;
1105
1106 return &priv->conn_cont.ac == cont;
1107}
1108
Mike Christie7b8631b2006-01-13 18:05:50 -06001109struct scsi_transport_template *
1110iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001111{
1112 struct iscsi_internal *priv;
1113 unsigned long flags;
1114 int count = 0, err;
1115
1116 BUG_ON(!tt);
1117
1118 priv = iscsi_if_transport_lookup(tt);
1119 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001120 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001121
1122 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
1123 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001124 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001125 memset(priv, 0, sizeof(*priv));
1126 INIT_LIST_HEAD(&priv->list);
1127 INIT_LIST_HEAD(&priv->sessions);
Alex Aizman0896b752005-08-04 19:33:07 -07001128 priv->iscsi_transport = tt;
1129
1130 priv->cdev.class = &iscsi_transport_class;
1131 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1132 err = class_device_register(&priv->cdev);
1133 if (err)
1134 goto free_priv;
1135
1136 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1137 if (err)
1138 goto unregister_cdev;
1139
1140 /* setup parameters mask */
1141 priv->param_mask = 0xFFFFFFFF;
1142 if (!(tt->caps & CAP_MULTI_R2T))
1143 priv->param_mask &= ~(1 << ISCSI_PARAM_MAX_R2T);
1144 if (!(tt->caps & CAP_HDRDGST))
1145 priv->param_mask &= ~(1 << ISCSI_PARAM_HDRDGST_EN);
1146 if (!(tt->caps & CAP_DATADGST))
1147 priv->param_mask &= ~(1 << ISCSI_PARAM_DATADGST_EN);
1148 if (!(tt->caps & CAP_MARKERS)) {
1149 priv->param_mask &= ~(1 << ISCSI_PARAM_IFMARKER_EN);
1150 priv->param_mask &= ~(1 << ISCSI_PARAM_OFMARKER_EN);
1151 }
1152
1153 /* connection parameters */
1154 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1155 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1156 priv->conn_cont.ac.match = iscsi_conn_match;
1157 transport_container_register(&priv->conn_cont);
1158
1159 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1160 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1161 SETUP_CONN_RD_ATTR(header_digest, ISCSI_PARAM_HDRDGST_EN);
1162 SETUP_CONN_RD_ATTR(data_digest, ISCSI_PARAM_DATADGST_EN);
1163 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1164 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1165
1166 BUG_ON(count > ISCSI_CONN_ATTRS);
1167 priv->conn_attrs[count] = NULL;
1168 count = 0;
1169
1170 /* session parameters */
1171 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1172 priv->session_cont.ac.class = &iscsi_session_class.class;
1173 priv->session_cont.ac.match = iscsi_session_match;
1174 transport_container_register(&priv->session_cont);
1175
1176 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1177 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1178 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1179 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1180 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_PARAM_MAX_BURST);
1181 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1182 SETUP_SESSION_RD_ATTR(data_seq_in_order,ISCSI_PARAM_DATASEQ_INORDER_EN)
1183 SETUP_SESSION_RD_ATTR(erl, ISCSI_PARAM_ERL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001186 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Alex Aizman0896b752005-08-04 19:33:07 -07001188 spin_lock_irqsave(&iscsi_transport_lock, flags);
1189 list_add(&priv->list, &iscsi_transports);
1190 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Alex Aizman0896b752005-08-04 19:33:07 -07001192 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001193 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Alex Aizman0896b752005-08-04 19:33:07 -07001195unregister_cdev:
1196 class_device_unregister(&priv->cdev);
1197free_priv:
1198 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001199 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
Alex Aizman0896b752005-08-04 19:33:07 -07001201EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Alex Aizman0896b752005-08-04 19:33:07 -07001203int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Alex Aizman0896b752005-08-04 19:33:07 -07001205 struct iscsi_internal *priv;
1206 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Alex Aizman0896b752005-08-04 19:33:07 -07001208 BUG_ON(!tt);
1209
Arjan van de Ven0b950672006-01-11 13:16:10 +01001210 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001211
1212 priv = iscsi_if_transport_lookup(tt);
1213 BUG_ON (!priv);
1214
Alex Aizman0896b752005-08-04 19:33:07 -07001215 spin_lock_irqsave(&iscsi_transport_lock, flags);
1216 list_del(&priv->list);
1217 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1218
1219 transport_container_unregister(&priv->conn_cont);
1220 transport_container_unregister(&priv->session_cont);
1221
1222 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1223 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001224 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001225
1226 return 0;
1227}
1228EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1229
1230static int
1231iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1232{
1233 struct netlink_notify *n = ptr;
1234
1235 if (event == NETLINK_URELEASE &&
1236 n->protocol == NETLINK_ISCSI && n->pid) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001237 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001238 unsigned long flags;
1239
Mike Christie7b8631b2006-01-13 18:05:50 -06001240 mempool_zone_complete(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001241 spin_lock_irqsave(&connlock, flags);
1242 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christie7b8631b2006-01-13 18:05:50 -06001243 mempool_zone_complete(conn->z_error);
1244 mempool_zone_complete(conn->z_pdu);
Alex Aizman0896b752005-08-04 19:33:07 -07001245 }
1246 spin_unlock_irqrestore(&connlock, flags);
1247 }
1248
1249 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Alex Aizman0896b752005-08-04 19:33:07 -07001252static struct notifier_block iscsi_nl_notifier = {
1253 .notifier_call = iscsi_rcv_nl_event,
1254};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256static __init int iscsi_transport_init(void)
1257{
Alex Aizman0896b752005-08-04 19:33:07 -07001258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Alex Aizman0896b752005-08-04 19:33:07 -07001260 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (err)
1262 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001263
1264 err = transport_class_register(&iscsi_connection_class);
1265 if (err)
1266 goto unregister_transport_class;
1267
1268 err = transport_class_register(&iscsi_session_class);
1269 if (err)
1270 goto unregister_conn_class;
1271
1272 err = netlink_register_notifier(&iscsi_nl_notifier);
1273 if (err)
1274 goto unregister_session_class;
1275
James Bottomley2290d2b2005-09-08 10:14:11 -05001276 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
Mike Christie7b8631b2006-01-13 18:05:50 -06001277 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001278 if (!nls) {
1279 err = -ENOBUFS;
1280 goto unregister_notifier;
1281 }
1282
Mike Christie7b8631b2006-01-13 18:05:50 -06001283 z_reply = mempool_zone_init(Z_MAX_REPLY,
Alex Aizman0896b752005-08-04 19:33:07 -07001284 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
Mike Christie7b8631b2006-01-13 18:05:50 -06001285 if (z_reply)
Alex Aizman0896b752005-08-04 19:33:07 -07001286 return 0;
1287
1288 sock_release(nls->sk_socket);
1289unregister_notifier:
1290 netlink_unregister_notifier(&iscsi_nl_notifier);
1291unregister_session_class:
1292 transport_class_unregister(&iscsi_session_class);
1293unregister_conn_class:
1294 transport_class_unregister(&iscsi_connection_class);
1295unregister_transport_class:
1296 class_unregister(&iscsi_transport_class);
1297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300static void __exit iscsi_transport_exit(void)
1301{
Mike Christie7b8631b2006-01-13 18:05:50 -06001302 mempool_zone_destroy(z_reply);
Alex Aizman0896b752005-08-04 19:33:07 -07001303 sock_release(nls->sk_socket);
1304 netlink_unregister_notifier(&iscsi_nl_notifier);
1305 transport_class_unregister(&iscsi_connection_class);
1306 transport_class_unregister(&iscsi_session_class);
1307 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310module_init(iscsi_transport_init);
1311module_exit(iscsi_transport_exit);
1312
Alex Aizman0896b752005-08-04 19:33:07 -07001313MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1314 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1315 "Alex Aizman <itn780@yahoo.com>");
1316MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317MODULE_LICENSE("GPL");