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