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 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 34 | #define ISCSI_SESSION_ATTRS 11 |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 35 | #define ISCSI_CONN_ATTRS 11 |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 36 | #define ISCSI_HOST_ATTRS 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | struct iscsi_internal { |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 39 | int daemon_pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct scsi_transport_template t; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 41 | struct iscsi_transport *iscsi_transport; |
| 42 | struct list_head list; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 43 | struct class_device cdev; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 44 | |
| 45 | struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1]; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 46 | struct transport_container conn_cont; |
| 47 | struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1]; |
| 48 | struct transport_container session_cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 52 | static int iscsi_session_nr; /* sysfs session id for next new session */ |
| 53 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 54 | /* |
| 55 | * list of registered transports and lock that must |
| 56 | * be held while accessing list. The iscsi_transport_lock must |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 57 | * be acquired after the rx_queue_mutex. |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 58 | */ |
| 59 | static LIST_HEAD(iscsi_transports); |
| 60 | static DEFINE_SPINLOCK(iscsi_transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 62 | #define to_iscsi_internal(tmpl) \ |
| 63 | container_of(tmpl, struct iscsi_internal, t) |
| 64 | |
| 65 | #define cdev_to_iscsi_internal(_cdev) \ |
| 66 | container_of(_cdev, struct iscsi_internal, cdev) |
| 67 | |
| 68 | static void iscsi_transport_release(struct class_device *cdev) |
| 69 | { |
| 70 | struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); |
| 71 | kfree(priv); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * iscsi_transport_class represents the iscsi_transports that are |
| 76 | * registered. |
| 77 | */ |
| 78 | static struct class iscsi_transport_class = { |
| 79 | .name = "iscsi_transport", |
| 80 | .release = iscsi_transport_release, |
| 81 | }; |
| 82 | |
| 83 | static ssize_t |
| 84 | show_transport_handle(struct class_device *cdev, char *buf) |
| 85 | { |
| 86 | struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); |
Mike Christie | 762e2bf | 2005-09-12 21:01:46 -0500 | [diff] [blame] | 87 | 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] | 88 | } |
| 89 | static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL); |
| 90 | |
| 91 | #define show_transport_attr(name, format) \ |
| 92 | static ssize_t \ |
| 93 | show_transport_##name(struct class_device *cdev, char *buf) \ |
| 94 | { \ |
| 95 | struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \ |
| 96 | return sprintf(buf, format"\n", priv->iscsi_transport->name); \ |
| 97 | } \ |
| 98 | static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL); |
| 99 | |
| 100 | show_transport_attr(caps, "0x%x"); |
| 101 | show_transport_attr(max_lun, "%d"); |
| 102 | show_transport_attr(max_conn, "%d"); |
| 103 | show_transport_attr(max_cmd_len, "%d"); |
| 104 | |
| 105 | static struct attribute *iscsi_transport_attrs[] = { |
| 106 | &class_device_attr_handle.attr, |
| 107 | &class_device_attr_caps.attr, |
| 108 | &class_device_attr_max_lun.attr, |
| 109 | &class_device_attr_max_conn.attr, |
| 110 | &class_device_attr_max_cmd_len.attr, |
| 111 | NULL, |
| 112 | }; |
| 113 | |
| 114 | static struct attribute_group iscsi_transport_group = { |
| 115 | .attrs = iscsi_transport_attrs, |
| 116 | }; |
| 117 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 118 | static int iscsi_setup_host(struct transport_container *tc, struct device *dev, |
| 119 | struct class_device *cdev) |
| 120 | { |
| 121 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 122 | struct iscsi_host *ihost = shost->shost_data; |
| 123 | |
| 124 | memset(ihost, 0, sizeof(*ihost)); |
| 125 | INIT_LIST_HEAD(&ihost->sessions); |
| 126 | mutex_init(&ihost->mutex); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static DECLARE_TRANSPORT_CLASS(iscsi_host_class, |
| 131 | "iscsi_host", |
| 132 | iscsi_setup_host, |
| 133 | NULL, |
| 134 | NULL); |
| 135 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 136 | static DECLARE_TRANSPORT_CLASS(iscsi_session_class, |
| 137 | "iscsi_session", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | NULL, |
| 139 | NULL, |
| 140 | NULL); |
| 141 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 142 | static DECLARE_TRANSPORT_CLASS(iscsi_connection_class, |
| 143 | "iscsi_connection", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | NULL, |
| 145 | NULL, |
| 146 | NULL); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 147 | |
| 148 | static struct sock *nls; |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 149 | static DEFINE_MUTEX(rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 150 | |
| 151 | struct mempool_zone { |
| 152 | mempool_t *pool; |
| 153 | atomic_t allocated; |
| 154 | int size; |
| 155 | int hiwat; |
| 156 | struct list_head freequeue; |
| 157 | spinlock_t freelock; |
| 158 | }; |
| 159 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 160 | static struct mempool_zone *z_reply; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 163 | * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time |
| 164 | * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM |
| 165 | * so daemon will notice OOM on NETLINK tranposrt level and will |
| 166 | * be able to predict or change operational behavior |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 168 | #define Z_MAX_REPLY 8 |
| 169 | #define Z_HIWAT_REPLY 6 |
| 170 | #define Z_MAX_PDU 8 |
| 171 | #define Z_HIWAT_PDU 6 |
| 172 | #define Z_MAX_ERROR 16 |
| 173 | #define Z_HIWAT_ERROR 12 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 175 | static LIST_HEAD(sesslist); |
| 176 | static DEFINE_SPINLOCK(sesslock); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 177 | static LIST_HEAD(connlist); |
| 178 | static DEFINE_SPINLOCK(connlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 180 | static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn) |
| 181 | { |
| 182 | struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent); |
| 183 | return sess->sid; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Returns the matching session to a given sid |
| 188 | */ |
| 189 | static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid) |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 190 | { |
| 191 | unsigned long flags; |
| 192 | struct iscsi_cls_session *sess; |
| 193 | |
| 194 | spin_lock_irqsave(&sesslock, flags); |
| 195 | list_for_each_entry(sess, &sesslist, sess_list) { |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 196 | if (sess->sid == sid) { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 197 | spin_unlock_irqrestore(&sesslock, flags); |
| 198 | return sess; |
| 199 | } |
| 200 | } |
| 201 | spin_unlock_irqrestore(&sesslock, flags); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 205 | /* |
| 206 | * Returns the matching connection to a given sid / cid tuple |
| 207 | */ |
| 208 | static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid) |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 209 | { |
| 210 | unsigned long flags; |
| 211 | struct iscsi_cls_conn *conn; |
| 212 | |
| 213 | spin_lock_irqsave(&connlock, flags); |
| 214 | list_for_each_entry(conn, &connlist, conn_list) { |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 215 | if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 216 | spin_unlock_irqrestore(&connlock, flags); |
| 217 | return conn; |
| 218 | } |
| 219 | } |
| 220 | spin_unlock_irqrestore(&connlock, flags); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 224 | /* |
| 225 | * The following functions can be used by LLDs that allocate |
| 226 | * their own scsi_hosts or by software iscsi LLDs |
| 227 | */ |
| 228 | static void iscsi_session_release(struct device *dev) |
| 229 | { |
| 230 | struct iscsi_cls_session *session = iscsi_dev_to_session(dev); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 231 | struct Scsi_Host *shost; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 233 | shost = iscsi_session_to_shost(session); |
| 234 | scsi_host_put(shost); |
| 235 | kfree(session); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 236 | } |
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 | static int iscsi_is_session_dev(const struct device *dev) |
| 239 | { |
| 240 | return dev->release == iscsi_session_release; |
| 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 243 | static int iscsi_user_scan(struct Scsi_Host *shost, uint channel, |
| 244 | uint id, uint lun) |
| 245 | { |
| 246 | struct iscsi_host *ihost = shost->shost_data; |
| 247 | struct iscsi_cls_session *session; |
| 248 | |
| 249 | mutex_lock(&ihost->mutex); |
| 250 | list_for_each_entry(session, &ihost->sessions, host_list) { |
Mike Christie | e6f3b63 | 2006-06-28 12:00:29 -0500 | [diff] [blame] | 251 | if ((channel == SCAN_WILD_CARD || channel == 0) && |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 252 | (id == SCAN_WILD_CARD || id == session->target_id)) |
Mike Christie | e6f3b63 | 2006-06-28 12:00:29 -0500 | [diff] [blame] | 253 | scsi_scan_target(&session->dev, 0, |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 254 | session->target_id, lun, 1); |
| 255 | } |
| 256 | mutex_unlock(&ihost->mutex); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static void session_recovery_timedout(void *data) |
| 262 | { |
| 263 | struct iscsi_cls_session *session = data; |
| 264 | |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame] | 265 | dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed " |
| 266 | "out after %d secs\n", session->recovery_tmo); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 267 | |
| 268 | if (session->transport->session_recovery_timedout) |
| 269 | session->transport->session_recovery_timedout(session); |
| 270 | |
| 271 | scsi_target_unblock(&session->dev); |
| 272 | } |
| 273 | |
| 274 | void iscsi_unblock_session(struct iscsi_cls_session *session) |
| 275 | { |
| 276 | if (!cancel_delayed_work(&session->recovery_work)) |
| 277 | flush_scheduled_work(); |
| 278 | scsi_target_unblock(&session->dev); |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(iscsi_unblock_session); |
| 281 | |
| 282 | void iscsi_block_session(struct iscsi_cls_session *session) |
| 283 | { |
| 284 | scsi_target_block(&session->dev); |
| 285 | schedule_delayed_work(&session->recovery_work, |
| 286 | session->recovery_tmo * HZ); |
| 287 | } |
| 288 | EXPORT_SYMBOL_GPL(iscsi_block_session); |
| 289 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 290 | struct iscsi_cls_session * |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 291 | iscsi_alloc_session(struct Scsi_Host *shost, |
| 292 | struct iscsi_transport *transport) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 293 | { |
| 294 | struct iscsi_cls_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 296 | session = kzalloc(sizeof(*session) + transport->sessiondata_size, |
| 297 | GFP_KERNEL); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 298 | if (!session) |
Mike Christie | f53a88d | 2006-06-28 12:00:27 -0500 | [diff] [blame] | 299 | return NULL; |
| 300 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 301 | session->transport = transport; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 302 | session->recovery_tmo = 120; |
| 303 | INIT_WORK(&session->recovery_work, session_recovery_timedout, session); |
| 304 | INIT_LIST_HEAD(&session->host_list); |
| 305 | INIT_LIST_HEAD(&session->sess_list); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 306 | |
Mike Christie | 6a8a0d3 | 2006-06-28 12:00:31 -0500 | [diff] [blame] | 307 | /* this is released in the dev's release function */ |
| 308 | scsi_host_get(shost); |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 309 | session->dev.parent = &shost->shost_gendev; |
| 310 | session->dev.release = iscsi_session_release; |
| 311 | device_initialize(&session->dev); |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 312 | if (transport->sessiondata_size) |
| 313 | session->dd_data = &session[1]; |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 314 | return session; |
| 315 | } |
| 316 | EXPORT_SYMBOL_GPL(iscsi_alloc_session); |
| 317 | |
Mike Christie | 6a8a0d3 | 2006-06-28 12:00:31 -0500 | [diff] [blame] | 318 | int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id) |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 319 | { |
| 320 | struct Scsi_Host *shost = iscsi_session_to_shost(session); |
| 321 | struct iscsi_host *ihost; |
| 322 | int err; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 323 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 324 | ihost = shost->shost_data; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 325 | session->sid = iscsi_session_nr++; |
Mike Christie | 6a8a0d3 | 2006-06-28 12:00:31 -0500 | [diff] [blame] | 326 | session->target_id = target_id; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 327 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 328 | snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", |
| 329 | session->sid); |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 330 | err = device_add(&session->dev); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 331 | if (err) { |
| 332 | dev_printk(KERN_ERR, &session->dev, "iscsi: could not " |
| 333 | "register session's dev\n"); |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 334 | goto release_host; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 335 | } |
| 336 | transport_register_device(&session->dev); |
| 337 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 338 | mutex_lock(&ihost->mutex); |
| 339 | list_add(&session->host_list, &ihost->sessions); |
| 340 | mutex_unlock(&ihost->mutex); |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 341 | return 0; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 342 | |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 343 | release_host: |
| 344 | scsi_host_put(shost); |
| 345 | return err; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 346 | } |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(iscsi_add_session); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 348 | |
| 349 | /** |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 350 | * iscsi_create_session - create iscsi class session |
| 351 | * @shost: scsi host |
| 352 | * @transport: iscsi transport |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 353 | * |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 354 | * This can be called from a LLD or iscsi_transport. |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 355 | **/ |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 356 | struct iscsi_cls_session * |
| 357 | iscsi_create_session(struct Scsi_Host *shost, |
Mike Christie | 6a8a0d3 | 2006-06-28 12:00:31 -0500 | [diff] [blame] | 358 | struct iscsi_transport *transport, |
| 359 | unsigned int target_id) |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 360 | { |
| 361 | struct iscsi_cls_session *session; |
| 362 | |
| 363 | session = iscsi_alloc_session(shost, transport); |
| 364 | if (!session) |
| 365 | return NULL; |
| 366 | |
Mike Christie | 6a8a0d3 | 2006-06-28 12:00:31 -0500 | [diff] [blame] | 367 | if (iscsi_add_session(session, target_id)) { |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 368 | iscsi_free_session(session); |
| 369 | return NULL; |
| 370 | } |
| 371 | return session; |
| 372 | } |
| 373 | EXPORT_SYMBOL_GPL(iscsi_create_session); |
| 374 | |
| 375 | void iscsi_remove_session(struct iscsi_cls_session *session) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 376 | { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 377 | struct Scsi_Host *shost = iscsi_session_to_shost(session); |
| 378 | struct iscsi_host *ihost = shost->shost_data; |
| 379 | |
| 380 | if (!cancel_delayed_work(&session->recovery_work)) |
| 381 | flush_scheduled_work(); |
| 382 | |
| 383 | mutex_lock(&ihost->mutex); |
| 384 | list_del(&session->host_list); |
| 385 | mutex_unlock(&ihost->mutex); |
| 386 | |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 387 | scsi_remove_target(&session->dev); |
| 388 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 389 | transport_unregister_device(&session->dev); |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 390 | device_del(&session->dev); |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(iscsi_remove_session); |
| 393 | |
| 394 | void iscsi_free_session(struct iscsi_cls_session *session) |
| 395 | { |
| 396 | put_device(&session->dev); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 397 | } |
| 398 | |
Mike Christie | 8434aa8 | 2006-06-28 12:00:30 -0500 | [diff] [blame] | 399 | EXPORT_SYMBOL_GPL(iscsi_free_session); |
| 400 | |
| 401 | /** |
| 402 | * iscsi_destroy_session - destroy iscsi session |
| 403 | * @session: iscsi_session |
| 404 | * |
| 405 | * Can be called by a LLD or iscsi_transport. There must not be |
| 406 | * any running connections. |
| 407 | **/ |
| 408 | int iscsi_destroy_session(struct iscsi_cls_session *session) |
| 409 | { |
| 410 | iscsi_remove_session(session); |
| 411 | iscsi_free_session(session); |
| 412 | return 0; |
| 413 | } |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(iscsi_destroy_session); |
| 415 | |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 416 | static void mempool_zone_destroy(struct mempool_zone *zp) |
| 417 | { |
| 418 | mempool_destroy(zp->pool); |
| 419 | kfree(zp); |
| 420 | } |
| 421 | |
| 422 | static void* |
| 423 | mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data) |
| 424 | { |
| 425 | struct mempool_zone *zone = pool_data; |
| 426 | |
| 427 | return alloc_skb(zone->size, gfp_mask); |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | mempool_zone_free_skb(void *element, void *pool_data) |
| 432 | { |
| 433 | kfree_skb(element); |
| 434 | } |
| 435 | |
| 436 | static struct mempool_zone * |
| 437 | mempool_zone_init(unsigned max, unsigned size, unsigned hiwat) |
| 438 | { |
| 439 | struct mempool_zone *zp; |
| 440 | |
| 441 | zp = kzalloc(sizeof(*zp), GFP_KERNEL); |
| 442 | if (!zp) |
| 443 | return NULL; |
| 444 | |
| 445 | zp->size = size; |
| 446 | zp->hiwat = hiwat; |
| 447 | INIT_LIST_HEAD(&zp->freequeue); |
| 448 | spin_lock_init(&zp->freelock); |
| 449 | atomic_set(&zp->allocated, 0); |
| 450 | |
| 451 | zp->pool = mempool_create(max, mempool_zone_alloc_skb, |
| 452 | mempool_zone_free_skb, zp); |
| 453 | if (!zp->pool) { |
| 454 | kfree(zp); |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | return zp; |
| 459 | } |
| 460 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 461 | static void iscsi_conn_release(struct device *dev) |
| 462 | { |
| 463 | struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev); |
| 464 | struct device *parent = conn->dev.parent; |
| 465 | |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 466 | mempool_zone_destroy(conn->z_pdu); |
| 467 | mempool_zone_destroy(conn->z_error); |
| 468 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 469 | kfree(conn); |
| 470 | put_device(parent); |
| 471 | } |
| 472 | |
| 473 | static int iscsi_is_conn_dev(const struct device *dev) |
| 474 | { |
| 475 | return dev->release == iscsi_conn_release; |
| 476 | } |
| 477 | |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 478 | static int iscsi_create_event_pools(struct iscsi_cls_conn *conn) |
| 479 | { |
| 480 | conn->z_pdu = mempool_zone_init(Z_MAX_PDU, |
| 481 | NLMSG_SPACE(sizeof(struct iscsi_uevent) + |
| 482 | sizeof(struct iscsi_hdr) + |
| 483 | DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH), |
| 484 | Z_HIWAT_PDU); |
| 485 | if (!conn->z_pdu) { |
| 486 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate " |
| 487 | "pdu zone for new conn\n"); |
| 488 | return -ENOMEM; |
| 489 | } |
| 490 | |
| 491 | conn->z_error = mempool_zone_init(Z_MAX_ERROR, |
| 492 | NLMSG_SPACE(sizeof(struct iscsi_uevent)), |
| 493 | Z_HIWAT_ERROR); |
| 494 | if (!conn->z_error) { |
| 495 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate " |
| 496 | "error zone for new conn\n"); |
| 497 | mempool_zone_destroy(conn->z_pdu); |
| 498 | return -ENOMEM; |
| 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 503 | /** |
| 504 | * iscsi_create_conn - create iscsi class connection |
| 505 | * @session: iscsi cls session |
| 506 | * @cid: connection id |
| 507 | * |
| 508 | * This can be called from a LLD or iscsi_transport. The connection |
| 509 | * is child of the session so cid must be unique for all connections |
| 510 | * on the session. |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 511 | * |
| 512 | * Since we do not support MCS, cid will normally be zero. In some cases |
| 513 | * for software iscsi we could be trying to preallocate a connection struct |
| 514 | * in which case there could be two connection structs and cid would be |
| 515 | * non-zero. |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 516 | **/ |
| 517 | struct iscsi_cls_conn * |
| 518 | iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid) |
| 519 | { |
| 520 | struct iscsi_transport *transport = session->transport; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 521 | struct iscsi_cls_conn *conn; |
| 522 | int err; |
| 523 | |
| 524 | conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL); |
| 525 | if (!conn) |
| 526 | return NULL; |
| 527 | |
| 528 | if (transport->conndata_size) |
| 529 | conn->dd_data = &conn[1]; |
| 530 | |
| 531 | INIT_LIST_HEAD(&conn->conn_list); |
| 532 | conn->transport = transport; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 533 | conn->cid = cid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 534 | |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 535 | if (iscsi_create_event_pools(conn)) |
| 536 | goto free_conn; |
| 537 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 538 | /* this is released in the dev's release function */ |
| 539 | if (!get_device(&session->dev)) |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 540 | goto free_conn_pools; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 541 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 542 | snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u", |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 543 | session->sid, cid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 544 | conn->dev.parent = &session->dev; |
| 545 | conn->dev.release = iscsi_conn_release; |
| 546 | err = device_register(&conn->dev); |
| 547 | if (err) { |
| 548 | dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register " |
| 549 | "connection's dev\n"); |
| 550 | goto release_parent_ref; |
| 551 | } |
| 552 | transport_register_device(&conn->dev); |
| 553 | return conn; |
| 554 | |
| 555 | release_parent_ref: |
| 556 | put_device(&session->dev); |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 557 | free_conn_pools: |
| 558 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 559 | free_conn: |
| 560 | kfree(conn); |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | EXPORT_SYMBOL_GPL(iscsi_create_conn); |
| 565 | |
| 566 | /** |
| 567 | * iscsi_destroy_conn - destroy iscsi class connection |
| 568 | * @session: iscsi cls session |
| 569 | * |
| 570 | * This can be called from a LLD or iscsi_transport. |
| 571 | **/ |
| 572 | int iscsi_destroy_conn(struct iscsi_cls_conn *conn) |
| 573 | { |
| 574 | transport_unregister_device(&conn->dev); |
| 575 | device_unregister(&conn->dev); |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | EXPORT_SYMBOL_GPL(iscsi_destroy_conn); |
| 580 | |
| 581 | /* |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 582 | * iscsi interface functions |
| 583 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 584 | static struct iscsi_internal * |
| 585 | iscsi_if_transport_lookup(struct iscsi_transport *tt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 587 | struct iscsi_internal *priv; |
| 588 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 590 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 591 | list_for_each_entry(priv, &iscsi_transports, list) { |
| 592 | if (tt == priv->iscsi_transport) { |
| 593 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 594 | return priv; |
| 595 | } |
| 596 | } |
| 597 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 598 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 601 | static inline struct list_head *skb_to_lh(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 603 | return (struct list_head *)&skb->cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 605 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 606 | static void |
| 607 | mempool_zone_complete(struct mempool_zone *zone) |
| 608 | { |
| 609 | unsigned long flags; |
| 610 | struct list_head *lh, *n; |
| 611 | |
| 612 | spin_lock_irqsave(&zone->freelock, flags); |
| 613 | list_for_each_safe(lh, n, &zone->freequeue) { |
| 614 | struct sk_buff *skb = (struct sk_buff *)((char *)lh - |
| 615 | offsetof(struct sk_buff, cb)); |
| 616 | if (!skb_shared(skb)) { |
| 617 | list_del(skb_to_lh(skb)); |
| 618 | mempool_free(skb, zone->pool); |
| 619 | atomic_dec(&zone->allocated); |
| 620 | } |
| 621 | } |
| 622 | spin_unlock_irqrestore(&zone->freelock, flags); |
| 623 | } |
| 624 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 625 | static struct sk_buff* |
| 626 | mempool_zone_get_skb(struct mempool_zone *zone) |
| 627 | { |
| 628 | struct sk_buff *skb; |
| 629 | |
| 630 | skb = mempool_alloc(zone->pool, GFP_ATOMIC); |
| 631 | if (skb) |
| 632 | atomic_inc(&zone->allocated); |
| 633 | return skb; |
| 634 | } |
| 635 | |
| 636 | static int |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 637 | iscsi_broadcast_skb(struct mempool_zone *zone, struct sk_buff *skb) |
| 638 | { |
| 639 | unsigned long flags; |
| 640 | int rc; |
| 641 | |
| 642 | skb_get(skb); |
| 643 | rc = netlink_broadcast(nls, skb, 0, 1, GFP_KERNEL); |
| 644 | if (rc < 0) { |
| 645 | mempool_free(skb, zone->pool); |
| 646 | printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc); |
| 647 | return rc; |
| 648 | } |
| 649 | |
| 650 | spin_lock_irqsave(&zone->freelock, flags); |
| 651 | INIT_LIST_HEAD(skb_to_lh(skb)); |
| 652 | list_add(skb_to_lh(skb), &zone->freequeue); |
| 653 | spin_unlock_irqrestore(&zone->freelock, flags); |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 658 | iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb, int pid) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 659 | { |
| 660 | unsigned long flags; |
| 661 | int rc; |
| 662 | |
| 663 | skb_get(skb); |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 664 | rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 665 | if (rc < 0) { |
| 666 | mempool_free(skb, zone->pool); |
| 667 | printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc); |
| 668 | return rc; |
| 669 | } |
| 670 | |
| 671 | spin_lock_irqsave(&zone->freelock, flags); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 672 | INIT_LIST_HEAD(skb_to_lh(skb)); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 673 | list_add(skb_to_lh(skb), &zone->freequeue); |
| 674 | spin_unlock_irqrestore(&zone->freelock, flags); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 679 | 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] | 680 | char *data, uint32_t data_size) |
| 681 | { |
| 682 | struct nlmsghdr *nlh; |
| 683 | struct sk_buff *skb; |
| 684 | struct iscsi_uevent *ev; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 685 | char *pdu; |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 686 | struct iscsi_internal *priv; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 687 | int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) + |
| 688 | data_size); |
| 689 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 690 | priv = iscsi_if_transport_lookup(conn->transport); |
| 691 | if (!priv) |
| 692 | return -EINVAL; |
| 693 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 694 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 695 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 696 | skb = mempool_zone_get_skb(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 697 | if (!skb) { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 698 | iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 699 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver " |
| 700 | "control PDU: OOM\n"); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 701 | return -ENOMEM; |
| 702 | } |
| 703 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 704 | nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 705 | ev = NLMSG_DATA(nlh); |
| 706 | memset(ev, 0, sizeof(*ev)); |
| 707 | ev->transport_handle = iscsi_handle(conn->transport); |
| 708 | ev->type = ISCSI_KEVENT_RECV_PDU; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 709 | if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 710 | ev->iferror = -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 711 | ev->r.recv_req.cid = conn->cid; |
| 712 | ev->r.recv_req.sid = iscsi_conn_get_sid(conn); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 713 | pdu = (char*)ev + sizeof(*ev); |
| 714 | memcpy(pdu, hdr, sizeof(struct iscsi_hdr)); |
| 715 | memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size); |
| 716 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 717 | return iscsi_unicast_skb(conn->z_pdu, skb, priv->daemon_pid); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 718 | } |
| 719 | EXPORT_SYMBOL_GPL(iscsi_recv_pdu); |
| 720 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 721 | 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] | 722 | { |
| 723 | struct nlmsghdr *nlh; |
| 724 | struct sk_buff *skb; |
| 725 | struct iscsi_uevent *ev; |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 726 | struct iscsi_internal *priv; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 727 | int len = NLMSG_SPACE(sizeof(*ev)); |
| 728 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 729 | priv = iscsi_if_transport_lookup(conn->transport); |
| 730 | if (!priv) |
| 731 | return; |
| 732 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 733 | mempool_zone_complete(conn->z_error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 734 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 735 | skb = mempool_zone_get_skb(conn->z_error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 736 | if (!skb) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 737 | dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored " |
| 738 | "conn error (%d)\n", error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 739 | return; |
| 740 | } |
| 741 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 742 | nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 743 | ev = NLMSG_DATA(nlh); |
| 744 | ev->transport_handle = iscsi_handle(conn->transport); |
| 745 | ev->type = ISCSI_KEVENT_CONN_ERROR; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 746 | if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 747 | ev->iferror = -ENOMEM; |
| 748 | ev->r.connerror.error = error; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 749 | ev->r.connerror.cid = conn->cid; |
| 750 | ev->r.connerror.sid = iscsi_conn_get_sid(conn); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 751 | |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 752 | iscsi_broadcast_skb(conn->z_error, skb); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 753 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 754 | dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n", |
| 755 | error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 756 | } |
| 757 | EXPORT_SYMBOL_GPL(iscsi_conn_error); |
| 758 | |
| 759 | static int |
| 760 | iscsi_if_send_reply(int pid, int seq, int type, int done, int multi, |
| 761 | void *payload, int size) |
| 762 | { |
| 763 | struct sk_buff *skb; |
| 764 | struct nlmsghdr *nlh; |
| 765 | int len = NLMSG_SPACE(size); |
| 766 | int flags = multi ? NLM_F_MULTI : 0; |
| 767 | int t = done ? NLMSG_DONE : type; |
| 768 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 769 | mempool_zone_complete(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 770 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 771 | skb = mempool_zone_get_skb(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 772 | /* |
| 773 | * FIXME: |
| 774 | * user is supposed to react on iferror == -ENOMEM; |
| 775 | * see iscsi_if_rx(). |
| 776 | */ |
| 777 | BUG_ON(!skb); |
| 778 | |
| 779 | nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0); |
| 780 | nlh->nlmsg_flags = flags; |
| 781 | memcpy(NLMSG_DATA(nlh), payload, size); |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 782 | return iscsi_unicast_skb(z_reply, skb, pid); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | static int |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 786 | iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 787 | { |
| 788 | struct iscsi_uevent *ev = NLMSG_DATA(nlh); |
| 789 | struct iscsi_stats *stats; |
| 790 | struct sk_buff *skbstat; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 791 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 792 | struct nlmsghdr *nlhstat; |
| 793 | struct iscsi_uevent *evstat; |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 794 | struct iscsi_internal *priv; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 795 | int len = NLMSG_SPACE(sizeof(*ev) + |
| 796 | sizeof(struct iscsi_stats) + |
| 797 | sizeof(struct iscsi_stats_custom) * |
| 798 | ISCSI_STATS_CUSTOM_MAX); |
| 799 | int err = 0; |
| 800 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 801 | priv = iscsi_if_transport_lookup(transport); |
| 802 | if (!priv) |
| 803 | return -EINVAL; |
| 804 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 805 | conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 806 | if (!conn) |
| 807 | return -EEXIST; |
| 808 | |
| 809 | do { |
| 810 | int actual_size; |
| 811 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 812 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 813 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 814 | skbstat = mempool_zone_get_skb(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 815 | if (!skbstat) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 816 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not " |
| 817 | "deliver stats: OOM\n"); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 818 | return -ENOMEM; |
| 819 | } |
| 820 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 821 | nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0, |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 822 | (len - sizeof(*nlhstat)), 0); |
| 823 | evstat = NLMSG_DATA(nlhstat); |
| 824 | memset(evstat, 0, sizeof(*evstat)); |
| 825 | evstat->transport_handle = iscsi_handle(conn->transport); |
| 826 | evstat->type = nlh->nlmsg_type; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 827 | if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 828 | evstat->iferror = -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 829 | evstat->u.get_stats.cid = |
| 830 | ev->u.get_stats.cid; |
| 831 | evstat->u.get_stats.sid = |
| 832 | ev->u.get_stats.sid; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 833 | stats = (struct iscsi_stats *) |
| 834 | ((char*)evstat + sizeof(*evstat)); |
| 835 | memset(stats, 0, sizeof(*stats)); |
| 836 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 837 | transport->get_stats(conn, stats); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 838 | actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) + |
| 839 | sizeof(struct iscsi_stats) + |
| 840 | sizeof(struct iscsi_stats_custom) * |
| 841 | stats->custom_length); |
| 842 | actual_size -= sizeof(*nlhstat); |
| 843 | actual_size = NLMSG_LENGTH(actual_size); |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 844 | skb_trim(skbstat, NLMSG_ALIGN(actual_size)); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 845 | nlhstat->nlmsg_len = actual_size; |
| 846 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 847 | err = iscsi_unicast_skb(conn->z_pdu, skbstat, priv->daemon_pid); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 848 | } while (err < 0 && err != -ECONNREFUSED); |
| 849 | |
| 850 | return err; |
| 851 | } |
| 852 | |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 853 | /** |
| 854 | * iscsi_if_destroy_session_done - send session destr. completion event |
| 855 | * @conn: last connection for session |
| 856 | * |
| 857 | * This is called by HW iscsi LLDs to notify userpsace that its HW has |
| 858 | * removed a session. |
| 859 | **/ |
| 860 | int iscsi_if_destroy_session_done(struct iscsi_cls_conn *conn) |
| 861 | { |
| 862 | struct iscsi_internal *priv; |
| 863 | struct iscsi_cls_session *session; |
| 864 | struct Scsi_Host *shost; |
| 865 | struct iscsi_uevent *ev; |
| 866 | struct sk_buff *skb; |
| 867 | struct nlmsghdr *nlh; |
| 868 | unsigned long flags; |
| 869 | int rc, len = NLMSG_SPACE(sizeof(*ev)); |
| 870 | |
| 871 | priv = iscsi_if_transport_lookup(conn->transport); |
| 872 | if (!priv) |
| 873 | return -EINVAL; |
| 874 | |
| 875 | session = iscsi_dev_to_session(conn->dev.parent); |
| 876 | shost = iscsi_session_to_shost(session); |
| 877 | |
| 878 | mempool_zone_complete(conn->z_pdu); |
| 879 | |
| 880 | skb = mempool_zone_get_skb(conn->z_pdu); |
| 881 | if (!skb) { |
| 882 | dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of " |
| 883 | "session creation event\n"); |
| 884 | return -ENOMEM; |
| 885 | } |
| 886 | |
| 887 | nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0); |
| 888 | ev = NLMSG_DATA(nlh); |
| 889 | ev->transport_handle = iscsi_handle(conn->transport); |
| 890 | ev->type = ISCSI_KEVENT_DESTROY_SESSION; |
| 891 | ev->r.d_session.host_no = shost->host_no; |
| 892 | ev->r.d_session.sid = session->sid; |
| 893 | |
| 894 | /* |
| 895 | * this will occur if the daemon is not up, so we just warn |
| 896 | * the user and when the daemon is restarted it will handle it |
| 897 | */ |
| 898 | rc = iscsi_broadcast_skb(conn->z_pdu, skb); |
| 899 | if (rc < 0) |
| 900 | dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of " |
| 901 | "session destruction event. Check iscsi daemon\n"); |
| 902 | |
| 903 | spin_lock_irqsave(&sesslock, flags); |
| 904 | list_del(&session->sess_list); |
| 905 | spin_unlock_irqrestore(&sesslock, flags); |
| 906 | |
| 907 | spin_lock_irqsave(&connlock, flags); |
| 908 | conn->active = 0; |
| 909 | list_del(&conn->conn_list); |
| 910 | spin_unlock_irqrestore(&connlock, flags); |
| 911 | |
| 912 | return rc; |
| 913 | } |
| 914 | EXPORT_SYMBOL_GPL(iscsi_if_destroy_session_done); |
| 915 | |
| 916 | /** |
| 917 | * iscsi_if_create_session_done - send session creation completion event |
| 918 | * @conn: leading connection for session |
| 919 | * |
| 920 | * This is called by HW iscsi LLDs to notify userpsace that its HW has |
| 921 | * created a session or a existing session is back in the logged in state. |
| 922 | **/ |
| 923 | int iscsi_if_create_session_done(struct iscsi_cls_conn *conn) |
| 924 | { |
| 925 | struct iscsi_internal *priv; |
| 926 | struct iscsi_cls_session *session; |
| 927 | struct Scsi_Host *shost; |
| 928 | struct iscsi_uevent *ev; |
| 929 | struct sk_buff *skb; |
| 930 | struct nlmsghdr *nlh; |
| 931 | unsigned long flags; |
| 932 | int rc, len = NLMSG_SPACE(sizeof(*ev)); |
| 933 | |
| 934 | priv = iscsi_if_transport_lookup(conn->transport); |
| 935 | if (!priv) |
| 936 | return -EINVAL; |
| 937 | |
| 938 | session = iscsi_dev_to_session(conn->dev.parent); |
| 939 | shost = iscsi_session_to_shost(session); |
| 940 | |
| 941 | mempool_zone_complete(conn->z_pdu); |
| 942 | |
| 943 | skb = mempool_zone_get_skb(conn->z_pdu); |
| 944 | if (!skb) { |
| 945 | dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of " |
| 946 | "session creation event\n"); |
| 947 | return -ENOMEM; |
| 948 | } |
| 949 | |
| 950 | nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0); |
| 951 | ev = NLMSG_DATA(nlh); |
| 952 | ev->transport_handle = iscsi_handle(conn->transport); |
| 953 | ev->type = ISCSI_UEVENT_CREATE_SESSION; |
| 954 | ev->r.c_session_ret.host_no = shost->host_no; |
| 955 | ev->r.c_session_ret.sid = session->sid; |
| 956 | |
| 957 | /* |
| 958 | * this will occur if the daemon is not up, so we just warn |
| 959 | * the user and when the daemon is restarted it will handle it |
| 960 | */ |
| 961 | rc = iscsi_broadcast_skb(conn->z_pdu, skb); |
| 962 | if (rc < 0) |
| 963 | dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of " |
| 964 | "session creation event. Check iscsi daemon\n"); |
| 965 | |
| 966 | spin_lock_irqsave(&sesslock, flags); |
| 967 | list_add(&session->sess_list, &sesslist); |
| 968 | spin_unlock_irqrestore(&sesslock, flags); |
| 969 | |
| 970 | spin_lock_irqsave(&connlock, flags); |
| 971 | list_add(&conn->conn_list, &connlist); |
| 972 | conn->active = 1; |
| 973 | spin_unlock_irqrestore(&connlock, flags); |
| 974 | return rc; |
| 975 | } |
| 976 | EXPORT_SYMBOL_GPL(iscsi_if_create_session_done); |
| 977 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 978 | static int |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 979 | iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev) |
| 980 | { |
| 981 | struct iscsi_transport *transport = priv->iscsi_transport; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 982 | struct iscsi_cls_session *session; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 983 | unsigned long flags; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 984 | uint32_t hostno; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 985 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 986 | session = transport->create_session(transport, &priv->t, |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 987 | ev->u.c_session.initial_cmdsn, |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 988 | &hostno); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 989 | if (!session) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 990 | return -ENOMEM; |
| 991 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 992 | spin_lock_irqsave(&sesslock, flags); |
| 993 | list_add(&session->sess_list, &sesslist); |
| 994 | spin_unlock_irqrestore(&sesslock, flags); |
| 995 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 996 | ev->r.c_session_ret.host_no = hostno; |
| 997 | ev->r.c_session_ret.sid = session->sid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | static int |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1002 | iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1003 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1004 | struct iscsi_cls_conn *conn; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1005 | struct iscsi_cls_session *session; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1006 | unsigned long flags; |
| 1007 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1008 | session = iscsi_session_lookup(ev->u.c_conn.sid); |
| 1009 | if (!session) { |
| 1010 | printk(KERN_ERR "iscsi: invalid session %d\n", |
| 1011 | ev->u.c_conn.sid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1012 | return -EINVAL; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1013 | } |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1014 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1015 | conn = transport->create_conn(session, ev->u.c_conn.cid); |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1016 | if (!conn) { |
| 1017 | printk(KERN_ERR "iscsi: couldn't create a new " |
| 1018 | "connection for session %d\n", |
| 1019 | session->sid); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1020 | return -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1021 | } |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1022 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1023 | ev->r.c_conn_ret.sid = session->sid; |
| 1024 | ev->r.c_conn_ret.cid = conn->cid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1025 | |
| 1026 | spin_lock_irqsave(&connlock, flags); |
| 1027 | list_add(&conn->conn_list, &connlist); |
| 1028 | conn->active = 1; |
| 1029 | spin_unlock_irqrestore(&connlock, flags); |
| 1030 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1031 | return 0; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static int |
| 1035 | iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
| 1036 | { |
| 1037 | unsigned long flags; |
| 1038 | struct iscsi_cls_conn *conn; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1039 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1040 | conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1041 | if (!conn) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1042 | return -EINVAL; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1043 | spin_lock_irqsave(&connlock, flags); |
| 1044 | conn->active = 0; |
| 1045 | list_del(&conn->conn_list); |
| 1046 | spin_unlock_irqrestore(&connlock, flags); |
| 1047 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1048 | if (transport->destroy_conn) |
| 1049 | transport->destroy_conn(conn); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
| 1052 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1053 | static int |
| 1054 | iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
| 1055 | { |
| 1056 | char *data = (char*)ev + sizeof(*ev); |
| 1057 | struct iscsi_cls_conn *conn; |
| 1058 | struct iscsi_cls_session *session; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1059 | int err = 0, value = 0; |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1060 | |
| 1061 | session = iscsi_session_lookup(ev->u.set_param.sid); |
| 1062 | conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid); |
| 1063 | if (!conn || !session) |
| 1064 | return -EINVAL; |
| 1065 | |
| 1066 | switch (ev->u.set_param.param) { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1067 | case ISCSI_PARAM_SESS_RECOVERY_TMO: |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1068 | sscanf(data, "%d", &value); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1069 | if (value != 0) |
| 1070 | session->recovery_tmo = value; |
| 1071 | break; |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1072 | default: |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1073 | err = transport->set_param(conn, ev->u.set_param.param, |
| 1074 | data, ev->u.set_param.len); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | return err; |
| 1078 | } |
| 1079 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1080 | static int |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 1081 | iscsi_if_transport_ep(struct iscsi_transport *transport, |
| 1082 | struct iscsi_uevent *ev, int msg_type) |
| 1083 | { |
| 1084 | struct sockaddr *dst_addr; |
| 1085 | int rc = 0; |
| 1086 | |
| 1087 | switch (msg_type) { |
| 1088 | case ISCSI_UEVENT_TRANSPORT_EP_CONNECT: |
| 1089 | if (!transport->ep_connect) |
| 1090 | return -EINVAL; |
| 1091 | |
| 1092 | dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev)); |
| 1093 | rc = transport->ep_connect(dst_addr, |
| 1094 | ev->u.ep_connect.non_blocking, |
| 1095 | &ev->r.ep_connect_ret.handle); |
| 1096 | break; |
| 1097 | case ISCSI_UEVENT_TRANSPORT_EP_POLL: |
| 1098 | if (!transport->ep_poll) |
| 1099 | return -EINVAL; |
| 1100 | |
| 1101 | ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle, |
| 1102 | ev->u.ep_poll.timeout_ms); |
| 1103 | break; |
| 1104 | case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT: |
| 1105 | if (!transport->ep_disconnect) |
| 1106 | return -EINVAL; |
| 1107 | |
| 1108 | transport->ep_disconnect(ev->u.ep_disconnect.ep_handle); |
| 1109 | break; |
| 1110 | } |
| 1111 | return rc; |
| 1112 | } |
| 1113 | |
| 1114 | static int |
Mike Christie | 01cb225 | 2006-06-28 12:00:22 -0500 | [diff] [blame] | 1115 | iscsi_tgt_dscvr(struct iscsi_transport *transport, |
| 1116 | struct iscsi_uevent *ev) |
| 1117 | { |
| 1118 | struct sockaddr *dst_addr; |
| 1119 | |
| 1120 | if (!transport->tgt_dscvr) |
| 1121 | return -EINVAL; |
| 1122 | |
| 1123 | dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev)); |
| 1124 | return transport->tgt_dscvr(ev->u.tgt_dscvr.type, |
| 1125 | ev->u.tgt_dscvr.host_no, |
| 1126 | ev->u.tgt_dscvr.enable, dst_addr); |
| 1127 | } |
| 1128 | |
| 1129 | static int |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1130 | iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 1131 | { |
| 1132 | int err = 0; |
| 1133 | struct iscsi_uevent *ev = NLMSG_DATA(nlh); |
| 1134 | struct iscsi_transport *transport = NULL; |
| 1135 | struct iscsi_internal *priv; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1136 | struct iscsi_cls_session *session; |
| 1137 | struct iscsi_cls_conn *conn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1138 | unsigned long flags; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1139 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1140 | priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle)); |
| 1141 | if (!priv) |
| 1142 | return -EINVAL; |
| 1143 | transport = priv->iscsi_transport; |
| 1144 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1145 | if (!try_module_get(transport->owner)) |
| 1146 | return -EINVAL; |
| 1147 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 1148 | priv->daemon_pid = NETLINK_CREDS(skb)->pid; |
| 1149 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1150 | switch (nlh->nlmsg_type) { |
| 1151 | case ISCSI_UEVENT_CREATE_SESSION: |
| 1152 | err = iscsi_if_create_session(priv, ev); |
| 1153 | break; |
| 1154 | case ISCSI_UEVENT_DESTROY_SESSION: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1155 | session = iscsi_session_lookup(ev->u.d_session.sid); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1156 | if (session) { |
| 1157 | spin_lock_irqsave(&sesslock, flags); |
| 1158 | list_del(&session->sess_list); |
| 1159 | spin_unlock_irqrestore(&sesslock, flags); |
| 1160 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1161 | transport->destroy_session(session); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1162 | } else |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1163 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1164 | break; |
| 1165 | case ISCSI_UEVENT_CREATE_CONN: |
| 1166 | err = iscsi_if_create_conn(transport, ev); |
| 1167 | break; |
| 1168 | case ISCSI_UEVENT_DESTROY_CONN: |
| 1169 | err = iscsi_if_destroy_conn(transport, ev); |
| 1170 | break; |
| 1171 | case ISCSI_UEVENT_BIND_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1172 | session = iscsi_session_lookup(ev->u.b_conn.sid); |
| 1173 | conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1174 | |
| 1175 | if (session && conn) |
| 1176 | ev->r.retcode = transport->bind_conn(session, conn, |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 1177 | ev->u.b_conn.transport_eph, |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1178 | ev->u.b_conn.is_leading); |
| 1179 | else |
| 1180 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1181 | break; |
| 1182 | case ISCSI_UEVENT_SET_PARAM: |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1183 | err = iscsi_set_param(transport, ev); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1184 | break; |
| 1185 | case ISCSI_UEVENT_START_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1186 | conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1187 | if (conn) |
| 1188 | ev->r.retcode = transport->start_conn(conn); |
| 1189 | else |
| 1190 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1191 | break; |
| 1192 | case ISCSI_UEVENT_STOP_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1193 | conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1194 | if (conn) |
| 1195 | transport->stop_conn(conn, ev->u.stop_conn.flag); |
| 1196 | else |
| 1197 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | case ISCSI_UEVENT_SEND_PDU: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1200 | conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1201 | if (conn) |
| 1202 | ev->r.retcode = transport->send_pdu(conn, |
| 1203 | (struct iscsi_hdr*)((char*)ev + sizeof(*ev)), |
| 1204 | (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size, |
| 1205 | ev->u.send_pdu.data_size); |
| 1206 | else |
| 1207 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1208 | break; |
| 1209 | case ISCSI_UEVENT_GET_STATS: |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 1210 | err = iscsi_if_get_stats(transport, nlh); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1211 | break; |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 1212 | case ISCSI_UEVENT_TRANSPORT_EP_CONNECT: |
| 1213 | case ISCSI_UEVENT_TRANSPORT_EP_POLL: |
| 1214 | case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT: |
| 1215 | err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type); |
| 1216 | break; |
Mike Christie | 01cb225 | 2006-06-28 12:00:22 -0500 | [diff] [blame] | 1217 | case ISCSI_UEVENT_TGT_DSCVR: |
| 1218 | err = iscsi_tgt_dscvr(transport, ev); |
| 1219 | break; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1220 | default: |
| 1221 | err = -EINVAL; |
| 1222 | break; |
| 1223 | } |
| 1224 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1225 | module_put(transport->owner); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1226 | return err; |
| 1227 | } |
| 1228 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1229 | /* |
| 1230 | * Get message from skb (based on rtnetlink_rcv_skb). Each message is |
| 1231 | * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or |
| 1232 | * invalid creds are discarded silently. |
| 1233 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1234 | static void |
| 1235 | iscsi_if_rx(struct sock *sk, int len) |
| 1236 | { |
| 1237 | struct sk_buff *skb; |
| 1238 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1239 | mutex_lock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1240 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1241 | if (NETLINK_CREDS(skb)->uid) { |
| 1242 | skb_pull(skb, skb->len); |
| 1243 | goto free_skb; |
| 1244 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1245 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1246 | while (skb->len >= NLMSG_SPACE(0)) { |
| 1247 | int err; |
| 1248 | uint32_t rlen; |
| 1249 | struct nlmsghdr *nlh; |
| 1250 | struct iscsi_uevent *ev; |
| 1251 | |
| 1252 | nlh = (struct nlmsghdr *)skb->data; |
| 1253 | if (nlh->nlmsg_len < sizeof(*nlh) || |
| 1254 | skb->len < nlh->nlmsg_len) { |
| 1255 | break; |
| 1256 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1257 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1258 | ev = NLMSG_DATA(nlh); |
| 1259 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1260 | if (rlen > skb->len) |
| 1261 | rlen = skb->len; |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1262 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1263 | err = iscsi_if_recv_msg(skb, nlh); |
| 1264 | if (err) { |
| 1265 | ev->type = ISCSI_KEVENT_IF_ERROR; |
| 1266 | ev->iferror = err; |
| 1267 | } |
| 1268 | do { |
| 1269 | /* |
| 1270 | * special case for GET_STATS: |
| 1271 | * on success - sending reply and stats from |
| 1272 | * inside of if_recv_msg(), |
| 1273 | * on error - fall through. |
| 1274 | */ |
| 1275 | if (ev->type == ISCSI_UEVENT_GET_STATS && !err) |
| 1276 | break; |
| 1277 | err = iscsi_if_send_reply( |
| 1278 | NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq, |
| 1279 | nlh->nlmsg_type, 0, 0, ev, sizeof(*ev)); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1280 | if (atomic_read(&z_reply->allocated) >= |
| 1281 | z_reply->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1282 | ev->iferror = -ENOMEM; |
| 1283 | } while (err < 0 && err != -ECONNREFUSED); |
| 1284 | skb_pull(skb, rlen); |
| 1285 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1286 | free_skb: |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1287 | kfree_skb(skb); |
| 1288 | } |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1289 | mutex_unlock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1292 | #define iscsi_cdev_to_conn(_cdev) \ |
| 1293 | iscsi_dev_to_conn(_cdev->dev) |
| 1294 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1295 | #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \ |
| 1296 | struct class_device_attribute class_device_attr_##_prefix##_##_name = \ |
| 1297 | __ATTR(_name,_mode,_show,_store) |
| 1298 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1299 | /* |
| 1300 | * iSCSI connection attrs |
| 1301 | */ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1302 | #define iscsi_conn_attr_show(param) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | static ssize_t \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1304 | show_conn_param_##param(struct class_device *cdev, char *buf) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1305 | { \ |
| 1306 | struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \ |
| 1307 | struct iscsi_transport *t = conn->transport; \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1308 | return t->get_conn_param(conn, param, buf); \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1309 | } |
| 1310 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1311 | #define iscsi_conn_attr(field, param) \ |
| 1312 | iscsi_conn_attr_show(param) \ |
| 1313 | static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1314 | NULL); |
| 1315 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1316 | iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH); |
| 1317 | iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH); |
| 1318 | iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN); |
| 1319 | iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN); |
| 1320 | iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN); |
| 1321 | iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN); |
| 1322 | iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT); |
| 1323 | iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT); |
| 1324 | iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN); |
| 1325 | iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS); |
| 1326 | iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1328 | #define iscsi_cdev_to_session(_cdev) \ |
| 1329 | iscsi_dev_to_session(_cdev->dev) |
| 1330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | /* |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1332 | * iSCSI session attrs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | */ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1334 | #define iscsi_session_attr_show(param) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | static ssize_t \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1336 | show_session_param_##param(struct class_device *cdev, char *buf) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1337 | { \ |
| 1338 | struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \ |
| 1339 | struct iscsi_transport *t = session->transport; \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1340 | return t->get_session_param(session, param, buf); \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1343 | #define iscsi_session_attr(field, param) \ |
| 1344 | iscsi_session_attr_show(param) \ |
| 1345 | static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1346 | NULL); |
| 1347 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1348 | iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME); |
| 1349 | iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN); |
| 1350 | iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T); |
| 1351 | iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN); |
| 1352 | iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST); |
| 1353 | iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST); |
| 1354 | iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN); |
| 1355 | iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN); |
| 1356 | iscsi_session_attr(erl, ISCSI_PARAM_ERL); |
| 1357 | iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1358 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1359 | #define iscsi_priv_session_attr_show(field, format) \ |
| 1360 | static ssize_t \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1361 | show_priv_session_##field(struct class_device *cdev, char *buf) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1362 | { \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1363 | struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1364 | return sprintf(buf, format"\n", session->field); \ |
| 1365 | } |
| 1366 | |
| 1367 | #define iscsi_priv_session_attr(field, format) \ |
| 1368 | iscsi_priv_session_attr_show(field, format) \ |
| 1369 | static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \ |
| 1370 | NULL) |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1371 | iscsi_priv_session_attr(recovery_tmo, "%d"); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1372 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1373 | #define SETUP_PRIV_SESSION_RD_ATTR(field) \ |
| 1374 | do { \ |
| 1375 | priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \ |
| 1376 | count++; \ |
| 1377 | } while (0) |
| 1378 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1379 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1380 | #define SETUP_SESSION_RD_ATTR(field, param_flag) \ |
| 1381 | do { \ |
| 1382 | if (tt->param_mask & param_flag) { \ |
| 1383 | priv->session_attrs[count] = &class_device_attr_sess_##field; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | count++; \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1385 | } \ |
| 1386 | } while (0) |
| 1387 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1388 | #define SETUP_CONN_RD_ATTR(field, param_flag) \ |
| 1389 | do { \ |
| 1390 | if (tt->param_mask & param_flag) { \ |
| 1391 | priv->conn_attrs[count] = &class_device_attr_conn_##field; \ |
| 1392 | count++; \ |
| 1393 | } \ |
| 1394 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1396 | static int iscsi_session_match(struct attribute_container *cont, |
| 1397 | struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1399 | struct iscsi_cls_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | struct Scsi_Host *shost; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1401 | struct iscsi_internal *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1403 | if (!iscsi_is_session_dev(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | return 0; |
| 1405 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1406 | session = iscsi_dev_to_session(dev); |
| 1407 | shost = iscsi_session_to_shost(session); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1408 | if (!shost->transportt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | return 0; |
| 1410 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1411 | priv = to_iscsi_internal(shost->transportt); |
| 1412 | if (priv->session_cont.ac.class != &iscsi_session_class.class) |
| 1413 | return 0; |
| 1414 | |
| 1415 | return &priv->session_cont.ac == cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1418 | static int iscsi_conn_match(struct attribute_container *cont, |
| 1419 | struct device *dev) |
| 1420 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1421 | struct iscsi_cls_session *session; |
| 1422 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1423 | struct Scsi_Host *shost; |
| 1424 | struct iscsi_internal *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1426 | if (!iscsi_is_conn_dev(dev)) |
| 1427 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1429 | conn = iscsi_dev_to_conn(dev); |
| 1430 | session = iscsi_dev_to_session(conn->dev.parent); |
| 1431 | shost = iscsi_session_to_shost(session); |
| 1432 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1433 | if (!shost->transportt) |
| 1434 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1436 | priv = to_iscsi_internal(shost->transportt); |
| 1437 | if (priv->conn_cont.ac.class != &iscsi_connection_class.class) |
| 1438 | return 0; |
| 1439 | |
| 1440 | return &priv->conn_cont.ac == cont; |
| 1441 | } |
| 1442 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1443 | static int iscsi_host_match(struct attribute_container *cont, |
| 1444 | struct device *dev) |
| 1445 | { |
| 1446 | struct Scsi_Host *shost; |
| 1447 | struct iscsi_internal *priv; |
| 1448 | |
| 1449 | if (!scsi_is_host_device(dev)) |
| 1450 | return 0; |
| 1451 | |
| 1452 | shost = dev_to_shost(dev); |
| 1453 | if (!shost->transportt || |
| 1454 | shost->transportt->host_attrs.ac.class != &iscsi_host_class.class) |
| 1455 | return 0; |
| 1456 | |
| 1457 | priv = to_iscsi_internal(shost->transportt); |
| 1458 | return &priv->t.host_attrs.ac == cont; |
| 1459 | } |
| 1460 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1461 | struct scsi_transport_template * |
| 1462 | iscsi_register_transport(struct iscsi_transport *tt) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1463 | { |
| 1464 | struct iscsi_internal *priv; |
| 1465 | unsigned long flags; |
| 1466 | int count = 0, err; |
| 1467 | |
| 1468 | BUG_ON(!tt); |
| 1469 | |
| 1470 | priv = iscsi_if_transport_lookup(tt); |
| 1471 | if (priv) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1472 | return NULL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1473 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 1474 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1475 | if (!priv) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1476 | return NULL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1477 | INIT_LIST_HEAD(&priv->list); |
Mike Christie | 53cb8a1 | 2006-06-28 12:00:32 -0500 | [diff] [blame^] | 1478 | priv->daemon_pid = -1; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1479 | priv->iscsi_transport = tt; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1480 | priv->t.user_scan = iscsi_user_scan; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1481 | |
| 1482 | priv->cdev.class = &iscsi_transport_class; |
| 1483 | snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name); |
| 1484 | err = class_device_register(&priv->cdev); |
| 1485 | if (err) |
| 1486 | goto free_priv; |
| 1487 | |
| 1488 | err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group); |
| 1489 | if (err) |
| 1490 | goto unregister_cdev; |
| 1491 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1492 | /* host parameters */ |
| 1493 | priv->t.host_attrs.ac.attrs = &priv->host_attrs[0]; |
| 1494 | priv->t.host_attrs.ac.class = &iscsi_host_class.class; |
| 1495 | priv->t.host_attrs.ac.match = iscsi_host_match; |
| 1496 | priv->t.host_size = sizeof(struct iscsi_host); |
| 1497 | priv->host_attrs[0] = NULL; |
| 1498 | transport_container_register(&priv->t.host_attrs); |
| 1499 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1500 | /* connection parameters */ |
| 1501 | priv->conn_cont.ac.attrs = &priv->conn_attrs[0]; |
| 1502 | priv->conn_cont.ac.class = &iscsi_connection_class.class; |
| 1503 | priv->conn_cont.ac.match = iscsi_conn_match; |
| 1504 | transport_container_register(&priv->conn_cont); |
| 1505 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1506 | SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH); |
| 1507 | SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH); |
| 1508 | SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN); |
| 1509 | SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN); |
| 1510 | SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN); |
| 1511 | SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN); |
| 1512 | SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS); |
| 1513 | SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT); |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 1514 | SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN); |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1515 | SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS); |
| 1516 | SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1517 | |
| 1518 | BUG_ON(count > ISCSI_CONN_ATTRS); |
| 1519 | priv->conn_attrs[count] = NULL; |
| 1520 | count = 0; |
| 1521 | |
| 1522 | /* session parameters */ |
| 1523 | priv->session_cont.ac.attrs = &priv->session_attrs[0]; |
| 1524 | priv->session_cont.ac.class = &iscsi_session_class.class; |
| 1525 | priv->session_cont.ac.match = iscsi_session_match; |
| 1526 | transport_container_register(&priv->session_cont); |
| 1527 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1528 | SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN); |
| 1529 | SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T); |
| 1530 | SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN); |
| 1531 | SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST); |
| 1532 | SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST); |
| 1533 | SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN); |
| 1534 | SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN); |
| 1535 | SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL); |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1536 | SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME); |
| 1537 | SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1538 | SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | BUG_ON(count > ISCSI_SESSION_ATTRS); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1541 | priv->session_attrs[count] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1543 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 1544 | list_add(&priv->list, &iscsi_transports); |
| 1545 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1547 | printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1548 | return &priv->t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1550 | unregister_cdev: |
| 1551 | class_device_unregister(&priv->cdev); |
| 1552 | free_priv: |
| 1553 | kfree(priv); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1554 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1556 | EXPORT_SYMBOL_GPL(iscsi_register_transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1558 | int iscsi_unregister_transport(struct iscsi_transport *tt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1560 | struct iscsi_internal *priv; |
| 1561 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1563 | BUG_ON(!tt); |
| 1564 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1565 | mutex_lock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1566 | |
| 1567 | priv = iscsi_if_transport_lookup(tt); |
| 1568 | BUG_ON (!priv); |
| 1569 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1570 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 1571 | list_del(&priv->list); |
| 1572 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 1573 | |
| 1574 | transport_container_unregister(&priv->conn_cont); |
| 1575 | transport_container_unregister(&priv->session_cont); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1576 | transport_container_unregister(&priv->t.host_attrs); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1577 | |
| 1578 | sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group); |
| 1579 | class_device_unregister(&priv->cdev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1580 | mutex_unlock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1581 | |
| 1582 | return 0; |
| 1583 | } |
| 1584 | EXPORT_SYMBOL_GPL(iscsi_unregister_transport); |
| 1585 | |
| 1586 | static int |
| 1587 | iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1588 | { |
| 1589 | struct netlink_notify *n = ptr; |
| 1590 | |
| 1591 | if (event == NETLINK_URELEASE && |
| 1592 | n->protocol == NETLINK_ISCSI && n->pid) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1593 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1594 | unsigned long flags; |
| 1595 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1596 | mempool_zone_complete(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1597 | spin_lock_irqsave(&connlock, flags); |
| 1598 | list_for_each_entry(conn, &connlist, conn_list) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1599 | mempool_zone_complete(conn->z_error); |
| 1600 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1601 | } |
| 1602 | spin_unlock_irqrestore(&connlock, flags); |
| 1603 | } |
| 1604 | |
| 1605 | return NOTIFY_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1608 | static struct notifier_block iscsi_nl_notifier = { |
| 1609 | .notifier_call = iscsi_rcv_nl_event, |
| 1610 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
| 1612 | static __init int iscsi_transport_init(void) |
| 1613 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1614 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1616 | err = class_register(&iscsi_transport_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | if (err) |
| 1618 | return err; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1619 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1620 | err = transport_class_register(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1621 | if (err) |
| 1622 | goto unregister_transport_class; |
| 1623 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1624 | err = transport_class_register(&iscsi_connection_class); |
| 1625 | if (err) |
| 1626 | goto unregister_host_class; |
| 1627 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1628 | err = transport_class_register(&iscsi_session_class); |
| 1629 | if (err) |
| 1630 | goto unregister_conn_class; |
| 1631 | |
| 1632 | err = netlink_register_notifier(&iscsi_nl_notifier); |
| 1633 | if (err) |
| 1634 | goto unregister_session_class; |
| 1635 | |
James Bottomley | 2290d2b | 2005-09-08 10:14:11 -0500 | [diff] [blame] | 1636 | nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx, |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1637 | THIS_MODULE); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1638 | if (!nls) { |
| 1639 | err = -ENOBUFS; |
| 1640 | goto unregister_notifier; |
| 1641 | } |
| 1642 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1643 | z_reply = mempool_zone_init(Z_MAX_REPLY, |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1644 | NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1645 | if (z_reply) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1646 | return 0; |
| 1647 | |
| 1648 | sock_release(nls->sk_socket); |
| 1649 | unregister_notifier: |
| 1650 | netlink_unregister_notifier(&iscsi_nl_notifier); |
| 1651 | unregister_session_class: |
| 1652 | transport_class_unregister(&iscsi_session_class); |
| 1653 | unregister_conn_class: |
| 1654 | transport_class_unregister(&iscsi_connection_class); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1655 | unregister_host_class: |
| 1656 | transport_class_unregister(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1657 | unregister_transport_class: |
| 1658 | class_unregister(&iscsi_transport_class); |
| 1659 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | static void __exit iscsi_transport_exit(void) |
| 1663 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1664 | mempool_zone_destroy(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1665 | sock_release(nls->sk_socket); |
| 1666 | netlink_unregister_notifier(&iscsi_nl_notifier); |
| 1667 | transport_class_unregister(&iscsi_connection_class); |
| 1668 | transport_class_unregister(&iscsi_session_class); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1669 | transport_class_unregister(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1670 | class_unregister(&iscsi_transport_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | module_init(iscsi_transport_init); |
| 1674 | module_exit(iscsi_transport_exit); |
| 1675 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1676 | MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, " |
| 1677 | "Dmitry Yusupov <dmitry_yus@yahoo.com>, " |
| 1678 | "Alex Aizman <itn780@yahoo.com>"); |
| 1679 | MODULE_DESCRIPTION("iSCSI Transport Interface"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | MODULE_LICENSE("GPL"); |