Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * | |
3 | * linux/drivers/s390/scsi/zfcp_aux.c | |
4 | * | |
5 | * FCP adapter driver for IBM eServer zSeries | |
6 | * | |
7 | * (C) Copyright IBM Corp. 2002, 2004 | |
8 | * | |
9 | * Author(s): Martin Peschke <mpeschke@de.ibm.com> | |
10 | * Raimund Schroeder <raimund.schroeder@de.ibm.com> | |
11 | * Aron Zeh | |
12 | * Wolfgang Taphorn | |
13 | * Stefan Bader <stefan.bader@de.ibm.com> | |
14 | * Heiko Carstens <heiko.carstens@de.ibm.com> | |
15 | * Andreas Herrmann <aherrman@de.ibm.com> | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation; either version 2, or (at your option) | |
20 | * any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU General Public License | |
28 | * along with this program; if not, write to the Free Software | |
29 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
30 | */ | |
31 | ||
32 | #define ZFCP_AUX_REVISION "$Revision: 1.145 $" | |
33 | ||
34 | #include "zfcp_ext.h" | |
35 | ||
36 | /* accumulated log level (module parameter) */ | |
37 | static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS; | |
38 | static char *device; | |
39 | /*********************** FUNCTION PROTOTYPES *********************************/ | |
40 | ||
41 | /* written against the module interface */ | |
42 | static int __init zfcp_module_init(void); | |
43 | ||
44 | /* FCP related */ | |
45 | static void zfcp_ns_gid_pn_handler(unsigned long); | |
46 | ||
47 | /* miscellaneous */ | |
48 | static inline int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t); | |
49 | static inline void zfcp_sg_list_free(struct zfcp_sg_list *); | |
50 | static inline int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *, | |
51 | void __user *, size_t); | |
52 | static inline int zfcp_sg_list_copy_to_user(void __user *, | |
53 | struct zfcp_sg_list *, size_t); | |
54 | ||
bd6ae2f6 | 55 | static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long); |
1da177e4 LT |
56 | |
57 | #define ZFCP_CFDC_IOC_MAGIC 0xDD | |
58 | #define ZFCP_CFDC_IOC \ | |
59 | _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data) | |
60 | ||
1da177e4 LT |
61 | |
62 | static struct file_operations zfcp_cfdc_fops = { | |
e183b06b | 63 | .unlocked_ioctl = zfcp_cfdc_dev_ioctl, |
64 | #ifdef CONFIG_COMPAT | |
65 | .compat_ioctl = zfcp_cfdc_dev_ioctl | |
66 | #endif | |
1da177e4 LT |
67 | }; |
68 | ||
69 | static struct miscdevice zfcp_cfdc_misc = { | |
70 | .minor = ZFCP_CFDC_DEV_MINOR, | |
71 | .name = ZFCP_CFDC_DEV_NAME, | |
72 | .fops = &zfcp_cfdc_fops | |
73 | }; | |
74 | ||
75 | /*********************** KERNEL/MODULE PARAMETERS ***************************/ | |
76 | ||
77 | /* declare driver module init/cleanup functions */ | |
78 | module_init(zfcp_module_init); | |
79 | ||
80 | MODULE_AUTHOR("Heiko Carstens <heiko.carstens@de.ibm.com>, " | |
81 | "Andreas Herrman <aherrman@de.ibm.com>, " | |
82 | "Martin Peschke <mpeschke@de.ibm.com>, " | |
83 | "Raimund Schroeder <raimund.schroeder@de.ibm.com>, " | |
84 | "Wolfgang Taphorn <taphorn@de.ibm.com>, " | |
85 | "Aron Zeh <arzeh@de.ibm.com>, " | |
86 | "IBM Deutschland Entwicklung GmbH"); | |
87 | MODULE_DESCRIPTION | |
88 | ("FCP (SCSI over Fibre Channel) HBA driver for IBM eServer zSeries"); | |
89 | MODULE_LICENSE("GPL"); | |
90 | ||
6f71d9bc | 91 | module_param(device, charp, 0400); |
1da177e4 LT |
92 | MODULE_PARM_DESC(device, "specify initial device"); |
93 | ||
6f71d9bc | 94 | module_param(loglevel, uint, 0400); |
1da177e4 LT |
95 | MODULE_PARM_DESC(loglevel, |
96 | "log levels, 8 nibbles: " | |
97 | "FC ERP QDIO CIO Config FSF SCSI Other, " | |
98 | "levels: 0=none 1=normal 2=devel 3=trace"); | |
99 | ||
1da177e4 LT |
100 | /****************************************************************/ |
101 | /************** Functions without logging ***********************/ | |
102 | /****************************************************************/ | |
103 | ||
104 | void | |
105 | _zfcp_hex_dump(char *addr, int count) | |
106 | { | |
107 | int i; | |
108 | for (i = 0; i < count; i++) { | |
109 | printk("%02x", addr[i]); | |
110 | if ((i % 4) == 3) | |
111 | printk(" "); | |
112 | if ((i % 32) == 31) | |
113 | printk("\n"); | |
114 | } | |
115 | if (((i-1) % 32) != 31) | |
116 | printk("\n"); | |
117 | } | |
118 | ||
119 | /****************************************************************/ | |
120 | /************** Uncategorised Functions *************************/ | |
121 | /****************************************************************/ | |
122 | ||
123 | #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER | |
124 | ||
125 | static inline int | |
126 | zfcp_fsf_req_is_scsi_cmnd(struct zfcp_fsf_req *fsf_req) | |
127 | { | |
128 | return ((fsf_req->fsf_command == FSF_QTCB_FCP_CMND) && | |
129 | !(fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)); | |
130 | } | |
131 | ||
132 | void | |
133 | zfcp_cmd_dbf_event_fsf(const char *text, struct zfcp_fsf_req *fsf_req, | |
134 | void *add_data, int add_length) | |
135 | { | |
136 | struct zfcp_adapter *adapter = fsf_req->adapter; | |
137 | struct scsi_cmnd *scsi_cmnd; | |
138 | int level = 3; | |
139 | int i; | |
140 | unsigned long flags; | |
141 | ||
142 | spin_lock_irqsave(&adapter->dbf_lock, flags); | |
143 | if (zfcp_fsf_req_is_scsi_cmnd(fsf_req)) { | |
144 | scsi_cmnd = fsf_req->data.send_fcp_command_task.scsi_cmnd; | |
145 | debug_text_event(adapter->cmd_dbf, level, "fsferror"); | |
146 | debug_text_event(adapter->cmd_dbf, level, text); | |
147 | debug_event(adapter->cmd_dbf, level, &fsf_req, | |
148 | sizeof (unsigned long)); | |
149 | debug_event(adapter->cmd_dbf, level, &fsf_req->seq_no, | |
150 | sizeof (u32)); | |
151 | debug_event(adapter->cmd_dbf, level, &scsi_cmnd, | |
152 | sizeof (unsigned long)); | |
153 | debug_event(adapter->cmd_dbf, level, &scsi_cmnd->cmnd, | |
154 | min(ZFCP_CMD_DBF_LENGTH, (int)scsi_cmnd->cmd_len)); | |
155 | for (i = 0; i < add_length; i += ZFCP_CMD_DBF_LENGTH) | |
156 | debug_event(adapter->cmd_dbf, | |
157 | level, | |
158 | (char *) add_data + i, | |
159 | min(ZFCP_CMD_DBF_LENGTH, add_length - i)); | |
160 | } | |
161 | spin_unlock_irqrestore(&adapter->dbf_lock, flags); | |
162 | } | |
163 | ||
164 | /* XXX additionally log unit if available */ | |
165 | /* ---> introduce new parameter for unit, see 2.4 code */ | |
166 | void | |
167 | zfcp_cmd_dbf_event_scsi(const char *text, struct scsi_cmnd *scsi_cmnd) | |
168 | { | |
169 | struct zfcp_adapter *adapter; | |
170 | union zfcp_req_data *req_data; | |
171 | struct zfcp_fsf_req *fsf_req; | |
172 | int level = ((host_byte(scsi_cmnd->result) != 0) ? 1 : 5); | |
173 | unsigned long flags; | |
174 | ||
175 | adapter = (struct zfcp_adapter *) scsi_cmnd->device->host->hostdata[0]; | |
176 | req_data = (union zfcp_req_data *) scsi_cmnd->host_scribble; | |
177 | fsf_req = (req_data ? req_data->send_fcp_command_task.fsf_req : NULL); | |
178 | spin_lock_irqsave(&adapter->dbf_lock, flags); | |
179 | debug_text_event(adapter->cmd_dbf, level, "hostbyte"); | |
180 | debug_text_event(adapter->cmd_dbf, level, text); | |
181 | debug_event(adapter->cmd_dbf, level, &scsi_cmnd->result, sizeof (u32)); | |
182 | debug_event(adapter->cmd_dbf, level, &scsi_cmnd, | |
183 | sizeof (unsigned long)); | |
184 | debug_event(adapter->cmd_dbf, level, &scsi_cmnd->cmnd, | |
185 | min(ZFCP_CMD_DBF_LENGTH, (int)scsi_cmnd->cmd_len)); | |
186 | if (likely(fsf_req)) { | |
187 | debug_event(adapter->cmd_dbf, level, &fsf_req, | |
188 | sizeof (unsigned long)); | |
189 | debug_event(adapter->cmd_dbf, level, &fsf_req->seq_no, | |
190 | sizeof (u32)); | |
191 | } else { | |
192 | debug_text_event(adapter->cmd_dbf, level, ""); | |
193 | debug_text_event(adapter->cmd_dbf, level, ""); | |
194 | } | |
195 | spin_unlock_irqrestore(&adapter->dbf_lock, flags); | |
196 | } | |
197 | ||
198 | void | |
199 | zfcp_in_els_dbf_event(struct zfcp_adapter *adapter, const char *text, | |
200 | struct fsf_status_read_buffer *status_buffer, int length) | |
201 | { | |
202 | int level = 1; | |
203 | int i; | |
204 | ||
205 | debug_text_event(adapter->in_els_dbf, level, text); | |
206 | debug_event(adapter->in_els_dbf, level, &status_buffer->d_id, 8); | |
207 | for (i = 0; i < length; i += ZFCP_IN_ELS_DBF_LENGTH) | |
208 | debug_event(adapter->in_els_dbf, | |
209 | level, | |
210 | (char *) status_buffer->payload + i, | |
211 | min(ZFCP_IN_ELS_DBF_LENGTH, length - i)); | |
212 | } | |
213 | ||
214 | /** | |
215 | * zfcp_device_setup - setup function | |
216 | * @str: pointer to parameter string | |
217 | * | |
218 | * Parse "device=..." parameter string. | |
219 | */ | |
220 | static int __init | |
221 | zfcp_device_setup(char *str) | |
222 | { | |
223 | char *tmp; | |
224 | ||
225 | if (!str) | |
226 | return 0; | |
227 | ||
228 | tmp = strchr(str, ','); | |
229 | if (!tmp) | |
230 | goto err_out; | |
231 | *tmp++ = '\0'; | |
232 | strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE); | |
233 | zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0'; | |
234 | ||
235 | zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0); | |
236 | if (*tmp++ != ',') | |
237 | goto err_out; | |
238 | if (*tmp == '\0') | |
239 | goto err_out; | |
240 | ||
241 | zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0); | |
242 | if (*tmp != '\0') | |
243 | goto err_out; | |
244 | return 1; | |
245 | ||
246 | err_out: | |
247 | ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | static void __init | |
252 | zfcp_init_device_configure(void) | |
253 | { | |
254 | struct zfcp_adapter *adapter; | |
255 | struct zfcp_port *port; | |
256 | struct zfcp_unit *unit; | |
257 | ||
258 | down(&zfcp_data.config_sema); | |
259 | read_lock_irq(&zfcp_data.config_lock); | |
260 | adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid); | |
261 | if (adapter) | |
262 | zfcp_adapter_get(adapter); | |
263 | read_unlock_irq(&zfcp_data.config_lock); | |
264 | ||
265 | if (adapter == NULL) | |
266 | goto out_adapter; | |
267 | port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0); | |
268 | if (!port) | |
269 | goto out_port; | |
270 | unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun); | |
271 | if (!unit) | |
272 | goto out_unit; | |
273 | up(&zfcp_data.config_sema); | |
274 | ccw_device_set_online(adapter->ccw_device); | |
275 | zfcp_erp_wait(adapter); | |
276 | down(&zfcp_data.config_sema); | |
277 | zfcp_unit_put(unit); | |
278 | out_unit: | |
279 | zfcp_port_put(port); | |
280 | out_port: | |
281 | zfcp_adapter_put(adapter); | |
282 | out_adapter: | |
283 | up(&zfcp_data.config_sema); | |
284 | return; | |
285 | } | |
286 | ||
287 | static int __init | |
288 | zfcp_module_init(void) | |
289 | { | |
290 | ||
291 | int retval = 0; | |
292 | ||
293 | atomic_set(&zfcp_data.loglevel, loglevel); | |
294 | ||
295 | /* initialize adapter list */ | |
296 | INIT_LIST_HEAD(&zfcp_data.adapter_list_head); | |
297 | ||
298 | /* initialize adapters to be removed list head */ | |
299 | INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh); | |
300 | ||
301 | zfcp_transport_template = fc_attach_transport(&zfcp_transport_functions); | |
302 | if (!zfcp_transport_template) | |
303 | return -ENODEV; | |
304 | ||
1da177e4 LT |
305 | retval = misc_register(&zfcp_cfdc_misc); |
306 | if (retval != 0) { | |
307 | ZFCP_LOG_INFO("registration of misc device " | |
308 | "zfcp_cfdc failed\n"); | |
e183b06b | 309 | goto out; |
1da177e4 LT |
310 | } |
311 | ||
e183b06b | 312 | ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n", |
313 | ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor); | |
314 | ||
1da177e4 LT |
315 | /* Initialise proc semaphores */ |
316 | sema_init(&zfcp_data.config_sema, 1); | |
317 | ||
318 | /* initialise configuration rw lock */ | |
319 | rwlock_init(&zfcp_data.config_lock); | |
320 | ||
321 | /* save address of data structure managing the driver module */ | |
322 | zfcp_data.scsi_host_template.module = THIS_MODULE; | |
323 | ||
324 | /* setup dynamic I/O */ | |
325 | retval = zfcp_ccw_register(); | |
326 | if (retval) { | |
327 | ZFCP_LOG_NORMAL("registration with common I/O layer failed\n"); | |
328 | goto out_ccw_register; | |
329 | } | |
330 | ||
331 | if (zfcp_device_setup(device)) | |
332 | zfcp_init_device_configure(); | |
333 | ||
334 | goto out; | |
335 | ||
336 | out_ccw_register: | |
337 | misc_deregister(&zfcp_cfdc_misc); | |
1da177e4 LT |
338 | out: |
339 | return retval; | |
340 | } | |
341 | ||
342 | /* | |
343 | * function: zfcp_cfdc_dev_ioctl | |
344 | * | |
345 | * purpose: Handle control file upload/download transaction via IOCTL | |
346 | * interface | |
347 | * | |
348 | * returns: 0 - Operation completed successfuly | |
349 | * -ENOTTY - Unknown IOCTL command | |
350 | * -EINVAL - Invalid sense data record | |
351 | * -ENXIO - The FCP adapter is not available | |
352 | * -EOPNOTSUPP - The FCP adapter does not have CFDC support | |
353 | * -ENOMEM - Insufficient memory | |
354 | * -EFAULT - User space memory I/O operation fault | |
355 | * -EPERM - Cannot create or queue FSF request or create SBALs | |
356 | * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS) | |
357 | */ | |
e183b06b | 358 | static long |
359 | zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command, | |
360 | unsigned long buffer) | |
1da177e4 LT |
361 | { |
362 | struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user; | |
363 | struct zfcp_adapter *adapter = NULL; | |
364 | struct zfcp_fsf_req *fsf_req = NULL; | |
365 | struct zfcp_sg_list *sg_list = NULL; | |
366 | u32 fsf_command, option; | |
367 | char *bus_id = NULL; | |
368 | int retval = 0; | |
369 | ||
370 | sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL); | |
371 | if (sense_data == NULL) { | |
372 | retval = -ENOMEM; | |
373 | goto out; | |
374 | } | |
375 | ||
376 | sg_list = kmalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL); | |
377 | if (sg_list == NULL) { | |
378 | retval = -ENOMEM; | |
379 | goto out; | |
380 | } | |
381 | memset(sg_list, 0, sizeof(*sg_list)); | |
382 | ||
383 | if (command != ZFCP_CFDC_IOC) { | |
384 | ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command); | |
385 | retval = -ENOTTY; | |
386 | goto out; | |
387 | } | |
388 | ||
389 | if ((sense_data_user = (void __user *) buffer) == NULL) { | |
390 | ZFCP_LOG_INFO("sense data record is required\n"); | |
391 | retval = -EINVAL; | |
392 | goto out; | |
393 | } | |
394 | ||
395 | retval = copy_from_user(sense_data, sense_data_user, | |
396 | sizeof(struct zfcp_cfdc_sense_data)); | |
397 | if (retval) { | |
398 | retval = -EFAULT; | |
399 | goto out; | |
400 | } | |
401 | ||
402 | if (sense_data->signature != ZFCP_CFDC_SIGNATURE) { | |
403 | ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n", | |
404 | ZFCP_CFDC_SIGNATURE); | |
405 | retval = -EINVAL; | |
406 | goto out; | |
407 | } | |
408 | ||
409 | switch (sense_data->command) { | |
410 | ||
411 | case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL: | |
412 | fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE; | |
413 | option = FSF_CFDC_OPTION_NORMAL_MODE; | |
414 | break; | |
415 | ||
416 | case ZFCP_CFDC_CMND_DOWNLOAD_FORCE: | |
417 | fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE; | |
418 | option = FSF_CFDC_OPTION_FORCE; | |
419 | break; | |
420 | ||
421 | case ZFCP_CFDC_CMND_FULL_ACCESS: | |
422 | fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE; | |
423 | option = FSF_CFDC_OPTION_FULL_ACCESS; | |
424 | break; | |
425 | ||
426 | case ZFCP_CFDC_CMND_RESTRICTED_ACCESS: | |
427 | fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE; | |
428 | option = FSF_CFDC_OPTION_RESTRICTED_ACCESS; | |
429 | break; | |
430 | ||
431 | case ZFCP_CFDC_CMND_UPLOAD: | |
432 | fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE; | |
433 | option = 0; | |
434 | break; | |
435 | ||
436 | default: | |
437 | ZFCP_LOG_INFO("invalid command code 0x%08x\n", | |
438 | sense_data->command); | |
439 | retval = -EINVAL; | |
440 | goto out; | |
441 | } | |
442 | ||
443 | bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL); | |
444 | if (bus_id == NULL) { | |
445 | retval = -ENOMEM; | |
446 | goto out; | |
447 | } | |
448 | snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x", | |
449 | (sense_data->devno >> 24), | |
450 | (sense_data->devno >> 16) & 0xFF, | |
451 | (sense_data->devno & 0xFFFF)); | |
452 | ||
453 | read_lock_irq(&zfcp_data.config_lock); | |
454 | adapter = zfcp_get_adapter_by_busid(bus_id); | |
455 | if (adapter) | |
456 | zfcp_adapter_get(adapter); | |
457 | read_unlock_irq(&zfcp_data.config_lock); | |
458 | ||
459 | kfree(bus_id); | |
460 | ||
461 | if (adapter == NULL) { | |
462 | ZFCP_LOG_INFO("invalid adapter\n"); | |
463 | retval = -ENXIO; | |
464 | goto out; | |
465 | } | |
466 | ||
467 | if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) { | |
468 | retval = zfcp_sg_list_alloc(sg_list, | |
469 | ZFCP_CFDC_MAX_CONTROL_FILE_SIZE); | |
470 | if (retval) { | |
471 | retval = -ENOMEM; | |
472 | goto out; | |
473 | } | |
474 | } | |
475 | ||
476 | if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) && | |
477 | (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) { | |
478 | retval = zfcp_sg_list_copy_from_user( | |
479 | sg_list, &sense_data_user->control_file, | |
480 | ZFCP_CFDC_MAX_CONTROL_FILE_SIZE); | |
481 | if (retval) { | |
482 | retval = -EFAULT; | |
483 | goto out; | |
484 | } | |
485 | } | |
486 | ||
487 | retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command, | |
488 | option, sg_list); | |
489 | if (retval) | |
490 | goto out; | |
491 | ||
492 | if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) && | |
493 | (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) { | |
494 | retval = -ENXIO; | |
495 | goto out; | |
496 | } | |
497 | ||
498 | sense_data->fsf_status = fsf_req->qtcb->header.fsf_status; | |
499 | memcpy(&sense_data->fsf_status_qual, | |
500 | &fsf_req->qtcb->header.fsf_status_qual, | |
501 | sizeof(union fsf_status_qual)); | |
502 | memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256); | |
503 | ||
504 | retval = copy_to_user(sense_data_user, sense_data, | |
505 | sizeof(struct zfcp_cfdc_sense_data)); | |
506 | if (retval) { | |
507 | retval = -EFAULT; | |
508 | goto out; | |
509 | } | |
510 | ||
511 | if (sense_data->command & ZFCP_CFDC_UPLOAD) { | |
512 | retval = zfcp_sg_list_copy_to_user( | |
513 | &sense_data_user->control_file, sg_list, | |
514 | ZFCP_CFDC_MAX_CONTROL_FILE_SIZE); | |
515 | if (retval) { | |
516 | retval = -EFAULT; | |
517 | goto out; | |
518 | } | |
519 | } | |
520 | ||
521 | out: | |
522 | if (fsf_req != NULL) | |
1db2c9c0 | 523 | zfcp_fsf_req_free(fsf_req); |
1da177e4 LT |
524 | |
525 | if ((adapter != NULL) && (retval != -ENXIO)) | |
526 | zfcp_adapter_put(adapter); | |
527 | ||
528 | if (sg_list != NULL) { | |
529 | zfcp_sg_list_free(sg_list); | |
530 | kfree(sg_list); | |
531 | } | |
532 | ||
533 | if (sense_data != NULL) | |
534 | kfree(sense_data); | |
535 | ||
536 | return retval; | |
537 | } | |
538 | ||
539 | ||
540 | /** | |
541 | * zfcp_sg_list_alloc - create a scatter-gather list of the specified size | |
542 | * @sg_list: structure describing a scatter gather list | |
543 | * @size: size of scatter-gather list | |
544 | * Return: 0 on success, else -ENOMEM | |
545 | * | |
546 | * In sg_list->sg a pointer to the created scatter-gather list is returned, | |
547 | * or NULL if we run out of memory. sg_list->count specifies the number of | |
548 | * elements of the scatter-gather list. The maximum size of a single element | |
549 | * in the scatter-gather list is PAGE_SIZE. | |
550 | */ | |
551 | static inline int | |
552 | zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size) | |
553 | { | |
554 | struct scatterlist *sg; | |
555 | unsigned int i; | |
556 | int retval = 0; | |
557 | void *address; | |
558 | ||
559 | BUG_ON(sg_list == NULL); | |
560 | ||
561 | sg_list->count = size >> PAGE_SHIFT; | |
562 | if (size & ~PAGE_MASK) | |
563 | sg_list->count++; | |
564 | sg_list->sg = kmalloc(sg_list->count * sizeof(struct scatterlist), | |
565 | GFP_KERNEL); | |
566 | if (sg_list->sg == NULL) { | |
567 | sg_list->count = 0; | |
568 | retval = -ENOMEM; | |
569 | goto out; | |
570 | } | |
571 | memset(sg_list->sg, 0, sg_list->count * sizeof(struct scatterlist)); | |
572 | ||
573 | for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) { | |
574 | sg->length = min(size, PAGE_SIZE); | |
575 | sg->offset = 0; | |
576 | address = (void *) get_zeroed_page(GFP_KERNEL); | |
577 | if (address == NULL) { | |
578 | sg_list->count = i; | |
579 | zfcp_sg_list_free(sg_list); | |
580 | retval = -ENOMEM; | |
581 | goto out; | |
582 | } | |
583 | zfcp_address_to_sg(address, sg); | |
584 | size -= sg->length; | |
585 | } | |
586 | ||
587 | out: | |
588 | return retval; | |
589 | } | |
590 | ||
591 | ||
592 | /** | |
593 | * zfcp_sg_list_free - free memory of a scatter-gather list | |
594 | * @sg_list: structure describing a scatter-gather list | |
595 | * | |
596 | * Memory for each element in the scatter-gather list is freed. | |
597 | * Finally sg_list->sg is freed itself and sg_list->count is reset. | |
598 | */ | |
599 | static inline void | |
600 | zfcp_sg_list_free(struct zfcp_sg_list *sg_list) | |
601 | { | |
602 | struct scatterlist *sg; | |
603 | unsigned int i; | |
604 | ||
605 | BUG_ON(sg_list == NULL); | |
606 | ||
607 | for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) | |
608 | free_page((unsigned long) zfcp_sg_to_address(sg)); | |
609 | ||
610 | sg_list->count = 0; | |
611 | kfree(sg_list->sg); | |
612 | } | |
613 | ||
614 | /** | |
615 | * zfcp_sg_size - determine size of a scatter-gather list | |
616 | * @sg: array of (struct scatterlist) | |
617 | * @sg_count: elements in array | |
618 | * Return: size of entire scatter-gather list | |
619 | */ | |
620 | size_t | |
621 | zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count) | |
622 | { | |
623 | unsigned int i; | |
624 | struct scatterlist *p; | |
625 | size_t size; | |
626 | ||
627 | size = 0; | |
628 | for (i = 0, p = sg; i < sg_count; i++, p++) { | |
629 | BUG_ON(p == NULL); | |
630 | size += p->length; | |
631 | } | |
632 | ||
633 | return size; | |
634 | } | |
635 | ||
636 | ||
637 | /** | |
638 | * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list | |
639 | * @sg_list: structure describing a scatter-gather list | |
640 | * @user_buffer: pointer to buffer in user space | |
641 | * @size: number of bytes to be copied | |
642 | * Return: 0 on success, -EFAULT if copy_from_user fails. | |
643 | */ | |
644 | static inline int | |
645 | zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list, | |
646 | void __user *user_buffer, | |
647 | size_t size) | |
648 | { | |
649 | struct scatterlist *sg; | |
650 | unsigned int length; | |
651 | void *zfcp_buffer; | |
652 | int retval = 0; | |
653 | ||
654 | BUG_ON(sg_list == NULL); | |
655 | ||
656 | if (zfcp_sg_size(sg_list->sg, sg_list->count) < size) | |
657 | return -EFAULT; | |
658 | ||
659 | for (sg = sg_list->sg; size > 0; sg++) { | |
660 | length = min((unsigned int)size, sg->length); | |
661 | zfcp_buffer = zfcp_sg_to_address(sg); | |
662 | if (copy_from_user(zfcp_buffer, user_buffer, length)) { | |
663 | retval = -EFAULT; | |
664 | goto out; | |
665 | } | |
666 | user_buffer += length; | |
667 | size -= length; | |
668 | } | |
669 | ||
670 | out: | |
671 | return retval; | |
672 | } | |
673 | ||
674 | ||
675 | /** | |
676 | * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space | |
677 | * @user_buffer: pointer to buffer in user space | |
678 | * @sg_list: structure describing a scatter-gather list | |
679 | * @size: number of bytes to be copied | |
680 | * Return: 0 on success, -EFAULT if copy_to_user fails | |
681 | */ | |
682 | static inline int | |
683 | zfcp_sg_list_copy_to_user(void __user *user_buffer, | |
684 | struct zfcp_sg_list *sg_list, | |
685 | size_t size) | |
686 | { | |
687 | struct scatterlist *sg; | |
688 | unsigned int length; | |
689 | void *zfcp_buffer; | |
690 | int retval = 0; | |
691 | ||
692 | BUG_ON(sg_list == NULL); | |
693 | ||
694 | if (zfcp_sg_size(sg_list->sg, sg_list->count) < size) | |
695 | return -EFAULT; | |
696 | ||
697 | for (sg = sg_list->sg; size > 0; sg++) { | |
698 | length = min((unsigned int) size, sg->length); | |
699 | zfcp_buffer = zfcp_sg_to_address(sg); | |
700 | if (copy_to_user(user_buffer, zfcp_buffer, length)) { | |
701 | retval = -EFAULT; | |
702 | goto out; | |
703 | } | |
704 | user_buffer += length; | |
705 | size -= length; | |
706 | } | |
707 | ||
708 | out: | |
709 | return retval; | |
710 | } | |
711 | ||
712 | ||
713 | #undef ZFCP_LOG_AREA | |
714 | ||
715 | /****************************************************************/ | |
716 | /****** Functions for configuration/set-up of structures ********/ | |
717 | /****************************************************************/ | |
718 | ||
719 | #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG | |
720 | ||
721 | /** | |
722 | * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN | |
723 | * @port: pointer to port to search for unit | |
724 | * @fcp_lun: FCP LUN to search for | |
725 | * Traverse list of all units of a port and return pointer to a unit | |
726 | * with the given FCP LUN. | |
727 | */ | |
728 | struct zfcp_unit * | |
729 | zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun) | |
730 | { | |
731 | struct zfcp_unit *unit; | |
732 | int found = 0; | |
733 | ||
734 | list_for_each_entry(unit, &port->unit_list_head, list) { | |
735 | if ((unit->fcp_lun == fcp_lun) && | |
736 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status)) | |
737 | { | |
738 | found = 1; | |
739 | break; | |
740 | } | |
741 | } | |
742 | return found ? unit : NULL; | |
743 | } | |
744 | ||
745 | /** | |
746 | * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn | |
747 | * @adapter: pointer to adapter to search for port | |
748 | * @wwpn: wwpn to search for | |
749 | * Traverse list of all ports of an adapter and return pointer to a port | |
750 | * with the given wwpn. | |
751 | */ | |
752 | struct zfcp_port * | |
753 | zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn) | |
754 | { | |
755 | struct zfcp_port *port; | |
756 | int found = 0; | |
757 | ||
758 | list_for_each_entry(port, &adapter->port_list_head, list) { | |
759 | if ((port->wwpn == wwpn) && | |
760 | !(atomic_read(&port->status) & | |
761 | (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) { | |
762 | found = 1; | |
763 | break; | |
764 | } | |
765 | } | |
766 | return found ? port : NULL; | |
767 | } | |
768 | ||
769 | /** | |
770 | * zfcp_get_port_by_did - find port in port list of adapter by d_id | |
771 | * @adapter: pointer to adapter to search for port | |
772 | * @d_id: d_id to search for | |
773 | * Traverse list of all ports of an adapter and return pointer to a port | |
774 | * with the given d_id. | |
775 | */ | |
776 | struct zfcp_port * | |
777 | zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id) | |
778 | { | |
779 | struct zfcp_port *port; | |
780 | int found = 0; | |
781 | ||
782 | list_for_each_entry(port, &adapter->port_list_head, list) { | |
783 | if ((port->d_id == d_id) && | |
784 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) | |
785 | { | |
786 | found = 1; | |
787 | break; | |
788 | } | |
789 | } | |
790 | return found ? port : NULL; | |
791 | } | |
792 | ||
793 | /** | |
794 | * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id | |
795 | * @bus_id: bus_id to search for | |
796 | * Traverse list of all adapters and return pointer to an adapter | |
797 | * with the given bus_id. | |
798 | */ | |
799 | struct zfcp_adapter * | |
800 | zfcp_get_adapter_by_busid(char *bus_id) | |
801 | { | |
802 | struct zfcp_adapter *adapter; | |
803 | int found = 0; | |
804 | ||
805 | list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) { | |
806 | if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter), | |
807 | BUS_ID_SIZE) == 0) && | |
808 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, | |
809 | &adapter->status)){ | |
810 | found = 1; | |
811 | break; | |
812 | } | |
813 | } | |
814 | return found ? adapter : NULL; | |
815 | } | |
816 | ||
817 | /** | |
818 | * zfcp_unit_enqueue - enqueue unit to unit list of a port. | |
819 | * @port: pointer to port where unit is added | |
820 | * @fcp_lun: FCP LUN of unit to be enqueued | |
821 | * Return: pointer to enqueued unit on success, NULL on error | |
822 | * Locks: config_sema must be held to serialize changes to the unit list | |
823 | * | |
824 | * Sets up some unit internal structures and creates sysfs entry. | |
825 | */ | |
826 | struct zfcp_unit * | |
827 | zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun) | |
828 | { | |
829 | struct zfcp_unit *unit, *tmp_unit; | |
830 | scsi_lun_t scsi_lun; | |
831 | int found; | |
832 | ||
833 | /* | |
834 | * check that there is no unit with this FCP_LUN already in list | |
835 | * and enqueue it. | |
836 | * Note: Unlike for the adapter and the port, this is an error | |
837 | */ | |
838 | read_lock_irq(&zfcp_data.config_lock); | |
839 | unit = zfcp_get_unit_by_lun(port, fcp_lun); | |
840 | read_unlock_irq(&zfcp_data.config_lock); | |
841 | if (unit) | |
842 | return NULL; | |
843 | ||
844 | unit = kmalloc(sizeof (struct zfcp_unit), GFP_KERNEL); | |
845 | if (!unit) | |
846 | return NULL; | |
847 | memset(unit, 0, sizeof (struct zfcp_unit)); | |
848 | ||
849 | /* initialise reference count stuff */ | |
850 | atomic_set(&unit->refcount, 0); | |
851 | init_waitqueue_head(&unit->remove_wq); | |
852 | ||
853 | unit->port = port; | |
854 | unit->fcp_lun = fcp_lun; | |
855 | ||
856 | /* setup for sysfs registration */ | |
857 | snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun); | |
858 | unit->sysfs_device.parent = &port->sysfs_device; | |
859 | unit->sysfs_device.release = zfcp_sysfs_unit_release; | |
860 | dev_set_drvdata(&unit->sysfs_device, unit); | |
861 | ||
862 | /* mark unit unusable as long as sysfs registration is not complete */ | |
863 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); | |
864 | ||
865 | if (device_register(&unit->sysfs_device)) { | |
866 | kfree(unit); | |
867 | return NULL; | |
868 | } | |
869 | ||
870 | if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) { | |
871 | device_unregister(&unit->sysfs_device); | |
872 | return NULL; | |
873 | } | |
874 | ||
875 | zfcp_unit_get(unit); | |
876 | ||
877 | scsi_lun = 0; | |
878 | found = 0; | |
879 | write_lock_irq(&zfcp_data.config_lock); | |
880 | list_for_each_entry(tmp_unit, &port->unit_list_head, list) { | |
881 | if (tmp_unit->scsi_lun != scsi_lun) { | |
882 | found = 1; | |
883 | break; | |
884 | } | |
885 | scsi_lun++; | |
886 | } | |
887 | unit->scsi_lun = scsi_lun; | |
888 | if (found) | |
889 | list_add_tail(&unit->list, &tmp_unit->list); | |
890 | else | |
891 | list_add_tail(&unit->list, &port->unit_list_head); | |
892 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); | |
893 | atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status); | |
894 | write_unlock_irq(&zfcp_data.config_lock); | |
895 | ||
896 | port->units++; | |
897 | zfcp_port_get(port); | |
898 | ||
899 | return unit; | |
900 | } | |
901 | ||
902 | void | |
903 | zfcp_unit_dequeue(struct zfcp_unit *unit) | |
904 | { | |
905 | zfcp_unit_wait(unit); | |
906 | write_lock_irq(&zfcp_data.config_lock); | |
907 | list_del(&unit->list); | |
908 | write_unlock_irq(&zfcp_data.config_lock); | |
909 | unit->port->units--; | |
910 | zfcp_port_put(unit->port); | |
911 | zfcp_sysfs_unit_remove_files(&unit->sysfs_device); | |
912 | device_unregister(&unit->sysfs_device); | |
913 | } | |
914 | ||
915 | static void * | |
916 | zfcp_mempool_alloc(unsigned int __nocast gfp_mask, void *size) | |
917 | { | |
918 | return kmalloc((size_t) size, gfp_mask); | |
919 | } | |
920 | ||
921 | static void | |
922 | zfcp_mempool_free(void *element, void *size) | |
923 | { | |
924 | kfree(element); | |
925 | } | |
926 | ||
927 | /* | |
928 | * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI | |
929 | * commands. | |
930 | * It also genrates fcp-nameserver request/response buffer and unsolicited | |
931 | * status read fsf_req buffers. | |
932 | * | |
933 | * locks: must only be called with zfcp_data.config_sema taken | |
934 | */ | |
935 | static int | |
936 | zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter) | |
937 | { | |
938 | adapter->pool.fsf_req_erp = | |
939 | mempool_create(ZFCP_POOL_FSF_REQ_ERP_NR, | |
940 | zfcp_mempool_alloc, zfcp_mempool_free, (void *) | |
941 | sizeof(struct zfcp_fsf_req_pool_element)); | |
942 | ||
943 | if (NULL == adapter->pool.fsf_req_erp) | |
944 | return -ENOMEM; | |
945 | ||
946 | adapter->pool.fsf_req_scsi = | |
947 | mempool_create(ZFCP_POOL_FSF_REQ_SCSI_NR, | |
948 | zfcp_mempool_alloc, zfcp_mempool_free, (void *) | |
949 | sizeof(struct zfcp_fsf_req_pool_element)); | |
950 | ||
951 | if (NULL == adapter->pool.fsf_req_scsi) | |
952 | return -ENOMEM; | |
953 | ||
954 | adapter->pool.fsf_req_abort = | |
955 | mempool_create(ZFCP_POOL_FSF_REQ_ABORT_NR, | |
956 | zfcp_mempool_alloc, zfcp_mempool_free, (void *) | |
957 | sizeof(struct zfcp_fsf_req_pool_element)); | |
958 | ||
959 | if (NULL == adapter->pool.fsf_req_abort) | |
960 | return -ENOMEM; | |
961 | ||
962 | adapter->pool.fsf_req_status_read = | |
963 | mempool_create(ZFCP_POOL_STATUS_READ_NR, | |
964 | zfcp_mempool_alloc, zfcp_mempool_free, | |
965 | (void *) sizeof(struct zfcp_fsf_req)); | |
966 | ||
967 | if (NULL == adapter->pool.fsf_req_status_read) | |
968 | return -ENOMEM; | |
969 | ||
970 | adapter->pool.data_status_read = | |
971 | mempool_create(ZFCP_POOL_STATUS_READ_NR, | |
972 | zfcp_mempool_alloc, zfcp_mempool_free, | |
973 | (void *) sizeof(struct fsf_status_read_buffer)); | |
974 | ||
975 | if (NULL == adapter->pool.data_status_read) | |
976 | return -ENOMEM; | |
977 | ||
978 | adapter->pool.data_gid_pn = | |
979 | mempool_create(ZFCP_POOL_DATA_GID_PN_NR, | |
980 | zfcp_mempool_alloc, zfcp_mempool_free, (void *) | |
981 | sizeof(struct zfcp_gid_pn_data)); | |
982 | ||
983 | if (NULL == adapter->pool.data_gid_pn) | |
984 | return -ENOMEM; | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
989 | /** | |
990 | * zfcp_free_low_mem_buffers - free memory pools of an adapter | |
991 | * @adapter: pointer to zfcp_adapter for which memory pools should be freed | |
992 | * locking: zfcp_data.config_sema must be held | |
993 | */ | |
994 | static void | |
995 | zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter) | |
996 | { | |
997 | if (adapter->pool.fsf_req_erp) | |
998 | mempool_destroy(adapter->pool.fsf_req_erp); | |
999 | if (adapter->pool.fsf_req_scsi) | |
1000 | mempool_destroy(adapter->pool.fsf_req_scsi); | |
1001 | if (adapter->pool.fsf_req_abort) | |
1002 | mempool_destroy(adapter->pool.fsf_req_abort); | |
1003 | if (adapter->pool.fsf_req_status_read) | |
1004 | mempool_destroy(adapter->pool.fsf_req_status_read); | |
1005 | if (adapter->pool.data_status_read) | |
1006 | mempool_destroy(adapter->pool.data_status_read); | |
1007 | if (adapter->pool.data_gid_pn) | |
1008 | mempool_destroy(adapter->pool.data_gid_pn); | |
1009 | } | |
1010 | ||
1011 | /** | |
1012 | * zfcp_adapter_debug_register - registers debug feature for an adapter | |
1013 | * @adapter: pointer to adapter for which debug features should be registered | |
1014 | * return: -ENOMEM on error, 0 otherwise | |
1015 | */ | |
1016 | int | |
1017 | zfcp_adapter_debug_register(struct zfcp_adapter *adapter) | |
1018 | { | |
1019 | char dbf_name[20]; | |
1020 | ||
1021 | /* debug feature area which records SCSI command failures (hostbyte) */ | |
1022 | spin_lock_init(&adapter->dbf_lock); | |
1023 | ||
1024 | sprintf(dbf_name, ZFCP_CMD_DBF_NAME "%s", | |
1025 | zfcp_get_busid_by_adapter(adapter)); | |
1026 | adapter->cmd_dbf = debug_register(dbf_name, ZFCP_CMD_DBF_INDEX, | |
1027 | ZFCP_CMD_DBF_AREAS, | |
1028 | ZFCP_CMD_DBF_LENGTH); | |
1029 | debug_register_view(adapter->cmd_dbf, &debug_hex_ascii_view); | |
1030 | debug_set_level(adapter->cmd_dbf, ZFCP_CMD_DBF_LEVEL); | |
1031 | ||
1032 | /* debug feature area which records SCSI command aborts */ | |
1033 | sprintf(dbf_name, ZFCP_ABORT_DBF_NAME "%s", | |
1034 | zfcp_get_busid_by_adapter(adapter)); | |
1035 | adapter->abort_dbf = debug_register(dbf_name, ZFCP_ABORT_DBF_INDEX, | |
1036 | ZFCP_ABORT_DBF_AREAS, | |
1037 | ZFCP_ABORT_DBF_LENGTH); | |
1038 | debug_register_view(adapter->abort_dbf, &debug_hex_ascii_view); | |
1039 | debug_set_level(adapter->abort_dbf, ZFCP_ABORT_DBF_LEVEL); | |
1040 | ||
1041 | /* debug feature area which records incoming ELS commands */ | |
1042 | sprintf(dbf_name, ZFCP_IN_ELS_DBF_NAME "%s", | |
1043 | zfcp_get_busid_by_adapter(adapter)); | |
1044 | adapter->in_els_dbf = debug_register(dbf_name, ZFCP_IN_ELS_DBF_INDEX, | |
1045 | ZFCP_IN_ELS_DBF_AREAS, | |
1046 | ZFCP_IN_ELS_DBF_LENGTH); | |
1047 | debug_register_view(adapter->in_els_dbf, &debug_hex_ascii_view); | |
1048 | debug_set_level(adapter->in_els_dbf, ZFCP_IN_ELS_DBF_LEVEL); | |
1049 | ||
1050 | /* debug feature area which records erp events */ | |
1051 | sprintf(dbf_name, ZFCP_ERP_DBF_NAME "%s", | |
1052 | zfcp_get_busid_by_adapter(adapter)); | |
1053 | adapter->erp_dbf = debug_register(dbf_name, ZFCP_ERP_DBF_INDEX, | |
1054 | ZFCP_ERP_DBF_AREAS, | |
1055 | ZFCP_ERP_DBF_LENGTH); | |
1056 | debug_register_view(adapter->erp_dbf, &debug_hex_ascii_view); | |
1057 | debug_set_level(adapter->erp_dbf, ZFCP_ERP_DBF_LEVEL); | |
1058 | ||
1059 | if (!(adapter->cmd_dbf && adapter->abort_dbf && | |
1060 | adapter->in_els_dbf && adapter->erp_dbf)) { | |
1061 | zfcp_adapter_debug_unregister(adapter); | |
1062 | return -ENOMEM; | |
1063 | } | |
1064 | ||
1065 | return 0; | |
1066 | ||
1067 | } | |
1068 | ||
1069 | /** | |
1070 | * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter | |
1071 | * @adapter: pointer to adapter for which debug features should be unregistered | |
1072 | */ | |
1073 | void | |
1074 | zfcp_adapter_debug_unregister(struct zfcp_adapter *adapter) | |
1075 | { | |
1076 | debug_unregister(adapter->abort_dbf); | |
1077 | debug_unregister(adapter->cmd_dbf); | |
1078 | debug_unregister(adapter->erp_dbf); | |
1079 | debug_unregister(adapter->in_els_dbf); | |
1080 | adapter->abort_dbf = NULL; | |
1081 | adapter->cmd_dbf = NULL; | |
1082 | adapter->erp_dbf = NULL; | |
1083 | adapter->in_els_dbf = NULL; | |
1084 | } | |
1085 | ||
1086 | void | |
1087 | zfcp_dummy_release(struct device *dev) | |
1088 | { | |
1089 | return; | |
1090 | } | |
1091 | ||
1092 | /* | |
1093 | * Enqueues an adapter at the end of the adapter list in the driver data. | |
1094 | * All adapter internal structures are set up. | |
1095 | * Proc-fs entries are also created. | |
1096 | * | |
1097 | * returns: 0 if a new adapter was successfully enqueued | |
1098 | * ZFCP_KNOWN if an adapter with this devno was already present | |
1099 | * -ENOMEM if alloc failed | |
1100 | * locks: config_sema must be held to serialise changes to the adapter list | |
1101 | */ | |
1102 | struct zfcp_adapter * | |
1103 | zfcp_adapter_enqueue(struct ccw_device *ccw_device) | |
1104 | { | |
1105 | int retval = 0; | |
1106 | struct zfcp_adapter *adapter; | |
1107 | ||
1108 | /* | |
1109 | * Note: It is safe to release the list_lock, as any list changes | |
1110 | * are protected by the config_sema, which must be held to get here | |
1111 | */ | |
1112 | ||
1113 | /* try to allocate new adapter data structure (zeroed) */ | |
1114 | adapter = kmalloc(sizeof (struct zfcp_adapter), GFP_KERNEL); | |
1115 | if (!adapter) { | |
1116 | ZFCP_LOG_INFO("error: allocation of base adapter " | |
1117 | "structure failed\n"); | |
1118 | goto out; | |
1119 | } | |
1120 | memset(adapter, 0, sizeof (struct zfcp_adapter)); | |
1121 | ||
1122 | ccw_device->handler = NULL; | |
1123 | ||
1124 | /* save ccw_device pointer */ | |
1125 | adapter->ccw_device = ccw_device; | |
1126 | ||
1127 | retval = zfcp_qdio_allocate_queues(adapter); | |
1128 | if (retval) | |
1129 | goto queues_alloc_failed; | |
1130 | ||
1131 | retval = zfcp_qdio_allocate(adapter); | |
1132 | if (retval) | |
1133 | goto qdio_allocate_failed; | |
1134 | ||
1135 | retval = zfcp_allocate_low_mem_buffers(adapter); | |
1136 | if (retval) { | |
1137 | ZFCP_LOG_INFO("error: pool allocation failed\n"); | |
1138 | goto failed_low_mem_buffers; | |
1139 | } | |
1140 | ||
1141 | /* initialise reference count stuff */ | |
1142 | atomic_set(&adapter->refcount, 0); | |
1143 | init_waitqueue_head(&adapter->remove_wq); | |
1144 | ||
1145 | /* initialise list of ports */ | |
1146 | INIT_LIST_HEAD(&adapter->port_list_head); | |
1147 | ||
1148 | /* initialise list of ports to be removed */ | |
1149 | INIT_LIST_HEAD(&adapter->port_remove_lh); | |
1150 | ||
1151 | /* initialize list of fsf requests */ | |
1db2c9c0 | 1152 | spin_lock_init(&adapter->fsf_req_list_lock); |
1da177e4 LT |
1153 | INIT_LIST_HEAD(&adapter->fsf_req_list_head); |
1154 | ||
1155 | /* initialize abort lock */ | |
1156 | rwlock_init(&adapter->abort_lock); | |
1157 | ||
1158 | /* initialise some erp stuff */ | |
1159 | init_waitqueue_head(&adapter->erp_thread_wqh); | |
1160 | init_waitqueue_head(&adapter->erp_done_wqh); | |
1161 | ||
1162 | /* initialize lock of associated request queue */ | |
1163 | rwlock_init(&adapter->request_queue.queue_lock); | |
1164 | ||
1165 | /* intitialise SCSI ER timer */ | |
1166 | init_timer(&adapter->scsi_er_timer); | |
1167 | ||
1168 | /* set FC service class used per default */ | |
1169 | adapter->fc_service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT; | |
1170 | ||
1171 | sprintf(adapter->name, "%s", zfcp_get_busid_by_adapter(adapter)); | |
1172 | ASCEBC(adapter->name, strlen(adapter->name)); | |
1173 | ||
1174 | /* mark adapter unusable as long as sysfs registration is not complete */ | |
1175 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); | |
1176 | ||
1177 | adapter->ccw_device = ccw_device; | |
1178 | dev_set_drvdata(&ccw_device->dev, adapter); | |
1179 | ||
1180 | if (zfcp_sysfs_adapter_create_files(&ccw_device->dev)) | |
1181 | goto sysfs_failed; | |
1182 | ||
1183 | adapter->generic_services.parent = &adapter->ccw_device->dev; | |
1184 | adapter->generic_services.release = zfcp_dummy_release; | |
1185 | snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE, | |
1186 | "generic_services"); | |
1187 | ||
1188 | if (device_register(&adapter->generic_services)) | |
1189 | goto generic_services_failed; | |
1190 | ||
1191 | /* put allocated adapter at list tail */ | |
1192 | write_lock_irq(&zfcp_data.config_lock); | |
1193 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); | |
1194 | list_add_tail(&adapter->list, &zfcp_data.adapter_list_head); | |
1195 | write_unlock_irq(&zfcp_data.config_lock); | |
1196 | ||
1197 | zfcp_data.adapters++; | |
1198 | ||
1199 | goto out; | |
1200 | ||
1201 | generic_services_failed: | |
1202 | zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); | |
1203 | sysfs_failed: | |
1204 | dev_set_drvdata(&ccw_device->dev, NULL); | |
1205 | failed_low_mem_buffers: | |
1206 | zfcp_free_low_mem_buffers(adapter); | |
1207 | if (qdio_free(ccw_device) != 0) | |
1208 | ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n", | |
1209 | zfcp_get_busid_by_adapter(adapter)); | |
1210 | qdio_allocate_failed: | |
1211 | zfcp_qdio_free_queues(adapter); | |
1212 | queues_alloc_failed: | |
1213 | kfree(adapter); | |
1214 | adapter = NULL; | |
1215 | out: | |
1216 | return adapter; | |
1217 | } | |
1218 | ||
1219 | /* | |
1220 | * returns: 0 - struct zfcp_adapter data structure successfully removed | |
1221 | * !0 - struct zfcp_adapter data structure could not be removed | |
1222 | * (e.g. still used) | |
1223 | * locks: adapter list write lock is assumed to be held by caller | |
1224 | * adapter->fsf_req_list_lock is taken and released within this | |
1225 | * function and must not be held on entry | |
1226 | */ | |
1227 | void | |
1228 | zfcp_adapter_dequeue(struct zfcp_adapter *adapter) | |
1229 | { | |
1230 | int retval = 0; | |
1231 | unsigned long flags; | |
1232 | ||
1233 | device_unregister(&adapter->generic_services); | |
1234 | zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); | |
1235 | dev_set_drvdata(&adapter->ccw_device->dev, NULL); | |
1236 | /* sanity check: no pending FSF requests */ | |
1db2c9c0 | 1237 | spin_lock_irqsave(&adapter->fsf_req_list_lock, flags); |
1da177e4 | 1238 | retval = !list_empty(&adapter->fsf_req_list_head); |
1db2c9c0 | 1239 | spin_unlock_irqrestore(&adapter->fsf_req_list_lock, flags); |
1da177e4 LT |
1240 | if (retval) { |
1241 | ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, " | |
1242 | "%i requests outstanding\n", | |
1243 | zfcp_get_busid_by_adapter(adapter), adapter, | |
1244 | atomic_read(&adapter->fsf_reqs_active)); | |
1245 | retval = -EBUSY; | |
1246 | goto out; | |
1247 | } | |
1248 | ||
1249 | /* remove specified adapter data structure from list */ | |
1250 | write_lock_irq(&zfcp_data.config_lock); | |
1251 | list_del(&adapter->list); | |
1252 | write_unlock_irq(&zfcp_data.config_lock); | |
1253 | ||
1254 | /* decrease number of adapters in list */ | |
1255 | zfcp_data.adapters--; | |
1256 | ||
1257 | ZFCP_LOG_TRACE("adapter %s (%p) removed from list, " | |
1258 | "%i adapters still in list\n", | |
1259 | zfcp_get_busid_by_adapter(adapter), | |
1260 | adapter, zfcp_data.adapters); | |
1261 | ||
1262 | retval = qdio_free(adapter->ccw_device); | |
1263 | if (retval) | |
1264 | ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n", | |
1265 | zfcp_get_busid_by_adapter(adapter)); | |
1266 | ||
1267 | zfcp_free_low_mem_buffers(adapter); | |
1268 | /* free memory of adapter data structure and queues */ | |
1269 | zfcp_qdio_free_queues(adapter); | |
1270 | ZFCP_LOG_TRACE("freeing adapter structure\n"); | |
1271 | kfree(adapter); | |
1272 | out: | |
1273 | return; | |
1274 | } | |
1275 | ||
1276 | /** | |
1277 | * zfcp_port_enqueue - enqueue port to port list of adapter | |
1278 | * @adapter: adapter where remote port is added | |
1279 | * @wwpn: WWPN of the remote port to be enqueued | |
1280 | * @status: initial status for the port | |
1281 | * @d_id: destination id of the remote port to be enqueued | |
1282 | * Return: pointer to enqueued port on success, NULL on error | |
1283 | * Locks: config_sema must be held to serialize changes to the port list | |
1284 | * | |
1285 | * All port internal structures are set up and the sysfs entry is generated. | |
1286 | * d_id is used to enqueue ports with a well known address like the Directory | |
1287 | * Service for nameserver lookup. | |
1288 | */ | |
1289 | struct zfcp_port * | |
1290 | zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status, | |
1291 | u32 d_id) | |
1292 | { | |
1293 | struct zfcp_port *port, *tmp_port; | |
1294 | int check_wwpn; | |
1295 | scsi_id_t scsi_id; | |
1296 | int found; | |
1297 | ||
1298 | check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN); | |
1299 | ||
1300 | /* | |
1301 | * check that there is no port with this WWPN already in list | |
1302 | */ | |
1303 | if (check_wwpn) { | |
1304 | read_lock_irq(&zfcp_data.config_lock); | |
1305 | port = zfcp_get_port_by_wwpn(adapter, wwpn); | |
1306 | read_unlock_irq(&zfcp_data.config_lock); | |
1307 | if (port) | |
1308 | return NULL; | |
1309 | } | |
1310 | ||
1311 | port = kmalloc(sizeof (struct zfcp_port), GFP_KERNEL); | |
1312 | if (!port) | |
1313 | return NULL; | |
1314 | memset(port, 0, sizeof (struct zfcp_port)); | |
1315 | ||
1316 | /* initialise reference count stuff */ | |
1317 | atomic_set(&port->refcount, 0); | |
1318 | init_waitqueue_head(&port->remove_wq); | |
1319 | ||
1320 | INIT_LIST_HEAD(&port->unit_list_head); | |
1321 | INIT_LIST_HEAD(&port->unit_remove_lh); | |
1322 | ||
1323 | port->adapter = adapter; | |
1324 | ||
1325 | if (check_wwpn) | |
1326 | port->wwpn = wwpn; | |
1327 | ||
1328 | atomic_set_mask(status, &port->status); | |
1329 | ||
1330 | /* setup for sysfs registration */ | |
1331 | if (status & ZFCP_STATUS_PORT_WKA) { | |
1332 | switch (d_id) { | |
1333 | case ZFCP_DID_DIRECTORY_SERVICE: | |
1334 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, | |
1335 | "directory"); | |
1336 | break; | |
1337 | case ZFCP_DID_MANAGEMENT_SERVICE: | |
1338 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, | |
1339 | "management"); | |
1340 | break; | |
1341 | case ZFCP_DID_KEY_DISTRIBUTION_SERVICE: | |
1342 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, | |
1343 | "key_distribution"); | |
1344 | break; | |
1345 | case ZFCP_DID_ALIAS_SERVICE: | |
1346 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, | |
1347 | "alias"); | |
1348 | break; | |
1349 | case ZFCP_DID_TIME_SERVICE: | |
1350 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, | |
1351 | "time"); | |
1352 | break; | |
1353 | default: | |
1354 | kfree(port); | |
1355 | return NULL; | |
1356 | } | |
1357 | port->d_id = d_id; | |
1358 | port->sysfs_device.parent = &adapter->generic_services; | |
1359 | } else { | |
1360 | snprintf(port->sysfs_device.bus_id, | |
1361 | BUS_ID_SIZE, "0x%016llx", wwpn); | |
1362 | port->sysfs_device.parent = &adapter->ccw_device->dev; | |
1363 | } | |
1364 | port->sysfs_device.release = zfcp_sysfs_port_release; | |
1365 | dev_set_drvdata(&port->sysfs_device, port); | |
1366 | ||
1367 | /* mark port unusable as long as sysfs registration is not complete */ | |
1368 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status); | |
1369 | ||
1370 | if (device_register(&port->sysfs_device)) { | |
1371 | kfree(port); | |
1372 | return NULL; | |
1373 | } | |
1374 | ||
1375 | if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) { | |
1376 | device_unregister(&port->sysfs_device); | |
1377 | return NULL; | |
1378 | } | |
1379 | ||
1380 | zfcp_port_get(port); | |
1381 | ||
1382 | scsi_id = 1; | |
1383 | found = 0; | |
1384 | write_lock_irq(&zfcp_data.config_lock); | |
1385 | list_for_each_entry(tmp_port, &adapter->port_list_head, list) { | |
1386 | if (atomic_test_mask(ZFCP_STATUS_PORT_NO_SCSI_ID, | |
1387 | &tmp_port->status)) | |
1388 | continue; | |
1389 | if (tmp_port->scsi_id != scsi_id) { | |
1390 | found = 1; | |
1391 | break; | |
1392 | } | |
1393 | scsi_id++; | |
1394 | } | |
1395 | port->scsi_id = scsi_id; | |
1396 | if (found) | |
1397 | list_add_tail(&port->list, &tmp_port->list); | |
1398 | else | |
1399 | list_add_tail(&port->list, &adapter->port_list_head); | |
1400 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status); | |
1401 | atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status); | |
1402 | if (d_id == ZFCP_DID_DIRECTORY_SERVICE) | |
1403 | if (!adapter->nameserver_port) | |
1404 | adapter->nameserver_port = port; | |
1405 | adapter->ports++; | |
1406 | write_unlock_irq(&zfcp_data.config_lock); | |
1407 | ||
1408 | zfcp_adapter_get(adapter); | |
1409 | ||
1410 | return port; | |
1411 | } | |
1412 | ||
1413 | void | |
1414 | zfcp_port_dequeue(struct zfcp_port *port) | |
1415 | { | |
1416 | zfcp_port_wait(port); | |
1417 | write_lock_irq(&zfcp_data.config_lock); | |
1418 | list_del(&port->list); | |
1419 | port->adapter->ports--; | |
1420 | write_unlock_irq(&zfcp_data.config_lock); | |
1421 | zfcp_adapter_put(port->adapter); | |
1422 | zfcp_sysfs_port_remove_files(&port->sysfs_device, | |
1423 | atomic_read(&port->status)); | |
1424 | device_unregister(&port->sysfs_device); | |
1425 | } | |
1426 | ||
1427 | /* Enqueues a nameserver port */ | |
1428 | int | |
1429 | zfcp_nameserver_enqueue(struct zfcp_adapter *adapter) | |
1430 | { | |
1431 | struct zfcp_port *port; | |
1432 | ||
1433 | port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA, | |
1434 | ZFCP_DID_DIRECTORY_SERVICE); | |
1435 | if (!port) { | |
1436 | ZFCP_LOG_INFO("error: enqueue of nameserver port for " | |
1437 | "adapter %s failed\n", | |
1438 | zfcp_get_busid_by_adapter(adapter)); | |
1439 | return -ENXIO; | |
1440 | } | |
1441 | zfcp_port_put(port); | |
1442 | ||
1443 | return 0; | |
1444 | } | |
1445 | ||
1446 | #undef ZFCP_LOG_AREA | |
1447 | ||
1448 | /****************************************************************/ | |
1449 | /******* Fibre Channel Standard related Functions **************/ | |
1450 | /****************************************************************/ | |
1451 | ||
1452 | #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FC | |
1453 | ||
1454 | void | |
1455 | zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter, | |
1456 | struct fsf_status_read_buffer *status_buffer) | |
1457 | { | |
1458 | struct fcp_rscn_head *fcp_rscn_head; | |
1459 | struct fcp_rscn_element *fcp_rscn_element; | |
1460 | struct zfcp_port *port; | |
1461 | u16 i; | |
1462 | u16 no_entries; | |
1463 | u32 range_mask; | |
1464 | unsigned long flags; | |
1465 | ||
1466 | fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload; | |
1467 | fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload; | |
1468 | ||
1469 | /* see FC-FS */ | |
1470 | no_entries = (fcp_rscn_head->payload_len / 4); | |
1471 | ||
1472 | zfcp_in_els_dbf_event(adapter, "##rscn", status_buffer, | |
1473 | fcp_rscn_head->payload_len); | |
1474 | ||
1475 | debug_text_event(adapter->erp_dbf, 1, "unsol_els_rscn:"); | |
1476 | for (i = 1; i < no_entries; i++) { | |
1477 | /* skip head and start with 1st element */ | |
1478 | fcp_rscn_element++; | |
1479 | switch (fcp_rscn_element->addr_format) { | |
1480 | case ZFCP_PORT_ADDRESS: | |
1da177e4 LT |
1481 | range_mask = ZFCP_PORTS_RANGE_PORT; |
1482 | break; | |
1483 | case ZFCP_AREA_ADDRESS: | |
1da177e4 LT |
1484 | range_mask = ZFCP_PORTS_RANGE_AREA; |
1485 | break; | |
1486 | case ZFCP_DOMAIN_ADDRESS: | |
1da177e4 LT |
1487 | range_mask = ZFCP_PORTS_RANGE_DOMAIN; |
1488 | break; | |
1489 | case ZFCP_FABRIC_ADDRESS: | |
1da177e4 LT |
1490 | range_mask = ZFCP_PORTS_RANGE_FABRIC; |
1491 | break; | |
1492 | default: | |
1493 | ZFCP_LOG_INFO("incoming RSCN with unknown " | |
1494 | "address format\n"); | |
1495 | continue; | |
1496 | } | |
1497 | read_lock_irqsave(&zfcp_data.config_lock, flags); | |
1498 | list_for_each_entry(port, &adapter->port_list_head, list) { | |
1499 | if (atomic_test_mask | |
1500 | (ZFCP_STATUS_PORT_WKA, &port->status)) | |
1501 | continue; | |
1502 | /* Do we know this port? If not skip it. */ | |
1503 | if (!atomic_test_mask | |
1504 | (ZFCP_STATUS_PORT_DID_DID, &port->status)) { | |
1505 | ZFCP_LOG_INFO("incoming RSCN, trying to open " | |
1506 | "port 0x%016Lx\n", port->wwpn); | |
1507 | debug_text_event(adapter->erp_dbf, 1, | |
1508 | "unsol_els_rscnu:"); | |
1509 | zfcp_erp_port_reopen(port, | |
1510 | ZFCP_STATUS_COMMON_ERP_FAILED); | |
1511 | continue; | |
1512 | } | |
1513 | ||
1514 | /* | |
1515 | * FIXME: race: d_id might being invalidated | |
1516 | * (...DID_DID reset) | |
1517 | */ | |
1518 | if ((port->d_id & range_mask) | |
1519 | == (fcp_rscn_element->nport_did & range_mask)) { | |
1520 | ZFCP_LOG_TRACE("reopen did 0x%08x\n", | |
1521 | fcp_rscn_element->nport_did); | |
1522 | /* | |
1523 | * Unfortunately, an RSCN does not specify the | |
1524 | * type of change a target underwent. We assume | |
1525 | * that it makes sense to reopen the link. | |
1526 | * FIXME: Shall we try to find out more about | |
1527 | * the target and link state before closing it? | |
1528 | * How to accomplish this? (nameserver?) | |
1529 | * Where would such code be put in? | |
1530 | * (inside or outside erp) | |
1531 | */ | |
1532 | ZFCP_LOG_INFO("incoming RSCN, trying to open " | |
1533 | "port 0x%016Lx\n", port->wwpn); | |
1534 | debug_text_event(adapter->erp_dbf, 1, | |
1535 | "unsol_els_rscnk:"); | |
1536 | zfcp_test_link(port); | |
1537 | } | |
1538 | } | |
1539 | read_unlock_irqrestore(&zfcp_data.config_lock, flags); | |
1540 | } | |
1541 | } | |
1542 | ||
1543 | static void | |
1544 | zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter, | |
1545 | struct fsf_status_read_buffer *status_buffer) | |
1546 | { | |
1547 | logi *els_logi = (logi *) status_buffer->payload; | |
1548 | struct zfcp_port *port; | |
1549 | unsigned long flags; | |
1550 | ||
1551 | zfcp_in_els_dbf_event(adapter, "##plogi", status_buffer, 28); | |
1552 | ||
1553 | read_lock_irqsave(&zfcp_data.config_lock, flags); | |
1554 | list_for_each_entry(port, &adapter->port_list_head, list) { | |
1555 | if (port->wwpn == (*(wwn_t *) & els_logi->nport_wwn)) | |
1556 | break; | |
1557 | } | |
1558 | read_unlock_irqrestore(&zfcp_data.config_lock, flags); | |
1559 | ||
1560 | if (!port || (port->wwpn != (*(wwn_t *) & els_logi->nport_wwn))) { | |
1561 | ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port " | |
1562 | "with d_id 0x%08x on adapter %s\n", | |
1563 | status_buffer->d_id, | |
1564 | zfcp_get_busid_by_adapter(adapter)); | |
1565 | } else { | |
1566 | debug_text_event(adapter->erp_dbf, 1, "unsol_els_plogi:"); | |
1567 | debug_event(adapter->erp_dbf, 1, &els_logi->nport_wwn, 8); | |
1568 | zfcp_erp_port_forced_reopen(port, 0); | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | static void | |
1573 | zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter, | |
1574 | struct fsf_status_read_buffer *status_buffer) | |
1575 | { | |
1576 | struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload; | |
1577 | struct zfcp_port *port; | |
1578 | unsigned long flags; | |
1579 | ||
1580 | zfcp_in_els_dbf_event(adapter, "##logo", status_buffer, 16); | |
1581 | ||
1582 | read_lock_irqsave(&zfcp_data.config_lock, flags); | |
1583 | list_for_each_entry(port, &adapter->port_list_head, list) { | |
1584 | if (port->wwpn == els_logo->nport_wwpn) | |
1585 | break; | |
1586 | } | |
1587 | read_unlock_irqrestore(&zfcp_data.config_lock, flags); | |
1588 | ||
1589 | if (!port || (port->wwpn != els_logo->nport_wwpn)) { | |
1590 | ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port " | |
1591 | "with d_id 0x%08x on adapter %s\n", | |
1592 | status_buffer->d_id, | |
1593 | zfcp_get_busid_by_adapter(adapter)); | |
1594 | } else { | |
1595 | debug_text_event(adapter->erp_dbf, 1, "unsol_els_logo:"); | |
1596 | debug_event(adapter->erp_dbf, 1, &els_logo->nport_wwpn, 8); | |
1597 | zfcp_erp_port_forced_reopen(port, 0); | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | static void | |
1602 | zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter, | |
1603 | struct fsf_status_read_buffer *status_buffer) | |
1604 | { | |
1605 | zfcp_in_els_dbf_event(adapter, "##undef", status_buffer, 24); | |
1606 | ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x " | |
1607 | "for adapter %s\n", *(u32 *) (status_buffer->payload), | |
1608 | zfcp_get_busid_by_adapter(adapter)); | |
1609 | ||
1610 | } | |
1611 | ||
1612 | void | |
1613 | zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req) | |
1614 | { | |
1615 | struct fsf_status_read_buffer *status_buffer; | |
1616 | u32 els_type; | |
1617 | struct zfcp_adapter *adapter; | |
1618 | ||
1619 | status_buffer = fsf_req->data.status_read.buffer; | |
1620 | els_type = *(u32 *) (status_buffer->payload); | |
1621 | adapter = fsf_req->adapter; | |
1622 | ||
1623 | if (els_type == LS_PLOGI) | |
1624 | zfcp_fsf_incoming_els_plogi(adapter, status_buffer); | |
1625 | else if (els_type == LS_LOGO) | |
1626 | zfcp_fsf_incoming_els_logo(adapter, status_buffer); | |
1627 | else if ((els_type & 0xffff0000) == LS_RSCN) | |
1628 | /* we are only concerned with the command, not the length */ | |
1629 | zfcp_fsf_incoming_els_rscn(adapter, status_buffer); | |
1630 | else | |
1631 | zfcp_fsf_incoming_els_unknown(adapter, status_buffer); | |
1632 | } | |
1633 | ||
1634 | ||
1635 | /** | |
1636 | * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request | |
1637 | * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data | |
1638 | * @pool: pointer to mempool_t if non-null memory pool is used for allocation | |
1639 | */ | |
1640 | static int | |
1641 | zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool) | |
1642 | { | |
1643 | struct zfcp_gid_pn_data *data; | |
1644 | ||
1645 | if (pool != NULL) { | |
1646 | data = mempool_alloc(pool, GFP_ATOMIC); | |
1647 | if (likely(data != NULL)) { | |
1648 | data->ct.pool = pool; | |
1649 | } | |
1650 | } else { | |
1651 | data = kmalloc(sizeof(struct zfcp_gid_pn_data), GFP_ATOMIC); | |
1652 | } | |
1653 | ||
1654 | if (NULL == data) | |
1655 | return -ENOMEM; | |
1656 | ||
1657 | memset(data, 0, sizeof(*data)); | |
1658 | data->ct.req = &data->req; | |
1659 | data->ct.resp = &data->resp; | |
1660 | data->ct.req_count = data->ct.resp_count = 1; | |
1661 | zfcp_address_to_sg(&data->ct_iu_req, &data->req); | |
1662 | zfcp_address_to_sg(&data->ct_iu_resp, &data->resp); | |
1663 | data->req.length = sizeof(struct ct_iu_gid_pn_req); | |
1664 | data->resp.length = sizeof(struct ct_iu_gid_pn_resp); | |
1665 | ||
1666 | *gid_pn = data; | |
1667 | return 0; | |
1668 | } | |
1669 | ||
1670 | /** | |
1671 | * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request | |
1672 | * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed | |
1673 | */ | |
1674 | static void | |
1675 | zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn) | |
1676 | { | |
1677 | if ((gid_pn->ct.pool != 0)) | |
1678 | mempool_free(gid_pn, gid_pn->ct.pool); | |
1679 | else | |
1680 | kfree(gid_pn); | |
1681 | ||
1682 | return; | |
1683 | } | |
1684 | ||
1685 | /** | |
1686 | * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request | |
1687 | * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed | |
1688 | */ | |
1689 | int | |
1690 | zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action) | |
1691 | { | |
1692 | int ret; | |
1693 | struct ct_iu_gid_pn_req *ct_iu_req; | |
1694 | struct zfcp_gid_pn_data *gid_pn; | |
1695 | struct zfcp_adapter *adapter = erp_action->adapter; | |
1696 | ||
1697 | ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn); | |
1698 | if (ret < 0) { | |
1699 | ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver " | |
1700 | "request failed for adapter %s\n", | |
1701 | zfcp_get_busid_by_adapter(adapter)); | |
1702 | goto out; | |
1703 | } | |
1704 | ||
1705 | /* setup nameserver request */ | |
1706 | ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req); | |
1707 | ct_iu_req->header.revision = ZFCP_CT_REVISION; | |
1708 | ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE; | |
1709 | ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER; | |
1710 | ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS; | |
1711 | ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN; | |
1712 | ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE; | |
1713 | ct_iu_req->wwpn = erp_action->port->wwpn; | |
1714 | ||
1715 | /* setup parameters for send generic command */ | |
1716 | gid_pn->ct.port = adapter->nameserver_port; | |
1717 | gid_pn->ct.handler = zfcp_ns_gid_pn_handler; | |
1718 | gid_pn->ct.handler_data = (unsigned long) gid_pn; | |
1719 | gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT; | |
1720 | gid_pn->ct.timer = &erp_action->timer; | |
1721 | gid_pn->port = erp_action->port; | |
1722 | ||
1723 | ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp, | |
1724 | erp_action); | |
1725 | if (ret) { | |
1726 | ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request " | |
1727 | "failed for adapter %s\n", | |
1728 | zfcp_get_busid_by_adapter(adapter)); | |
1729 | ||
1730 | zfcp_gid_pn_buffers_free(gid_pn); | |
1731 | } | |
1732 | ||
1733 | out: | |
1734 | return ret; | |
1735 | } | |
1736 | ||
1737 | /** | |
1738 | * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request | |
1739 | * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data | |
1740 | */ | |
1741 | static void zfcp_ns_gid_pn_handler(unsigned long data) | |
1742 | { | |
1743 | struct zfcp_port *port; | |
1744 | struct zfcp_send_ct *ct; | |
1745 | struct ct_iu_gid_pn_req *ct_iu_req; | |
1746 | struct ct_iu_gid_pn_resp *ct_iu_resp; | |
1747 | struct zfcp_gid_pn_data *gid_pn; | |
1748 | ||
1749 | ||
1750 | gid_pn = (struct zfcp_gid_pn_data *) data; | |
1751 | port = gid_pn->port; | |
1752 | ct = &gid_pn->ct; | |
1753 | ct_iu_req = zfcp_sg_to_address(ct->req); | |
1754 | ct_iu_resp = zfcp_sg_to_address(ct->resp); | |
1755 | ||
66c8684a AH |
1756 | if (ct->status != 0) |
1757 | goto failed; | |
1758 | ||
1759 | if (zfcp_check_ct_response(&ct_iu_resp->header)) { | |
1da177e4 LT |
1760 | /* FIXME: do we need some specific erp entry points */ |
1761 | atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status); | |
1762 | goto failed; | |
1763 | } | |
1764 | /* paranoia */ | |
1765 | if (ct_iu_req->wwpn != port->wwpn) { | |
1766 | ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver " | |
1767 | "lookup does not match expected wwpn 0x%016Lx " | |
1768 | "for adapter %s\n", ct_iu_req->wwpn, port->wwpn, | |
1769 | zfcp_get_busid_by_port(port)); | |
1770 | goto mismatch; | |
1771 | } | |
1772 | ||
1773 | /* looks like a valid d_id */ | |
1774 | port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK; | |
1775 | atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status); | |
1776 | ZFCP_LOG_DEBUG("adapter %s: wwpn=0x%016Lx ---> d_id=0x%08x\n", | |
1777 | zfcp_get_busid_by_port(port), port->wwpn, port->d_id); | |
1778 | goto out; | |
1779 | ||
1780 | mismatch: | |
1781 | ZFCP_LOG_DEBUG("CT IUs do not match:\n"); | |
1782 | ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req, | |
1783 | sizeof(struct ct_iu_gid_pn_req)); | |
1784 | ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp, | |
1785 | sizeof(struct ct_iu_gid_pn_resp)); | |
1786 | ||
1787 | failed: | |
1788 | ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn " | |
1789 | "0x%016Lx for adapter %s\n", | |
1790 | port->wwpn, zfcp_get_busid_by_port(port)); | |
1791 | out: | |
1792 | zfcp_gid_pn_buffers_free(gid_pn); | |
1793 | return; | |
1794 | } | |
1795 | ||
1796 | /* reject CT_IU reason codes acc. to FC-GS-4 */ | |
1797 | static const struct zfcp_rc_entry zfcp_ct_rc[] = { | |
1798 | {0x01, "invalid command code"}, | |
1799 | {0x02, "invalid version level"}, | |
1800 | {0x03, "logical error"}, | |
1801 | {0x04, "invalid CT_IU size"}, | |
1802 | {0x05, "logical busy"}, | |
1803 | {0x07, "protocol error"}, | |
1804 | {0x09, "unable to perform command request"}, | |
1805 | {0x0b, "command not supported"}, | |
1806 | {0x0d, "server not available"}, | |
1807 | {0x0e, "session could not be established"}, | |
1808 | {0xff, "vendor specific error"}, | |
1809 | {0, NULL}, | |
1810 | }; | |
1811 | ||
1812 | /* LS_RJT reason codes acc. to FC-FS */ | |
1813 | static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = { | |
1814 | {0x01, "invalid LS_Command code"}, | |
1815 | {0x03, "logical error"}, | |
1816 | {0x05, "logical busy"}, | |
1817 | {0x07, "protocol error"}, | |
1818 | {0x09, "unable to perform command request"}, | |
1819 | {0x0b, "command not supported"}, | |
1820 | {0x0e, "command already in progress"}, | |
1821 | {0xff, "vendor specific error"}, | |
1822 | {0, NULL}, | |
1823 | }; | |
1824 | ||
1825 | /* reject reason codes according to FC-PH/FC-FS */ | |
1826 | static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = { | |
1827 | {0x01, "invalid D_ID"}, | |
1828 | {0x02, "invalid S_ID"}, | |
1829 | {0x03, "Nx_Port not available, temporary"}, | |
1830 | {0x04, "Nx_Port not available, permament"}, | |
1831 | {0x05, "class not supported"}, | |
1832 | {0x06, "delimiter usage error"}, | |
1833 | {0x07, "TYPE not supported"}, | |
1834 | {0x08, "invalid Link_Control"}, | |
1835 | {0x09, "invalid R_CTL field"}, | |
1836 | {0x0a, "invalid F_CTL field"}, | |
1837 | {0x0b, "invalid OX_ID"}, | |
1838 | {0x0c, "invalid RX_ID"}, | |
1839 | {0x0d, "invalid SEQ_ID"}, | |
1840 | {0x0e, "invalid DF_CTL"}, | |
1841 | {0x0f, "invalid SEQ_CNT"}, | |
1842 | {0x10, "invalid parameter field"}, | |
1843 | {0x11, "exchange error"}, | |
1844 | {0x12, "protocol error"}, | |
1845 | {0x13, "incorrect length"}, | |
1846 | {0x14, "unsupported ACK"}, | |
1847 | {0x15, "class of service not supported by entity at FFFFFE"}, | |
1848 | {0x16, "login required"}, | |
1849 | {0x17, "excessive sequences attempted"}, | |
1850 | {0x18, "unable to establish exchange"}, | |
1851 | {0x1a, "fabric path not available"}, | |
1852 | {0x1b, "invalid VC_ID (class 4)"}, | |
1853 | {0x1c, "invalid CS_CTL field"}, | |
1854 | {0x1d, "insufficient resources for VC (class 4)"}, | |
1855 | {0x1f, "invalid class of service"}, | |
1856 | {0x20, "preemption request rejected"}, | |
1857 | {0x21, "preemption not enabled"}, | |
1858 | {0x22, "multicast error"}, | |
1859 | {0x23, "multicast error terminate"}, | |
1860 | {0x24, "process login required"}, | |
1861 | {0xff, "vendor specific reject"}, | |
1862 | {0, NULL}, | |
1863 | }; | |
1864 | ||
1865 | /** | |
1866 | * zfcp_rc_description - return description for given reaon code | |
1867 | * @code: reason code | |
1868 | * @rc_table: table of reason codes and descriptions | |
1869 | */ | |
1870 | static inline const char * | |
1871 | zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table) | |
1872 | { | |
1873 | const char *descr = "unknown reason code"; | |
1874 | ||
1875 | do { | |
1876 | if (code == rc_table->code) { | |
1877 | descr = rc_table->description; | |
1878 | break; | |
1879 | } | |
1880 | rc_table++; | |
1881 | } while (rc_table->code && rc_table->description); | |
1882 | ||
1883 | return descr; | |
1884 | } | |
1885 | ||
1886 | /** | |
1887 | * zfcp_check_ct_response - evaluate reason code for CT_IU | |
1888 | * @rjt: response payload to an CT_IU request | |
1889 | * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code | |
1890 | */ | |
1891 | int | |
1892 | zfcp_check_ct_response(struct ct_hdr *rjt) | |
1893 | { | |
1894 | if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT) | |
1895 | return 0; | |
1896 | ||
1897 | if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) { | |
1898 | ZFCP_LOG_NORMAL("error: invalid Generic Service command/" | |
1899 | "response code (0x%04hx)\n", | |
1900 | rjt->cmd_rsp_code); | |
1901 | return 1; | |
1902 | } | |
1903 | ||
1904 | ZFCP_LOG_INFO("Generic Service command rejected\n"); | |
1905 | ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n", | |
1906 | zfcp_rc_description(rjt->reason_code, zfcp_ct_rc), | |
1907 | (u32) rjt->reason_code, (u32) rjt->reason_code_expl, | |
1908 | (u32) rjt->vendor_unique); | |
1909 | ||
1910 | return 1; | |
1911 | } | |
1912 | ||
1913 | /** | |
1914 | * zfcp_print_els_rjt - print reject parameter and description for ELS reject | |
1915 | * @rjt_par: reject parameter acc. to FC-PH/FC-FS | |
1916 | * @rc_table: table of reason codes and descriptions | |
1917 | */ | |
1918 | static inline void | |
1919 | zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par, | |
1920 | const struct zfcp_rc_entry *rc_table) | |
1921 | { | |
1922 | ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n", | |
1923 | zfcp_rc_description(rjt_par->reason_code, rc_table), | |
1924 | (u32) rjt_par->action, (u32) rjt_par->reason_code, | |
1925 | (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique); | |
1926 | } | |
1927 | ||
1928 | /** | |
1929 | * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject | |
1930 | * @sq: status qualifier word | |
1931 | * @rjt_par: reject parameter as described in FC-PH and FC-FS | |
1932 | * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else | |
1933 | */ | |
1934 | int | |
1935 | zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par) | |
1936 | { | |
1937 | int ret = -EIO; | |
1938 | ||
1939 | if (sq == FSF_IOSTAT_NPORT_RJT) { | |
1940 | ZFCP_LOG_INFO("ELS rejected (P_RJT)\n"); | |
1941 | zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc); | |
1942 | /* invalid d_id */ | |
1943 | if (rjt_par->reason_code == 0x01) | |
1944 | ret = -EREMCHG; | |
1945 | } else if (sq == FSF_IOSTAT_FABRIC_RJT) { | |
1946 | ZFCP_LOG_INFO("ELS rejected (F_RJT)\n"); | |
1947 | zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc); | |
1948 | /* invalid d_id */ | |
1949 | if (rjt_par->reason_code == 0x01) | |
1950 | ret = -EREMCHG; | |
1951 | } else if (sq == FSF_IOSTAT_LS_RJT) { | |
1952 | ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n"); | |
1953 | zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc); | |
1954 | ret = -EREMOTEIO; | |
1955 | } else | |
1956 | ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq); | |
1957 | ||
1958 | return ret; | |
1959 | } | |
1960 | ||
1961 | #undef ZFCP_LOG_AREA |