aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi
diff options
context:
space:
mode:
authorAlex Aizman <itn780@yahoo.com>2005-08-04 22:33:07 -0400
committerJames Bottomley <jejb@mulgrave.(none)>2005-08-06 10:34:48 -0400
commit0896b752302662909b52895bd7f601136001069d (patch)
treeceaf9740d0258ff45cadf81f10468e637f5ee13a /drivers/scsi
parentc213ca0792bd83075233a48090c46155249b3991 (diff)
[SCSI] open-iscsi/linux-iscsi-5 Initiator: Transport class update for iSCSI
Signed-off-by: Alex Aizman <itn780@yahoo.com> Signed-off-by: Dmitry Yusupov <dmitry_yus@yahoo.com> Signed-off-by: Mike Christie <michaelc@cs.wisc.edu> Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>
Diffstat (limited to 'drivers/scsi')
-rw-r--r--drivers/scsi/scsi_transport_iscsi.c1379
1 files changed, 1132 insertions, 247 deletions
diff --git a/drivers/scsi/scsi_transport_iscsi.c b/drivers/scsi/scsi_transport_iscsi.c
index 8bb8222ea589..2effa1c33fb2 100644
--- a/drivers/scsi/scsi_transport_iscsi.c
+++ b/drivers/scsi/scsi_transport_iscsi.c
@@ -1,8 +1,10 @@
1/* 1/*
2 * iSCSI transport class definitions 2 * iSCSI transport class definitions
3 * 3 *
4 * Copyright (C) IBM Corporation, 2004 4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 5 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
6 * 8 *
7 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -19,370 +21,1253 @@
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */ 22 */
21#include <linux/module.h> 23#include <linux/module.h>
24#include <linux/mempool.h>
25#include <net/tcp.h>
22#include <scsi/scsi.h> 26#include <scsi/scsi.h>
23#include <scsi/scsi_host.h> 27#include <scsi/scsi_host.h>
24#include <scsi/scsi_device.h> 28#include <scsi/scsi_device.h>
25#include <scsi/scsi_transport.h> 29#include <scsi/scsi_transport.h>
26#include <scsi/scsi_transport_iscsi.h> 30#include <scsi/scsi_transport_iscsi.h>
31#include <scsi/iscsi_if.h>
27 32
28#define ISCSI_SESSION_ATTRS 20 33#define ISCSI_SESSION_ATTRS 8
29#define ISCSI_HOST_ATTRS 2 34#define ISCSI_CONN_ATTRS 6
30 35
31struct iscsi_internal { 36struct iscsi_internal {
32 struct scsi_transport_template t; 37 struct scsi_transport_template t;
33 struct iscsi_function_template *fnt; 38 struct iscsi_transport *iscsi_transport;
39 struct list_head list;
40 /*
41 * List of sessions for this transport
42 */
43 struct list_head sessions;
44 /*
45 * lock to serialize access to the sessions list which must
46 * be taken after the rx_queue_sema
47 */
48 spinlock_t session_lock;
49 /*
50 * based on transport capabilities, at register time we set these
51 * bits to tell the transport class it wants attributes displayed
52 * in sysfs or that it can support different iSCSI Data-Path
53 * capabilities
54 */
55 uint32_t param_mask;
56
57 struct class_device cdev;
34 /* 58 /*
35 * We do not have any private or other attrs. 59 * We do not have any private or other attrs.
36 */ 60 */
61 struct transport_container conn_cont;
62 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
63 struct transport_container session_cont;
37 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1]; 64 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
38 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
39}; 65};
40 66
41#define to_iscsi_internal(tmpl) container_of(tmpl, struct iscsi_internal, t) 67/*
68 * list of registered transports and lock that must
69 * be held while accessing list. The iscsi_transport_lock must
70 * be acquired after the rx_queue_sema.
71 */
72static LIST_HEAD(iscsi_transports);
73static DEFINE_SPINLOCK(iscsi_transport_lock);
74
75#define to_iscsi_internal(tmpl) \
76 container_of(tmpl, struct iscsi_internal, t)
77
78#define cdev_to_iscsi_internal(_cdev) \
79 container_of(_cdev, struct iscsi_internal, cdev)
80
81static void iscsi_transport_release(struct class_device *cdev)
82{
83 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
84 kfree(priv);
85}
42 86
43static DECLARE_TRANSPORT_CLASS(iscsi_transport_class, 87/*
44 "iscsi_transport", 88 * iscsi_transport_class represents the iscsi_transports that are
89 * registered.
90 */
91static struct class iscsi_transport_class = {
92 .name = "iscsi_transport",
93 .release = iscsi_transport_release,
94};
95
96static ssize_t
97show_transport_handle(struct class_device *cdev, char *buf)
98{
99 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
100 return sprintf(buf, "%llu", iscsi_handle(priv->iscsi_transport));
101}
102static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
103
104#define show_transport_attr(name, format) \
105static ssize_t \
106show_transport_##name(struct class_device *cdev, char *buf) \
107{ \
108 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
109 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
110} \
111static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
112
113show_transport_attr(caps, "0x%x");
114show_transport_attr(max_lun, "%d");
115show_transport_attr(max_conn, "%d");
116show_transport_attr(max_cmd_len, "%d");
117
118static struct attribute *iscsi_transport_attrs[] = {
119 &class_device_attr_handle.attr,
120 &class_device_attr_caps.attr,
121 &class_device_attr_max_lun.attr,
122 &class_device_attr_max_conn.attr,
123 &class_device_attr_max_cmd_len.attr,
124 NULL,
125};
126
127static struct attribute_group iscsi_transport_group = {
128 .attrs = iscsi_transport_attrs,
129};
130
131static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
132 "iscsi_session",
45 NULL, 133 NULL,
46 NULL, 134 NULL,
47 NULL); 135 NULL);
48 136
49static DECLARE_TRANSPORT_CLASS(iscsi_host_class, 137static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
50 "iscsi_host", 138 "iscsi_connection",
51 NULL, 139 NULL,
52 NULL, 140 NULL,
53 NULL); 141 NULL);
142
143static struct sock *nls;
144static int daemon_pid;
145static DECLARE_MUTEX(rx_queue_sema);
146
147struct mempool_zone {
148 mempool_t *pool;
149 atomic_t allocated;
150 int size;
151 int hiwat;
152 struct list_head freequeue;
153 spinlock_t freelock;
154};
155
156static struct mempool_zone z_reply;
157
54/* 158/*
55 * iSCSI target and session attrs 159 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
160 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
161 * so daemon will notice OOM on NETLINK tranposrt level and will
162 * be able to predict or change operational behavior
56 */ 163 */
57#define iscsi_session_show_fn(field, format) \ 164#define Z_MAX_REPLY 8
58 \ 165#define Z_HIWAT_REPLY 6
59static ssize_t \ 166#define Z_MAX_PDU 8
60show_session_##field(struct class_device *cdev, char *buf) \ 167#define Z_HIWAT_PDU 6
61{ \ 168#define Z_MAX_ERROR 16
62 struct scsi_target *starget = transport_class_to_starget(cdev); \ 169#define Z_HIWAT_ERROR 12
63 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 170
64 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 171struct iscsi_if_conn {
65 \ 172 struct list_head conn_list; /* item in connlist */
66 if (i->fnt->get_##field) \ 173 struct list_head session_list; /* item in session->connections */
67 i->fnt->get_##field(starget); \ 174 iscsi_connh_t connh;
68 return snprintf(buf, 20, format"\n", iscsi_##field(starget)); \ 175 int active; /* must be accessed with the connlock */
176 struct Scsi_Host *host; /* originated shost */
177 struct device dev; /* sysfs transport/container device */
178 struct iscsi_transport *transport;
179 struct mempool_zone z_error;
180 struct mempool_zone z_pdu;
181 struct list_head freequeue;
182};
183
184#define iscsi_dev_to_if_conn(_dev) \
185 container_of(_dev, struct iscsi_if_conn, dev)
186
187#define iscsi_cdev_to_if_conn(_cdev) \
188 iscsi_dev_to_if_conn(_cdev->dev)
189
190static LIST_HEAD(connlist);
191static DEFINE_SPINLOCK(connlock);
192
193struct iscsi_if_session {
194 struct list_head list; /* item in session_list */
195 struct list_head connections;
196 iscsi_sessionh_t sessionh;
197 struct iscsi_transport *transport;
198 struct device dev; /* sysfs transport/container device */
199};
200
201#define iscsi_dev_to_if_session(_dev) \
202 container_of(_dev, struct iscsi_if_session, dev)
203
204#define iscsi_cdev_to_if_session(_cdev) \
205 iscsi_dev_to_if_session(_cdev->dev)
206
207#define iscsi_if_session_to_shost(_session) \
208 dev_to_shost(_session->dev.parent)
209
210static struct iscsi_if_conn*
211iscsi_if_find_conn(uint64_t key)
212{
213 unsigned long flags;
214 struct iscsi_if_conn *conn;
215
216 spin_lock_irqsave(&connlock, flags);
217 list_for_each_entry(conn, &connlist, conn_list)
218 if (conn->connh == key) {
219 spin_unlock_irqrestore(&connlock, flags);
220 return conn;
221 }
222 spin_unlock_irqrestore(&connlock, flags);
223 return NULL;
69} 224}
70 225
71#define iscsi_session_rd_attr(field, format) \ 226static struct iscsi_internal *
72 iscsi_session_show_fn(field, format) \ 227iscsi_if_transport_lookup(struct iscsi_transport *tt)
73static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_##field, NULL); 228{
229 struct iscsi_internal *priv;
230 unsigned long flags;
231
232 spin_lock_irqsave(&iscsi_transport_lock, flags);
233 list_for_each_entry(priv, &iscsi_transports, list) {
234 if (tt == priv->iscsi_transport) {
235 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
236 return priv;
237 }
238 }
239 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
240 return NULL;
241}
74 242
75iscsi_session_rd_attr(tpgt, "%hu"); 243static inline struct list_head *skb_to_lh(struct sk_buff *skb)
76iscsi_session_rd_attr(tsih, "%2x"); 244{
77iscsi_session_rd_attr(max_recv_data_segment_len, "%u"); 245 return (struct list_head *)&skb->cb;
78iscsi_session_rd_attr(max_burst_len, "%u"); 246}
79iscsi_session_rd_attr(first_burst_len, "%u");
80iscsi_session_rd_attr(def_time2wait, "%hu");
81iscsi_session_rd_attr(def_time2retain, "%hu");
82iscsi_session_rd_attr(max_outstanding_r2t, "%hu");
83iscsi_session_rd_attr(erl, "%d");
84 247
248static void*
249mempool_zone_alloc_skb(unsigned int gfp_mask, void *pool_data)
250{
251 struct mempool_zone *zone = pool_data;
85 252
86#define iscsi_session_show_bool_fn(field) \ 253 return alloc_skb(zone->size, gfp_mask);
87 \
88static ssize_t \
89show_session_bool_##field(struct class_device *cdev, char *buf) \
90{ \
91 struct scsi_target *starget = transport_class_to_starget(cdev); \
92 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
93 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \
94 \
95 if (i->fnt->get_##field) \
96 i->fnt->get_##field(starget); \
97 \
98 if (iscsi_##field(starget)) \
99 return sprintf(buf, "Yes\n"); \
100 return sprintf(buf, "No\n"); \
101} 254}
102 255
103#define iscsi_session_rd_bool_attr(field) \ 256static void
104 iscsi_session_show_bool_fn(field) \ 257mempool_zone_free_skb(void *element, void *pool_data)
105static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_bool_##field, NULL); 258{
259 kfree_skb(element);
260}
106 261
107iscsi_session_rd_bool_attr(initial_r2t); 262static void
108iscsi_session_rd_bool_attr(immediate_data); 263mempool_zone_complete(struct mempool_zone *zone)
109iscsi_session_rd_bool_attr(data_pdu_in_order); 264{
110iscsi_session_rd_bool_attr(data_sequence_in_order); 265 unsigned long flags;
266 struct list_head *lh, *n;
111 267
112#define iscsi_session_show_digest_fn(field) \ 268 spin_lock_irqsave(&zone->freelock, flags);
113 \ 269 list_for_each_safe(lh, n, &zone->freequeue) {
114static ssize_t \ 270 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
115show_##field(struct class_device *cdev, char *buf) \ 271 offsetof(struct sk_buff, cb));
116{ \ 272 if (!skb_shared(skb)) {
117 struct scsi_target *starget = transport_class_to_starget(cdev); \ 273 list_del(skb_to_lh(skb));
118 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 274 mempool_free(skb, zone->pool);
119 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 275 atomic_dec(&zone->allocated);
120 \ 276 }
121 if (i->fnt->get_##field) \ 277 }
122 i->fnt->get_##field(starget); \ 278 spin_unlock_irqrestore(&zone->freelock, flags);
123 \
124 if (iscsi_##field(starget)) \
125 return sprintf(buf, "CRC32C\n"); \
126 return sprintf(buf, "None\n"); \
127} 279}
128 280
129#define iscsi_session_rd_digest_attr(field) \ 281static int
130 iscsi_session_show_digest_fn(field) \ 282mempool_zone_init(struct mempool_zone *zp, unsigned max, unsigned size,
131static CLASS_DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); 283 unsigned hiwat)
284{
285 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
286 mempool_zone_free_skb, zp);
287 if (!zp->pool)
288 return -ENOMEM;
132 289
133iscsi_session_rd_digest_attr(header_digest); 290 zp->size = size;
134iscsi_session_rd_digest_attr(data_digest); 291 zp->hiwat = hiwat;
135 292
136static ssize_t 293 INIT_LIST_HEAD(&zp->freequeue);
137show_port(struct class_device *cdev, char *buf) 294 spin_lock_init(&zp->freelock);
295 atomic_set(&zp->allocated, 0);
296
297 return 0;
298}
299
300
301static struct sk_buff*
302mempool_zone_get_skb(struct mempool_zone *zone)
138{ 303{
139 struct scsi_target *starget = transport_class_to_starget(cdev); 304 struct sk_buff *skb;
140 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 305
141 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); 306 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
307 if (skb)
308 atomic_inc(&zone->allocated);
309 return skb;
310}
142 311
143 if (i->fnt->get_port) 312static int
144 i->fnt->get_port(starget); 313iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
314{
315 unsigned long flags;
316 int rc;
145 317
146 return snprintf(buf, 20, "%hu\n", ntohs(iscsi_port(starget))); 318 skb_get(skb);
319 rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT);
320 if (rc < 0) {
321 mempool_free(skb, zone->pool);
322 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
323 return rc;
324 }
325
326 spin_lock_irqsave(&zone->freelock, flags);
327 list_add(skb_to_lh(skb), &zone->freequeue);
328 spin_unlock_irqrestore(&zone->freelock, flags);
329
330 return 0;
147} 331}
148static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
149 332
150static ssize_t 333int iscsi_recv_pdu(iscsi_connh_t connh, struct iscsi_hdr *hdr,
151show_ip_address(struct class_device *cdev, char *buf) 334 char *data, uint32_t data_size)
152{ 335{
153 struct scsi_target *starget = transport_class_to_starget(cdev); 336 struct nlmsghdr *nlh;
154 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 337 struct sk_buff *skb;
155 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); 338 struct iscsi_uevent *ev;
339 struct iscsi_if_conn *conn;
340 char *pdu;
341 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
342 data_size);
343
344 conn = iscsi_if_find_conn(connh);
345 BUG_ON(!conn);
346
347 mempool_zone_complete(&conn->z_pdu);
348
349 skb = mempool_zone_get_skb(&conn->z_pdu);
350 if (!skb) {
351 iscsi_conn_error(connh, ISCSI_ERR_CONN_FAILED);
352 printk(KERN_ERR "iscsi%d: can not deliver control PDU: OOM\n",
353 conn->host->host_no);
354 return -ENOMEM;
355 }
156 356
157 if (i->fnt->get_ip_address) 357 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
158 i->fnt->get_ip_address(starget); 358 ev = NLMSG_DATA(nlh);
359 memset(ev, 0, sizeof(*ev));
360 ev->transport_handle = iscsi_handle(conn->transport);
361 ev->type = ISCSI_KEVENT_RECV_PDU;
362 if (atomic_read(&conn->z_pdu.allocated) >= conn->z_pdu.hiwat)
363 ev->iferror = -ENOMEM;
364 ev->r.recv_req.conn_handle = connh;
365 pdu = (char*)ev + sizeof(*ev);
366 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
367 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
159 368
160 if (iscsi_addr_type(starget) == AF_INET) 369 return iscsi_unicast_skb(&conn->z_pdu, skb);
161 return sprintf(buf, "%u.%u.%u.%u\n",
162 NIPQUAD(iscsi_sin_addr(starget)));
163 else if(iscsi_addr_type(starget) == AF_INET6)
164 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
165 NIP6(iscsi_sin6_addr(starget)));
166 return -EINVAL;
167} 370}
168static CLASS_DEVICE_ATTR(ip_address, S_IRUGO, show_ip_address, NULL); 371EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
169 372
170static ssize_t 373void iscsi_conn_error(iscsi_connh_t connh, enum iscsi_err error)
171show_isid(struct class_device *cdev, char *buf)
172{ 374{
173 struct scsi_target *starget = transport_class_to_starget(cdev); 375 struct nlmsghdr *nlh;
174 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 376 struct sk_buff *skb;
175 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); 377 struct iscsi_uevent *ev;
378 struct iscsi_if_conn *conn;
379 int len = NLMSG_SPACE(sizeof(*ev));
380
381 conn = iscsi_if_find_conn(connh);
382 BUG_ON(!conn);
383
384 mempool_zone_complete(&conn->z_error);
385
386 skb = mempool_zone_get_skb(&conn->z_error);
387 if (!skb) {
388 printk(KERN_ERR "iscsi%d: gracefully ignored conn error (%d)\n",
389 conn->host->host_no, error);
390 return;
391 }
392
393 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
394 ev = NLMSG_DATA(nlh);
395 ev->transport_handle = iscsi_handle(conn->transport);
396 ev->type = ISCSI_KEVENT_CONN_ERROR;
397 if (atomic_read(&conn->z_error.allocated) >= conn->z_error.hiwat)
398 ev->iferror = -ENOMEM;
399 ev->r.connerror.error = error;
400 ev->r.connerror.conn_handle = connh;
176 401
177 if (i->fnt->get_isid) 402 iscsi_unicast_skb(&conn->z_error, skb);
178 i->fnt->get_isid(starget);
179 403
180 return sprintf(buf, "%02x%02x%02x%02x%02x%02x\n", 404 printk(KERN_INFO "iscsi%d: detected conn error (%d)\n",
181 iscsi_isid(starget)[0], iscsi_isid(starget)[1], 405 conn->host->host_no, error);
182 iscsi_isid(starget)[2], iscsi_isid(starget)[3], 406}
183 iscsi_isid(starget)[4], iscsi_isid(starget)[5]); 407EXPORT_SYMBOL_GPL(iscsi_conn_error);
408
409static int
410iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
411 void *payload, int size)
412{
413 struct sk_buff *skb;
414 struct nlmsghdr *nlh;
415 int len = NLMSG_SPACE(size);
416 int flags = multi ? NLM_F_MULTI : 0;
417 int t = done ? NLMSG_DONE : type;
418
419 mempool_zone_complete(&z_reply);
420
421 skb = mempool_zone_get_skb(&z_reply);
422 /*
423 * FIXME:
424 * user is supposed to react on iferror == -ENOMEM;
425 * see iscsi_if_rx().
426 */
427 BUG_ON(!skb);
428
429 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
430 nlh->nlmsg_flags = flags;
431 memcpy(NLMSG_DATA(nlh), payload, size);
432 return iscsi_unicast_skb(&z_reply, skb);
184} 433}
185static CLASS_DEVICE_ATTR(isid, S_IRUGO, show_isid, NULL);
186 434
187/* 435/*
188 * This is used for iSCSI names. Normally, we follow 436 * iSCSI Session's hostdata organization:
189 * the transport class convention of having the lld 437 *
190 * set the field, but in these cases the value is 438 * *------------------* <== host->hostdata
191 * too large. 439 * | transport |
440 * |------------------| <== iscsi_hostdata(host->hostdata)
441 * | transport's data |
442 * |------------------| <== hostdata_session(host->hostdata)
443 * | interface's data |
444 * *------------------*
192 */ 445 */
193#define iscsi_session_show_str_fn(field) \ 446
194 \ 447#define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
195static ssize_t \ 448 _t->hostdata_size % sizeof(unsigned long) + \
196show_session_str_##field(struct class_device *cdev, char *buf) \ 449 sizeof(struct iscsi_if_session))
197{ \ 450
198 ssize_t ret = 0; \ 451#define hostdata_session(_hostdata) ((void*)_hostdata + sizeof(unsigned long) + \
199 struct scsi_target *starget = transport_class_to_starget(cdev); \ 452 ((struct iscsi_transport *) \
200 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 453 iscsi_ptr(*(uint64_t *)_hostdata))->hostdata_size)
201 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 454
202 \ 455static void iscsi_if_session_dev_release(struct device *dev)
203 if (i->fnt->get_##field) \ 456{
204 ret = i->fnt->get_##field(starget, buf, PAGE_SIZE); \ 457 struct iscsi_if_session *session = iscsi_dev_to_if_session(dev);
205 return ret; \ 458 struct iscsi_transport *transport = session->transport;
459 struct Scsi_Host *shost = iscsi_if_session_to_shost(session);
460 struct iscsi_if_conn *conn, *tmp;
461 unsigned long flags;
462
463 /* now free connections */
464 spin_lock_irqsave(&connlock, flags);
465 list_for_each_entry_safe(conn, tmp, &session->connections,
466 session_list) {
467 list_del(&conn->session_list);
468 mempool_destroy(conn->z_pdu.pool);
469 mempool_destroy(conn->z_error.pool);
470 kfree(conn);
471 }
472 spin_unlock_irqrestore(&connlock, flags);
473 scsi_host_put(shost);
474 module_put(transport->owner);
475}
476
477static int
478iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
479{
480 struct iscsi_transport *transport = priv->iscsi_transport;
481 struct iscsi_if_session *session;
482 struct Scsi_Host *shost;
483 unsigned long flags;
484 int error;
485
486 if (!try_module_get(transport->owner))
487 return -EPERM;
488
489 shost = scsi_host_alloc(transport->host_template,
490 hostdata_privsize(transport));
491 if (!shost) {
492 ev->r.c_session_ret.session_handle = iscsi_handle(NULL);
493 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
494 "session\n");
495 error = -ENOMEM;
496 goto out_module_put;
497 }
498 shost->max_id = 1;
499 shost->max_channel = 0;
500 shost->max_lun = transport->max_lun;
501 shost->max_cmd_len = transport->max_cmd_len;
502 shost->transportt = &priv->t;
503
504 /* store struct iscsi_transport in hostdata */
505 *(uint64_t*)shost->hostdata = ev->transport_handle;
506
507 ev->r.c_session_ret.session_handle = transport->create_session(
508 ev->u.c_session.initial_cmdsn, shost);
509 if (ev->r.c_session_ret.session_handle == iscsi_handle(NULL)) {
510 error = 0;
511 goto out_host_put;
512 }
513
514 /* host_no becomes assigned SID */
515 ev->r.c_session_ret.sid = shost->host_no;
516 /* initialize session */
517 session = hostdata_session(shost->hostdata);
518 INIT_LIST_HEAD(&session->connections);
519 INIT_LIST_HEAD(&session->list);
520 session->sessionh = ev->r.c_session_ret.session_handle;
521 session->transport = transport;
522
523 error = scsi_add_host(shost, NULL);
524 if (error)
525 goto out_destroy_session;
526
527 /*
528 * this is released in the dev's release function)
529 */
530 scsi_host_get(shost);
531 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", shost->host_no);
532 session->dev.parent = &shost->shost_gendev;
533 session->dev.release = iscsi_if_session_dev_release;
534 error = device_register(&session->dev);
535 if (error) {
536 printk(KERN_ERR "iscsi: could not register session%d's dev\n",
537 shost->host_no);
538 goto out_remove_host;
539 }
540 transport_register_device(&session->dev);
541
542 /* add this session to the list of active sessions */
543 spin_lock_irqsave(&priv->session_lock, flags);
544 list_add(&session->list, &priv->sessions);
545 spin_unlock_irqrestore(&priv->session_lock, flags);
546
547 return 0;
548
549out_remove_host:
550 scsi_remove_host(shost);
551out_destroy_session:
552 transport->destroy_session(ev->r.c_session_ret.session_handle);
553 ev->r.c_session_ret.session_handle = iscsi_handle(NULL);
554out_host_put:
555 scsi_host_put(shost);
556out_module_put:
557 module_put(transport->owner);
558 return error;
559}
560
561static int
562iscsi_if_destroy_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
563{
564 struct iscsi_transport *transport = priv->iscsi_transport;
565 struct Scsi_Host *shost;
566 struct iscsi_if_session *session;
567 unsigned long flags;
568 struct iscsi_if_conn *conn;
569 int error = 0;
570
571 shost = scsi_host_lookup(ev->u.d_session.sid);
572 if (shost == ERR_PTR(-ENXIO))
573 return -EEXIST;
574 session = hostdata_session(shost->hostdata);
575
576 /* check if we have active connections */
577 spin_lock_irqsave(&connlock, flags);
578 list_for_each_entry(conn, &session->connections, session_list) {
579 if (conn->active) {
580 printk(KERN_ERR "iscsi%d: can not destroy session: "
581 "has active connection (%p)\n",
582 shost->host_no, iscsi_ptr(conn->connh));
583 spin_unlock_irqrestore(&connlock, flags);
584 error = EIO;
585 goto out_release_ref;
586 }
587 }
588 spin_unlock_irqrestore(&connlock, flags);
589
590 scsi_remove_host(shost);
591 transport->destroy_session(ev->u.d_session.session_handle);
592 transport_unregister_device(&session->dev);
593 device_unregister(&session->dev);
594
595 /* remove this session from the list of active sessions */
596 spin_lock_irqsave(&priv->session_lock, flags);
597 list_del(&session->list);
598 spin_unlock_irqrestore(&priv->session_lock, flags);
599
600 /* ref from host alloc */
601 scsi_host_put(shost);
602out_release_ref:
603 /* ref from host lookup */
604 scsi_host_put(shost);
605 return error;
606}
607
608static void iscsi_if_conn_dev_release(struct device *dev)
609{
610 struct iscsi_if_conn *conn = iscsi_dev_to_if_conn(dev);
611 struct Scsi_Host *shost = conn->host;
612
613 scsi_host_put(shost);
614}
615
616static int
617iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
618{
619 struct iscsi_if_session *session;
620 struct Scsi_Host *shost;
621 struct iscsi_if_conn *conn;
622 unsigned long flags;
623 int error;
624
625 shost = scsi_host_lookup(ev->u.c_conn.sid);
626 if (shost == ERR_PTR(-ENXIO))
627 return -EEXIST;
628 session = hostdata_session(shost->hostdata);
629
630 conn = kmalloc(sizeof(struct iscsi_if_conn), GFP_KERNEL);
631 if (!conn) {
632 error = -ENOMEM;
633 goto out_release_ref;
634 }
635 memset(conn, 0, sizeof(struct iscsi_if_conn));
636 INIT_LIST_HEAD(&conn->session_list);
637 INIT_LIST_HEAD(&conn->conn_list);
638 conn->host = shost;
639 conn->transport = transport;
640
641 error = mempool_zone_init(&conn->z_pdu, Z_MAX_PDU,
642 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
643 sizeof(struct iscsi_hdr) +
644 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
645 Z_HIWAT_PDU);
646 if (error) {
647 printk(KERN_ERR "iscsi%d: can not allocate pdu zone for new "
648 "conn\n", shost->host_no);
649 goto out_free_conn;
650 }
651 error = mempool_zone_init(&conn->z_error, Z_MAX_ERROR,
652 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
653 Z_HIWAT_ERROR);
654 if (error) {
655 printk(KERN_ERR "iscsi%d: can not allocate error zone for "
656 "new conn\n", shost->host_no);
657 goto out_free_pdu_pool;
658 }
659
660 ev->r.handle = transport->create_conn(ev->u.c_conn.session_handle,
661 ev->u.c_conn.cid);
662 if (!ev->r.handle) {
663 error = -ENODEV;
664 goto out_free_error_pool;
665 }
666
667 conn->connh = ev->r.handle;
668
669 /*
670 * this is released in the dev's release function
671 */
672 if (!scsi_host_get(shost))
673 goto out_destroy_conn;
674 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
675 shost->host_no, ev->u.c_conn.cid);
676 conn->dev.parent = &session->dev;
677 conn->dev.release = iscsi_if_conn_dev_release;
678 error = device_register(&conn->dev);
679 if (error) {
680 printk(KERN_ERR "iscsi%d: could not register connections%u "
681 "dev\n", shost->host_no, ev->u.c_conn.cid);
682 goto out_release_parent_ref;
683 }
684 transport_register_device(&conn->dev);
685
686 spin_lock_irqsave(&connlock, flags);
687 list_add(&conn->conn_list, &connlist);
688 list_add(&conn->session_list, &session->connections);
689 conn->active = 1;
690 spin_unlock_irqrestore(&connlock, flags);
691
692 scsi_host_put(shost);
693 return 0;
694
695out_release_parent_ref:
696 scsi_host_put(shost);
697out_destroy_conn:
698 transport->destroy_conn(ev->r.handle);
699out_free_error_pool:
700 mempool_destroy(conn->z_error.pool);
701out_free_pdu_pool:
702 mempool_destroy(conn->z_pdu.pool);
703out_free_conn:
704 kfree(conn);
705out_release_ref:
706 scsi_host_put(shost);
707 return error;
206} 708}
207 709
208#define iscsi_session_rd_str_attr(field) \ 710static int
209 iscsi_session_show_str_fn(field) \ 711iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
210static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_str_##field, NULL); 712{
713 unsigned long flags;
714 struct iscsi_if_conn *conn;
715
716 conn = iscsi_if_find_conn(ev->u.d_conn.conn_handle);
717 if (!conn)
718 return -EEXIST;
719
720 transport->destroy_conn(ev->u.d_conn.conn_handle);
721
722 spin_lock_irqsave(&connlock, flags);
723 conn->active = 0;
724 list_del(&conn->conn_list);
725 spin_unlock_irqrestore(&connlock, flags);
726
727 transport_unregister_device(&conn->dev);
728 device_unregister(&conn->dev);
729 return 0;
730}
731
732static int
733iscsi_if_get_stats(struct iscsi_transport *transport, struct sk_buff *skb,
734 struct nlmsghdr *nlh)
735{
736 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
737 struct iscsi_stats *stats;
738 struct sk_buff *skbstat;
739 struct iscsi_if_conn *conn;
740 struct nlmsghdr *nlhstat;
741 struct iscsi_uevent *evstat;
742 int len = NLMSG_SPACE(sizeof(*ev) +
743 sizeof(struct iscsi_stats) +
744 sizeof(struct iscsi_stats_custom) *
745 ISCSI_STATS_CUSTOM_MAX);
746 int err = 0;
747
748 conn = iscsi_if_find_conn(ev->u.get_stats.conn_handle);
749 if (!conn)
750 return -EEXIST;
751
752 do {
753 int actual_size;
754
755 mempool_zone_complete(&conn->z_pdu);
756
757 skbstat = mempool_zone_get_skb(&conn->z_pdu);
758 if (!skbstat) {
759 printk(KERN_ERR "iscsi%d: can not deliver stats: OOM\n",
760 conn->host->host_no);
761 return -ENOMEM;
762 }
763
764 nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0,
765 (len - sizeof(*nlhstat)), 0);
766 evstat = NLMSG_DATA(nlhstat);
767 memset(evstat, 0, sizeof(*evstat));
768 evstat->transport_handle = iscsi_handle(conn->transport);
769 evstat->type = nlh->nlmsg_type;
770 if (atomic_read(&conn->z_pdu.allocated) >= conn->z_pdu.hiwat)
771 evstat->iferror = -ENOMEM;
772 evstat->u.get_stats.conn_handle =
773 ev->u.get_stats.conn_handle;
774 stats = (struct iscsi_stats *)
775 ((char*)evstat + sizeof(*evstat));
776 memset(stats, 0, sizeof(*stats));
777
778 transport->get_stats(ev->u.get_stats.conn_handle, stats);
779 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
780 sizeof(struct iscsi_stats) +
781 sizeof(struct iscsi_stats_custom) *
782 stats->custom_length);
783 actual_size -= sizeof(*nlhstat);
784 actual_size = NLMSG_LENGTH(actual_size);
785 skb_trim(skb, NLMSG_ALIGN(actual_size));
786 nlhstat->nlmsg_len = actual_size;
787
788 err = iscsi_unicast_skb(&conn->z_pdu, skbstat);
789 } while (err < 0 && err != -ECONNREFUSED);
790
791 return err;
792}
793
794static int
795iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
796{
797 int err = 0;
798 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
799 struct iscsi_transport *transport = NULL;
800 struct iscsi_internal *priv;
801
802 if (NETLINK_CREDS(skb)->uid)
803 return -EPERM;
804
805 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
806 if (!priv)
807 return -EINVAL;
808 transport = priv->iscsi_transport;
809
810 daemon_pid = NETLINK_CREDS(skb)->pid;
811
812 switch (nlh->nlmsg_type) {
813 case ISCSI_UEVENT_CREATE_SESSION:
814 err = iscsi_if_create_session(priv, ev);
815 break;
816 case ISCSI_UEVENT_DESTROY_SESSION:
817 err = iscsi_if_destroy_session(priv, ev);
818 break;
819 case ISCSI_UEVENT_CREATE_CONN:
820 err = iscsi_if_create_conn(transport, ev);
821 break;
822 case ISCSI_UEVENT_DESTROY_CONN:
823 err = iscsi_if_destroy_conn(transport, ev);
824 break;
825 case ISCSI_UEVENT_BIND_CONN:
826 if (!iscsi_if_find_conn(ev->u.b_conn.conn_handle))
827 return -EEXIST;
828 ev->r.retcode = transport->bind_conn(
829 ev->u.b_conn.session_handle,
830 ev->u.b_conn.conn_handle,
831 ev->u.b_conn.transport_fd,
832 ev->u.b_conn.is_leading);
833 break;
834 case ISCSI_UEVENT_SET_PARAM:
835 if (!iscsi_if_find_conn(ev->u.set_param.conn_handle))
836 return -EEXIST;
837 ev->r.retcode = transport->set_param(
838 ev->u.set_param.conn_handle,
839 ev->u.set_param.param, ev->u.set_param.value);
840 break;
841 case ISCSI_UEVENT_START_CONN:
842 if (!iscsi_if_find_conn(ev->u.start_conn.conn_handle))
843 return -EEXIST;
844 ev->r.retcode = transport->start_conn(
845 ev->u.start_conn.conn_handle);
846 break;
847 case ISCSI_UEVENT_STOP_CONN:
848 if (!iscsi_if_find_conn(ev->u.stop_conn.conn_handle))
849 return -EEXIST;
850 transport->stop_conn(ev->u.stop_conn.conn_handle,
851 ev->u.stop_conn.flag);
852 break;
853 case ISCSI_UEVENT_SEND_PDU:
854 if (!iscsi_if_find_conn(ev->u.send_pdu.conn_handle))
855 return -EEXIST;
856 ev->r.retcode = transport->send_pdu(
857 ev->u.send_pdu.conn_handle,
858 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
859 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
860 ev->u.send_pdu.data_size);
861 break;
862 case ISCSI_UEVENT_GET_STATS:
863 err = iscsi_if_get_stats(transport, skb, nlh);
864 break;
865 default:
866 err = -EINVAL;
867 break;
868 }
869
870 return err;
871}
872
873/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
874 * processed by iscsi_if_recv_msg. Malformed skbs with wrong length are
875 * discarded silently. */
876static void
877iscsi_if_rx(struct sock *sk, int len)
878{
879 struct sk_buff *skb;
211 880
212iscsi_session_rd_str_attr(target_name); 881 down(&rx_queue_sema);
213iscsi_session_rd_str_attr(target_alias); 882 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
883 while (skb->len >= NLMSG_SPACE(0)) {
884 int err;
885 uint32_t rlen;
886 struct nlmsghdr *nlh;
887 struct iscsi_uevent *ev;
888
889 nlh = (struct nlmsghdr *)skb->data;
890 if (nlh->nlmsg_len < sizeof(*nlh) ||
891 skb->len < nlh->nlmsg_len) {
892 break;
893 }
894 ev = NLMSG_DATA(nlh);
895 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
896 if (rlen > skb->len)
897 rlen = skb->len;
898 err = iscsi_if_recv_msg(skb, nlh);
899 if (err) {
900 ev->type = ISCSI_KEVENT_IF_ERROR;
901 ev->iferror = err;
902 }
903 do {
904 /*
905 * special case for GET_STATS:
906 * on success - sending reply and stats from
907 * inside of if_recv_msg(),
908 * on error - fall through.
909 */
910 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
911 break;
912 err = iscsi_if_send_reply(
913 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
914 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
915 if (atomic_read(&z_reply.allocated) >=
916 z_reply.hiwat)
917 ev->iferror = -ENOMEM;
918 } while (err < 0 && err != -ECONNREFUSED);
919 skb_pull(skb, rlen);
920 }
921 kfree_skb(skb);
922 }
923 up(&rx_queue_sema);
924}
214 925
215/* 926/*
216 * iSCSI host attrs 927 * iSCSI connection attrs
217 */ 928 */
929#define iscsi_conn_int_attr_show(param, format) \
930static ssize_t \
931show_conn_int_param_##param(struct class_device *cdev, char *buf) \
932{ \
933 uint32_t value = 0; \
934 struct iscsi_if_conn *conn = iscsi_cdev_to_if_conn(cdev); \
935 struct iscsi_internal *priv; \
936 \
937 priv = to_iscsi_internal(conn->host->transportt); \
938 if (priv->param_mask & (1 << param)) \
939 priv->iscsi_transport->get_param(conn->connh, param, &value); \
940 return snprintf(buf, 20, format"\n", value); \
941}
942
943#define iscsi_conn_int_attr(field, param, format) \
944 iscsi_conn_int_attr_show(param, format) \
945static CLASS_DEVICE_ATTR(field, S_IRUGO, show_conn_int_param_##param, NULL);
946
947iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
948iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
949iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
950iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
951iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
952iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
218 953
219/* 954/*
220 * Again, this is used for iSCSI names. Normally, we follow 955 * iSCSI session attrs
221 * the transport class convention of having the lld set
222 * the field, but in these cases the value is too large.
223 */ 956 */
224#define iscsi_host_show_str_fn(field) \ 957#define iscsi_session_int_attr_show(param, format) \
225 \
226static ssize_t \ 958static ssize_t \
227show_host_str_##field(struct class_device *cdev, char *buf) \ 959show_session_int_param_##param(struct class_device *cdev, char *buf) \
228{ \ 960{ \
229 int ret = 0; \ 961 uint32_t value = 0; \
230 struct Scsi_Host *shost = transport_class_to_shost(cdev); \ 962 struct iscsi_if_session *session = iscsi_cdev_to_if_session(cdev); \
231 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 963 struct Scsi_Host *shost = iscsi_if_session_to_shost(session); \
964 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
965 struct iscsi_if_conn *conn = NULL; \
966 unsigned long flags; \
967 \
968 spin_lock_irqsave(&connlock, flags); \
969 if (!list_empty(&session->connections)) \
970 conn = list_entry(session->connections.next, \
971 struct iscsi_if_conn, session_list); \
972 spin_unlock_irqrestore(&connlock, flags); \
232 \ 973 \
233 if (i->fnt->get_##field) \ 974 if (conn && (priv->param_mask & (1 << param))) \
234 ret = i->fnt->get_##field(shost, buf, PAGE_SIZE); \ 975 priv->iscsi_transport->get_param(conn->connh, param, &value);\
235 return ret; \ 976 return snprintf(buf, 20, format"\n", value); \
236} 977}
237 978
238#define iscsi_host_rd_str_attr(field) \ 979#define iscsi_session_int_attr(field, param, format) \
239 iscsi_host_show_str_fn(field) \ 980 iscsi_session_int_attr_show(param, format) \
240static CLASS_DEVICE_ATTR(field, S_IRUGO, show_host_str_##field, NULL); 981static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_int_param_##param, NULL);
241 982
242iscsi_host_rd_str_attr(initiator_name); 983iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
243iscsi_host_rd_str_attr(initiator_alias); 984iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
985iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
986iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
987iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
988iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
989iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
990iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
244 991
245#define SETUP_SESSION_RD_ATTR(field) \ 992#define SETUP_SESSION_RD_ATTR(field, param) \
246 if (i->fnt->show_##field) { \ 993 if (priv->param_mask & (1 << param)) { \
247 i->session_attrs[count] = &class_device_attr_##field; \ 994 priv->session_attrs[count] = &class_device_attr_##field;\
248 count++; \ 995 count++; \
249 } 996 }
250 997
251#define SETUP_HOST_RD_ATTR(field) \ 998#define SETUP_CONN_RD_ATTR(field, param) \
252 if (i->fnt->show_##field) { \ 999 if (priv->param_mask & (1 << param)) { \
253 i->host_attrs[count] = &class_device_attr_##field; \ 1000 priv->conn_attrs[count] = &class_device_attr_##field; \
254 count++; \ 1001 count++; \
255 } 1002 }
256 1003
257static int iscsi_host_match(struct attribute_container *cont, 1004static int iscsi_is_session_dev(const struct device *dev)
258 struct device *dev) 1005{
1006 return dev->release == iscsi_if_session_dev_release;
1007}
1008
1009static int iscsi_session_match(struct attribute_container *cont,
1010 struct device *dev)
259{ 1011{
1012 struct iscsi_if_session *session;
260 struct Scsi_Host *shost; 1013 struct Scsi_Host *shost;
261 struct iscsi_internal *i; 1014 struct iscsi_internal *priv;
1015
1016 if (!iscsi_is_session_dev(dev))
1017 return 0;
262 1018
263 if (!scsi_is_host_device(dev)) 1019 session = iscsi_dev_to_if_session(dev);
1020 shost = iscsi_if_session_to_shost(session);
1021 if (!shost->transportt)
264 return 0; 1022 return 0;
265 1023
266 shost = dev_to_shost(dev); 1024 priv = to_iscsi_internal(shost->transportt);
267 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1025 if (priv->session_cont.ac.class != &iscsi_session_class.class)
268 != &iscsi_host_class.class)
269 return 0; 1026 return 0;
270 1027
271 i = to_iscsi_internal(shost->transportt); 1028 return &priv->session_cont.ac == cont;
272
273 return &i->t.host_attrs.ac == cont;
274} 1029}
275 1030
276static int iscsi_target_match(struct attribute_container *cont, 1031static int iscsi_is_conn_dev(const struct device *dev)
277 struct device *dev)
278{ 1032{
1033 return dev->release == iscsi_if_conn_dev_release;
1034}
1035
1036static int iscsi_conn_match(struct attribute_container *cont,
1037 struct device *dev)
1038{
1039 struct iscsi_if_conn *conn;
279 struct Scsi_Host *shost; 1040 struct Scsi_Host *shost;
280 struct iscsi_internal *i; 1041 struct iscsi_internal *priv;
281 1042
282 if (!scsi_is_target_device(dev)) 1043 if (!iscsi_is_conn_dev(dev))
283 return 0; 1044 return 0;
284 1045
285 shost = dev_to_shost(dev->parent); 1046 conn = iscsi_dev_to_if_conn(dev);
286 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1047 shost = conn->host;
287 != &iscsi_host_class.class) 1048 if (!shost->transportt)
288 return 0; 1049 return 0;
289 1050
290 i = to_iscsi_internal(shost->transportt); 1051 priv = to_iscsi_internal(shost->transportt);
291 1052 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
292 return &i->t.target_attrs.ac == cont; 1053 return 0;
293}
294
295struct scsi_transport_template *
296iscsi_attach_transport(struct iscsi_function_template *fnt)
297{
298 struct iscsi_internal *i = kmalloc(sizeof(struct iscsi_internal),
299 GFP_KERNEL);
300 int count = 0;
301
302 if (unlikely(!i))
303 return NULL;
304
305 memset(i, 0, sizeof(struct iscsi_internal));
306 i->fnt = fnt;
307
308 i->t.target_attrs.ac.attrs = &i->session_attrs[0];
309 i->t.target_attrs.ac.class = &iscsi_transport_class.class;
310 i->t.target_attrs.ac.match = iscsi_target_match;
311 transport_container_register(&i->t.target_attrs);
312 i->t.target_size = sizeof(struct iscsi_class_session);
313
314 SETUP_SESSION_RD_ATTR(tsih);
315 SETUP_SESSION_RD_ATTR(isid);
316 SETUP_SESSION_RD_ATTR(header_digest);
317 SETUP_SESSION_RD_ATTR(data_digest);
318 SETUP_SESSION_RD_ATTR(target_name);
319 SETUP_SESSION_RD_ATTR(target_alias);
320 SETUP_SESSION_RD_ATTR(port);
321 SETUP_SESSION_RD_ATTR(tpgt);
322 SETUP_SESSION_RD_ATTR(ip_address);
323 SETUP_SESSION_RD_ATTR(initial_r2t);
324 SETUP_SESSION_RD_ATTR(immediate_data);
325 SETUP_SESSION_RD_ATTR(max_recv_data_segment_len);
326 SETUP_SESSION_RD_ATTR(max_burst_len);
327 SETUP_SESSION_RD_ATTR(first_burst_len);
328 SETUP_SESSION_RD_ATTR(def_time2wait);
329 SETUP_SESSION_RD_ATTR(def_time2retain);
330 SETUP_SESSION_RD_ATTR(max_outstanding_r2t);
331 SETUP_SESSION_RD_ATTR(data_pdu_in_order);
332 SETUP_SESSION_RD_ATTR(data_sequence_in_order);
333 SETUP_SESSION_RD_ATTR(erl);
334 1054
335 BUG_ON(count > ISCSI_SESSION_ATTRS); 1055 return &priv->conn_cont.ac == cont;
336 i->session_attrs[count] = NULL; 1056}
1057
1058int iscsi_register_transport(struct iscsi_transport *tt)
1059{
1060 struct iscsi_internal *priv;
1061 unsigned long flags;
1062 int count = 0, err;
1063
1064 BUG_ON(!tt);
1065
1066 priv = iscsi_if_transport_lookup(tt);
1067 if (priv)
1068 return -EEXIST;
1069
1070 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
1071 if (!priv)
1072 return -ENOMEM;
1073 memset(priv, 0, sizeof(*priv));
1074 INIT_LIST_HEAD(&priv->list);
1075 INIT_LIST_HEAD(&priv->sessions);
1076 spin_lock_init(&priv->session_lock);
1077 priv->iscsi_transport = tt;
1078
1079 priv->cdev.class = &iscsi_transport_class;
1080 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1081 err = class_device_register(&priv->cdev);
1082 if (err)
1083 goto free_priv;
1084
1085 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1086 if (err)
1087 goto unregister_cdev;
1088
1089 /* setup parameters mask */
1090 priv->param_mask = 0xFFFFFFFF;
1091 if (!(tt->caps & CAP_MULTI_R2T))
1092 priv->param_mask &= ~(1 << ISCSI_PARAM_MAX_R2T);
1093 if (!(tt->caps & CAP_HDRDGST))
1094 priv->param_mask &= ~(1 << ISCSI_PARAM_HDRDGST_EN);
1095 if (!(tt->caps & CAP_DATADGST))
1096 priv->param_mask &= ~(1 << ISCSI_PARAM_DATADGST_EN);
1097 if (!(tt->caps & CAP_MARKERS)) {
1098 priv->param_mask &= ~(1 << ISCSI_PARAM_IFMARKER_EN);
1099 priv->param_mask &= ~(1 << ISCSI_PARAM_OFMARKER_EN);
1100 }
337 1101
338 i->t.host_attrs.ac.attrs = &i->host_attrs[0]; 1102 /* connection parameters */
339 i->t.host_attrs.ac.class = &iscsi_host_class.class; 1103 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
340 i->t.host_attrs.ac.match = iscsi_host_match; 1104 priv->conn_cont.ac.class = &iscsi_connection_class.class;
341 transport_container_register(&i->t.host_attrs); 1105 priv->conn_cont.ac.match = iscsi_conn_match;
342 i->t.host_size = 0; 1106 transport_container_register(&priv->conn_cont);
343 1107
1108 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1109 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1110 SETUP_CONN_RD_ATTR(header_digest, ISCSI_PARAM_HDRDGST_EN);
1111 SETUP_CONN_RD_ATTR(data_digest, ISCSI_PARAM_DATADGST_EN);
1112 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1113 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1114
1115 BUG_ON(count > ISCSI_CONN_ATTRS);
1116 priv->conn_attrs[count] = NULL;
344 count = 0; 1117 count = 0;
345 SETUP_HOST_RD_ATTR(initiator_name);
346 SETUP_HOST_RD_ATTR(initiator_alias);
347 1118
348 BUG_ON(count > ISCSI_HOST_ATTRS); 1119 /* session parameters */
349 i->host_attrs[count] = NULL; 1120 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1121 priv->session_cont.ac.class = &iscsi_session_class.class;
1122 priv->session_cont.ac.match = iscsi_session_match;
1123 transport_container_register(&priv->session_cont);
1124
1125 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1126 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1127 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1128 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1129 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_PARAM_MAX_BURST);
1130 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1131 SETUP_SESSION_RD_ATTR(data_seq_in_order,ISCSI_PARAM_DATASEQ_INORDER_EN)
1132 SETUP_SESSION_RD_ATTR(erl, ISCSI_PARAM_ERL);
1133
1134 BUG_ON(count > ISCSI_SESSION_ATTRS);
1135 priv->session_attrs[count] = NULL;
1136
1137 spin_lock_irqsave(&iscsi_transport_lock, flags);
1138 list_add(&priv->list, &iscsi_transports);
1139 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1140
1141 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1142 return 0;
350 1143
351 return &i->t; 1144unregister_cdev:
1145 class_device_unregister(&priv->cdev);
1146free_priv:
1147 kfree(priv);
1148 return err;
352} 1149}
1150EXPORT_SYMBOL_GPL(iscsi_register_transport);
1151
1152int iscsi_unregister_transport(struct iscsi_transport *tt)
1153{
1154 struct iscsi_internal *priv;
1155 unsigned long flags;
1156
1157 BUG_ON(!tt);
1158
1159 down(&rx_queue_sema);
1160
1161 priv = iscsi_if_transport_lookup(tt);
1162 BUG_ON (!priv);
1163
1164 spin_lock_irqsave(&priv->session_lock, flags);
1165 if (!list_empty(&priv->sessions)) {
1166 spin_unlock_irqrestore(&priv->session_lock, flags);
1167 up(&rx_queue_sema);
1168 return -EPERM;
1169 }
1170 spin_unlock_irqrestore(&priv->session_lock, flags);
1171
1172 spin_lock_irqsave(&iscsi_transport_lock, flags);
1173 list_del(&priv->list);
1174 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1175
1176 transport_container_unregister(&priv->conn_cont);
1177 transport_container_unregister(&priv->session_cont);
1178
1179 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1180 class_device_unregister(&priv->cdev);
1181 up(&rx_queue_sema);
353 1182
354EXPORT_SYMBOL(iscsi_attach_transport); 1183 return 0;
1184}
1185EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
355 1186
356void iscsi_release_transport(struct scsi_transport_template *t) 1187static int
1188iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
357{ 1189{
358 struct iscsi_internal *i = to_iscsi_internal(t); 1190 struct netlink_notify *n = ptr;
1191
1192 if (event == NETLINK_URELEASE &&
1193 n->protocol == NETLINK_ISCSI && n->pid) {
1194 struct iscsi_if_conn *conn;
1195 unsigned long flags;
1196
1197 mempool_zone_complete(&z_reply);
1198 spin_lock_irqsave(&connlock, flags);
1199 list_for_each_entry(conn, &connlist, conn_list) {
1200 mempool_zone_complete(&conn->z_error);
1201 mempool_zone_complete(&conn->z_pdu);
1202 }
1203 spin_unlock_irqrestore(&connlock, flags);
1204 }
359 1205
360 transport_container_unregister(&i->t.target_attrs); 1206 return NOTIFY_DONE;
361 transport_container_unregister(&i->t.host_attrs);
362
363 kfree(i);
364} 1207}
365 1208
366EXPORT_SYMBOL(iscsi_release_transport); 1209static struct notifier_block iscsi_nl_notifier = {
1210 .notifier_call = iscsi_rcv_nl_event,
1211};
367 1212
368static __init int iscsi_transport_init(void) 1213static __init int iscsi_transport_init(void)
369{ 1214{
370 int err = transport_class_register(&iscsi_transport_class); 1215 int err;
371 1216
1217 err = class_register(&iscsi_transport_class);
372 if (err) 1218 if (err)
373 return err; 1219 return err;
374 return transport_class_register(&iscsi_host_class); 1220
1221 err = transport_class_register(&iscsi_connection_class);
1222 if (err)
1223 goto unregister_transport_class;
1224
1225 err = transport_class_register(&iscsi_session_class);
1226 if (err)
1227 goto unregister_conn_class;
1228
1229 err = netlink_register_notifier(&iscsi_nl_notifier);
1230 if (err)
1231 goto unregister_session_class;
1232
1233 nls = netlink_kernel_create(NETLINK_ISCSI, iscsi_if_rx);
1234 if (!nls) {
1235 err = -ENOBUFS;
1236 goto unregister_notifier;
1237 }
1238
1239 err = mempool_zone_init(&z_reply, Z_MAX_REPLY,
1240 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
1241 if (!err)
1242 return 0;
1243
1244 sock_release(nls->sk_socket);
1245unregister_notifier:
1246 netlink_unregister_notifier(&iscsi_nl_notifier);
1247unregister_session_class:
1248 transport_class_unregister(&iscsi_session_class);
1249unregister_conn_class:
1250 transport_class_unregister(&iscsi_connection_class);
1251unregister_transport_class:
1252 class_unregister(&iscsi_transport_class);
1253 return err;
375} 1254}
376 1255
377static void __exit iscsi_transport_exit(void) 1256static void __exit iscsi_transport_exit(void)
378{ 1257{
379 transport_class_unregister(&iscsi_host_class); 1258 mempool_destroy(z_reply.pool);
380 transport_class_unregister(&iscsi_transport_class); 1259 sock_release(nls->sk_socket);
1260 netlink_unregister_notifier(&iscsi_nl_notifier);
1261 transport_class_unregister(&iscsi_connection_class);
1262 transport_class_unregister(&iscsi_session_class);
1263 class_unregister(&iscsi_transport_class);
381} 1264}
382 1265
383module_init(iscsi_transport_init); 1266module_init(iscsi_transport_init);
384module_exit(iscsi_transport_exit); 1267module_exit(iscsi_transport_exit);
385 1268
386MODULE_AUTHOR("Mike Christie"); 1269MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
387MODULE_DESCRIPTION("iSCSI Transport Attributes"); 1270 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1271 "Alex Aizman <itn780@yahoo.com>");
1272MODULE_DESCRIPTION("iSCSI Transport Interface");
388MODULE_LICENSE("GPL"); 1273MODULE_LICENSE("GPL");