]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/s390/scsi/zfcp_erp.c
Pull new-efi-memmap into release branch
[linux-2.6-omap-h63xx.git] / drivers / s390 / scsi / zfcp_erp.c
1 /* 
2  * 
3  * linux/drivers/s390/scsi/zfcp_erp.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_LOG_AREA                   ZFCP_LOG_AREA_ERP
33
34 #define ZFCP_ERP_REVISION "$Revision: 1.86 $"
35
36 #include "zfcp_ext.h"
37
38 static int zfcp_erp_adisc(struct zfcp_port *);
39 static void zfcp_erp_adisc_handler(unsigned long);
40
41 static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *, int);
42 static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *, int);
43 static int zfcp_erp_port_reopen_internal(struct zfcp_port *, int);
44 static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *, int);
45
46 static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *, int);
47 static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *, int);
48
49 static void zfcp_erp_adapter_block(struct zfcp_adapter *, int);
50 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *);
51 static void zfcp_erp_port_block(struct zfcp_port *, int);
52 static void zfcp_erp_port_unblock(struct zfcp_port *);
53 static void zfcp_erp_unit_block(struct zfcp_unit *, int);
54 static void zfcp_erp_unit_unblock(struct zfcp_unit *);
55
56 static int zfcp_erp_thread(void *);
57
58 static int zfcp_erp_strategy(struct zfcp_erp_action *);
59
60 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *);
61 static int zfcp_erp_strategy_memwait(struct zfcp_erp_action *);
62 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *, int);
63 static int zfcp_erp_strategy_check_unit(struct zfcp_unit *, int);
64 static int zfcp_erp_strategy_check_port(struct zfcp_port *, int);
65 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *, int);
66 static int zfcp_erp_strategy_statechange(int, u32, struct zfcp_adapter *,
67                                          struct zfcp_port *,
68                                          struct zfcp_unit *, int);
69 static inline int zfcp_erp_strategy_statechange_detected(atomic_t *, u32);
70 static int zfcp_erp_strategy_followup_actions(int, struct zfcp_adapter *,
71                                               struct zfcp_port *,
72                                               struct zfcp_unit *, int);
73 static int zfcp_erp_strategy_check_queues(struct zfcp_adapter *);
74 static int zfcp_erp_strategy_check_action(struct zfcp_erp_action *, int);
75
76 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *);
77 static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *, int);
78 static int zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *);
79 static int zfcp_erp_adapter_strategy_close_qdio(struct zfcp_erp_action *);
80 static int zfcp_erp_adapter_strategy_close_fsf(struct zfcp_erp_action *);
81 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *);
82 static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *);
83 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *);
84 static int zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *);
85 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *);
86 static int zfcp_erp_adapter_strategy_open_fsf_statusread(
87         struct zfcp_erp_action *);
88
89 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *);
90 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *);
91
92 static int zfcp_erp_port_strategy(struct zfcp_erp_action *);
93 static int zfcp_erp_port_strategy_clearstati(struct zfcp_port *);
94 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *);
95 static int zfcp_erp_port_strategy_open(struct zfcp_erp_action *);
96 static int zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *);
97 static int zfcp_erp_port_strategy_open_nameserver_wakeup(
98         struct zfcp_erp_action *);
99 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *);
100 static int zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *);
101 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *);
102
103 static int zfcp_erp_unit_strategy(struct zfcp_erp_action *);
104 static int zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *);
105 static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *);
106 static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *);
107
108 static int zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *);
109 static int zfcp_erp_action_dismiss_port(struct zfcp_port *);
110 static int zfcp_erp_action_dismiss_unit(struct zfcp_unit *);
111 static int zfcp_erp_action_dismiss(struct zfcp_erp_action *);
112
113 static int zfcp_erp_action_enqueue(int, struct zfcp_adapter *,
114                                    struct zfcp_port *, struct zfcp_unit *);
115 static int zfcp_erp_action_dequeue(struct zfcp_erp_action *);
116 static void zfcp_erp_action_cleanup(int, struct zfcp_adapter *,
117                                     struct zfcp_port *, struct zfcp_unit *,
118                                     int);
119
120 static void zfcp_erp_action_ready(struct zfcp_erp_action *);
121 static int  zfcp_erp_action_exists(struct zfcp_erp_action *);
122
123 static inline void zfcp_erp_action_to_ready(struct zfcp_erp_action *);
124 static inline void zfcp_erp_action_to_running(struct zfcp_erp_action *);
125
126 static void zfcp_erp_memwait_handler(unsigned long);
127 static void zfcp_erp_timeout_handler(unsigned long);
128 static inline void zfcp_erp_timeout_init(struct zfcp_erp_action *);
129
130 /**
131  * zfcp_fsf_request_timeout_handler - called if a request timed out
132  * @data: pointer to adapter for handler function
133  *
134  * This function needs to be called if requests (ELS, Generic Service,
135  * or SCSI commands) exceed a certain time limit. The assumption is
136  * that after the time limit the adapter get stuck. So we trigger a reopen of
137  * the adapter. This should not be used for error recovery, SCSI abort
138  * commands and SCSI requests from SCSI mid-layer.
139  */
140 void
141 zfcp_fsf_request_timeout_handler(unsigned long data)
142 {
143         struct zfcp_adapter *adapter;
144
145         adapter = (struct zfcp_adapter *) data;
146
147         zfcp_erp_adapter_reopen(adapter, 0);
148 }
149
150 /*
151  * function:    zfcp_fsf_scsi_er_timeout_handler
152  *
153  * purpose:     This function needs to be called whenever a SCSI error recovery
154  *              action (abort/reset) does not return.
155  *              Re-opening the adapter means that the command can be returned
156  *              by zfcp (it is guarranteed that it does not return via the
157  *              adapter anymore). The buffer can then be used again.
158  *    
159  * returns:     sod all
160  */
161 void
162 zfcp_fsf_scsi_er_timeout_handler(unsigned long data)
163 {
164         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
165
166         ZFCP_LOG_NORMAL("warning: SCSI error recovery timed out. "
167                         "Restarting all operations on the adapter %s\n",
168                         zfcp_get_busid_by_adapter(adapter));
169         debug_text_event(adapter->erp_dbf, 1, "eh_lmem_tout");
170         zfcp_erp_adapter_reopen(adapter, 0);
171
172         return;
173 }
174
175 /*
176  * function:    
177  *
178  * purpose:     called if an adapter failed,
179  *              initiates adapter recovery which is done
180  *              asynchronously
181  *
182  * returns:     0       - initiated action succesfully
183  *              <0      - failed to initiate action
184  */
185 int
186 zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *adapter, int clear_mask)
187 {
188         int retval;
189
190         debug_text_event(adapter->erp_dbf, 5, "a_ro");
191         ZFCP_LOG_DEBUG("reopen adapter %s\n",
192                        zfcp_get_busid_by_adapter(adapter));
193
194         zfcp_erp_adapter_block(adapter, clear_mask);
195
196         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
197                 ZFCP_LOG_DEBUG("skipped reopen of failed adapter %s\n",
198                                zfcp_get_busid_by_adapter(adapter));
199                 debug_text_event(adapter->erp_dbf, 5, "a_ro_f");
200                 /* ensure propagation of failed status to new devices */
201                 zfcp_erp_adapter_failed(adapter);
202                 retval = -EIO;
203                 goto out;
204         }
205         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
206                                          adapter, NULL, NULL);
207
208  out:
209         return retval;
210 }
211
212 /*
213  * function:    
214  *
215  * purpose:     Wrappper for zfcp_erp_adapter_reopen_internal
216  *              used to ensure the correct locking
217  *
218  * returns:     0       - initiated action succesfully
219  *              <0      - failed to initiate action
220  */
221 int
222 zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear_mask)
223 {
224         int retval;
225         unsigned long flags;
226
227         read_lock_irqsave(&zfcp_data.config_lock, flags);
228         write_lock(&adapter->erp_lock);
229         retval = zfcp_erp_adapter_reopen_internal(adapter, clear_mask);
230         write_unlock(&adapter->erp_lock);
231         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
232
233         return retval;
234 }
235
236 /*
237  * function:    
238  *
239  * purpose:     
240  *
241  * returns:
242  */
243 int
244 zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear_mask)
245 {
246         int retval;
247
248         retval = zfcp_erp_adapter_reopen(adapter,
249                                          ZFCP_STATUS_COMMON_RUNNING |
250                                          ZFCP_STATUS_COMMON_ERP_FAILED |
251                                          clear_mask);
252
253         return retval;
254 }
255
256 /*
257  * function:    
258  *
259  * purpose:     
260  *
261  * returns:
262  */
263 int
264 zfcp_erp_port_shutdown(struct zfcp_port *port, int clear_mask)
265 {
266         int retval;
267
268         retval = zfcp_erp_port_reopen(port,
269                                       ZFCP_STATUS_COMMON_RUNNING |
270                                       ZFCP_STATUS_COMMON_ERP_FAILED |
271                                       clear_mask);
272
273         return retval;
274 }
275
276 /*
277  * function:    
278  *
279  * purpose:     
280  *
281  * returns:
282  */
283 int
284 zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear_mask)
285 {
286         int retval;
287
288         retval = zfcp_erp_unit_reopen(unit,
289                                       ZFCP_STATUS_COMMON_RUNNING |
290                                       ZFCP_STATUS_COMMON_ERP_FAILED |
291                                       clear_mask);
292
293         return retval;
294 }
295
296
297 /**
298  * zfcp_erp_adisc - send ADISC ELS command
299  * @port: port structure
300  */
301 int
302 zfcp_erp_adisc(struct zfcp_port *port)
303 {
304         struct zfcp_adapter *adapter = port->adapter;
305         struct zfcp_send_els *send_els;
306         struct zfcp_ls_adisc *adisc;
307         void *address = NULL;
308         int retval = 0;
309         struct timer_list *timer;
310
311         send_els = kmalloc(sizeof(struct zfcp_send_els), GFP_ATOMIC);
312         if (send_els == NULL)
313                 goto nomem;
314         memset(send_els, 0, sizeof(*send_els));
315
316         send_els->req = kmalloc(sizeof(struct scatterlist), GFP_ATOMIC);
317         if (send_els->req == NULL)
318                 goto nomem;
319         memset(send_els->req, 0, sizeof(*send_els->req));
320
321         send_els->resp = kmalloc(sizeof(struct scatterlist), GFP_ATOMIC);
322         if (send_els->resp == NULL)
323                 goto nomem;
324         memset(send_els->resp, 0, sizeof(*send_els->resp));
325
326         address = (void *) get_zeroed_page(GFP_ATOMIC);
327         if (address == NULL)
328                 goto nomem;
329
330         zfcp_address_to_sg(address, send_els->req);
331         address += PAGE_SIZE >> 1;
332         zfcp_address_to_sg(address, send_els->resp);
333         send_els->req_count = send_els->resp_count = 1;
334
335         send_els->adapter = adapter;
336         send_els->port = port;
337         send_els->d_id = port->d_id;
338         send_els->handler = zfcp_erp_adisc_handler;
339         send_els->handler_data = (unsigned long) send_els;
340
341         adisc = zfcp_sg_to_address(send_els->req);
342         send_els->ls_code = adisc->code = ZFCP_LS_ADISC;
343
344         send_els->req->length = sizeof(struct zfcp_ls_adisc);
345         send_els->resp->length = sizeof(struct zfcp_ls_adisc_acc);
346
347         /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
348            without FC-AL-2 capability, so we don't set it */
349         adisc->wwpn = fc_host_port_name(adapter->scsi_host);
350         adisc->wwnn = fc_host_node_name(adapter->scsi_host);
351         adisc->nport_id = fc_host_port_id(adapter->scsi_host);
352         ZFCP_LOG_INFO("ADISC request from s_id 0x%08x to d_id 0x%08x "
353                       "(wwpn=0x%016Lx, wwnn=0x%016Lx, "
354                       "hard_nport_id=0x%08x, nport_id=0x%08x)\n",
355                       adisc->nport_id, send_els->d_id, (wwn_t) adisc->wwpn,
356                       (wwn_t) adisc->wwnn, adisc->hard_nport_id,
357                       adisc->nport_id);
358
359         timer = kmalloc(sizeof(struct timer_list), GFP_ATOMIC);
360         if (!timer)
361                 goto nomem;
362
363         init_timer(timer);
364         timer->function = zfcp_fsf_request_timeout_handler;
365         timer->data = (unsigned long) adapter;
366         timer->expires = ZFCP_FSF_REQUEST_TIMEOUT;
367         send_els->timer = timer;
368
369         retval = zfcp_fsf_send_els(send_els);
370         if (retval != 0) {
371                 ZFCP_LOG_NORMAL("error: initiation of Send ELS failed for port "
372                                 "0x%08x on adapter %s\n", send_els->d_id,
373                                 zfcp_get_busid_by_adapter(adapter));
374                 del_timer(send_els->timer);
375                 goto freemem;
376         }
377
378         goto out;
379
380  nomem:
381         retval = -ENOMEM;
382  freemem:
383         if (address != NULL)
384                 __free_pages(send_els->req->page, 0);
385         if (send_els != NULL) {
386                 kfree(send_els->timer);
387                 kfree(send_els->req);
388                 kfree(send_els->resp);
389                 kfree(send_els);
390         }
391  out:
392         return retval;
393 }
394
395
396 /**
397  * zfcp_erp_adisc_handler - handler for ADISC ELS command
398  * @data: pointer to struct zfcp_send_els
399  *
400  * If ADISC failed (LS_RJT or timed out) forced reopen of the port is triggered.
401  */
402 void
403 zfcp_erp_adisc_handler(unsigned long data)
404 {
405         struct zfcp_send_els *send_els;
406         struct zfcp_port *port;
407         struct zfcp_adapter *adapter;
408         u32 d_id;
409         struct zfcp_ls_adisc_acc *adisc;
410
411         send_els = (struct zfcp_send_els *) data;
412
413         del_timer(send_els->timer);
414
415         adapter = send_els->adapter;
416         port = send_els->port;
417         d_id = send_els->d_id;
418
419         /* request rejected or timed out */
420         if (send_els->status != 0) {
421                 ZFCP_LOG_NORMAL("ELS request rejected/timed out, "
422                                 "force physical port reopen "
423                                 "(adapter %s, port d_id=0x%08x)\n",
424                                 zfcp_get_busid_by_adapter(adapter), d_id);
425                 debug_text_event(adapter->erp_dbf, 3, "forcreop");
426                 if (zfcp_erp_port_forced_reopen(port, 0))
427                         ZFCP_LOG_NORMAL("failed reopen of port "
428                                         "(adapter %s, wwpn=0x%016Lx)\n",
429                                         zfcp_get_busid_by_port(port),
430                                         port->wwpn);
431                 goto out;
432         }
433
434         adisc = zfcp_sg_to_address(send_els->resp);
435
436         ZFCP_LOG_INFO("ADISC response from d_id 0x%08x to s_id "
437                       "0x%08x (wwpn=0x%016Lx, wwnn=0x%016Lx, "
438                       "hard_nport_id=0x%08x, nport_id=0x%08x)\n",
439                       d_id, fc_host_port_id(adapter->scsi_host),
440                       (wwn_t) adisc->wwpn, (wwn_t) adisc->wwnn,
441                       adisc->hard_nport_id, adisc->nport_id);
442
443         /* set wwnn for port */
444         if (port->wwnn == 0)
445                 port->wwnn = adisc->wwnn;
446
447         if (port->wwpn != adisc->wwpn) {
448                 ZFCP_LOG_NORMAL("d_id assignment changed, reopening "
449                                 "port (adapter %s, wwpn=0x%016Lx, "
450                                 "adisc_resp_wwpn=0x%016Lx)\n",
451                                 zfcp_get_busid_by_port(port),
452                                 port->wwpn, (wwn_t) adisc->wwpn);
453                 if (zfcp_erp_port_reopen(port, 0))
454                         ZFCP_LOG_NORMAL("failed reopen of port "
455                                         "(adapter %s, wwpn=0x%016Lx)\n",
456                                         zfcp_get_busid_by_port(port),
457                                         port->wwpn);
458         }
459
460  out:
461         zfcp_port_put(port);
462         __free_pages(send_els->req->page, 0);
463         kfree(send_els->timer);
464         kfree(send_els->req);
465         kfree(send_els->resp);
466         kfree(send_els);
467 }
468
469
470 /**
471  * zfcp_test_link - lightweight link test procedure
472  * @port: port to be tested
473  *
474  * Test status of a link to a remote port using the ELS command ADISC.
475  */
476 int
477 zfcp_test_link(struct zfcp_port *port)
478 {
479         int retval;
480
481         zfcp_port_get(port);
482         retval = zfcp_erp_adisc(port);
483         if (retval != 0) {
484                 zfcp_port_put(port);
485                 ZFCP_LOG_NORMAL("reopen needed for port 0x%016Lx "
486                                 "on adapter %s\n ", port->wwpn,
487                                 zfcp_get_busid_by_port(port));
488                 retval = zfcp_erp_port_forced_reopen(port, 0);
489                 if (retval != 0) {
490                         ZFCP_LOG_NORMAL("reopen of remote port 0x%016Lx "
491                                         "on adapter %s failed\n", port->wwpn,
492                                         zfcp_get_busid_by_port(port));
493                         retval = -EPERM;
494                 }
495         }
496
497         return retval;
498 }
499
500
501 /*
502  * function:    
503  *
504  * purpose:     called if a port failed to be opened normally
505  *              initiates Forced Reopen recovery which is done
506  *              asynchronously
507  *
508  * returns:     0       - initiated action succesfully
509  *              <0      - failed to initiate action
510  */
511 static int
512 zfcp_erp_port_forced_reopen_internal(struct zfcp_port *port, int clear_mask)
513 {
514         int retval;
515         struct zfcp_adapter *adapter = port->adapter;
516
517         debug_text_event(adapter->erp_dbf, 5, "pf_ro");
518         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
519
520         ZFCP_LOG_DEBUG("forced reopen of port 0x%016Lx on adapter %s\n",
521                        port->wwpn, zfcp_get_busid_by_port(port));
522
523         zfcp_erp_port_block(port, clear_mask);
524
525         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
526                 ZFCP_LOG_DEBUG("skipped forced reopen of failed port 0x%016Lx "
527                                "on adapter %s\n", port->wwpn,
528                                zfcp_get_busid_by_port(port));
529                 debug_text_event(adapter->erp_dbf, 5, "pf_ro_f");
530                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
531                 retval = -EIO;
532                 goto out;
533         }
534
535         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
536                                          port->adapter, port, NULL);
537
538  out:
539         return retval;
540 }
541
542 /*
543  * function:    
544  *
545  * purpose:     Wrappper for zfcp_erp_port_forced_reopen_internal
546  *              used to ensure the correct locking
547  *
548  * returns:     0       - initiated action succesfully
549  *              <0      - failed to initiate action
550  */
551 int
552 zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear_mask)
553 {
554         int retval;
555         unsigned long flags;
556         struct zfcp_adapter *adapter;
557
558         adapter = port->adapter;
559         read_lock_irqsave(&zfcp_data.config_lock, flags);
560         write_lock(&adapter->erp_lock);
561         retval = zfcp_erp_port_forced_reopen_internal(port, clear_mask);
562         write_unlock(&adapter->erp_lock);
563         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
564
565         return retval;
566 }
567
568 /*
569  * function:    
570  *
571  * purpose:     called if a port is to be opened
572  *              initiates Reopen recovery which is done
573  *              asynchronously
574  *
575  * returns:     0       - initiated action succesfully
576  *              <0      - failed to initiate action
577  */
578 static int
579 zfcp_erp_port_reopen_internal(struct zfcp_port *port, int clear_mask)
580 {
581         int retval;
582         struct zfcp_adapter *adapter = port->adapter;
583
584         debug_text_event(adapter->erp_dbf, 5, "p_ro");
585         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
586
587         ZFCP_LOG_DEBUG("reopen of port 0x%016Lx on adapter %s\n",
588                        port->wwpn, zfcp_get_busid_by_port(port));
589
590         zfcp_erp_port_block(port, clear_mask);
591
592         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
593                 ZFCP_LOG_DEBUG("skipped reopen of failed port 0x%016Lx "
594                                "on adapter %s\n", port->wwpn,
595                                zfcp_get_busid_by_port(port));
596                 debug_text_event(adapter->erp_dbf, 5, "p_ro_f");
597                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
598                 /* ensure propagation of failed status to new devices */
599                 zfcp_erp_port_failed(port);
600                 retval = -EIO;
601                 goto out;
602         }
603
604         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
605                                          port->adapter, port, NULL);
606
607  out:
608         return retval;
609 }
610
611 /**
612  * zfcp_erp_port_reopen - initiate reopen of a remote port
613  * @port: port to be reopened
614  * @clear_mask: specifies flags in port status to be cleared
615  * Return: 0 on success, < 0 on error
616  *
617  * This is a wrappper function for zfcp_erp_port_reopen_internal. It ensures
618  * correct locking. An error recovery task is initiated to do the reopen.
619  * To wait for the completion of the reopen zfcp_erp_wait should be used.
620  */
621 int
622 zfcp_erp_port_reopen(struct zfcp_port *port, int clear_mask)
623 {
624         int retval;
625         unsigned long flags;
626         struct zfcp_adapter *adapter = port->adapter;
627
628         read_lock_irqsave(&zfcp_data.config_lock, flags);
629         write_lock(&adapter->erp_lock);
630         retval = zfcp_erp_port_reopen_internal(port, clear_mask);
631         write_unlock(&adapter->erp_lock);
632         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
633
634         return retval;
635 }
636
637 /*
638  * function:    
639  *
640  * purpose:     called if a unit is to be opened
641  *              initiates Reopen recovery which is done
642  *              asynchronously
643  *
644  * returns:     0       - initiated action succesfully
645  *              <0      - failed to initiate action
646  */
647 static int
648 zfcp_erp_unit_reopen_internal(struct zfcp_unit *unit, int clear_mask)
649 {
650         int retval;
651         struct zfcp_adapter *adapter = unit->port->adapter;
652
653         debug_text_event(adapter->erp_dbf, 5, "u_ro");
654         debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
655         ZFCP_LOG_DEBUG("reopen of unit 0x%016Lx on port 0x%016Lx "
656                        "on adapter %s\n", unit->fcp_lun,
657                        unit->port->wwpn, zfcp_get_busid_by_unit(unit));
658
659         zfcp_erp_unit_block(unit, clear_mask);
660
661         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
662                 ZFCP_LOG_DEBUG("skipped reopen of failed unit 0x%016Lx "
663                                "on port 0x%016Lx on adapter %s\n",
664                                unit->fcp_lun, unit->port->wwpn,
665                                zfcp_get_busid_by_unit(unit));
666                 debug_text_event(adapter->erp_dbf, 5, "u_ro_f");
667                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
668                             sizeof (fcp_lun_t));
669                 retval = -EIO;
670                 goto out;
671         }
672
673         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
674                                          unit->port->adapter, unit->port, unit);
675  out:
676         return retval;
677 }
678
679 /**
680  * zfcp_erp_unit_reopen - initiate reopen of a unit
681  * @unit: unit to be reopened
682  * @clear_mask: specifies flags in unit status to be cleared
683  * Return: 0 on success, < 0 on error
684  *
685  * This is a wrappper for zfcp_erp_unit_reopen_internal. It ensures correct
686  * locking. An error recovery task is initiated to do the reopen.
687  * To wait for the completion of the reopen zfcp_erp_wait should be used.
688  */
689 int
690 zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear_mask)
691 {
692         int retval;
693         unsigned long flags;
694         struct zfcp_adapter *adapter;
695         struct zfcp_port *port;
696
697         port = unit->port;
698         adapter = port->adapter;
699
700         read_lock_irqsave(&zfcp_data.config_lock, flags);
701         write_lock(&adapter->erp_lock);
702         retval = zfcp_erp_unit_reopen_internal(unit, clear_mask);
703         write_unlock(&adapter->erp_lock);
704         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
705
706         return retval;
707 }
708
709 /*
710  * function:    
711  *
712  * purpose:     disable I/O,
713  *              return any open requests and clean them up,
714  *              aim: no pending and incoming I/O
715  *
716  * returns:
717  */
718 static void
719 zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int clear_mask)
720 {
721         debug_text_event(adapter->erp_dbf, 6, "a_bl");
722         zfcp_erp_modify_adapter_status(adapter,
723                                        ZFCP_STATUS_COMMON_UNBLOCKED |
724                                        clear_mask, ZFCP_CLEAR);
725 }
726
727 /*
728  * function:    
729  *
730  * purpose:     enable I/O
731  *
732  * returns:
733  */
734 static void
735 zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
736 {
737         debug_text_event(adapter->erp_dbf, 6, "a_ubl");
738         atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
739 }
740
741 /*
742  * function:    
743  *
744  * purpose:     disable I/O,
745  *              return any open requests and clean them up,
746  *              aim: no pending and incoming I/O
747  *
748  * returns:
749  */
750 static void
751 zfcp_erp_port_block(struct zfcp_port *port, int clear_mask)
752 {
753         struct zfcp_adapter *adapter = port->adapter;
754
755         debug_text_event(adapter->erp_dbf, 6, "p_bl");
756         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
757         zfcp_erp_modify_port_status(port,
758                                     ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
759                                     ZFCP_CLEAR);
760 }
761
762 /*
763  * function:    
764  *
765  * purpose:     enable I/O
766  *
767  * returns:
768  */
769 static void
770 zfcp_erp_port_unblock(struct zfcp_port *port)
771 {
772         struct zfcp_adapter *adapter = port->adapter;
773
774         debug_text_event(adapter->erp_dbf, 6, "p_ubl");
775         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
776         atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
777 }
778
779 /*
780  * function:    
781  *
782  * purpose:     disable I/O,
783  *              return any open requests and clean them up,
784  *              aim: no pending and incoming I/O
785  *
786  * returns:
787  */
788 static void
789 zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
790 {
791         struct zfcp_adapter *adapter = unit->port->adapter;
792
793         debug_text_event(adapter->erp_dbf, 6, "u_bl");
794         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
795         zfcp_erp_modify_unit_status(unit,
796                                     ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
797                                     ZFCP_CLEAR);
798 }
799
800 /*
801  * function:    
802  *
803  * purpose:     enable I/O
804  *
805  * returns:
806  */
807 static void
808 zfcp_erp_unit_unblock(struct zfcp_unit *unit)
809 {
810         struct zfcp_adapter *adapter = unit->port->adapter;
811
812         debug_text_event(adapter->erp_dbf, 6, "u_ubl");
813         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
814         atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
815 }
816
817 /*
818  * function:    
819  *
820  * purpose:     
821  *
822  * returns:
823  */
824 static void
825 zfcp_erp_action_ready(struct zfcp_erp_action *erp_action)
826 {
827         struct zfcp_adapter *adapter = erp_action->adapter;
828
829         debug_text_event(adapter->erp_dbf, 4, "a_ar");
830         debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
831
832         zfcp_erp_action_to_ready(erp_action);
833         up(&adapter->erp_ready_sem);
834 }
835
836 /*
837  * function:    
838  *
839  * purpose:
840  *
841  * returns:     <0                      erp_action not found in any list
842  *              ZFCP_ERP_ACTION_READY   erp_action is in ready list
843  *              ZFCP_ERP_ACTION_RUNNING erp_action is in running list
844  *
845  * locks:       erp_lock must be held
846  */
847 static int
848 zfcp_erp_action_exists(struct zfcp_erp_action *erp_action)
849 {
850         int retval = -EINVAL;
851         struct list_head *entry;
852         struct zfcp_erp_action *entry_erp_action;
853         struct zfcp_adapter *adapter = erp_action->adapter;
854
855         /* search in running list */
856         list_for_each(entry, &adapter->erp_running_head) {
857                 entry_erp_action =
858                     list_entry(entry, struct zfcp_erp_action, list);
859                 if (entry_erp_action == erp_action) {
860                         retval = ZFCP_ERP_ACTION_RUNNING;
861                         goto out;
862                 }
863         }
864         /* search in ready list */
865         list_for_each(entry, &adapter->erp_ready_head) {
866                 entry_erp_action =
867                     list_entry(entry, struct zfcp_erp_action, list);
868                 if (entry_erp_action == erp_action) {
869                         retval = ZFCP_ERP_ACTION_READY;
870                         goto out;
871                 }
872         }
873
874  out:
875         return retval;
876 }
877
878 /*
879  * purpose:     checks current status of action (timed out, dismissed, ...)
880  *              and does appropriate preparations (dismiss fsf request, ...)
881  *
882  * locks:       called under erp_lock (disabled interrupts)
883  *
884  * returns:     0
885  */
886 static int
887 zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
888 {
889         int retval = 0;
890         struct zfcp_fsf_req *fsf_req = NULL;
891         struct zfcp_adapter *adapter = erp_action->adapter;
892
893         if (erp_action->fsf_req) {
894                 /* take lock to ensure that request is not being deleted meanwhile */
895                 spin_lock(&adapter->fsf_req_list_lock);
896                 /* check whether fsf req does still exist */
897                 list_for_each_entry(fsf_req, &adapter->fsf_req_list_head, list)
898                     if (fsf_req == erp_action->fsf_req)
899                         break;
900                 if (fsf_req && (fsf_req->erp_action == erp_action)) {
901                         /* fsf_req still exists */
902                         debug_text_event(adapter->erp_dbf, 3, "a_ca_req");
903                         debug_event(adapter->erp_dbf, 3, &fsf_req,
904                                     sizeof (unsigned long));
905                         /* dismiss fsf_req of timed out or dismissed erp_action */
906                         if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
907                                                   ZFCP_STATUS_ERP_TIMEDOUT)) {
908                                 debug_text_event(adapter->erp_dbf, 3,
909                                                  "a_ca_disreq");
910                                 fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
911                         }
912                         if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
913                                 ZFCP_LOG_NORMAL("error: erp step timed out "
914                                                 "(action=%d, fsf_req=%p)\n ",
915                                                 erp_action->action,
916                                                 erp_action->fsf_req);
917                         }
918                         /*
919                          * If fsf_req is neither dismissed nor completed
920                          * then keep it running asynchronously and don't mess
921                          * with the association of erp_action and fsf_req.
922                          */
923                         if (fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
924                                                ZFCP_STATUS_FSFREQ_DISMISSED)) {
925                                 /* forget about association between fsf_req
926                                    and erp_action */
927                                 fsf_req->erp_action = NULL;
928                                 erp_action->fsf_req = NULL;
929                         }
930                 } else {
931                         debug_text_event(adapter->erp_dbf, 3, "a_ca_gonereq");
932                         /*
933                          * even if this fsf_req has gone, forget about
934                          * association between erp_action and fsf_req
935                          */
936                         erp_action->fsf_req = NULL;
937                 }
938                 spin_unlock(&adapter->fsf_req_list_lock);
939         } else
940                 debug_text_event(adapter->erp_dbf, 3, "a_ca_noreq");
941
942         return retval;
943 }
944
945 /*
946  * purpose:     generic handler for asynchronous events related to erp_action events
947  *              (normal completion, time-out, dismissing, retry after
948  *              low memory condition)
949  *
950  * note:        deletion of timer is not required (e.g. in case of a time-out),
951  *              but a second try does no harm,
952  *              we leave it in here to allow for greater simplification
953  *
954  * returns:     0 - there was an action to handle
955  *              !0 - otherwise
956  */
957 static int
958 zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
959                               unsigned long set_mask)
960 {
961         int retval;
962         struct zfcp_adapter *adapter = erp_action->adapter;
963
964         if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
965                 debug_text_event(adapter->erp_dbf, 2, "a_asyh_ex");
966                 debug_event(adapter->erp_dbf, 2, &erp_action->action,
967                             sizeof (int));
968                 if (!(set_mask & ZFCP_STATUS_ERP_TIMEDOUT))
969                         del_timer(&erp_action->timer);
970                 erp_action->status |= set_mask;
971                 zfcp_erp_action_ready(erp_action);
972                 retval = 0;
973         } else {
974                 /* action is ready or gone - nothing to do */
975                 debug_text_event(adapter->erp_dbf, 3, "a_asyh_gone");
976                 debug_event(adapter->erp_dbf, 3, &erp_action->action,
977                             sizeof (int));
978                 retval = 1;
979         }
980
981         return retval;
982 }
983
984 /*
985  * purpose:     generic handler for asynchronous events related to erp_action
986  *               events (normal completion, time-out, dismissing, retry after
987  *              low memory condition)
988  *
989  * note:        deletion of timer is not required (e.g. in case of a time-out),
990  *              but a second try does no harm,
991  *              we leave it in here to allow for greater simplification
992  *
993  * returns:     0 - there was an action to handle
994  *              !0 - otherwise
995  */
996 int
997 zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
998                        unsigned long set_mask)
999 {
1000         struct zfcp_adapter *adapter = erp_action->adapter;
1001         unsigned long flags;
1002         int retval;
1003
1004         write_lock_irqsave(&adapter->erp_lock, flags);
1005         retval = zfcp_erp_async_handler_nolock(erp_action, set_mask);
1006         write_unlock_irqrestore(&adapter->erp_lock, flags);
1007
1008         return retval;
1009 }
1010
1011 /*
1012  * purpose:     is called for erp_action which was slept waiting for
1013  *              memory becoming avaliable,
1014  *              will trigger that this action will be continued
1015  */
1016 static void
1017 zfcp_erp_memwait_handler(unsigned long data)
1018 {
1019         struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
1020         struct zfcp_adapter *adapter = erp_action->adapter;
1021
1022         debug_text_event(adapter->erp_dbf, 2, "a_mwh");
1023         debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
1024
1025         zfcp_erp_async_handler(erp_action, 0);
1026 }
1027
1028 /*
1029  * purpose:     is called if an asynchronous erp step timed out,
1030  *              action gets an appropriate flag and will be processed
1031  *              accordingly
1032  */
1033 static void
1034 zfcp_erp_timeout_handler(unsigned long data)
1035 {
1036         struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
1037         struct zfcp_adapter *adapter = erp_action->adapter;
1038
1039         debug_text_event(adapter->erp_dbf, 2, "a_th");
1040         debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
1041
1042         zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
1043 }
1044
1045 /*
1046  * purpose:     is called for an erp_action which needs to be ended
1047  *              though not being done,
1048  *              this is usually required if an higher is generated,
1049  *              action gets an appropriate flag and will be processed
1050  *              accordingly
1051  *
1052  * locks:       erp_lock held (thus we need to call another handler variant)
1053  */
1054 static int
1055 zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
1056 {
1057         struct zfcp_adapter *adapter = erp_action->adapter;
1058
1059         debug_text_event(adapter->erp_dbf, 2, "a_adis");
1060         debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
1061
1062         zfcp_erp_async_handler_nolock(erp_action, ZFCP_STATUS_ERP_DISMISSED);
1063
1064         return 0;
1065 }
1066
1067 int
1068 zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1069 {
1070         int retval = 0;
1071
1072         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1073
1074         rwlock_init(&adapter->erp_lock);
1075         INIT_LIST_HEAD(&adapter->erp_ready_head);
1076         INIT_LIST_HEAD(&adapter->erp_running_head);
1077         sema_init(&adapter->erp_ready_sem, 0);
1078
1079         retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
1080         if (retval < 0) {
1081                 ZFCP_LOG_NORMAL("error: creation of erp thread failed for "
1082                                 "adapter %s\n",
1083                                 zfcp_get_busid_by_adapter(adapter));
1084                 debug_text_event(adapter->erp_dbf, 5, "a_thset_fail");
1085         } else {
1086                 wait_event(adapter->erp_thread_wqh,
1087                            atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1088                                             &adapter->status));
1089                 debug_text_event(adapter->erp_dbf, 5, "a_thset_ok");
1090         }
1091
1092         return (retval < 0);
1093 }
1094
1095 /*
1096  * function:    
1097  *
1098  * purpose:     
1099  *
1100  * returns:
1101  *
1102  * context:     process (i.e. proc-fs or rmmod/insmod)
1103  *
1104  * note:        The caller of this routine ensures that the specified
1105  *              adapter has been shut down and that this operation
1106  *              has been completed. Thus, there are no pending erp_actions
1107  *              which would need to be handled here.
1108  */
1109 int
1110 zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1111 {
1112         int retval = 0;
1113
1114         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1115         up(&adapter->erp_ready_sem);
1116
1117         wait_event(adapter->erp_thread_wqh,
1118                    !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1119                                      &adapter->status));
1120
1121         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1122                           &adapter->status);
1123
1124         debug_text_event(adapter->erp_dbf, 5, "a_thki_ok");
1125
1126         return retval;
1127 }
1128
1129 /*
1130  * purpose:     is run as a kernel thread,
1131  *              goes through list of error recovery actions of associated adapter
1132  *              and delegates single action to execution
1133  *
1134  * returns:     0
1135  */
1136 static int
1137 zfcp_erp_thread(void *data)
1138 {
1139         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1140         struct list_head *next;
1141         struct zfcp_erp_action *erp_action;
1142         unsigned long flags;
1143
1144         daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
1145         /* Block all signals */
1146         siginitsetinv(&current->blocked, 0);
1147         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1148         debug_text_event(adapter->erp_dbf, 5, "a_th_run");
1149         wake_up(&adapter->erp_thread_wqh);
1150
1151         while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1152                                  &adapter->status)) {
1153
1154                 write_lock_irqsave(&adapter->erp_lock, flags);
1155                 next = adapter->erp_ready_head.prev;
1156                 write_unlock_irqrestore(&adapter->erp_lock, flags);
1157
1158                 if (next != &adapter->erp_ready_head) {
1159                         erp_action =
1160                             list_entry(next, struct zfcp_erp_action, list);
1161                         /*
1162                          * process action (incl. [re]moving it
1163                          * from 'ready' queue)
1164                          */
1165                         zfcp_erp_strategy(erp_action);
1166                 }
1167
1168                 /*
1169                  * sleep as long as there is nothing to do, i.e.
1170                  * no action in 'ready' queue to be processed and
1171                  * thread is not to be killed
1172                  */
1173                 down_interruptible(&adapter->erp_ready_sem);
1174                 debug_text_event(adapter->erp_dbf, 5, "a_th_woken");
1175         }
1176
1177         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1178         debug_text_event(adapter->erp_dbf, 5, "a_th_stop");
1179         wake_up(&adapter->erp_thread_wqh);
1180
1181         return 0;
1182 }
1183
1184 /*
1185  * function:    
1186  *
1187  * purpose:     drives single error recovery action and schedules higher and
1188  *              subordinate actions, if necessary
1189  *
1190  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
1191  *              ZFCP_ERP_SUCCEEDED      - action finished successfully (deqd)
1192  *              ZFCP_ERP_FAILED         - action finished unsuccessfully (deqd)
1193  *              ZFCP_ERP_EXIT           - action finished (dequeued), offline
1194  *              ZFCP_ERP_DISMISSED      - action canceled (dequeued)
1195  */
1196 static int
1197 zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1198 {
1199         int retval = 0;
1200         struct zfcp_adapter *adapter = erp_action->adapter;
1201         struct zfcp_port *port = erp_action->port;
1202         struct zfcp_unit *unit = erp_action->unit;
1203         int action = erp_action->action;
1204         u32 status = erp_action->status;
1205         unsigned long flags;
1206
1207         /* serialise dismissing, timing out, moving, enqueueing */
1208         read_lock_irqsave(&zfcp_data.config_lock, flags);
1209         write_lock(&adapter->erp_lock);
1210
1211         /* dequeue dismissed action and leave, if required */
1212         retval = zfcp_erp_strategy_check_action(erp_action, retval);
1213         if (retval == ZFCP_ERP_DISMISSED) {
1214                 debug_text_event(adapter->erp_dbf, 4, "a_st_dis1");
1215                 goto unlock;
1216         }
1217
1218         /*
1219          * move action to 'running' queue before processing it
1220          * (to avoid a race condition regarding moving the
1221          * action to the 'running' queue and back)
1222          */
1223         zfcp_erp_action_to_running(erp_action);
1224
1225         /*
1226          * try to process action as far as possible,
1227          * no lock to allow for blocking operations (kmalloc, qdio, ...),
1228          * afterwards the lock is required again for the following reasons:
1229          * - dequeueing of finished action and enqueueing of
1230          *   follow-up actions must be atomic so that any other
1231          *   reopen-routine does not believe there is nothing to do
1232          *   and that it is safe to enqueue something else,
1233          * - we want to force any control thread which is dismissing
1234          *   actions to finish this before we decide about
1235          *   necessary steps to be taken here further
1236          */
1237         write_unlock(&adapter->erp_lock);
1238         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1239         retval = zfcp_erp_strategy_do_action(erp_action);
1240         read_lock_irqsave(&zfcp_data.config_lock, flags);
1241         write_lock(&adapter->erp_lock);
1242
1243         /*
1244          * check for dismissed status again to avoid follow-up actions,
1245          * failing of targets and so on for dismissed actions
1246          */
1247         retval = zfcp_erp_strategy_check_action(erp_action, retval);
1248
1249         switch (retval) {
1250         case ZFCP_ERP_DISMISSED:
1251                 /* leave since this action has ridden to its ancestors */
1252                 debug_text_event(adapter->erp_dbf, 6, "a_st_dis2");
1253                 goto unlock;
1254         case ZFCP_ERP_NOMEM:
1255                 /* no memory to continue immediately, let it sleep */
1256                 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1257                         ++adapter->erp_low_mem_count;
1258                         erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1259                 }
1260                 /* This condition is true if there is no memory available
1261                    for any erp_action on this adapter. This implies that there
1262                    are no elements in the memory pool(s) left for erp_actions.
1263                    This might happen if an erp_action that used a memory pool
1264                    element was timed out.
1265                  */
1266                 if (adapter->erp_total_count == adapter->erp_low_mem_count) {
1267                         debug_text_event(adapter->erp_dbf, 3, "a_st_lowmem");
1268                         ZFCP_LOG_NORMAL("error: no mempool elements available, "
1269                                         "restarting I/O on adapter %s "
1270                                         "to free mempool\n",
1271                                         zfcp_get_busid_by_adapter(adapter));
1272                         zfcp_erp_adapter_reopen_internal(adapter, 0);
1273                 } else {
1274                 debug_text_event(adapter->erp_dbf, 2, "a_st_memw");
1275                 retval = zfcp_erp_strategy_memwait(erp_action);
1276                 }
1277                 goto unlock;
1278         case ZFCP_ERP_CONTINUES:
1279                 /* leave since this action runs asynchronously */
1280                 debug_text_event(adapter->erp_dbf, 6, "a_st_cont");
1281                 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1282                         --adapter->erp_low_mem_count;
1283                         erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1284                 }
1285                 goto unlock;
1286         }
1287         /* ok, finished action (whatever its result is) */
1288
1289         /* check for unrecoverable targets */
1290         retval = zfcp_erp_strategy_check_target(erp_action, retval);
1291
1292         /* action must be dequeued (here to allow for further ones) */
1293         zfcp_erp_action_dequeue(erp_action);
1294
1295         /*
1296          * put this target through the erp mill again if someone has
1297          * requested to change the status of a target being online 
1298          * to offline or the other way around
1299          * (old retval is preserved if nothing has to be done here)
1300          */
1301         retval = zfcp_erp_strategy_statechange(action, status, adapter,
1302                                                port, unit, retval);
1303
1304         /*
1305          * leave if target is in permanent error state or if
1306          * action is repeated in order to process state change
1307          */
1308         if (retval == ZFCP_ERP_EXIT) {
1309                 debug_text_event(adapter->erp_dbf, 2, "a_st_exit");
1310                 goto unlock;
1311         }
1312
1313         /* trigger follow up actions */
1314         zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
1315
1316  unlock:
1317         write_unlock(&adapter->erp_lock);
1318         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1319         
1320         if (retval != ZFCP_ERP_CONTINUES)
1321                 zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
1322
1323         /*
1324          * a few tasks remain when the erp queues are empty
1325          * (don't do that if the last action evaluated was dismissed
1326          * since this clearly indicates that there is more to come) :
1327          * - close the name server port if it is open yet
1328          *   (enqueues another [probably] final action)
1329          * - otherwise, wake up whoever wants to be woken when we are
1330          *   done with erp
1331          */
1332         if (retval != ZFCP_ERP_DISMISSED)
1333                 zfcp_erp_strategy_check_queues(adapter);
1334
1335         debug_text_event(adapter->erp_dbf, 6, "a_st_done");
1336
1337         return retval;
1338 }
1339
1340 /*
1341  * function:    
1342  *
1343  * purpose:     
1344  *
1345  * returns:     ZFCP_ERP_DISMISSED      - if action has been dismissed
1346  *              retval                  - otherwise
1347  */
1348 static int
1349 zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
1350 {
1351         struct zfcp_adapter *adapter = erp_action->adapter;
1352
1353         zfcp_erp_strategy_check_fsfreq(erp_action);
1354
1355         debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1356         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1357                 debug_text_event(adapter->erp_dbf, 3, "a_stcd_dis");
1358                 zfcp_erp_action_dequeue(erp_action);
1359                 retval = ZFCP_ERP_DISMISSED;
1360         } else
1361                 debug_text_event(adapter->erp_dbf, 5, "a_stcd_nodis");
1362
1363         return retval;
1364 }
1365
1366 /*
1367  * function:    
1368  *
1369  * purpose:     
1370  *
1371  * returns:
1372  */
1373 static int
1374 zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1375 {
1376         int retval = ZFCP_ERP_FAILED;
1377         struct zfcp_adapter *adapter = erp_action->adapter;
1378
1379         /*
1380          * try to execute/continue action as far as possible,
1381          * note: no lock in subsequent strategy routines
1382          * (this allows these routine to call schedule, e.g.
1383          * kmalloc with such flags or qdio_initialize & friends)
1384          * Note: in case of timeout, the seperate strategies will fail
1385          * anyhow. No need for a special action. Even worse, a nameserver
1386          * failure would not wake up waiting ports without the call.
1387          */
1388         switch (erp_action->action) {
1389
1390         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1391                 retval = zfcp_erp_adapter_strategy(erp_action);
1392                 break;
1393
1394         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1395                 retval = zfcp_erp_port_forced_strategy(erp_action);
1396                 break;
1397
1398         case ZFCP_ERP_ACTION_REOPEN_PORT:
1399                 retval = zfcp_erp_port_strategy(erp_action);
1400                 break;
1401
1402         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1403                 retval = zfcp_erp_unit_strategy(erp_action);
1404                 break;
1405
1406         default:
1407                 debug_text_exception(adapter->erp_dbf, 1, "a_stda_bug");
1408                 debug_event(adapter->erp_dbf, 1, &erp_action->action,
1409                             sizeof (int));
1410                 ZFCP_LOG_NORMAL("bug: unknown erp action requested on "
1411                                 "adapter %s (action=%d)\n",
1412                                 zfcp_get_busid_by_adapter(erp_action->adapter),
1413                                 erp_action->action);
1414         }
1415
1416         return retval;
1417 }
1418
1419 /*
1420  * function:    
1421  *
1422  * purpose:     triggers retry of this action after a certain amount of time
1423  *              by means of timer provided by erp_action
1424  *
1425  * returns:     ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
1426  */
1427 static int
1428 zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
1429 {
1430         int retval = ZFCP_ERP_CONTINUES;
1431         struct zfcp_adapter *adapter = erp_action->adapter;
1432
1433         debug_text_event(adapter->erp_dbf, 6, "a_mwinit");
1434         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
1435         init_timer(&erp_action->timer);
1436         erp_action->timer.function = zfcp_erp_memwait_handler;
1437         erp_action->timer.data = (unsigned long) erp_action;
1438         erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
1439         add_timer(&erp_action->timer);
1440
1441         return retval;
1442 }
1443
1444 /* 
1445  * function:    zfcp_erp_adapter_failed
1446  *
1447  * purpose:     sets the adapter and all underlying devices to ERP_FAILED
1448  *
1449  */
1450 void
1451 zfcp_erp_adapter_failed(struct zfcp_adapter *adapter)
1452 {
1453         zfcp_erp_modify_adapter_status(adapter,
1454                                        ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1455         ZFCP_LOG_NORMAL("adapter erp failed on adapter %s\n",
1456                         zfcp_get_busid_by_adapter(adapter));
1457         debug_text_event(adapter->erp_dbf, 2, "a_afail");
1458 }
1459
1460 /* 
1461  * function:    zfcp_erp_port_failed
1462  *
1463  * purpose:     sets the port and all underlying devices to ERP_FAILED
1464  *
1465  */
1466 void
1467 zfcp_erp_port_failed(struct zfcp_port *port)
1468 {
1469         zfcp_erp_modify_port_status(port,
1470                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1471
1472         if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1473                 ZFCP_LOG_NORMAL("port erp failed (adapter %s, "
1474                                 "port d_id=0x%08x)\n",
1475                                 zfcp_get_busid_by_port(port), port->d_id);
1476         else
1477                 ZFCP_LOG_NORMAL("port erp failed (adapter %s, wwpn=0x%016Lx)\n",
1478                                 zfcp_get_busid_by_port(port), port->wwpn);
1479
1480         debug_text_event(port->adapter->erp_dbf, 2, "p_pfail");
1481         debug_event(port->adapter->erp_dbf, 2, &port->wwpn, sizeof (wwn_t));
1482 }
1483
1484 /* 
1485  * function:    zfcp_erp_unit_failed
1486  *
1487  * purpose:     sets the unit to ERP_FAILED
1488  *
1489  */
1490 void
1491 zfcp_erp_unit_failed(struct zfcp_unit *unit)
1492 {
1493         zfcp_erp_modify_unit_status(unit,
1494                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1495
1496         ZFCP_LOG_NORMAL("unit erp failed on unit 0x%016Lx on port 0x%016Lx "
1497                         " on adapter %s\n", unit->fcp_lun,
1498                         unit->port->wwpn, zfcp_get_busid_by_unit(unit));
1499         debug_text_event(unit->port->adapter->erp_dbf, 2, "u_ufail");
1500         debug_event(unit->port->adapter->erp_dbf, 2,
1501                     &unit->fcp_lun, sizeof (fcp_lun_t));
1502 }
1503
1504 /*
1505  * function:    zfcp_erp_strategy_check_target
1506  *
1507  * purpose:     increments the erp action count on the device currently in
1508  *              recovery if the action failed or resets the count in case of
1509  *              success. If a maximum count is exceeded the device is marked
1510  *              as ERP_FAILED.
1511  *              The 'blocked' state of a target which has been recovered
1512  *              successfully is reset.
1513  *
1514  * returns:     ZFCP_ERP_CONTINUES      - action continues (not considered)
1515  *              ZFCP_ERP_SUCCEEDED      - action finished successfully 
1516  *              ZFCP_ERP_EXIT           - action failed and will not continue
1517  */
1518 static int
1519 zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
1520 {
1521         struct zfcp_adapter *adapter = erp_action->adapter;
1522         struct zfcp_port *port = erp_action->port;
1523         struct zfcp_unit *unit = erp_action->unit;
1524
1525         debug_text_event(adapter->erp_dbf, 5, "a_stct_norm");
1526         debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1527         debug_event(adapter->erp_dbf, 5, &result, sizeof (int));
1528
1529         switch (erp_action->action) {
1530
1531         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1532                 result = zfcp_erp_strategy_check_unit(unit, result);
1533                 break;
1534
1535         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1536         case ZFCP_ERP_ACTION_REOPEN_PORT:
1537                 result = zfcp_erp_strategy_check_port(port, result);
1538                 break;
1539
1540         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1541                 result = zfcp_erp_strategy_check_adapter(adapter, result);
1542                 break;
1543         }
1544
1545         return result;
1546 }
1547
1548 /*
1549  * function:    
1550  *
1551  * purpose:     
1552  *
1553  * returns:
1554  */
1555 static int
1556 zfcp_erp_strategy_statechange(int action,
1557                               u32 status,
1558                               struct zfcp_adapter *adapter,
1559                               struct zfcp_port *port,
1560                               struct zfcp_unit *unit, int retval)
1561 {
1562         debug_text_event(adapter->erp_dbf, 3, "a_stsc");
1563         debug_event(adapter->erp_dbf, 3, &action, sizeof (int));
1564
1565         switch (action) {
1566
1567         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1568                 if (zfcp_erp_strategy_statechange_detected(&adapter->status,
1569                                                            status)) {
1570                         zfcp_erp_adapter_reopen_internal(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
1571                         retval = ZFCP_ERP_EXIT;
1572                 }
1573                 break;
1574
1575         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1576         case ZFCP_ERP_ACTION_REOPEN_PORT:
1577                 if (zfcp_erp_strategy_statechange_detected(&port->status,
1578                                                            status)) {
1579                         zfcp_erp_port_reopen_internal(port, ZFCP_STATUS_COMMON_ERP_FAILED);
1580                         retval = ZFCP_ERP_EXIT;
1581                 }
1582                 break;
1583
1584         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1585                 if (zfcp_erp_strategy_statechange_detected(&unit->status,
1586                                                            status)) {
1587                         zfcp_erp_unit_reopen_internal(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
1588                         retval = ZFCP_ERP_EXIT;
1589                 }
1590                 break;
1591         }
1592
1593         return retval;
1594 }
1595
1596 /*
1597  * function:    
1598  *
1599  * purpose:     
1600  *
1601  * returns:
1602  */
1603 static inline int
1604 zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
1605 {
1606         return
1607             /* take it online */
1608             (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1609              (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
1610             /* take it offline */
1611             (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1612              !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
1613 }
1614
1615 /*
1616  * function:    
1617  *
1618  * purpose:     
1619  *
1620  * returns:
1621  */
1622 static int
1623 zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
1624 {
1625         debug_text_event(unit->port->adapter->erp_dbf, 5, "u_stct");
1626         debug_event(unit->port->adapter->erp_dbf, 5, &unit->fcp_lun,
1627                     sizeof (fcp_lun_t));
1628
1629         switch (result) {
1630         case ZFCP_ERP_SUCCEEDED :
1631                 atomic_set(&unit->erp_counter, 0);
1632                 zfcp_erp_unit_unblock(unit);
1633                 break;
1634         case ZFCP_ERP_FAILED :
1635                 atomic_inc(&unit->erp_counter);
1636                 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
1637                         zfcp_erp_unit_failed(unit);
1638                 break;
1639         case ZFCP_ERP_EXIT :
1640                 /* nothing */
1641                 break;
1642         }
1643
1644         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
1645                 zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
1646                 result = ZFCP_ERP_EXIT;
1647         }
1648
1649         return result;
1650 }
1651
1652 /*
1653  * function:    
1654  *
1655  * purpose:     
1656  *
1657  * returns:
1658  */
1659 static int
1660 zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1661 {
1662         debug_text_event(port->adapter->erp_dbf, 5, "p_stct");
1663         debug_event(port->adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
1664
1665         switch (result) {
1666         case ZFCP_ERP_SUCCEEDED :
1667                 atomic_set(&port->erp_counter, 0);
1668                 zfcp_erp_port_unblock(port);
1669                 break;
1670         case ZFCP_ERP_FAILED :
1671                 atomic_inc(&port->erp_counter);
1672                 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
1673                         zfcp_erp_port_failed(port);
1674                 break;
1675         case ZFCP_ERP_EXIT :
1676                 /* nothing */
1677                 break;
1678         }
1679
1680         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
1681                 zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
1682                 result = ZFCP_ERP_EXIT;
1683         }
1684
1685         return result;
1686 }
1687
1688 /*
1689  * function:    
1690  *
1691  * purpose:     
1692  *
1693  * returns:
1694  */
1695 static int
1696 zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
1697 {
1698         debug_text_event(adapter->erp_dbf, 5, "a_stct");
1699
1700         switch (result) {
1701         case ZFCP_ERP_SUCCEEDED :
1702                 atomic_set(&adapter->erp_counter, 0);
1703                 zfcp_erp_adapter_unblock(adapter);
1704                 break;
1705         case ZFCP_ERP_FAILED :
1706                 atomic_inc(&adapter->erp_counter);
1707                 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
1708                         zfcp_erp_adapter_failed(adapter);
1709                 break;
1710         case ZFCP_ERP_EXIT :
1711                 /* nothing */
1712                 break;
1713         }
1714
1715         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
1716                 zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
1717                 result = ZFCP_ERP_EXIT;
1718         }
1719
1720         return result;
1721 }
1722
1723 /*
1724  * function:    
1725  *
1726  * purpose:     remaining things in good cases,
1727  *              escalation in bad cases
1728  *
1729  * returns:
1730  */
1731 static int
1732 zfcp_erp_strategy_followup_actions(int action,
1733                                    struct zfcp_adapter *adapter,
1734                                    struct zfcp_port *port,
1735                                    struct zfcp_unit *unit, int status)
1736 {
1737         debug_text_event(adapter->erp_dbf, 5, "a_stfol");
1738         debug_event(adapter->erp_dbf, 5, &action, sizeof (int));
1739
1740         /* initiate follow-up actions depending on success of finished action */
1741         switch (action) {
1742
1743         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1744                 if (status == ZFCP_ERP_SUCCEEDED)
1745                         zfcp_erp_port_reopen_all_internal(adapter, 0);
1746                 else
1747                         zfcp_erp_adapter_reopen_internal(adapter, 0);
1748                 break;
1749
1750         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1751                 if (status == ZFCP_ERP_SUCCEEDED)
1752                         zfcp_erp_port_reopen_internal(port, 0);
1753                 else
1754                         zfcp_erp_adapter_reopen_internal(adapter, 0);
1755                 break;
1756
1757         case ZFCP_ERP_ACTION_REOPEN_PORT:
1758                 if (status == ZFCP_ERP_SUCCEEDED)
1759                         zfcp_erp_unit_reopen_all_internal(port, 0);
1760                 else
1761                         zfcp_erp_port_forced_reopen_internal(port, 0);
1762                 break;
1763
1764         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1765                 if (status == ZFCP_ERP_SUCCEEDED) ;     /* no further action */
1766                 else
1767                         zfcp_erp_port_reopen_internal(unit->port, 0);
1768                 break;
1769         }
1770
1771         return 0;
1772 }
1773
1774 /*
1775  * function:    
1776  *
1777  * purpose:     
1778  *
1779  * returns:
1780  */
1781 static int
1782 zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
1783 {
1784         unsigned long flags;
1785
1786         read_lock_irqsave(&zfcp_data.config_lock, flags);
1787         read_lock(&adapter->erp_lock);
1788         if (list_empty(&adapter->erp_ready_head) &&
1789             list_empty(&adapter->erp_running_head)) {
1790                         debug_text_event(adapter->erp_dbf, 4, "a_cq_wake");
1791                         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1792                                           &adapter->status);
1793                         wake_up(&adapter->erp_done_wqh);
1794         } else
1795                 debug_text_event(adapter->erp_dbf, 5, "a_cq_notempty");
1796         read_unlock(&adapter->erp_lock);
1797         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1798
1799         return 0;
1800 }
1801
1802 /**
1803  * zfcp_erp_wait - wait for completion of error recovery on an adapter
1804  * @adapter: adapter for which to wait for completion of its error recovery
1805  * Return: 0
1806  */
1807 int
1808 zfcp_erp_wait(struct zfcp_adapter *adapter)
1809 {
1810         int retval = 0;
1811
1812         wait_event(adapter->erp_done_wqh,
1813                    !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1814                                      &adapter->status));
1815
1816         return retval;
1817 }
1818
1819 /*
1820  * function:    zfcp_erp_modify_adapter_status
1821  *
1822  * purpose:     
1823  *
1824  */
1825 void
1826 zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter,
1827                                u32 mask, int set_or_clear)
1828 {
1829         struct zfcp_port *port;
1830         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1831
1832         if (set_or_clear == ZFCP_SET) {
1833                 atomic_set_mask(mask, &adapter->status);
1834                 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_s");
1835         } else {
1836                 atomic_clear_mask(mask, &adapter->status);
1837                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1838                         atomic_set(&adapter->erp_counter, 0);
1839                 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_c");
1840         }
1841         debug_event(adapter->erp_dbf, 3, &mask, sizeof (u32));
1842
1843         /* Deal with all underlying devices, only pass common_mask */
1844         if (common_mask)
1845                 list_for_each_entry(port, &adapter->port_list_head, list)
1846                     zfcp_erp_modify_port_status(port, common_mask,
1847                                                 set_or_clear);
1848 }
1849
1850 /*
1851  * function:    zfcp_erp_modify_port_status
1852  *
1853  * purpose:     sets the port and all underlying devices to ERP_FAILED
1854  *
1855  */
1856 void
1857 zfcp_erp_modify_port_status(struct zfcp_port *port, u32 mask, int set_or_clear)
1858 {
1859         struct zfcp_unit *unit;
1860         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1861
1862         if (set_or_clear == ZFCP_SET) {
1863                 atomic_set_mask(mask, &port->status);
1864                 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_s");
1865         } else {
1866                 atomic_clear_mask(mask, &port->status);
1867                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1868                         atomic_set(&port->erp_counter, 0);
1869                 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_c");
1870         }
1871         debug_event(port->adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
1872         debug_event(port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1873
1874         /* Modify status of all underlying devices, only pass common mask */
1875         if (common_mask)
1876                 list_for_each_entry(unit, &port->unit_list_head, list)
1877                     zfcp_erp_modify_unit_status(unit, common_mask,
1878                                                 set_or_clear);
1879 }
1880
1881 /*
1882  * function:    zfcp_erp_modify_unit_status
1883  *
1884  * purpose:     sets the unit to ERP_FAILED
1885  *
1886  */
1887 void
1888 zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u32 mask, int set_or_clear)
1889 {
1890         if (set_or_clear == ZFCP_SET) {
1891                 atomic_set_mask(mask, &unit->status);
1892                 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_s");
1893         } else {
1894                 atomic_clear_mask(mask, &unit->status);
1895                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1896                         atomic_set(&unit->erp_counter, 0);
1897                 }
1898                 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_c");
1899         }
1900         debug_event(unit->port->adapter->erp_dbf, 3, &unit->fcp_lun,
1901                     sizeof (fcp_lun_t));
1902         debug_event(unit->port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1903 }
1904
1905 /*
1906  * function:    
1907  *
1908  * purpose:     Wrappper for zfcp_erp_port_reopen_all_internal
1909  *              used to ensure the correct locking
1910  *
1911  * returns:     0       - initiated action succesfully
1912  *              <0      - failed to initiate action
1913  */
1914 int
1915 zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask)
1916 {
1917         int retval;
1918         unsigned long flags;
1919
1920         read_lock_irqsave(&zfcp_data.config_lock, flags);
1921         write_lock(&adapter->erp_lock);
1922         retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask);
1923         write_unlock(&adapter->erp_lock);
1924         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1925
1926         return retval;
1927 }
1928
1929 /*
1930  * function:    
1931  *
1932  * purpose:     
1933  *
1934  * returns:     FIXME
1935  */
1936 static int
1937 zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter, int clear_mask)
1938 {
1939         int retval = 0;
1940         struct zfcp_port *port;
1941
1942         list_for_each_entry(port, &adapter->port_list_head, list)
1943                 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1944                         zfcp_erp_port_reopen_internal(port, clear_mask);
1945
1946         return retval;
1947 }
1948
1949 /*
1950  * function:    
1951  *
1952  * purpose:     
1953  *
1954  * returns:     FIXME
1955  */
1956 static int
1957 zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port, int clear_mask)
1958 {
1959         int retval = 0;
1960         struct zfcp_unit *unit;
1961
1962         list_for_each_entry(unit, &port->unit_list_head, list)
1963             zfcp_erp_unit_reopen_internal(unit, clear_mask);
1964
1965         return retval;
1966 }
1967
1968 /*
1969  * function:    
1970  *
1971  * purpose:     this routine executes the 'Reopen Adapter' action
1972  *              (the entire action is processed synchronously, since
1973  *              there are no actions which might be run concurrently
1974  *              per definition)
1975  *
1976  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
1977  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1978  */
1979 static int
1980 zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
1981 {
1982         int retval;
1983         struct zfcp_adapter *adapter = erp_action->adapter;
1984
1985         retval = zfcp_erp_adapter_strategy_close(erp_action);
1986         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1987                 retval = ZFCP_ERP_EXIT;
1988         else
1989                 retval = zfcp_erp_adapter_strategy_open(erp_action);
1990
1991         debug_text_event(adapter->erp_dbf, 3, "a_ast/ret");
1992         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
1993         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
1994
1995         if (retval == ZFCP_ERP_FAILED) {
1996                 ZFCP_LOG_INFO("Waiting to allow the adapter %s "
1997                               "to recover itself\n",
1998                               zfcp_get_busid_by_adapter(adapter));
1999                 msleep(jiffies_to_msecs(ZFCP_TYPE2_RECOVERY_TIME));
2000         }
2001
2002         return retval;
2003 }
2004
2005 /*
2006  * function:    
2007  *
2008  * purpose:     
2009  *
2010  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
2011  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2012  */
2013 static int
2014 zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
2015 {
2016         int retval;
2017
2018         atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
2019                         &erp_action->adapter->status);
2020         retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
2021         atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
2022                           &erp_action->adapter->status);
2023
2024         return retval;
2025 }
2026
2027 /*
2028  * function:    
2029  *
2030  * purpose:     
2031  *
2032  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
2033  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2034  */
2035 static int
2036 zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
2037 {
2038         int retval;
2039
2040         atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
2041                         &erp_action->adapter->status);
2042         retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
2043         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
2044                           &erp_action->adapter->status);
2045
2046         return retval;
2047 }
2048
2049 /*
2050  * function:    zfcp_register_adapter
2051  *
2052  * purpose:     allocate the irq associated with this devno and register
2053  *              the FSF adapter with the SCSI stack
2054  *
2055  * returns:     
2056  */
2057 static int
2058 zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
2059 {
2060         int retval = ZFCP_ERP_SUCCEEDED;
2061
2062         if (close)
2063                 goto close_only;
2064
2065         retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
2066         if (retval != ZFCP_ERP_SUCCEEDED)
2067                 goto failed_qdio;
2068
2069         retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
2070         if (retval != ZFCP_ERP_SUCCEEDED)
2071                 goto failed_openfcp;
2072
2073         atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
2074         goto out;
2075
2076  close_only:
2077         atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
2078                           &erp_action->adapter->status);
2079
2080  failed_openfcp:
2081         zfcp_erp_adapter_strategy_close_qdio(erp_action);
2082         zfcp_erp_adapter_strategy_close_fsf(erp_action);
2083  failed_qdio:
2084  out:
2085         return retval;
2086 }
2087
2088 /*
2089  * function:    zfcp_qdio_init
2090  *
2091  * purpose:     setup QDIO operation for specified adapter
2092  *
2093  * returns:     0 - successful setup
2094  *              !0 - failed setup
2095  */
2096 int
2097 zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
2098 {
2099         int retval;
2100         int i;
2101         volatile struct qdio_buffer_element *sbale;
2102         struct zfcp_adapter *adapter = erp_action->adapter;
2103
2104         if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
2105                 ZFCP_LOG_NORMAL("bug: second attempt to set up QDIO on "
2106                                 "adapter %s\n",
2107                                 zfcp_get_busid_by_adapter(adapter));
2108                 goto failed_sanity;
2109         }
2110
2111         if (qdio_establish(&adapter->qdio_init_data) != 0) {
2112                 ZFCP_LOG_INFO("error: establishment of QDIO queues failed "
2113                               "on adapter %s\n",
2114                               zfcp_get_busid_by_adapter(adapter));
2115                 goto failed_qdio_establish;
2116         }
2117         debug_text_event(adapter->erp_dbf, 3, "qdio_est");
2118
2119         if (qdio_activate(adapter->ccw_device, 0) != 0) {
2120                 ZFCP_LOG_INFO("error: activation of QDIO queues failed "
2121                               "on adapter %s\n",
2122                               zfcp_get_busid_by_adapter(adapter));
2123                 goto failed_qdio_activate;
2124         }
2125         debug_text_event(adapter->erp_dbf, 3, "qdio_act");
2126
2127         /*
2128          * put buffers into response queue,
2129          */
2130         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
2131                 sbale = &(adapter->response_queue.buffer[i]->element[0]);
2132                 sbale->length = 0;
2133                 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
2134                 sbale->addr = 0;
2135         }
2136
2137         ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
2138                        "queue_no=%i, index_in_queue=%i, count=%i)\n",
2139                        zfcp_get_busid_by_adapter(adapter),
2140                        QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q);
2141
2142         retval = do_QDIO(adapter->ccw_device,
2143                          QDIO_FLAG_SYNC_INPUT,
2144                          0, 0, QDIO_MAX_BUFFERS_PER_Q, NULL);
2145
2146         if (retval) {
2147                 ZFCP_LOG_NORMAL("bug: setup of QDIO failed (retval=%d)\n",
2148                                 retval);
2149                 goto failed_do_qdio;
2150         } else {
2151                 adapter->response_queue.free_index = 0;
2152                 atomic_set(&adapter->response_queue.free_count, 0);
2153                 ZFCP_LOG_DEBUG("%i buffers successfully enqueued to "
2154                                "response queue\n", QDIO_MAX_BUFFERS_PER_Q);
2155         }
2156         /* set index of first avalable SBALS / number of available SBALS */
2157         adapter->request_queue.free_index = 0;
2158         atomic_set(&adapter->request_queue.free_count, QDIO_MAX_BUFFERS_PER_Q);
2159         adapter->request_queue.distance_from_int = 0;
2160
2161         /* initialize waitqueue used to wait for free SBALs in requests queue */
2162         init_waitqueue_head(&adapter->request_wq);
2163
2164         /* ok, we did it - skip all cleanups for different failures */
2165         atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
2166         retval = ZFCP_ERP_SUCCEEDED;
2167         goto out;
2168
2169  failed_do_qdio:
2170         /* NOP */
2171
2172  failed_qdio_activate:
2173         debug_text_event(adapter->erp_dbf, 3, "qdio_down1a");
2174         while (qdio_shutdown(adapter->ccw_device,
2175                              QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
2176                 msleep(1000);
2177         debug_text_event(adapter->erp_dbf, 3, "qdio_down1b");
2178
2179  failed_qdio_establish:
2180  failed_sanity:
2181         retval = ZFCP_ERP_FAILED;
2182
2183  out:
2184         return retval;
2185 }
2186
2187 /*
2188  * function:    zfcp_qdio_cleanup
2189  *
2190  * purpose:     cleans up QDIO operation for the specified adapter
2191  *
2192  * returns:     0 - successful cleanup
2193  *              !0 - failed cleanup
2194  */
2195 int
2196 zfcp_erp_adapter_strategy_close_qdio(struct zfcp_erp_action *erp_action)
2197 {
2198         int retval = ZFCP_ERP_SUCCEEDED;
2199         int first_used;
2200         int used_count;
2201         struct zfcp_adapter *adapter = erp_action->adapter;
2202
2203         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
2204                 ZFCP_LOG_DEBUG("error: attempt to shut down inactive QDIO "
2205                                "queues on adapter %s\n",
2206                                zfcp_get_busid_by_adapter(adapter));
2207                 retval = ZFCP_ERP_FAILED;
2208                 goto out;
2209         }
2210
2211         /*
2212          * Get queue_lock and clear QDIOUP flag. Thus it's guaranteed that
2213          * do_QDIO won't be called while qdio_shutdown is in progress.
2214          */
2215
2216         write_lock_irq(&adapter->request_queue.queue_lock);
2217         atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
2218         write_unlock_irq(&adapter->request_queue.queue_lock);
2219
2220         debug_text_event(adapter->erp_dbf, 3, "qdio_down2a");
2221         while (qdio_shutdown(adapter->ccw_device,
2222                              QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
2223                 msleep(1000);
2224         debug_text_event(adapter->erp_dbf, 3, "qdio_down2b");
2225
2226         /*
2227          * First we had to stop QDIO operation.
2228          * Now it is safe to take the following actions.
2229          */
2230
2231         /* Cleanup only necessary when there are unacknowledged buffers */
2232         if (atomic_read(&adapter->request_queue.free_count)
2233             < QDIO_MAX_BUFFERS_PER_Q) {
2234                 first_used = (adapter->request_queue.free_index +
2235                               atomic_read(&adapter->request_queue.free_count))
2236                         % QDIO_MAX_BUFFERS_PER_Q;
2237                 used_count = QDIO_MAX_BUFFERS_PER_Q -
2238                         atomic_read(&adapter->request_queue.free_count);
2239                 zfcp_qdio_zero_sbals(adapter->request_queue.buffer,
2240                                      first_used, used_count);
2241         }
2242         adapter->response_queue.free_index = 0;
2243         atomic_set(&adapter->response_queue.free_count, 0);
2244         adapter->request_queue.free_index = 0;
2245         atomic_set(&adapter->request_queue.free_count, 0);
2246         adapter->request_queue.distance_from_int = 0;
2247  out:
2248         return retval;
2249 }
2250
2251 /*
2252  * function:    zfcp_fsf_init
2253  *
2254  * purpose:     initializes FSF operation for the specified adapter
2255  *
2256  * returns:     0 - succesful initialization of FSF operation
2257  *              !0 - failed to initialize FSF operation
2258  */
2259 static int
2260 zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
2261 {
2262         int xconfig, xport;
2263
2264         if (atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
2265                              &erp_action->adapter->status)) {
2266                 zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
2267                 atomic_set(&erp_action->adapter->erp_counter, 0);
2268                 return ZFCP_ERP_FAILED;
2269         }
2270
2271         xconfig = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
2272         xport   = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
2273         if ((xconfig == ZFCP_ERP_FAILED) || (xport == ZFCP_ERP_FAILED))
2274                 return ZFCP_ERP_FAILED;
2275
2276         return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
2277 }
2278
2279 /*
2280  * function:    
2281  *
2282  * purpose:     
2283  *
2284  * returns:
2285  */
2286 static int
2287 zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
2288 {
2289         int retval = ZFCP_ERP_SUCCEEDED;
2290         int retries;
2291         int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
2292         struct zfcp_adapter *adapter = erp_action->adapter;
2293
2294         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
2295
2296         for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
2297                 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2298                                   &adapter->status);
2299                 ZFCP_LOG_DEBUG("Doing exchange config data\n");
2300                 write_lock(&adapter->erp_lock);
2301                 zfcp_erp_action_to_running(erp_action);
2302                 write_unlock(&adapter->erp_lock);
2303                 zfcp_erp_timeout_init(erp_action);
2304                 if (zfcp_fsf_exchange_config_data(erp_action)) {
2305                         retval = ZFCP_ERP_FAILED;
2306                         debug_text_event(adapter->erp_dbf, 5, "a_fstx_xf");
2307                         ZFCP_LOG_INFO("error:  initiation of exchange of "
2308                                       "configuration data failed for "
2309                                       "adapter %s\n",
2310                                       zfcp_get_busid_by_adapter(adapter));
2311                         break;
2312                 }
2313                 debug_text_event(adapter->erp_dbf, 6, "a_fstx_xok");
2314                 ZFCP_LOG_DEBUG("Xchange underway\n");
2315
2316                 /*
2317                  * Why this works:
2318                  * Both the normal completion handler as well as the timeout
2319                  * handler will do an 'up' when the 'exchange config data'
2320                  * request completes or times out. Thus, the signal to go on
2321                  * won't be lost utilizing this semaphore.
2322                  * Furthermore, this 'adapter_reopen' action is
2323                  * guaranteed to be the only action being there (highest action
2324                  * which prevents other actions from being created).
2325                  * Resulting from that, the wake signal recognized here
2326                  * _must_ be the one belonging to the 'exchange config
2327                  * data' request.
2328                  */
2329                 down(&adapter->erp_ready_sem);
2330                 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2331                         ZFCP_LOG_INFO("error: exchange of configuration data "
2332                                       "for adapter %s timed out\n",
2333                                       zfcp_get_busid_by_adapter(adapter));
2334                         break;
2335                 }
2336
2337                 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2338                                      &adapter->status))
2339                         break;
2340
2341                 ZFCP_LOG_DEBUG("host connection still initialising... "
2342                                "waiting and retrying...\n");
2343                 /* sleep a little bit before retry */
2344                 msleep(jiffies_to_msecs(sleep));
2345                 sleep *= 2;
2346         }
2347
2348         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2349                               &adapter->status)) {
2350                 ZFCP_LOG_INFO("error: exchange of configuration data for "
2351                               "adapter %s failed\n",
2352                               zfcp_get_busid_by_adapter(adapter));
2353                 retval = ZFCP_ERP_FAILED;
2354         }
2355
2356         return retval;
2357 }
2358
2359 static int
2360 zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
2361 {
2362         int retval = ZFCP_ERP_SUCCEEDED;
2363         int retries;
2364         int sleep;
2365         struct zfcp_adapter *adapter = erp_action->adapter;
2366
2367         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2368
2369         for (retries = 0; ; retries++) {
2370                 ZFCP_LOG_DEBUG("Doing exchange port data\n");
2371                 zfcp_erp_action_to_running(erp_action);
2372                 zfcp_erp_timeout_init(erp_action);
2373                 if (zfcp_fsf_exchange_port_data(erp_action, adapter, NULL)) {
2374                         retval = ZFCP_ERP_FAILED;
2375                         debug_text_event(adapter->erp_dbf, 5, "a_fstx_xf");
2376                         ZFCP_LOG_INFO("error: initiation of exchange of "
2377                                       "port data failed for adapter %s\n",
2378                                       zfcp_get_busid_by_adapter(adapter));
2379                         break;
2380                 }
2381                 debug_text_event(adapter->erp_dbf, 6, "a_fstx_xok");
2382                 ZFCP_LOG_DEBUG("Xchange underway\n");
2383
2384                 /*
2385                  * Why this works:
2386                  * Both the normal completion handler as well as the timeout
2387                  * handler will do an 'up' when the 'exchange port data'
2388                  * request completes or times out. Thus, the signal to go on
2389                  * won't be lost utilizing this semaphore.
2390                  * Furthermore, this 'adapter_reopen' action is
2391                  * guaranteed to be the only action being there (highest action
2392                  * which prevents other actions from being created).
2393                  * Resulting from that, the wake signal recognized here
2394                  * _must_ be the one belonging to the 'exchange port
2395                  * data' request.
2396                  */
2397                 down(&adapter->erp_ready_sem);
2398                 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2399                         ZFCP_LOG_INFO("error: exchange of port data "
2400                                       "for adapter %s timed out\n",
2401                                       zfcp_get_busid_by_adapter(adapter));
2402                         break;
2403                 }
2404
2405                 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
2406                                       &adapter->status))
2407                         break;
2408
2409                 ZFCP_LOG_DEBUG("host connection still initialising... "
2410                                "waiting and retrying...\n");
2411                 /* sleep a little bit before retry */
2412                 sleep = retries < ZFCP_EXCHANGE_PORT_DATA_SHORT_RETRIES ?
2413                                 ZFCP_EXCHANGE_PORT_DATA_SHORT_SLEEP :
2414                                 ZFCP_EXCHANGE_PORT_DATA_LONG_SLEEP;
2415                 msleep(jiffies_to_msecs(sleep));
2416         }
2417
2418         if (atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
2419                              &adapter->status)) {
2420                 ZFCP_LOG_INFO("error: exchange of port data for "
2421                               "adapter %s failed\n",
2422                               zfcp_get_busid_by_adapter(adapter));
2423                 retval = ZFCP_ERP_FAILED;
2424         }
2425
2426         return retval;
2427 }
2428
2429 /*
2430  * function:    
2431  *
2432  * purpose:     
2433  *
2434  * returns:
2435  */
2436 static int
2437 zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
2438                                               *erp_action)
2439 {
2440         int retval = ZFCP_ERP_SUCCEEDED;
2441         int temp_ret;
2442         struct zfcp_adapter *adapter = erp_action->adapter;
2443         int i;
2444
2445         adapter->status_read_failed = 0;
2446         for (i = 0; i < ZFCP_STATUS_READS_RECOM; i++) {
2447                 temp_ret = zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL);
2448                 if (temp_ret < 0) {
2449                         ZFCP_LOG_INFO("error: set-up of unsolicited status "
2450                                       "notification failed on adapter %s\n",
2451                                       zfcp_get_busid_by_adapter(adapter));
2452                         retval = ZFCP_ERP_FAILED;
2453                         i--;
2454                         break;
2455                 }
2456         }
2457
2458         return retval;
2459 }
2460
2461 /*
2462  * function:    zfcp_fsf_cleanup
2463  *
2464  * purpose:     cleanup FSF operation for specified adapter
2465  *
2466  * returns:     0 - FSF operation successfully cleaned up
2467  *              !0 - failed to cleanup FSF operation for this adapter
2468  */
2469 static int
2470 zfcp_erp_adapter_strategy_close_fsf(struct zfcp_erp_action *erp_action)
2471 {
2472         int retval = ZFCP_ERP_SUCCEEDED;
2473         struct zfcp_adapter *adapter = erp_action->adapter;
2474
2475         /*
2476          * wake waiting initiators of requests,
2477          * return SCSI commands (with error status),
2478          * clean up all requests (synchronously)
2479          */
2480         zfcp_fsf_req_dismiss_all(adapter);
2481         /* reset FSF request sequence number */
2482         adapter->fsf_req_seq_no = 0;
2483         /* all ports and units are closed */
2484         zfcp_erp_modify_adapter_status(adapter,
2485                                        ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
2486
2487         return retval;
2488 }
2489
2490 /*
2491  * function:    
2492  *
2493  * purpose:     this routine executes the 'Reopen Physical Port' action
2494  *
2495  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2496  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2497  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2498  */
2499 static int
2500 zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
2501 {
2502         int retval = ZFCP_ERP_FAILED;
2503         struct zfcp_port *port = erp_action->port;
2504         struct zfcp_adapter *adapter = erp_action->adapter;
2505
2506         switch (erp_action->step) {
2507
2508                 /*
2509                  * FIXME:
2510                  * the ULP spec. begs for waiting for oustanding commands
2511                  */
2512         case ZFCP_ERP_STEP_UNINITIALIZED:
2513                 zfcp_erp_port_strategy_clearstati(port);
2514                 /*
2515                  * it would be sufficient to test only the normal open flag
2516                  * since the phys. open flag cannot be set if the normal
2517                  * open flag is unset - however, this is for readabilty ...
2518                  */
2519                 if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
2520                                       ZFCP_STATUS_COMMON_OPEN),
2521                                      &port->status)) {
2522                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2523                                        "close physical\n", port->wwpn);
2524                         retval =
2525                             zfcp_erp_port_forced_strategy_close(erp_action);
2526                 } else
2527                         retval = ZFCP_ERP_FAILED;
2528                 break;
2529
2530         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2531                 if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
2532                                      &port->status)) {
2533                         ZFCP_LOG_DEBUG("close physical failed for port "
2534                                        "0x%016Lx\n", port->wwpn);
2535                         retval = ZFCP_ERP_FAILED;
2536                 } else
2537                         retval = ZFCP_ERP_SUCCEEDED;
2538                 break;
2539         }
2540
2541         debug_text_event(adapter->erp_dbf, 3, "p_pfst/ret");
2542         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2543         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2544         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2545
2546         return retval;
2547 }
2548
2549 /*
2550  * function:    
2551  *
2552  * purpose:     this routine executes the 'Reopen Port' action
2553  *
2554  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2555  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2556  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2557  */
2558 static int
2559 zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
2560 {
2561         int retval = ZFCP_ERP_FAILED;
2562         struct zfcp_port *port = erp_action->port;
2563         struct zfcp_adapter *adapter = erp_action->adapter;
2564
2565         switch (erp_action->step) {
2566
2567                 /*
2568                  * FIXME:
2569                  * the ULP spec. begs for waiting for oustanding commands
2570                  */
2571         case ZFCP_ERP_STEP_UNINITIALIZED:
2572                 zfcp_erp_port_strategy_clearstati(port);
2573                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2574                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2575                                        "close\n", port->wwpn);
2576                         retval = zfcp_erp_port_strategy_close(erp_action);
2577                         goto out;
2578                 }               /* else it's already closed, open it */
2579                 break;
2580
2581         case ZFCP_ERP_STEP_PORT_CLOSING:
2582                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2583                         ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
2584                                        port->wwpn);
2585                         retval = ZFCP_ERP_FAILED;
2586                         goto out;
2587                 }               /* else it's closed now, open it */
2588                 break;
2589         }
2590         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2591                 retval = ZFCP_ERP_EXIT;
2592         else
2593                 retval = zfcp_erp_port_strategy_open(erp_action);
2594
2595  out:
2596         debug_text_event(adapter->erp_dbf, 3, "p_pst/ret");
2597         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2598         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2599         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2600
2601         return retval;
2602 }
2603
2604 /*
2605  * function:    
2606  *
2607  * purpose:     
2608  *
2609  * returns:
2610  */
2611 static int
2612 zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
2613 {
2614         int retval;
2615
2616         if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2617                              &erp_action->port->status))
2618                 retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
2619         else
2620                 retval = zfcp_erp_port_strategy_open_common(erp_action);
2621
2622         return retval;
2623 }
2624
2625 /*
2626  * function:    
2627  *
2628  * purpose:     
2629  *
2630  * returns:
2631  *
2632  * FIXME(design):       currently only prepared for fabric (nameserver!)
2633  */
2634 static int
2635 zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
2636 {
2637         int retval = 0;
2638         struct zfcp_adapter *adapter = erp_action->adapter;
2639         struct zfcp_port *port = erp_action->port;
2640
2641         switch (erp_action->step) {
2642
2643         case ZFCP_ERP_STEP_UNINITIALIZED:
2644         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2645         case ZFCP_ERP_STEP_PORT_CLOSING:
2646                 if (adapter->fc_topology == FSF_TOPO_P2P) {
2647                         if (port->wwpn != adapter->peer_wwpn) {
2648                                 ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
2649                                                 "on adapter %s.\nPeer WWPN "
2650                                                 "0x%016Lx does not match\n",
2651                                                 port->wwpn,
2652                                                 zfcp_get_busid_by_adapter(adapter),
2653                                                 adapter->peer_wwpn);
2654                                 zfcp_erp_port_failed(port);
2655                                 retval = ZFCP_ERP_FAILED;
2656                                 break;
2657                         }
2658                         port->d_id = adapter->peer_d_id;
2659                         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
2660                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2661                         break;
2662                 }
2663                 if (!(adapter->nameserver_port)) {
2664                         retval = zfcp_nameserver_enqueue(adapter);
2665                         if (retval != 0) {
2666                                 ZFCP_LOG_NORMAL("error: nameserver port "
2667                                                 "unavailable for adapter %s\n",
2668                                                 zfcp_get_busid_by_adapter(adapter));
2669                                 retval = ZFCP_ERP_FAILED;
2670                                 break;
2671                         }
2672                 }
2673                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
2674                                       &adapter->nameserver_port->status)) {
2675                         ZFCP_LOG_DEBUG("nameserver port is not open -> open "
2676                                        "nameserver port\n");
2677                         /* nameserver port may live again */
2678                         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
2679                                         &adapter->nameserver_port->status);
2680                         if (zfcp_erp_port_reopen(adapter->nameserver_port, 0)
2681                             >= 0) {
2682                                 erp_action->step =
2683                                         ZFCP_ERP_STEP_NAMESERVER_OPEN;
2684                                 retval = ZFCP_ERP_CONTINUES;
2685                         } else
2686                                 retval = ZFCP_ERP_FAILED;
2687                         break;
2688                 }
2689                 /* else nameserver port is already open, fall through */
2690         case ZFCP_ERP_STEP_NAMESERVER_OPEN:
2691                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
2692                                       &adapter->nameserver_port->status)) {
2693                         ZFCP_LOG_DEBUG("open failed for nameserver port\n");
2694                         retval = ZFCP_ERP_FAILED;
2695                 } else {
2696                         ZFCP_LOG_DEBUG("nameserver port is open -> "
2697                                        "nameserver look-up for port 0x%016Lx\n",
2698                                        port->wwpn);
2699                         retval = zfcp_erp_port_strategy_open_common_lookup
2700                                 (erp_action);
2701                 }
2702                 break;
2703
2704         case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
2705                 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
2706                         if (atomic_test_mask
2707                             (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
2708                                 ZFCP_LOG_DEBUG("nameserver look-up failed "
2709                                                "for port 0x%016Lx "
2710                                                "(misconfigured WWPN?)\n",
2711                                                port->wwpn);
2712                                 zfcp_erp_port_failed(port);
2713                                 retval = ZFCP_ERP_EXIT;
2714                         } else {
2715                                 ZFCP_LOG_DEBUG("nameserver look-up failed for "
2716                                                "port 0x%016Lx\n", port->wwpn);
2717                                 retval = ZFCP_ERP_FAILED;
2718                         }
2719                 } else {
2720                         ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> "
2721                                        "trying open\n", port->wwpn, port->d_id);
2722                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2723                 }
2724                 break;
2725
2726         case ZFCP_ERP_STEP_PORT_OPENING:
2727                 /* D_ID might have changed during open */
2728                 if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
2729                                       ZFCP_STATUS_PORT_DID_DID),
2730                                      &port->status)) {
2731                         ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
2732                         retval = ZFCP_ERP_SUCCEEDED;
2733                 } else {
2734                         ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
2735                                        port->wwpn);
2736                         retval = ZFCP_ERP_FAILED;
2737                 }
2738                 break;
2739
2740         default:
2741                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2742                                 erp_action->step);
2743                 retval = ZFCP_ERP_FAILED;
2744         }
2745
2746         return retval;
2747 }
2748
2749 /*
2750  * function:    
2751  *
2752  * purpose:     
2753  *
2754  * returns:
2755  */
2756 static int
2757 zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
2758 {
2759         int retval;
2760         struct zfcp_port *port = erp_action->port;
2761
2762         switch (erp_action->step) {
2763
2764         case ZFCP_ERP_STEP_UNINITIALIZED:
2765         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2766         case ZFCP_ERP_STEP_PORT_CLOSING:
2767                 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> trying open\n",
2768                                port->wwpn, port->d_id);
2769                 retval = zfcp_erp_port_strategy_open_port(erp_action);
2770                 break;
2771
2772         case ZFCP_ERP_STEP_PORT_OPENING:
2773                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2774                         ZFCP_LOG_DEBUG("WKA port is open\n");
2775                         retval = ZFCP_ERP_SUCCEEDED;
2776                 } else {
2777                         ZFCP_LOG_DEBUG("open failed for WKA port\n");
2778                         retval = ZFCP_ERP_FAILED;
2779                 }
2780                 /* this is needed anyway (dont care for retval of wakeup) */
2781                 ZFCP_LOG_DEBUG("continue other open port operations\n");
2782                 zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
2783                 break;
2784
2785         default:
2786                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2787                                 erp_action->step);
2788                 retval = ZFCP_ERP_FAILED;
2789         }
2790
2791         return retval;
2792 }
2793
2794 /*
2795  * function:    
2796  *
2797  * purpose:     makes the erp thread continue with reopen (physical) port
2798  *              actions which have been paused until the name server port
2799  *              is opened (or failed)
2800  *
2801  * returns:     0       (a kind of void retval, its not used)
2802  */
2803 static int
2804 zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
2805                                               *ns_erp_action)
2806 {
2807         int retval = 0;
2808         unsigned long flags;
2809         struct zfcp_adapter *adapter = ns_erp_action->adapter;
2810         struct zfcp_erp_action *erp_action, *tmp;
2811
2812         read_lock_irqsave(&adapter->erp_lock, flags);
2813         list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
2814                                  list) {
2815                 debug_text_event(adapter->erp_dbf, 4, "p_pstnsw_n");
2816                 debug_event(adapter->erp_dbf, 4, &erp_action->port->wwpn,
2817                             sizeof (wwn_t));
2818                 if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
2819                         debug_text_event(adapter->erp_dbf, 3, "p_pstnsw_w");
2820                         debug_event(adapter->erp_dbf, 3,
2821                                     &erp_action->port->wwpn, sizeof (wwn_t));
2822                         if (atomic_test_mask(
2823                                     ZFCP_STATUS_COMMON_ERP_FAILED,
2824                                     &adapter->nameserver_port->status))
2825                                 zfcp_erp_port_failed(erp_action->port);
2826                         zfcp_erp_action_ready(erp_action);
2827                 }
2828         }
2829         read_unlock_irqrestore(&adapter->erp_lock, flags);
2830
2831         return retval;
2832 }
2833
2834 /*
2835  * function:    
2836  *
2837  * purpose:     
2838  *
2839  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2840  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2841  */
2842 static int
2843 zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
2844 {
2845         int retval;
2846         struct zfcp_adapter *adapter = erp_action->adapter;
2847         struct zfcp_port *port = erp_action->port;
2848
2849         zfcp_erp_timeout_init(erp_action);
2850         retval = zfcp_fsf_close_physical_port(erp_action);
2851         if (retval == -ENOMEM) {
2852                 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_nomem");
2853                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2854                 retval = ZFCP_ERP_NOMEM;
2855                 goto out;
2856         }
2857         erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
2858         if (retval != 0) {
2859                 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_cpf");
2860                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2861                 /* could not send 'open', fail */
2862                 retval = ZFCP_ERP_FAILED;
2863                 goto out;
2864         }
2865         debug_text_event(adapter->erp_dbf, 6, "o_pfstc_cpok");
2866         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2867         retval = ZFCP_ERP_CONTINUES;
2868  out:
2869         return retval;
2870 }
2871
2872 /*
2873  * function:    
2874  *
2875  * purpose:     
2876  *
2877  * returns:
2878  */
2879 static int
2880 zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
2881 {
2882         int retval = 0;
2883         struct zfcp_adapter *adapter = port->adapter;
2884
2885         debug_text_event(adapter->erp_dbf, 5, "p_pstclst");
2886         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2887
2888         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2889                           ZFCP_STATUS_COMMON_CLOSING |
2890                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
2891                           ZFCP_STATUS_PORT_DID_DID |
2892                           ZFCP_STATUS_PORT_PHYS_CLOSING |
2893                           ZFCP_STATUS_PORT_INVALID_WWPN,
2894                           &port->status);
2895         return retval;
2896 }
2897
2898 /*
2899  * function:    
2900  *
2901  * purpose:     
2902  *
2903  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2904  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2905  */
2906 static int
2907 zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
2908 {
2909         int retval;
2910         struct zfcp_adapter *adapter = erp_action->adapter;
2911         struct zfcp_port *port = erp_action->port;
2912
2913         zfcp_erp_timeout_init(erp_action);
2914         retval = zfcp_fsf_close_port(erp_action);
2915         if (retval == -ENOMEM) {
2916                 debug_text_event(adapter->erp_dbf, 5, "p_pstc_nomem");
2917                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2918                 retval = ZFCP_ERP_NOMEM;
2919                 goto out;
2920         }
2921         erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
2922         if (retval != 0) {
2923                 debug_text_event(adapter->erp_dbf, 5, "p_pstc_cpf");
2924                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2925                 /* could not send 'close', fail */
2926                 retval = ZFCP_ERP_FAILED;
2927                 goto out;
2928         }
2929         debug_text_event(adapter->erp_dbf, 6, "p_pstc_cpok");
2930         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2931         retval = ZFCP_ERP_CONTINUES;
2932  out:
2933         return retval;
2934 }
2935
2936 /*
2937  * function:    
2938  *
2939  * purpose:     
2940  *
2941  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2942  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2943  */
2944 static int
2945 zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
2946 {
2947         int retval;
2948         struct zfcp_adapter *adapter = erp_action->adapter;
2949         struct zfcp_port *port = erp_action->port;
2950
2951         zfcp_erp_timeout_init(erp_action);
2952         retval = zfcp_fsf_open_port(erp_action);
2953         if (retval == -ENOMEM) {
2954                 debug_text_event(adapter->erp_dbf, 5, "p_psto_nomem");
2955                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2956                 retval = ZFCP_ERP_NOMEM;
2957                 goto out;
2958         }
2959         erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
2960         if (retval != 0) {
2961                 debug_text_event(adapter->erp_dbf, 5, "p_psto_opf");
2962                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2963                 /* could not send 'open', fail */
2964                 retval = ZFCP_ERP_FAILED;
2965                 goto out;
2966         }
2967         debug_text_event(adapter->erp_dbf, 6, "p_psto_opok");
2968         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2969         retval = ZFCP_ERP_CONTINUES;
2970  out:
2971         return retval;
2972 }
2973
2974 /*
2975  * function:    
2976  *
2977  * purpose:     
2978  *
2979  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2980  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2981  */
2982 static int
2983 zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
2984 {
2985         int retval;
2986         struct zfcp_adapter *adapter = erp_action->adapter;
2987         struct zfcp_port *port = erp_action->port;
2988
2989         zfcp_erp_timeout_init(erp_action);
2990         retval = zfcp_ns_gid_pn_request(erp_action);
2991         if (retval == -ENOMEM) {
2992                 debug_text_event(adapter->erp_dbf, 5, "p_pstn_nomem");
2993                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2994                 retval = ZFCP_ERP_NOMEM;
2995                 goto out;
2996         }
2997         erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
2998         if (retval != 0) {
2999                 debug_text_event(adapter->erp_dbf, 5, "p_pstn_ref");
3000                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
3001                 /* could not send nameserver request, fail */
3002                 retval = ZFCP_ERP_FAILED;
3003                 goto out;
3004         }
3005         debug_text_event(adapter->erp_dbf, 6, "p_pstn_reok");
3006         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
3007         retval = ZFCP_ERP_CONTINUES;
3008  out:
3009         return retval;
3010 }
3011
3012 /*
3013  * function:    
3014  *
3015  * purpose:     this routine executes the 'Reopen Unit' action
3016  *              currently no retries
3017  *
3018  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
3019  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
3020  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
3021  */
3022 static int
3023 zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
3024 {
3025         int retval = ZFCP_ERP_FAILED;
3026         struct zfcp_unit *unit = erp_action->unit;
3027         struct zfcp_adapter *adapter = erp_action->adapter;
3028
3029         switch (erp_action->step) {
3030
3031                 /*
3032                  * FIXME:
3033                  * the ULP spec. begs for waiting for oustanding commands
3034                  */
3035         case ZFCP_ERP_STEP_UNINITIALIZED:
3036                 zfcp_erp_unit_strategy_clearstati(unit);
3037                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
3038                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
3039                                        "trying close\n", unit->fcp_lun);
3040                         retval = zfcp_erp_unit_strategy_close(erp_action);
3041                         break;
3042                 }
3043                 /* else it's already closed, fall through */
3044         case ZFCP_ERP_STEP_UNIT_CLOSING:
3045                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
3046                         ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
3047                                        unit->fcp_lun);
3048                         retval = ZFCP_ERP_FAILED;
3049                 } else {
3050                         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
3051                                 retval = ZFCP_ERP_EXIT;
3052                         else {
3053                                 ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
3054                                                "trying open\n", unit->fcp_lun);
3055                                 retval =
3056                                     zfcp_erp_unit_strategy_open(erp_action);
3057                         }
3058                 }
3059                 break;
3060
3061         case ZFCP_ERP_STEP_UNIT_OPENING:
3062                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
3063                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
3064                                        unit->fcp_lun);
3065                         retval = ZFCP_ERP_SUCCEEDED;
3066                 } else {
3067                         ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
3068                                        unit->fcp_lun);
3069                         retval = ZFCP_ERP_FAILED;
3070                 }
3071                 break;
3072         }
3073
3074         debug_text_event(adapter->erp_dbf, 3, "u_ust/ret");
3075         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof (fcp_lun_t));
3076         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
3077         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
3078         return retval;
3079 }
3080
3081 /*
3082  * function:
3083  *
3084  * purpose:
3085  *
3086  * returns:
3087  */
3088 static int
3089 zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
3090 {
3091         int retval = 0;
3092         struct zfcp_adapter *adapter = unit->port->adapter;
3093
3094         debug_text_event(adapter->erp_dbf, 5, "u_ustclst");
3095         debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3096
3097         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
3098                           ZFCP_STATUS_COMMON_CLOSING |
3099                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
3100                           ZFCP_STATUS_UNIT_SHARED |
3101                           ZFCP_STATUS_UNIT_READONLY,
3102                           &unit->status);
3103
3104         return retval;
3105 }
3106
3107 /*
3108  * function:    
3109  *
3110  * purpose:     
3111  *
3112  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
3113  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
3114  */
3115 static int
3116 zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
3117 {
3118         int retval;
3119         struct zfcp_adapter *adapter = erp_action->adapter;
3120         struct zfcp_unit *unit = erp_action->unit;
3121
3122         zfcp_erp_timeout_init(erp_action);
3123         retval = zfcp_fsf_close_unit(erp_action);
3124         if (retval == -ENOMEM) {
3125                 debug_text_event(adapter->erp_dbf, 5, "u_ustc_nomem");
3126                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3127                             sizeof (fcp_lun_t));
3128                 retval = ZFCP_ERP_NOMEM;
3129                 goto out;
3130         }
3131         erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
3132         if (retval != 0) {
3133                 debug_text_event(adapter->erp_dbf, 5, "u_ustc_cuf");
3134                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3135                             sizeof (fcp_lun_t));
3136                 /* could not send 'close', fail */
3137                 retval = ZFCP_ERP_FAILED;
3138                 goto out;
3139         }
3140         debug_text_event(adapter->erp_dbf, 6, "u_ustc_cuok");
3141         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
3142         retval = ZFCP_ERP_CONTINUES;
3143
3144  out:
3145         return retval;
3146 }
3147
3148 /*
3149  * function:    
3150  *
3151  * purpose:     
3152  *
3153  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
3154  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
3155  */
3156 static int
3157 zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
3158 {
3159         int retval;
3160         struct zfcp_adapter *adapter = erp_action->adapter;
3161         struct zfcp_unit *unit = erp_action->unit;
3162
3163         zfcp_erp_timeout_init(erp_action);
3164         retval = zfcp_fsf_open_unit(erp_action);
3165         if (retval == -ENOMEM) {
3166                 debug_text_event(adapter->erp_dbf, 5, "u_usto_nomem");
3167                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3168                             sizeof (fcp_lun_t));
3169                 retval = ZFCP_ERP_NOMEM;
3170                 goto out;
3171         }
3172         erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
3173         if (retval != 0) {
3174                 debug_text_event(adapter->erp_dbf, 5, "u_usto_ouf");
3175                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3176                             sizeof (fcp_lun_t));
3177                 /* could not send 'open', fail */
3178                 retval = ZFCP_ERP_FAILED;
3179                 goto out;
3180         }
3181         debug_text_event(adapter->erp_dbf, 6, "u_usto_ouok");
3182         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
3183         retval = ZFCP_ERP_CONTINUES;
3184  out:
3185         return retval;
3186 }
3187
3188 /*
3189  * function:    
3190  *
3191  * purpose:     
3192  *
3193  * returns:
3194  */
3195 static inline void
3196 zfcp_erp_timeout_init(struct zfcp_erp_action *erp_action)
3197 {
3198         init_timer(&erp_action->timer);
3199         erp_action->timer.function = zfcp_erp_timeout_handler;
3200         erp_action->timer.data = (unsigned long) erp_action;
3201         /* jiffies will be added in zfcp_fsf_req_send */
3202         erp_action->timer.expires = ZFCP_ERP_FSFREQ_TIMEOUT;
3203 }
3204
3205 /*
3206  * function:    
3207  *
3208  * purpose:     enqueue the specified error recovery action, if needed
3209  *
3210  * returns:
3211  */
3212 static int
3213 zfcp_erp_action_enqueue(int action,
3214                         struct zfcp_adapter *adapter,
3215                         struct zfcp_port *port, struct zfcp_unit *unit)
3216 {
3217         int retval = 1;
3218         struct zfcp_erp_action *erp_action = NULL;
3219         int stronger_action = 0;
3220         u32 status = 0;
3221
3222         /*
3223          * We need some rules here which check whether we really need
3224          * this action or whether we should just drop it.
3225          * E.g. if there is a unfinished 'Reopen Port' request then we drop a
3226          * 'Reopen Unit' request for an associated unit since we can't
3227          * satisfy this request now. A 'Reopen Port' action will trigger
3228          * 'Reopen Unit' actions when it completes.
3229          * Thus, there are only actions in the queue which can immediately be
3230          * executed. This makes the processing of the action queue more
3231          * efficient.
3232          */
3233
3234         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
3235                               &adapter->status))
3236                 return -EIO;
3237
3238         debug_event(adapter->erp_dbf, 4, &action, sizeof (int));
3239         /* check whether we really need this */
3240         switch (action) {
3241         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3242                 if (atomic_test_mask
3243                     (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
3244                         debug_text_event(adapter->erp_dbf, 4, "u_actenq_drp");
3245                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
3246                                     sizeof (wwn_t));
3247                         debug_event(adapter->erp_dbf, 4, &unit->fcp_lun,
3248                                     sizeof (fcp_lun_t));
3249                         goto out;
3250                 }
3251                 if (!atomic_test_mask
3252                     (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
3253                     atomic_test_mask
3254                     (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
3255                         goto out;
3256                 }
3257                 if (!atomic_test_mask
3258                     (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status)) {
3259                         stronger_action = ZFCP_ERP_ACTION_REOPEN_PORT;
3260                         unit = NULL;
3261                 }
3262                 /* fall through !!! */
3263
3264         case ZFCP_ERP_ACTION_REOPEN_PORT:
3265                 if (atomic_test_mask
3266                     (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
3267                         debug_text_event(adapter->erp_dbf, 4, "p_actenq_drp");
3268                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
3269                                     sizeof (wwn_t));
3270                         goto out;
3271                 }
3272                 /* fall through !!! */
3273
3274         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3275                 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3276                                      &port->status)) {
3277                         if (port->erp_action.action !=
3278                             ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
3279                                 ZFCP_LOG_INFO("dropped erp action %i (port "
3280                                               "0x%016Lx, action in use: %i)\n",
3281                                               action, port->wwpn,
3282                                               port->erp_action.action);
3283                                 debug_text_event(adapter->erp_dbf, 4,
3284                                                  "pf_actenq_drp");
3285                         } else 
3286                                 debug_text_event(adapter->erp_dbf, 4,
3287                                                  "pf_actenq_drpcp");
3288                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
3289                                     sizeof (wwn_t));
3290                         goto out;
3291                 }
3292                 if (!atomic_test_mask
3293                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
3294                     atomic_test_mask
3295                     (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
3296                         goto out;
3297                 }
3298                 if (!atomic_test_mask
3299                     (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status)) {
3300                         stronger_action = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
3301                         port = NULL;
3302                 }
3303                 /* fall through !!! */
3304
3305         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3306                 if (atomic_test_mask
3307                     (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
3308                         debug_text_event(adapter->erp_dbf, 4, "a_actenq_drp");
3309                         goto out;
3310                 }
3311                 break;
3312
3313         default:
3314                 debug_text_exception(adapter->erp_dbf, 1, "a_actenq_bug");
3315                 debug_event(adapter->erp_dbf, 1, &action, sizeof (int));
3316                 ZFCP_LOG_NORMAL("bug: unknown erp action requested "
3317                                 "on adapter %s (action=%d)\n",
3318                                 zfcp_get_busid_by_adapter(adapter), action);
3319                 goto out;
3320         }
3321
3322         /* check whether we need something stronger first */
3323         if (stronger_action) {
3324                 debug_text_event(adapter->erp_dbf, 4, "a_actenq_str");
3325                 debug_event(adapter->erp_dbf, 4, &stronger_action,
3326                             sizeof (int));
3327                 ZFCP_LOG_DEBUG("stronger erp action %d needed before "
3328                                "erp action %d on adapter %s\n",
3329                                stronger_action, action,
3330                                zfcp_get_busid_by_adapter(adapter));
3331                 action = stronger_action;
3332         }
3333
3334         /* mark adapter to have some error recovery pending */
3335         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
3336
3337         /* setup error recovery action */
3338         switch (action) {
3339
3340         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3341                 zfcp_unit_get(unit);
3342                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
3343                 erp_action = &unit->erp_action;
3344                 if (!atomic_test_mask
3345                     (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
3346                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3347                 break;
3348
3349         case ZFCP_ERP_ACTION_REOPEN_PORT:
3350         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3351                 zfcp_port_get(port);
3352                 zfcp_erp_action_dismiss_port(port);
3353                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
3354                 erp_action = &port->erp_action;
3355                 if (!atomic_test_mask
3356                     (ZFCP_STATUS_COMMON_RUNNING, &port->status))
3357                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3358                 break;
3359
3360         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3361                 zfcp_adapter_get(adapter);
3362                 zfcp_erp_action_dismiss_adapter(adapter);
3363                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
3364                 erp_action = &adapter->erp_action;
3365                 if (!atomic_test_mask
3366                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
3367                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3368                 break;
3369         }
3370
3371         debug_text_event(adapter->erp_dbf, 4, "a_actenq");
3372
3373         memset(erp_action, 0, sizeof (struct zfcp_erp_action));
3374         erp_action->adapter = adapter;
3375         erp_action->port = port;
3376         erp_action->unit = unit;
3377         erp_action->action = action;
3378         erp_action->status = status;
3379
3380         ++adapter->erp_total_count;
3381
3382         /* finally put it into 'ready' queue and kick erp thread */
3383         list_add(&erp_action->list, &adapter->erp_ready_head);
3384         up(&adapter->erp_ready_sem);
3385         retval = 0;
3386  out:
3387         return retval;
3388 }
3389
3390 /*
3391  * function:    
3392  *
3393  * purpose:     
3394  *
3395  * returns:
3396  */
3397 static int
3398 zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
3399 {
3400         int retval = 0;
3401         struct zfcp_adapter *adapter = erp_action->adapter;
3402
3403         --adapter->erp_total_count;
3404         if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
3405                 --adapter->erp_low_mem_count;
3406                 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
3407         }
3408
3409         debug_text_event(adapter->erp_dbf, 4, "a_actdeq");
3410         debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
3411         list_del(&erp_action->list);
3412         switch (erp_action->action) {
3413         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3414                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3415                                   &erp_action->unit->status);
3416                 break;
3417         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3418         case ZFCP_ERP_ACTION_REOPEN_PORT:
3419                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3420                                   &erp_action->port->status);
3421                 break;
3422         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3423                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3424                                   &erp_action->adapter->status);
3425                 break;
3426         default:
3427                 /* bug */
3428                 break;
3429         }
3430         return retval;
3431 }
3432
3433 /**
3434  * zfcp_erp_action_cleanup
3435  *
3436  * Register unit with scsi stack if appropiate and fix reference counts.
3437  * Note: Temporary units are not registered with scsi stack.
3438  */
3439 static void
3440 zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
3441                         struct zfcp_port *port, struct zfcp_unit *unit,
3442                         int result)
3443 {
3444         switch (action) {
3445         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3446                 if ((result == ZFCP_ERP_SUCCEEDED)
3447                     && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
3448                                           &unit->status))
3449                     && !unit->device
3450                     && port->rport)
3451                         scsi_add_device(port->adapter->scsi_host, 0,
3452                                         port->rport->scsi_target_id,
3453                                         unit->scsi_lun);
3454                 zfcp_unit_put(unit);
3455                 break;
3456         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3457         case ZFCP_ERP_ACTION_REOPEN_PORT:
3458                 if ((result == ZFCP_ERP_SUCCEEDED)
3459                     && !atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
3460                                          &port->status)
3461                     && !port->rport) {
3462                         struct fc_rport_identifiers ids;
3463                         ids.node_name = port->wwnn;
3464                         ids.port_name = port->wwpn;
3465                         ids.port_id = port->d_id;
3466                         ids.roles = FC_RPORT_ROLE_FCP_TARGET;
3467                         port->rport =
3468                                 fc_remote_port_add(adapter->scsi_host, 0, &ids);
3469                         if (!port->rport)
3470                                 ZFCP_LOG_NORMAL("failed registration of rport"
3471                                                 "(adapter %s, wwpn=0x%016Lx)\n",
3472                                                 zfcp_get_busid_by_port(port),
3473                                                 port->wwpn);
3474                 }
3475                 zfcp_port_put(port);
3476                 break;
3477         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3478                 zfcp_adapter_put(adapter);
3479                 break;
3480         default:
3481                 break;
3482         }
3483 }
3484
3485
3486 /*
3487  * function:    
3488  *
3489  * purpose:     
3490  *
3491  * returns:     FIXME
3492  */
3493 static int
3494 zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
3495 {
3496         int retval = 0;
3497         struct zfcp_port *port;
3498
3499         debug_text_event(adapter->erp_dbf, 5, "a_actab");
3500         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
3501                 zfcp_erp_action_dismiss(&adapter->erp_action);
3502         else
3503                 list_for_each_entry(port, &adapter->port_list_head, list)
3504                     zfcp_erp_action_dismiss_port(port);
3505
3506         return retval;
3507 }
3508
3509 /*
3510  * function:    
3511  *
3512  * purpose:     
3513  *
3514  * returns:     FIXME
3515  */
3516 static int
3517 zfcp_erp_action_dismiss_port(struct zfcp_port *port)
3518 {
3519         int retval = 0;
3520         struct zfcp_unit *unit;
3521         struct zfcp_adapter *adapter = port->adapter;
3522
3523         debug_text_event(adapter->erp_dbf, 5, "p_actab");
3524         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
3525         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
3526                 zfcp_erp_action_dismiss(&port->erp_action);
3527         else
3528                 list_for_each_entry(unit, &port->unit_list_head, list)
3529                     zfcp_erp_action_dismiss_unit(unit);
3530
3531         return retval;
3532 }
3533
3534 /*
3535  * function:    
3536  *
3537  * purpose:     
3538  *
3539  * returns:     FIXME
3540  */
3541 static int
3542 zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
3543 {
3544         int retval = 0;
3545         struct zfcp_adapter *adapter = unit->port->adapter;
3546
3547         debug_text_event(adapter->erp_dbf, 5, "u_actab");
3548         debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3549         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
3550                 zfcp_erp_action_dismiss(&unit->erp_action);
3551
3552         return retval;
3553 }
3554
3555 /*
3556  * function:    
3557  *
3558  * purpose:     moves erp_action to 'erp running list'
3559  *
3560  * returns:
3561  */
3562 static inline void
3563 zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
3564 {
3565         struct zfcp_adapter *adapter = erp_action->adapter;
3566
3567         debug_text_event(adapter->erp_dbf, 6, "a_toru");
3568         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3569         list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
3570 }
3571
3572 /*
3573  * function:    
3574  *
3575  * purpose:     moves erp_action to 'erp ready list'
3576  *
3577  * returns:
3578  */
3579 static inline void
3580 zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
3581 {
3582         struct zfcp_adapter *adapter = erp_action->adapter;
3583
3584         debug_text_event(adapter->erp_dbf, 6, "a_tore");
3585         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3586         list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
3587 }
3588
3589 /*
3590  * function:    zfcp_erp_port_boxed
3591  *
3592  * purpose:
3593  */
3594 void
3595 zfcp_erp_port_boxed(struct zfcp_port *port)
3596 {
3597         struct zfcp_adapter *adapter = port->adapter;
3598         unsigned long flags;
3599
3600         debug_text_event(adapter->erp_dbf, 3, "p_access_boxed");
3601         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3602         read_lock_irqsave(&zfcp_data.config_lock, flags);
3603         zfcp_erp_modify_port_status(port,
3604                         ZFCP_STATUS_COMMON_ACCESS_BOXED,
3605                         ZFCP_SET);
3606         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3607         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
3608 }
3609
3610 /*
3611  * function:    zfcp_erp_unit_boxed
3612  *
3613  * purpose:
3614  */
3615 void
3616 zfcp_erp_unit_boxed(struct zfcp_unit *unit)
3617 {
3618         struct zfcp_adapter *adapter = unit->port->adapter;
3619
3620         debug_text_event(adapter->erp_dbf, 3, "u_access_boxed");
3621         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3622         zfcp_erp_modify_unit_status(unit,
3623                         ZFCP_STATUS_COMMON_ACCESS_BOXED,
3624                         ZFCP_SET);
3625         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
3626 }
3627
3628 /*
3629  * function:    zfcp_erp_port_access_denied
3630  *
3631  * purpose:
3632  */
3633 void
3634 zfcp_erp_port_access_denied(struct zfcp_port *port)
3635 {
3636         struct zfcp_adapter *adapter = port->adapter;
3637         unsigned long flags;
3638
3639         debug_text_event(adapter->erp_dbf, 3, "p_access_denied");
3640         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3641         read_lock_irqsave(&zfcp_data.config_lock, flags);
3642         zfcp_erp_modify_port_status(port,
3643                         ZFCP_STATUS_COMMON_ERP_FAILED |
3644                         ZFCP_STATUS_COMMON_ACCESS_DENIED,
3645                         ZFCP_SET);
3646         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3647 }
3648
3649 /*
3650  * function:    zfcp_erp_unit_access_denied
3651  *
3652  * purpose:
3653  */
3654 void
3655 zfcp_erp_unit_access_denied(struct zfcp_unit *unit)
3656 {
3657         struct zfcp_adapter *adapter = unit->port->adapter;
3658
3659         debug_text_event(adapter->erp_dbf, 3, "u_access_denied");
3660         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3661         zfcp_erp_modify_unit_status(unit,
3662                         ZFCP_STATUS_COMMON_ERP_FAILED |
3663                         ZFCP_STATUS_COMMON_ACCESS_DENIED,
3664                         ZFCP_SET);
3665 }
3666
3667 /*
3668  * function:    zfcp_erp_adapter_access_changed
3669  *
3670  * purpose:
3671  */
3672 void
3673 zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter)
3674 {
3675         struct zfcp_port *port;
3676         unsigned long flags;
3677
3678         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
3679                 return;
3680
3681         debug_text_event(adapter->erp_dbf, 3, "a_access_recover");
3682         debug_event(adapter->erp_dbf, 3, &adapter->name, 8);
3683
3684         read_lock_irqsave(&zfcp_data.config_lock, flags);
3685         if (adapter->nameserver_port)
3686                 zfcp_erp_port_access_changed(adapter->nameserver_port);
3687         list_for_each_entry(port, &adapter->port_list_head, list)
3688                 if (port != adapter->nameserver_port)
3689                         zfcp_erp_port_access_changed(port);
3690         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3691 }
3692
3693 /*
3694  * function:    zfcp_erp_port_access_changed
3695  *
3696  * purpose:
3697  */
3698 void
3699 zfcp_erp_port_access_changed(struct zfcp_port *port)
3700 {
3701         struct zfcp_adapter *adapter = port->adapter;
3702         struct zfcp_unit *unit;
3703
3704         debug_text_event(adapter->erp_dbf, 3, "p_access_recover");
3705         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3706
3707         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3708                               &port->status) &&
3709             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3710                               &port->status)) {
3711                 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
3712                         list_for_each_entry(unit, &port->unit_list_head, list)
3713                                 zfcp_erp_unit_access_changed(unit);
3714                 return;
3715         }
3716
3717         ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
3718                         "(due to ACT update)\n",
3719                         port->wwpn, zfcp_get_busid_by_adapter(adapter));
3720         if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3721                 ZFCP_LOG_NORMAL("failed reopen of port"
3722                                 "(adapter %s, wwpn=0x%016Lx)\n",
3723                                 zfcp_get_busid_by_adapter(adapter), port->wwpn);
3724 }
3725
3726 /*
3727  * function:    zfcp_erp_unit_access_changed
3728  *
3729  * purpose:
3730  */
3731 void
3732 zfcp_erp_unit_access_changed(struct zfcp_unit *unit)
3733 {
3734         struct zfcp_adapter *adapter = unit->port->adapter;
3735
3736         debug_text_event(adapter->erp_dbf, 3, "u_access_recover");
3737         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3738
3739         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3740                               &unit->status) &&
3741             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3742                               &unit->status))
3743                 return;
3744
3745         ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
3746                         " on adapter %s (due to ACT update)\n",
3747                         unit->fcp_lun, unit->port->wwpn,
3748                         zfcp_get_busid_by_adapter(adapter));
3749         if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3750                 ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
3751                                 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
3752                                 zfcp_get_busid_by_adapter(adapter),
3753                                 unit->port->wwpn, unit->fcp_lun);
3754 }
3755
3756 #undef ZFCP_LOG_AREA