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