Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/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/slab.h>
 32 #include <linux/of.h>
 33 #include <linux/pm.h>
 34 #include <linux/stringify.h>
 35 #include <linux/bsg-lib.h>
 36 #include <asm/firmware.h>
 37 #include <asm/irq.h>
 38 #include <asm/vio.h>
 39 #include <scsi/scsi.h>
 40 #include <scsi/scsi_cmnd.h>
 41 #include <scsi/scsi_host.h>
 42 #include <scsi/scsi_device.h>
 43 #include <scsi/scsi_tcq.h>
 44 #include <scsi/scsi_transport_fc.h>
 45 #include <scsi/scsi_bsg_fc.h>
 46 #include "ibmvfc.h"
 47 
 48 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
 49 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
 50 static u64 max_lun = IBMVFC_MAX_LUN;
 51 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
 52 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
 53 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
 54 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
 55 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
 56 static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
 57 static LIST_HEAD(ibmvfc_head);
 58 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
 59 static struct scsi_transport_template *ibmvfc_transport_template;
 60 
 61 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
 62 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
 63 MODULE_LICENSE("GPL");
 64 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
 65 
 66 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
 67 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
 68                  "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
 69 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
 70 MODULE_PARM_DESC(default_timeout,
 71                  "Default timeout in seconds for initialization and EH commands. "
 72                  "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
 73 module_param_named(max_requests, max_requests, uint, S_IRUGO);
 74 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
 75                  "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
 76 module_param_named(max_lun, max_lun, ullong, S_IRUGO);
 77 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
 78                  "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
 79 module_param_named(max_targets, max_targets, uint, S_IRUGO);
 80 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
 81                  "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
 82 module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
 83 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
 84                  "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
 85 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
 86 MODULE_PARM_DESC(debug, "Enable driver debug information. "
 87                  "[Default=" __stringify(IBMVFC_DEBUG) "]");
 88 module_param_named(log_level, log_level, uint, 0);
 89 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
 90                  "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
 91 module_param_named(cls3_error, cls3_error, uint, 0);
 92 MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
 93                  "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
 94 
 95 static const struct {
 96         u16 status;
 97         u16 error;
 98         u8 result;
 99         u8 retry;
100         int log;
101         char *name;
102 } cmd_status [] = {
103         { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
104         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
105         { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
106         { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
107         { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
108         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
109         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
110         { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
111         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
112         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
113         { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
114         { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
115         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
116         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
117 
118         { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
119         { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
120         { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
121         { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
122         { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
123         { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
124         { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
125         { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
126         { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
127         { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
128 
129         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
130         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
131         { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
132         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
133         { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
134         { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
135         { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
136         { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
137         { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
138         { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
139         { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
140 
141         { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
142 };
143 
144 static void ibmvfc_npiv_login(struct ibmvfc_host *);
145 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
146 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
147 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
148 static void ibmvfc_npiv_logout(struct ibmvfc_host *);
149 
150 static const char *unknown_error = "unknown error";
151 
152 #ifdef CONFIG_SCSI_IBMVFC_TRACE
153 /**
154  * ibmvfc_trc_start - Log a start trace entry
155  * @evt:                ibmvfc event struct
156  *
157  **/
158 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
159 {
160         struct ibmvfc_host *vhost = evt->vhost;
161         struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
162         struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
163         struct ibmvfc_trace_entry *entry;
164 
165         entry = &vhost->trace[vhost->trace_index++];
166         entry->evt = evt;
167         entry->time = jiffies;
168         entry->fmt = evt->crq.format;
169         entry->type = IBMVFC_TRC_START;
170 
171         switch (entry->fmt) {
172         case IBMVFC_CMD_FORMAT:
173                 entry->op_code = vfc_cmd->iu.cdb[0];
174                 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
175                 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
176                 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
177                 entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
178                 break;
179         case IBMVFC_MAD_FORMAT:
180                 entry->op_code = be32_to_cpu(mad->opcode);
181                 break;
182         default:
183                 break;
184         };
185 }
186 
187 /**
188  * ibmvfc_trc_end - Log an end trace entry
189  * @evt:                ibmvfc event struct
190  *
191  **/
192 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
193 {
194         struct ibmvfc_host *vhost = evt->vhost;
195         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
196         struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
197         struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
198 
199         entry->evt = evt;
200         entry->time = jiffies;
201         entry->fmt = evt->crq.format;
202         entry->type = IBMVFC_TRC_END;
203 
204         switch (entry->fmt) {
205         case IBMVFC_CMD_FORMAT:
206                 entry->op_code = vfc_cmd->iu.cdb[0];
207                 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
208                 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
209                 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
210                 entry->u.end.status = be16_to_cpu(vfc_cmd->status);
211                 entry->u.end.error = be16_to_cpu(vfc_cmd->error);
212                 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
213                 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
214                 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
215                 break;
216         case IBMVFC_MAD_FORMAT:
217                 entry->op_code = be32_to_cpu(mad->opcode);
218                 entry->u.end.status = be16_to_cpu(mad->status);
219                 break;
220         default:
221                 break;
222 
223         };
224 }
225 
226 #else
227 #define ibmvfc_trc_start(evt) do { } while (0)
228 #define ibmvfc_trc_end(evt) do { } while (0)
229 #endif
230 
231 /**
232  * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
233  * @status:             status / error class
234  * @error:              error
235  *
236  * Return value:
237  *      index into cmd_status / -EINVAL on failure
238  **/
239 static int ibmvfc_get_err_index(u16 status, u16 error)
240 {
241         int i;
242 
243         for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
244                 if ((cmd_status[i].status & status) == cmd_status[i].status &&
245                     cmd_status[i].error == error)
246                         return i;
247 
248         return -EINVAL;
249 }
250 
251 /**
252  * ibmvfc_get_cmd_error - Find the error description for the fcp response
253  * @status:             status / error class
254  * @error:              error
255  *
256  * Return value:
257  *      error description string
258  **/
259 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
260 {
261         int rc = ibmvfc_get_err_index(status, error);
262         if (rc >= 0)
263                 return cmd_status[rc].name;
264         return unknown_error;
265 }
266 
267 /**
268  * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
269  * @vfc_cmd:    ibmvfc command struct
270  *
271  * Return value:
272  *      SCSI result value to return for completed command
273  **/
274 static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
275 {
276         int err;
277         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
278         int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
279 
280         if ((rsp->flags & FCP_RSP_LEN_VALID) &&
281             ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
282              rsp->data.info.rsp_code))
283                 return DID_ERROR << 16;
284 
285         err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
286         if (err >= 0)
287                 return rsp->scsi_status | (cmd_status[err].result << 16);
288         return rsp->scsi_status | (DID_ERROR << 16);
289 }
290 
291 /**
292  * ibmvfc_retry_cmd - Determine if error status is retryable
293  * @status:             status / error class
294  * @error:              error
295  *
296  * Return value:
297  *      1 if error should be retried / 0 if it should not
298  **/
299 static int ibmvfc_retry_cmd(u16 status, u16 error)
300 {
301         int rc = ibmvfc_get_err_index(status, error);
302 
303         if (rc >= 0)
304                 return cmd_status[rc].retry;
305         return 1;
306 }
307 
308 static const char *unknown_fc_explain = "unknown fc explain";
309 
310 static const struct {
311         u16 fc_explain;
312         char *name;
313 } ls_explain [] = {
314         { 0x00, "no additional explanation" },
315         { 0x01, "service parameter error - options" },
316         { 0x03, "service parameter error - initiator control" },
317         { 0x05, "service parameter error - recipient control" },
318         { 0x07, "service parameter error - received data field size" },
319         { 0x09, "service parameter error - concurrent seq" },
320         { 0x0B, "service parameter error - credit" },
321         { 0x0D, "invalid N_Port/F_Port_Name" },
322         { 0x0E, "invalid node/Fabric Name" },
323         { 0x0F, "invalid common service parameters" },
324         { 0x11, "invalid association header" },
325         { 0x13, "association header required" },
326         { 0x15, "invalid originator S_ID" },
327         { 0x17, "invalid OX_ID-RX-ID combination" },
328         { 0x19, "command (request) already in progress" },
329         { 0x1E, "N_Port Login requested" },
330         { 0x1F, "Invalid N_Port_ID" },
331 };
332 
333 static const struct {
334         u16 fc_explain;
335         char *name;
336 } gs_explain [] = {
337         { 0x00, "no additional explanation" },
338         { 0x01, "port identifier not registered" },
339         { 0x02, "port name not registered" },
340         { 0x03, "node name not registered" },
341         { 0x04, "class of service not registered" },
342         { 0x06, "initial process associator not registered" },
343         { 0x07, "FC-4 TYPEs not registered" },
344         { 0x08, "symbolic port name not registered" },
345         { 0x09, "symbolic node name not registered" },
346         { 0x0A, "port type not registered" },
347         { 0xF0, "authorization exception" },
348         { 0xF1, "authentication exception" },
349         { 0xF2, "data base full" },
350         { 0xF3, "data base empty" },
351         { 0xF4, "processing request" },
352         { 0xF5, "unable to verify connection" },
353         { 0xF6, "devices not in a common zone" },
354 };
355 
356 /**
357  * ibmvfc_get_ls_explain - Return the FC Explain description text
358  * @status:     FC Explain status
359  *
360  * Returns:
361  *      error string
362  **/
363 static const char *ibmvfc_get_ls_explain(u16 status)
364 {
365         int i;
366 
367         for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
368                 if (ls_explain[i].fc_explain == status)
369                         return ls_explain[i].name;
370 
371         return unknown_fc_explain;
372 }
373 
374 /**
375  * ibmvfc_get_gs_explain - Return the FC Explain description text
376  * @status:     FC Explain status
377  *
378  * Returns:
379  *      error string
380  **/
381 static const char *ibmvfc_get_gs_explain(u16 status)
382 {
383         int i;
384 
385         for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
386                 if (gs_explain[i].fc_explain == status)
387                         return gs_explain[i].name;
388 
389         return unknown_fc_explain;
390 }
391 
392 static const struct {
393         enum ibmvfc_fc_type fc_type;
394         char *name;
395 } fc_type [] = {
396         { IBMVFC_FABRIC_REJECT, "fabric reject" },
397         { IBMVFC_PORT_REJECT, "port reject" },
398         { IBMVFC_LS_REJECT, "ELS reject" },
399         { IBMVFC_FABRIC_BUSY, "fabric busy" },
400         { IBMVFC_PORT_BUSY, "port busy" },
401         { IBMVFC_BASIC_REJECT, "basic reject" },
402 };
403 
404 static const char *unknown_fc_type = "unknown fc type";
405 
406 /**
407  * ibmvfc_get_fc_type - Return the FC Type description text
408  * @status:     FC Type error status
409  *
410  * Returns:
411  *      error string
412  **/
413 static const char *ibmvfc_get_fc_type(u16 status)
414 {
415         int i;
416 
417         for (i = 0; i < ARRAY_SIZE(fc_type); i++)
418                 if (fc_type[i].fc_type == status)
419                         return fc_type[i].name;
420 
421         return unknown_fc_type;
422 }
423 
424 /**
425  * ibmvfc_set_tgt_action - Set the next init action for the target
426  * @tgt:                ibmvfc target struct
427  * @action:             action to perform
428  *
429  **/
430 static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
431                                   enum ibmvfc_target_action action)
432 {
433         switch (tgt->action) {
434         case IBMVFC_TGT_ACTION_DEL_RPORT:
435                 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT)
436                         tgt->action = action;
437         case IBMVFC_TGT_ACTION_DELETED_RPORT:
438                 break;
439         default:
440                 if (action == IBMVFC_TGT_ACTION_DEL_RPORT)
441                         tgt->add_rport = 0;
442                 tgt->action = action;
443                 break;
444         }
445 }
446 
447 /**
448  * ibmvfc_set_host_state - Set the state for the host
449  * @vhost:              ibmvfc host struct
450  * @state:              state to set host to
451  *
452  * Returns:
453  *      0 if state changed / non-zero if not changed
454  **/
455 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
456                                   enum ibmvfc_host_state state)
457 {
458         int rc = 0;
459 
460         switch (vhost->state) {
461         case IBMVFC_HOST_OFFLINE:
462                 rc = -EINVAL;
463                 break;
464         default:
465                 vhost->state = state;
466                 break;
467         };
468 
469         return rc;
470 }
471 
472 /**
473  * ibmvfc_set_host_action - Set the next init action for the host
474  * @vhost:              ibmvfc host struct
475  * @action:             action to perform
476  *
477  **/
478 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
479                                    enum ibmvfc_host_action action)
480 {
481         switch (action) {
482         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
483                 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
484                         vhost->action = action;
485                 break;
486         case IBMVFC_HOST_ACTION_LOGO_WAIT:
487                 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
488                         vhost->action = action;
489                 break;
490         case IBMVFC_HOST_ACTION_INIT_WAIT:
491                 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
492                         vhost->action = action;
493                 break;
494         case IBMVFC_HOST_ACTION_QUERY:
495                 switch (vhost->action) {
496                 case IBMVFC_HOST_ACTION_INIT_WAIT:
497                 case IBMVFC_HOST_ACTION_NONE:
498                 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
499                         vhost->action = action;
500                         break;
501                 default:
502                         break;
503                 };
504                 break;
505         case IBMVFC_HOST_ACTION_TGT_INIT:
506                 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
507                         vhost->action = action;
508                 break;
509         case IBMVFC_HOST_ACTION_INIT:
510         case IBMVFC_HOST_ACTION_TGT_DEL:
511                 switch (vhost->action) {
512                 case IBMVFC_HOST_ACTION_RESET:
513                 case IBMVFC_HOST_ACTION_REENABLE:
514                         break;
515                 default:
516                         vhost->action = action;
517                         break;
518                 };
519                 break;
520         case IBMVFC_HOST_ACTION_LOGO:
521         case IBMVFC_HOST_ACTION_QUERY_TGTS:
522         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
523         case IBMVFC_HOST_ACTION_NONE:
524         case IBMVFC_HOST_ACTION_RESET:
525         case IBMVFC_HOST_ACTION_REENABLE:
526         default:
527                 vhost->action = action;
528                 break;
529         };
530 }
531 
532 /**
533  * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
534  * @vhost:              ibmvfc host struct
535  *
536  * Return value:
537  *      nothing
538  **/
539 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
540 {
541         if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
542                 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
543                         scsi_block_requests(vhost->host);
544                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
545                 }
546         } else
547                 vhost->reinit = 1;
548 
549         wake_up(&vhost->work_wait_q);
550 }
551 
552 /**
553  * ibmvfc_link_down - Handle a link down event from the adapter
554  * @vhost:      ibmvfc host struct
555  * @state:      ibmvfc host state to enter
556  *
557  **/
558 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
559                              enum ibmvfc_host_state state)
560 {
561         struct ibmvfc_target *tgt;
562 
563         ENTER;
564         scsi_block_requests(vhost->host);
565         list_for_each_entry(tgt, &vhost->targets, queue)
566                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
567         ibmvfc_set_host_state(vhost, state);
568         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
569         vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
570         wake_up(&vhost->work_wait_q);
571         LEAVE;
572 }
573 
574 /**
575  * ibmvfc_init_host - Start host initialization
576  * @vhost:              ibmvfc host struct
577  *
578  * Return value:
579  *      nothing
580  **/
581 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
582 {
583         struct ibmvfc_target *tgt;
584 
585         if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
586                 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
587                         dev_err(vhost->dev,
588                                 "Host initialization retries exceeded. Taking adapter offline\n");
589                         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
590                         return;
591                 }
592         }
593 
594         if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
595                 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
596                 vhost->async_crq.cur = 0;
597 
598                 list_for_each_entry(tgt, &vhost->targets, queue)
599                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
600                 scsi_block_requests(vhost->host);
601                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
602                 vhost->job_step = ibmvfc_npiv_login;
603                 wake_up(&vhost->work_wait_q);
604         }
605 }
606 
607 /**
608  * ibmvfc_send_crq - Send a CRQ
609  * @vhost:      ibmvfc host struct
610  * @word1:      the first 64 bits of the data
611  * @word2:      the second 64 bits of the data
612  *
613  * Return value:
614  *      0 on success / other on failure
615  **/
616 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
617 {
618         struct vio_dev *vdev = to_vio_dev(vhost->dev);
619         return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
620 }
621 
622 /**
623  * ibmvfc_send_crq_init - Send a CRQ init message
624  * @vhost:      ibmvfc host struct
625  *
626  * Return value:
627  *      0 on success / other on failure
628  **/
629 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
630 {
631         ibmvfc_dbg(vhost, "Sending CRQ init\n");
632         return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
633 }
634 
635 /**
636  * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
637  * @vhost:      ibmvfc host struct
638  *
639  * Return value:
640  *      0 on success / other on failure
641  **/
642 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
643 {
644         ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
645         return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
646 }
647 
648 /**
649  * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
650  * @vhost:      ibmvfc host struct
651  *
652  * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
653  * the crq with the hypervisor.
654  **/
655 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
656 {
657         long rc = 0;
658         struct vio_dev *vdev = to_vio_dev(vhost->dev);
659         struct ibmvfc_crq_queue *crq = &vhost->crq;
660 
661         ibmvfc_dbg(vhost, "Releasing CRQ\n");
662         free_irq(vdev->irq, vhost);
663         tasklet_kill(&vhost->tasklet);
664         do {
665                 if (rc)
666                         msleep(100);
667                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
668         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
669 
670         vhost->state = IBMVFC_NO_CRQ;
671         vhost->logged_in = 0;
672         dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
673         free_page((unsigned long)crq->msgs);
674 }
675 
676 /**
677  * ibmvfc_reenable_crq_queue - reenables the CRQ
678  * @vhost:      ibmvfc host struct
679  *
680  * Return value:
681  *      0 on success / other on failure
682  **/
683 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
684 {
685         int rc = 0;
686         struct vio_dev *vdev = to_vio_dev(vhost->dev);
687 
688         /* Re-enable the CRQ */
689         do {
690                 if (rc)
691                         msleep(100);
692                 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
693         } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
694 
695         if (rc)
696                 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
697 
698         return rc;
699 }
700 
701 /**
702  * ibmvfc_reset_crq - resets a crq after a failure
703  * @vhost:      ibmvfc host struct
704  *
705  * Return value:
706  *      0 on success / other on failure
707  **/
708 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
709 {
710         int rc = 0;
711         unsigned long flags;
712         struct vio_dev *vdev = to_vio_dev(vhost->dev);
713         struct ibmvfc_crq_queue *crq = &vhost->crq;
714 
715         /* Close the CRQ */
716         do {
717                 if (rc)
718                         msleep(100);
719                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
720         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
721 
722         spin_lock_irqsave(vhost->host->host_lock, flags);
723         vhost->state = IBMVFC_NO_CRQ;
724         vhost->logged_in = 0;
725 
726         /* Clean out the queue */
727         memset(crq->msgs, 0, PAGE_SIZE);
728         crq->cur = 0;
729 
730         /* And re-open it again */
731         rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
732                                 crq->msg_token, PAGE_SIZE);
733 
734         if (rc == H_CLOSED)
735                 /* Adapter is good, but other end is not ready */
736                 dev_warn(vhost->dev, "Partner adapter not ready\n");
737         else if (rc != 0)
738                 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
739         spin_unlock_irqrestore(vhost->host->host_lock, flags);
740 
741         return rc;
742 }
743 
744 /**
745  * ibmvfc_valid_event - Determines if event is valid.
746  * @pool:       event_pool that contains the event
747  * @evt:        ibmvfc event to be checked for validity
748  *
749  * Return value:
750  *      1 if event is valid / 0 if event is not valid
751  **/
752 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
753                               struct ibmvfc_event *evt)
754 {
755         int index = evt - pool->events;
756         if (index < 0 || index >= pool->size)   /* outside of bounds */
757                 return 0;
758         if (evt != pool->events + index)        /* unaligned */
759                 return 0;
760         return 1;
761 }
762 
763 /**
764  * ibmvfc_free_event - Free the specified event
765  * @evt:        ibmvfc_event to be freed
766  *
767  **/
768 static void ibmvfc_free_event(struct ibmvfc_event *evt)
769 {
770         struct ibmvfc_host *vhost = evt->vhost;
771         struct ibmvfc_event_pool *pool = &vhost->pool;
772 
773         BUG_ON(!ibmvfc_valid_event(pool, evt));
774         BUG_ON(atomic_inc_return(&evt->free) != 1);
775         list_add_tail(&evt->queue, &vhost->free);
776 }
777 
778 /**
779  * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
780  * @evt:        ibmvfc event struct
781  *
782  * This function does not setup any error status, that must be done
783  * before this function gets called.
784  **/
785 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
786 {
787         struct scsi_cmnd *cmnd = evt->cmnd;
788 
789         if (cmnd) {
790                 scsi_dma_unmap(cmnd);
791                 cmnd->scsi_done(cmnd);
792         }
793 
794         if (evt->eh_comp)
795                 complete(evt->eh_comp);
796 
797         ibmvfc_free_event(evt);
798 }
799 
800 /**
801  * ibmvfc_fail_request - Fail request with specified error code
802  * @evt:                ibmvfc event struct
803  * @error_code: error code to fail request with
804  *
805  * Return value:
806  *      none
807  **/
808 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
809 {
810         if (evt->cmnd) {
811                 evt->cmnd->result = (error_code << 16);
812                 evt->done = ibmvfc_scsi_eh_done;
813         } else
814                 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
815 
816         list_del(&evt->queue);
817         del_timer(&evt->timer);
818         ibmvfc_trc_end(evt);
819         evt->done(evt);
820 }
821 
822 /**
823  * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
824  * @vhost:              ibmvfc host struct
825  * @error_code: error code to fail requests with
826  *
827  * Return value:
828  *      none
829  **/
830 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
831 {
832         struct ibmvfc_event *evt, *pos;
833 
834         ibmvfc_dbg(vhost, "Purging all requests\n");
835         list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
836                 ibmvfc_fail_request(evt, error_code);
837 }
838 
839 /**
840  * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
841  * @vhost:      struct ibmvfc host to reset
842  **/
843 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
844 {
845         ibmvfc_purge_requests(vhost, DID_ERROR);
846         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
847         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
848 }
849 
850 /**
851  * __ibmvfc_reset_host - Reset the connection to the server (no locking)
852  * @vhost:      struct ibmvfc host to reset
853  **/
854 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
855 {
856         if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
857             !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
858                 scsi_block_requests(vhost->host);
859                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
860                 vhost->job_step = ibmvfc_npiv_logout;
861                 wake_up(&vhost->work_wait_q);
862         } else
863                 ibmvfc_hard_reset_host(vhost);
864 }
865 
866 /**
867  * ibmvfc_reset_host - Reset the connection to the server
868  * @vhost:      ibmvfc host struct
869  **/
870 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
871 {
872         unsigned long flags;
873 
874         spin_lock_irqsave(vhost->host->host_lock, flags);
875         __ibmvfc_reset_host(vhost);
876         spin_unlock_irqrestore(vhost->host->host_lock, flags);
877 }
878 
879 /**
880  * ibmvfc_retry_host_init - Retry host initialization if allowed
881  * @vhost:      ibmvfc host struct
882  *
883  * Returns: 1 if init will be retried / 0 if not
884  *
885  **/
886 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
887 {
888         int retry = 0;
889 
890         if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
891                 vhost->delay_init = 1;
892                 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
893                         dev_err(vhost->dev,
894                                 "Host initialization retries exceeded. Taking adapter offline\n");
895                         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
896                 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
897                         __ibmvfc_reset_host(vhost);
898                 else {
899                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
900                         retry = 1;
901                 }
902         }
903 
904         wake_up(&vhost->work_wait_q);
905         return retry;
906 }
907 
908 /**
909  * __ibmvfc_get_target - Find the specified scsi_target (no locking)
910  * @starget:    scsi target struct
911  *
912  * Return value:
913  *      ibmvfc_target struct / NULL if not found
914  **/
915 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
916 {
917         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
918         struct ibmvfc_host *vhost = shost_priv(shost);
919         struct ibmvfc_target *tgt;
920 
921         list_for_each_entry(tgt, &vhost->targets, queue)
922                 if (tgt->target_id == starget->id) {
923                         kref_get(&tgt->kref);
924                         return tgt;
925                 }
926         return NULL;
927 }
928 
929 /**
930  * ibmvfc_get_target - Find the specified scsi_target
931  * @starget:    scsi target struct
932  *
933  * Return value:
934  *      ibmvfc_target struct / NULL if not found
935  **/
936 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
937 {
938         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
939         struct ibmvfc_target *tgt;
940         unsigned long flags;
941 
942         spin_lock_irqsave(shost->host_lock, flags);
943         tgt = __ibmvfc_get_target(starget);
944         spin_unlock_irqrestore(shost->host_lock, flags);
945         return tgt;
946 }
947 
948 /**
949  * ibmvfc_get_host_speed - Get host port speed
950  * @shost:              scsi host struct
951  *
952  * Return value:
953  *      none
954  **/
955 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
956 {
957         struct ibmvfc_host *vhost = shost_priv(shost);
958         unsigned long flags;
959 
960         spin_lock_irqsave(shost->host_lock, flags);
961         if (vhost->state == IBMVFC_ACTIVE) {
962                 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
963                 case 1:
964                         fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
965                         break;
966                 case 2:
967                         fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
968                         break;
969                 case 4:
970                         fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
971                         break;
972                 case 8:
973                         fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
974                         break;
975                 case 10:
976                         fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
977                         break;
978                 case 16:
979                         fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
980                         break;
981                 default:
982                         ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
983                                    be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
984                         fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
985                         break;
986                 }
987         } else
988                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
989         spin_unlock_irqrestore(shost->host_lock, flags);
990 }
991 
992 /**
993  * ibmvfc_get_host_port_state - Get host port state
994  * @shost:              scsi host struct
995  *
996  * Return value:
997  *      none
998  **/
999 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1000 {
1001         struct ibmvfc_host *vhost = shost_priv(shost);
1002         unsigned long flags;
1003 
1004         spin_lock_irqsave(shost->host_lock, flags);
1005         switch (vhost->state) {
1006         case IBMVFC_INITIALIZING:
1007         case IBMVFC_ACTIVE:
1008                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1009                 break;
1010         case IBMVFC_LINK_DOWN:
1011                 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1012                 break;
1013         case IBMVFC_LINK_DEAD:
1014         case IBMVFC_HOST_OFFLINE:
1015                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1016                 break;
1017         case IBMVFC_HALTED:
1018                 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1019                 break;
1020         case IBMVFC_NO_CRQ:
1021                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1022                 break;
1023         default:
1024                 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1025                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1026                 break;
1027         }
1028         spin_unlock_irqrestore(shost->host_lock, flags);
1029 }
1030 
1031 /**
1032  * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1033  * @rport:              rport struct
1034  * @timeout:    timeout value
1035  *
1036  * Return value:
1037  *      none
1038  **/
1039 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1040 {
1041         if (timeout)
1042                 rport->dev_loss_tmo = timeout;
1043         else
1044                 rport->dev_loss_tmo = 1;
1045 }
1046 
1047 /**
1048  * ibmvfc_release_tgt - Free memory allocated for a target
1049  * @kref:               kref struct
1050  *
1051  **/
1052 static void ibmvfc_release_tgt(struct kref *kref)
1053 {
1054         struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1055         kfree(tgt);
1056 }
1057 
1058 /**
1059  * ibmvfc_get_starget_node_name - Get SCSI target's node name
1060  * @starget:    scsi target struct
1061  *
1062  * Return value:
1063  *      none
1064  **/
1065 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1066 {
1067         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1068         fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1069         if (tgt)
1070                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1071 }
1072 
1073 /**
1074  * ibmvfc_get_starget_port_name - Get SCSI target's port name
1075  * @starget:    scsi target struct
1076  *
1077  * Return value:
1078  *      none
1079  **/
1080 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1081 {
1082         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1083         fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1084         if (tgt)
1085                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1086 }
1087 
1088 /**
1089  * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1090  * @starget:    scsi target struct
1091  *
1092  * Return value:
1093  *      none
1094  **/
1095 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1096 {
1097         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1098         fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1099         if (tgt)
1100                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1101 }
1102 
1103 /**
1104  * ibmvfc_wait_while_resetting - Wait while the host resets
1105  * @vhost:              ibmvfc host struct
1106  *
1107  * Return value:
1108  *      0 on success / other on failure
1109  **/
1110 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1111 {
1112         long timeout = wait_event_timeout(vhost->init_wait_q,
1113                                           ((vhost->state == IBMVFC_ACTIVE ||
1114                                             vhost->state == IBMVFC_HOST_OFFLINE ||
1115                                             vhost->state == IBMVFC_LINK_DEAD) &&
1116                                            vhost->action == IBMVFC_HOST_ACTION_NONE),
1117                                           (init_timeout * HZ));
1118 
1119         return timeout ? 0 : -EIO;
1120 }
1121 
1122 /**
1123  * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1124  * @shost:              scsi host struct
1125  *
1126  * Return value:
1127  *      0 on success / other on failure
1128  **/
1129 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1130 {
1131         struct ibmvfc_host *vhost = shost_priv(shost);
1132 
1133         dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1134         ibmvfc_reset_host(vhost);
1135         return ibmvfc_wait_while_resetting(vhost);
1136 }
1137 
1138 /**
1139  * ibmvfc_gather_partition_info - Gather info about the LPAR
1140  *
1141  * Return value:
1142  *      none
1143  **/
1144 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1145 {
1146         struct device_node *rootdn;
1147         const char *name;
1148         const unsigned int *num;
1149 
1150         rootdn = of_find_node_by_path("/");
1151         if (!rootdn)
1152                 return;
1153 
1154         name = of_get_property(rootdn, "ibm,partition-name", NULL);
1155         if (name)
1156                 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1157         num = of_get_property(rootdn, "ibm,partition-no", NULL);
1158         if (num)
1159                 vhost->partition_number = *num;
1160         of_node_put(rootdn);
1161 }
1162 
1163 /**
1164  * ibmvfc_set_login_info - Setup info for NPIV login
1165  * @vhost:      ibmvfc host struct
1166  *
1167  * Return value:
1168  *      none
1169  **/
1170 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1171 {
1172         struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1173         struct device_node *of_node = vhost->dev->of_node;
1174         const char *location;
1175 
1176         memset(login_info, 0, sizeof(*login_info));
1177 
1178         login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1179         login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1180         login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1181         login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1182         login_info->partition_num = cpu_to_be32(vhost->partition_number);
1183         login_info->vfc_frame_version = cpu_to_be32(1);
1184         login_info->fcp_version = cpu_to_be16(3);
1185         login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1186         if (vhost->client_migrated)
1187                 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1188 
1189         login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1190         login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
1191         login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1192         login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
1193         strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1194         strncpy(login_info->device_name,
1195                 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1196 
1197         location = of_get_property(of_node, "ibm,loc-code", NULL);
1198         location = location ? location : dev_name(vhost->dev);
1199         strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1200 }
1201 
1202 /**
1203  * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1204  * @vhost:      ibmvfc host who owns the event pool
1205  *
1206  * Returns zero on success.
1207  **/
1208 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1209 {
1210         int i;
1211         struct ibmvfc_event_pool *pool = &vhost->pool;
1212 
1213         ENTER;
1214         pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1215         pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1216         if (!pool->events)
1217                 return -ENOMEM;
1218 
1219         pool->iu_storage = dma_alloc_coherent(vhost->dev,
1220                                               pool->size * sizeof(*pool->iu_storage),
1221                                               &pool->iu_token, 0);
1222 
1223         if (!pool->iu_storage) {
1224                 kfree(pool->events);
1225                 return -ENOMEM;
1226         }
1227 
1228         for (i = 0; i < pool->size; ++i) {
1229                 struct ibmvfc_event *evt = &pool->events[i];
1230                 atomic_set(&evt->free, 1);
1231                 evt->crq.valid = 0x80;
1232                 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
1233                 evt->xfer_iu = pool->iu_storage + i;
1234                 evt->vhost = vhost;
1235                 evt->ext_list = NULL;
1236                 list_add_tail(&evt->queue, &vhost->free);
1237         }
1238 
1239         LEAVE;
1240         return 0;
1241 }
1242 
1243 /**
1244  * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1245  * @vhost:      ibmvfc host who owns the event pool
1246  *
1247  **/
1248 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1249 {
1250         int i;
1251         struct ibmvfc_event_pool *pool = &vhost->pool;
1252 
1253         ENTER;
1254         for (i = 0; i < pool->size; ++i) {
1255                 list_del(&pool->events[i].queue);
1256                 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1257                 if (pool->events[i].ext_list)
1258                         dma_pool_free(vhost->sg_pool,
1259                                       pool->events[i].ext_list,
1260                                       pool->events[i].ext_list_token);
1261         }
1262 
1263         kfree(pool->events);
1264         dma_free_coherent(vhost->dev,
1265                           pool->size * sizeof(*pool->iu_storage),
1266                           pool->iu_storage, pool->iu_token);
1267         LEAVE;
1268 }
1269 
1270 /**
1271  * ibmvfc_get_event - Gets the next free event in pool
1272  * @vhost:      ibmvfc host struct
1273  *
1274  * Returns a free event from the pool.
1275  **/
1276 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1277 {
1278         struct ibmvfc_event *evt;
1279 
1280         BUG_ON(list_empty(&vhost->free));
1281         evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1282         atomic_set(&evt->free, 0);
1283         list_del(&evt->queue);
1284         return evt;
1285 }
1286 
1287 /**
1288  * ibmvfc_init_event - Initialize fields in an event struct that are always
1289  *                              required.
1290  * @evt:        The event
1291  * @done:       Routine to call when the event is responded to
1292  * @format:     SRP or MAD format
1293  **/
1294 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1295                               void (*done) (struct ibmvfc_event *), u8 format)
1296 {
1297         evt->cmnd = NULL;
1298         evt->sync_iu = NULL;
1299         evt->crq.format = format;
1300         evt->done = done;
1301         evt->eh_comp = NULL;
1302 }
1303 
1304 /**
1305  * ibmvfc_map_sg_list - Initialize scatterlist
1306  * @scmd:       scsi command struct
1307  * @nseg:       number of scatterlist segments
1308  * @md: memory descriptor list to initialize
1309  **/
1310 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1311                                struct srp_direct_buf *md)
1312 {
1313         int i;
1314         struct scatterlist *sg;
1315 
1316         scsi_for_each_sg(scmd, sg, nseg, i) {
1317                 md[i].va = cpu_to_be64(sg_dma_address(sg));
1318                 md[i].len = cpu_to_be32(sg_dma_len(sg));
1319                 md[i].key = 0;
1320         }
1321 }
1322 
1323 /**
1324  * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1325  * @scmd:               Scsi_Cmnd with the scatterlist
1326  * @evt:                ibmvfc event struct
1327  * @vfc_cmd:    vfc_cmd that contains the memory descriptor
1328  * @dev:                device for which to map dma memory
1329  *
1330  * Returns:
1331  *      0 on success / non-zero on failure
1332  **/
1333 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1334                               struct ibmvfc_event *evt,
1335                               struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1336 {
1337 
1338         int sg_mapped;
1339         struct srp_direct_buf *data = &vfc_cmd->ioba;
1340         struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1341 
1342         if (cls3_error)
1343                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1344 
1345         sg_mapped = scsi_dma_map(scmd);
1346         if (!sg_mapped) {
1347                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1348                 return 0;
1349         } else if (unlikely(sg_mapped < 0)) {
1350                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1351                         scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1352                 return sg_mapped;
1353         }
1354 
1355         if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1356                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1357                 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1358         } else {
1359                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1360                 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1361         }
1362 
1363         if (sg_mapped == 1) {
1364                 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1365                 return 0;
1366         }
1367 
1368         vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1369 
1370         if (!evt->ext_list) {
1371                 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1372                                                &evt->ext_list_token);
1373 
1374                 if (!evt->ext_list) {
1375                         scsi_dma_unmap(scmd);
1376                         if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1377                                 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1378                         return -ENOMEM;
1379                 }
1380         }
1381 
1382         ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1383 
1384         data->va = cpu_to_be64(evt->ext_list_token);
1385         data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1386         data->key = 0;
1387         return 0;
1388 }
1389 
1390 /**
1391  * ibmvfc_timeout - Internal command timeout handler
1392  * @evt:        struct ibmvfc_event that timed out
1393  *
1394  * Called when an internally generated command times out
1395  **/
1396 static void ibmvfc_timeout(struct ibmvfc_event *evt)
1397 {
1398         struct ibmvfc_host *vhost = evt->vhost;
1399         dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1400         ibmvfc_reset_host(vhost);
1401 }
1402 
1403 /**
1404  * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1405  * @evt:                event to be sent
1406  * @vhost:              ibmvfc host struct
1407  * @timeout:    timeout in seconds - 0 means do not time command
1408  *
1409  * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1410  **/
1411 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1412                              struct ibmvfc_host *vhost, unsigned long timeout)
1413 {
1414         __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1415         int rc;
1416 
1417         /* Copy the IU into the transfer area */
1418         *evt->xfer_iu = evt->iu;
1419         if (evt->crq.format == IBMVFC_CMD_FORMAT)
1420                 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1421         else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1422                 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1423         else
1424                 BUG();
1425 
1426         list_add_tail(&evt->queue, &vhost->sent);
1427         init_timer(&evt->timer);
1428 
1429         if (timeout) {
1430                 evt->timer.data = (unsigned long) evt;
1431                 evt->timer.expires = jiffies + (timeout * HZ);
1432                 evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1433                 add_timer(&evt->timer);
1434         }
1435 
1436         mb();
1437 
1438         if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1439                                   be64_to_cpu(crq_as_u64[1])))) {
1440                 list_del(&evt->queue);
1441                 del_timer(&evt->timer);
1442 
1443                 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1444                  * Firmware will send a CRQ with a transport event (0xFF) to
1445                  * tell this client what has happened to the transport. This
1446                  * will be handled in ibmvfc_handle_crq()
1447                  */
1448                 if (rc == H_CLOSED) {
1449                         if (printk_ratelimit())
1450                                 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1451                         if (evt->cmnd)
1452                                 scsi_dma_unmap(evt->cmnd);
1453                         ibmvfc_free_event(evt);
1454                         return SCSI_MLQUEUE_HOST_BUSY;
1455                 }
1456 
1457                 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1458                 if (evt->cmnd) {
1459                         evt->cmnd->result = DID_ERROR << 16;
1460                         evt->done = ibmvfc_scsi_eh_done;
1461                 } else
1462                         evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1463 
1464                 evt->done(evt);
1465         } else
1466                 ibmvfc_trc_start(evt);
1467 
1468         return 0;
1469 }
1470 
1471 /**
1472  * ibmvfc_log_error - Log an error for the failed command if appropriate
1473  * @evt:        ibmvfc event to log
1474  *
1475  **/
1476 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1477 {
1478         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1479         struct ibmvfc_host *vhost = evt->vhost;
1480         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1481         struct scsi_cmnd *cmnd = evt->cmnd;
1482         const char *err = unknown_error;
1483         int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1484         int logerr = 0;
1485         int rsp_code = 0;
1486 
1487         if (index >= 0) {
1488                 logerr = cmd_status[index].log;
1489                 err = cmd_status[index].name;
1490         }
1491 
1492         if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1493                 return;
1494 
1495         if (rsp->flags & FCP_RSP_LEN_VALID)
1496                 rsp_code = rsp->data.info.rsp_code;
1497 
1498         scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1499                     "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1500                     cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1501                     rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1502 }
1503 
1504 /**
1505  * ibmvfc_relogin - Log back into the specified device
1506  * @sdev:       scsi device struct
1507  *
1508  **/
1509 static void ibmvfc_relogin(struct scsi_device *sdev)
1510 {
1511         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1512         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1513         struct ibmvfc_target *tgt;
1514 
1515         list_for_each_entry(tgt, &vhost->targets, queue) {
1516                 if (rport == tgt->rport) {
1517                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
1518                         break;
1519                 }
1520         }
1521 
1522         ibmvfc_reinit_host(vhost);
1523 }
1524 
1525 /**
1526  * ibmvfc_scsi_done - Handle responses from commands
1527  * @evt:        ibmvfc event to be handled
1528  *
1529  * Used as a callback when sending scsi cmds.
1530  **/
1531 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1532 {
1533         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1534         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1535         struct scsi_cmnd *cmnd = evt->cmnd;
1536         u32 rsp_len = 0;
1537         u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1538 
1539         if (cmnd) {
1540                 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1541                         scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1542                 else if (rsp->flags & FCP_RESID_UNDER)
1543                         scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1544                 else
1545                         scsi_set_resid(cmnd, 0);
1546 
1547                 if (vfc_cmd->status) {
1548                         cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1549 
1550                         if (rsp->flags & FCP_RSP_LEN_VALID)
1551                                 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1552                         if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1553                                 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1554                         if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1555                                 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1556                         if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1557                             (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1558                                 ibmvfc_relogin(cmnd->device);
1559 
1560                         if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1561                                 cmnd->result = (DID_ERROR << 16);
1562 
1563                         ibmvfc_log_error(evt);
1564                 }
1565 
1566                 if (!cmnd->result &&
1567                     (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1568                         cmnd->result = (DID_ERROR << 16);
1569 
1570                 scsi_dma_unmap(cmnd);
1571                 cmnd->scsi_done(cmnd);
1572         }
1573 
1574         if (evt->eh_comp)
1575                 complete(evt->eh_comp);
1576 
1577         ibmvfc_free_event(evt);
1578 }
1579 
1580 /**
1581  * ibmvfc_host_chkready - Check if the host can accept commands
1582  * @vhost:       struct ibmvfc host
1583  *
1584  * Returns:
1585  *      1 if host can accept command / 0 if not
1586  **/
1587 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1588 {
1589         int result = 0;
1590 
1591         switch (vhost->state) {
1592         case IBMVFC_LINK_DEAD:
1593         case IBMVFC_HOST_OFFLINE:
1594                 result = DID_NO_CONNECT << 16;
1595                 break;
1596         case IBMVFC_NO_CRQ:
1597         case IBMVFC_INITIALIZING:
1598         case IBMVFC_HALTED:
1599         case IBMVFC_LINK_DOWN:
1600                 result = DID_REQUEUE << 16;
1601                 break;
1602         case IBMVFC_ACTIVE:
1603                 result = 0;
1604                 break;
1605         };
1606 
1607         return result;
1608 }
1609 
1610 /**
1611  * ibmvfc_queuecommand - The queuecommand function of the scsi template
1612  * @cmnd:       struct scsi_cmnd to be executed
1613  * @done:       Callback function to be called when cmnd is completed
1614  *
1615  * Returns:
1616  *      0 on success / other on failure
1617  **/
1618 static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
1619                                void (*done) (struct scsi_cmnd *))
1620 {
1621         struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1622         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1623         struct ibmvfc_cmd *vfc_cmd;
1624         struct ibmvfc_event *evt;
1625         int rc;
1626 
1627         if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1628             unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1629                 cmnd->result = rc;
1630                 done(cmnd);
1631                 return 0;
1632         }
1633 
1634         cmnd->result = (DID_OK << 16);
1635         evt = ibmvfc_get_event(vhost);
1636         ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1637         evt->cmnd = cmnd;
1638         cmnd->scsi_done = done;
1639         vfc_cmd = &evt->iu.cmd;
1640         memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1641         vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1642         vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1643         vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1644         vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
1645         vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1646         vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
1647         vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1648         vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1649         int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1650         memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1651 
1652         if (cmnd->flags & SCMD_TAGGED) {
1653                 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1654                 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1655         }
1656 
1657         if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1658                 return ibmvfc_send_event(evt, vhost, 0);
1659 
1660         ibmvfc_free_event(evt);
1661         if (rc == -ENOMEM)
1662                 return SCSI_MLQUEUE_HOST_BUSY;
1663 
1664         if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1665                 scmd_printk(KERN_ERR, cmnd,
1666                             "Failed to map DMA buffer for command. rc=%d\n", rc);
1667 
1668         cmnd->result = DID_ERROR << 16;
1669         done(cmnd);
1670         return 0;
1671 }
1672 
1673 static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1674 
1675 /**
1676  * ibmvfc_sync_completion - Signal that a synchronous command has completed
1677  * @evt:        ibmvfc event struct
1678  *
1679  **/
1680 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1681 {
1682         /* copy the response back */
1683         if (evt->sync_iu)
1684                 *evt->sync_iu = *evt->xfer_iu;
1685 
1686         complete(&evt->comp);
1687 }
1688 
1689 /**
1690  * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1691  * @evt:        struct ibmvfc_event
1692  *
1693  **/
1694 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1695 {
1696         struct ibmvfc_host *vhost = evt->vhost;
1697 
1698         ibmvfc_free_event(evt);
1699         vhost->aborting_passthru = 0;
1700         dev_info(vhost->dev, "Passthru command cancelled\n");
1701 }
1702 
1703 /**
1704  * ibmvfc_bsg_timeout - Handle a BSG timeout
1705  * @job:        struct bsg_job that timed out
1706  *
1707  * Returns:
1708  *      0 on success / other on failure
1709  **/
1710 static int ibmvfc_bsg_timeout(struct bsg_job *job)
1711 {
1712         struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1713         unsigned long port_id = (unsigned long)job->dd_data;
1714         struct ibmvfc_event *evt;
1715         struct ibmvfc_tmf *tmf;
1716         unsigned long flags;
1717         int rc;
1718 
1719         ENTER;
1720         spin_lock_irqsave(vhost->host->host_lock, flags);
1721         if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
1722                 __ibmvfc_reset_host(vhost);
1723                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1724                 return 0;
1725         }
1726 
1727         vhost->aborting_passthru = 1;
1728         evt = ibmvfc_get_event(vhost);
1729         ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
1730 
1731         tmf = &evt->iu.tmf;
1732         memset(tmf, 0, sizeof(*tmf));
1733         tmf->common.version = cpu_to_be32(1);
1734         tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
1735         tmf->common.length = cpu_to_be16(sizeof(*tmf));
1736         tmf->scsi_id = cpu_to_be64(port_id);
1737         tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1738         tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
1739         rc = ibmvfc_send_event(evt, vhost, default_timeout);
1740 
1741         if (rc != 0) {
1742                 vhost->aborting_passthru = 0;
1743                 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
1744                 rc = -EIO;
1745         } else
1746                 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
1747                          port_id);
1748 
1749         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1750 
1751         LEAVE;
1752         return rc;
1753 }
1754 
1755 /**
1756  * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1757  * @vhost:              struct ibmvfc_host to send command
1758  * @port_id:    port ID to send command
1759  *
1760  * Returns:
1761  *      0 on success / other on failure
1762  **/
1763 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
1764 {
1765         struct ibmvfc_port_login *plogi;
1766         struct ibmvfc_target *tgt;
1767         struct ibmvfc_event *evt;
1768         union ibmvfc_iu rsp_iu;
1769         unsigned long flags;
1770         int rc = 0, issue_login = 1;
1771 
1772         ENTER;
1773         spin_lock_irqsave(vhost->host->host_lock, flags);
1774         list_for_each_entry(tgt, &vhost->targets, queue) {
1775                 if (tgt->scsi_id == port_id) {
1776                         issue_login = 0;
1777                         break;
1778                 }
1779         }
1780 
1781         if (!issue_login)
1782                 goto unlock_out;
1783         if (unlikely((rc = ibmvfc_host_chkready(vhost))))
1784                 goto unlock_out;
1785 
1786         evt = ibmvfc_get_event(vhost);
1787         ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1788         plogi = &evt->iu.plogi;
1789         memset(plogi, 0, sizeof(*plogi));
1790         plogi->common.version = cpu_to_be32(1);
1791         plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
1792         plogi->common.length = cpu_to_be16(sizeof(*plogi));
1793         plogi->scsi_id = cpu_to_be64(port_id);
1794         evt->sync_iu = &rsp_iu;
1795         init_completion(&evt->comp);
1796 
1797         rc = ibmvfc_send_event(evt, vhost, default_timeout);
1798         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1799 
1800         if (rc)
1801                 return -EIO;
1802 
1803         wait_for_completion(&evt->comp);
1804 
1805         if (rsp_iu.plogi.common.status)
1806                 rc = -EIO;
1807 
1808         spin_lock_irqsave(vhost->host->host_lock, flags);
1809         ibmvfc_free_event(evt);
1810 unlock_out:
1811         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1812         LEAVE;
1813         return rc;
1814 }
1815 
1816 /**
1817  * ibmvfc_bsg_request - Handle a BSG request
1818  * @job:        struct bsg_job to be executed
1819  *
1820  * Returns:
1821  *      0 on success / other on failure
1822  **/
1823 static int ibmvfc_bsg_request(struct bsg_job *job)
1824 {
1825         struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1826         struct fc_rport *rport = fc_bsg_to_rport(job);
1827         struct ibmvfc_passthru_mad *mad;
1828         struct ibmvfc_event *evt;
1829         union ibmvfc_iu rsp_iu;
1830         unsigned long flags, port_id = -1;
1831         struct fc_bsg_request *bsg_request = job->request;
1832         struct fc_bsg_reply *bsg_reply = job->reply;
1833         unsigned int code = bsg_request->msgcode;
1834         int rc = 0, req_seg, rsp_seg, issue_login = 0;
1835         u32 fc_flags, rsp_len;
1836 
1837         ENTER;
1838         bsg_reply->reply_payload_rcv_len = 0;
1839         if (rport)
1840                 port_id = rport->port_id;
1841 
1842         switch (code) {
1843         case FC_BSG_HST_ELS_NOLOGIN:
1844                 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
1845                         (bsg_request->rqst_data.h_els.port_id[1] << 8) |
1846                         bsg_request->rqst_data.h_els.port_id[2];
1847         case FC_BSG_RPT_ELS:
1848                 fc_flags = IBMVFC_FC_ELS;
1849                 break;
1850         case FC_BSG_HST_CT:
1851                 issue_login = 1;
1852                 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
1853                         (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
1854                         bsg_request->rqst_data.h_ct.port_id[2];
1855         case FC_BSG_RPT_CT:
1856                 fc_flags = IBMVFC_FC_CT_IU;
1857                 break;
1858         default:
1859                 return -ENOTSUPP;
1860         };
1861 
1862         if (port_id == -1)
1863                 return -EINVAL;
1864         if (!mutex_trylock(&vhost->passthru_mutex))
1865                 return -EBUSY;
1866 
1867         job->dd_data = (void *)port_id;
1868         req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
1869                              job->request_payload.sg_cnt, DMA_TO_DEVICE);
1870 
1871         if (!req_seg) {
1872                 mutex_unlock(&vhost->passthru_mutex);
1873                 return -ENOMEM;
1874         }
1875 
1876         rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
1877                              job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1878 
1879         if (!rsp_seg) {
1880                 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1881                              job->request_payload.sg_cnt, DMA_TO_DEVICE);
1882                 mutex_unlock(&vhost->passthru_mutex);
1883                 return -ENOMEM;
1884         }
1885 
1886         if (req_seg > 1 || rsp_seg > 1) {
1887                 rc = -EINVAL;
1888                 goto out;
1889         }
1890 
1891         if (issue_login)
1892                 rc = ibmvfc_bsg_plogi(vhost, port_id);
1893 
1894         spin_lock_irqsave(vhost->host->host_lock, flags);
1895 
1896         if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
1897             unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1898                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1899                 goto out;
1900         }
1901 
1902         evt = ibmvfc_get_event(vhost);
1903         ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1904         mad = &evt->iu.passthru;
1905 
1906         memset(mad, 0, sizeof(*mad));
1907         mad->common.version = cpu_to_be32(1);
1908         mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
1909         mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
1910 
1911         mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
1912                 offsetof(struct ibmvfc_passthru_mad, iu));
1913         mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
1914 
1915         mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
1916         mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
1917         mad->iu.flags = cpu_to_be32(fc_flags);
1918         mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1919 
1920         mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
1921         mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
1922         mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
1923         mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
1924         mad->iu.scsi_id = cpu_to_be64(port_id);
1925         mad->iu.tag = cpu_to_be64((u64)evt);
1926         rsp_len = be32_to_cpu(mad->iu.rsp.len);
1927 
1928         evt->sync_iu = &rsp_iu;
1929         init_completion(&evt->comp);
1930         rc = ibmvfc_send_event(evt, vhost, 0);
1931         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1932 
1933         if (rc) {
1934                 rc = -EIO;
1935                 goto out;
1936         }
1937 
1938         wait_for_completion(&evt->comp);
1939 
1940         if (rsp_iu.passthru.common.status)
1941                 rc = -EIO;
1942         else
1943                 bsg_reply->reply_payload_rcv_len = rsp_len;
1944 
1945         spin_lock_irqsave(vhost->host->host_lock, flags);
1946         ibmvfc_free_event(evt);
1947         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1948         bsg_reply->result = rc;
1949         bsg_job_done(job, bsg_reply->result,
1950                        bsg_reply->reply_payload_rcv_len);
1951         rc = 0;
1952 out:
1953         dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1954                      job->request_payload.sg_cnt, DMA_TO_DEVICE);
1955         dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
1956                      job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1957         mutex_unlock(&vhost->passthru_mutex);
1958         LEAVE;
1959         return rc;
1960 }
1961 
1962 /**
1963  * ibmvfc_reset_device - Reset the device with the specified reset type
1964  * @sdev:       scsi device to reset
1965  * @type:       reset type
1966  * @desc:       reset type description for log messages
1967  *
1968  * Returns:
1969  *      0 on success / other on failure
1970  **/
1971 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1972 {
1973         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1974         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1975         struct ibmvfc_cmd *tmf;
1976         struct ibmvfc_event *evt = NULL;
1977         union ibmvfc_iu rsp_iu;
1978         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1979         int rsp_rc = -EBUSY;
1980         unsigned long flags;
1981         int rsp_code = 0;
1982 
1983         spin_lock_irqsave(vhost->host->host_lock, flags);
1984         if (vhost->state == IBMVFC_ACTIVE) {
1985                 evt = ibmvfc_get_event(vhost);
1986                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1987 
1988                 tmf = &evt->iu.cmd;
1989                 memset(tmf, 0, sizeof(*tmf));
1990                 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1991                 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
1992                 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1993                 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
1994                 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
1995                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
1996                 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
1997                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1998                 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
1999                 tmf->iu.tmf_flags = type;
2000                 evt->sync_iu = &rsp_iu;
2001 
2002                 init_completion(&evt->comp);
2003                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2004         }
2005         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2006 
2007         if (rsp_rc != 0) {
2008                 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2009                             desc, rsp_rc);
2010                 return -EIO;
2011         }
2012 
2013         sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2014         wait_for_completion(&evt->comp);
2015 
2016         if (rsp_iu.cmd.status)
2017                 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2018 
2019         if (rsp_code) {
2020                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2021                         rsp_code = fc_rsp->data.info.rsp_code;
2022 
2023                 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2024                             "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2025                             ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2026                             rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
2027                             fc_rsp->scsi_status);
2028                 rsp_rc = -EIO;
2029         } else
2030                 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2031 
2032         spin_lock_irqsave(vhost->host->host_lock, flags);
2033         ibmvfc_free_event(evt);
2034         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2035         return rsp_rc;
2036 }
2037 
2038 /**
2039  * ibmvfc_match_rport - Match function for specified remote port
2040  * @evt:        ibmvfc event struct
2041  * @device:     device to match (rport)
2042  *
2043  * Returns:
2044  *      1 if event matches rport / 0 if event does not match rport
2045  **/
2046 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2047 {
2048         struct fc_rport *cmd_rport;
2049 
2050         if (evt->cmnd) {
2051                 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2052                 if (cmd_rport == rport)
2053                         return 1;
2054         }
2055         return 0;
2056 }
2057 
2058 /**
2059  * ibmvfc_match_target - Match function for specified target
2060  * @evt:        ibmvfc event struct
2061  * @device:     device to match (starget)
2062  *
2063  * Returns:
2064  *      1 if event matches starget / 0 if event does not match starget
2065  **/
2066 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2067 {
2068         if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2069                 return 1;
2070         return 0;
2071 }
2072 
2073 /**
2074  * ibmvfc_match_lun - Match function for specified LUN
2075  * @evt:        ibmvfc event struct
2076  * @device:     device to match (sdev)
2077  *
2078  * Returns:
2079  *      1 if event matches sdev / 0 if event does not match sdev
2080  **/
2081 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2082 {
2083         if (evt->cmnd && evt->cmnd->device == device)
2084                 return 1;
2085         return 0;
2086 }
2087 
2088 /**
2089  * ibmvfc_wait_for_ops - Wait for ops to complete
2090  * @vhost:      ibmvfc host struct
2091  * @device:     device to match (starget or sdev)
2092  * @match:      match function
2093  *
2094  * Returns:
2095  *      SUCCESS / FAILED
2096  **/
2097 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2098                                int (*match) (struct ibmvfc_event *, void *))
2099 {
2100         struct ibmvfc_event *evt;
2101         DECLARE_COMPLETION_ONSTACK(comp);
2102         int wait;
2103         unsigned long flags;
2104         signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2105 
2106         ENTER;
2107         do {
2108                 wait = 0;
2109                 spin_lock_irqsave(vhost->host->host_lock, flags);
2110                 list_for_each_entry(evt, &vhost->sent, queue) {
2111                         if (match(evt, device)) {
2112                                 evt->eh_comp = &comp;
2113                                 wait++;
2114                         }
2115                 }
2116                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2117 
2118                 if (wait) {
2119                         timeout = wait_for_completion_timeout(&comp, timeout);
2120 
2121                         if (!timeout) {
2122                                 wait = 0;
2123                                 spin_lock_irqsave(vhost->host->host_lock, flags);
2124                                 list_for_each_entry(evt, &vhost->sent, queue) {
2125                                         if (match(evt, device)) {
2126                                                 evt->eh_comp = NULL;
2127                                                 wait++;
2128                                         }
2129                                 }
2130                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2131                                 if (wait)
2132                                         dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2133                                 LEAVE;
2134                                 return wait ? FAILED : SUCCESS;
2135                         }
2136                 }
2137         } while (wait);
2138 
2139         LEAVE;
2140         return SUCCESS;
2141 }
2142 
2143 /**
2144  * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2145  * @sdev:       scsi device to cancel commands
2146  * @type:       type of error recovery being performed
2147  *
2148  * This sends a cancel to the VIOS for the specified device. This does
2149  * NOT send any abort to the actual device. That must be done separately.
2150  *
2151  * Returns:
2152  *      0 on success / other on failure
2153  **/
2154 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2155 {
2156         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2157         struct scsi_target *starget = scsi_target(sdev);
2158         struct fc_rport *rport = starget_to_rport(starget);
2159         struct ibmvfc_tmf *tmf;
2160         struct ibmvfc_event *evt, *found_evt;
2161         union ibmvfc_iu rsp;
2162         int rsp_rc = -EBUSY;
2163         unsigned long flags;
2164         u16 status;
2165 
2166         ENTER;
2167         spin_lock_irqsave(vhost->host->host_lock, flags);
2168         found_evt = NULL;
2169         list_for_each_entry(evt, &vhost->sent, queue) {
2170                 if (evt->cmnd && evt->cmnd->device == sdev) {
2171                         found_evt = evt;
2172                         break;
2173                 }
2174         }
2175 
2176         if (!found_evt) {
2177                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2178                         sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2179                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2180                 return 0;
2181         }
2182 
2183         if (vhost->logged_in) {
2184                 evt = ibmvfc_get_event(vhost);
2185                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2186 
2187                 tmf = &evt->iu.tmf;
2188                 memset(tmf, 0, sizeof(*tmf));
2189                 tmf->common.version = cpu_to_be32(1);
2190                 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2191                 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2192                 tmf->scsi_id = cpu_to_be64(rport->port_id);
2193                 int_to_scsilun(sdev->lun, &tmf->lun);
2194                 if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
2195                         type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2196                 if (vhost->state == IBMVFC_ACTIVE)
2197                         tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2198                 else
2199                         tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2200                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2201                 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2202 
2203                 evt->sync_iu = &rsp;
2204                 init_completion(&evt->comp);
2205                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2206         }
2207 
2208         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2209 
2210         if (rsp_rc != 0) {
2211                 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2212                 /* If failure is received, the host adapter is most likely going
2213                  through reset, return success so the caller will wait for the command
2214                  being cancelled to get returned */
2215                 return 0;
2216         }
2217 
2218         sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2219 
2220         wait_for_completion(&evt->comp);
2221         status = be16_to_cpu(rsp.mad_common.status);
2222         spin_lock_irqsave(vhost->host->host_lock, flags);
2223         ibmvfc_free_event(evt);
2224         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2225 
2226         if (status != IBMVFC_MAD_SUCCESS) {
2227                 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2228                 switch (status) {
2229                 case IBMVFC_MAD_DRIVER_FAILED:
2230                 case IBMVFC_MAD_CRQ_ERROR:
2231                         /* Host adapter most likely going through reset, return success to
2232                          the caller will wait for the command being cancelled to get returned */
2233                         return 0;
2234                 default:
2235                         return -EIO;
2236                 };
2237         }
2238 
2239         sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2240         return 0;
2241 }
2242 
2243 /**
2244  * ibmvfc_match_key - Match function for specified cancel key
2245  * @evt:        ibmvfc event struct
2246  * @key:        cancel key to match
2247  *
2248  * Returns:
2249  *      1 if event matches key / 0 if event does not match key
2250  **/
2251 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2252 {
2253         unsigned long cancel_key = (unsigned long)key;
2254 
2255         if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2256             be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2257                 return 1;
2258         return 0;
2259 }
2260 
2261 /**
2262  * ibmvfc_match_evt - Match function for specified event
2263  * @evt:        ibmvfc event struct
2264  * @match:      event to match
2265  *
2266  * Returns:
2267  *      1 if event matches key / 0 if event does not match key
2268  **/
2269 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2270 {
2271         if (evt == match)
2272                 return 1;
2273         return 0;
2274 }
2275 
2276 /**
2277  * ibmvfc_abort_task_set - Abort outstanding commands to the device
2278  * @sdev:       scsi device to abort commands
2279  *
2280  * This sends an Abort Task Set to the VIOS for the specified device. This does
2281  * NOT send any cancel to the VIOS. That must be done separately.
2282  *
2283  * Returns:
2284  *      0 on success / other on failure
2285  **/
2286 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2287 {
2288         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2289         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2290         struct ibmvfc_cmd *tmf;
2291         struct ibmvfc_event *evt, *found_evt;
2292         union ibmvfc_iu rsp_iu;
2293         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2294         int rc, rsp_rc = -EBUSY;
2295         unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2296         int rsp_code = 0;
2297 
2298         spin_lock_irqsave(vhost->host->host_lock, flags);
2299         found_evt = NULL;
2300         list_for_each_entry(evt, &vhost->sent, queue) {
2301                 if (evt->cmnd && evt->cmnd->device == sdev) {
2302                         found_evt = evt;
2303                         break;
2304                 }
2305         }
2306 
2307         if (!found_evt) {
2308                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2309                         sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2310                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2311                 return 0;
2312         }
2313 
2314         if (vhost->state == IBMVFC_ACTIVE) {
2315                 evt = ibmvfc_get_event(vhost);
2316                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2317 
2318                 tmf = &evt->iu.cmd;
2319                 memset(tmf, 0, sizeof(*tmf));
2320                 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2321                 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2322                 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2323                 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2324                 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2325                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2326                 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2327                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2328                 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2329                 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2330                 evt->sync_iu = &rsp_iu;
2331 
2332                 init_completion(&evt->comp);
2333                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2334         }
2335 
2336         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2337 
2338         if (rsp_rc != 0) {
2339                 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2340                 return -EIO;
2341         }
2342 
2343         sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2344         timeout = wait_for_completion_timeout(&evt->comp, timeout);
2345 
2346         if (!timeout) {
2347                 rc = ibmvfc_cancel_all(sdev, 0);
2348                 if (!rc) {
2349                         rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2350                         if (rc == SUCCESS)
2351                                 rc = 0;
2352                 }
2353 
2354                 if (rc) {
2355                         sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2356                         ibmvfc_reset_host(vhost);
2357                         rsp_rc = -EIO;
2358                         rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2359 
2360                         if (rc == SUCCESS)
2361                                 rsp_rc = 0;
2362 
2363                         rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2364                         if (rc != SUCCESS) {
2365                                 spin_lock_irqsave(vhost->host->host_lock, flags);
2366                                 ibmvfc_hard_reset_host(vhost);
2367                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2368                                 rsp_rc = 0;
2369                         }
2370 
2371                         goto out;
2372                 }
2373         }
2374 
2375         if (rsp_iu.cmd.status)
2376                 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2377 
2378         if (rsp_code) {
2379                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2380                         rsp_code = fc_rsp->data.info.rsp_code;
2381 
2382                 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2383                             "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2384                             ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2385                             rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
2386                             fc_rsp->scsi_status);
2387                 rsp_rc = -EIO;
2388         } else
2389                 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2390 
2391 out:
2392         spin_lock_irqsave(vhost->host->host_lock, flags);
2393         ibmvfc_free_event(evt);
2394         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2395         return rsp_rc;
2396 }
2397 
2398 /**
2399  * ibmvfc_eh_abort_handler - Abort a command
2400  * @cmd:        scsi command to abort
2401  *
2402  * Returns:
2403  *      SUCCESS / FAST_IO_FAIL / FAILED
2404  **/
2405 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2406 {
2407         struct scsi_device *sdev = cmd->device;
2408         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2409         int cancel_rc, block_rc;
2410         int rc = FAILED;
2411 
2412         ENTER;
2413         block_rc = fc_block_scsi_eh(cmd);
2414         ibmvfc_wait_while_resetting(vhost);
2415         if (block_rc != FAST_IO_FAIL) {
2416                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2417                 ibmvfc_abort_task_set(sdev);
2418         } else
2419                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2420 
2421         if (!cancel_rc)
2422                 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2423 
2424         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2425                 rc = FAST_IO_FAIL;
2426 
2427         LEAVE;
2428         return rc;
2429 }
2430 
2431 /**
2432  * ibmvfc_eh_device_reset_handler - Reset a single LUN
2433  * @cmd:        scsi command struct
2434  *
2435  * Returns:
2436  *      SUCCESS / FAST_IO_FAIL / FAILED
2437  **/
2438 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2439 {
2440         struct scsi_device *sdev = cmd->device;
2441         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2442         int cancel_rc, block_rc, reset_rc = 0;
2443         int rc = FAILED;
2444 
2445         ENTER;
2446         block_rc = fc_block_scsi_eh(cmd);
2447         ibmvfc_wait_while_resetting(vhost);
2448         if (block_rc != FAST_IO_FAIL) {
2449                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2450                 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2451         } else
2452                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2453 
2454         if (!cancel_rc && !reset_rc)
2455                 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2456 
2457         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2458                 rc = FAST_IO_FAIL;
2459 
2460         LEAVE;
2461         return rc;
2462 }
2463 
2464 /**
2465  * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2466  * @sdev:       scsi device struct
2467  * @data:       return code
2468  *
2469  **/
2470 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2471 {
2472         unsigned long *rc = data;
2473         *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2474 }
2475 
2476 /**
2477  * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2478  * @sdev:       scsi device struct
2479  * @data:       return code
2480  *
2481  **/
2482 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2483 {
2484         unsigned long *rc = data;
2485         *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2486 }
2487 
2488 /**
2489  * ibmvfc_eh_target_reset_handler - Reset the target
2490  * @cmd:        scsi command struct
2491  *
2492  * Returns:
2493  *      SUCCESS / FAST_IO_FAIL / FAILED
2494  **/
2495 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2496 {
2497         struct scsi_device *sdev = cmd->device;
2498         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2499         struct scsi_target *starget = scsi_target(sdev);
2500         int block_rc;
2501         int reset_rc = 0;
2502         int rc = FAILED;
2503         unsigned long cancel_rc = 0;
2504 
2505         ENTER;
2506         block_rc = fc_block_scsi_eh(cmd);
2507         ibmvfc_wait_while_resetting(vhost);
2508         if (block_rc != FAST_IO_FAIL) {
2509                 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2510                 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2511         } else
2512                 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2513 
2514         if (!cancel_rc && !reset_rc)
2515                 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2516 
2517         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2518                 rc = FAST_IO_FAIL;
2519 
2520         LEAVE;
2521         return rc;
2522 }
2523 
2524 /**
2525  * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2526  * @cmd:        struct scsi_cmnd having problems
2527  *
2528  **/
2529 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2530 {
2531         int rc, block_rc;
2532         struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2533 
2534         block_rc = fc_block_scsi_eh(cmd);
2535         dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2536         rc = ibmvfc_issue_fc_host_lip(vhost->host);
2537 
2538         if (block_rc == FAST_IO_FAIL)
2539                 return FAST_IO_FAIL;
2540 
2541         return rc ? FAILED : SUCCESS;
2542 }
2543 
2544 /**
2545  * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2546  * @rport:              rport struct
2547  *
2548  * Return value:
2549  *      none
2550  **/
2551 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2552 {
2553         struct Scsi_Host *shost = rport_to_shost(rport);
2554         struct ibmvfc_host *vhost = shost_priv(shost);
2555         struct fc_rport *dev_rport;
2556         struct scsi_device *sdev;
2557         unsigned long rc;
2558 
2559         ENTER;
2560         shost_for_each_device(sdev, shost) {
2561                 dev_rport = starget_to_rport(scsi_target(sdev));
2562                 if (dev_rport != rport)
2563                         continue;
2564                 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2565         }
2566 
2567         rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2568 
2569         if (rc == FAILED)
2570                 ibmvfc_issue_fc_host_lip(shost);
2571         LEAVE;
2572 }
2573 
2574 static const struct ibmvfc_async_desc ae_desc [] = {
2575         { "PLOGI",      IBMVFC_AE_ELS_PLOGI,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2576         { "LOGO",       IBMVFC_AE_ELS_LOGO,     IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2577         { "PRLO",       IBMVFC_AE_ELS_PRLO,     IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2578         { "N-Port SCN", IBMVFC_AE_SCN_NPORT,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2579         { "Group SCN",  IBMVFC_AE_SCN_GROUP,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2580         { "Domain SCN", IBMVFC_AE_SCN_DOMAIN,   IBMVFC_DEFAULT_LOG_LEVEL },
2581         { "Fabric SCN", IBMVFC_AE_SCN_FABRIC,   IBMVFC_DEFAULT_LOG_LEVEL },
2582         { "Link Up",    IBMVFC_AE_LINK_UP,      IBMVFC_DEFAULT_LOG_LEVEL },
2583         { "Link Down",  IBMVFC_AE_LINK_DOWN,    IBMVFC_DEFAULT_LOG_LEVEL },
2584         { "Link Dead",  IBMVFC_AE_LINK_DEAD,    IBMVFC_DEFAULT_LOG_LEVEL },
2585         { "Halt",       IBMVFC_AE_HALT,         IBMVFC_DEFAULT_LOG_LEVEL },
2586         { "Resume",     IBMVFC_AE_RESUME,       IBMVFC_DEFAULT_LOG_LEVEL },
2587         { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
2588 };
2589 
2590 static const struct ibmvfc_async_desc unknown_ae = {
2591         "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
2592 };
2593 
2594 /**
2595  * ibmvfc_get_ae_desc - Get text description for async event
2596  * @ae: async event
2597  *
2598  **/
2599 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
2600 {
2601         int i;
2602 
2603         for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2604                 if (ae_desc[i].ae == ae)
2605                         return &ae_desc[i];
2606 
2607         return &unknown_ae;
2608 }
2609 
2610 static const struct {
2611         enum ibmvfc_ae_link_state state;
2612         const char *desc;
2613 } link_desc [] = {
2614         { IBMVFC_AE_LS_LINK_UP,         " link up" },
2615         { IBMVFC_AE_LS_LINK_BOUNCED,    " link bounced" },
2616         { IBMVFC_AE_LS_LINK_DOWN,       " link down" },
2617         { IBMVFC_AE_LS_LINK_DEAD,       " link dead" },
2618 };
2619 
2620 /**
2621  * ibmvfc_get_link_state - Get text description for link state
2622  * @state:      link state
2623  *
2624  **/
2625 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2626 {
2627         int i;
2628 
2629         for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2630                 if (link_desc[i].state == state)
2631                         return link_desc[i].desc;
2632 
2633         return "";
2634 }
2635 
2636 /**
2637  * ibmvfc_handle_async - Handle an async event from the adapter
2638  * @crq:        crq to process
2639  * @vhost:      ibmvfc host struct
2640  *
2641  **/
2642 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2643                                 struct ibmvfc_host *vhost)
2644 {
2645         const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
2646         struct ibmvfc_target *tgt;
2647 
2648         ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2649                    " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
2650                    be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
2651                    ibmvfc_get_link_state(crq->link_state));
2652 
2653         switch (be64_to_cpu(crq->event)) {
2654         case IBMVFC_AE_RESUME:
2655                 switch (crq->link_state) {
2656                 case IBMVFC_AE_LS_LINK_DOWN:
2657                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2658                         break;
2659                 case IBMVFC_AE_LS_LINK_DEAD:
2660                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2661                         break;
2662                 case IBMVFC_AE_LS_LINK_UP:
2663                 case IBMVFC_AE_LS_LINK_BOUNCED:
2664                 default:
2665                         vhost->events_to_log |= IBMVFC_AE_LINKUP;
2666                         vhost->delay_init = 1;
2667                         __ibmvfc_reset_host(vhost);
2668                         break;
2669                 };
2670 
2671                 break;
2672         case IBMVFC_AE_LINK_UP:
2673                 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2674                 vhost->delay_init = 1;
2675                 __ibmvfc_reset_host(vhost);
2676                 break;
2677         case IBMVFC_AE_SCN_FABRIC:
2678         case IBMVFC_AE_SCN_DOMAIN:
2679                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2680                 if (vhost->state < IBMVFC_HALTED) {
2681                         vhost->delay_init = 1;
2682                         __ibmvfc_reset_host(vhost);
2683                 }
2684                 break;
2685         case IBMVFC_AE_SCN_NPORT:
2686         case IBMVFC_AE_SCN_GROUP:
2687                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2688                 ibmvfc_reinit_host(vhost);
2689                 break;
2690         case IBMVFC_AE_ELS_LOGO:
2691         case IBMVFC_AE_ELS_PRLO:
2692         case IBMVFC_AE_ELS_PLOGI:
2693                 list_for_each_entry(tgt, &vhost->targets, queue) {
2694                         if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2695                                 break;
2696                         if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
2697                                 continue;
2698                         if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
2699                                 continue;
2700                         if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
2701                                 continue;
2702                         if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
2703                                 tgt->logo_rcvd = 1;
2704                         if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
2705                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2706                                 ibmvfc_reinit_host(vhost);
2707                         }
2708                 }
2709                 break;
2710         case IBMVFC_AE_LINK_DOWN:
2711         case IBMVFC_AE_ADAPTER_FAILED:
2712                 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2713                 break;
2714         case IBMVFC_AE_LINK_DEAD:
2715                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2716                 break;
2717         case IBMVFC_AE_HALT:
2718                 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2719                 break;
2720         default:
2721                 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
2722                 break;
2723         };
2724 }
2725 
2726 /**
2727  * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2728  * @crq:        Command/Response queue
2729  * @vhost:      ibmvfc host struct
2730  *
2731  **/
2732 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2733 {
2734         long rc;
2735         struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
2736 
2737         switch (crq->valid) {
2738         case IBMVFC_CRQ_INIT_RSP:
2739                 switch (crq->format) {
2740                 case IBMVFC_CRQ_INIT:
2741                         dev_info(vhost->dev, "Partner initialized\n");
2742                         /* Send back a response */
2743                         rc = ibmvfc_send_crq_init_complete(vhost);
2744                         if (rc == 0)
2745                                 ibmvfc_init_host(vhost);
2746                         else
2747                                 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2748                         break;
2749                 case IBMVFC_CRQ_INIT_COMPLETE:
2750                         dev_info(vhost->dev, "Partner initialization complete\n");
2751                         ibmvfc_init_host(vhost);
2752                         break;
2753                 default:
2754                         dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2755                 }
2756                 return;
2757         case IBMVFC_CRQ_XPORT_EVENT:
2758                 vhost->state = IBMVFC_NO_CRQ;
2759                 vhost->logged_in = 0;
2760                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2761                 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2762                         /* We need to re-setup the interpartition connection */
2763                         dev_info(vhost->dev, "Re-enabling adapter\n");
2764                         vhost->client_migrated = 1;
2765                         ibmvfc_purge_requests(vhost, DID_REQUEUE);
2766                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2767                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
2768                 } else {
2769                         dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2770                         ibmvfc_purge_requests(vhost, DID_ERROR);
2771                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2772                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
2773                 }
2774                 return;
2775         case IBMVFC_CRQ_CMD_RSP:
2776                 break;
2777         default:
2778                 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2779                 return;
2780         }
2781 
2782         if (crq->format == IBMVFC_ASYNC_EVENT)
2783                 return;
2784 
2785         /* The only kind of payload CRQs we should get are responses to
2786          * things we send. Make sure this response is to something we
2787          * actually sent
2788          */
2789         if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2790                 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
2791                         crq->ioba);
2792                 return;
2793         }
2794 
2795         if (unlikely(atomic_read(&evt->free))) {
2796                 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
2797                         crq->ioba);
2798                 return;
2799         }
2800 
2801         del_timer(&evt->timer);
2802         list_del(&evt->queue);
2803         ibmvfc_trc_end(evt);
2804         evt->done(evt);
2805 }
2806 
2807 /**
2808  * ibmvfc_scan_finished - Check if the device scan is done.
2809  * @shost:      scsi host struct
2810  * @time:       current elapsed time
2811  *
2812  * Returns:
2813  *      0 if scan is not done / 1 if scan is done
2814  **/
2815 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2816 {
2817         unsigned long flags;
2818         struct ibmvfc_host *vhost = shost_priv(shost);
2819         int done = 0;
2820 
2821         spin_lock_irqsave(shost->host_lock, flags);
2822         if (time >= (init_timeout * HZ)) {
2823                 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2824                          "continuing initialization\n", init_timeout);
2825                 done = 1;
2826         }
2827 
2828         if (vhost->scan_complete)
2829                 done = 1;
2830         spin_unlock_irqrestore(shost->host_lock, flags);
2831         return done;
2832 }
2833 
2834 /**
2835  * ibmvfc_slave_alloc - Setup the device's task set value
2836  * @sdev:       struct scsi_device device to configure
2837  *
2838  * Set the device's task set value so that error handling works as
2839  * expected.
2840  *
2841  * Returns:
2842  *      0 on success / -ENXIO if device does not exist
2843  **/
2844 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2845 {
2846         struct Scsi_Host *shost = sdev->host;
2847         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2848         struct ibmvfc_host *vhost = shost_priv(shost);
2849         unsigned long flags = 0;
2850 
2851         if (!rport || fc_remote_port_chkready(rport))
2852                 return -ENXIO;
2853 
2854         spin_lock_irqsave(shost->host_lock, flags);
2855         sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2856         spin_unlock_irqrestore(shost->host_lock, flags);
2857         return 0;
2858 }
2859 
2860 /**
2861  * ibmvfc_target_alloc - Setup the target's task set value
2862  * @starget:    struct scsi_target
2863  *
2864  * Set the target's task set value so that error handling works as
2865  * expected.
2866  *
2867  * Returns:
2868  *      0 on success / -ENXIO if device does not exist
2869  **/
2870 static int ibmvfc_target_alloc(struct scsi_target *starget)
2871 {
2872         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2873         struct ibmvfc_host *vhost = shost_priv(shost);
2874         unsigned long flags = 0;
2875 
2876         spin_lock_irqsave(shost->host_lock, flags);
2877         starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2878         spin_unlock_irqrestore(shost->host_lock, flags);
2879         return 0;
2880 }
2881 
2882 /**
2883  * ibmvfc_slave_configure - Configure the device
2884  * @sdev:       struct scsi_device device to configure
2885  *
2886  * Enable allow_restart for a device if it is a disk. Adjust the
2887  * queue_depth here also.
2888  *
2889  * Returns:
2890  *      0
2891  **/
2892 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2893 {
2894         struct Scsi_Host *shost = sdev->host;
2895         unsigned long flags = 0;
2896 
2897         spin_lock_irqsave(shost->host_lock, flags);
2898         if (sdev->type == TYPE_DISK)
2899                 sdev->allow_restart = 1;
2900         spin_unlock_irqrestore(shost->host_lock, flags);
2901         return 0;
2902 }
2903 
2904 /**
2905  * ibmvfc_change_queue_depth - Change the device's queue depth
2906  * @sdev:       scsi device struct
2907  * @qdepth:     depth to set
2908  * @reason:     calling context
2909  *
2910  * Return value:
2911  *      actual depth set
2912  **/
2913 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2914 {
2915         if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2916                 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2917 
2918         return scsi_change_queue_depth(sdev, qdepth);
2919 }
2920 
2921 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2922                                                  struct device_attribute *attr, char *buf)
2923 {
2924         struct Scsi_Host *shost = class_to_shost(dev);
2925         struct ibmvfc_host *vhost = shost_priv(shost);
2926 
2927         return snprintf(buf, PAGE_SIZE, "%s\n",
2928                         vhost->login_buf->resp.partition_name);
2929 }
2930 
2931 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2932                                             struct device_attribute *attr, char *buf)
2933 {
2934         struct Scsi_Host *shost = class_to_shost(dev);
2935         struct ibmvfc_host *vhost = shost_priv(shost);
2936 
2937         return snprintf(buf, PAGE_SIZE, "%s\n",
2938                         vhost->login_buf->resp.device_name);
2939 }
2940 
2941 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2942                                          struct device_attribute *attr, char *buf)
2943 {
2944         struct Scsi_Host *shost = class_to_shost(dev);
2945         struct ibmvfc_host *vhost = shost_priv(shost);
2946 
2947         return snprintf(buf, PAGE_SIZE, "%s\n",
2948                         vhost->login_buf->resp.port_loc_code);
2949 }
2950 
2951 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2952                                          struct device_attribute *attr, char *buf)
2953 {
2954         struct Scsi_Host *shost = class_to_shost(dev);
2955         struct ibmvfc_host *vhost = shost_priv(shost);
2956 
2957         return snprintf(buf, PAGE_SIZE, "%s\n",
2958                         vhost->login_buf->resp.drc_name);
2959 }
2960 
2961 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2962                                              struct device_attribute *attr, char *buf)
2963 {
2964         struct Scsi_Host *shost = class_to_shost(dev);
2965         struct ibmvfc_host *vhost = shost_priv(shost);
2966         return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2967 }
2968 
2969 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
2970                                              struct device_attribute *attr, char *buf)
2971 {
2972         struct Scsi_Host *shost = class_to_shost(dev);
2973         struct ibmvfc_host *vhost = shost_priv(shost);
2974         return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
2975 }
2976 
2977 /**
2978  * ibmvfc_show_log_level - Show the adapter's error logging level
2979  * @dev:        class device struct
2980  * @buf:        buffer
2981  *
2982  * Return value:
2983  *      number of bytes printed to buffer
2984  **/
2985 static ssize_t ibmvfc_show_log_level(struct device *dev,
2986                                      struct device_attribute *attr, char *buf)
2987 {
2988         struct Scsi_Host *shost = class_to_shost(dev);
2989         struct ibmvfc_host *vhost = shost_priv(shost);
2990         unsigned long flags = 0;
2991         int len;
2992 
2993         spin_lock_irqsave(shost->host_lock, flags);
2994         len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2995         spin_unlock_irqrestore(shost->host_lock, flags);
2996         return len;
2997 }
2998 
2999 /**
3000  * ibmvfc_store_log_level - Change the adapter's error logging level
3001  * @dev:        class device struct
3002  * @buf:        buffer
3003  *
3004  * Return value:
3005  *      number of bytes printed to buffer
3006  **/
3007 static ssize_t ibmvfc_store_log_level(struct device *dev,
3008                                       struct device_attribute *attr,
3009                                       const char *buf, size_t count)
3010 {
3011         struct Scsi_Host *shost = class_to_shost(dev);
3012         struct ibmvfc_host *vhost = shost_priv(shost);
3013         unsigned long flags = 0;
3014 
3015         spin_lock_irqsave(shost->host_lock, flags);
3016         vhost->log_level = simple_strtoul(buf, NULL, 10);
3017         spin_unlock_irqrestore(shost->host_lock, flags);
3018         return strlen(buf);
3019 }
3020 
3021 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3022 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3023 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3024 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3025 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3026 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3027 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3028                    ibmvfc_show_log_level, ibmvfc_store_log_level);
3029 
3030 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3031 /**
3032  * ibmvfc_read_trace - Dump the adapter trace
3033  * @filp:               open sysfs file
3034  * @kobj:               kobject struct
3035  * @bin_attr:   bin_attribute struct
3036  * @buf:                buffer
3037  * @off:                offset
3038  * @count:              buffer size
3039  *
3040  * Return value:
3041  *      number of bytes printed to buffer
3042  **/
3043 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3044                                  struct bin_attribute *bin_attr,
3045                                  char *buf, loff_t off, size_t count)
3046 {
3047         struct device *dev = container_of(kobj, struct device, kobj);
3048         struct Scsi_Host *shost = class_to_shost(dev);
3049         struct ibmvfc_host *vhost = shost_priv(shost);
3050         unsigned long flags = 0;
3051         int size = IBMVFC_TRACE_SIZE;
3052         char *src = (char *)vhost->trace;
3053 
3054         if (off > size)
3055                 return 0;
3056         if (off + count > size) {
3057                 size -= off;
3058                 count = size;
3059         }
3060 
3061         spin_lock_irqsave(shost->host_lock, flags);
3062         memcpy(buf, &src[off], count);
3063         spin_unlock_irqrestore(shost->host_lock, flags);
3064         return count;
3065 }
3066 
3067 static struct bin_attribute ibmvfc_trace_attr = {
3068         .attr = {
3069                 .name = "trace",
3070                 .mode = S_IRUGO,
3071         },
3072         .size = 0,
3073         .read = ibmvfc_read_trace,
3074 };
3075 #endif
3076 
3077 static struct device_attribute *ibmvfc_attrs[] = {
3078         &dev_attr_partition_name,
3079         &dev_attr_device_name,
3080         &dev_attr_port_loc_code,
3081         &dev_attr_drc_name,
3082         &dev_attr_npiv_version,
3083         &dev_attr_capabilities,
3084         &dev_attr_log_level,
3085         NULL
3086 };
3087 
3088 static struct scsi_host_template driver_template = {
3089         .module = THIS_MODULE,
3090         .name = "IBM POWER Virtual FC Adapter",
3091         .proc_name = IBMVFC_NAME,
3092         .queuecommand = ibmvfc_queuecommand,
3093         .eh_abort_handler = ibmvfc_eh_abort_handler,
3094         .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3095         .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3096         .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3097         .slave_alloc = ibmvfc_slave_alloc,
3098         .slave_configure = ibmvfc_slave_configure,
3099         .target_alloc = ibmvfc_target_alloc,
3100         .scan_finished = ibmvfc_scan_finished,
3101         .change_queue_depth = ibmvfc_change_queue_depth,
3102         .cmd_per_lun = 16,
3103         .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3104         .this_id = -1,
3105         .sg_tablesize = SG_ALL,
3106         .max_sectors = IBMVFC_MAX_SECTORS,
3107         .use_clustering = ENABLE_CLUSTERING,
3108         .shost_attrs = ibmvfc_attrs,
3109         .track_queue_depth = 1,
3110 };
3111 
3112 /**
3113  * ibmvfc_next_async_crq - Returns the next entry in async queue
3114  * @vhost:      ibmvfc host struct
3115  *
3116  * Returns:
3117  *      Pointer to next entry in queue / NULL if empty
3118  **/
3119 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3120 {
3121         struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3122         struct ibmvfc_async_crq *crq;
3123 
3124         crq = &async_crq->msgs[async_crq->cur];
3125         if (crq->valid & 0x80) {
3126                 if (++async_crq->cur == async_crq->size)
3127                         async_crq->cur = 0;
3128                 rmb();
3129         } else
3130                 crq = NULL;
3131 
3132         return crq;
3133 }
3134 
3135 /**
3136  * ibmvfc_next_crq - Returns the next entry in message queue
3137  * @vhost:      ibmvfc host struct
3138  *
3139  * Returns:
3140  *      Pointer to next entry in queue / NULL if empty
3141  **/
3142 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3143 {
3144         struct ibmvfc_crq_queue *queue = &vhost->crq;
3145         struct ibmvfc_crq *crq;
3146 
3147         crq = &queue->msgs[queue->cur];
3148         if (crq->valid & 0x80) {
3149                 if (++queue->cur == queue->size)
3150                         queue->cur = 0;
3151                 rmb();
3152         } else
3153                 crq = NULL;
3154 
3155         return crq;
3156 }
3157 
3158 /**
3159  * ibmvfc_interrupt - Interrupt handler
3160  * @irq:                number of irq to handle, not used
3161  * @dev_instance: ibmvfc_host that received interrupt
3162  *
3163  * Returns:
3164  *      IRQ_HANDLED
3165  **/
3166 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3167 {
3168         struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3169         unsigned long flags;
3170 
3171         spin_lock_irqsave(vhost->host->host_lock, flags);
3172         vio_disable_interrupts(to_vio_dev(vhost->dev));
3173         tasklet_schedule(&vhost->tasklet);
3174         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3175         return IRQ_HANDLED;
3176 }
3177 
3178 /**
3179  * ibmvfc_tasklet - Interrupt handler tasklet
3180  * @data:               ibmvfc host struct
3181  *
3182  * Returns:
3183  *      Nothing
3184  **/
3185 static void ibmvfc_tasklet(void *data)
3186 {
3187         struct ibmvfc_host *vhost = data;
3188         struct vio_dev *vdev = to_vio_dev(vhost->dev);
3189         struct ibmvfc_crq *crq;
3190         struct ibmvfc_async_crq *async;
3191         unsigned long flags;
3192         int done = 0;
3193 
3194         spin_lock_irqsave(vhost->host->host_lock, flags);
3195         while (!done) {
3196                 /* Pull all the valid messages off the async CRQ */
3197                 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3198                         ibmvfc_handle_async(async, vhost);
3199                         async->valid = 0;
3200                         wmb();
3201                 }
3202 
3203                 /* Pull all the valid messages off the CRQ */
3204                 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3205                         ibmvfc_handle_crq(crq, vhost);
3206                         crq->valid = 0;
3207                         wmb();
3208                 }
3209 
3210                 vio_enable_interrupts(vdev);
3211                 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3212                         vio_disable_interrupts(vdev);
3213                         ibmvfc_handle_async(async, vhost);
3214                         async->valid = 0;
3215                         wmb();
3216                 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3217                         vio_disable_interrupts(vdev);
3218                         ibmvfc_handle_crq(crq, vhost);
3219                         crq->valid = 0;
3220                         wmb();
3221                 } else
3222                         done = 1;
3223         }
3224 
3225         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3226 }
3227 
3228 /**
3229  * ibmvfc_init_tgt - Set the next init job step for the target
3230  * @tgt:                ibmvfc target struct
3231  * @job_step:   job step to perform
3232  *
3233  **/
3234 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3235                             void (*job_step) (struct ibmvfc_target *))
3236 {
3237         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
3238         tgt->job_step = job_step;
3239         wake_up(&tgt->vhost->work_wait_q);
3240 }
3241 
3242 /**
3243  * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3244  * @tgt:                ibmvfc target struct
3245  * @job_step:   initialization job step
3246  *
3247  * Returns: 1 if step will be retried / 0 if not
3248  *
3249  **/
3250 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3251                                   void (*job_step) (struct ibmvfc_target *))
3252 {
3253         if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3254                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3255                 wake_up(&tgt->vhost->work_wait_q);
3256                 return 0;
3257         } else
3258                 ibmvfc_init_tgt(tgt, job_step);
3259         return 1;
3260 }
3261 
3262 /* Defined in FC-LS */
3263 static const struct {
3264         int code;
3265         int retry;
3266         int logged_in;
3267 } prli_rsp [] = {
3268         { 0, 1, 0 },
3269         { 1, 0, 1 },
3270         { 2, 1, 0 },
3271         { 3, 1, 0 },
3272         { 4, 0, 0 },
3273         { 5, 0, 0 },
3274         { 6, 0, 1 },
3275         { 7, 0, 0 },
3276         { 8, 1, 0 },
3277 };
3278 
3279 /**
3280  * ibmvfc_get_prli_rsp - Find PRLI response index
3281  * @flags:      PRLI response flags
3282  *
3283  **/
3284 static int ibmvfc_get_prli_rsp(u16 flags)
3285 {
3286         int i;
3287         int code = (flags & 0x0f00) >> 8;
3288 
3289         for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3290                 if (prli_rsp[i].code == code)
3291                         return i;
3292 
3293         return 0;
3294 }
3295 
3296 /**
3297  * ibmvfc_tgt_prli_done - Completion handler for Process Login
3298  * @evt:        ibmvfc event struct
3299  *
3300  **/
3301 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3302 {
3303         struct ibmvfc_target *tgt = evt->tgt;
3304         struct ibmvfc_host *vhost = evt->vhost;
3305         struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3306         struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3307         u32 status = be16_to_cpu(rsp->common.status);
3308         int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3309 
3310         vhost->discovery_threads--;
3311         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3312         switch (status) {
3313         case IBMVFC_MAD_SUCCESS:
3314                 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3315                         parms->type, parms->flags, parms->service_parms);
3316 
3317                 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3318                         index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3319                         if (prli_rsp[index].logged_in) {
3320                                 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3321                                         tgt->need_login = 0;
3322                                         tgt->ids.roles = 0;
3323                                         if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3324                                                 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3325                                         if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3326                                                 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3327                                         tgt->add_rport = 1;
3328                                 } else
3329                                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3330                         } else if (prli_rsp[index].retry)
3331                                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3332                         else
3333                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3334                 } else
3335                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3336                 break;
3337         case IBMVFC_MAD_DRIVER_FAILED:
3338                 break;
3339         case IBMVFC_MAD_CRQ_ERROR:
3340                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3341                 break;
3342         case IBMVFC_MAD_FAILED:
3343         default:
3344                 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3345                      be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3346                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3347                 else if (tgt->logo_rcvd)
3348                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3349                 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3350                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3351                 else
3352                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3353 
3354                 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3355                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3356                         rsp->status, rsp->error, status);
3357                 break;
3358         };
3359 
3360         kref_put(&tgt->kref, ibmvfc_release_tgt);
3361         ibmvfc_free_event(evt);
3362         wake_up(&vhost->work_wait_q);
3363 }
3364 
3365 /**
3366  * ibmvfc_tgt_send_prli - Send a process login
3367  * @tgt:        ibmvfc target struct
3368  *
3369  **/
3370 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3371 {
3372         struct ibmvfc_process_login *prli;
3373         struct ibmvfc_host *vhost = tgt->vhost;
3374         struct ibmvfc_event *evt;
3375 
3376         if (vhost->discovery_threads >= disc_threads)
3377                 return;
3378 
3379         kref_get(&tgt->kref);
3380         evt = ibmvfc_get_event(vhost);
3381         vhost->discovery_threads++;
3382         ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3383         evt->tgt = tgt;
3384         prli = &evt->iu.prli;
3385         memset(prli, 0, sizeof(*prli));
3386         prli->common.version = cpu_to_be32(1);
3387         prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3388         prli->common.length = cpu_to_be16(sizeof(*prli));
3389         prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3390 
3391         prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3392         prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3393         prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3394         prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3395 
3396         if (cls3_error)
3397                 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3398 
3399         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3400         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3401                 vhost->discovery_threads--;
3402                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3403                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3404         } else
3405                 tgt_dbg(tgt, "Sent process login\n");
3406 }
3407 
3408 /**
3409  * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3410  * @evt:        ibmvfc event struct
3411  *
3412  **/
3413 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3414 {
3415         struct ibmvfc_target *tgt = evt->tgt;
3416         struct ibmvfc_host *vhost = evt->vhost;
3417         struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3418         u32 status = be16_to_cpu(rsp->common.status);
3419         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3420 
3421         vhost->discovery_threads--;
3422         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3423         switch (status) {
3424         case IBMVFC_MAD_SUCCESS:
3425                 tgt_dbg(tgt, "Port Login succeeded\n");
3426                 if (tgt->ids.port_name &&
3427                     tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3428                         vhost->reinit = 1;
3429                         tgt_dbg(tgt, "Port re-init required\n");
3430                         break;
3431                 }
3432                 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3433                 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3434                 tgt->ids.port_id = tgt->scsi_id;
3435                 memcpy(&tgt->service_parms, &rsp->service_parms,
3436                        sizeof(tgt->service_parms));
3437                 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3438                        sizeof(tgt->service_parms_change));
3439                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3440                 break;
3441         case IBMVFC_MAD_DRIVER_FAILED:
3442                 break;
3443         case IBMVFC_MAD_CRQ_ERROR:
3444                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3445                 break;
3446         case IBMVFC_MAD_FAILED:
3447         default:
3448                 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3449                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3450                 else
3451                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3452 
3453                 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3454                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), rsp->status, rsp->error,
3455                         ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), rsp->fc_type,
3456                         ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), rsp->fc_explain, status);
3457                 break;
3458         };
3459 
3460         kref_put(&tgt->kref, ibmvfc_release_tgt);
3461         ibmvfc_free_event(evt);
3462         wake_up(&vhost->work_wait_q);
3463 }
3464 
3465 /**
3466  * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3467  * @tgt:        ibmvfc target struct
3468  *
3469  **/
3470 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3471 {
3472         struct ibmvfc_port_login *plogi;
3473         struct ibmvfc_host *vhost = tgt->vhost;
3474         struct ibmvfc_event *evt;
3475 
3476         if (vhost->discovery_threads >= disc_threads)
3477                 return;
3478 
3479         kref_get(&tgt->kref);
3480         tgt->logo_rcvd = 0;
3481         evt = ibmvfc_get_event(vhost);
3482         vhost->discovery_threads++;
3483         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3484         ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3485         evt->tgt = tgt;
3486         plogi = &evt->iu.plogi;
3487         memset(plogi, 0, sizeof(*plogi));
3488         plogi->common.version = cpu_to_be32(1);
3489         plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3490         plogi->common.length = cpu_to_be16(sizeof(*plogi));
3491         plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
3492 
3493         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3494                 vhost->discovery_threads--;
3495                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3496                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3497         } else
3498                 tgt_dbg(tgt, "Sent port login\n");
3499 }
3500 
3501 /**
3502  * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3503  * @evt:        ibmvfc event struct
3504  *
3505  **/
3506 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3507 {
3508         struct ibmvfc_target *tgt = evt->tgt;
3509         struct ibmvfc_host *vhost = evt->vhost;
3510         struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3511         u32 status = be16_to_cpu(rsp->common.status);
3512 
3513         vhost->discovery_threads--;
3514         ibmvfc_free_event(evt);
3515         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3516 
3517         switch (status) {
3518         case IBMVFC_MAD_SUCCESS:
3519                 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3520                 break;
3521         case IBMVFC_MAD_DRIVER_FAILED:
3522                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3523                 wake_up(&vhost->work_wait_q);
3524                 return;
3525         case IBMVFC_MAD_FAILED:
3526         default:
3527                 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3528                 break;
3529         };
3530 
3531         if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
3532                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3533         else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
3534                  tgt->scsi_id != tgt->new_scsi_id)
3535                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3536         kref_put(&tgt->kref, ibmvfc_release_tgt);
3537         wake_up(&vhost->work_wait_q);
3538 }
3539 
3540 /**
3541  * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3542  * @tgt:                ibmvfc target struct
3543  *
3544  **/
3545 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3546 {
3547         struct ibmvfc_implicit_logout *mad;
3548         struct ibmvfc_host *vhost = tgt->vhost;
3549         struct ibmvfc_event *evt;
3550 
3551         if (vhost->discovery_threads >= disc_threads)
3552                 return;
3553 
3554         kref_get(&tgt->kref);
3555         evt = ibmvfc_get_event(vhost);
3556         vhost->discovery_threads++;
3557         ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3558         evt->tgt = tgt;
3559         mad = &evt->iu.implicit_logout;
3560         memset(mad, 0, sizeof(*mad));
3561         mad->common.version = cpu_to_be32(1);
3562         mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3563         mad->common.length = cpu_to_be16(sizeof(*mad));
3564         mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
3565 
3566         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3567         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3568                 vhost->discovery_threads--;
3569                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3570                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3571         } else
3572                 tgt_dbg(tgt, "Sent Implicit Logout\n");
3573 }
3574 
3575 /**
3576  * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3577  * @mad:        ibmvfc passthru mad struct
3578  * @tgt:        ibmvfc target struct
3579  *
3580  * Returns:
3581  *      1 if PLOGI needed / 0 if PLOGI not needed
3582  **/
3583 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3584                                     struct ibmvfc_target *tgt)
3585 {
3586         if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3587                    sizeof(tgt->ids.port_name)))
3588                 return 1;
3589         if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3590                    sizeof(tgt->ids.node_name)))
3591                 return 1;
3592         if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
3593                 return 1;
3594         return 0;
3595 }
3596 
3597 /**
3598  * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3599  * @evt:        ibmvfc event struct
3600  *
3601  **/
3602 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3603 {
3604         struct ibmvfc_target *tgt = evt->tgt;
3605         struct ibmvfc_host *vhost = evt->vhost;
3606         struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3607         u32 status = be16_to_cpu(mad->common.status);
3608         u8 fc_reason, fc_explain;
3609 
3610         vhost->discovery_threads--;
3611         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3612         del_timer(&tgt->timer);
3613 
3614         switch (status) {
3615         case IBMVFC_MAD_SUCCESS:
3616                 tgt_dbg(tgt, "ADISC succeeded\n");
3617                 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3618                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3619                 break;
3620         case IBMVFC_MAD_DRIVER_FAILED:
3621                 break;
3622         case IBMVFC_MAD_FAILED:
3623         default:
3624                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3625                 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3626                 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
3627                 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3628                          ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
3629                          mad->iu.status, mad->iu.error,
3630                          ibmvfc_get_fc_type(fc_reason), fc_reason,
3631                          ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3632                 break;
3633         };
3634 
3635         kref_put(&tgt->kref, ibmvfc_release_tgt);
3636         ibmvfc_free_event(evt);
3637         wake_up(&vhost->work_wait_q);
3638 }
3639 
3640 /**
3641  * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3642  * @evt:                ibmvfc event struct
3643  *
3644  **/
3645 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3646 {
3647         struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3648 
3649         memset(mad, 0, sizeof(*mad));
3650         mad->common.version = cpu_to_be32(1);
3651         mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3652         mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3653         mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3654                 offsetof(struct ibmvfc_passthru_mad, iu));
3655         mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3656         mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3657         mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3658         mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3659                 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3660                 offsetof(struct ibmvfc_passthru_fc_iu, payload));
3661         mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3662         mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3663                 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3664                 offsetof(struct ibmvfc_passthru_fc_iu, response));
3665         mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
3666 }
3667 
3668 /**
3669  * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3670  * @evt:                ibmvfc event struct
3671  *
3672  * Just cleanup this event struct. Everything else is handled by
3673  * the ADISC completion handler. If the ADISC never actually comes
3674  * back, we still have the timer running on the ADISC event struct
3675  * which will fire and cause the CRQ to get reset.
3676  *
3677  **/
3678 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3679 {
3680         struct ibmvfc_host *vhost = evt->vhost;
3681         struct ibmvfc_target *tgt = evt->tgt;
3682 
3683         tgt_dbg(tgt, "ADISC cancel complete\n");
3684         vhost->abort_threads--;
3685         ibmvfc_free_event(evt);
3686         kref_put(&tgt->kref, ibmvfc_release_tgt);
3687         wake_up(&vhost->work_wait_q);
3688 }
3689 
3690 /**
3691  * ibmvfc_adisc_timeout - Handle an ADISC timeout
3692  * @tgt:                ibmvfc target struct
3693  *
3694  * If an ADISC times out, send a cancel. If the cancel times
3695  * out, reset the CRQ. When the ADISC comes back as cancelled,
3696  * log back into the target.
3697  **/
3698 static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
3699 {
3700         struct ibmvfc_host *vhost = tgt->vhost;
3701         struct ibmvfc_event *evt;
3702         struct ibmvfc_tmf *tmf;
3703         unsigned long flags;
3704         int rc;
3705 
3706         tgt_dbg(tgt, "ADISC timeout\n");
3707         spin_lock_irqsave(vhost->host->host_lock, flags);
3708         if (vhost->abort_threads >= disc_threads ||
3709             tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3710             vhost->state != IBMVFC_INITIALIZING ||
3711             vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3712                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3713                 return;
3714         }
3715 
3716         vhost->abort_threads++;
3717         kref_get(&tgt->kref);
3718         evt = ibmvfc_get_event(vhost);
3719         ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3720 
3721         evt->tgt = tgt;
3722         tmf = &evt->iu.tmf;
3723         memset(tmf, 0, sizeof(*tmf));
3724         tmf->common.version = cpu_to_be32(1);
3725         tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3726         tmf->common.length = cpu_to_be16(sizeof(*tmf));
3727         tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3728         tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
3729 
3730         rc = ibmvfc_send_event(evt, vhost, default_timeout);
3731 
3732         if (rc) {
3733                 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3734                 vhost->abort_threads--;
3735                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3736                 __ibmvfc_reset_host(vhost);
3737         } else
3738                 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3739         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3740 }
3741 
3742 /**
3743  * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3744  * @tgt:                ibmvfc target struct
3745  *
3746  * When sending an ADISC we end up with two timers running. The
3747  * first timer is the timer in the ibmvfc target struct. If this
3748  * fires, we send a cancel to the target. The second timer is the
3749  * timer on the ibmvfc event for the ADISC, which is longer. If that
3750  * fires, it means the ADISC timed out and our attempt to cancel it
3751  * also failed, so we need to reset the CRQ.
3752  **/
3753 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3754 {
3755         struct ibmvfc_passthru_mad *mad;
3756         struct ibmvfc_host *vhost = tgt->vhost;
3757         struct ibmvfc_event *evt;
3758 
3759         if (vhost->discovery_threads >= disc_threads)
3760                 return;
3761 
3762         kref_get(&tgt->kref);
3763         evt = ibmvfc_get_event(vhost);
3764         vhost->discovery_threads++;
3765         ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3766         evt->tgt = tgt;
3767 
3768         ibmvfc_init_passthru(evt);
3769         mad = &evt->iu.passthru;
3770         mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
3771         mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
3772         mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
3773 
3774         mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
3775         memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3776                sizeof(vhost->login_buf->resp.port_name));
3777         memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3778                sizeof(vhost->login_buf->resp.node_name));
3779         mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
3780 
3781         if (timer_pending(&tgt->timer))
3782                 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
3783         else {
3784                 tgt->timer.data = (unsigned long) tgt;
3785                 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
3786                 tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
3787                 add_timer(&tgt->timer);
3788         }
3789 
3790         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3791         if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
3792                 vhost->discovery_threads--;
3793                 del_timer(&tgt->timer);
3794                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3795                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3796         } else
3797                 tgt_dbg(tgt, "Sent ADISC\n");
3798 }
3799 
3800 /**
3801  * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3802  * @evt:        ibmvfc event struct
3803  *
3804  **/
3805 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3806 {
3807         struct ibmvfc_target *tgt = evt->tgt;
3808         struct ibmvfc_host *vhost = evt->vhost;
3809         struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3810         u32 status = be16_to_cpu(rsp->common.status);
3811         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3812 
3813         vhost->discovery_threads--;
3814         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3815         switch (status) {
3816         case IBMVFC_MAD_SUCCESS:
3817                 tgt_dbg(tgt, "Query Target succeeded\n");
3818                 tgt->new_scsi_id = be64_to_cpu(rsp->scsi_id);
3819                 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
3820                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3821                 else
3822                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
3823                 break;
3824         case IBMVFC_MAD_DRIVER_FAILED:
3825                 break;
3826         case IBMVFC_MAD_CRQ_ERROR:
3827                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3828                 break;
3829         case IBMVFC_MAD_FAILED:
3830         default:
3831                 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3832                     be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3833                     be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
3834                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3835                 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3836                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3837                 else
3838                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3839 
3840                 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3841                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3842                         rsp->status, rsp->error, ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)),
3843                         rsp->fc_type, ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)),
3844                         rsp->fc_explain, status);
3845                 break;
3846         };
3847 
3848         kref_put(&tgt->kref, ibmvfc_release_tgt);
3849         ibmvfc_free_event(evt);
3850         wake_up(&vhost->work_wait_q);
3851 }
3852 
3853 /**
3854  * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3855  * @tgt:        ibmvfc target struct
3856  *
3857  **/
3858 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3859 {
3860         struct ibmvfc_query_tgt *query_tgt;
3861         struct ibmvfc_host *vhost = tgt->vhost;
3862         struct ibmvfc_event *evt;
3863 
3864         if (vhost->discovery_threads >= disc_threads)
3865                 return;
3866 
3867         kref_get(&tgt->kref);
3868         evt = ibmvfc_get_event(vhost);
3869         vhost->discovery_threads++;
3870         evt->tgt = tgt;
3871         ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3872         query_tgt = &evt->iu.query_tgt;
3873         memset(query_tgt, 0, sizeof(*query_tgt));
3874         query_tgt->common.version = cpu_to_be32(1);
3875         query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
3876         query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
3877         query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
3878 
3879         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3880         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3881                 vhost->discovery_threads--;
3882                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3883                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3884         } else
3885                 tgt_dbg(tgt, "Sent Query Target\n");
3886 }
3887 
3888 /**
3889  * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3890  * @vhost:              ibmvfc host struct
3891  * @scsi_id:    SCSI ID to allocate target for
3892  *
3893  * Returns:
3894  *      0 on success / other on failure
3895  **/
3896 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3897 {
3898         struct ibmvfc_target *tgt;
3899         unsigned long flags;
3900 
3901         spin_lock_irqsave(vhost->host->host_lock, flags);
3902         list_for_each_entry(tgt, &vhost->targets, queue) {
3903                 if (tgt->scsi_id == scsi_id) {
3904                         if (tgt->need_login)
3905                                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3906                         goto unlock_out;
3907                 }
3908         }
3909         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3910 
3911         tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
3912         if (!tgt) {
3913                 dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
3914                         scsi_id);
3915                 return -ENOMEM;
3916         }
3917 
3918         memset(tgt, 0, sizeof(*tgt));
3919         tgt->scsi_id = scsi_id;
3920         tgt->new_scsi_id = scsi_id;
3921         tgt->vhost = vhost;
3922         tgt->need_login = 1;
3923         tgt->cancel_key = vhost->task_set++;
3924         init_timer(&tgt->timer);
3925         kref_init(&tgt->kref);
3926         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3927         spin_lock_irqsave(vhost->host->host_lock, flags);
3928         list_add_tail(&tgt->queue, &vhost->targets);
3929 
3930 unlock_out:
3931         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3932         return 0;
3933 }
3934 
3935 /**
3936  * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3937  * @vhost:              ibmvfc host struct
3938  *
3939  * Returns:
3940  *      0 on success / other on failure
3941  **/
3942 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3943 {
3944         int i, rc;
3945 
3946         for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3947                 rc = ibmvfc_alloc_target(vhost,
3948                                          be32_to_cpu(vhost->disc_buf->scsi_id[i]) &
3949                                          IBMVFC_DISC_TGT_SCSI_ID_MASK);
3950 
3951         return rc;
3952 }
3953 
3954 /**
3955  * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3956  * @evt:        ibmvfc event struct
3957  *
3958  **/
3959 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3960 {
3961         struct ibmvfc_host *vhost = evt->vhost;
3962         struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3963         u32 mad_status = be16_to_cpu(rsp->common.status);
3964         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3965 
3966         switch (mad_status) {
3967         case IBMVFC_MAD_SUCCESS:
3968                 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3969                 vhost->num_targets = be32_to_cpu(rsp->num_written);
3970                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3971                 break;
3972         case IBMVFC_MAD_FAILED:
3973                 level += ibmvfc_retry_host_init(vhost);
3974                 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
3975                            ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3976                            rsp->status, rsp->error);
3977                 break;
3978         case IBMVFC_MAD_DRIVER_FAILED:
3979                 break;
3980         default:
3981                 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3982                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3983                 break;
3984         }
3985 
3986         ibmvfc_free_event(evt);
3987         wake_up(&vhost->work_wait_q);
3988 }
3989 
3990 /**
3991  * ibmvfc_discover_targets - Send Discover Targets MAD
3992  * @vhost:      ibmvfc host struct
3993  *
3994  **/
3995 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3996 {
3997         struct ibmvfc_discover_targets *mad;
3998         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3999 
4000         ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4001         mad = &evt->iu.discover_targets;
4002         memset(mad, 0, sizeof(*mad));
4003         mad->common.version = cpu_to_be32(1);
4004         mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4005         mad->common.length = cpu_to_be16(sizeof(*mad));
4006         mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4007         mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4008         mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4009         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4010 
4011         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4012                 ibmvfc_dbg(vhost, "Sent discover targets\n");
4013         else
4014                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4015 }
4016 
4017 /**
4018  * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4019  * @evt:        ibmvfc event struct
4020  *
4021  **/
4022 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4023 {
4024         struct ibmvfc_host *vhost = evt->vhost;
4025         u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4026         struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4027         unsigned int npiv_max_sectors;
4028         int level = IBMVFC_DEFAULT_LOG_LEVEL;
4029 
4030         switch (mad_status) {
4031         case IBMVFC_MAD_SUCCESS:
4032                 ibmvfc_free_event(evt);
4033                 break;
4034         case IBMVFC_MAD_FAILED:
4035                 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4036                         level += ibmvfc_retry_host_init(vhost);
4037                 else
4038                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4039                 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4040                            ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4041                                                 rsp->status, rsp->error);
4042                 ibmvfc_free_event(evt);
4043                 return;
4044         case IBMVFC_MAD_CRQ_ERROR:
4045                 ibmvfc_retry_host_init(vhost);
4046         case IBMVFC_MAD_DRIVER_FAILED:
4047                 ibmvfc_free_event(evt);
4048                 return;
4049         default:
4050                 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4051                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4052                 ibmvfc_free_event(evt);
4053                 return;
4054         }
4055 
4056         vhost->client_migrated = 0;
4057 
4058         if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4059                 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4060                         rsp->flags);
4061                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4062                 wake_up(&vhost->work_wait_q);
4063                 return;
4064         }
4065 
4066         if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
4067                 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4068                         rsp->max_cmds);
4069                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4070                 wake_up(&vhost->work_wait_q);
4071                 return;
4072         }
4073 
4074         vhost->logged_in = 1;
4075         npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
4076         dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4077                  rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4078                  rsp->drc_name, npiv_max_sectors);
4079 
4080         fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4081         fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4082         fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4083         fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
4084         fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4085         fc_host_supported_classes(vhost->host) = 0;
4086         if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
4087                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4088         if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
4089                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4090         if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
4091                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4092         fc_host_maxframe_size(vhost->host) =
4093                 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
4094 
4095         vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
4096         vhost->host->max_sectors = npiv_max_sectors;
4097         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4098         wake_up(&vhost->work_wait_q);
4099 }
4100 
4101 /**
4102  * ibmvfc_npiv_login - Sends NPIV login
4103  * @vhost:      ibmvfc host struct
4104  *
4105  **/
4106 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4107 {
4108         struct ibmvfc_npiv_login_mad *mad;
4109         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4110 
4111         ibmvfc_gather_partition_info(vhost);
4112         ibmvfc_set_login_info(vhost);
4113         ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4114 
4115         memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4116         mad = &evt->iu.npiv_login;
4117         memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4118         mad->common.version = cpu_to_be32(1);
4119         mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4120         mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4121         mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4122         mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
4123 
4124         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4125 
4126         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4127                 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4128         else
4129                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4130 };
4131 
4132 /**
4133  * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4134  * @vhost:              ibmvfc host struct
4135  *
4136  **/
4137 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4138 {
4139         struct ibmvfc_host *vhost = evt->vhost;
4140         u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
4141 
4142         ibmvfc_free_event(evt);
4143 
4144         switch (mad_status) {
4145         case IBMVFC_MAD_SUCCESS:
4146                 if (list_empty(&vhost->sent) &&
4147                     vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
4148                         ibmvfc_init_host(vhost);
4149                         return;
4150                 }
4151                 break;
4152         case IBMVFC_MAD_FAILED:
4153         case IBMVFC_MAD_NOT_SUPPORTED:
4154         case IBMVFC_MAD_CRQ_ERROR:
4155         case IBMVFC_MAD_DRIVER_FAILED:
4156         default:
4157                 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4158                 break;
4159         }
4160 
4161         ibmvfc_hard_reset_host(vhost);
4162 }
4163 
4164 /**
4165  * ibmvfc_npiv_logout - Issue an NPIV Logout
4166  * @vhost:              ibmvfc host struct
4167  *
4168  **/
4169 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4170 {
4171         struct ibmvfc_npiv_logout_mad *mad;
4172         struct ibmvfc_event *evt;
4173 
4174         evt = ibmvfc_get_event(vhost);
4175         ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4176 
4177         mad = &evt->iu.npiv_logout;
4178         memset(mad, 0, sizeof(*mad));
4179         mad->common.version = cpu_to_be32(1);
4180         mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4181         mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
4182 
4183         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4184 
4185         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4186                 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4187         else
4188                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4189 }
4190 
4191 /**
4192  * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4193  * @vhost:              ibmvfc host struct
4194  *
4195  * Returns:
4196  *      1 if work to do / 0 if not
4197  **/
4198 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4199 {
4200         struct ibmvfc_target *tgt;
4201 
4202         list_for_each_entry(tgt, &vhost->targets, queue) {
4203                 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4204                     tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4205                         return 1;
4206         }
4207 
4208         return 0;
4209 }
4210 
4211 /**
4212  * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4213  * @vhost:              ibmvfc host struct
4214  *
4215  * Returns:
4216  *      1 if work to do / 0 if not
4217  **/
4218 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4219 {
4220         struct ibmvfc_target *tgt;
4221 
4222         if (kthread_should_stop())
4223                 return 1;
4224         switch (vhost->action) {
4225         case IBMVFC_HOST_ACTION_NONE:
4226         case IBMVFC_HOST_ACTION_INIT_WAIT:
4227         case IBMVFC_HOST_ACTION_LOGO_WAIT:
4228                 return 0;
4229         case IBMVFC_HOST_ACTION_TGT_INIT:
4230         case IBMVFC_HOST_ACTION_QUERY_TGTS:
4231                 if (vhost->discovery_threads == disc_threads)
4232                         return 0;
4233                 list_for_each_entry(tgt, &vhost->targets, queue)
4234                         if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4235                                 return 1;
4236                 list_for_each_entry(tgt, &vhost->targets, queue)
4237                         if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4238                                 return 0;
4239                 return 1;
4240         case IBMVFC_HOST_ACTION_LOGO:
4241         case IBMVFC_HOST_ACTION_INIT:
4242         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4243         case IBMVFC_HOST_ACTION_TGT_DEL:
4244         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4245         case IBMVFC_HOST_ACTION_QUERY:
4246         case IBMVFC_HOST_ACTION_RESET:
4247         case IBMVFC_HOST_ACTION_REENABLE:
4248         default:
4249                 break;
4250         };
4251 
4252         return 1;
4253 }
4254 
4255 /**
4256  * ibmvfc_work_to_do - Is there task level work to do?
4257  * @vhost:              ibmvfc host struct
4258  *
4259  * Returns:
4260  *      1 if work to do / 0 if not
4261  **/
4262 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4263 {
4264         unsigned long flags;
4265         int rc;
4266 
4267         spin_lock_irqsave(vhost->host->host_lock, flags);
4268         rc = __ibmvfc_work_to_do(vhost);
4269         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4270         return rc;
4271 }
4272 
4273 /**
4274  * ibmvfc_log_ae - Log async events if necessary
4275  * @vhost:              ibmvfc host struct
4276  * @events:             events to log
4277  *
4278  **/
4279 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4280 {
4281         if (events & IBMVFC_AE_RSCN)
4282                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4283         if ((events & IBMVFC_AE_LINKDOWN) &&
4284             vhost->state >= IBMVFC_HALTED)
4285                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4286         if ((events & IBMVFC_AE_LINKUP) &&
4287             vhost->state == IBMVFC_INITIALIZING)
4288                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4289 }
4290 
4291 /**
4292  * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4293  * @tgt:                ibmvfc target struct
4294  *
4295  **/
4296 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4297 {
4298         struct ibmvfc_host *vhost = tgt->vhost;
4299         struct fc_rport *rport;
4300         unsigned long flags;
4301 
4302         tgt_dbg(tgt, "Adding rport\n");
4303         rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4304         spin_lock_irqsave(vhost->host->host_lock, flags);
4305 
4306         if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4307                 tgt_dbg(tgt, "Deleting rport\n");
4308                 list_del(&tgt->queue);
4309                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4310                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4311                 fc_remote_port_delete(rport);
4312                 del_timer_sync(&tgt->timer);
4313                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4314                 return;
4315         } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4316                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4317                 return;
4318         }
4319 
4320         if (rport) {
4321                 tgt_dbg(tgt, "rport add succeeded\n");
4322                 tgt->rport = rport;
4323                 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
4324                 rport->supported_classes = 0;
4325                 tgt->target_id = rport->scsi_target_id;
4326                 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
4327                         rport->supported_classes |= FC_COS_CLASS1;
4328                 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
4329                         rport->supported_classes |= FC_COS_CLASS2;
4330                 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
4331                         rport->supported_classes |= FC_COS_CLASS3;
4332                 if (rport->rqst_q)
4333                         blk_queue_max_segments(rport->rqst_q, 1);
4334         } else
4335                 tgt_dbg(tgt, "rport add failed\n");
4336         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4337 }
4338 
4339 /**
4340  * ibmvfc_do_work - Do task level work
4341  * @vhost:              ibmvfc host struct
4342  *
4343  **/
4344 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4345 {
4346         struct ibmvfc_target *tgt;
4347         unsigned long flags;
4348         struct fc_rport *rport;
4349         int rc;
4350 
4351         ibmvfc_log_ae(vhost, vhost->events_to_log);
4352         spin_lock_irqsave(vhost->host->host_lock, flags);
4353         vhost->events_to_log = 0;
4354         switch (vhost->action) {
4355         case IBMVFC_HOST_ACTION_NONE:
4356         case IBMVFC_HOST_ACTION_LOGO_WAIT:
4357         case IBMVFC_HOST_ACTION_INIT_WAIT:
4358                 break;
4359         case IBMVFC_HOST_ACTION_RESET:
4360                 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4361                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4362                 rc = ibmvfc_reset_crq(vhost);
4363                 spin_lock_irqsave(vhost->host->host_lock, flags);
4364                 if (rc == H_CLOSED)
4365                         vio_enable_interrupts(to_vio_dev(vhost->dev));
4366                 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4367                     (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
4368                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4369                         dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4370                 }
4371                 break;
4372         case IBMVFC_HOST_ACTION_REENABLE:
4373                 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4374                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4375                 rc = ibmvfc_reenable_crq_queue(vhost);
4376                 spin_lock_irqsave(vhost->host->host_lock, flags);
4377                 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4378                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4379                         dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4380                 }
4381                 break;
4382         case IBMVFC_HOST_ACTION_LOGO:
4383                 vhost->job_step(vhost);
4384                 break;
4385         case IBMVFC_HOST_ACTION_INIT:
4386                 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
4387                 if (vhost->delay_init) {
4388                         vhost->delay_init = 0;
4389                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4390                         ssleep(15);
4391                         return;
4392                 } else
4393                         vhost->job_step(vhost);
4394                 break;
4395         case IBMVFC_HOST_ACTION_QUERY:
4396                 list_for_each_entry(tgt, &vhost->targets, queue)
4397                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4398                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4399                 break;
4400         case IBMVFC_HOST_ACTION_QUERY_TGTS:
4401                 list_for_each_entry(tgt, &vhost->targets, queue) {
4402                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4403                                 tgt->job_step(tgt);
4404                                 break;
4405                         }
4406                 }
4407 
4408                 if (!ibmvfc_dev_init_to_do(vhost))
4409                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4410                 break;
4411         case IBMVFC_HOST_ACTION_TGT_DEL:
4412         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4413                 list_for_each_entry(tgt, &vhost->targets, queue) {
4414                         if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4415                                 tgt_dbg(tgt, "Deleting rport\n");
4416                                 rport = tgt->rport;
4417                                 tgt->rport = NULL;
4418                                 list_del(&tgt->queue);
4419                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4420                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4421                                 if (rport)
4422                                         fc_remote_port_delete(rport);
4423                                 del_timer_sync(&tgt->timer);
4424                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4425                                 return;
4426                         }
4427                 }
4428 
4429                 if (vhost->state == IBMVFC_INITIALIZING) {
4430                         if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
4431                                 if (vhost->reinit) {
4432                                         vhost->reinit = 0;
4433                                         scsi_block_requests(vhost->host);
4434                                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4435                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4436                                 } else {
4437                                         ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4438                                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4439                                         wake_up(&vhost->init_wait_q);
4440                                         schedule_work(&vhost->rport_add_work_q);
4441                                         vhost->init_retries = 0;
4442                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4443                                         scsi_unblock_requests(vhost->host);
4444                                 }
4445 
4446                                 return;
4447                         } else {
4448                                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4449                                 vhost->job_step = ibmvfc_discover_targets;
4450                         }
4451                 } else {
4452                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4453                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4454                         scsi_unblock_requests(vhost->host);
4455                         wake_up(&vhost->init_wait_q);
4456                         return;
4457                 }
4458                 break;
4459         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4460                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4461                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4462                 ibmvfc_alloc_targets(vhost);
4463                 spin_lock_irqsave(vhost->host->host_lock, flags);
4464                 break;
4465         case IBMVFC_HOST_ACTION_TGT_INIT:
4466                 list_for_each_entry(tgt, &vhost->targets, queue) {
4467                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4468                                 tgt->job_step(tgt);
4469                                 break;
4470                         }
4471                 }
4472 
4473                 if (!ibmvfc_dev_init_to_do(vhost))
4474                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
4475                 break;
4476         default:
4477                 break;
4478         };
4479 
4480         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4481 }
4482 
4483 /**
4484  * ibmvfc_work - Do task level work
4485  * @data:               ibmvfc host struct
4486  *
4487  * Returns:
4488  *      zero
4489  **/
4490 static int ibmvfc_work(void *data)
4491 {
4492         struct ibmvfc_host *vhost = data;
4493         int rc;
4494 
4495         set_user_nice(current, MIN_NICE);
4496 
4497         while (1) {
4498                 rc = wait_event_interruptible(vhost->work_wait_q,
4499                                               ibmvfc_work_to_do(vhost));
4500 
4501                 BUG_ON(rc);
4502 
4503                 if (kthread_should_stop())
4504                         break;
4505 
4506                 ibmvfc_do_work(vhost);
4507         }
4508 
4509         ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4510         return 0;
4511 }
4512 
4513 /**
4514  * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4515  * @vhost:      ibmvfc host struct
4516  *
4517  * Allocates a page for messages, maps it for dma, and registers
4518  * the crq with the hypervisor.
4519  *
4520  * Return value:
4521  *      zero on success / other on failure
4522  **/
4523 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4524 {
4525         int rc, retrc = -ENOMEM;
4526         struct device *dev = vhost->dev;
4527         struct vio_dev *vdev = to_vio_dev(dev);
4528         struct ibmvfc_crq_queue *crq = &vhost->crq;
4529 
4530         ENTER;
4531         crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4532 
4533         if (!crq->msgs)
4534                 return -ENOMEM;
4535 
4536         crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4537         crq->msg_token = dma_map_single(dev, crq->msgs,
4538                                         PAGE_SIZE, DMA_BIDIRECTIONAL);
4539 
4540         if (dma_mapping_error(dev, crq->msg_token))
4541                 goto map_failed;
4542 
4543         retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4544                                         crq->msg_token, PAGE_SIZE);
4545 
4546         if (rc == H_RESOURCE)
4547                 /* maybe kexecing and resource is busy. try a reset */
4548                 retrc = rc = ibmvfc_reset_crq(vhost);
4549 
4550         if (rc == H_CLOSED)
4551                 dev_warn(dev, "Partner adapter not ready\n");
4552         else if (rc) {
4553                 dev_warn(dev, "Error %d opening adapter\n", rc);
4554                 goto reg_crq_failed;
4555         }
4556 
4557         retrc = 0;
4558 
4559         tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4560 
4561         if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4562                 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4563                 goto req_irq_failed;
4564         }
4565 
4566         if ((rc = vio_enable_interrupts(vdev))) {
4567                 dev_err(dev, "Error %d enabling interrupts\n", rc);
4568                 goto req_irq_failed;
4569         }
4570 
4571         crq->cur = 0;
4572         LEAVE;
4573         return retrc;
4574 
4575 req_irq_failed:
4576         tasklet_kill(&vhost->tasklet);
4577         do {
4578                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4579         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4580 reg_crq_failed:
4581         dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4582 map_failed:
4583         free_page((unsigned long)crq->msgs);
4584         return retrc;
4585 }
4586 
4587 /**
4588  * ibmvfc_free_mem - Free memory for vhost
4589  * @vhost:      ibmvfc host struct
4590  *
4591  * Return value:
4592  *      none
4593  **/
4594 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4595 {
4596         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4597 
4598         ENTER;
4599         mempool_destroy(vhost->tgt_pool);
4600         kfree(vhost->trace);
4601         dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4602                           vhost->disc_buf_dma);
4603         dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4604                           vhost->login_buf, vhost->login_buf_dma);
4605         dma_pool_destroy(vhost->sg_pool);
4606         dma_unmap_single(vhost->dev, async_q->msg_token,
4607                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4608         free_page((unsigned long)async_q->msgs);
4609         LEAVE;
4610 }
4611 
4612 /**
4613  * ibmvfc_alloc_mem - Allocate memory for vhost
4614  * @vhost:      ibmvfc host struct
4615  *
4616  * Return value:
4617  *      0 on success / non-zero on failure
4618  **/
4619 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4620 {
4621         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4622         struct device *dev = vhost->dev;
4623 
4624         ENTER;
4625         async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4626         if (!async_q->msgs) {
4627                 dev_err(dev, "Couldn't allocate async queue.\n");
4628                 goto nomem;
4629         }
4630 
4631         async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4632         async_q->msg_token = dma_map_single(dev, async_q->msgs,
4633                                             async_q->size * sizeof(*async_q->msgs),
4634                                             DMA_BIDIRECTIONAL);
4635 
4636         if (dma_mapping_error(dev, async_q->msg_token)) {
4637                 dev_err(dev, "Failed to map async queue\n");
4638                 goto free_async_crq;
4639         }
4640 
4641         vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4642                                          SG_ALL * sizeof(struct srp_direct_buf),
4643                                          sizeof(struct srp_direct_buf), 0);
4644 
4645         if (!vhost->sg_pool) {
4646                 dev_err(dev, "Failed to allocate sg pool\n");
4647                 goto unmap_async_crq;
4648         }
4649 
4650         vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4651                                               &vhost->login_buf_dma, GFP_KERNEL);
4652 
4653         if (!vhost->login_buf) {
4654                 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4655                 goto free_sg_pool;
4656         }
4657 
4658         vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
4659         vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
4660                                              &vhost->disc_buf_dma, GFP_KERNEL);
4661 
4662         if (!vhost->disc_buf) {
4663                 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
4664                 goto free_login_buffer;
4665         }
4666 
4667         vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
4668                                sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
4669 
4670         if (!vhost->trace)
4671                 goto free_disc_buffer;
4672 
4673         vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
4674                                                       sizeof(struct ibmvfc_target));
4675 
4676         if (!vhost->tgt_pool) {
4677                 dev_err(dev, "Couldn't allocate target memory pool\n");
4678                 goto free_trace;
4679         }
4680 
4681         LEAVE;
4682         return 0;
4683 
4684 free_trace:
4685         kfree(vhost->trace);
4686 free_disc_buffer:
4687         dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
4688                           vhost->disc_buf_dma);
4689 free_login_buffer:
4690         dma_free_coherent(dev, sizeof(*vhost->login_buf),
4691                           vhost->login_buf, vhost->login_buf_dma);
4692 free_sg_pool:
4693         dma_pool_destroy(vhost->sg_pool);
4694 unmap_async_crq:
4695         dma_unmap_single(dev, async_q->msg_token,
4696                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4697 free_async_crq:
4698         free_page((unsigned long)async_q->msgs);
4699 nomem:
4700         LEAVE;
4701         return -ENOMEM;
4702 }
4703 
4704 /**
4705  * ibmvfc_rport_add_thread - Worker thread for rport adds
4706  * @work:       work struct
4707  *
4708  **/
4709 static void ibmvfc_rport_add_thread(struct work_struct *work)
4710 {
4711         struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
4712                                                  rport_add_work_q);
4713         struct ibmvfc_target *tgt;
4714         struct fc_rport *rport;
4715         unsigned long flags;
4716         int did_work;
4717 
4718         ENTER;
4719         spin_lock_irqsave(vhost->host->host_lock, flags);
4720         do {
4721                 did_work = 0;
4722                 if (vhost->state != IBMVFC_ACTIVE)
4723                         break;
4724 
4725                 list_for_each_entry(tgt, &vhost->targets, queue) {
4726                         if (tgt->add_rport) {
4727                                 did_work = 1;
4728                                 tgt->add_rport = 0;
4729                                 kref_get(&tgt->kref);
4730                                 rport = tgt->rport;
4731                                 if (!rport) {
4732                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4733                                         ibmvfc_tgt_add_rport(tgt);
4734                                 } else if (get_device(&rport->dev)) {
4735                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4736                                         tgt_dbg(tgt, "Setting rport roles\n");
4737                                         fc_remote_port_rolechg(rport, tgt->ids.roles);
4738                                         put_device(&rport->dev);
4739                                 } else {
4740                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4741                                 }
4742 
4743                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4744                                 spin_lock_irqsave(vhost->host->host_lock, flags);
4745                                 break;
4746                         }
4747                 }
4748         } while(did_work);
4749 
4750         if (vhost->state == IBMVFC_ACTIVE)
4751                 vhost->scan_complete = 1;
4752         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4753         LEAVE;
4754 }
4755 
4756 /**
4757  * ibmvfc_probe - Adapter hot plug add entry point
4758  * @vdev:       vio device struct
4759  * @id: vio device id struct
4760  *
4761  * Return value:
4762  *      0 on success / non-zero on failure
4763  **/
4764 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4765 {
4766         struct ibmvfc_host *vhost;
4767         struct Scsi_Host *shost;
4768         struct device *dev = &vdev->dev;
4769         int rc = -ENOMEM;
4770 
4771         ENTER;
4772         shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
4773         if (!shost) {
4774                 dev_err(dev, "Couldn't allocate host data\n");
4775                 goto out;
4776         }
4777 
4778         shost->transportt = ibmvfc_transport_template;
4779         shost->can_queue = max_requests;
4780         shost->max_lun = max_lun;
4781         shost->max_id = max_targets;
4782         shost->max_sectors = IBMVFC_MAX_SECTORS;
4783         shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
4784         shost->unique_id = shost->host_no;
4785 
4786         vhost = shost_priv(shost);
4787         INIT_LIST_HEAD(&vhost->sent);
4788         INIT_LIST_HEAD(&vhost->free);
4789         INIT_LIST_HEAD(&vhost->targets);
4790         sprintf(vhost->name, IBMVFC_NAME);
4791         vhost->host = shost;
4792         vhost->dev = dev;
4793         vhost->partition_number = -1;
4794         vhost->log_level = log_level;
4795         vhost->task_set = 1;
4796         strcpy(vhost->partition_name, "UNKNOWN");
4797         init_waitqueue_head(&vhost->work_wait_q);
4798         init_waitqueue_head(&vhost->init_wait_q);
4799         INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
4800         mutex_init(&vhost->passthru_mutex);
4801 
4802         if ((rc = ibmvfc_alloc_mem(vhost)))
4803                 goto free_scsi_host;
4804 
4805         vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
4806                                          shost->host_no);
4807 
4808         if (IS_ERR(vhost->work_thread)) {
4809                 dev_err(dev, "Couldn't create kernel thread: %ld\n",
4810                         PTR_ERR(vhost->work_thread));
4811                 goto free_host_mem;
4812         }
4813 
4814         if ((rc = ibmvfc_init_crq(vhost))) {
4815                 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4816                 goto kill_kthread;
4817         }
4818 
4819         if ((rc = ibmvfc_init_event_pool(vhost))) {
4820                 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4821                 goto release_crq;
4822         }
4823 
4824         if ((rc = scsi_add_host(shost, dev)))
4825                 goto release_event_pool;
4826 
4827         fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
4828 
4829         if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4830                                            &ibmvfc_trace_attr))) {
4831                 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4832                 goto remove_shost;
4833         }
4834 
4835         if (shost_to_fc_host(shost)->rqst_q)
4836                 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
4837         dev_set_drvdata(dev, vhost);
4838         spin_lock(&ibmvfc_driver_lock);
4839         list_add_tail(&vhost->queue, &ibmvfc_head);
4840         spin_unlock(&ibmvfc_driver_lock);
4841 
4842         ibmvfc_send_crq_init(vhost);
4843         scsi_scan_host(shost);
4844         return 0;
4845 
4846 remove_shost:
4847         scsi_remove_host(shost);
4848 release_event_pool:
4849         ibmvfc_free_event_pool(vhost);
4850 release_crq:
4851         ibmvfc_release_crq_queue(vhost);
4852 kill_kthread:
4853         kthread_stop(vhost->work_thread);
4854 free_host_mem:
4855         ibmvfc_free_mem(vhost);
4856 free_scsi_host:
4857         scsi_host_put(shost);
4858 out:
4859         LEAVE;
4860         return rc;
4861 }
4862 
4863 /**
4864  * ibmvfc_remove - Adapter hot plug remove entry point
4865  * @vdev:       vio device struct
4866  *
4867  * Return value:
4868  *      0
4869  **/
4870 static int ibmvfc_remove(struct vio_dev *vdev)
4871 {
4872         struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4873         unsigned long flags;
4874 
4875         ENTER;
4876         ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
4877 
4878         spin_lock_irqsave(vhost->host->host_lock, flags);
4879         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
4880         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4881 
4882         ibmvfc_wait_while_resetting(vhost);
4883         ibmvfc_release_crq_queue(vhost);
4884         kthread_stop(vhost->work_thread);
4885         fc_remove_host(vhost->host);
4886         scsi_remove_host(vhost->host);
4887 
4888         spin_lock_irqsave(vhost->host->host_lock, flags);
4889         ibmvfc_purge_requests(vhost, DID_ERROR);
4890         ibmvfc_free_event_pool(vhost);
4891         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4892 
4893         ibmvfc_free_mem(vhost);
4894         spin_lock(&ibmvfc_driver_lock);
4895         list_del(&vhost->queue);
4896         spin_unlock(&ibmvfc_driver_lock);
4897         scsi_host_put(vhost->host);
4898         LEAVE;
4899         return 0;
4900 }
4901 
4902 /**
4903  * ibmvfc_resume - Resume from suspend
4904  * @dev:        device struct
4905  *
4906  * We may have lost an interrupt across suspend/resume, so kick the
4907  * interrupt handler
4908  *
4909  */
4910 static int ibmvfc_resume(struct device *dev)
4911 {
4912         unsigned long flags;
4913         struct ibmvfc_host *vhost = dev_get_drvdata(dev);
4914         struct vio_dev *vdev = to_vio_dev(dev);
4915 
4916         spin_lock_irqsave(vhost->host->host_lock, flags);
4917         vio_disable_interrupts(vdev);
4918         tasklet_schedule(&vhost->tasklet);
4919         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4920         return 0;
4921 }
4922 
4923 /**
4924  * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4925  * @vdev:       vio device struct
4926  *
4927  * Return value:
4928  *      Number of bytes the driver will need to DMA map at the same time in
4929  *      order to perform well.
4930  */
4931 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4932 {
4933         unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4934         return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4935 }
4936 
4937 static struct vio_device_id ibmvfc_device_table[] = {
4938         {"fcp", "IBM,vfc-client"},
4939         { "", "" }
4940 };
4941 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4942 
4943 static struct dev_pm_ops ibmvfc_pm_ops = {
4944         .resume = ibmvfc_resume
4945 };
4946 
4947 static struct vio_driver ibmvfc_driver = {
4948         .id_table = ibmvfc_device_table,
4949         .probe = ibmvfc_probe,
4950         .remove = ibmvfc_remove,
4951         .get_desired_dma = ibmvfc_get_desired_dma,
4952         .name = IBMVFC_NAME,
4953         .pm = &ibmvfc_pm_ops,
4954 };
4955 
4956 static struct fc_function_template ibmvfc_transport_functions = {
4957         .show_host_fabric_name = 1,
4958         .show_host_node_name = 1,
4959         .show_host_port_name = 1,
4960         .show_host_supported_classes = 1,
4961         .show_host_port_type = 1,
4962         .show_host_port_id = 1,
4963         .show_host_maxframe_size = 1,
4964 
4965         .get_host_port_state = ibmvfc_get_host_port_state,
4966         .show_host_port_state = 1,
4967 
4968         .get_host_speed = ibmvfc_get_host_speed,
4969         .show_host_speed = 1,
4970 
4971         .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4972         .terminate_rport_io = ibmvfc_terminate_rport_io,
4973 
4974         .show_rport_maxframe_size = 1,
4975         .show_rport_supported_classes = 1,
4976 
4977         .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4978         .show_rport_dev_loss_tmo = 1,
4979 
4980         .get_starget_node_name = ibmvfc_get_starget_node_name,
4981         .show_starget_node_name = 1,
4982 
4983         .get_starget_port_name = ibmvfc_get_starget_port_name,
4984         .show_starget_port_name = 1,
4985 
4986         .get_starget_port_id = ibmvfc_get_starget_port_id,
4987         .show_starget_port_id = 1,
4988 
4989         .bsg_request = ibmvfc_bsg_request,
4990         .bsg_timeout = ibmvfc_bsg_timeout,
4991 };
4992 
4993 /**
4994  * ibmvfc_module_init - Initialize the ibmvfc module
4995  *
4996  * Return value:
4997  *      0 on success / other on failure
4998  **/
4999 static int __init ibmvfc_module_init(void)
5000 {
5001         int rc;
5002 
5003         if (!firmware_has_feature(FW_FEATURE_VIO))
5004                 return -ENODEV;
5005 
5006         printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
5007                IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
5008 
5009         ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
5010         if (!ibmvfc_transport_template)
5011                 return -ENOMEM;
5012 
5013         rc = vio_register_driver(&ibmvfc_driver);
5014         if (rc)
5015                 fc_release_transport(ibmvfc_transport_template);
5016         return rc;
5017 }
5018 
5019 /**
5020  * ibmvfc_module_exit - Teardown the ibmvfc module
5021  *
5022  * Return value:
5023  *      nothing
5024  **/
5025 static void __exit ibmvfc_module_exit(void)
5026 {
5027         vio_unregister_driver(&ibmvfc_driver);
5028         fc_release_transport(ibmvfc_transport_template);
5029 }
5030 
5031 module_init(ibmvfc_module_init);
5032 module_exit(ibmvfc_module_exit);
5033 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us