]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/ibmvscsi/ibmvfc.c
[SCSI] ibmvfc: Target refcounting fixes
[linux-2.6-omap-h63xx.git] / drivers / scsi / ibmvscsi / ibmvfc.c
1 /*
2  * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
3  *
4  * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) IBM Corporation, 2008
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmapool.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/kthread.h>
31 #include <linux/of.h>
32 #include <linux/stringify.h>
33 #include <asm/firmware.h>
34 #include <asm/irq.h>
35 #include <asm/vio.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_tcq.h>
41 #include <scsi/scsi_transport_fc.h>
42 #include "ibmvfc.h"
43
44 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
45 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
46 static unsigned int max_lun = IBMVFC_MAX_LUN;
47 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
48 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
49 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
50 static unsigned int dev_loss_tmo = IBMVFC_DEV_LOSS_TMO;
51 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
52 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
53 static LIST_HEAD(ibmvfc_head);
54 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
55 static struct scsi_transport_template *ibmvfc_transport_template;
56
57 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
58 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
61
62 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
64                  "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
65 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
66 MODULE_PARM_DESC(default_timeout,
67                  "Default timeout in seconds for initialization and EH commands. "
68                  "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
69 module_param_named(max_requests, max_requests, uint, S_IRUGO);
70 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
71                  "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
72 module_param_named(max_lun, max_lun, uint, S_IRUGO);
73 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
74                  "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
75 module_param_named(max_targets, max_targets, uint, S_IRUGO);
76 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
77                  "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
78 module_param_named(disc_threads, disc_threads, uint, S_IRUGO | S_IWUSR);
79 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
80                  "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
81 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
82 MODULE_PARM_DESC(debug, "Enable driver debug information. "
83                  "[Default=" __stringify(IBMVFC_DEBUG) "]");
84 module_param_named(dev_loss_tmo, dev_loss_tmo, uint, S_IRUGO | S_IWUSR);
85 MODULE_PARM_DESC(dev_loss_tmo, "Maximum number of seconds that the FC "
86                  "transport should insulate the loss of a remote port. Once this "
87                  "value is exceeded, the scsi target is removed. "
88                  "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO) "]");
89 module_param_named(log_level, log_level, uint, 0);
90 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
91                  "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
92
93 static const struct {
94         u16 status;
95         u16 error;
96         u8 result;
97         u8 retry;
98         int log;
99         char *name;
100 } cmd_status [] = {
101         { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
102         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
103         { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
104         { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_NO_CONNECT, 1, 1, "network down" },
105         { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
106         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
107         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
108         { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
109         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
110         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
111         { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
112         { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
113         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 0, 0, "link halted" },
114         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
115
116         { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
117         { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
118         { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ABORT, 0, 1, "invalid parameter" },
119         { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ABORT, 0, 1, "missing parameter" },
120         { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
121         { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ABORT, 0, 1, "transaction cancelled" },
122         { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ABORT, 0, 1, "transaction cancelled implicit" },
123         { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
124         { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
125
126         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
127         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
128         { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
129         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
130         { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
131         { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
132         { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
133         { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
134         { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
135         { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
136         { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
137
138         { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
139 };
140
141 static void ibmvfc_npiv_login(struct ibmvfc_host *);
142 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
143 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
144 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
145
146 static const char *unknown_error = "unknown error";
147
148 #ifdef CONFIG_SCSI_IBMVFC_TRACE
149 /**
150  * ibmvfc_trc_start - Log a start trace entry
151  * @evt:                ibmvfc event struct
152  *
153  **/
154 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
155 {
156         struct ibmvfc_host *vhost = evt->vhost;
157         struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
158         struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
159         struct ibmvfc_trace_entry *entry;
160
161         entry = &vhost->trace[vhost->trace_index++];
162         entry->evt = evt;
163         entry->time = jiffies;
164         entry->fmt = evt->crq.format;
165         entry->type = IBMVFC_TRC_START;
166
167         switch (entry->fmt) {
168         case IBMVFC_CMD_FORMAT:
169                 entry->op_code = vfc_cmd->iu.cdb[0];
170                 entry->scsi_id = vfc_cmd->tgt_scsi_id;
171                 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
172                 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
173                 entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
174                 break;
175         case IBMVFC_MAD_FORMAT:
176                 entry->op_code = mad->opcode;
177                 break;
178         default:
179                 break;
180         };
181 }
182
183 /**
184  * ibmvfc_trc_end - Log an end trace entry
185  * @evt:                ibmvfc event struct
186  *
187  **/
188 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
189 {
190         struct ibmvfc_host *vhost = evt->vhost;
191         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
192         struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
193         struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
194
195         entry->evt = evt;
196         entry->time = jiffies;
197         entry->fmt = evt->crq.format;
198         entry->type = IBMVFC_TRC_END;
199
200         switch (entry->fmt) {
201         case IBMVFC_CMD_FORMAT:
202                 entry->op_code = vfc_cmd->iu.cdb[0];
203                 entry->scsi_id = vfc_cmd->tgt_scsi_id;
204                 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
205                 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
206                 entry->u.end.status = vfc_cmd->status;
207                 entry->u.end.error = vfc_cmd->error;
208                 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
209                 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
210                 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
211                 break;
212         case IBMVFC_MAD_FORMAT:
213                 entry->op_code = mad->opcode;
214                 entry->u.end.status = mad->status;
215                 break;
216         default:
217                 break;
218
219         };
220 }
221
222 #else
223 #define ibmvfc_trc_start(evt) do { } while (0)
224 #define ibmvfc_trc_end(evt) do { } while (0)
225 #endif
226
227 /**
228  * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
229  * @status:             status / error class
230  * @error:              error
231  *
232  * Return value:
233  *      index into cmd_status / -EINVAL on failure
234  **/
235 static int ibmvfc_get_err_index(u16 status, u16 error)
236 {
237         int i;
238
239         for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
240                 if ((cmd_status[i].status & status) == cmd_status[i].status &&
241                     cmd_status[i].error == error)
242                         return i;
243
244         return -EINVAL;
245 }
246
247 /**
248  * ibmvfc_get_cmd_error - Find the error description for the fcp response
249  * @status:             status / error class
250  * @error:              error
251  *
252  * Return value:
253  *      error description string
254  **/
255 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
256 {
257         int rc = ibmvfc_get_err_index(status, error);
258         if (rc >= 0)
259                 return cmd_status[rc].name;
260         return unknown_error;
261 }
262
263 /**
264  * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
265  * @vfc_cmd:    ibmvfc command struct
266  *
267  * Return value:
268  *      SCSI result value to return for completed command
269  **/
270 static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
271 {
272         int err;
273         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
274         int fc_rsp_len = rsp->fcp_rsp_len;
275
276         if ((rsp->flags & FCP_RSP_LEN_VALID) &&
277             ((!fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
278              rsp->data.info.rsp_code))
279                 return DID_ERROR << 16;
280
281         if (!vfc_cmd->status) {
282                 if (rsp->flags & FCP_RESID_OVER)
283                         return rsp->scsi_status | (DID_ERROR << 16);
284                 else
285                         return rsp->scsi_status | (DID_OK << 16);
286         }
287
288         err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
289         if (err >= 0)
290                 return rsp->scsi_status | (cmd_status[err].result << 16);
291         return rsp->scsi_status | (DID_ERROR << 16);
292 }
293
294 /**
295  * ibmvfc_retry_cmd - Determine if error status is retryable
296  * @status:             status / error class
297  * @error:              error
298  *
299  * Return value:
300  *      1 if error should be retried / 0 if it should not
301  **/
302 static int ibmvfc_retry_cmd(u16 status, u16 error)
303 {
304         int rc = ibmvfc_get_err_index(status, error);
305
306         if (rc >= 0)
307                 return cmd_status[rc].retry;
308         return 1;
309 }
310
311 static const char *unknown_fc_explain = "unknown fc explain";
312
313 static const struct {
314         u16 fc_explain;
315         char *name;
316 } ls_explain [] = {
317         { 0x00, "no additional explanation" },
318         { 0x01, "service parameter error - options" },
319         { 0x03, "service parameter error - initiator control" },
320         { 0x05, "service parameter error - recipient control" },
321         { 0x07, "service parameter error - received data field size" },
322         { 0x09, "service parameter error - concurrent seq" },
323         { 0x0B, "service parameter error - credit" },
324         { 0x0D, "invalid N_Port/F_Port_Name" },
325         { 0x0E, "invalid node/Fabric Name" },
326         { 0x0F, "invalid common service parameters" },
327         { 0x11, "invalid association header" },
328         { 0x13, "association header required" },
329         { 0x15, "invalid originator S_ID" },
330         { 0x17, "invalid OX_ID-RX-ID combination" },
331         { 0x19, "command (request) already in progress" },
332         { 0x1E, "N_Port Login requested" },
333         { 0x1F, "Invalid N_Port_ID" },
334 };
335
336 static const struct {
337         u16 fc_explain;
338         char *name;
339 } gs_explain [] = {
340         { 0x00, "no additional explanation" },
341         { 0x01, "port identifier not registered" },
342         { 0x02, "port name not registered" },
343         { 0x03, "node name not registered" },
344         { 0x04, "class of service not registered" },
345         { 0x06, "initial process associator not registered" },
346         { 0x07, "FC-4 TYPEs not registered" },
347         { 0x08, "symbolic port name not registered" },
348         { 0x09, "symbolic node name not registered" },
349         { 0x0A, "port type not registered" },
350         { 0xF0, "authorization exception" },
351         { 0xF1, "authentication exception" },
352         { 0xF2, "data base full" },
353         { 0xF3, "data base empty" },
354         { 0xF4, "processing request" },
355         { 0xF5, "unable to verify connection" },
356         { 0xF6, "devices not in a common zone" },
357 };
358
359 /**
360  * ibmvfc_get_ls_explain - Return the FC Explain description text
361  * @status:     FC Explain status
362  *
363  * Returns:
364  *      error string
365  **/
366 static const char *ibmvfc_get_ls_explain(u16 status)
367 {
368         int i;
369
370         for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
371                 if (ls_explain[i].fc_explain == status)
372                         return ls_explain[i].name;
373
374         return unknown_fc_explain;
375 }
376
377 /**
378  * ibmvfc_get_gs_explain - Return the FC Explain description text
379  * @status:     FC Explain status
380  *
381  * Returns:
382  *      error string
383  **/
384 static const char *ibmvfc_get_gs_explain(u16 status)
385 {
386         int i;
387
388         for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
389                 if (gs_explain[i].fc_explain == status)
390                         return gs_explain[i].name;
391
392         return unknown_fc_explain;
393 }
394
395 static const struct {
396         enum ibmvfc_fc_type fc_type;
397         char *name;
398 } fc_type [] = {
399         { IBMVFC_FABRIC_REJECT, "fabric reject" },
400         { IBMVFC_PORT_REJECT, "port reject" },
401         { IBMVFC_LS_REJECT, "ELS reject" },
402         { IBMVFC_FABRIC_BUSY, "fabric busy" },
403         { IBMVFC_PORT_BUSY, "port busy" },
404         { IBMVFC_BASIC_REJECT, "basic reject" },
405 };
406
407 static const char *unknown_fc_type = "unknown fc type";
408
409 /**
410  * ibmvfc_get_fc_type - Return the FC Type description text
411  * @status:     FC Type error status
412  *
413  * Returns:
414  *      error string
415  **/
416 static const char *ibmvfc_get_fc_type(u16 status)
417 {
418         int i;
419
420         for (i = 0; i < ARRAY_SIZE(fc_type); i++)
421                 if (fc_type[i].fc_type == status)
422                         return fc_type[i].name;
423
424         return unknown_fc_type;
425 }
426
427 /**
428  * ibmvfc_set_tgt_action - Set the next init action for the target
429  * @tgt:                ibmvfc target struct
430  * @action:             action to perform
431  *
432  **/
433 static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
434                                   enum ibmvfc_target_action action)
435 {
436         switch (tgt->action) {
437         case IBMVFC_TGT_ACTION_DEL_RPORT:
438                 break;
439         default:
440                 tgt->action = action;
441                 break;
442         }
443 }
444
445 /**
446  * ibmvfc_set_host_state - Set the state for the host
447  * @vhost:              ibmvfc host struct
448  * @state:              state to set host to
449  *
450  * Returns:
451  *      0 if state changed / non-zero if not changed
452  **/
453 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
454                                   enum ibmvfc_host_state state)
455 {
456         int rc = 0;
457
458         switch (vhost->state) {
459         case IBMVFC_HOST_OFFLINE:
460                 rc = -EINVAL;
461                 break;
462         default:
463                 vhost->state = state;
464                 break;
465         };
466
467         return rc;
468 }
469
470 /**
471  * ibmvfc_set_host_action - Set the next init action for the host
472  * @vhost:              ibmvfc host struct
473  * @action:             action to perform
474  *
475  **/
476 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
477                                    enum ibmvfc_host_action action)
478 {
479         switch (action) {
480         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
481                 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
482                         vhost->action = action;
483                 break;
484         case IBMVFC_HOST_ACTION_INIT_WAIT:
485                 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
486                         vhost->action = action;
487                 break;
488         case IBMVFC_HOST_ACTION_QUERY:
489                 switch (vhost->action) {
490                 case IBMVFC_HOST_ACTION_INIT_WAIT:
491                 case IBMVFC_HOST_ACTION_NONE:
492                 case IBMVFC_HOST_ACTION_TGT_ADD:
493                         vhost->action = action;
494                         break;
495                 default:
496                         break;
497                 };
498                 break;
499         case IBMVFC_HOST_ACTION_TGT_INIT:
500                 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
501                         vhost->action = action;
502                 break;
503         case IBMVFC_HOST_ACTION_INIT:
504         case IBMVFC_HOST_ACTION_TGT_DEL:
505         case IBMVFC_HOST_ACTION_QUERY_TGTS:
506         case IBMVFC_HOST_ACTION_TGT_ADD:
507         case IBMVFC_HOST_ACTION_NONE:
508         default:
509                 vhost->action = action;
510                 break;
511         };
512 }
513
514 /**
515  * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
516  * @vhost:              ibmvfc host struct
517  *
518  * Return value:
519  *      nothing
520  **/
521 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
522 {
523         if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
524                 scsi_block_requests(vhost->host);
525                 ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING);
526                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
527         } else
528                 vhost->reinit = 1;
529
530         wake_up(&vhost->work_wait_q);
531 }
532
533 /**
534  * ibmvfc_link_down - Handle a link down event from the adapter
535  * @vhost:      ibmvfc host struct
536  * @state:      ibmvfc host state to enter
537  *
538  **/
539 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
540                              enum ibmvfc_host_state state)
541 {
542         struct ibmvfc_target *tgt;
543
544         ENTER;
545         scsi_block_requests(vhost->host);
546         list_for_each_entry(tgt, &vhost->targets, queue)
547                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
548         ibmvfc_set_host_state(vhost, state);
549         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
550         vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
551         wake_up(&vhost->work_wait_q);
552         LEAVE;
553 }
554
555 /**
556  * ibmvfc_init_host - Start host initialization
557  * @vhost:              ibmvfc host struct
558  *
559  * Return value:
560  *      nothing
561  **/
562 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
563 {
564         struct ibmvfc_target *tgt;
565
566         if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
567                 if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
568                         dev_err(vhost->dev,
569                                 "Host initialization retries exceeded. Taking adapter offline\n");
570                         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
571                         return;
572                 }
573         }
574
575         if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
576                 list_for_each_entry(tgt, &vhost->targets, queue)
577                         tgt->need_login = 1;
578                 scsi_block_requests(vhost->host);
579                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
580                 vhost->job_step = ibmvfc_npiv_login;
581                 wake_up(&vhost->work_wait_q);
582         }
583 }
584
585 /**
586  * ibmvfc_send_crq - Send a CRQ
587  * @vhost:      ibmvfc host struct
588  * @word1:      the first 64 bits of the data
589  * @word2:      the second 64 bits of the data
590  *
591  * Return value:
592  *      0 on success / other on failure
593  **/
594 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
595 {
596         struct vio_dev *vdev = to_vio_dev(vhost->dev);
597         return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
598 }
599
600 /**
601  * ibmvfc_send_crq_init - Send a CRQ init message
602  * @vhost:      ibmvfc host struct
603  *
604  * Return value:
605  *      0 on success / other on failure
606  **/
607 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
608 {
609         ibmvfc_dbg(vhost, "Sending CRQ init\n");
610         return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
611 }
612
613 /**
614  * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
615  * @vhost:      ibmvfc host struct
616  *
617  * Return value:
618  *      0 on success / other on failure
619  **/
620 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
621 {
622         ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
623         return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
624 }
625
626 /**
627  * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
628  * @vhost:      ibmvfc host struct
629  *
630  * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
631  * the crq with the hypervisor.
632  **/
633 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
634 {
635         long rc;
636         struct vio_dev *vdev = to_vio_dev(vhost->dev);
637         struct ibmvfc_crq_queue *crq = &vhost->crq;
638
639         ibmvfc_dbg(vhost, "Releasing CRQ\n");
640         free_irq(vdev->irq, vhost);
641         do {
642                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
643         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
644
645         vhost->state = IBMVFC_NO_CRQ;
646         dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
647         free_page((unsigned long)crq->msgs);
648 }
649
650 /**
651  * ibmvfc_reenable_crq_queue - reenables the CRQ
652  * @vhost:      ibmvfc host struct
653  *
654  * Return value:
655  *      0 on success / other on failure
656  **/
657 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
658 {
659         int rc;
660         struct vio_dev *vdev = to_vio_dev(vhost->dev);
661
662         /* Re-enable the CRQ */
663         do {
664                 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
665         } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
666
667         if (rc)
668                 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
669
670         return rc;
671 }
672
673 /**
674  * ibmvfc_reset_crq - resets a crq after a failure
675  * @vhost:      ibmvfc host struct
676  *
677  * Return value:
678  *      0 on success / other on failure
679  **/
680 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
681 {
682         int rc;
683         struct vio_dev *vdev = to_vio_dev(vhost->dev);
684         struct ibmvfc_crq_queue *crq = &vhost->crq;
685
686         /* Close the CRQ */
687         do {
688                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
689         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
690
691         vhost->state = IBMVFC_NO_CRQ;
692         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
693
694         /* Clean out the queue */
695         memset(crq->msgs, 0, PAGE_SIZE);
696         crq->cur = 0;
697
698         /* And re-open it again */
699         rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
700                                 crq->msg_token, PAGE_SIZE);
701
702         if (rc == H_CLOSED)
703                 /* Adapter is good, but other end is not ready */
704                 dev_warn(vhost->dev, "Partner adapter not ready\n");
705         else if (rc != 0)
706                 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
707
708         return rc;
709 }
710
711 /**
712  * ibmvfc_valid_event - Determines if event is valid.
713  * @pool:       event_pool that contains the event
714  * @evt:        ibmvfc event to be checked for validity
715  *
716  * Return value:
717  *      1 if event is valid / 0 if event is not valid
718  **/
719 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
720                               struct ibmvfc_event *evt)
721 {
722         int index = evt - pool->events;
723         if (index < 0 || index >= pool->size)   /* outside of bounds */
724                 return 0;
725         if (evt != pool->events + index)        /* unaligned */
726                 return 0;
727         return 1;
728 }
729
730 /**
731  * ibmvfc_free_event - Free the specified event
732  * @evt:        ibmvfc_event to be freed
733  *
734  **/
735 static void ibmvfc_free_event(struct ibmvfc_event *evt)
736 {
737         struct ibmvfc_host *vhost = evt->vhost;
738         struct ibmvfc_event_pool *pool = &vhost->pool;
739
740         BUG_ON(!ibmvfc_valid_event(pool, evt));
741         BUG_ON(atomic_inc_return(&evt->free) != 1);
742         list_add_tail(&evt->queue, &vhost->free);
743 }
744
745 /**
746  * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
747  * @evt:        ibmvfc event struct
748  *
749  * This function does not setup any error status, that must be done
750  * before this function gets called.
751  **/
752 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
753 {
754         struct scsi_cmnd *cmnd = evt->cmnd;
755
756         if (cmnd) {
757                 scsi_dma_unmap(cmnd);
758                 cmnd->scsi_done(cmnd);
759         }
760
761         ibmvfc_free_event(evt);
762 }
763
764 /**
765  * ibmvfc_fail_request - Fail request with specified error code
766  * @evt:                ibmvfc event struct
767  * @error_code: error code to fail request with
768  *
769  * Return value:
770  *      none
771  **/
772 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
773 {
774         if (evt->cmnd) {
775                 evt->cmnd->result = (error_code << 16);
776                 evt->done = ibmvfc_scsi_eh_done;
777         } else
778                 evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
779
780         list_del(&evt->queue);
781         del_timer(&evt->timer);
782         ibmvfc_trc_end(evt);
783         evt->done(evt);
784 }
785
786 /**
787  * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
788  * @vhost:              ibmvfc host struct
789  * @error_code: error code to fail requests with
790  *
791  * Return value:
792  *      none
793  **/
794 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
795 {
796         struct ibmvfc_event *evt, *pos;
797
798         ibmvfc_dbg(vhost, "Purging all requests\n");
799         list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
800                 ibmvfc_fail_request(evt, error_code);
801 }
802
803 /**
804  * __ibmvfc_reset_host - Reset the connection to the server (no locking)
805  * @vhost:      struct ibmvfc host to reset
806  **/
807 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
808 {
809         int rc;
810
811         scsi_block_requests(vhost->host);
812         ibmvfc_purge_requests(vhost, DID_ERROR);
813         if ((rc = ibmvfc_reset_crq(vhost)) ||
814             (rc = ibmvfc_send_crq_init(vhost)) ||
815             (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
816                 dev_err(vhost->dev, "Error after reset rc=%d\n", rc);
817                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
818         } else
819                 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
820 }
821
822 /**
823  * ibmvfc_reset_host - Reset the connection to the server
824  * @vhost:      struct ibmvfc host to reset
825  **/
826 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
827 {
828         unsigned long flags;
829
830         spin_lock_irqsave(vhost->host->host_lock, flags);
831         __ibmvfc_reset_host(vhost);
832         spin_unlock_irqrestore(vhost->host->host_lock, flags);
833 }
834
835 /**
836  * ibmvfc_retry_host_init - Retry host initialization if allowed
837  * @vhost:      ibmvfc host struct
838  *
839  **/
840 static void ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
841 {
842         if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
843                 if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
844                         dev_err(vhost->dev,
845                                 "Host initialization retries exceeded. Taking adapter offline\n");
846                         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
847                 } else if (vhost->init_retries == IBMVFC_MAX_INIT_RETRIES)
848                         __ibmvfc_reset_host(vhost);
849                 else
850                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
851         }
852
853         wake_up(&vhost->work_wait_q);
854 }
855
856 /**
857  * __ibmvfc_get_target - Find the specified scsi_target (no locking)
858  * @starget:    scsi target struct
859  *
860  * Return value:
861  *      ibmvfc_target struct / NULL if not found
862  **/
863 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
864 {
865         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
866         struct ibmvfc_host *vhost = shost_priv(shost);
867         struct ibmvfc_target *tgt;
868
869         list_for_each_entry(tgt, &vhost->targets, queue)
870                 if (tgt->target_id == starget->id) {
871                         kref_get(&tgt->kref);
872                         return tgt;
873                 }
874         return NULL;
875 }
876
877 /**
878  * ibmvfc_get_target - Find the specified scsi_target
879  * @starget:    scsi target struct
880  *
881  * Return value:
882  *      ibmvfc_target struct / NULL if not found
883  **/
884 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
885 {
886         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
887         struct ibmvfc_target *tgt;
888         unsigned long flags;
889
890         spin_lock_irqsave(shost->host_lock, flags);
891         tgt = __ibmvfc_get_target(starget);
892         spin_unlock_irqrestore(shost->host_lock, flags);
893         return tgt;
894 }
895
896 /**
897  * ibmvfc_get_host_speed - Get host port speed
898  * @shost:              scsi host struct
899  *
900  * Return value:
901  *      none
902  **/
903 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
904 {
905         struct ibmvfc_host *vhost = shost_priv(shost);
906         unsigned long flags;
907
908         spin_lock_irqsave(shost->host_lock, flags);
909         if (vhost->state == IBMVFC_ACTIVE) {
910                 switch (vhost->login_buf->resp.link_speed / 100) {
911                 case 1:
912                         fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
913                         break;
914                 case 2:
915                         fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
916                         break;
917                 case 4:
918                         fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
919                         break;
920                 case 8:
921                         fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
922                         break;
923                 case 10:
924                         fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
925                         break;
926                 case 16:
927                         fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
928                         break;
929                 default:
930                         ibmvfc_log(vhost, 3, "Unknown port speed: %ld Gbit\n",
931                                    vhost->login_buf->resp.link_speed / 100);
932                         fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
933                         break;
934                 }
935         } else
936                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
937         spin_unlock_irqrestore(shost->host_lock, flags);
938 }
939
940 /**
941  * ibmvfc_get_host_port_state - Get host port state
942  * @shost:              scsi host struct
943  *
944  * Return value:
945  *      none
946  **/
947 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
948 {
949         struct ibmvfc_host *vhost = shost_priv(shost);
950         unsigned long flags;
951
952         spin_lock_irqsave(shost->host_lock, flags);
953         switch (vhost->state) {
954         case IBMVFC_INITIALIZING:
955         case IBMVFC_ACTIVE:
956                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
957                 break;
958         case IBMVFC_LINK_DOWN:
959                 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
960                 break;
961         case IBMVFC_LINK_DEAD:
962         case IBMVFC_HOST_OFFLINE:
963                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
964                 break;
965         case IBMVFC_HALTED:
966                 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
967                 break;
968         case IBMVFC_NO_CRQ:
969                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
970                 break;
971         default:
972                 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
973                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
974                 break;
975         }
976         spin_unlock_irqrestore(shost->host_lock, flags);
977 }
978
979 /**
980  * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
981  * @rport:              rport struct
982  * @timeout:    timeout value
983  *
984  * Return value:
985  *      none
986  **/
987 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
988 {
989         if (timeout)
990                 rport->dev_loss_tmo = timeout;
991         else
992                 rport->dev_loss_tmo = 1;
993 }
994
995 /**
996  * ibmvfc_release_tgt - Free memory allocated for a target
997  * @kref:               kref struct
998  *
999  **/
1000 static void ibmvfc_release_tgt(struct kref *kref)
1001 {
1002         struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1003         kfree(tgt);
1004 }
1005
1006 /**
1007  * ibmvfc_get_starget_node_name - Get SCSI target's node name
1008  * @starget:    scsi target struct
1009  *
1010  * Return value:
1011  *      none
1012  **/
1013 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1014 {
1015         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1016         fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1017         if (tgt)
1018                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1019 }
1020
1021 /**
1022  * ibmvfc_get_starget_port_name - Get SCSI target's port name
1023  * @starget:    scsi target struct
1024  *
1025  * Return value:
1026  *      none
1027  **/
1028 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1029 {
1030         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1031         fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1032         if (tgt)
1033                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1034 }
1035
1036 /**
1037  * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1038  * @starget:    scsi target struct
1039  *
1040  * Return value:
1041  *      none
1042  **/
1043 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1044 {
1045         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1046         fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1047         if (tgt)
1048                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1049 }
1050
1051 /**
1052  * ibmvfc_wait_while_resetting - Wait while the host resets
1053  * @vhost:              ibmvfc host struct
1054  *
1055  * Return value:
1056  *      0 on success / other on failure
1057  **/
1058 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1059 {
1060         long timeout = wait_event_timeout(vhost->init_wait_q,
1061                                           (vhost->state == IBMVFC_ACTIVE ||
1062                                            vhost->state == IBMVFC_HOST_OFFLINE ||
1063                                            vhost->state == IBMVFC_LINK_DEAD),
1064                                           (init_timeout * HZ));
1065
1066         return timeout ? 0 : -EIO;
1067 }
1068
1069 /**
1070  * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1071  * @shost:              scsi host struct
1072  *
1073  * Return value:
1074  *      0 on success / other on failure
1075  **/
1076 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1077 {
1078         struct ibmvfc_host *vhost = shost_priv(shost);
1079
1080         dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1081         ibmvfc_reset_host(vhost);
1082         return ibmvfc_wait_while_resetting(vhost);
1083 }
1084
1085 /**
1086  * ibmvfc_gather_partition_info - Gather info about the LPAR
1087  *
1088  * Return value:
1089  *      none
1090  **/
1091 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1092 {
1093         struct device_node *rootdn;
1094         const char *name;
1095         const unsigned int *num;
1096
1097         rootdn = of_find_node_by_path("/");
1098         if (!rootdn)
1099                 return;
1100
1101         name = of_get_property(rootdn, "ibm,partition-name", NULL);
1102         if (name)
1103                 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1104         num = of_get_property(rootdn, "ibm,partition-no", NULL);
1105         if (num)
1106                 vhost->partition_number = *num;
1107         of_node_put(rootdn);
1108 }
1109
1110 /**
1111  * ibmvfc_set_login_info - Setup info for NPIV login
1112  * @vhost:      ibmvfc host struct
1113  *
1114  * Return value:
1115  *      none
1116  **/
1117 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1118 {
1119         struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1120         struct device_node *of_node = vhost->dev->archdata.of_node;
1121         const char *location;
1122
1123         memset(login_info, 0, sizeof(*login_info));
1124
1125         login_info->ostype = IBMVFC_OS_LINUX;
1126         login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
1127         login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
1128         login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
1129         login_info->partition_num = vhost->partition_number;
1130         login_info->vfc_frame_version = 1;
1131         login_info->fcp_version = 3;
1132         if (vhost->client_migrated)
1133                 login_info->flags = IBMVFC_CLIENT_MIGRATED;
1134
1135         login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1136         login_info->capabilities = IBMVFC_CAN_MIGRATE;
1137         login_info->async.va = vhost->async_crq.msg_token;
1138         login_info->async.len = vhost->async_crq.size;
1139         strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1140         strncpy(login_info->device_name,
1141                 vhost->host->shost_gendev.bus_id, IBMVFC_MAX_NAME);
1142
1143         location = of_get_property(of_node, "ibm,loc-code", NULL);
1144         location = location ? location : vhost->dev->bus_id;
1145         strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1146 }
1147
1148 /**
1149  * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1150  * @vhost:      ibmvfc host who owns the event pool
1151  *
1152  * Returns zero on success.
1153  **/
1154 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1155 {
1156         int i;
1157         struct ibmvfc_event_pool *pool = &vhost->pool;
1158
1159         ENTER;
1160         pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1161         pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1162         if (!pool->events)
1163                 return -ENOMEM;
1164
1165         pool->iu_storage = dma_alloc_coherent(vhost->dev,
1166                                               pool->size * sizeof(*pool->iu_storage),
1167                                               &pool->iu_token, 0);
1168
1169         if (!pool->iu_storage) {
1170                 kfree(pool->events);
1171                 return -ENOMEM;
1172         }
1173
1174         for (i = 0; i < pool->size; ++i) {
1175                 struct ibmvfc_event *evt = &pool->events[i];
1176                 atomic_set(&evt->free, 1);
1177                 evt->crq.valid = 0x80;
1178                 evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
1179                 evt->xfer_iu = pool->iu_storage + i;
1180                 evt->vhost = vhost;
1181                 evt->ext_list = NULL;
1182                 list_add_tail(&evt->queue, &vhost->free);
1183         }
1184
1185         LEAVE;
1186         return 0;
1187 }
1188
1189 /**
1190  * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1191  * @vhost:      ibmvfc host who owns the event pool
1192  *
1193  **/
1194 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1195 {
1196         int i;
1197         struct ibmvfc_event_pool *pool = &vhost->pool;
1198
1199         ENTER;
1200         for (i = 0; i < pool->size; ++i) {
1201                 list_del(&pool->events[i].queue);
1202                 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1203                 if (pool->events[i].ext_list)
1204                         dma_pool_free(vhost->sg_pool,
1205                                       pool->events[i].ext_list,
1206                                       pool->events[i].ext_list_token);
1207         }
1208
1209         kfree(pool->events);
1210         dma_free_coherent(vhost->dev,
1211                           pool->size * sizeof(*pool->iu_storage),
1212                           pool->iu_storage, pool->iu_token);
1213         LEAVE;
1214 }
1215
1216 /**
1217  * ibmvfc_get_event - Gets the next free event in pool
1218  * @vhost:      ibmvfc host struct
1219  *
1220  * Returns a free event from the pool.
1221  **/
1222 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1223 {
1224         struct ibmvfc_event *evt;
1225
1226         BUG_ON(list_empty(&vhost->free));
1227         evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1228         atomic_set(&evt->free, 0);
1229         list_del(&evt->queue);
1230         return evt;
1231 }
1232
1233 /**
1234  * ibmvfc_init_event - Initialize fields in an event struct that are always
1235  *                              required.
1236  * @evt:        The event
1237  * @done:       Routine to call when the event is responded to
1238  * @format:     SRP or MAD format
1239  **/
1240 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1241                               void (*done) (struct ibmvfc_event *), u8 format)
1242 {
1243         evt->cmnd = NULL;
1244         evt->sync_iu = NULL;
1245         evt->crq.format = format;
1246         evt->done = done;
1247 }
1248
1249 /**
1250  * ibmvfc_map_sg_list - Initialize scatterlist
1251  * @scmd:       scsi command struct
1252  * @nseg:       number of scatterlist segments
1253  * @md: memory descriptor list to initialize
1254  **/
1255 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1256                                struct srp_direct_buf *md)
1257 {
1258         int i;
1259         struct scatterlist *sg;
1260
1261         scsi_for_each_sg(scmd, sg, nseg, i) {
1262                 md[i].va = sg_dma_address(sg);
1263                 md[i].len = sg_dma_len(sg);
1264                 md[i].key = 0;
1265         }
1266 }
1267
1268 /**
1269  * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1270  * @scmd:               Scsi_Cmnd with the scatterlist
1271  * @evt:                ibmvfc event struct
1272  * @vfc_cmd:    vfc_cmd that contains the memory descriptor
1273  * @dev:                device for which to map dma memory
1274  *
1275  * Returns:
1276  *      0 on success / non-zero on failure
1277  **/
1278 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1279                               struct ibmvfc_event *evt,
1280                               struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1281 {
1282
1283         int sg_mapped;
1284         struct srp_direct_buf *data = &vfc_cmd->ioba;
1285         struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1286
1287         sg_mapped = scsi_dma_map(scmd);
1288         if (!sg_mapped) {
1289                 vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
1290                 return 0;
1291         } else if (unlikely(sg_mapped < 0)) {
1292                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1293                         scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1294                 return sg_mapped;
1295         }
1296
1297         if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1298                 vfc_cmd->flags |= IBMVFC_WRITE;
1299                 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1300         } else {
1301                 vfc_cmd->flags |= IBMVFC_READ;
1302                 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1303         }
1304
1305         if (sg_mapped == 1) {
1306                 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1307                 return 0;
1308         }
1309
1310         vfc_cmd->flags |= IBMVFC_SCATTERLIST;
1311
1312         if (!evt->ext_list) {
1313                 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1314                                                &evt->ext_list_token);
1315
1316                 if (!evt->ext_list) {
1317                         scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1318                         return -ENOMEM;
1319                 }
1320         }
1321
1322         ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1323
1324         data->va = evt->ext_list_token;
1325         data->len = sg_mapped * sizeof(struct srp_direct_buf);
1326         data->key = 0;
1327         return 0;
1328 }
1329
1330 /**
1331  * ibmvfc_timeout - Internal command timeout handler
1332  * @evt:        struct ibmvfc_event that timed out
1333  *
1334  * Called when an internally generated command times out
1335  **/
1336 static void ibmvfc_timeout(struct ibmvfc_event *evt)
1337 {
1338         struct ibmvfc_host *vhost = evt->vhost;
1339         dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1340         ibmvfc_reset_host(vhost);
1341 }
1342
1343 /**
1344  * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1345  * @evt:                event to be sent
1346  * @vhost:              ibmvfc host struct
1347  * @timeout:    timeout in seconds - 0 means do not time command
1348  *
1349  * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1350  **/
1351 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1352                              struct ibmvfc_host *vhost, unsigned long timeout)
1353 {
1354         u64 *crq_as_u64 = (u64 *) &evt->crq;
1355         int rc;
1356
1357         /* Copy the IU into the transfer area */
1358         *evt->xfer_iu = evt->iu;
1359         if (evt->crq.format == IBMVFC_CMD_FORMAT)
1360                 evt->xfer_iu->cmd.tag = (u64)evt;
1361         else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1362                 evt->xfer_iu->mad_common.tag = (u64)evt;
1363         else
1364                 BUG();
1365
1366         list_add_tail(&evt->queue, &vhost->sent);
1367         init_timer(&evt->timer);
1368
1369         if (timeout) {
1370                 evt->timer.data = (unsigned long) evt;
1371                 evt->timer.expires = jiffies + (timeout * HZ);
1372                 evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1373                 add_timer(&evt->timer);
1374         }
1375
1376         if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
1377                 list_del(&evt->queue);
1378                 del_timer(&evt->timer);
1379
1380                 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1381                  * Firmware will send a CRQ with a transport event (0xFF) to
1382                  * tell this client what has happened to the transport. This
1383                  * will be handled in ibmvfc_handle_crq()
1384                  */
1385                 if (rc == H_CLOSED) {
1386                         if (printk_ratelimit())
1387                                 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1388                         if (evt->cmnd)
1389                                 scsi_dma_unmap(evt->cmnd);
1390                         ibmvfc_free_event(evt);
1391                         return SCSI_MLQUEUE_HOST_BUSY;
1392                 }
1393
1394                 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1395                 if (evt->cmnd) {
1396                         evt->cmnd->result = DID_ERROR << 16;
1397                         evt->done = ibmvfc_scsi_eh_done;
1398                 } else
1399                         evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
1400
1401                 evt->done(evt);
1402         } else
1403                 ibmvfc_trc_start(evt);
1404
1405         return 0;
1406 }
1407
1408 /**
1409  * ibmvfc_log_error - Log an error for the failed command if appropriate
1410  * @evt:        ibmvfc event to log
1411  *
1412  **/
1413 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1414 {
1415         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1416         struct ibmvfc_host *vhost = evt->vhost;
1417         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1418         struct scsi_cmnd *cmnd = evt->cmnd;
1419         const char *err = unknown_error;
1420         int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
1421         int logerr = 0;
1422         int rsp_code = 0;
1423
1424         if (index >= 0) {
1425                 logerr = cmd_status[index].log;
1426                 err = cmd_status[index].name;
1427         }
1428
1429         if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1430                 return;
1431
1432         if (rsp->flags & FCP_RSP_LEN_VALID)
1433                 rsp_code = rsp->data.info.rsp_code;
1434
1435         scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1436                     "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1437                     cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1438                     rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1439 }
1440
1441 /**
1442  * ibmvfc_scsi_done - Handle responses from commands
1443  * @evt:        ibmvfc event to be handled
1444  *
1445  * Used as a callback when sending scsi cmds.
1446  **/
1447 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1448 {
1449         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1450         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1451         struct scsi_cmnd *cmnd = evt->cmnd;
1452         int rsp_len = 0;
1453         int sense_len = rsp->fcp_sense_len;
1454
1455         if (cmnd) {
1456                 if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
1457                         scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
1458                 else if (rsp->flags & FCP_RESID_UNDER)
1459                         scsi_set_resid(cmnd, rsp->fcp_resid);
1460                 else
1461                         scsi_set_resid(cmnd, 0);
1462
1463                 if (vfc_cmd->status) {
1464                         cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1465
1466                         if (rsp->flags & FCP_RSP_LEN_VALID)
1467                                 rsp_len = rsp->fcp_rsp_len;
1468                         if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1469                                 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1470                         if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len)
1471                                 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1472
1473                         ibmvfc_log_error(evt);
1474                 }
1475
1476                 if (!cmnd->result &&
1477                     (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1478                         cmnd->result = (DID_ERROR << 16);
1479
1480                 scsi_dma_unmap(cmnd);
1481                 cmnd->scsi_done(cmnd);
1482         }
1483
1484         ibmvfc_free_event(evt);
1485 }
1486
1487 /**
1488  * ibmvfc_host_chkready - Check if the host can accept commands
1489  * @vhost:       struct ibmvfc host
1490  *
1491  * Returns:
1492  *      1 if host can accept command / 0 if not
1493  **/
1494 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1495 {
1496         int result = 0;
1497
1498         switch (vhost->state) {
1499         case IBMVFC_LINK_DEAD:
1500         case IBMVFC_HOST_OFFLINE:
1501                 result = DID_NO_CONNECT << 16;
1502                 break;
1503         case IBMVFC_NO_CRQ:
1504         case IBMVFC_INITIALIZING:
1505         case IBMVFC_HALTED:
1506         case IBMVFC_LINK_DOWN:
1507                 result = DID_REQUEUE << 16;
1508                 break;
1509         case IBMVFC_ACTIVE:
1510                 result = 0;
1511                 break;
1512         };
1513
1514         return result;
1515 }
1516
1517 /**
1518  * ibmvfc_queuecommand - The queuecommand function of the scsi template
1519  * @cmnd:       struct scsi_cmnd to be executed
1520  * @done:       Callback function to be called when cmnd is completed
1521  *
1522  * Returns:
1523  *      0 on success / other on failure
1524  **/
1525 static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
1526                                void (*done) (struct scsi_cmnd *))
1527 {
1528         struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1529         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1530         struct ibmvfc_cmd *vfc_cmd;
1531         struct ibmvfc_event *evt;
1532         u8 tag[2];
1533         int rc;
1534
1535         if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1536             unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1537                 cmnd->result = rc;
1538                 done(cmnd);
1539                 return 0;
1540         }
1541
1542         cmnd->result = (DID_OK << 16);
1543         evt = ibmvfc_get_event(vhost);
1544         ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1545         evt->cmnd = cmnd;
1546         cmnd->scsi_done = done;
1547         vfc_cmd = &evt->iu.cmd;
1548         memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1549         vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1550         vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
1551         vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
1552         vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
1553         vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
1554         vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
1555         vfc_cmd->tgt_scsi_id = rport->port_id;
1556         if ((rport->supported_classes & FC_COS_CLASS3) &&
1557             (fc_host_supported_classes(vhost->host) & FC_COS_CLASS3))
1558                 vfc_cmd->flags = IBMVFC_CLASS_3_ERR;
1559         vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
1560         int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1561         memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1562
1563         if (scsi_populate_tag_msg(cmnd, tag)) {
1564                 vfc_cmd->task_tag = tag[1];
1565                 switch (tag[0]) {
1566                 case MSG_SIMPLE_TAG:
1567                         vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1568                         break;
1569                 case MSG_HEAD_TAG:
1570                         vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
1571                         break;
1572                 case MSG_ORDERED_TAG:
1573                         vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
1574                         break;
1575                 };
1576         }
1577
1578         if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1579                 return ibmvfc_send_event(evt, vhost, 0);
1580
1581         ibmvfc_free_event(evt);
1582         if (rc == -ENOMEM)
1583                 return SCSI_MLQUEUE_HOST_BUSY;
1584
1585         if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1586                 scmd_printk(KERN_ERR, cmnd,
1587                             "Failed to map DMA buffer for command. rc=%d\n", rc);
1588
1589         cmnd->result = DID_ERROR << 16;
1590         done(cmnd);
1591         return 0;
1592 }
1593
1594 /**
1595  * ibmvfc_sync_completion - Signal that a synchronous command has completed
1596  * @evt:        ibmvfc event struct
1597  *
1598  **/
1599 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1600 {
1601         /* copy the response back */
1602         if (evt->sync_iu)
1603                 *evt->sync_iu = *evt->xfer_iu;
1604
1605         complete(&evt->comp);
1606 }
1607
1608 /**
1609  * ibmvfc_reset_device - Reset the device with the specified reset type
1610  * @sdev:       scsi device to reset
1611  * @type:       reset type
1612  * @desc:       reset type description for log messages
1613  *
1614  * Returns:
1615  *      0 on success / other on failure
1616  **/
1617 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1618 {
1619         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1620         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1621         struct ibmvfc_cmd *tmf;
1622         struct ibmvfc_event *evt;
1623         union ibmvfc_iu rsp_iu;
1624         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1625         int rsp_rc = -EBUSY;
1626         unsigned long flags;
1627         int rsp_code = 0;
1628
1629         spin_lock_irqsave(vhost->host->host_lock, flags);
1630         if (vhost->state == IBMVFC_ACTIVE) {
1631                 evt = ibmvfc_get_event(vhost);
1632                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1633
1634                 tmf = &evt->iu.cmd;
1635                 memset(tmf, 0, sizeof(*tmf));
1636                 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1637                 tmf->resp.len = sizeof(tmf->rsp);
1638                 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1639                 tmf->payload_len = sizeof(tmf->iu);
1640                 tmf->resp_len = sizeof(tmf->rsp);
1641                 tmf->cancel_key = (unsigned long)sdev->hostdata;
1642                 tmf->tgt_scsi_id = rport->port_id;
1643                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1644                 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1645                 tmf->iu.tmf_flags = type;
1646                 evt->sync_iu = &rsp_iu;
1647
1648                 init_completion(&evt->comp);
1649                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1650         }
1651         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1652
1653         if (rsp_rc != 0) {
1654                 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
1655                             desc, rsp_rc);
1656                 return -EIO;
1657         }
1658
1659         sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
1660         wait_for_completion(&evt->comp);
1661
1662         if (rsp_iu.cmd.status) {
1663                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1664                         rsp_code = fc_rsp->data.info.rsp_code;
1665
1666                 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
1667                             "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1668                             desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1669                             rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1670                             fc_rsp->scsi_status);
1671                 rsp_rc = -EIO;
1672         } else
1673                 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
1674
1675         spin_lock_irqsave(vhost->host->host_lock, flags);
1676         ibmvfc_free_event(evt);
1677         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1678         return rsp_rc;
1679 }
1680
1681 /**
1682  * ibmvfc_abort_task_set - Abort outstanding commands to the device
1683  * @sdev:       scsi device to abort commands
1684  *
1685  * This sends an Abort Task Set to the VIOS for the specified device. This does
1686  * NOT send any cancel to the VIOS. That must be done separately.
1687  *
1688  * Returns:
1689  *      0 on success / other on failure
1690  **/
1691 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
1692 {
1693         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1694         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1695         struct ibmvfc_cmd *tmf;
1696         struct ibmvfc_event *evt, *found_evt;
1697         union ibmvfc_iu rsp_iu;
1698         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1699         int rsp_rc = -EBUSY;
1700         unsigned long flags;
1701         int rsp_code = 0;
1702
1703         spin_lock_irqsave(vhost->host->host_lock, flags);
1704         found_evt = NULL;
1705         list_for_each_entry(evt, &vhost->sent, queue) {
1706                 if (evt->cmnd && evt->cmnd->device == sdev) {
1707                         found_evt = evt;
1708                         break;
1709                 }
1710         }
1711
1712         if (!found_evt) {
1713                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1714                         sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
1715                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1716                 return 0;
1717         }
1718
1719         if (vhost->state == IBMVFC_ACTIVE) {
1720                 evt = ibmvfc_get_event(vhost);
1721                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1722
1723                 tmf = &evt->iu.cmd;
1724                 memset(tmf, 0, sizeof(*tmf));
1725                 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1726                 tmf->resp.len = sizeof(tmf->rsp);
1727                 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1728                 tmf->payload_len = sizeof(tmf->iu);
1729                 tmf->resp_len = sizeof(tmf->rsp);
1730                 tmf->cancel_key = (unsigned long)sdev->hostdata;
1731                 tmf->tgt_scsi_id = rport->port_id;
1732                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1733                 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1734                 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
1735                 evt->sync_iu = &rsp_iu;
1736
1737                 init_completion(&evt->comp);
1738                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1739         }
1740
1741         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1742
1743         if (rsp_rc != 0) {
1744                 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
1745                 return -EIO;
1746         }
1747
1748         sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
1749         wait_for_completion(&evt->comp);
1750
1751         if (rsp_iu.cmd.status) {
1752                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1753                         rsp_code = fc_rsp->data.info.rsp_code;
1754
1755                 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
1756                             "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1757                             ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1758                             rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1759                             fc_rsp->scsi_status);
1760                 rsp_rc = -EIO;
1761         } else
1762                 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
1763
1764         spin_lock_irqsave(vhost->host->host_lock, flags);
1765         ibmvfc_free_event(evt);
1766         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1767         return rsp_rc;
1768 }
1769
1770 /**
1771  * ibmvfc_cancel_all - Cancel all outstanding commands to the device
1772  * @sdev:       scsi device to cancel commands
1773  * @type:       type of error recovery being performed
1774  *
1775  * This sends a cancel to the VIOS for the specified device. This does
1776  * NOT send any abort to the actual device. That must be done separately.
1777  *
1778  * Returns:
1779  *      0 on success / other on failure
1780  **/
1781 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
1782 {
1783         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1784         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1785         struct ibmvfc_tmf *tmf;
1786         struct ibmvfc_event *evt, *found_evt;
1787         union ibmvfc_iu rsp;
1788         int rsp_rc = -EBUSY;
1789         unsigned long flags;
1790         u16 status;
1791
1792         ENTER;
1793         spin_lock_irqsave(vhost->host->host_lock, flags);
1794         found_evt = NULL;
1795         list_for_each_entry(evt, &vhost->sent, queue) {
1796                 if (evt->cmnd && evt->cmnd->device == sdev) {
1797                         found_evt = evt;
1798                         break;
1799                 }
1800         }
1801
1802         if (!found_evt) {
1803                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1804                         sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
1805                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1806                 return 0;
1807         }
1808
1809         if (vhost->state == IBMVFC_ACTIVE) {
1810                 evt = ibmvfc_get_event(vhost);
1811                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1812
1813                 tmf = &evt->iu.tmf;
1814                 memset(tmf, 0, sizeof(*tmf));
1815                 tmf->common.version = 1;
1816                 tmf->common.opcode = IBMVFC_TMF_MAD;
1817                 tmf->common.length = sizeof(*tmf);
1818                 tmf->scsi_id = rport->port_id;
1819                 int_to_scsilun(sdev->lun, &tmf->lun);
1820                 tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
1821                 tmf->cancel_key = (unsigned long)sdev->hostdata;
1822                 tmf->my_cancel_key = (IBMVFC_TMF_CANCEL_KEY | (unsigned long)sdev->hostdata);
1823
1824                 evt->sync_iu = &rsp;
1825                 init_completion(&evt->comp);
1826                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1827         }
1828
1829         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1830
1831         if (rsp_rc != 0) {
1832                 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
1833                 return -EIO;
1834         }
1835
1836         sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
1837
1838         wait_for_completion(&evt->comp);
1839         status = rsp.mad_common.status;
1840         spin_lock_irqsave(vhost->host->host_lock, flags);
1841         ibmvfc_free_event(evt);
1842         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1843
1844         if (status != IBMVFC_MAD_SUCCESS) {
1845                 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
1846                 return -EIO;
1847         }
1848
1849         sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
1850         return 0;
1851 }
1852
1853 /**
1854  * ibmvfc_eh_abort_handler - Abort a command
1855  * @cmd:        scsi command to abort
1856  *
1857  * Returns:
1858  *      SUCCESS / FAILED
1859  **/
1860 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
1861 {
1862         struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1863         struct ibmvfc_event *evt, *pos;
1864         int cancel_rc, abort_rc;
1865         unsigned long flags;
1866
1867         ENTER;
1868         ibmvfc_wait_while_resetting(vhost);
1869         cancel_rc = ibmvfc_cancel_all(cmd->device, IBMVFC_TMF_ABORT_TASK_SET);
1870         abort_rc = ibmvfc_abort_task_set(cmd->device);
1871
1872         if (!cancel_rc && !abort_rc) {
1873                 spin_lock_irqsave(vhost->host->host_lock, flags);
1874                 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
1875                         if (evt->cmnd && evt->cmnd->device == cmd->device)
1876                                 ibmvfc_fail_request(evt, DID_ABORT);
1877                 }
1878                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1879                 LEAVE;
1880                 return SUCCESS;
1881         }
1882
1883         LEAVE;
1884         return FAILED;
1885 }
1886
1887 /**
1888  * ibmvfc_eh_device_reset_handler - Reset a single LUN
1889  * @cmd:        scsi command struct
1890  *
1891  * Returns:
1892  *      SUCCESS / FAILED
1893  **/
1894 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
1895 {
1896         struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1897         struct ibmvfc_event *evt, *pos;
1898         int cancel_rc, reset_rc;
1899         unsigned long flags;
1900
1901         ENTER;
1902         ibmvfc_wait_while_resetting(vhost);
1903         cancel_rc = ibmvfc_cancel_all(cmd->device, IBMVFC_TMF_LUN_RESET);
1904         reset_rc = ibmvfc_reset_device(cmd->device, IBMVFC_LUN_RESET, "LUN");
1905
1906         if (!cancel_rc && !reset_rc) {
1907                 spin_lock_irqsave(vhost->host->host_lock, flags);
1908                 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
1909                         if (evt->cmnd && evt->cmnd->device == cmd->device)
1910                                 ibmvfc_fail_request(evt, DID_ABORT);
1911                 }
1912                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1913                 LEAVE;
1914                 return SUCCESS;
1915         }
1916
1917         LEAVE;
1918         return FAILED;
1919 }
1920
1921 /**
1922  * ibmvfc_dev_cancel_all - Device iterated cancel all function
1923  * @sdev:       scsi device struct
1924  * @data:       return code
1925  *
1926  **/
1927 static void ibmvfc_dev_cancel_all(struct scsi_device *sdev, void *data)
1928 {
1929         unsigned long *rc = data;
1930         *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
1931 }
1932
1933 /**
1934  * ibmvfc_dev_abort_all - Device iterated abort task set function
1935  * @sdev:       scsi device struct
1936  * @data:       return code
1937  *
1938  **/
1939 static void ibmvfc_dev_abort_all(struct scsi_device *sdev, void *data)
1940 {
1941         unsigned long *rc = data;
1942         *rc |= ibmvfc_abort_task_set(sdev);
1943 }
1944
1945 /**
1946  * ibmvfc_eh_target_reset_handler - Reset the target
1947  * @cmd:        scsi command struct
1948  *
1949  * Returns:
1950  *      SUCCESS / FAILED
1951  **/
1952 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
1953 {
1954         struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1955         struct scsi_target *starget = scsi_target(cmd->device);
1956         struct ibmvfc_event *evt, *pos;
1957         int reset_rc;
1958         unsigned long cancel_rc = 0;
1959         unsigned long flags;
1960
1961         ENTER;
1962         ibmvfc_wait_while_resetting(vhost);
1963         starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
1964         reset_rc = ibmvfc_reset_device(cmd->device, IBMVFC_TARGET_RESET, "target");
1965
1966         if (!cancel_rc && !reset_rc) {
1967                 spin_lock_irqsave(vhost->host->host_lock, flags);
1968                 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
1969                         if (evt->cmnd && scsi_target(evt->cmnd->device) == starget)
1970                                 ibmvfc_fail_request(evt, DID_ABORT);
1971                 }
1972                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1973                 LEAVE;
1974                 return SUCCESS;
1975         }
1976
1977         LEAVE;
1978         return FAILED;
1979 }
1980
1981 /**
1982  * ibmvfc_eh_host_reset_handler - Reset the connection to the server
1983  * @cmd:        struct scsi_cmnd having problems
1984  *
1985  **/
1986 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
1987 {
1988         int rc;
1989         struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1990
1991         dev_err(vhost->dev, "Resetting connection due to error recovery\n");
1992         rc = ibmvfc_issue_fc_host_lip(vhost->host);
1993         return rc ? FAILED : SUCCESS;
1994 }
1995
1996 /**
1997  * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
1998  * @rport:              rport struct
1999  *
2000  * Return value:
2001  *      none
2002  **/
2003 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2004 {
2005         struct scsi_target *starget = to_scsi_target(&rport->dev);
2006         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2007         struct ibmvfc_host *vhost = shost_priv(shost);
2008         struct ibmvfc_event *evt, *pos;
2009         unsigned long cancel_rc = 0;
2010         unsigned long abort_rc = 0;
2011         unsigned long flags;
2012
2013         ENTER;
2014         starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2015         starget_for_each_device(starget, &abort_rc, ibmvfc_dev_abort_all);
2016
2017         if (!cancel_rc && !abort_rc) {
2018                 spin_lock_irqsave(shost->host_lock, flags);
2019                 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
2020                         if (evt->cmnd && scsi_target(evt->cmnd->device) == starget)
2021                                 ibmvfc_fail_request(evt, DID_ABORT);
2022                 }
2023                 spin_unlock_irqrestore(shost->host_lock, flags);
2024         } else
2025                 ibmvfc_issue_fc_host_lip(shost);
2026
2027         scsi_target_unblock(&rport->dev);
2028         LEAVE;
2029 }
2030
2031 static const struct {
2032         enum ibmvfc_async_event ae;
2033         const char *desc;
2034 } ae_desc [] = {
2035         { IBMVFC_AE_ELS_PLOGI,          "PLOGI" },
2036         { IBMVFC_AE_ELS_LOGO,           "LOGO" },
2037         { IBMVFC_AE_ELS_PRLO,           "PRLO" },
2038         { IBMVFC_AE_SCN_NPORT,          "N-Port SCN" },
2039         { IBMVFC_AE_SCN_GROUP,          "Group SCN" },
2040         { IBMVFC_AE_SCN_DOMAIN,         "Domain SCN" },
2041         { IBMVFC_AE_SCN_FABRIC,         "Fabric SCN" },
2042         { IBMVFC_AE_LINK_UP,            "Link Up" },
2043         { IBMVFC_AE_LINK_DOWN,          "Link Down" },
2044         { IBMVFC_AE_LINK_DEAD,          "Link Dead" },
2045         { IBMVFC_AE_HALT,                       "Halt" },
2046         { IBMVFC_AE_RESUME,             "Resume" },
2047         { IBMVFC_AE_ADAPTER_FAILED,     "Adapter Failed" },
2048 };
2049
2050 static const char *unknown_ae = "Unknown async";
2051
2052 /**
2053  * ibmvfc_get_ae_desc - Get text description for async event
2054  * @ae: async event
2055  *
2056  **/
2057 static const char *ibmvfc_get_ae_desc(u64 ae)
2058 {
2059         int i;
2060
2061         for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2062                 if (ae_desc[i].ae == ae)
2063                         return ae_desc[i].desc;
2064
2065         return unknown_ae;
2066 }
2067
2068 /**
2069  * ibmvfc_handle_async - Handle an async event from the adapter
2070  * @crq:        crq to process
2071  * @vhost:      ibmvfc host struct
2072  *
2073  **/
2074 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2075                                 struct ibmvfc_host *vhost)
2076 {
2077         const char *desc = ibmvfc_get_ae_desc(crq->event);
2078
2079         ibmvfc_log(vhost, 3, "%s event received\n", desc);
2080
2081         switch (crq->event) {
2082         case IBMVFC_AE_LINK_UP:
2083         case IBMVFC_AE_RESUME:
2084                 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2085                 ibmvfc_init_host(vhost);
2086                 break;
2087         case IBMVFC_AE_SCN_FABRIC:
2088                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2089                 ibmvfc_init_host(vhost);
2090                 break;
2091         case IBMVFC_AE_SCN_NPORT:
2092         case IBMVFC_AE_SCN_GROUP:
2093         case IBMVFC_AE_SCN_DOMAIN:
2094                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2095         case IBMVFC_AE_ELS_LOGO:
2096         case IBMVFC_AE_ELS_PRLO:
2097         case IBMVFC_AE_ELS_PLOGI:
2098                 ibmvfc_reinit_host(vhost);
2099                 break;
2100         case IBMVFC_AE_LINK_DOWN:
2101         case IBMVFC_AE_ADAPTER_FAILED:
2102                 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2103                 break;
2104         case IBMVFC_AE_LINK_DEAD:
2105                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2106                 break;
2107         case IBMVFC_AE_HALT:
2108                 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2109                 break;
2110         default:
2111                 dev_err(vhost->dev, "Unknown async event received: %ld\n", crq->event);
2112                 break;
2113         };
2114 }
2115
2116 /**
2117  * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2118  * @crq:        Command/Response queue
2119  * @vhost:      ibmvfc host struct
2120  *
2121  **/
2122 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2123 {
2124         long rc;
2125         struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2126
2127         switch (crq->valid) {
2128         case IBMVFC_CRQ_INIT_RSP:
2129                 switch (crq->format) {
2130                 case IBMVFC_CRQ_INIT:
2131                         dev_info(vhost->dev, "Partner initialized\n");
2132                         /* Send back a response */
2133                         rc = ibmvfc_send_crq_init_complete(vhost);
2134                         if (rc == 0)
2135                                 ibmvfc_init_host(vhost);
2136                         else
2137                                 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2138                         break;
2139                 case IBMVFC_CRQ_INIT_COMPLETE:
2140                         dev_info(vhost->dev, "Partner initialization complete\n");
2141                         ibmvfc_init_host(vhost);
2142                         break;
2143                 default:
2144                         dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2145                 }
2146                 return;
2147         case IBMVFC_CRQ_XPORT_EVENT:
2148                 vhost->state = IBMVFC_NO_CRQ;
2149                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2150                 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2151                         /* We need to re-setup the interpartition connection */
2152                         dev_info(vhost->dev, "Re-enabling adapter\n");
2153                         vhost->client_migrated = 1;
2154                         ibmvfc_purge_requests(vhost, DID_REQUEUE);
2155                         if ((rc = ibmvfc_reenable_crq_queue(vhost)) ||
2156                             (rc = ibmvfc_send_crq_init(vhost))) {
2157                                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2158                                 dev_err(vhost->dev, "Error after enable (rc=%ld)\n", rc);
2159                         } else
2160                                 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2161                 } else {
2162                         dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2163
2164                         ibmvfc_purge_requests(vhost, DID_ERROR);
2165                         if ((rc = ibmvfc_reset_crq(vhost)) ||
2166                             (rc = ibmvfc_send_crq_init(vhost))) {
2167                                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2168                                 dev_err(vhost->dev, "Error after reset (rc=%ld)\n", rc);
2169                         } else
2170                                 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2171                 }
2172                 return;
2173         case IBMVFC_CRQ_CMD_RSP:
2174                 break;
2175         default:
2176                 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2177                 return;
2178         }
2179
2180         if (crq->format == IBMVFC_ASYNC_EVENT)
2181                 return;
2182
2183         /* The only kind of payload CRQs we should get are responses to
2184          * things we send. Make sure this response is to something we
2185          * actually sent
2186          */
2187         if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2188                 dev_err(vhost->dev, "Returned correlation_token 0x%08lx is invalid!\n",
2189                         crq->ioba);
2190                 return;
2191         }
2192
2193         if (unlikely(atomic_read(&evt->free))) {
2194                 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08lx!\n",
2195                         crq->ioba);
2196                 return;
2197         }
2198
2199         del_timer(&evt->timer);
2200         list_del(&evt->queue);
2201         ibmvfc_trc_end(evt);
2202         evt->done(evt);
2203 }
2204
2205 /**
2206  * ibmvfc_scan_finished - Check if the device scan is done.
2207  * @shost:      scsi host struct
2208  * @time:       current elapsed time
2209  *
2210  * Returns:
2211  *      0 if scan is not done / 1 if scan is done
2212  **/
2213 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2214 {
2215         unsigned long flags;
2216         struct ibmvfc_host *vhost = shost_priv(shost);
2217         int done = 0;
2218
2219         spin_lock_irqsave(shost->host_lock, flags);
2220         if (time >= (init_timeout * HZ)) {
2221                 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2222                          "continuing initialization\n", init_timeout);
2223                 done = 1;
2224         }
2225
2226         if (vhost->state != IBMVFC_NO_CRQ && vhost->action == IBMVFC_HOST_ACTION_NONE)
2227                 done = 1;
2228         spin_unlock_irqrestore(shost->host_lock, flags);
2229         return done;
2230 }
2231
2232 /**
2233  * ibmvfc_slave_alloc - Setup the device's task set value
2234  * @sdev:       struct scsi_device device to configure
2235  *
2236  * Set the device's task set value so that error handling works as
2237  * expected.
2238  *
2239  * Returns:
2240  *      0 on success / -ENXIO if device does not exist
2241  **/
2242 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2243 {
2244         struct Scsi_Host *shost = sdev->host;
2245         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2246         struct ibmvfc_host *vhost = shost_priv(shost);
2247         unsigned long flags = 0;
2248
2249         if (!rport || fc_remote_port_chkready(rport))
2250                 return -ENXIO;
2251
2252         spin_lock_irqsave(shost->host_lock, flags);
2253         sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2254         spin_unlock_irqrestore(shost->host_lock, flags);
2255         return 0;
2256 }
2257
2258 /**
2259  * ibmvfc_slave_configure - Configure the device
2260  * @sdev:       struct scsi_device device to configure
2261  *
2262  * Enable allow_restart for a device if it is a disk. Adjust the
2263  * queue_depth here also.
2264  *
2265  * Returns:
2266  *      0
2267  **/
2268 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2269 {
2270         struct Scsi_Host *shost = sdev->host;
2271         struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
2272         unsigned long flags = 0;
2273
2274         spin_lock_irqsave(shost->host_lock, flags);
2275         if (sdev->type == TYPE_DISK)
2276                 sdev->allow_restart = 1;
2277
2278         if (sdev->tagged_supported) {
2279                 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2280                 scsi_activate_tcq(sdev, sdev->queue_depth);
2281         } else
2282                 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2283
2284         rport->dev_loss_tmo = dev_loss_tmo;
2285         spin_unlock_irqrestore(shost->host_lock, flags);
2286         return 0;
2287 }
2288
2289 /**
2290  * ibmvfc_change_queue_depth - Change the device's queue depth
2291  * @sdev:       scsi device struct
2292  * @qdepth:     depth to set
2293  *
2294  * Return value:
2295  *      actual depth set
2296  **/
2297 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2298 {
2299         if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2300                 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2301
2302         scsi_adjust_queue_depth(sdev, 0, qdepth);
2303         return sdev->queue_depth;
2304 }
2305
2306 /**
2307  * ibmvfc_change_queue_type - Change the device's queue type
2308  * @sdev:               scsi device struct
2309  * @tag_type:   type of tags to use
2310  *
2311  * Return value:
2312  *      actual queue type set
2313  **/
2314 static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2315 {
2316         if (sdev->tagged_supported) {
2317                 scsi_set_tag_type(sdev, tag_type);
2318
2319                 if (tag_type)
2320                         scsi_activate_tcq(sdev, sdev->queue_depth);
2321                 else
2322                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
2323         } else
2324                 tag_type = 0;
2325
2326         return tag_type;
2327 }
2328
2329 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2330                                                  struct device_attribute *attr, char *buf)
2331 {
2332         struct Scsi_Host *shost = class_to_shost(dev);
2333         struct ibmvfc_host *vhost = shost_priv(shost);
2334
2335         return snprintf(buf, PAGE_SIZE, "%s\n",
2336                         vhost->login_buf->resp.partition_name);
2337 }
2338
2339 static struct device_attribute ibmvfc_host_partition_name = {
2340         .attr = {
2341                 .name = "partition_name",
2342                 .mode = S_IRUGO,
2343         },
2344         .show = ibmvfc_show_host_partition_name,
2345 };
2346
2347 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2348                                             struct device_attribute *attr, char *buf)
2349 {
2350         struct Scsi_Host *shost = class_to_shost(dev);
2351         struct ibmvfc_host *vhost = shost_priv(shost);
2352
2353         return snprintf(buf, PAGE_SIZE, "%s\n",
2354                         vhost->login_buf->resp.device_name);
2355 }
2356
2357 static struct device_attribute ibmvfc_host_device_name = {
2358         .attr = {
2359                 .name = "device_name",
2360                 .mode = S_IRUGO,
2361         },
2362         .show = ibmvfc_show_host_device_name,
2363 };
2364
2365 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2366                                          struct device_attribute *attr, char *buf)
2367 {
2368         struct Scsi_Host *shost = class_to_shost(dev);
2369         struct ibmvfc_host *vhost = shost_priv(shost);
2370
2371         return snprintf(buf, PAGE_SIZE, "%s\n",
2372                         vhost->login_buf->resp.port_loc_code);
2373 }
2374
2375 static struct device_attribute ibmvfc_host_loc_code = {
2376         .attr = {
2377                 .name = "port_loc_code",
2378                 .mode = S_IRUGO,
2379         },
2380         .show = ibmvfc_show_host_loc_code,
2381 };
2382
2383 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2384                                          struct device_attribute *attr, char *buf)
2385 {
2386         struct Scsi_Host *shost = class_to_shost(dev);
2387         struct ibmvfc_host *vhost = shost_priv(shost);
2388
2389         return snprintf(buf, PAGE_SIZE, "%s\n",
2390                         vhost->login_buf->resp.drc_name);
2391 }
2392
2393 static struct device_attribute ibmvfc_host_drc_name = {
2394         .attr = {
2395                 .name = "drc_name",
2396                 .mode = S_IRUGO,
2397         },
2398         .show = ibmvfc_show_host_drc_name,
2399 };
2400
2401 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2402                                              struct device_attribute *attr, char *buf)
2403 {
2404         struct Scsi_Host *shost = class_to_shost(dev);
2405         struct ibmvfc_host *vhost = shost_priv(shost);
2406         return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2407 }
2408
2409 static struct device_attribute ibmvfc_host_npiv_version = {
2410         .attr = {
2411                 .name = "npiv_version",
2412                 .mode = S_IRUGO,
2413         },
2414         .show = ibmvfc_show_host_npiv_version,
2415 };
2416
2417 /**
2418  * ibmvfc_show_log_level - Show the adapter's error logging level
2419  * @dev:        class device struct
2420  * @buf:        buffer
2421  *
2422  * Return value:
2423  *      number of bytes printed to buffer
2424  **/
2425 static ssize_t ibmvfc_show_log_level(struct device *dev,
2426                                      struct device_attribute *attr, char *buf)
2427 {
2428         struct Scsi_Host *shost = class_to_shost(dev);
2429         struct ibmvfc_host *vhost = shost_priv(shost);
2430         unsigned long flags = 0;
2431         int len;
2432
2433         spin_lock_irqsave(shost->host_lock, flags);
2434         len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2435         spin_unlock_irqrestore(shost->host_lock, flags);
2436         return len;
2437 }
2438
2439 /**
2440  * ibmvfc_store_log_level - Change the adapter's error logging level
2441  * @dev:        class device struct
2442  * @buf:        buffer
2443  *
2444  * Return value:
2445  *      number of bytes printed to buffer
2446  **/
2447 static ssize_t ibmvfc_store_log_level(struct device *dev,
2448                                       struct device_attribute *attr,
2449                                       const char *buf, size_t count)
2450 {
2451         struct Scsi_Host *shost = class_to_shost(dev);
2452         struct ibmvfc_host *vhost = shost_priv(shost);
2453         unsigned long flags = 0;
2454
2455         spin_lock_irqsave(shost->host_lock, flags);
2456         vhost->log_level = simple_strtoul(buf, NULL, 10);
2457         spin_unlock_irqrestore(shost->host_lock, flags);
2458         return strlen(buf);
2459 }
2460
2461 static struct device_attribute ibmvfc_log_level_attr = {
2462         .attr = {
2463                 .name =         "log_level",
2464                 .mode =         S_IRUGO | S_IWUSR,
2465         },
2466         .show = ibmvfc_show_log_level,
2467         .store = ibmvfc_store_log_level
2468 };
2469
2470 #ifdef CONFIG_SCSI_IBMVFC_TRACE
2471 /**
2472  * ibmvfc_read_trace - Dump the adapter trace
2473  * @kobj:               kobject struct
2474  * @bin_attr:   bin_attribute struct
2475  * @buf:                buffer
2476  * @off:                offset
2477  * @count:              buffer size
2478  *
2479  * Return value:
2480  *      number of bytes printed to buffer
2481  **/
2482 static ssize_t ibmvfc_read_trace(struct kobject *kobj,
2483                                  struct bin_attribute *bin_attr,
2484                                  char *buf, loff_t off, size_t count)
2485 {
2486         struct device *dev = container_of(kobj, struct device, kobj);
2487         struct Scsi_Host *shost = class_to_shost(dev);
2488         struct ibmvfc_host *vhost = shost_priv(shost);
2489         unsigned long flags = 0;
2490         int size = IBMVFC_TRACE_SIZE;
2491         char *src = (char *)vhost->trace;
2492
2493         if (off > size)
2494                 return 0;
2495         if (off + count > size) {
2496                 size -= off;
2497                 count = size;
2498         }
2499
2500         spin_lock_irqsave(shost->host_lock, flags);
2501         memcpy(buf, &src[off], count);
2502         spin_unlock_irqrestore(shost->host_lock, flags);
2503         return count;
2504 }
2505
2506 static struct bin_attribute ibmvfc_trace_attr = {
2507         .attr = {
2508                 .name = "trace",
2509                 .mode = S_IRUGO,
2510         },
2511         .size = 0,
2512         .read = ibmvfc_read_trace,
2513 };
2514 #endif
2515
2516 static struct device_attribute *ibmvfc_attrs[] = {
2517         &ibmvfc_host_partition_name,
2518         &ibmvfc_host_device_name,
2519         &ibmvfc_host_loc_code,
2520         &ibmvfc_host_drc_name,
2521         &ibmvfc_host_npiv_version,
2522         &ibmvfc_log_level_attr,
2523         NULL
2524 };
2525
2526 static struct scsi_host_template driver_template = {
2527         .module = THIS_MODULE,
2528         .name = "IBM POWER Virtual FC Adapter",
2529         .proc_name = IBMVFC_NAME,
2530         .queuecommand = ibmvfc_queuecommand,
2531         .eh_abort_handler = ibmvfc_eh_abort_handler,
2532         .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
2533         .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
2534         .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
2535         .slave_alloc = ibmvfc_slave_alloc,
2536         .slave_configure = ibmvfc_slave_configure,
2537         .scan_finished = ibmvfc_scan_finished,
2538         .change_queue_depth = ibmvfc_change_queue_depth,
2539         .change_queue_type = ibmvfc_change_queue_type,
2540         .cmd_per_lun = 16,
2541         .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
2542         .this_id = -1,
2543         .sg_tablesize = SG_ALL,
2544         .max_sectors = IBMVFC_MAX_SECTORS,
2545         .use_clustering = ENABLE_CLUSTERING,
2546         .shost_attrs = ibmvfc_attrs,
2547 };
2548
2549 /**
2550  * ibmvfc_next_async_crq - Returns the next entry in async queue
2551  * @vhost:      ibmvfc host struct
2552  *
2553  * Returns:
2554  *      Pointer to next entry in queue / NULL if empty
2555  **/
2556 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
2557 {
2558         struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
2559         struct ibmvfc_async_crq *crq;
2560
2561         crq = &async_crq->msgs[async_crq->cur];
2562         if (crq->valid & 0x80) {
2563                 if (++async_crq->cur == async_crq->size)
2564                         async_crq->cur = 0;
2565         } else
2566                 crq = NULL;
2567
2568         return crq;
2569 }
2570
2571 /**
2572  * ibmvfc_next_crq - Returns the next entry in message queue
2573  * @vhost:      ibmvfc host struct
2574  *
2575  * Returns:
2576  *      Pointer to next entry in queue / NULL if empty
2577  **/
2578 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
2579 {
2580         struct ibmvfc_crq_queue *queue = &vhost->crq;
2581         struct ibmvfc_crq *crq;
2582
2583         crq = &queue->msgs[queue->cur];
2584         if (crq->valid & 0x80) {
2585                 if (++queue->cur == queue->size)
2586                         queue->cur = 0;
2587         } else
2588                 crq = NULL;
2589
2590         return crq;
2591 }
2592
2593 /**
2594  * ibmvfc_interrupt - Interrupt handler
2595  * @irq:                number of irq to handle, not used
2596  * @dev_instance: ibmvfc_host that received interrupt
2597  *
2598  * Returns:
2599  *      IRQ_HANDLED
2600  **/
2601 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
2602 {
2603         struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
2604         struct vio_dev *vdev = to_vio_dev(vhost->dev);
2605         struct ibmvfc_crq *crq;
2606         struct ibmvfc_async_crq *async;
2607         unsigned long flags;
2608         int done = 0;
2609
2610         spin_lock_irqsave(vhost->host->host_lock, flags);
2611         vio_disable_interrupts(to_vio_dev(vhost->dev));
2612         while (!done) {
2613                 /* Pull all the valid messages off the CRQ */
2614                 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2615                         ibmvfc_handle_crq(crq, vhost);
2616                         crq->valid = 0;
2617                 }
2618
2619                 /* Pull all the valid messages off the async CRQ */
2620                 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2621                         ibmvfc_handle_async(async, vhost);
2622                         async->valid = 0;
2623                 }
2624
2625                 vio_enable_interrupts(vdev);
2626                 if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2627                         vio_disable_interrupts(vdev);
2628                         ibmvfc_handle_crq(crq, vhost);
2629                         crq->valid = 0;
2630                 } else if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2631                         vio_disable_interrupts(vdev);
2632                         ibmvfc_handle_async(async, vhost);
2633                         crq->valid = 0;
2634                 } else
2635                         done = 1;
2636         }
2637
2638         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2639         return IRQ_HANDLED;
2640 }
2641
2642 /**
2643  * ibmvfc_init_tgt - Set the next init job step for the target
2644  * @tgt:                ibmvfc target struct
2645  * @job_step:   job step to perform
2646  *
2647  **/
2648 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
2649                             void (*job_step) (struct ibmvfc_target *))
2650 {
2651         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
2652         tgt->job_step = job_step;
2653         wake_up(&tgt->vhost->work_wait_q);
2654 }
2655
2656 /**
2657  * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
2658  * @tgt:                ibmvfc target struct
2659  * @job_step:   initialization job step
2660  *
2661  **/
2662 static void ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
2663                                   void (*job_step) (struct ibmvfc_target *))
2664 {
2665         if (++tgt->init_retries > IBMVFC_MAX_INIT_RETRIES) {
2666                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2667                 wake_up(&tgt->vhost->work_wait_q);
2668         } else
2669                 ibmvfc_init_tgt(tgt, job_step);
2670 }
2671
2672 /**
2673  * ibmvfc_tgt_prli_done - Completion handler for Process Login
2674  * @evt:        ibmvfc event struct
2675  *
2676  **/
2677 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
2678 {
2679         struct ibmvfc_target *tgt = evt->tgt;
2680         struct ibmvfc_host *vhost = evt->vhost;
2681         struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
2682         u32 status = rsp->common.status;
2683
2684         vhost->discovery_threads--;
2685         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2686         switch (status) {
2687         case IBMVFC_MAD_SUCCESS:
2688                 tgt_dbg(tgt, "Process Login succeeded\n");
2689                 tgt->need_login = 0;
2690                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_ADD_RPORT);
2691                 break;
2692         case IBMVFC_MAD_DRIVER_FAILED:
2693                 break;
2694         case IBMVFC_MAD_CRQ_ERROR:
2695                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2696                 break;
2697         case IBMVFC_MAD_FAILED:
2698         default:
2699                 tgt_err(tgt, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
2700                         ibmvfc_get_cmd_error(rsp->status, rsp->error),
2701                         rsp->status, rsp->error, status);
2702                 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2703                         ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2704                 break;
2705         };
2706
2707         kref_put(&tgt->kref, ibmvfc_release_tgt);
2708         ibmvfc_free_event(evt);
2709         wake_up(&vhost->work_wait_q);
2710 }
2711
2712 /**
2713  * ibmvfc_tgt_send_prli - Send a process login
2714  * @tgt:        ibmvfc target struct
2715  *
2716  **/
2717 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
2718 {
2719         struct ibmvfc_process_login *prli;
2720         struct ibmvfc_host *vhost = tgt->vhost;
2721         struct ibmvfc_event *evt;
2722
2723         if (vhost->discovery_threads >= disc_threads)
2724                 return;
2725
2726         kref_get(&tgt->kref);
2727         evt = ibmvfc_get_event(vhost);
2728         vhost->discovery_threads++;
2729         ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
2730         evt->tgt = tgt;
2731         prli = &evt->iu.prli;
2732         memset(prli, 0, sizeof(*prli));
2733         prli->common.version = 1;
2734         prli->common.opcode = IBMVFC_PROCESS_LOGIN;
2735         prli->common.length = sizeof(*prli);
2736         prli->scsi_id = tgt->scsi_id;
2737
2738         prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
2739         prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
2740         prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
2741
2742         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2743         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2744                 vhost->discovery_threads--;
2745                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2746                 kref_put(&tgt->kref, ibmvfc_release_tgt);
2747         } else
2748                 tgt_dbg(tgt, "Sent process login\n");
2749 }
2750
2751 /**
2752  * ibmvfc_tgt_plogi_done - Completion handler for Port Login
2753  * @evt:        ibmvfc event struct
2754  *
2755  **/
2756 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
2757 {
2758         struct ibmvfc_target *tgt = evt->tgt;
2759         struct ibmvfc_host *vhost = evt->vhost;
2760         struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
2761         u32 status = rsp->common.status;
2762
2763         vhost->discovery_threads--;
2764         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2765         switch (status) {
2766         case IBMVFC_MAD_SUCCESS:
2767                 tgt_dbg(tgt, "Port Login succeeded\n");
2768                 if (tgt->ids.port_name &&
2769                     tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
2770                         vhost->reinit = 1;
2771                         tgt_dbg(tgt, "Port re-init required\n");
2772                         break;
2773                 }
2774                 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
2775                 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
2776                 tgt->ids.port_id = tgt->scsi_id;
2777                 tgt->ids.roles = FC_PORT_ROLE_FCP_TARGET;
2778                 memcpy(&tgt->service_parms, &rsp->service_parms,
2779                        sizeof(tgt->service_parms));
2780                 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
2781                        sizeof(tgt->service_parms_change));
2782                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
2783                 break;
2784         case IBMVFC_MAD_DRIVER_FAILED:
2785                 break;
2786         case IBMVFC_MAD_CRQ_ERROR:
2787                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2788                 break;
2789         case IBMVFC_MAD_FAILED:
2790         default:
2791                 tgt_err(tgt, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2792                         ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
2793                         ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
2794                         ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
2795
2796                 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2797                         ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2798                 break;
2799         };
2800
2801         kref_put(&tgt->kref, ibmvfc_release_tgt);
2802         ibmvfc_free_event(evt);
2803         wake_up(&vhost->work_wait_q);
2804 }
2805
2806 /**
2807  * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
2808  * @tgt:        ibmvfc target struct
2809  *
2810  **/
2811 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
2812 {
2813         struct ibmvfc_port_login *plogi;
2814         struct ibmvfc_host *vhost = tgt->vhost;
2815         struct ibmvfc_event *evt;
2816
2817         if (vhost->discovery_threads >= disc_threads)
2818                 return;
2819
2820         kref_get(&tgt->kref);
2821         evt = ibmvfc_get_event(vhost);
2822         vhost->discovery_threads++;
2823         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2824         ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
2825         evt->tgt = tgt;
2826         plogi = &evt->iu.plogi;
2827         memset(plogi, 0, sizeof(*plogi));
2828         plogi->common.version = 1;
2829         plogi->common.opcode = IBMVFC_PORT_LOGIN;
2830         plogi->common.length = sizeof(*plogi);
2831         plogi->scsi_id = tgt->scsi_id;
2832
2833         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2834                 vhost->discovery_threads--;
2835                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2836                 kref_put(&tgt->kref, ibmvfc_release_tgt);
2837         } else
2838                 tgt_dbg(tgt, "Sent port login\n");
2839 }
2840
2841 /**
2842  * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
2843  * @evt:        ibmvfc event struct
2844  *
2845  **/
2846 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
2847 {
2848         struct ibmvfc_target *tgt = evt->tgt;
2849         struct ibmvfc_host *vhost = evt->vhost;
2850         struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
2851         u32 status = rsp->common.status;
2852
2853         vhost->discovery_threads--;
2854         ibmvfc_free_event(evt);
2855         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2856
2857         switch (status) {
2858         case IBMVFC_MAD_SUCCESS:
2859                 tgt_dbg(tgt, "Implicit Logout succeeded\n");
2860                 break;
2861         case IBMVFC_MAD_DRIVER_FAILED:
2862                 kref_put(&tgt->kref, ibmvfc_release_tgt);
2863                 wake_up(&vhost->work_wait_q);
2864                 return;
2865         case IBMVFC_MAD_FAILED:
2866         default:
2867                 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
2868                 break;
2869         };
2870
2871         if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
2872                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
2873         else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
2874                  tgt->scsi_id != tgt->new_scsi_id)
2875                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2876         kref_put(&tgt->kref, ibmvfc_release_tgt);
2877         wake_up(&vhost->work_wait_q);
2878 }
2879
2880 /**
2881  * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
2882  * @tgt:                ibmvfc target struct
2883  *
2884  **/
2885 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
2886 {
2887         struct ibmvfc_implicit_logout *mad;
2888         struct ibmvfc_host *vhost = tgt->vhost;
2889         struct ibmvfc_event *evt;
2890
2891         if (vhost->discovery_threads >= disc_threads)
2892                 return;
2893
2894         kref_get(&tgt->kref);
2895         evt = ibmvfc_get_event(vhost);
2896         vhost->discovery_threads++;
2897         ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
2898         evt->tgt = tgt;
2899         mad = &evt->iu.implicit_logout;
2900         memset(mad, 0, sizeof(*mad));
2901         mad->common.version = 1;
2902         mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
2903         mad->common.length = sizeof(*mad);
2904         mad->old_scsi_id = tgt->scsi_id;
2905
2906         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2907         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2908                 vhost->discovery_threads--;
2909                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2910                 kref_put(&tgt->kref, ibmvfc_release_tgt);
2911         } else
2912                 tgt_dbg(tgt, "Sent Implicit Logout\n");
2913 }
2914
2915 /**
2916  * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
2917  * @evt:        ibmvfc event struct
2918  *
2919  **/
2920 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
2921 {
2922         struct ibmvfc_target *tgt = evt->tgt;
2923         struct ibmvfc_host *vhost = evt->vhost;
2924         struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
2925         u32 status = rsp->common.status;
2926
2927         vhost->discovery_threads--;
2928         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2929         switch (status) {
2930         case IBMVFC_MAD_SUCCESS:
2931                 tgt_dbg(tgt, "Query Target succeeded\n");
2932                 tgt->new_scsi_id = rsp->scsi_id;
2933                 if (rsp->scsi_id != tgt->scsi_id)
2934                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
2935                 break;
2936         case IBMVFC_MAD_DRIVER_FAILED:
2937                 break;
2938         case IBMVFC_MAD_CRQ_ERROR:
2939                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
2940                 break;
2941         case IBMVFC_MAD_FAILED:
2942         default:
2943                 tgt_err(tgt, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2944                         ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
2945                         ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
2946                         ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
2947
2948                 if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
2949                     rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
2950                     rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
2951                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2952                 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2953                         ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
2954                 break;
2955         };
2956
2957         kref_put(&tgt->kref, ibmvfc_release_tgt);
2958         ibmvfc_free_event(evt);
2959         wake_up(&vhost->work_wait_q);
2960 }
2961
2962 /**
2963  * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
2964  * @tgt:        ibmvfc target struct
2965  *
2966  **/
2967 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
2968 {
2969         struct ibmvfc_query_tgt *query_tgt;
2970         struct ibmvfc_host *vhost = tgt->vhost;
2971         struct ibmvfc_event *evt;
2972
2973         if (vhost->discovery_threads >= disc_threads)
2974                 return;
2975
2976         kref_get(&tgt->kref);
2977         evt = ibmvfc_get_event(vhost);
2978         vhost->discovery_threads++;
2979         evt->tgt = tgt;
2980         ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
2981         query_tgt = &evt->iu.query_tgt;
2982         memset(query_tgt, 0, sizeof(*query_tgt));
2983         query_tgt->common.version = 1;
2984         query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
2985         query_tgt->common.length = sizeof(*query_tgt);
2986         query_tgt->wwpn = tgt->ids.port_name;
2987
2988         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2989         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2990                 vhost->discovery_threads--;
2991                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2992                 kref_put(&tgt->kref, ibmvfc_release_tgt);
2993         } else
2994                 tgt_dbg(tgt, "Sent Query Target\n");
2995 }
2996
2997 /**
2998  * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
2999  * @vhost:              ibmvfc host struct
3000  * @scsi_id:    SCSI ID to allocate target for
3001  *
3002  * Returns:
3003  *      0 on success / other on failure
3004  **/
3005 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3006 {
3007         struct ibmvfc_target *tgt;
3008         unsigned long flags;
3009
3010         spin_lock_irqsave(vhost->host->host_lock, flags);
3011         list_for_each_entry(tgt, &vhost->targets, queue) {
3012                 if (tgt->scsi_id == scsi_id) {
3013                         if (tgt->need_login)
3014                                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3015                         goto unlock_out;
3016                 }
3017         }
3018         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3019
3020         tgt = mempool_alloc(vhost->tgt_pool, GFP_KERNEL);
3021         if (!tgt) {
3022                 dev_err(vhost->dev, "Target allocation failure for scsi id %08lx\n",
3023                         scsi_id);
3024                 return -ENOMEM;
3025         }
3026
3027         tgt->scsi_id = scsi_id;
3028         tgt->new_scsi_id = scsi_id;
3029         tgt->vhost = vhost;
3030         tgt->need_login = 1;
3031         kref_init(&tgt->kref);
3032         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3033         spin_lock_irqsave(vhost->host->host_lock, flags);
3034         list_add_tail(&tgt->queue, &vhost->targets);
3035
3036 unlock_out:
3037         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3038         return 0;
3039 }
3040
3041 /**
3042  * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3043  * @vhost:              ibmvfc host struct
3044  *
3045  * Returns:
3046  *      0 on success / other on failure
3047  **/
3048 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3049 {
3050         int i, rc;
3051
3052         for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3053                 rc = ibmvfc_alloc_target(vhost,
3054                                          vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3055
3056         return rc;
3057 }
3058
3059 /**
3060  * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3061  * @evt:        ibmvfc event struct
3062  *
3063  **/
3064 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3065 {
3066         struct ibmvfc_host *vhost = evt->vhost;
3067         struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3068         u32 mad_status = rsp->common.status;
3069
3070         switch (mad_status) {
3071         case IBMVFC_MAD_SUCCESS:
3072                 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3073                 vhost->num_targets = rsp->num_written;
3074                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3075                 break;
3076         case IBMVFC_MAD_FAILED:
3077                 dev_err(vhost->dev, "Discover Targets failed: %s (%x:%x)\n",
3078                         ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3079                 ibmvfc_retry_host_init(vhost);
3080                 break;
3081         case IBMVFC_MAD_DRIVER_FAILED:
3082                 break;
3083         default:
3084                 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3085                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3086                 break;
3087         }
3088
3089         ibmvfc_free_event(evt);
3090         wake_up(&vhost->work_wait_q);
3091 }
3092
3093 /**
3094  * ibmvfc_discover_targets - Send Discover Targets MAD
3095  * @vhost:      ibmvfc host struct
3096  *
3097  **/
3098 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3099 {
3100         struct ibmvfc_discover_targets *mad;
3101         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3102
3103         ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3104         mad = &evt->iu.discover_targets;
3105         memset(mad, 0, sizeof(*mad));
3106         mad->common.version = 1;
3107         mad->common.opcode = IBMVFC_DISC_TARGETS;
3108         mad->common.length = sizeof(*mad);
3109         mad->bufflen = vhost->disc_buf_sz;
3110         mad->buffer.va = vhost->disc_buf_dma;
3111         mad->buffer.len = vhost->disc_buf_sz;
3112         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3113
3114         if (!ibmvfc_send_event(evt, vhost, default_timeout))
3115                 ibmvfc_dbg(vhost, "Sent discover targets\n");
3116         else
3117                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3118 }
3119
3120 /**
3121  * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3122  * @evt:        ibmvfc event struct
3123  *
3124  **/
3125 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3126 {
3127         struct ibmvfc_host *vhost = evt->vhost;
3128         u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3129         struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3130         unsigned int npiv_max_sectors;
3131
3132         switch (mad_status) {
3133         case IBMVFC_MAD_SUCCESS:
3134                 ibmvfc_free_event(evt);
3135                 break;
3136         case IBMVFC_MAD_FAILED:
3137                 dev_err(vhost->dev, "NPIV Login failed: %s (%x:%x)\n",
3138                         ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3139                 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3140                         ibmvfc_retry_host_init(vhost);
3141                 else
3142                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3143                 ibmvfc_free_event(evt);
3144                 return;
3145         case IBMVFC_MAD_CRQ_ERROR:
3146                 ibmvfc_retry_host_init(vhost);
3147         case IBMVFC_MAD_DRIVER_FAILED:
3148                 ibmvfc_free_event(evt);
3149                 return;
3150         default:
3151                 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
3152                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3153                 ibmvfc_free_event(evt);
3154                 return;
3155         }
3156
3157         vhost->client_migrated = 0;
3158
3159         if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
3160                 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
3161                         rsp->flags);
3162                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3163                 wake_up(&vhost->work_wait_q);
3164                 return;
3165         }
3166
3167         if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
3168                 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
3169                         rsp->max_cmds);
3170                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3171                 wake_up(&vhost->work_wait_q);
3172                 return;
3173         }
3174
3175         npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
3176         dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
3177                  rsp->partition_name, rsp->device_name, rsp->port_loc_code,
3178                  rsp->drc_name, npiv_max_sectors);
3179
3180         fc_host_fabric_name(vhost->host) = rsp->node_name;
3181         fc_host_node_name(vhost->host) = rsp->node_name;
3182         fc_host_port_name(vhost->host) = rsp->port_name;
3183         fc_host_port_id(vhost->host) = rsp->scsi_id;
3184         fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
3185         fc_host_supported_classes(vhost->host) = 0;
3186         if (rsp->service_parms.class1_parms[0] & 0x80000000)
3187                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
3188         if (rsp->service_parms.class2_parms[0] & 0x80000000)
3189                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
3190         if (rsp->service_parms.class3_parms[0] & 0x80000000)
3191                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
3192         fc_host_maxframe_size(vhost->host) =
3193                 rsp->service_parms.common.bb_rcv_sz & 0x0fff;
3194
3195         vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
3196         vhost->host->max_sectors = npiv_max_sectors;
3197         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3198         wake_up(&vhost->work_wait_q);
3199 }
3200
3201 /**
3202  * ibmvfc_npiv_login - Sends NPIV login
3203  * @vhost:      ibmvfc host struct
3204  *
3205  **/
3206 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
3207 {
3208         struct ibmvfc_npiv_login_mad *mad;
3209         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3210
3211         ibmvfc_gather_partition_info(vhost);
3212         ibmvfc_set_login_info(vhost);
3213         ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
3214
3215         memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
3216         mad = &evt->iu.npiv_login;
3217         memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
3218         mad->common.version = 1;
3219         mad->common.opcode = IBMVFC_NPIV_LOGIN;
3220         mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
3221         mad->buffer.va = vhost->login_buf_dma;
3222         mad->buffer.len = sizeof(*vhost->login_buf);
3223
3224         memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
3225         vhost->async_crq.cur = 0;
3226         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3227
3228         if (!ibmvfc_send_event(evt, vhost, default_timeout))
3229                 ibmvfc_dbg(vhost, "Sent NPIV login\n");
3230         else
3231                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3232 };
3233
3234 /**
3235  * ibmvfc_dev_init_to_do - Is there target initialization work to do?
3236  * @vhost:              ibmvfc host struct
3237  *
3238  * Returns:
3239  *      1 if work to do / 0 if not
3240  **/
3241 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
3242 {
3243         struct ibmvfc_target *tgt;
3244
3245         list_for_each_entry(tgt, &vhost->targets, queue) {
3246                 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
3247                     tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3248                         return 1;
3249         }
3250
3251         return 0;
3252 }
3253
3254 /**
3255  * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
3256  * @vhost:              ibmvfc host struct
3257  *
3258  * Returns:
3259  *      1 if work to do / 0 if not
3260  **/
3261 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3262 {
3263         struct ibmvfc_target *tgt;
3264
3265         if (kthread_should_stop())
3266                 return 1;
3267         switch (vhost->action) {
3268         case IBMVFC_HOST_ACTION_NONE:
3269         case IBMVFC_HOST_ACTION_INIT_WAIT:
3270                 return 0;
3271         case IBMVFC_HOST_ACTION_TGT_INIT:
3272         case IBMVFC_HOST_ACTION_QUERY_TGTS:
3273                 if (vhost->discovery_threads == disc_threads)
3274                         return 0;
3275                 list_for_each_entry(tgt, &vhost->targets, queue)
3276                         if (tgt->action == IBMVFC_TGT_ACTION_INIT)
3277                                 return 1;
3278                 list_for_each_entry(tgt, &vhost->targets, queue)
3279                         if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3280                                 return 0;
3281                 return 1;
3282         case IBMVFC_HOST_ACTION_INIT:
3283         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3284         case IBMVFC_HOST_ACTION_TGT_ADD:
3285         case IBMVFC_HOST_ACTION_TGT_DEL:
3286         case IBMVFC_HOST_ACTION_QUERY:
3287         default:
3288                 break;
3289         };
3290
3291         return 1;
3292 }
3293
3294 /**
3295  * ibmvfc_work_to_do - Is there task level work to do?
3296  * @vhost:              ibmvfc host struct
3297  *
3298  * Returns:
3299  *      1 if work to do / 0 if not
3300  **/
3301 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3302 {
3303         unsigned long flags;
3304         int rc;
3305
3306         spin_lock_irqsave(vhost->host->host_lock, flags);
3307         rc = __ibmvfc_work_to_do(vhost);
3308         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3309         return rc;
3310 }
3311
3312 /**
3313  * ibmvfc_log_ae - Log async events if necessary
3314  * @vhost:              ibmvfc host struct
3315  * @events:             events to log
3316  *
3317  **/
3318 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
3319 {
3320         if (events & IBMVFC_AE_RSCN)
3321                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
3322         if ((events & IBMVFC_AE_LINKDOWN) &&
3323             vhost->state >= IBMVFC_HALTED)
3324                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
3325         if ((events & IBMVFC_AE_LINKUP) &&
3326             vhost->state == IBMVFC_INITIALIZING)
3327                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
3328 }
3329
3330 /**
3331  * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
3332  * @tgt:                ibmvfc target struct
3333  *
3334  **/
3335 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
3336 {
3337         struct ibmvfc_host *vhost = tgt->vhost;
3338         struct fc_rport *rport;
3339         unsigned long flags;
3340
3341         tgt_dbg(tgt, "Adding rport\n");
3342         rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
3343         spin_lock_irqsave(vhost->host->host_lock, flags);
3344         tgt->rport = rport;
3345         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3346         if (rport) {
3347                 tgt_dbg(tgt, "rport add succeeded\n");
3348                 rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
3349                 rport->supported_classes = 0;
3350                 if (tgt->service_parms.class1_parms[0] & 0x80000000)
3351                         rport->supported_classes |= FC_COS_CLASS1;
3352                 if (tgt->service_parms.class2_parms[0] & 0x80000000)
3353                         rport->supported_classes |= FC_COS_CLASS2;
3354                 if (tgt->service_parms.class3_parms[0] & 0x80000000)
3355                         rport->supported_classes |= FC_COS_CLASS3;
3356         } else
3357                 tgt_dbg(tgt, "rport add failed\n");
3358         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3359 }
3360
3361 /**
3362  * ibmvfc_do_work - Do task level work
3363  * @vhost:              ibmvfc host struct
3364  *
3365  **/
3366 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
3367 {
3368         struct ibmvfc_target *tgt;
3369         unsigned long flags;
3370         struct fc_rport *rport;
3371
3372         ibmvfc_log_ae(vhost, vhost->events_to_log);
3373         spin_lock_irqsave(vhost->host->host_lock, flags);
3374         vhost->events_to_log = 0;
3375         switch (vhost->action) {
3376         case IBMVFC_HOST_ACTION_NONE:
3377         case IBMVFC_HOST_ACTION_INIT_WAIT:
3378                 break;
3379         case IBMVFC_HOST_ACTION_INIT:
3380                 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
3381                 vhost->job_step(vhost);
3382                 break;
3383         case IBMVFC_HOST_ACTION_QUERY:
3384                 list_for_each_entry(tgt, &vhost->targets, queue)
3385                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
3386                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
3387                 break;
3388         case IBMVFC_HOST_ACTION_QUERY_TGTS:
3389                 list_for_each_entry(tgt, &vhost->targets, queue) {
3390                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3391                                 tgt->job_step(tgt);
3392                                 break;
3393                         }
3394                 }
3395
3396                 if (!ibmvfc_dev_init_to_do(vhost))
3397                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
3398                 break;
3399         case IBMVFC_HOST_ACTION_TGT_DEL:
3400                 list_for_each_entry(tgt, &vhost->targets, queue) {
3401                         if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3402                                 tgt_dbg(tgt, "Deleting rport\n");
3403                                 rport = tgt->rport;
3404                                 tgt->rport = NULL;
3405                                 list_del(&tgt->queue);
3406                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3407                                 if (rport)
3408                                         fc_remote_port_delete(rport);
3409                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3410                                 return;
3411                         }
3412                 }
3413
3414                 if (vhost->state == IBMVFC_INITIALIZING) {
3415                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
3416                         vhost->job_step = ibmvfc_discover_targets;
3417                 } else {
3418                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3419                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3420                         scsi_unblock_requests(vhost->host);
3421                         wake_up(&vhost->init_wait_q);
3422                         return;
3423                 }
3424                 break;
3425         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3426                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
3427                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3428                 ibmvfc_alloc_targets(vhost);
3429                 spin_lock_irqsave(vhost->host->host_lock, flags);
3430                 break;
3431         case IBMVFC_HOST_ACTION_TGT_INIT:
3432                 list_for_each_entry(tgt, &vhost->targets, queue) {
3433                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3434                                 tgt->job_step(tgt);
3435                                 break;
3436                         }
3437                 }
3438
3439                 if (!ibmvfc_dev_init_to_do(vhost)) {
3440                         ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
3441                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_ADD);
3442                         vhost->init_retries = 0;
3443                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3444                         scsi_unblock_requests(vhost->host);
3445                         return;
3446                 }
3447                 break;
3448         case IBMVFC_HOST_ACTION_TGT_ADD:
3449                 list_for_each_entry(tgt, &vhost->targets, queue) {
3450                         if (tgt->action == IBMVFC_TGT_ACTION_ADD_RPORT) {
3451                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3452                                 ibmvfc_tgt_add_rport(tgt);
3453                                 return;
3454                         } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3455                                 tgt_dbg(tgt, "Deleting rport\n");
3456                                 rport = tgt->rport;
3457                                 tgt->rport = NULL;
3458                                 list_del(&tgt->queue);
3459                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3460                                 if (rport)
3461                                         fc_remote_port_delete(rport);
3462                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3463                                 return;
3464                         }
3465                 }
3466
3467                 if (vhost->reinit) {
3468                         vhost->reinit = 0;
3469                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3470                 } else {
3471                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3472                         wake_up(&vhost->init_wait_q);
3473                 }
3474                 break;
3475         default:
3476                 break;
3477         };
3478
3479         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3480 }
3481
3482 /**
3483  * ibmvfc_work - Do task level work
3484  * @data:               ibmvfc host struct
3485  *
3486  * Returns:
3487  *      zero
3488  **/
3489 static int ibmvfc_work(void *data)
3490 {
3491         struct ibmvfc_host *vhost = data;
3492         int rc;
3493
3494         set_user_nice(current, -20);
3495
3496         while (1) {
3497                 rc = wait_event_interruptible(vhost->work_wait_q,
3498                                               ibmvfc_work_to_do(vhost));
3499
3500                 BUG_ON(rc);
3501
3502                 if (kthread_should_stop())
3503                         break;
3504
3505                 ibmvfc_do_work(vhost);
3506         }
3507
3508         ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
3509         return 0;
3510 }
3511
3512 /**
3513  * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
3514  * @vhost:      ibmvfc host struct
3515  *
3516  * Allocates a page for messages, maps it for dma, and registers
3517  * the crq with the hypervisor.
3518  *
3519  * Return value:
3520  *      zero on success / other on failure
3521  **/
3522 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
3523 {
3524         int rc, retrc = -ENOMEM;
3525         struct device *dev = vhost->dev;
3526         struct vio_dev *vdev = to_vio_dev(dev);
3527         struct ibmvfc_crq_queue *crq = &vhost->crq;
3528
3529         ENTER;
3530         crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
3531
3532         if (!crq->msgs)
3533                 return -ENOMEM;
3534
3535         crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3536         crq->msg_token = dma_map_single(dev, crq->msgs,
3537                                         PAGE_SIZE, DMA_BIDIRECTIONAL);
3538
3539         if (dma_mapping_error(crq->msg_token))
3540                 goto map_failed;
3541
3542         retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3543                                         crq->msg_token, PAGE_SIZE);
3544
3545         if (rc == H_RESOURCE)
3546                 /* maybe kexecing and resource is busy. try a reset */
3547                 retrc = rc = ibmvfc_reset_crq(vhost);
3548
3549         if (rc == H_CLOSED)
3550                 dev_warn(dev, "Partner adapter not ready\n");
3551         else if (rc) {
3552                 dev_warn(dev, "Error %d opening adapter\n", rc);
3553                 goto reg_crq_failed;
3554         }
3555
3556         retrc = 0;
3557
3558         if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
3559                 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
3560                 goto req_irq_failed;
3561         }
3562
3563         if ((rc = vio_enable_interrupts(vdev))) {
3564                 dev_err(dev, "Error %d enabling interrupts\n", rc);
3565                 goto req_irq_failed;
3566         }
3567
3568         crq->cur = 0;
3569         LEAVE;
3570         return retrc;
3571
3572 req_irq_failed:
3573         do {
3574                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3575         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3576 reg_crq_failed:
3577         dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3578 map_failed:
3579         free_page((unsigned long)crq->msgs);
3580         return retrc;
3581 }
3582
3583 /**
3584  * ibmvfc_free_mem - Free memory for vhost
3585  * @vhost:      ibmvfc host struct
3586  *
3587  * Return value:
3588  *      none
3589  **/
3590 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
3591 {
3592         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3593
3594         ENTER;
3595         mempool_destroy(vhost->tgt_pool);
3596         kfree(vhost->trace);
3597         dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
3598                           vhost->disc_buf_dma);
3599         dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
3600                           vhost->login_buf, vhost->login_buf_dma);
3601         dma_pool_destroy(vhost->sg_pool);
3602         dma_unmap_single(vhost->dev, async_q->msg_token,
3603                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3604         free_page((unsigned long)async_q->msgs);
3605         LEAVE;
3606 }
3607
3608 /**
3609  * ibmvfc_alloc_mem - Allocate memory for vhost
3610  * @vhost:      ibmvfc host struct
3611  *
3612  * Return value:
3613  *      0 on success / non-zero on failure
3614  **/
3615 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
3616 {
3617         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3618         struct device *dev = vhost->dev;
3619
3620         ENTER;
3621         async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
3622         if (!async_q->msgs) {
3623                 dev_err(dev, "Couldn't allocate async queue.\n");
3624                 goto nomem;
3625         }
3626
3627         async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
3628         async_q->msg_token = dma_map_single(dev, async_q->msgs,
3629                                             async_q->size * sizeof(*async_q->msgs),
3630                                             DMA_BIDIRECTIONAL);
3631
3632         if (dma_mapping_error(async_q->msg_token)) {
3633                 dev_err(dev, "Failed to map async queue\n");
3634                 goto free_async_crq;
3635         }
3636
3637         vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
3638                                          SG_ALL * sizeof(struct srp_direct_buf),
3639                                          sizeof(struct srp_direct_buf), 0);
3640
3641         if (!vhost->sg_pool) {
3642                 dev_err(dev, "Failed to allocate sg pool\n");
3643                 goto unmap_async_crq;
3644         }
3645
3646         vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
3647                                               &vhost->login_buf_dma, GFP_KERNEL);
3648
3649         if (!vhost->login_buf) {
3650                 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
3651                 goto free_sg_pool;
3652         }
3653
3654         vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
3655         vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
3656                                              &vhost->disc_buf_dma, GFP_KERNEL);
3657
3658         if (!vhost->disc_buf) {
3659                 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
3660                 goto free_login_buffer;
3661         }
3662
3663         vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
3664                                sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
3665
3666         if (!vhost->trace)
3667                 goto free_disc_buffer;
3668
3669         vhost->tgt_pool = mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
3670                                                       sizeof(struct ibmvfc_target));
3671
3672         if (!vhost->tgt_pool) {
3673                 dev_err(dev, "Couldn't allocate target memory pool\n");
3674                 goto free_trace;
3675         }
3676
3677         LEAVE;
3678         return 0;
3679
3680 free_trace:
3681         kfree(vhost->trace);
3682 free_disc_buffer:
3683         dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
3684                           vhost->disc_buf_dma);
3685 free_login_buffer:
3686         dma_free_coherent(dev, sizeof(*vhost->login_buf),
3687                           vhost->login_buf, vhost->login_buf_dma);
3688 free_sg_pool:
3689         dma_pool_destroy(vhost->sg_pool);
3690 unmap_async_crq:
3691         dma_unmap_single(dev, async_q->msg_token,
3692                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3693 free_async_crq:
3694         free_page((unsigned long)async_q->msgs);
3695 nomem:
3696         LEAVE;
3697         return -ENOMEM;
3698 }
3699
3700 /**
3701  * ibmvfc_probe - Adapter hot plug add entry point
3702  * @vdev:       vio device struct
3703  * @id: vio device id struct
3704  *
3705  * Return value:
3706  *      0 on success / non-zero on failure
3707  **/
3708 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
3709 {
3710         struct ibmvfc_host *vhost;
3711         struct Scsi_Host *shost;
3712         struct device *dev = &vdev->dev;
3713         int rc = -ENOMEM;
3714
3715         ENTER;
3716         shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
3717         if (!shost) {
3718                 dev_err(dev, "Couldn't allocate host data\n");
3719                 goto out;
3720         }
3721
3722         shost->transportt = ibmvfc_transport_template;
3723         shost->can_queue = max_requests;
3724         shost->max_lun = max_lun;
3725         shost->max_id = max_targets;
3726         shost->max_sectors = IBMVFC_MAX_SECTORS;
3727         shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
3728         shost->unique_id = shost->host_no;
3729
3730         vhost = shost_priv(shost);
3731         INIT_LIST_HEAD(&vhost->sent);
3732         INIT_LIST_HEAD(&vhost->free);
3733         INIT_LIST_HEAD(&vhost->targets);
3734         sprintf(vhost->name, IBMVFC_NAME);
3735         vhost->host = shost;
3736         vhost->dev = dev;
3737         vhost->partition_number = -1;
3738         vhost->log_level = log_level;
3739         strcpy(vhost->partition_name, "UNKNOWN");
3740         init_waitqueue_head(&vhost->work_wait_q);
3741         init_waitqueue_head(&vhost->init_wait_q);
3742
3743         if ((rc = ibmvfc_alloc_mem(vhost)))
3744                 goto free_scsi_host;
3745
3746         vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
3747                                          shost->host_no);
3748
3749         if (IS_ERR(vhost->work_thread)) {
3750                 dev_err(dev, "Couldn't create kernel thread: %ld\n",
3751                         PTR_ERR(vhost->work_thread));
3752                 goto free_host_mem;
3753         }
3754
3755         if ((rc = ibmvfc_init_crq(vhost))) {
3756                 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3757                 goto kill_kthread;
3758         }
3759
3760         if ((rc = ibmvfc_init_event_pool(vhost))) {
3761                 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
3762                 goto release_crq;
3763         }
3764
3765         if ((rc = scsi_add_host(shost, dev)))
3766                 goto release_event_pool;
3767
3768         if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
3769                                            &ibmvfc_trace_attr))) {
3770                 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
3771                 goto remove_shost;
3772         }
3773
3774         dev_set_drvdata(dev, vhost);
3775         spin_lock(&ibmvfc_driver_lock);
3776         list_add_tail(&vhost->queue, &ibmvfc_head);
3777         spin_unlock(&ibmvfc_driver_lock);
3778
3779         ibmvfc_send_crq_init(vhost);
3780         scsi_scan_host(shost);
3781         return 0;
3782
3783 remove_shost:
3784         scsi_remove_host(shost);
3785 release_event_pool:
3786         ibmvfc_free_event_pool(vhost);
3787 release_crq:
3788         ibmvfc_release_crq_queue(vhost);
3789 kill_kthread:
3790         kthread_stop(vhost->work_thread);
3791 free_host_mem:
3792         ibmvfc_free_mem(vhost);
3793 free_scsi_host:
3794         scsi_host_put(shost);
3795 out:
3796         LEAVE;
3797         return rc;
3798 }
3799
3800 /**
3801  * ibmvfc_remove - Adapter hot plug remove entry point
3802  * @vdev:       vio device struct
3803  *
3804  * Return value:
3805  *      0
3806  **/
3807 static int ibmvfc_remove(struct vio_dev *vdev)
3808 {
3809         struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
3810         unsigned long flags;
3811
3812         ENTER;
3813         ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
3814         kthread_stop(vhost->work_thread);
3815         fc_remove_host(vhost->host);
3816         scsi_remove_host(vhost->host);
3817         ibmvfc_release_crq_queue(vhost);
3818
3819         spin_lock_irqsave(vhost->host->host_lock, flags);
3820         ibmvfc_purge_requests(vhost, DID_ERROR);
3821         ibmvfc_free_event_pool(vhost);
3822         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3823
3824         ibmvfc_free_mem(vhost);
3825         spin_lock(&ibmvfc_driver_lock);
3826         list_del(&vhost->queue);
3827         spin_unlock(&ibmvfc_driver_lock);
3828         scsi_host_put(vhost->host);
3829         LEAVE;
3830         return 0;
3831 }
3832
3833 /**
3834  * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
3835  * @vdev:       vio device struct
3836  *
3837  * Return value:
3838  *      Number of bytes the driver will need to DMA map at the same time in
3839  *      order to perform well.
3840  */
3841 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
3842 {
3843         unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
3844         return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
3845 }
3846
3847 static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
3848         {"fcp", "IBM,vfc-client"},
3849         { "", "" }
3850 };
3851 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
3852
3853 static struct vio_driver ibmvfc_driver = {
3854         .id_table = ibmvfc_device_table,
3855         .probe = ibmvfc_probe,
3856         .remove = ibmvfc_remove,
3857         .get_desired_dma = ibmvfc_get_desired_dma,
3858         .driver = {
3859                 .name = IBMVFC_NAME,
3860                 .owner = THIS_MODULE,
3861         }
3862 };
3863
3864 static struct fc_function_template ibmvfc_transport_functions = {
3865         .show_host_fabric_name = 1,
3866         .show_host_node_name = 1,
3867         .show_host_port_name = 1,
3868         .show_host_supported_classes = 1,
3869         .show_host_port_type = 1,
3870         .show_host_port_id = 1,
3871
3872         .get_host_port_state = ibmvfc_get_host_port_state,
3873         .show_host_port_state = 1,
3874
3875         .get_host_speed = ibmvfc_get_host_speed,
3876         .show_host_speed = 1,
3877
3878         .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
3879         .terminate_rport_io = ibmvfc_terminate_rport_io,
3880
3881         .show_rport_maxframe_size = 1,
3882         .show_rport_supported_classes = 1,
3883
3884         .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
3885         .show_rport_dev_loss_tmo = 1,
3886
3887         .get_starget_node_name = ibmvfc_get_starget_node_name,
3888         .show_starget_node_name = 1,
3889
3890         .get_starget_port_name = ibmvfc_get_starget_port_name,
3891         .show_starget_port_name = 1,
3892
3893         .get_starget_port_id = ibmvfc_get_starget_port_id,
3894         .show_starget_port_id = 1,
3895 };
3896
3897 /**
3898  * ibmvfc_module_init - Initialize the ibmvfc module
3899  *
3900  * Return value:
3901  *      0 on success / other on failure
3902  **/
3903 static int __init ibmvfc_module_init(void)
3904 {
3905         int rc;
3906
3907         if (!firmware_has_feature(FW_FEATURE_VIO))
3908                 return -ENODEV;
3909
3910         printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
3911                IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
3912
3913         ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
3914         if (!ibmvfc_transport_template)
3915                 return -ENOMEM;
3916
3917         rc = vio_register_driver(&ibmvfc_driver);
3918         if (rc)
3919                 fc_release_transport(ibmvfc_transport_template);
3920         return rc;
3921 }
3922
3923 /**
3924  * ibmvfc_module_exit - Teardown the ibmvfc module
3925  *
3926  * Return value:
3927  *      nothing
3928  **/
3929 static void __exit ibmvfc_module_exit(void)
3930 {
3931         vio_unregister_driver(&ibmvfc_driver);
3932         fc_release_transport(ibmvfc_transport_template);
3933 }
3934
3935 module_init(ibmvfc_module_init);
3936 module_exit(ibmvfc_module_exit);