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