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 | /** |
| 291 | * iscsi_create_session - create iscsi class session |
| 292 | * @shost: scsi host |
| 293 | * @transport: iscsi transport |
| 294 | * |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 295 | * This can be called from a LLD or iscsi_transport. |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 296 | **/ |
| 297 | struct iscsi_cls_session * |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 298 | iscsi_create_session(struct Scsi_Host *shost, |
Mike Christie | e6f3b63 | 2006-06-28 12:00:29 -0500 | [diff] [blame^] | 299 | struct iscsi_transport *transport) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 300 | { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 301 | struct iscsi_host *ihost; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 302 | struct iscsi_cls_session *session; |
| 303 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 305 | session = kzalloc(sizeof(*session) + transport->sessiondata_size, |
| 306 | GFP_KERNEL); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 307 | if (!session) |
Mike Christie | f53a88d | 2006-06-28 12:00:27 -0500 | [diff] [blame] | 308 | return NULL; |
| 309 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 310 | session->transport = transport; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 311 | session->recovery_tmo = 120; |
| 312 | INIT_WORK(&session->recovery_work, session_recovery_timedout, session); |
| 313 | INIT_LIST_HEAD(&session->host_list); |
| 314 | INIT_LIST_HEAD(&session->sess_list); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 315 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 316 | if (transport->sessiondata_size) |
| 317 | session->dd_data = &session[1]; |
| 318 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 319 | /* this is released in the dev's release function */ |
| 320 | scsi_host_get(shost); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 321 | ihost = shost->shost_data; |
| 322 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 323 | session->sid = iscsi_session_nr++; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 324 | session->target_id = ihost->next_target_id++; |
| 325 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 326 | snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", |
| 327 | session->sid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 328 | session->dev.parent = &shost->shost_gendev; |
| 329 | session->dev.release = iscsi_session_release; |
| 330 | err = device_register(&session->dev); |
| 331 | if (err) { |
| 332 | dev_printk(KERN_ERR, &session->dev, "iscsi: could not " |
| 333 | "register session's dev\n"); |
| 334 | goto free_session; |
| 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); |
| 341 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 342 | return session; |
| 343 | |
| 344 | free_session: |
| 345 | kfree(session); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | EXPORT_SYMBOL_GPL(iscsi_create_session); |
| 350 | |
| 351 | /** |
| 352 | * iscsi_destroy_session - destroy iscsi session |
| 353 | * @session: iscsi_session |
| 354 | * |
| 355 | * Can be called by a LLD or iscsi_transport. There must not be |
| 356 | * any running connections. |
| 357 | **/ |
| 358 | int iscsi_destroy_session(struct iscsi_cls_session *session) |
| 359 | { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 360 | struct Scsi_Host *shost = iscsi_session_to_shost(session); |
| 361 | struct iscsi_host *ihost = shost->shost_data; |
| 362 | |
| 363 | if (!cancel_delayed_work(&session->recovery_work)) |
| 364 | flush_scheduled_work(); |
| 365 | |
| 366 | mutex_lock(&ihost->mutex); |
| 367 | list_del(&session->host_list); |
| 368 | mutex_unlock(&ihost->mutex); |
| 369 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 370 | transport_unregister_device(&session->dev); |
| 371 | device_unregister(&session->dev); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | EXPORT_SYMBOL_GPL(iscsi_destroy_session); |
| 376 | |
| 377 | static void iscsi_conn_release(struct device *dev) |
| 378 | { |
| 379 | struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev); |
| 380 | struct device *parent = conn->dev.parent; |
| 381 | |
| 382 | kfree(conn); |
| 383 | put_device(parent); |
| 384 | } |
| 385 | |
| 386 | static int iscsi_is_conn_dev(const struct device *dev) |
| 387 | { |
| 388 | return dev->release == iscsi_conn_release; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * iscsi_create_conn - create iscsi class connection |
| 393 | * @session: iscsi cls session |
| 394 | * @cid: connection id |
| 395 | * |
| 396 | * This can be called from a LLD or iscsi_transport. The connection |
| 397 | * is child of the session so cid must be unique for all connections |
| 398 | * on the session. |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 399 | * |
| 400 | * Since we do not support MCS, cid will normally be zero. In some cases |
| 401 | * for software iscsi we could be trying to preallocate a connection struct |
| 402 | * in which case there could be two connection structs and cid would be |
| 403 | * non-zero. |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 404 | **/ |
| 405 | struct iscsi_cls_conn * |
| 406 | iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid) |
| 407 | { |
| 408 | struct iscsi_transport *transport = session->transport; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 409 | struct iscsi_cls_conn *conn; |
| 410 | int err; |
| 411 | |
| 412 | conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL); |
| 413 | if (!conn) |
| 414 | return NULL; |
| 415 | |
| 416 | if (transport->conndata_size) |
| 417 | conn->dd_data = &conn[1]; |
| 418 | |
| 419 | INIT_LIST_HEAD(&conn->conn_list); |
| 420 | conn->transport = transport; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 421 | conn->cid = cid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 422 | |
| 423 | /* this is released in the dev's release function */ |
| 424 | if (!get_device(&session->dev)) |
| 425 | goto free_conn; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 426 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 427 | snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u", |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 428 | session->sid, cid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 429 | conn->dev.parent = &session->dev; |
| 430 | conn->dev.release = iscsi_conn_release; |
| 431 | err = device_register(&conn->dev); |
| 432 | if (err) { |
| 433 | dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register " |
| 434 | "connection's dev\n"); |
| 435 | goto release_parent_ref; |
| 436 | } |
| 437 | transport_register_device(&conn->dev); |
| 438 | return conn; |
| 439 | |
| 440 | release_parent_ref: |
| 441 | put_device(&session->dev); |
| 442 | free_conn: |
| 443 | kfree(conn); |
| 444 | return NULL; |
| 445 | } |
| 446 | |
| 447 | EXPORT_SYMBOL_GPL(iscsi_create_conn); |
| 448 | |
| 449 | /** |
| 450 | * iscsi_destroy_conn - destroy iscsi class connection |
| 451 | * @session: iscsi cls session |
| 452 | * |
| 453 | * This can be called from a LLD or iscsi_transport. |
| 454 | **/ |
| 455 | int iscsi_destroy_conn(struct iscsi_cls_conn *conn) |
| 456 | { |
| 457 | transport_unregister_device(&conn->dev); |
| 458 | device_unregister(&conn->dev); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | EXPORT_SYMBOL_GPL(iscsi_destroy_conn); |
| 463 | |
| 464 | /* |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 465 | * iscsi interface functions |
| 466 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 467 | static struct iscsi_internal * |
| 468 | iscsi_if_transport_lookup(struct iscsi_transport *tt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 470 | struct iscsi_internal *priv; |
| 471 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 473 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 474 | list_for_each_entry(priv, &iscsi_transports, list) { |
| 475 | if (tt == priv->iscsi_transport) { |
| 476 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 477 | return priv; |
| 478 | } |
| 479 | } |
| 480 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 481 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 484 | static inline struct list_head *skb_to_lh(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 486 | return (struct list_head *)&skb->cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 488 | |
| 489 | static void* |
Mike Christie | 28e5554 | 2006-02-01 21:07:11 -0600 | [diff] [blame] | 490 | mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 491 | { |
| 492 | struct mempool_zone *zone = pool_data; |
| 493 | |
| 494 | return alloc_skb(zone->size, gfp_mask); |
| 495 | } |
| 496 | |
| 497 | static void |
| 498 | mempool_zone_free_skb(void *element, void *pool_data) |
| 499 | { |
| 500 | kfree_skb(element); |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | mempool_zone_complete(struct mempool_zone *zone) |
| 505 | { |
| 506 | unsigned long flags; |
| 507 | struct list_head *lh, *n; |
| 508 | |
| 509 | spin_lock_irqsave(&zone->freelock, flags); |
| 510 | list_for_each_safe(lh, n, &zone->freequeue) { |
| 511 | struct sk_buff *skb = (struct sk_buff *)((char *)lh - |
| 512 | offsetof(struct sk_buff, cb)); |
| 513 | if (!skb_shared(skb)) { |
| 514 | list_del(skb_to_lh(skb)); |
| 515 | mempool_free(skb, zone->pool); |
| 516 | atomic_dec(&zone->allocated); |
| 517 | } |
| 518 | } |
| 519 | spin_unlock_irqrestore(&zone->freelock, flags); |
| 520 | } |
| 521 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 522 | static struct mempool_zone * |
| 523 | mempool_zone_init(unsigned max, unsigned size, unsigned hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 524 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 525 | struct mempool_zone *zp; |
| 526 | |
| 527 | zp = kzalloc(sizeof(*zp), GFP_KERNEL); |
| 528 | if (!zp) |
| 529 | return NULL; |
| 530 | |
Mike Christie | 142e301 | 2006-02-01 21:06:58 -0600 | [diff] [blame] | 531 | zp->size = size; |
| 532 | zp->hiwat = hiwat; |
| 533 | INIT_LIST_HEAD(&zp->freequeue); |
| 534 | spin_lock_init(&zp->freelock); |
| 535 | atomic_set(&zp->allocated, 0); |
| 536 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 537 | zp->pool = mempool_create(max, mempool_zone_alloc_skb, |
| 538 | mempool_zone_free_skb, zp); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 539 | if (!zp->pool) { |
| 540 | kfree(zp); |
| 541 | return NULL; |
| 542 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 543 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 544 | return zp; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 547 | static void mempool_zone_destroy(struct mempool_zone *zp) |
| 548 | { |
| 549 | mempool_destroy(zp->pool); |
| 550 | kfree(zp); |
| 551 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 552 | |
| 553 | static struct sk_buff* |
| 554 | mempool_zone_get_skb(struct mempool_zone *zone) |
| 555 | { |
| 556 | struct sk_buff *skb; |
| 557 | |
| 558 | skb = mempool_alloc(zone->pool, GFP_ATOMIC); |
| 559 | if (skb) |
| 560 | atomic_inc(&zone->allocated); |
| 561 | return skb; |
| 562 | } |
| 563 | |
| 564 | static int |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 565 | 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] | 566 | { |
| 567 | unsigned long flags; |
| 568 | int rc; |
| 569 | |
| 570 | skb_get(skb); |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 571 | rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 572 | if (rc < 0) { |
| 573 | mempool_free(skb, zone->pool); |
| 574 | printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc); |
| 575 | return rc; |
| 576 | } |
| 577 | |
| 578 | spin_lock_irqsave(&zone->freelock, flags); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 579 | INIT_LIST_HEAD(skb_to_lh(skb)); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 580 | list_add(skb_to_lh(skb), &zone->freequeue); |
| 581 | spin_unlock_irqrestore(&zone->freelock, flags); |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 586 | 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] | 587 | char *data, uint32_t data_size) |
| 588 | { |
| 589 | struct nlmsghdr *nlh; |
| 590 | struct sk_buff *skb; |
| 591 | struct iscsi_uevent *ev; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 592 | char *pdu; |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 593 | struct iscsi_internal *priv; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 594 | int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) + |
| 595 | data_size); |
| 596 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 597 | priv = iscsi_if_transport_lookup(conn->transport); |
| 598 | if (!priv) |
| 599 | return -EINVAL; |
| 600 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 601 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 602 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 603 | skb = mempool_zone_get_skb(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 604 | if (!skb) { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 605 | iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 606 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver " |
| 607 | "control PDU: OOM\n"); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 608 | return -ENOMEM; |
| 609 | } |
| 610 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 611 | 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] | 612 | ev = NLMSG_DATA(nlh); |
| 613 | memset(ev, 0, sizeof(*ev)); |
| 614 | ev->transport_handle = iscsi_handle(conn->transport); |
| 615 | ev->type = ISCSI_KEVENT_RECV_PDU; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 616 | if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 617 | ev->iferror = -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 618 | ev->r.recv_req.cid = conn->cid; |
| 619 | ev->r.recv_req.sid = iscsi_conn_get_sid(conn); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 620 | pdu = (char*)ev + sizeof(*ev); |
| 621 | memcpy(pdu, hdr, sizeof(struct iscsi_hdr)); |
| 622 | memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size); |
| 623 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 624 | return iscsi_unicast_skb(conn->z_pdu, skb, priv->daemon_pid); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 625 | } |
| 626 | EXPORT_SYMBOL_GPL(iscsi_recv_pdu); |
| 627 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 628 | 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] | 629 | { |
| 630 | struct nlmsghdr *nlh; |
| 631 | struct sk_buff *skb; |
| 632 | struct iscsi_uevent *ev; |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 633 | struct iscsi_internal *priv; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 634 | int len = NLMSG_SPACE(sizeof(*ev)); |
| 635 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 636 | priv = iscsi_if_transport_lookup(conn->transport); |
| 637 | if (!priv) |
| 638 | return; |
| 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 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 649 | 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] | 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 | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 659 | iscsi_unicast_skb(conn->z_error, skb, priv->daemon_pid); |
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 | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 689 | return iscsi_unicast_skb(z_reply, skb, pid); |
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; |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 701 | struct iscsi_internal *priv; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 702 | int len = NLMSG_SPACE(sizeof(*ev) + |
| 703 | sizeof(struct iscsi_stats) + |
| 704 | sizeof(struct iscsi_stats_custom) * |
| 705 | ISCSI_STATS_CUSTOM_MAX); |
| 706 | int err = 0; |
| 707 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 708 | priv = iscsi_if_transport_lookup(transport); |
| 709 | if (!priv) |
| 710 | return -EINVAL; |
| 711 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 712 | 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] | 713 | if (!conn) |
| 714 | return -EEXIST; |
| 715 | |
| 716 | do { |
| 717 | int actual_size; |
| 718 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 719 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 720 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 721 | skbstat = mempool_zone_get_skb(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 722 | if (!skbstat) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 723 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not " |
| 724 | "deliver stats: OOM\n"); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 725 | return -ENOMEM; |
| 726 | } |
| 727 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 728 | nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0, |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 729 | (len - sizeof(*nlhstat)), 0); |
| 730 | evstat = NLMSG_DATA(nlhstat); |
| 731 | memset(evstat, 0, sizeof(*evstat)); |
| 732 | evstat->transport_handle = iscsi_handle(conn->transport); |
| 733 | evstat->type = nlh->nlmsg_type; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 734 | if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 735 | evstat->iferror = -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 736 | evstat->u.get_stats.cid = |
| 737 | ev->u.get_stats.cid; |
| 738 | evstat->u.get_stats.sid = |
| 739 | ev->u.get_stats.sid; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 740 | stats = (struct iscsi_stats *) |
| 741 | ((char*)evstat + sizeof(*evstat)); |
| 742 | memset(stats, 0, sizeof(*stats)); |
| 743 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 744 | transport->get_stats(conn, stats); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 745 | actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) + |
| 746 | sizeof(struct iscsi_stats) + |
| 747 | sizeof(struct iscsi_stats_custom) * |
| 748 | stats->custom_length); |
| 749 | actual_size -= sizeof(*nlhstat); |
| 750 | actual_size = NLMSG_LENGTH(actual_size); |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 751 | skb_trim(skbstat, NLMSG_ALIGN(actual_size)); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 752 | nlhstat->nlmsg_len = actual_size; |
| 753 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 754 | err = iscsi_unicast_skb(conn->z_pdu, skbstat, priv->daemon_pid); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 755 | } while (err < 0 && err != -ECONNREFUSED); |
| 756 | |
| 757 | return err; |
| 758 | } |
| 759 | |
| 760 | static int |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 761 | iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev) |
| 762 | { |
| 763 | struct iscsi_transport *transport = priv->iscsi_transport; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 764 | struct iscsi_cls_session *session; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 765 | unsigned long flags; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 766 | uint32_t hostno; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 767 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 768 | session = transport->create_session(transport, &priv->t, |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 769 | ev->u.c_session.initial_cmdsn, |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 770 | &hostno); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 771 | if (!session) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 772 | return -ENOMEM; |
| 773 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 774 | spin_lock_irqsave(&sesslock, flags); |
| 775 | list_add(&session->sess_list, &sesslist); |
| 776 | spin_unlock_irqrestore(&sesslock, flags); |
| 777 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 778 | ev->r.c_session_ret.host_no = hostno; |
| 779 | ev->r.c_session_ret.sid = session->sid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | static int |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 784 | iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 785 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 786 | struct iscsi_cls_conn *conn; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 787 | struct iscsi_cls_session *session; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 788 | unsigned long flags; |
| 789 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 790 | session = iscsi_session_lookup(ev->u.c_conn.sid); |
| 791 | if (!session) { |
| 792 | printk(KERN_ERR "iscsi: invalid session %d\n", |
| 793 | ev->u.c_conn.sid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 794 | return -EINVAL; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 795 | } |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 796 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 797 | conn = transport->create_conn(session, ev->u.c_conn.cid); |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 798 | if (!conn) { |
| 799 | printk(KERN_ERR "iscsi: couldn't create a new " |
| 800 | "connection for session %d\n", |
| 801 | session->sid); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 802 | return -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 803 | } |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 804 | |
| 805 | conn->z_pdu = mempool_zone_init(Z_MAX_PDU, |
| 806 | NLMSG_SPACE(sizeof(struct iscsi_uevent) + |
| 807 | sizeof(struct iscsi_hdr) + |
| 808 | DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH), |
| 809 | Z_HIWAT_PDU); |
| 810 | if (!conn->z_pdu) { |
| 811 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate " |
| 812 | "pdu zone for new conn\n"); |
| 813 | goto destroy_conn; |
| 814 | } |
| 815 | |
| 816 | conn->z_error = mempool_zone_init(Z_MAX_ERROR, |
| 817 | NLMSG_SPACE(sizeof(struct iscsi_uevent)), |
| 818 | Z_HIWAT_ERROR); |
| 819 | if (!conn->z_error) { |
| 820 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate " |
| 821 | "error zone for new conn\n"); |
| 822 | goto free_pdu_pool; |
| 823 | } |
| 824 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 825 | ev->r.c_conn_ret.sid = session->sid; |
| 826 | ev->r.c_conn_ret.cid = conn->cid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 827 | |
| 828 | spin_lock_irqsave(&connlock, flags); |
| 829 | list_add(&conn->conn_list, &connlist); |
| 830 | conn->active = 1; |
| 831 | spin_unlock_irqrestore(&connlock, flags); |
| 832 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 833 | return 0; |
| 834 | |
| 835 | free_pdu_pool: |
| 836 | mempool_zone_destroy(conn->z_pdu); |
| 837 | destroy_conn: |
| 838 | if (transport->destroy_conn) |
| 839 | transport->destroy_conn(conn->dd_data); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 840 | return -ENOMEM; |
| 841 | } |
| 842 | |
| 843 | static int |
| 844 | iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
| 845 | { |
| 846 | unsigned long flags; |
| 847 | struct iscsi_cls_conn *conn; |
| 848 | struct mempool_zone *z_error, *z_pdu; |
| 849 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 850 | 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] | 851 | if (!conn) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 852 | return -EINVAL; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 853 | spin_lock_irqsave(&connlock, flags); |
| 854 | conn->active = 0; |
| 855 | list_del(&conn->conn_list); |
| 856 | spin_unlock_irqrestore(&connlock, flags); |
| 857 | |
| 858 | z_pdu = conn->z_pdu; |
| 859 | z_error = conn->z_error; |
| 860 | |
| 861 | if (transport->destroy_conn) |
| 862 | transport->destroy_conn(conn); |
| 863 | |
| 864 | mempool_zone_destroy(z_pdu); |
| 865 | mempool_zone_destroy(z_error); |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 870 | static int |
| 871 | iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
| 872 | { |
| 873 | char *data = (char*)ev + sizeof(*ev); |
| 874 | struct iscsi_cls_conn *conn; |
| 875 | struct iscsi_cls_session *session; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 876 | int err = 0, value = 0; |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 877 | |
| 878 | session = iscsi_session_lookup(ev->u.set_param.sid); |
| 879 | conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid); |
| 880 | if (!conn || !session) |
| 881 | return -EINVAL; |
| 882 | |
| 883 | switch (ev->u.set_param.param) { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 884 | case ISCSI_PARAM_SESS_RECOVERY_TMO: |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 885 | sscanf(data, "%d", &value); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 886 | if (value != 0) |
| 887 | session->recovery_tmo = value; |
| 888 | break; |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 889 | default: |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 890 | err = transport->set_param(conn, ev->u.set_param.param, |
| 891 | data, ev->u.set_param.len); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | return err; |
| 895 | } |
| 896 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 897 | static int |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 898 | iscsi_if_transport_ep(struct iscsi_transport *transport, |
| 899 | struct iscsi_uevent *ev, int msg_type) |
| 900 | { |
| 901 | struct sockaddr *dst_addr; |
| 902 | int rc = 0; |
| 903 | |
| 904 | switch (msg_type) { |
| 905 | case ISCSI_UEVENT_TRANSPORT_EP_CONNECT: |
| 906 | if (!transport->ep_connect) |
| 907 | return -EINVAL; |
| 908 | |
| 909 | dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev)); |
| 910 | rc = transport->ep_connect(dst_addr, |
| 911 | ev->u.ep_connect.non_blocking, |
| 912 | &ev->r.ep_connect_ret.handle); |
| 913 | break; |
| 914 | case ISCSI_UEVENT_TRANSPORT_EP_POLL: |
| 915 | if (!transport->ep_poll) |
| 916 | return -EINVAL; |
| 917 | |
| 918 | ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle, |
| 919 | ev->u.ep_poll.timeout_ms); |
| 920 | break; |
| 921 | case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT: |
| 922 | if (!transport->ep_disconnect) |
| 923 | return -EINVAL; |
| 924 | |
| 925 | transport->ep_disconnect(ev->u.ep_disconnect.ep_handle); |
| 926 | break; |
| 927 | } |
| 928 | return rc; |
| 929 | } |
| 930 | |
| 931 | static int |
Mike Christie | 01cb225 | 2006-06-28 12:00:22 -0500 | [diff] [blame] | 932 | iscsi_tgt_dscvr(struct iscsi_transport *transport, |
| 933 | struct iscsi_uevent *ev) |
| 934 | { |
| 935 | struct sockaddr *dst_addr; |
| 936 | |
| 937 | if (!transport->tgt_dscvr) |
| 938 | return -EINVAL; |
| 939 | |
| 940 | dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev)); |
| 941 | return transport->tgt_dscvr(ev->u.tgt_dscvr.type, |
| 942 | ev->u.tgt_dscvr.host_no, |
| 943 | ev->u.tgt_dscvr.enable, dst_addr); |
| 944 | } |
| 945 | |
| 946 | static int |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 947 | iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 948 | { |
| 949 | int err = 0; |
| 950 | struct iscsi_uevent *ev = NLMSG_DATA(nlh); |
| 951 | struct iscsi_transport *transport = NULL; |
| 952 | struct iscsi_internal *priv; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 953 | struct iscsi_cls_session *session; |
| 954 | struct iscsi_cls_conn *conn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 955 | unsigned long flags; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 956 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 957 | priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle)); |
| 958 | if (!priv) |
| 959 | return -EINVAL; |
| 960 | transport = priv->iscsi_transport; |
| 961 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 962 | if (!try_module_get(transport->owner)) |
| 963 | return -EINVAL; |
| 964 | |
Mike Christie | 790f39a | 2006-05-18 20:31:39 -0500 | [diff] [blame] | 965 | priv->daemon_pid = NETLINK_CREDS(skb)->pid; |
| 966 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 967 | switch (nlh->nlmsg_type) { |
| 968 | case ISCSI_UEVENT_CREATE_SESSION: |
| 969 | err = iscsi_if_create_session(priv, ev); |
| 970 | break; |
| 971 | case ISCSI_UEVENT_DESTROY_SESSION: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 972 | session = iscsi_session_lookup(ev->u.d_session.sid); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 973 | if (session) { |
| 974 | spin_lock_irqsave(&sesslock, flags); |
| 975 | list_del(&session->sess_list); |
| 976 | spin_unlock_irqrestore(&sesslock, flags); |
| 977 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 978 | transport->destroy_session(session); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 979 | } else |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 980 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 981 | break; |
| 982 | case ISCSI_UEVENT_CREATE_CONN: |
| 983 | err = iscsi_if_create_conn(transport, ev); |
| 984 | break; |
| 985 | case ISCSI_UEVENT_DESTROY_CONN: |
| 986 | err = iscsi_if_destroy_conn(transport, ev); |
| 987 | break; |
| 988 | case ISCSI_UEVENT_BIND_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 989 | session = iscsi_session_lookup(ev->u.b_conn.sid); |
| 990 | 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] | 991 | |
| 992 | if (session && conn) |
| 993 | ev->r.retcode = transport->bind_conn(session, conn, |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 994 | ev->u.b_conn.transport_eph, |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 995 | ev->u.b_conn.is_leading); |
| 996 | else |
| 997 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 998 | break; |
| 999 | case ISCSI_UEVENT_SET_PARAM: |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1000 | err = iscsi_set_param(transport, ev); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1001 | break; |
| 1002 | case ISCSI_UEVENT_START_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1003 | 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] | 1004 | if (conn) |
| 1005 | ev->r.retcode = transport->start_conn(conn); |
| 1006 | else |
| 1007 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1008 | break; |
| 1009 | case ISCSI_UEVENT_STOP_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1010 | 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] | 1011 | if (conn) |
| 1012 | transport->stop_conn(conn, ev->u.stop_conn.flag); |
| 1013 | else |
| 1014 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1015 | break; |
| 1016 | case ISCSI_UEVENT_SEND_PDU: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1017 | 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] | 1018 | if (conn) |
| 1019 | ev->r.retcode = transport->send_pdu(conn, |
| 1020 | (struct iscsi_hdr*)((char*)ev + sizeof(*ev)), |
| 1021 | (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size, |
| 1022 | ev->u.send_pdu.data_size); |
| 1023 | else |
| 1024 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1025 | break; |
| 1026 | case ISCSI_UEVENT_GET_STATS: |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 1027 | err = iscsi_if_get_stats(transport, nlh); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1028 | break; |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 1029 | case ISCSI_UEVENT_TRANSPORT_EP_CONNECT: |
| 1030 | case ISCSI_UEVENT_TRANSPORT_EP_POLL: |
| 1031 | case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT: |
| 1032 | err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type); |
| 1033 | break; |
Mike Christie | 01cb225 | 2006-06-28 12:00:22 -0500 | [diff] [blame] | 1034 | case ISCSI_UEVENT_TGT_DSCVR: |
| 1035 | err = iscsi_tgt_dscvr(transport, ev); |
| 1036 | break; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1037 | default: |
| 1038 | err = -EINVAL; |
| 1039 | break; |
| 1040 | } |
| 1041 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1042 | module_put(transport->owner); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1043 | return err; |
| 1044 | } |
| 1045 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1046 | /* |
| 1047 | * Get message from skb (based on rtnetlink_rcv_skb). Each message is |
| 1048 | * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or |
| 1049 | * invalid creds are discarded silently. |
| 1050 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1051 | static void |
| 1052 | iscsi_if_rx(struct sock *sk, int len) |
| 1053 | { |
| 1054 | struct sk_buff *skb; |
| 1055 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1056 | mutex_lock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1057 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1058 | if (NETLINK_CREDS(skb)->uid) { |
| 1059 | skb_pull(skb, skb->len); |
| 1060 | goto free_skb; |
| 1061 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1062 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1063 | while (skb->len >= NLMSG_SPACE(0)) { |
| 1064 | int err; |
| 1065 | uint32_t rlen; |
| 1066 | struct nlmsghdr *nlh; |
| 1067 | struct iscsi_uevent *ev; |
| 1068 | |
| 1069 | nlh = (struct nlmsghdr *)skb->data; |
| 1070 | if (nlh->nlmsg_len < sizeof(*nlh) || |
| 1071 | skb->len < nlh->nlmsg_len) { |
| 1072 | break; |
| 1073 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1074 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1075 | ev = NLMSG_DATA(nlh); |
| 1076 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1077 | if (rlen > skb->len) |
| 1078 | rlen = skb->len; |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1079 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1080 | err = iscsi_if_recv_msg(skb, nlh); |
| 1081 | if (err) { |
| 1082 | ev->type = ISCSI_KEVENT_IF_ERROR; |
| 1083 | ev->iferror = err; |
| 1084 | } |
| 1085 | do { |
| 1086 | /* |
| 1087 | * special case for GET_STATS: |
| 1088 | * on success - sending reply and stats from |
| 1089 | * inside of if_recv_msg(), |
| 1090 | * on error - fall through. |
| 1091 | */ |
| 1092 | if (ev->type == ISCSI_UEVENT_GET_STATS && !err) |
| 1093 | break; |
| 1094 | err = iscsi_if_send_reply( |
| 1095 | NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq, |
| 1096 | nlh->nlmsg_type, 0, 0, ev, sizeof(*ev)); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1097 | if (atomic_read(&z_reply->allocated) >= |
| 1098 | z_reply->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1099 | ev->iferror = -ENOMEM; |
| 1100 | } while (err < 0 && err != -ECONNREFUSED); |
| 1101 | skb_pull(skb, rlen); |
| 1102 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1103 | free_skb: |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1104 | kfree_skb(skb); |
| 1105 | } |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1106 | mutex_unlock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1109 | #define iscsi_cdev_to_conn(_cdev) \ |
| 1110 | iscsi_dev_to_conn(_cdev->dev) |
| 1111 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1112 | #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \ |
| 1113 | struct class_device_attribute class_device_attr_##_prefix##_##_name = \ |
| 1114 | __ATTR(_name,_mode,_show,_store) |
| 1115 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1116 | /* |
| 1117 | * iSCSI connection attrs |
| 1118 | */ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1119 | #define iscsi_conn_attr_show(param) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | static ssize_t \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1121 | show_conn_param_##param(struct class_device *cdev, char *buf) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1122 | { \ |
| 1123 | struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \ |
| 1124 | struct iscsi_transport *t = conn->transport; \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1125 | return t->get_conn_param(conn, param, buf); \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1126 | } |
| 1127 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1128 | #define iscsi_conn_attr(field, param) \ |
| 1129 | iscsi_conn_attr_show(param) \ |
| 1130 | static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1131 | NULL); |
| 1132 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1133 | iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH); |
| 1134 | iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH); |
| 1135 | iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN); |
| 1136 | iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN); |
| 1137 | iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN); |
| 1138 | iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN); |
| 1139 | iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT); |
| 1140 | iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT); |
| 1141 | iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN); |
| 1142 | iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS); |
| 1143 | iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1145 | #define iscsi_cdev_to_session(_cdev) \ |
| 1146 | iscsi_dev_to_session(_cdev->dev) |
| 1147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | /* |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1149 | * iSCSI session attrs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | */ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1151 | #define iscsi_session_attr_show(param) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | static ssize_t \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1153 | show_session_param_##param(struct class_device *cdev, char *buf) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1154 | { \ |
| 1155 | struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \ |
| 1156 | struct iscsi_transport *t = session->transport; \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1157 | return t->get_session_param(session, param, buf); \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1160 | #define iscsi_session_attr(field, param) \ |
| 1161 | iscsi_session_attr_show(param) \ |
| 1162 | static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1163 | NULL); |
| 1164 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1165 | iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME); |
| 1166 | iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN); |
| 1167 | iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T); |
| 1168 | iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN); |
| 1169 | iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST); |
| 1170 | iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST); |
| 1171 | iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN); |
| 1172 | iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN); |
| 1173 | iscsi_session_attr(erl, ISCSI_PARAM_ERL); |
| 1174 | iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1175 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1176 | #define iscsi_priv_session_attr_show(field, format) \ |
| 1177 | static ssize_t \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1178 | show_priv_session_##field(struct class_device *cdev, char *buf) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1179 | { \ |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1180 | struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1181 | return sprintf(buf, format"\n", session->field); \ |
| 1182 | } |
| 1183 | |
| 1184 | #define iscsi_priv_session_attr(field, format) \ |
| 1185 | iscsi_priv_session_attr_show(field, format) \ |
| 1186 | static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \ |
| 1187 | NULL) |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1188 | iscsi_priv_session_attr(recovery_tmo, "%d"); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1189 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1190 | #define SETUP_PRIV_SESSION_RD_ATTR(field) \ |
| 1191 | do { \ |
| 1192 | priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \ |
| 1193 | count++; \ |
| 1194 | } while (0) |
| 1195 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1196 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1197 | #define SETUP_SESSION_RD_ATTR(field, param_flag) \ |
| 1198 | do { \ |
| 1199 | if (tt->param_mask & param_flag) { \ |
| 1200 | priv->session_attrs[count] = &class_device_attr_sess_##field; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | count++; \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1202 | } \ |
| 1203 | } while (0) |
| 1204 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1205 | #define SETUP_CONN_RD_ATTR(field, param_flag) \ |
| 1206 | do { \ |
| 1207 | if (tt->param_mask & param_flag) { \ |
| 1208 | priv->conn_attrs[count] = &class_device_attr_conn_##field; \ |
| 1209 | count++; \ |
| 1210 | } \ |
| 1211 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1213 | static int iscsi_session_match(struct attribute_container *cont, |
| 1214 | struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1216 | struct iscsi_cls_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | struct Scsi_Host *shost; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1218 | struct iscsi_internal *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1220 | if (!iscsi_is_session_dev(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | return 0; |
| 1222 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1223 | session = iscsi_dev_to_session(dev); |
| 1224 | shost = iscsi_session_to_shost(session); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1225 | if (!shost->transportt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | return 0; |
| 1227 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1228 | priv = to_iscsi_internal(shost->transportt); |
| 1229 | if (priv->session_cont.ac.class != &iscsi_session_class.class) |
| 1230 | return 0; |
| 1231 | |
| 1232 | return &priv->session_cont.ac == cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1235 | static int iscsi_conn_match(struct attribute_container *cont, |
| 1236 | struct device *dev) |
| 1237 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1238 | struct iscsi_cls_session *session; |
| 1239 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1240 | struct Scsi_Host *shost; |
| 1241 | struct iscsi_internal *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1243 | if (!iscsi_is_conn_dev(dev)) |
| 1244 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1246 | conn = iscsi_dev_to_conn(dev); |
| 1247 | session = iscsi_dev_to_session(conn->dev.parent); |
| 1248 | shost = iscsi_session_to_shost(session); |
| 1249 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1250 | if (!shost->transportt) |
| 1251 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1253 | priv = to_iscsi_internal(shost->transportt); |
| 1254 | if (priv->conn_cont.ac.class != &iscsi_connection_class.class) |
| 1255 | return 0; |
| 1256 | |
| 1257 | return &priv->conn_cont.ac == cont; |
| 1258 | } |
| 1259 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1260 | static int iscsi_host_match(struct attribute_container *cont, |
| 1261 | struct device *dev) |
| 1262 | { |
| 1263 | struct Scsi_Host *shost; |
| 1264 | struct iscsi_internal *priv; |
| 1265 | |
| 1266 | if (!scsi_is_host_device(dev)) |
| 1267 | return 0; |
| 1268 | |
| 1269 | shost = dev_to_shost(dev); |
| 1270 | if (!shost->transportt || |
| 1271 | shost->transportt->host_attrs.ac.class != &iscsi_host_class.class) |
| 1272 | return 0; |
| 1273 | |
| 1274 | priv = to_iscsi_internal(shost->transportt); |
| 1275 | return &priv->t.host_attrs.ac == cont; |
| 1276 | } |
| 1277 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1278 | struct scsi_transport_template * |
| 1279 | iscsi_register_transport(struct iscsi_transport *tt) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1280 | { |
| 1281 | struct iscsi_internal *priv; |
| 1282 | unsigned long flags; |
| 1283 | int count = 0, err; |
| 1284 | |
| 1285 | BUG_ON(!tt); |
| 1286 | |
| 1287 | priv = iscsi_if_transport_lookup(tt); |
| 1288 | if (priv) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1289 | return NULL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1290 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 1291 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1292 | if (!priv) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1293 | return NULL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1294 | INIT_LIST_HEAD(&priv->list); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1295 | priv->iscsi_transport = tt; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1296 | priv->t.user_scan = iscsi_user_scan; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1297 | |
| 1298 | priv->cdev.class = &iscsi_transport_class; |
| 1299 | snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name); |
| 1300 | err = class_device_register(&priv->cdev); |
| 1301 | if (err) |
| 1302 | goto free_priv; |
| 1303 | |
| 1304 | err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group); |
| 1305 | if (err) |
| 1306 | goto unregister_cdev; |
| 1307 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1308 | /* host parameters */ |
| 1309 | priv->t.host_attrs.ac.attrs = &priv->host_attrs[0]; |
| 1310 | priv->t.host_attrs.ac.class = &iscsi_host_class.class; |
| 1311 | priv->t.host_attrs.ac.match = iscsi_host_match; |
| 1312 | priv->t.host_size = sizeof(struct iscsi_host); |
| 1313 | priv->host_attrs[0] = NULL; |
| 1314 | transport_container_register(&priv->t.host_attrs); |
| 1315 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1316 | /* connection parameters */ |
| 1317 | priv->conn_cont.ac.attrs = &priv->conn_attrs[0]; |
| 1318 | priv->conn_cont.ac.class = &iscsi_connection_class.class; |
| 1319 | priv->conn_cont.ac.match = iscsi_conn_match; |
| 1320 | transport_container_register(&priv->conn_cont); |
| 1321 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1322 | SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH); |
| 1323 | SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH); |
| 1324 | SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN); |
| 1325 | SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN); |
| 1326 | SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN); |
| 1327 | SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN); |
| 1328 | SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS); |
| 1329 | SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT); |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 1330 | SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN); |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1331 | SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS); |
| 1332 | SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1333 | |
| 1334 | BUG_ON(count > ISCSI_CONN_ATTRS); |
| 1335 | priv->conn_attrs[count] = NULL; |
| 1336 | count = 0; |
| 1337 | |
| 1338 | /* session parameters */ |
| 1339 | priv->session_cont.ac.attrs = &priv->session_attrs[0]; |
| 1340 | priv->session_cont.ac.class = &iscsi_session_class.class; |
| 1341 | priv->session_cont.ac.match = iscsi_session_match; |
| 1342 | transport_container_register(&priv->session_cont); |
| 1343 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1344 | SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN); |
| 1345 | SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T); |
| 1346 | SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN); |
| 1347 | SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST); |
| 1348 | SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST); |
| 1349 | SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN); |
| 1350 | SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN); |
| 1351 | SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL); |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 1352 | SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME); |
| 1353 | SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1354 | SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | BUG_ON(count > ISCSI_SESSION_ATTRS); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1357 | priv->session_attrs[count] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1359 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 1360 | list_add(&priv->list, &iscsi_transports); |
| 1361 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1363 | printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1364 | return &priv->t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1366 | unregister_cdev: |
| 1367 | class_device_unregister(&priv->cdev); |
| 1368 | free_priv: |
| 1369 | kfree(priv); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1370 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1372 | EXPORT_SYMBOL_GPL(iscsi_register_transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1374 | int iscsi_unregister_transport(struct iscsi_transport *tt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1376 | struct iscsi_internal *priv; |
| 1377 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1379 | BUG_ON(!tt); |
| 1380 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1381 | mutex_lock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1382 | |
| 1383 | priv = iscsi_if_transport_lookup(tt); |
| 1384 | BUG_ON (!priv); |
| 1385 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1386 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 1387 | list_del(&priv->list); |
| 1388 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 1389 | |
| 1390 | transport_container_unregister(&priv->conn_cont); |
| 1391 | transport_container_unregister(&priv->session_cont); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1392 | transport_container_unregister(&priv->t.host_attrs); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1393 | |
| 1394 | sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group); |
| 1395 | class_device_unregister(&priv->cdev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1396 | mutex_unlock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | EXPORT_SYMBOL_GPL(iscsi_unregister_transport); |
| 1401 | |
| 1402 | static int |
| 1403 | iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1404 | { |
| 1405 | struct netlink_notify *n = ptr; |
| 1406 | |
| 1407 | if (event == NETLINK_URELEASE && |
| 1408 | n->protocol == NETLINK_ISCSI && n->pid) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1409 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1410 | unsigned long flags; |
| 1411 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1412 | mempool_zone_complete(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1413 | spin_lock_irqsave(&connlock, flags); |
| 1414 | list_for_each_entry(conn, &connlist, conn_list) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1415 | mempool_zone_complete(conn->z_error); |
| 1416 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1417 | } |
| 1418 | spin_unlock_irqrestore(&connlock, flags); |
| 1419 | } |
| 1420 | |
| 1421 | return NOTIFY_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1424 | static struct notifier_block iscsi_nl_notifier = { |
| 1425 | .notifier_call = iscsi_rcv_nl_event, |
| 1426 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | |
| 1428 | static __init int iscsi_transport_init(void) |
| 1429 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1430 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1432 | err = class_register(&iscsi_transport_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | if (err) |
| 1434 | return err; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1435 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1436 | err = transport_class_register(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1437 | if (err) |
| 1438 | goto unregister_transport_class; |
| 1439 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1440 | err = transport_class_register(&iscsi_connection_class); |
| 1441 | if (err) |
| 1442 | goto unregister_host_class; |
| 1443 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1444 | err = transport_class_register(&iscsi_session_class); |
| 1445 | if (err) |
| 1446 | goto unregister_conn_class; |
| 1447 | |
| 1448 | err = netlink_register_notifier(&iscsi_nl_notifier); |
| 1449 | if (err) |
| 1450 | goto unregister_session_class; |
| 1451 | |
James Bottomley | 2290d2b | 2005-09-08 10:14:11 -0500 | [diff] [blame] | 1452 | nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx, |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1453 | THIS_MODULE); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1454 | if (!nls) { |
| 1455 | err = -ENOBUFS; |
| 1456 | goto unregister_notifier; |
| 1457 | } |
| 1458 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1459 | z_reply = mempool_zone_init(Z_MAX_REPLY, |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1460 | NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1461 | if (z_reply) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1462 | return 0; |
| 1463 | |
| 1464 | sock_release(nls->sk_socket); |
| 1465 | unregister_notifier: |
| 1466 | netlink_unregister_notifier(&iscsi_nl_notifier); |
| 1467 | unregister_session_class: |
| 1468 | transport_class_unregister(&iscsi_session_class); |
| 1469 | unregister_conn_class: |
| 1470 | transport_class_unregister(&iscsi_connection_class); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1471 | unregister_host_class: |
| 1472 | transport_class_unregister(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1473 | unregister_transport_class: |
| 1474 | class_unregister(&iscsi_transport_class); |
| 1475 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static void __exit iscsi_transport_exit(void) |
| 1479 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1480 | mempool_zone_destroy(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1481 | sock_release(nls->sk_socket); |
| 1482 | netlink_unregister_notifier(&iscsi_nl_notifier); |
| 1483 | transport_class_unregister(&iscsi_connection_class); |
| 1484 | transport_class_unregister(&iscsi_session_class); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1485 | transport_class_unregister(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1486 | class_unregister(&iscsi_transport_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | module_init(iscsi_transport_init); |
| 1490 | module_exit(iscsi_transport_exit); |
| 1491 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1492 | MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, " |
| 1493 | "Dmitry Yusupov <dmitry_yus@yahoo.com>, " |
| 1494 | "Alex Aizman <itn780@yahoo.com>"); |
| 1495 | MODULE_DESCRIPTION("iSCSI Transport Interface"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | MODULE_LICENSE("GPL"); |