Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 2 | * zfcp device driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 4 | * Module interface and handling of zfcp data structures. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 6 | * Copyright IBM Corporation 2002, 2008 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 9 | /* |
| 10 | * Driver authors: |
| 11 | * Martin Peschke (originator of the driver) |
| 12 | * Raimund Schroeder |
| 13 | * Aron Zeh |
| 14 | * Wolfgang Taphorn |
| 15 | * Stefan Bader |
| 16 | * Heiko Carstens (kernel 2.6 port of the driver) |
| 17 | * Andreas Herrmann |
| 18 | * Maxim Shchetynin |
| 19 | * Volker Sameske |
| 20 | * Ralph Wuerthner |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 21 | * Michael Loehr |
| 22 | * Swen Schillig |
| 23 | * Christof Schmitt |
| 24 | * Martin Petermann |
| 25 | * Sven Schuetz |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 26 | */ |
| 27 | |
Christof Schmitt | 45633fd | 2008-06-10 18:20:55 +0200 | [diff] [blame] | 28 | #include <linux/miscdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "zfcp_ext.h" |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static char *device; |
| 32 | /*********************** FUNCTION PROTOTYPES *********************************/ |
| 33 | |
| 34 | /* written against the module interface */ |
| 35 | static int __init zfcp_module_init(void); |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | /*********************** KERNEL/MODULE PARAMETERS ***************************/ |
| 38 | |
| 39 | /* declare driver module init/cleanup functions */ |
| 40 | module_init(zfcp_module_init); |
| 41 | |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 42 | MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | MODULE_DESCRIPTION |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 44 | ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | MODULE_LICENSE("GPL"); |
| 46 | |
| 6f71d9b | 2005-04-10 23:04:28 -0500 | [diff] [blame] | 47 | module_param(device, charp, 0400); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | MODULE_PARM_DESC(device, "specify initial device"); |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | /****************************************************************/ |
| 51 | /************** Functions without logging ***********************/ |
| 52 | /****************************************************************/ |
| 53 | |
| 54 | void |
| 55 | _zfcp_hex_dump(char *addr, int count) |
| 56 | { |
| 57 | int i; |
| 58 | for (i = 0; i < count; i++) { |
| 59 | printk("%02x", addr[i]); |
| 60 | if ((i % 4) == 3) |
| 61 | printk(" "); |
| 62 | if ((i % 32) == 31) |
| 63 | printk("\n"); |
| 64 | } |
| 65 | if (((i-1) % 32) != 31) |
| 66 | printk("\n"); |
| 67 | } |
| 68 | |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 69 | |
| 70 | /****************************************************************/ |
| 71 | /****** Functions to handle the request ID hash table ********/ |
| 72 | /****************************************************************/ |
| 73 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 74 | static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter) |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 75 | { |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 76 | int idx; |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 77 | |
| 78 | adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head), |
| 79 | GFP_KERNEL); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 80 | if (!adapter->req_list) |
| 81 | return -ENOMEM; |
| 82 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 83 | for (idx = 0; idx < REQUEST_LIST_SIZE; idx++) |
| 84 | INIT_LIST_HEAD(&adapter->req_list[idx]); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static void zfcp_reqlist_free(struct zfcp_adapter *adapter) |
| 89 | { |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 90 | kfree(adapter->req_list); |
| 91 | } |
| 92 | |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 93 | int zfcp_reqlist_isempty(struct zfcp_adapter *adapter) |
| 94 | { |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 95 | unsigned int idx; |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 96 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 97 | for (idx = 0; idx < REQUEST_LIST_SIZE; idx++) |
| 98 | if (!list_empty(&adapter->req_list[idx])) |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 99 | return 0; |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 100 | return 1; |
| 101 | } |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /****************************************************************/ |
| 104 | /************** Uncategorised Functions *************************/ |
| 105 | /****************************************************************/ |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /** |
| 108 | * zfcp_device_setup - setup function |
| 109 | * @str: pointer to parameter string |
| 110 | * |
| 111 | * Parse "device=..." parameter string. |
| 112 | */ |
| 113 | static int __init |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 114 | zfcp_device_setup(char *devstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 116 | char *tmp, *str; |
| 117 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 119 | if (!devstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 122 | len = strlen(devstr) + 1; |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 123 | str = kmalloc(len, GFP_KERNEL); |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 124 | if (!str) { |
| 125 | pr_err("zfcp: Could not allocate memory for " |
| 126 | "device parameter string, device not attached.\n"); |
| 127 | return 0; |
| 128 | } |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 129 | memcpy(str, devstr, len); |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | tmp = strchr(str, ','); |
| 132 | if (!tmp) |
| 133 | goto err_out; |
| 134 | *tmp++ = '\0'; |
| 135 | strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE); |
| 136 | zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0'; |
| 137 | |
| 138 | zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0); |
| 139 | if (*tmp++ != ',') |
| 140 | goto err_out; |
| 141 | if (*tmp == '\0') |
| 142 | goto err_out; |
| 143 | |
| 144 | zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0); |
| 145 | if (*tmp != '\0') |
| 146 | goto err_out; |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 147 | kfree(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return 1; |
| 149 | |
| 150 | err_out: |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 151 | pr_err("zfcp: Parse error for device parameter string %s, " |
| 152 | "device not attached.\n", str); |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 153 | kfree(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static void __init |
| 158 | zfcp_init_device_configure(void) |
| 159 | { |
| 160 | struct zfcp_adapter *adapter; |
| 161 | struct zfcp_port *port; |
| 162 | struct zfcp_unit *unit; |
| 163 | |
| 164 | down(&zfcp_data.config_sema); |
| 165 | read_lock_irq(&zfcp_data.config_lock); |
| 166 | adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid); |
| 167 | if (adapter) |
| 168 | zfcp_adapter_get(adapter); |
| 169 | read_unlock_irq(&zfcp_data.config_lock); |
| 170 | |
| 171 | if (adapter == NULL) |
| 172 | goto out_adapter; |
| 173 | port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0); |
| 174 | if (!port) |
| 175 | goto out_port; |
| 176 | unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun); |
| 177 | if (!unit) |
| 178 | goto out_unit; |
| 179 | up(&zfcp_data.config_sema); |
| 180 | ccw_device_set_online(adapter->ccw_device); |
| 181 | zfcp_erp_wait(adapter); |
| 182 | down(&zfcp_data.config_sema); |
| 183 | zfcp_unit_put(unit); |
| 184 | out_unit: |
| 185 | zfcp_port_put(port); |
| 186 | out_port: |
| 187 | zfcp_adapter_put(adapter); |
| 188 | out_adapter: |
| 189 | up(&zfcp_data.config_sema); |
| 190 | return; |
| 191 | } |
| 192 | |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 193 | static int calc_alignment(int size) |
| 194 | { |
| 195 | int align = 1; |
| 196 | |
| 197 | if (!size) |
| 198 | return 0; |
| 199 | |
| 200 | while ((size - align) > 0) |
| 201 | align <<= 1; |
| 202 | |
| 203 | return align; |
| 204 | } |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | static int __init |
| 207 | zfcp_module_init(void) |
| 208 | { |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 209 | int retval = -ENOMEM; |
| 210 | int size, align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 212 | size = sizeof(struct zfcp_fsf_req_qtcb); |
| 213 | align = calc_alignment(size); |
| 214 | zfcp_data.fsf_req_qtcb_cache = |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 215 | kmem_cache_create("zfcp_fsf", size, align, 0, NULL); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 216 | if (!zfcp_data.fsf_req_qtcb_cache) |
| 217 | goto out; |
| 218 | |
| 219 | size = sizeof(struct fsf_status_read_buffer); |
| 220 | align = calc_alignment(size); |
| 221 | zfcp_data.sr_buffer_cache = |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 222 | kmem_cache_create("zfcp_sr", size, align, 0, NULL); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 223 | if (!zfcp_data.sr_buffer_cache) |
| 224 | goto out_sr_cache; |
| 225 | |
| 226 | size = sizeof(struct zfcp_gid_pn_data); |
| 227 | align = calc_alignment(size); |
| 228 | zfcp_data.gid_pn_cache = |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 229 | kmem_cache_create("zfcp_gid", size, align, 0, NULL); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 230 | if (!zfcp_data.gid_pn_cache) |
| 231 | goto out_gid_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | /* initialize adapter list */ |
| 234 | INIT_LIST_HEAD(&zfcp_data.adapter_list_head); |
| 235 | |
| 236 | /* initialize adapters to be removed list head */ |
| 237 | INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh); |
| 238 | |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 239 | zfcp_data.scsi_transport_template = |
| 240 | fc_attach_transport(&zfcp_transport_functions); |
| 241 | if (!zfcp_data.scsi_transport_template) |
| 242 | goto out_transport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | retval = misc_register(&zfcp_cfdc_misc); |
| 245 | if (retval != 0) { |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 246 | pr_err("zfcp: registration of misc device zfcp_cfdc failed\n"); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 247 | goto out_misc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* Initialise proc semaphores */ |
| 251 | sema_init(&zfcp_data.config_sema, 1); |
| 252 | |
| 253 | /* initialise configuration rw lock */ |
| 254 | rwlock_init(&zfcp_data.config_lock); |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /* setup dynamic I/O */ |
| 257 | retval = zfcp_ccw_register(); |
| 258 | if (retval) { |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 259 | pr_err("zfcp: Registration with common I/O layer failed.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | goto out_ccw_register; |
| 261 | } |
| 262 | |
| 263 | if (zfcp_device_setup(device)) |
| 264 | zfcp_init_device_configure(); |
| 265 | |
| 266 | goto out; |
| 267 | |
| 268 | out_ccw_register: |
| 269 | misc_deregister(&zfcp_cfdc_misc); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 270 | out_misc: |
| 271 | fc_release_transport(zfcp_data.scsi_transport_template); |
| 272 | out_transport: |
| 273 | kmem_cache_destroy(zfcp_data.gid_pn_cache); |
| 274 | out_gid_cache: |
| 275 | kmem_cache_destroy(zfcp_data.sr_buffer_cache); |
| 276 | out_sr_cache: |
| 277 | kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | out: |
| 279 | return retval; |
| 280 | } |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | /****************************************************************/ |
| 283 | /****** Functions for configuration/set-up of structures ********/ |
| 284 | /****************************************************************/ |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /** |
| 287 | * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN |
| 288 | * @port: pointer to port to search for unit |
| 289 | * @fcp_lun: FCP LUN to search for |
| 290 | * Traverse list of all units of a port and return pointer to a unit |
| 291 | * with the given FCP LUN. |
| 292 | */ |
| 293 | struct zfcp_unit * |
| 294 | zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun) |
| 295 | { |
| 296 | struct zfcp_unit *unit; |
| 297 | int found = 0; |
| 298 | |
| 299 | list_for_each_entry(unit, &port->unit_list_head, list) { |
| 300 | if ((unit->fcp_lun == fcp_lun) && |
| 301 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status)) |
| 302 | { |
| 303 | found = 1; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | return found ? unit : NULL; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn |
| 312 | * @adapter: pointer to adapter to search for port |
| 313 | * @wwpn: wwpn to search for |
| 314 | * Traverse list of all ports of an adapter and return pointer to a port |
| 315 | * with the given wwpn. |
| 316 | */ |
| 317 | struct zfcp_port * |
| 318 | zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn) |
| 319 | { |
| 320 | struct zfcp_port *port; |
| 321 | int found = 0; |
| 322 | |
| 323 | list_for_each_entry(port, &adapter->port_list_head, list) { |
| 324 | if ((port->wwpn == wwpn) && |
| 325 | !(atomic_read(&port->status) & |
| 326 | (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) { |
| 327 | found = 1; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | return found ? port : NULL; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * zfcp_get_port_by_did - find port in port list of adapter by d_id |
| 336 | * @adapter: pointer to adapter to search for port |
| 337 | * @d_id: d_id to search for |
| 338 | * Traverse list of all ports of an adapter and return pointer to a port |
| 339 | * with the given d_id. |
| 340 | */ |
| 341 | struct zfcp_port * |
| 342 | zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id) |
| 343 | { |
| 344 | struct zfcp_port *port; |
| 345 | int found = 0; |
| 346 | |
| 347 | list_for_each_entry(port, &adapter->port_list_head, list) { |
| 348 | if ((port->d_id == d_id) && |
| 349 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) |
| 350 | { |
| 351 | found = 1; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | return found ? port : NULL; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id |
| 360 | * @bus_id: bus_id to search for |
| 361 | * Traverse list of all adapters and return pointer to an adapter |
| 362 | * with the given bus_id. |
| 363 | */ |
| 364 | struct zfcp_adapter * |
| 365 | zfcp_get_adapter_by_busid(char *bus_id) |
| 366 | { |
| 367 | struct zfcp_adapter *adapter; |
| 368 | int found = 0; |
| 369 | |
| 370 | list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) { |
| 371 | if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter), |
| 372 | BUS_ID_SIZE) == 0) && |
| 373 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, |
| 374 | &adapter->status)){ |
| 375 | found = 1; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | return found ? adapter : NULL; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * zfcp_unit_enqueue - enqueue unit to unit list of a port. |
| 384 | * @port: pointer to port where unit is added |
| 385 | * @fcp_lun: FCP LUN of unit to be enqueued |
| 386 | * Return: pointer to enqueued unit on success, NULL on error |
| 387 | * Locks: config_sema must be held to serialize changes to the unit list |
| 388 | * |
| 389 | * Sets up some unit internal structures and creates sysfs entry. |
| 390 | */ |
| 391 | struct zfcp_unit * |
| 392 | zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun) |
| 393 | { |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 394 | struct zfcp_unit *unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | /* |
| 397 | * check that there is no unit with this FCP_LUN already in list |
| 398 | * and enqueue it. |
| 399 | * Note: Unlike for the adapter and the port, this is an error |
| 400 | */ |
| 401 | read_lock_irq(&zfcp_data.config_lock); |
| 402 | unit = zfcp_get_unit_by_lun(port, fcp_lun); |
| 403 | read_unlock_irq(&zfcp_data.config_lock); |
| 404 | if (unit) |
| 405 | return NULL; |
| 406 | |
Andreas Herrmann | ec4081c | 2006-05-22 18:17:30 +0200 | [diff] [blame] | 407 | unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (!unit) |
| 409 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | /* initialise reference count stuff */ |
| 412 | atomic_set(&unit->refcount, 0); |
| 413 | init_waitqueue_head(&unit->remove_wq); |
| 414 | |
| 415 | unit->port = port; |
| 416 | unit->fcp_lun = fcp_lun; |
| 417 | |
| 418 | /* setup for sysfs registration */ |
| 419 | snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun); |
| 420 | unit->sysfs_device.parent = &port->sysfs_device; |
| 421 | unit->sysfs_device.release = zfcp_sysfs_unit_release; |
| 422 | dev_set_drvdata(&unit->sysfs_device, unit); |
| 423 | |
| 424 | /* mark unit unusable as long as sysfs registration is not complete */ |
| 425 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); |
| 426 | |
Christof Schmitt | c961585 | 2008-05-06 11:00:05 +0200 | [diff] [blame] | 427 | spin_lock_init(&unit->latencies.lock); |
| 428 | unit->latencies.write.channel.min = 0xFFFFFFFF; |
| 429 | unit->latencies.write.fabric.min = 0xFFFFFFFF; |
| 430 | unit->latencies.read.channel.min = 0xFFFFFFFF; |
| 431 | unit->latencies.read.fabric.min = 0xFFFFFFFF; |
| 432 | unit->latencies.cmd.channel.min = 0xFFFFFFFF; |
| 433 | unit->latencies.cmd.fabric.min = 0xFFFFFFFF; |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (device_register(&unit->sysfs_device)) { |
| 436 | kfree(unit); |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) { |
| 441 | device_unregister(&unit->sysfs_device); |
| 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | zfcp_unit_get(unit); |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 446 | unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | write_lock_irq(&zfcp_data.config_lock); |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 449 | list_add_tail(&unit->list, &port->unit_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); |
| 451 | atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status); |
| 452 | write_unlock_irq(&zfcp_data.config_lock); |
| 453 | |
| 454 | port->units++; |
| 455 | zfcp_port_get(port); |
| 456 | |
| 457 | return unit; |
| 458 | } |
| 459 | |
| 460 | void |
| 461 | zfcp_unit_dequeue(struct zfcp_unit *unit) |
| 462 | { |
| 463 | zfcp_unit_wait(unit); |
| 464 | write_lock_irq(&zfcp_data.config_lock); |
| 465 | list_del(&unit->list); |
| 466 | write_unlock_irq(&zfcp_data.config_lock); |
| 467 | unit->port->units--; |
| 468 | zfcp_port_put(unit->port); |
| 469 | zfcp_sysfs_unit_remove_files(&unit->sysfs_device); |
| 470 | device_unregister(&unit->sysfs_device); |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | /* |
| 474 | * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI |
| 475 | * commands. |
Swen Schillig | 41fa2ad | 2007-09-07 09:15:31 +0200 | [diff] [blame] | 476 | * It also genrates fcp-nameserver request/response buffer and unsolicited |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | * status read fsf_req buffers. |
| 478 | * |
| 479 | * locks: must only be called with zfcp_data.config_sema taken |
| 480 | */ |
| 481 | static int |
| 482 | zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter) |
| 483 | { |
| 484 | adapter->pool.fsf_req_erp = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 485 | mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR, |
| 486 | zfcp_data.fsf_req_qtcb_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 487 | if (!adapter->pool.fsf_req_erp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | return -ENOMEM; |
| 489 | |
| 490 | adapter->pool.fsf_req_scsi = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 491 | mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR, |
| 492 | zfcp_data.fsf_req_qtcb_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 493 | if (!adapter->pool.fsf_req_scsi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | return -ENOMEM; |
| 495 | |
| 496 | adapter->pool.fsf_req_abort = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 497 | mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR, |
| 498 | zfcp_data.fsf_req_qtcb_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 499 | if (!adapter->pool.fsf_req_abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | return -ENOMEM; |
| 501 | |
| 502 | adapter->pool.fsf_req_status_read = |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 503 | mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR, |
| 504 | sizeof(struct zfcp_fsf_req)); |
| 505 | if (!adapter->pool.fsf_req_status_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | return -ENOMEM; |
| 507 | |
| 508 | adapter->pool.data_status_read = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 509 | mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR, |
| 510 | zfcp_data.sr_buffer_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 511 | if (!adapter->pool.data_status_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | return -ENOMEM; |
| 513 | |
| 514 | adapter->pool.data_gid_pn = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 515 | mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR, |
| 516 | zfcp_data.gid_pn_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 517 | if (!adapter->pool.data_gid_pn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | return -ENOMEM; |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * zfcp_free_low_mem_buffers - free memory pools of an adapter |
| 525 | * @adapter: pointer to zfcp_adapter for which memory pools should be freed |
| 526 | * locking: zfcp_data.config_sema must be held |
| 527 | */ |
| 528 | static void |
| 529 | zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter) |
| 530 | { |
| 531 | if (adapter->pool.fsf_req_erp) |
| 532 | mempool_destroy(adapter->pool.fsf_req_erp); |
| 533 | if (adapter->pool.fsf_req_scsi) |
| 534 | mempool_destroy(adapter->pool.fsf_req_scsi); |
| 535 | if (adapter->pool.fsf_req_abort) |
| 536 | mempool_destroy(adapter->pool.fsf_req_abort); |
| 537 | if (adapter->pool.fsf_req_status_read) |
| 538 | mempool_destroy(adapter->pool.fsf_req_status_read); |
| 539 | if (adapter->pool.data_status_read) |
| 540 | mempool_destroy(adapter->pool.data_status_read); |
| 541 | if (adapter->pool.data_gid_pn) |
| 542 | mempool_destroy(adapter->pool.data_gid_pn); |
| 543 | } |
| 544 | |
Heiko Carstens | 763968e | 2007-05-10 15:45:46 +0200 | [diff] [blame] | 545 | static void zfcp_dummy_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | return; |
| 548 | } |
| 549 | |
Swen Schillig | d26ab06 | 2008-05-19 12:17:37 +0200 | [diff] [blame] | 550 | int zfcp_status_read_refill(struct zfcp_adapter *adapter) |
| 551 | { |
| 552 | while (atomic_read(&adapter->stat_miss) > 0) |
| 553 | if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL)) |
| 554 | break; |
| 555 | else |
| 556 | atomic_dec(&adapter->stat_miss); |
| 557 | |
| 558 | if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) { |
| 559 | zfcp_erp_adapter_reopen(adapter, 0, 103, NULL); |
| 560 | return 1; |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static void _zfcp_status_read_scheduler(struct work_struct *work) |
| 566 | { |
| 567 | zfcp_status_read_refill(container_of(work, struct zfcp_adapter, |
| 568 | stat_work)); |
| 569 | } |
| 570 | |
Swen Schillig | cc8c282 | 2008-06-10 18:21:00 +0200 | [diff] [blame^] | 571 | static int zfcp_nameserver_enqueue(struct zfcp_adapter *adapter) |
| 572 | { |
| 573 | struct zfcp_port *port; |
| 574 | |
| 575 | port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA, |
| 576 | ZFCP_DID_DIRECTORY_SERVICE); |
| 577 | if (!port) |
| 578 | return -ENXIO; |
| 579 | zfcp_port_put(port); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | /* |
| 585 | * Enqueues an adapter at the end of the adapter list in the driver data. |
| 586 | * All adapter internal structures are set up. |
| 587 | * Proc-fs entries are also created. |
| 588 | * |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 589 | * FIXME: Use -ENOMEM as return code for allocation failures |
| 590 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | * returns: 0 if a new adapter was successfully enqueued |
| 592 | * ZFCP_KNOWN if an adapter with this devno was already present |
| 593 | * -ENOMEM if alloc failed |
| 594 | * locks: config_sema must be held to serialise changes to the adapter list |
| 595 | */ |
| 596 | struct zfcp_adapter * |
| 597 | zfcp_adapter_enqueue(struct ccw_device *ccw_device) |
| 598 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | struct zfcp_adapter *adapter; |
| 600 | |
| 601 | /* |
Swen Schillig | 41fa2ad | 2007-09-07 09:15:31 +0200 | [diff] [blame] | 602 | * Note: It is safe to release the list_lock, as any list changes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | * are protected by the config_sema, which must be held to get here |
| 604 | */ |
| 605 | |
| 606 | /* try to allocate new adapter data structure (zeroed) */ |
Andreas Herrmann | ec4081c | 2006-05-22 18:17:30 +0200 | [diff] [blame] | 607 | adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL); |
Christof Schmitt | 553448f | 2008-06-10 18:20:58 +0200 | [diff] [blame] | 608 | if (!adapter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | ccw_device->handler = NULL; |
| 612 | |
| 613 | /* save ccw_device pointer */ |
| 614 | adapter->ccw_device = ccw_device; |
| 615 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 616 | if (zfcp_qdio_allocate(adapter)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | goto qdio_allocate_failed; |
| 618 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 619 | if (zfcp_allocate_low_mem_buffers(adapter)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | goto failed_low_mem_buffers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | /* initialise reference count stuff */ |
| 623 | atomic_set(&adapter->refcount, 0); |
| 624 | init_waitqueue_head(&adapter->remove_wq); |
| 625 | |
| 626 | /* initialise list of ports */ |
| 627 | INIT_LIST_HEAD(&adapter->port_list_head); |
| 628 | |
| 629 | /* initialise list of ports to be removed */ |
| 630 | INIT_LIST_HEAD(&adapter->port_remove_lh); |
| 631 | |
| 632 | /* initialize list of fsf requests */ |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 633 | spin_lock_init(&adapter->req_list_lock); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 634 | if (zfcp_reqlist_alloc(adapter)) |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 635 | goto failed_low_mem_buffers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 637 | /* initialize debug locks */ |
| 638 | |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 639 | spin_lock_init(&adapter->hba_dbf_lock); |
| 640 | spin_lock_init(&adapter->san_dbf_lock); |
| 641 | spin_lock_init(&adapter->scsi_dbf_lock); |
Martin Peschke | d79a83d | 2008-03-27 14:22:00 +0100 | [diff] [blame] | 642 | spin_lock_init(&adapter->rec_dbf_lock); |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 643 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 644 | if (zfcp_adapter_debug_register(adapter)) |
Christof Schmitt | ff17a29 | 2007-08-28 09:31:41 +0200 | [diff] [blame] | 645 | goto debug_register_failed; |
| 646 | |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 647 | /* initialize error recovery stuff */ |
| 648 | |
| 649 | rwlock_init(&adapter->erp_lock); |
| 650 | sema_init(&adapter->erp_ready_sem, 0); |
| 651 | INIT_LIST_HEAD(&adapter->erp_ready_head); |
| 652 | INIT_LIST_HEAD(&adapter->erp_running_head); |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /* initialize abort lock */ |
| 655 | rwlock_init(&adapter->abort_lock); |
| 656 | |
| 657 | /* initialise some erp stuff */ |
| 658 | init_waitqueue_head(&adapter->erp_thread_wqh); |
| 659 | init_waitqueue_head(&adapter->erp_done_wqh); |
| 660 | |
| 661 | /* initialize lock of associated request queue */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 662 | rwlock_init(&adapter->req_q.lock); |
Swen Schillig | d26ab06 | 2008-05-19 12:17:37 +0200 | [diff] [blame] | 663 | INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler); |
Swen Schillig | cc8c282 | 2008-06-10 18:21:00 +0200 | [diff] [blame^] | 664 | INIT_WORK(&adapter->scan_work, _zfcp_scan_ports_later); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | /* mark adapter unusable as long as sysfs registration is not complete */ |
| 667 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | dev_set_drvdata(&ccw_device->dev, adapter); |
| 670 | |
| 671 | if (zfcp_sysfs_adapter_create_files(&ccw_device->dev)) |
| 672 | goto sysfs_failed; |
| 673 | |
| 674 | adapter->generic_services.parent = &adapter->ccw_device->dev; |
| 675 | adapter->generic_services.release = zfcp_dummy_release; |
| 676 | snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE, |
| 677 | "generic_services"); |
| 678 | |
| 679 | if (device_register(&adapter->generic_services)) |
| 680 | goto generic_services_failed; |
| 681 | |
| 682 | /* put allocated adapter at list tail */ |
| 683 | write_lock_irq(&zfcp_data.config_lock); |
| 684 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); |
| 685 | list_add_tail(&adapter->list, &zfcp_data.adapter_list_head); |
| 686 | write_unlock_irq(&zfcp_data.config_lock); |
| 687 | |
| 688 | zfcp_data.adapters++; |
| 689 | |
Swen Schillig | cc8c282 | 2008-06-10 18:21:00 +0200 | [diff] [blame^] | 690 | zfcp_nameserver_enqueue(adapter); |
| 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | goto out; |
| 693 | |
| 694 | generic_services_failed: |
| 695 | zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); |
| 696 | sysfs_failed: |
Christof Schmitt | ff17a29 | 2007-08-28 09:31:41 +0200 | [diff] [blame] | 697 | zfcp_adapter_debug_unregister(adapter); |
| 698 | debug_register_failed: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | dev_set_drvdata(&ccw_device->dev, NULL); |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 700 | zfcp_reqlist_free(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | failed_low_mem_buffers: |
| 702 | zfcp_free_low_mem_buffers(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | qdio_allocate_failed: |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 704 | zfcp_qdio_free(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | kfree(adapter); |
| 706 | adapter = NULL; |
| 707 | out: |
| 708 | return adapter; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * returns: 0 - struct zfcp_adapter data structure successfully removed |
| 713 | * !0 - struct zfcp_adapter data structure could not be removed |
| 714 | * (e.g. still used) |
| 715 | * locks: adapter list write lock is assumed to be held by caller |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | */ |
| 717 | void |
| 718 | zfcp_adapter_dequeue(struct zfcp_adapter *adapter) |
| 719 | { |
| 720 | int retval = 0; |
| 721 | unsigned long flags; |
| 722 | |
Swen Schillig | cc8c282 | 2008-06-10 18:21:00 +0200 | [diff] [blame^] | 723 | cancel_work_sync(&adapter->scan_work); |
Swen Schillig | d26ab06 | 2008-05-19 12:17:37 +0200 | [diff] [blame] | 724 | cancel_work_sync(&adapter->stat_work); |
Michael Loehr | 9f28745 | 2007-05-09 11:01:24 +0200 | [diff] [blame] | 725 | zfcp_adapter_scsi_unregister(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | device_unregister(&adapter->generic_services); |
| 727 | zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); |
| 728 | dev_set_drvdata(&adapter->ccw_device->dev, NULL); |
| 729 | /* sanity check: no pending FSF requests */ |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 730 | spin_lock_irqsave(&adapter->req_list_lock, flags); |
| 731 | retval = zfcp_reqlist_isempty(adapter); |
| 732 | spin_unlock_irqrestore(&adapter->req_list_lock, flags); |
| 733 | if (!retval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | retval = -EBUSY; |
| 735 | goto out; |
| 736 | } |
| 737 | |
Christof Schmitt | ff17a29 | 2007-08-28 09:31:41 +0200 | [diff] [blame] | 738 | zfcp_adapter_debug_unregister(adapter); |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | /* remove specified adapter data structure from list */ |
| 741 | write_lock_irq(&zfcp_data.config_lock); |
| 742 | list_del(&adapter->list); |
| 743 | write_unlock_irq(&zfcp_data.config_lock); |
| 744 | |
| 745 | /* decrease number of adapters in list */ |
| 746 | zfcp_data.adapters--; |
| 747 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 748 | zfcp_qdio_free(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
| 750 | zfcp_free_low_mem_buffers(adapter); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 751 | zfcp_reqlist_free(adapter); |
Andreas Herrmann | f6cd94b | 2006-01-05 09:59:34 +0100 | [diff] [blame] | 752 | kfree(adapter->fc_stats); |
| 753 | kfree(adapter->stats_reset_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | kfree(adapter); |
| 755 | out: |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * zfcp_port_enqueue - enqueue port to port list of adapter |
| 761 | * @adapter: adapter where remote port is added |
| 762 | * @wwpn: WWPN of the remote port to be enqueued |
| 763 | * @status: initial status for the port |
| 764 | * @d_id: destination id of the remote port to be enqueued |
| 765 | * Return: pointer to enqueued port on success, NULL on error |
| 766 | * Locks: config_sema must be held to serialize changes to the port list |
| 767 | * |
| 768 | * All port internal structures are set up and the sysfs entry is generated. |
| 769 | * d_id is used to enqueue ports with a well known address like the Directory |
| 770 | * Service for nameserver lookup. |
| 771 | */ |
| 772 | struct zfcp_port * |
| 773 | zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status, |
| 774 | u32 d_id) |
| 775 | { |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 776 | struct zfcp_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | int check_wwpn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | /* |
| 781 | * check that there is no port with this WWPN already in list |
| 782 | */ |
| 783 | if (check_wwpn) { |
| 784 | read_lock_irq(&zfcp_data.config_lock); |
| 785 | port = zfcp_get_port_by_wwpn(adapter, wwpn); |
| 786 | read_unlock_irq(&zfcp_data.config_lock); |
| 787 | if (port) |
| 788 | return NULL; |
| 789 | } |
| 790 | |
Andreas Herrmann | ec4081c | 2006-05-22 18:17:30 +0200 | [diff] [blame] | 791 | port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (!port) |
| 793 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | /* initialise reference count stuff */ |
| 796 | atomic_set(&port->refcount, 0); |
| 797 | init_waitqueue_head(&port->remove_wq); |
| 798 | |
| 799 | INIT_LIST_HEAD(&port->unit_list_head); |
| 800 | INIT_LIST_HEAD(&port->unit_remove_lh); |
| 801 | |
| 802 | port->adapter = adapter; |
| 803 | |
| 804 | if (check_wwpn) |
| 805 | port->wwpn = wwpn; |
| 806 | |
| 807 | atomic_set_mask(status, &port->status); |
| 808 | |
| 809 | /* setup for sysfs registration */ |
| 810 | if (status & ZFCP_STATUS_PORT_WKA) { |
| 811 | switch (d_id) { |
| 812 | case ZFCP_DID_DIRECTORY_SERVICE: |
| 813 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 814 | "directory"); |
| 815 | break; |
| 816 | case ZFCP_DID_MANAGEMENT_SERVICE: |
| 817 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 818 | "management"); |
| 819 | break; |
| 820 | case ZFCP_DID_KEY_DISTRIBUTION_SERVICE: |
| 821 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 822 | "key_distribution"); |
| 823 | break; |
| 824 | case ZFCP_DID_ALIAS_SERVICE: |
| 825 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 826 | "alias"); |
| 827 | break; |
| 828 | case ZFCP_DID_TIME_SERVICE: |
| 829 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 830 | "time"); |
| 831 | break; |
| 832 | default: |
| 833 | kfree(port); |
| 834 | return NULL; |
| 835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | port->sysfs_device.parent = &adapter->generic_services; |
| 837 | } else { |
| 838 | snprintf(port->sysfs_device.bus_id, |
| 839 | BUS_ID_SIZE, "0x%016llx", wwpn); |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 840 | port->sysfs_device.parent = &adapter->ccw_device->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
Swen Schillig | cc8c282 | 2008-06-10 18:21:00 +0200 | [diff] [blame^] | 842 | |
| 843 | port->d_id = d_id; |
| 844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | port->sysfs_device.release = zfcp_sysfs_port_release; |
| 846 | dev_set_drvdata(&port->sysfs_device, port); |
| 847 | |
| 848 | /* mark port unusable as long as sysfs registration is not complete */ |
| 849 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status); |
| 850 | |
| 851 | if (device_register(&port->sysfs_device)) { |
| 852 | kfree(port); |
| 853 | return NULL; |
| 854 | } |
| 855 | |
| 856 | if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) { |
| 857 | device_unregister(&port->sysfs_device); |
| 858 | return NULL; |
| 859 | } |
| 860 | |
| 861 | zfcp_port_get(port); |
| 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | write_lock_irq(&zfcp_data.config_lock); |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 864 | list_add_tail(&port->list, &adapter->port_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status); |
| 866 | atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status); |
| 867 | if (d_id == ZFCP_DID_DIRECTORY_SERVICE) |
| 868 | if (!adapter->nameserver_port) |
| 869 | adapter->nameserver_port = port; |
| 870 | adapter->ports++; |
| 871 | write_unlock_irq(&zfcp_data.config_lock); |
| 872 | |
| 873 | zfcp_adapter_get(adapter); |
| 874 | |
| 875 | return port; |
| 876 | } |
| 877 | |
| 878 | void |
| 879 | zfcp_port_dequeue(struct zfcp_port *port) |
| 880 | { |
| 881 | zfcp_port_wait(port); |
| 882 | write_lock_irq(&zfcp_data.config_lock); |
| 883 | list_del(&port->list); |
| 884 | port->adapter->ports--; |
| 885 | write_unlock_irq(&zfcp_data.config_lock); |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 886 | if (port->rport) |
Heiko Carstens | 20b1730 | 2005-08-28 13:22:37 -0700 | [diff] [blame] | 887 | fc_remote_port_delete(port->rport); |
| 888 | port->rport = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | zfcp_adapter_put(port->adapter); |
| 890 | zfcp_sysfs_port_remove_files(&port->sysfs_device, |
| 891 | atomic_read(&port->status)); |
| 892 | device_unregister(&port->sysfs_device); |
| 893 | } |
| 894 | |
Christof Schmitt | 45633fd | 2008-06-10 18:20:55 +0200 | [diff] [blame] | 895 | void zfcp_sg_free_table(struct scatterlist *sg, int count) |
| 896 | { |
| 897 | int i; |
| 898 | |
| 899 | for (i = 0; i < count; i++, sg++) |
| 900 | if (sg) |
| 901 | free_page((unsigned long) sg_virt(sg)); |
| 902 | else |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | int zfcp_sg_setup_table(struct scatterlist *sg, int count) |
| 907 | { |
| 908 | void *addr; |
| 909 | int i; |
| 910 | |
| 911 | sg_init_table(sg, count); |
| 912 | for (i = 0; i < count; i++, sg++) { |
| 913 | addr = (void *) get_zeroed_page(GFP_KERNEL); |
| 914 | if (!addr) { |
| 915 | zfcp_sg_free_table(sg, i); |
| 916 | return -ENOMEM; |
| 917 | } |
| 918 | sg_set_buf(sg, addr, PAGE_SIZE); |
| 919 | } |
| 920 | return 0; |
| 921 | } |