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