OSDN Git Service

Merge tag 'kvmarm-fixes-for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[uclinux-h8/linux.git] / drivers / scsi / ibmvscsi / ibmvfc.c
1 /*
2  * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
3  *
4  * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) IBM Corporation, 2008
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmapool.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/kthread.h>
31 #include <linux/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:               struct 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 timer_list *t)
1397 {
1398         struct ibmvfc_event *evt = from_timer(evt, t, timer);
1399         struct ibmvfc_host *vhost = evt->vhost;
1400         dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1401         ibmvfc_reset_host(vhost);
1402 }
1403
1404 /**
1405  * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1406  * @evt:                event to be sent
1407  * @vhost:              ibmvfc host struct
1408  * @timeout:    timeout in seconds - 0 means do not time command
1409  *
1410  * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1411  **/
1412 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1413                              struct ibmvfc_host *vhost, unsigned long timeout)
1414 {
1415         __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1416         int rc;
1417
1418         /* Copy the IU into the transfer area */
1419         *evt->xfer_iu = evt->iu;
1420         if (evt->crq.format == IBMVFC_CMD_FORMAT)
1421                 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1422         else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1423                 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1424         else
1425                 BUG();
1426
1427         list_add_tail(&evt->queue, &vhost->sent);
1428         timer_setup(&evt->timer, ibmvfc_timeout, 0);
1429
1430         if (timeout) {
1431                 evt->timer.expires = jiffies + (timeout * HZ);
1432                 add_timer(&evt->timer);
1433         }
1434
1435         mb();
1436
1437         if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1438                                   be64_to_cpu(crq_as_u64[1])))) {
1439                 list_del(&evt->queue);
1440                 del_timer(&evt->timer);
1441
1442                 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1443                  * Firmware will send a CRQ with a transport event (0xFF) to
1444                  * tell this client what has happened to the transport. This
1445                  * will be handled in ibmvfc_handle_crq()
1446                  */
1447                 if (rc == H_CLOSED) {
1448                         if (printk_ratelimit())
1449                                 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1450                         if (evt->cmnd)
1451                                 scsi_dma_unmap(evt->cmnd);
1452                         ibmvfc_free_event(evt);
1453                         return SCSI_MLQUEUE_HOST_BUSY;
1454                 }
1455
1456                 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1457                 if (evt->cmnd) {
1458                         evt->cmnd->result = DID_ERROR << 16;
1459                         evt->done = ibmvfc_scsi_eh_done;
1460                 } else
1461                         evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1462
1463                 evt->done(evt);
1464         } else
1465                 ibmvfc_trc_start(evt);
1466
1467         return 0;
1468 }
1469
1470 /**
1471  * ibmvfc_log_error - Log an error for the failed command if appropriate
1472  * @evt:        ibmvfc event to log
1473  *
1474  **/
1475 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1476 {
1477         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1478         struct ibmvfc_host *vhost = evt->vhost;
1479         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1480         struct scsi_cmnd *cmnd = evt->cmnd;
1481         const char *err = unknown_error;
1482         int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1483         int logerr = 0;
1484         int rsp_code = 0;
1485
1486         if (index >= 0) {
1487                 logerr = cmd_status[index].log;
1488                 err = cmd_status[index].name;
1489         }
1490
1491         if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1492                 return;
1493
1494         if (rsp->flags & FCP_RSP_LEN_VALID)
1495                 rsp_code = rsp->data.info.rsp_code;
1496
1497         scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1498                     "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1499                     cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1500                     rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1501 }
1502
1503 /**
1504  * ibmvfc_relogin - Log back into the specified device
1505  * @sdev:       scsi device struct
1506  *
1507  **/
1508 static void ibmvfc_relogin(struct scsi_device *sdev)
1509 {
1510         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1511         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1512         struct ibmvfc_target *tgt;
1513
1514         list_for_each_entry(tgt, &vhost->targets, queue) {
1515                 if (rport == tgt->rport) {
1516                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
1517                         break;
1518                 }
1519         }
1520
1521         ibmvfc_reinit_host(vhost);
1522 }
1523
1524 /**
1525  * ibmvfc_scsi_done - Handle responses from commands
1526  * @evt:        ibmvfc event to be handled
1527  *
1528  * Used as a callback when sending scsi cmds.
1529  **/
1530 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1531 {
1532         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1533         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1534         struct scsi_cmnd *cmnd = evt->cmnd;
1535         u32 rsp_len = 0;
1536         u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1537
1538         if (cmnd) {
1539                 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1540                         scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1541                 else if (rsp->flags & FCP_RESID_UNDER)
1542                         scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1543                 else
1544                         scsi_set_resid(cmnd, 0);
1545
1546                 if (vfc_cmd->status) {
1547                         cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1548
1549                         if (rsp->flags & FCP_RSP_LEN_VALID)
1550                                 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1551                         if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1552                                 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1553                         if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1554                                 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1555                         if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1556                             (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1557                                 ibmvfc_relogin(cmnd->device);
1558
1559                         if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1560                                 cmnd->result = (DID_ERROR << 16);
1561
1562                         ibmvfc_log_error(evt);
1563                 }
1564
1565                 if (!cmnd->result &&
1566                     (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1567                         cmnd->result = (DID_ERROR << 16);
1568
1569                 scsi_dma_unmap(cmnd);
1570                 cmnd->scsi_done(cmnd);
1571         }
1572
1573         if (evt->eh_comp)
1574                 complete(evt->eh_comp);
1575
1576         ibmvfc_free_event(evt);
1577 }
1578
1579 /**
1580  * ibmvfc_host_chkready - Check if the host can accept commands
1581  * @vhost:       struct ibmvfc host
1582  *
1583  * Returns:
1584  *      1 if host can accept command / 0 if not
1585  **/
1586 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1587 {
1588         int result = 0;
1589
1590         switch (vhost->state) {
1591         case IBMVFC_LINK_DEAD:
1592         case IBMVFC_HOST_OFFLINE:
1593                 result = DID_NO_CONNECT << 16;
1594                 break;
1595         case IBMVFC_NO_CRQ:
1596         case IBMVFC_INITIALIZING:
1597         case IBMVFC_HALTED:
1598         case IBMVFC_LINK_DOWN:
1599                 result = DID_REQUEUE << 16;
1600                 break;
1601         case IBMVFC_ACTIVE:
1602                 result = 0;
1603                 break;
1604         }
1605
1606         return result;
1607 }
1608
1609 /**
1610  * ibmvfc_queuecommand - The queuecommand function of the scsi template
1611  * @cmnd:       struct scsi_cmnd to be executed
1612  * @done:       Callback function to be called when cmnd is completed
1613  *
1614  * Returns:
1615  *      0 on success / other on failure
1616  **/
1617 static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
1618                                void (*done) (struct scsi_cmnd *))
1619 {
1620         struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1621         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1622         struct ibmvfc_cmd *vfc_cmd;
1623         struct ibmvfc_event *evt;
1624         int rc;
1625
1626         if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1627             unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1628                 cmnd->result = rc;
1629                 done(cmnd);
1630                 return 0;
1631         }
1632
1633         cmnd->result = (DID_OK << 16);
1634         evt = ibmvfc_get_event(vhost);
1635         ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1636         evt->cmnd = cmnd;
1637         cmnd->scsi_done = done;
1638         vfc_cmd = &evt->iu.cmd;
1639         memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1640         vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1641         vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1642         vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1643         vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
1644         vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1645         vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
1646         vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1647         vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1648         int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1649         memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1650
1651         if (cmnd->flags & SCMD_TAGGED) {
1652                 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1653                 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1654         }
1655
1656         if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1657                 return ibmvfc_send_event(evt, vhost, 0);
1658
1659         ibmvfc_free_event(evt);
1660         if (rc == -ENOMEM)
1661                 return SCSI_MLQUEUE_HOST_BUSY;
1662
1663         if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1664                 scmd_printk(KERN_ERR, cmnd,
1665                             "Failed to map DMA buffer for command. rc=%d\n", rc);
1666
1667         cmnd->result = DID_ERROR << 16;
1668         done(cmnd);
1669         return 0;
1670 }
1671
1672 static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1673
1674 /**
1675  * ibmvfc_sync_completion - Signal that a synchronous command has completed
1676  * @evt:        ibmvfc event struct
1677  *
1678  **/
1679 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1680 {
1681         /* copy the response back */
1682         if (evt->sync_iu)
1683                 *evt->sync_iu = *evt->xfer_iu;
1684
1685         complete(&evt->comp);
1686 }
1687
1688 /**
1689  * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1690  * @evt:        struct ibmvfc_event
1691  *
1692  **/
1693 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1694 {
1695         struct ibmvfc_host *vhost = evt->vhost;
1696
1697         ibmvfc_free_event(evt);
1698         vhost->aborting_passthru = 0;
1699         dev_info(vhost->dev, "Passthru command cancelled\n");
1700 }
1701
1702 /**
1703  * ibmvfc_bsg_timeout - Handle a BSG timeout
1704  * @job:        struct bsg_job that timed out
1705  *
1706  * Returns:
1707  *      0 on success / other on failure
1708  **/
1709 static int ibmvfc_bsg_timeout(struct bsg_job *job)
1710 {
1711         struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1712         unsigned long port_id = (unsigned long)job->dd_data;
1713         struct ibmvfc_event *evt;
1714         struct ibmvfc_tmf *tmf;
1715         unsigned long flags;
1716         int rc;
1717
1718         ENTER;
1719         spin_lock_irqsave(vhost->host->host_lock, flags);
1720         if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
1721                 __ibmvfc_reset_host(vhost);
1722                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1723                 return 0;
1724         }
1725
1726         vhost->aborting_passthru = 1;
1727         evt = ibmvfc_get_event(vhost);
1728         ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
1729
1730         tmf = &evt->iu.tmf;
1731         memset(tmf, 0, sizeof(*tmf));
1732         tmf->common.version = cpu_to_be32(1);
1733         tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
1734         tmf->common.length = cpu_to_be16(sizeof(*tmf));
1735         tmf->scsi_id = cpu_to_be64(port_id);
1736         tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1737         tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
1738         rc = ibmvfc_send_event(evt, vhost, default_timeout);
1739
1740         if (rc != 0) {
1741                 vhost->aborting_passthru = 0;
1742                 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
1743                 rc = -EIO;
1744         } else
1745                 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
1746                          port_id);
1747
1748         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1749
1750         LEAVE;
1751         return rc;
1752 }
1753
1754 /**
1755  * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1756  * @vhost:              struct ibmvfc_host to send command
1757  * @port_id:    port ID to send command
1758  *
1759  * Returns:
1760  *      0 on success / other on failure
1761  **/
1762 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
1763 {
1764         struct ibmvfc_port_login *plogi;
1765         struct ibmvfc_target *tgt;
1766         struct ibmvfc_event *evt;
1767         union ibmvfc_iu rsp_iu;
1768         unsigned long flags;
1769         int rc = 0, issue_login = 1;
1770
1771         ENTER;
1772         spin_lock_irqsave(vhost->host->host_lock, flags);
1773         list_for_each_entry(tgt, &vhost->targets, queue) {
1774                 if (tgt->scsi_id == port_id) {
1775                         issue_login = 0;
1776                         break;
1777                 }
1778         }
1779
1780         if (!issue_login)
1781                 goto unlock_out;
1782         if (unlikely((rc = ibmvfc_host_chkready(vhost))))
1783                 goto unlock_out;
1784
1785         evt = ibmvfc_get_event(vhost);
1786         ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1787         plogi = &evt->iu.plogi;
1788         memset(plogi, 0, sizeof(*plogi));
1789         plogi->common.version = cpu_to_be32(1);
1790         plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
1791         plogi->common.length = cpu_to_be16(sizeof(*plogi));
1792         plogi->scsi_id = cpu_to_be64(port_id);
1793         evt->sync_iu = &rsp_iu;
1794         init_completion(&evt->comp);
1795
1796         rc = ibmvfc_send_event(evt, vhost, default_timeout);
1797         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1798
1799         if (rc)
1800                 return -EIO;
1801
1802         wait_for_completion(&evt->comp);
1803
1804         if (rsp_iu.plogi.common.status)
1805                 rc = -EIO;
1806
1807         spin_lock_irqsave(vhost->host->host_lock, flags);
1808         ibmvfc_free_event(evt);
1809 unlock_out:
1810         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1811         LEAVE;
1812         return rc;
1813 }
1814
1815 /**
1816  * ibmvfc_bsg_request - Handle a BSG request
1817  * @job:        struct bsg_job to be executed
1818  *
1819  * Returns:
1820  *      0 on success / other on failure
1821  **/
1822 static int ibmvfc_bsg_request(struct bsg_job *job)
1823 {
1824         struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1825         struct fc_rport *rport = fc_bsg_to_rport(job);
1826         struct ibmvfc_passthru_mad *mad;
1827         struct ibmvfc_event *evt;
1828         union ibmvfc_iu rsp_iu;
1829         unsigned long flags, port_id = -1;
1830         struct fc_bsg_request *bsg_request = job->request;
1831         struct fc_bsg_reply *bsg_reply = job->reply;
1832         unsigned int code = bsg_request->msgcode;
1833         int rc = 0, req_seg, rsp_seg, issue_login = 0;
1834         u32 fc_flags, rsp_len;
1835
1836         ENTER;
1837         bsg_reply->reply_payload_rcv_len = 0;
1838         if (rport)
1839                 port_id = rport->port_id;
1840
1841         switch (code) {
1842         case FC_BSG_HST_ELS_NOLOGIN:
1843                 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
1844                         (bsg_request->rqst_data.h_els.port_id[1] << 8) |
1845                         bsg_request->rqst_data.h_els.port_id[2];
1846         case FC_BSG_RPT_ELS:
1847                 fc_flags = IBMVFC_FC_ELS;
1848                 break;
1849         case FC_BSG_HST_CT:
1850                 issue_login = 1;
1851                 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
1852                         (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
1853                         bsg_request->rqst_data.h_ct.port_id[2];
1854         case FC_BSG_RPT_CT:
1855                 fc_flags = IBMVFC_FC_CT_IU;
1856                 break;
1857         default:
1858                 return -ENOTSUPP;
1859         }
1860
1861         if (port_id == -1)
1862                 return -EINVAL;
1863         if (!mutex_trylock(&vhost->passthru_mutex))
1864                 return -EBUSY;
1865
1866         job->dd_data = (void *)port_id;
1867         req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
1868                              job->request_payload.sg_cnt, DMA_TO_DEVICE);
1869
1870         if (!req_seg) {
1871                 mutex_unlock(&vhost->passthru_mutex);
1872                 return -ENOMEM;
1873         }
1874
1875         rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
1876                              job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1877
1878         if (!rsp_seg) {
1879                 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1880                              job->request_payload.sg_cnt, DMA_TO_DEVICE);
1881                 mutex_unlock(&vhost->passthru_mutex);
1882                 return -ENOMEM;
1883         }
1884
1885         if (req_seg > 1 || rsp_seg > 1) {
1886                 rc = -EINVAL;
1887                 goto out;
1888         }
1889
1890         if (issue_login)
1891                 rc = ibmvfc_bsg_plogi(vhost, port_id);
1892
1893         spin_lock_irqsave(vhost->host->host_lock, flags);
1894
1895         if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
1896             unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1897                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1898                 goto out;
1899         }
1900
1901         evt = ibmvfc_get_event(vhost);
1902         ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1903         mad = &evt->iu.passthru;
1904
1905         memset(mad, 0, sizeof(*mad));
1906         mad->common.version = cpu_to_be32(1);
1907         mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
1908         mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
1909
1910         mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
1911                 offsetof(struct ibmvfc_passthru_mad, iu));
1912         mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
1913
1914         mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
1915         mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
1916         mad->iu.flags = cpu_to_be32(fc_flags);
1917         mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1918
1919         mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
1920         mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
1921         mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
1922         mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
1923         mad->iu.scsi_id = cpu_to_be64(port_id);
1924         mad->iu.tag = cpu_to_be64((u64)evt);
1925         rsp_len = be32_to_cpu(mad->iu.rsp.len);
1926
1927         evt->sync_iu = &rsp_iu;
1928         init_completion(&evt->comp);
1929         rc = ibmvfc_send_event(evt, vhost, 0);
1930         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1931
1932         if (rc) {
1933                 rc = -EIO;
1934                 goto out;
1935         }
1936
1937         wait_for_completion(&evt->comp);
1938
1939         if (rsp_iu.passthru.common.status)
1940                 rc = -EIO;
1941         else
1942                 bsg_reply->reply_payload_rcv_len = rsp_len;
1943
1944         spin_lock_irqsave(vhost->host->host_lock, flags);
1945         ibmvfc_free_event(evt);
1946         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1947         bsg_reply->result = rc;
1948         bsg_job_done(job, bsg_reply->result,
1949                        bsg_reply->reply_payload_rcv_len);
1950         rc = 0;
1951 out:
1952         dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1953                      job->request_payload.sg_cnt, DMA_TO_DEVICE);
1954         dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
1955                      job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1956         mutex_unlock(&vhost->passthru_mutex);
1957         LEAVE;
1958         return rc;
1959 }
1960
1961 /**
1962  * ibmvfc_reset_device - Reset the device with the specified reset type
1963  * @sdev:       scsi device to reset
1964  * @type:       reset type
1965  * @desc:       reset type description for log messages
1966  *
1967  * Returns:
1968  *      0 on success / other on failure
1969  **/
1970 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1971 {
1972         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1973         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1974         struct ibmvfc_cmd *tmf;
1975         struct ibmvfc_event *evt = NULL;
1976         union ibmvfc_iu rsp_iu;
1977         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1978         int rsp_rc = -EBUSY;
1979         unsigned long flags;
1980         int rsp_code = 0;
1981
1982         spin_lock_irqsave(vhost->host->host_lock, flags);
1983         if (vhost->state == IBMVFC_ACTIVE) {
1984                 evt = ibmvfc_get_event(vhost);
1985                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1986
1987                 tmf = &evt->iu.cmd;
1988                 memset(tmf, 0, sizeof(*tmf));
1989                 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1990                 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
1991                 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1992                 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
1993                 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
1994                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
1995                 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
1996                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1997                 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
1998                 tmf->iu.tmf_flags = type;
1999                 evt->sync_iu = &rsp_iu;
2000
2001                 init_completion(&evt->comp);
2002                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2003         }
2004         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2005
2006         if (rsp_rc != 0) {
2007                 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2008                             desc, rsp_rc);
2009                 return -EIO;
2010         }
2011
2012         sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2013         wait_for_completion(&evt->comp);
2014
2015         if (rsp_iu.cmd.status)
2016                 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2017
2018         if (rsp_code) {
2019                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2020                         rsp_code = fc_rsp->data.info.rsp_code;
2021
2022                 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2023                             "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2024                             ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2025                             rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
2026                             fc_rsp->scsi_status);
2027                 rsp_rc = -EIO;
2028         } else
2029                 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2030
2031         spin_lock_irqsave(vhost->host->host_lock, flags);
2032         ibmvfc_free_event(evt);
2033         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2034         return rsp_rc;
2035 }
2036
2037 /**
2038  * ibmvfc_match_rport - Match function for specified remote port
2039  * @evt:        ibmvfc event struct
2040  * @device:     device to match (rport)
2041  *
2042  * Returns:
2043  *      1 if event matches rport / 0 if event does not match rport
2044  **/
2045 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2046 {
2047         struct fc_rport *cmd_rport;
2048
2049         if (evt->cmnd) {
2050                 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2051                 if (cmd_rport == rport)
2052                         return 1;
2053         }
2054         return 0;
2055 }
2056
2057 /**
2058  * ibmvfc_match_target - Match function for specified target
2059  * @evt:        ibmvfc event struct
2060  * @device:     device to match (starget)
2061  *
2062  * Returns:
2063  *      1 if event matches starget / 0 if event does not match starget
2064  **/
2065 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2066 {
2067         if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2068                 return 1;
2069         return 0;
2070 }
2071
2072 /**
2073  * ibmvfc_match_lun - Match function for specified LUN
2074  * @evt:        ibmvfc event struct
2075  * @device:     device to match (sdev)
2076  *
2077  * Returns:
2078  *      1 if event matches sdev / 0 if event does not match sdev
2079  **/
2080 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2081 {
2082         if (evt->cmnd && evt->cmnd->device == device)
2083                 return 1;
2084         return 0;
2085 }
2086
2087 /**
2088  * ibmvfc_wait_for_ops - Wait for ops to complete
2089  * @vhost:      ibmvfc host struct
2090  * @device:     device to match (starget or sdev)
2091  * @match:      match function
2092  *
2093  * Returns:
2094  *      SUCCESS / FAILED
2095  **/
2096 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2097                                int (*match) (struct ibmvfc_event *, void *))
2098 {
2099         struct ibmvfc_event *evt;
2100         DECLARE_COMPLETION_ONSTACK(comp);
2101         int wait;
2102         unsigned long flags;
2103         signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2104
2105         ENTER;
2106         do {
2107                 wait = 0;
2108                 spin_lock_irqsave(vhost->host->host_lock, flags);
2109                 list_for_each_entry(evt, &vhost->sent, queue) {
2110                         if (match(evt, device)) {
2111                                 evt->eh_comp = &comp;
2112                                 wait++;
2113                         }
2114                 }
2115                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2116
2117                 if (wait) {
2118                         timeout = wait_for_completion_timeout(&comp, timeout);
2119
2120                         if (!timeout) {
2121                                 wait = 0;
2122                                 spin_lock_irqsave(vhost->host->host_lock, flags);
2123                                 list_for_each_entry(evt, &vhost->sent, queue) {
2124                                         if (match(evt, device)) {
2125                                                 evt->eh_comp = NULL;
2126                                                 wait++;
2127                                         }
2128                                 }
2129                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2130                                 if (wait)
2131                                         dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2132                                 LEAVE;
2133                                 return wait ? FAILED : SUCCESS;
2134                         }
2135                 }
2136         } while (wait);
2137
2138         LEAVE;
2139         return SUCCESS;
2140 }
2141
2142 /**
2143  * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2144  * @sdev:       scsi device to cancel commands
2145  * @type:       type of error recovery being performed
2146  *
2147  * This sends a cancel to the VIOS for the specified device. This does
2148  * NOT send any abort to the actual device. That must be done separately.
2149  *
2150  * Returns:
2151  *      0 on success / other on failure
2152  **/
2153 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2154 {
2155         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2156         struct scsi_target *starget = scsi_target(sdev);
2157         struct fc_rport *rport = starget_to_rport(starget);
2158         struct ibmvfc_tmf *tmf;
2159         struct ibmvfc_event *evt, *found_evt;
2160         union ibmvfc_iu rsp;
2161         int rsp_rc = -EBUSY;
2162         unsigned long flags;
2163         u16 status;
2164
2165         ENTER;
2166         spin_lock_irqsave(vhost->host->host_lock, flags);
2167         found_evt = NULL;
2168         list_for_each_entry(evt, &vhost->sent, queue) {
2169                 if (evt->cmnd && evt->cmnd->device == sdev) {
2170                         found_evt = evt;
2171                         break;
2172                 }
2173         }
2174
2175         if (!found_evt) {
2176                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2177                         sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2178                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2179                 return 0;
2180         }
2181
2182         if (vhost->logged_in) {
2183                 evt = ibmvfc_get_event(vhost);
2184                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2185
2186                 tmf = &evt->iu.tmf;
2187                 memset(tmf, 0, sizeof(*tmf));
2188                 tmf->common.version = cpu_to_be32(1);
2189                 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2190                 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2191                 tmf->scsi_id = cpu_to_be64(rport->port_id);
2192                 int_to_scsilun(sdev->lun, &tmf->lun);
2193                 if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
2194                         type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2195                 if (vhost->state == IBMVFC_ACTIVE)
2196                         tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2197                 else
2198                         tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2199                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2200                 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2201
2202                 evt->sync_iu = &rsp;
2203                 init_completion(&evt->comp);
2204                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2205         }
2206
2207         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2208
2209         if (rsp_rc != 0) {
2210                 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2211                 /* If failure is received, the host adapter is most likely going
2212                  through reset, return success so the caller will wait for the command
2213                  being cancelled to get returned */
2214                 return 0;
2215         }
2216
2217         sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2218
2219         wait_for_completion(&evt->comp);
2220         status = be16_to_cpu(rsp.mad_common.status);
2221         spin_lock_irqsave(vhost->host->host_lock, flags);
2222         ibmvfc_free_event(evt);
2223         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2224
2225         if (status != IBMVFC_MAD_SUCCESS) {
2226                 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2227                 switch (status) {
2228                 case IBMVFC_MAD_DRIVER_FAILED:
2229                 case IBMVFC_MAD_CRQ_ERROR:
2230                         /* Host adapter most likely going through reset, return success to
2231                          the caller will wait for the command being cancelled to get returned */
2232                         return 0;
2233                 default:
2234                         return -EIO;
2235                 };
2236         }
2237
2238         sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2239         return 0;
2240 }
2241
2242 /**
2243  * ibmvfc_match_key - Match function for specified cancel key
2244  * @evt:        ibmvfc event struct
2245  * @key:        cancel key to match
2246  *
2247  * Returns:
2248  *      1 if event matches key / 0 if event does not match key
2249  **/
2250 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2251 {
2252         unsigned long cancel_key = (unsigned long)key;
2253
2254         if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2255             be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2256                 return 1;
2257         return 0;
2258 }
2259
2260 /**
2261  * ibmvfc_match_evt - Match function for specified event
2262  * @evt:        ibmvfc event struct
2263  * @match:      event to match
2264  *
2265  * Returns:
2266  *      1 if event matches key / 0 if event does not match key
2267  **/
2268 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2269 {
2270         if (evt == match)
2271                 return 1;
2272         return 0;
2273 }
2274
2275 /**
2276  * ibmvfc_abort_task_set - Abort outstanding commands to the device
2277  * @sdev:       scsi device to abort commands
2278  *
2279  * This sends an Abort Task Set to the VIOS for the specified device. This does
2280  * NOT send any cancel to the VIOS. That must be done separately.
2281  *
2282  * Returns:
2283  *      0 on success / other on failure
2284  **/
2285 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2286 {
2287         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2288         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2289         struct ibmvfc_cmd *tmf;
2290         struct ibmvfc_event *evt, *found_evt;
2291         union ibmvfc_iu rsp_iu;
2292         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2293         int rc, rsp_rc = -EBUSY;
2294         unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2295         int rsp_code = 0;
2296
2297         spin_lock_irqsave(vhost->host->host_lock, flags);
2298         found_evt = NULL;
2299         list_for_each_entry(evt, &vhost->sent, queue) {
2300                 if (evt->cmnd && evt->cmnd->device == sdev) {
2301                         found_evt = evt;
2302                         break;
2303                 }
2304         }
2305
2306         if (!found_evt) {
2307                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2308                         sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2309                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2310                 return 0;
2311         }
2312
2313         if (vhost->state == IBMVFC_ACTIVE) {
2314                 evt = ibmvfc_get_event(vhost);
2315                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2316
2317                 tmf = &evt->iu.cmd;
2318                 memset(tmf, 0, sizeof(*tmf));
2319                 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2320                 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2321                 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2322                 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2323                 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2324                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2325                 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2326                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2327                 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2328                 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2329                 evt->sync_iu = &rsp_iu;
2330
2331                 init_completion(&evt->comp);
2332                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2333         }
2334
2335         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2336
2337         if (rsp_rc != 0) {
2338                 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2339                 return -EIO;
2340         }
2341
2342         sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2343         timeout = wait_for_completion_timeout(&evt->comp, timeout);
2344
2345         if (!timeout) {
2346                 rc = ibmvfc_cancel_all(sdev, 0);
2347                 if (!rc) {
2348                         rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2349                         if (rc == SUCCESS)
2350                                 rc = 0;
2351                 }
2352
2353                 if (rc) {
2354                         sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2355                         ibmvfc_reset_host(vhost);
2356                         rsp_rc = -EIO;
2357                         rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2358
2359                         if (rc == SUCCESS)
2360                                 rsp_rc = 0;
2361
2362                         rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2363                         if (rc != SUCCESS) {
2364                                 spin_lock_irqsave(vhost->host->host_lock, flags);
2365                                 ibmvfc_hard_reset_host(vhost);
2366                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2367                                 rsp_rc = 0;
2368                         }
2369
2370                         goto out;
2371                 }
2372         }
2373
2374         if (rsp_iu.cmd.status)
2375                 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2376
2377         if (rsp_code) {
2378                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2379                         rsp_code = fc_rsp->data.info.rsp_code;
2380
2381                 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2382                             "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2383                             ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2384                             rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
2385                             fc_rsp->scsi_status);
2386                 rsp_rc = -EIO;
2387         } else
2388                 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2389
2390 out:
2391         spin_lock_irqsave(vhost->host->host_lock, flags);
2392         ibmvfc_free_event(evt);
2393         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2394         return rsp_rc;
2395 }
2396
2397 /**
2398  * ibmvfc_eh_abort_handler - Abort a command
2399  * @cmd:        scsi command to abort
2400  *
2401  * Returns:
2402  *      SUCCESS / FAST_IO_FAIL / FAILED
2403  **/
2404 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2405 {
2406         struct scsi_device *sdev = cmd->device;
2407         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2408         int cancel_rc, block_rc;
2409         int rc = FAILED;
2410
2411         ENTER;
2412         block_rc = fc_block_scsi_eh(cmd);
2413         ibmvfc_wait_while_resetting(vhost);
2414         if (block_rc != FAST_IO_FAIL) {
2415                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2416                 ibmvfc_abort_task_set(sdev);
2417         } else
2418                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2419
2420         if (!cancel_rc)
2421                 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2422
2423         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2424                 rc = FAST_IO_FAIL;
2425
2426         LEAVE;
2427         return rc;
2428 }
2429
2430 /**
2431  * ibmvfc_eh_device_reset_handler - Reset a single LUN
2432  * @cmd:        scsi command struct
2433  *
2434  * Returns:
2435  *      SUCCESS / FAST_IO_FAIL / FAILED
2436  **/
2437 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2438 {
2439         struct scsi_device *sdev = cmd->device;
2440         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2441         int cancel_rc, block_rc, reset_rc = 0;
2442         int rc = FAILED;
2443
2444         ENTER;
2445         block_rc = fc_block_scsi_eh(cmd);
2446         ibmvfc_wait_while_resetting(vhost);
2447         if (block_rc != FAST_IO_FAIL) {
2448                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2449                 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2450         } else
2451                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2452
2453         if (!cancel_rc && !reset_rc)
2454                 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2455
2456         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2457                 rc = FAST_IO_FAIL;
2458
2459         LEAVE;
2460         return rc;
2461 }
2462
2463 /**
2464  * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2465  * @sdev:       scsi device struct
2466  * @data:       return code
2467  *
2468  **/
2469 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2470 {
2471         unsigned long *rc = data;
2472         *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2473 }
2474
2475 /**
2476  * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2477  * @sdev:       scsi device struct
2478  * @data:       return code
2479  *
2480  **/
2481 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2482 {
2483         unsigned long *rc = data;
2484         *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2485 }
2486
2487 /**
2488  * ibmvfc_eh_target_reset_handler - Reset the target
2489  * @cmd:        scsi command struct
2490  *
2491  * Returns:
2492  *      SUCCESS / FAST_IO_FAIL / FAILED
2493  **/
2494 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2495 {
2496         struct scsi_device *sdev = cmd->device;
2497         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2498         struct scsi_target *starget = scsi_target(sdev);
2499         int block_rc;
2500         int reset_rc = 0;
2501         int rc = FAILED;
2502         unsigned long cancel_rc = 0;
2503
2504         ENTER;
2505         block_rc = fc_block_scsi_eh(cmd);
2506         ibmvfc_wait_while_resetting(vhost);
2507         if (block_rc != FAST_IO_FAIL) {
2508                 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2509                 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2510         } else
2511                 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2512
2513         if (!cancel_rc && !reset_rc)
2514                 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2515
2516         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2517                 rc = FAST_IO_FAIL;
2518
2519         LEAVE;
2520         return rc;
2521 }
2522
2523 /**
2524  * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2525  * @cmd:        struct scsi_cmnd having problems
2526  *
2527  **/
2528 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2529 {
2530         int rc;
2531         struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2532
2533         dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2534         rc = ibmvfc_issue_fc_host_lip(vhost->host);
2535
2536         return rc ? FAILED : SUCCESS;
2537 }
2538
2539 /**
2540  * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2541  * @rport:              rport struct
2542  *
2543  * Return value:
2544  *      none
2545  **/
2546 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2547 {
2548         struct Scsi_Host *shost = rport_to_shost(rport);
2549         struct ibmvfc_host *vhost = shost_priv(shost);
2550         struct fc_rport *dev_rport;
2551         struct scsi_device *sdev;
2552         unsigned long rc;
2553
2554         ENTER;
2555         shost_for_each_device(sdev, shost) {
2556                 dev_rport = starget_to_rport(scsi_target(sdev));
2557                 if (dev_rport != rport)
2558                         continue;
2559                 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2560         }
2561
2562         rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2563
2564         if (rc == FAILED)
2565                 ibmvfc_issue_fc_host_lip(shost);
2566         LEAVE;
2567 }
2568
2569 static const struct ibmvfc_async_desc ae_desc [] = {
2570         { "PLOGI",      IBMVFC_AE_ELS_PLOGI,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2571         { "LOGO",       IBMVFC_AE_ELS_LOGO,     IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2572         { "PRLO",       IBMVFC_AE_ELS_PRLO,     IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2573         { "N-Port SCN", IBMVFC_AE_SCN_NPORT,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2574         { "Group SCN",  IBMVFC_AE_SCN_GROUP,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2575         { "Domain SCN", IBMVFC_AE_SCN_DOMAIN,   IBMVFC_DEFAULT_LOG_LEVEL },
2576         { "Fabric SCN", IBMVFC_AE_SCN_FABRIC,   IBMVFC_DEFAULT_LOG_LEVEL },
2577         { "Link Up",    IBMVFC_AE_LINK_UP,      IBMVFC_DEFAULT_LOG_LEVEL },
2578         { "Link Down",  IBMVFC_AE_LINK_DOWN,    IBMVFC_DEFAULT_LOG_LEVEL },
2579         { "Link Dead",  IBMVFC_AE_LINK_DEAD,    IBMVFC_DEFAULT_LOG_LEVEL },
2580         { "Halt",       IBMVFC_AE_HALT,         IBMVFC_DEFAULT_LOG_LEVEL },
2581         { "Resume",     IBMVFC_AE_RESUME,       IBMVFC_DEFAULT_LOG_LEVEL },
2582         { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
2583 };
2584
2585 static const struct ibmvfc_async_desc unknown_ae = {
2586         "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
2587 };
2588
2589 /**
2590  * ibmvfc_get_ae_desc - Get text description for async event
2591  * @ae: async event
2592  *
2593  **/
2594 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
2595 {
2596         int i;
2597
2598         for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2599                 if (ae_desc[i].ae == ae)
2600                         return &ae_desc[i];
2601
2602         return &unknown_ae;
2603 }
2604
2605 static const struct {
2606         enum ibmvfc_ae_link_state state;
2607         const char *desc;
2608 } link_desc [] = {
2609         { IBMVFC_AE_LS_LINK_UP,         " link up" },
2610         { IBMVFC_AE_LS_LINK_BOUNCED,    " link bounced" },
2611         { IBMVFC_AE_LS_LINK_DOWN,       " link down" },
2612         { IBMVFC_AE_LS_LINK_DEAD,       " link dead" },
2613 };
2614
2615 /**
2616  * ibmvfc_get_link_state - Get text description for link state
2617  * @state:      link state
2618  *
2619  **/
2620 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2621 {
2622         int i;
2623
2624         for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2625                 if (link_desc[i].state == state)
2626                         return link_desc[i].desc;
2627
2628         return "";
2629 }
2630
2631 /**
2632  * ibmvfc_handle_async - Handle an async event from the adapter
2633  * @crq:        crq to process
2634  * @vhost:      ibmvfc host struct
2635  *
2636  **/
2637 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2638                                 struct ibmvfc_host *vhost)
2639 {
2640         const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
2641         struct ibmvfc_target *tgt;
2642
2643         ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2644                    " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
2645                    be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
2646                    ibmvfc_get_link_state(crq->link_state));
2647
2648         switch (be64_to_cpu(crq->event)) {
2649         case IBMVFC_AE_RESUME:
2650                 switch (crq->link_state) {
2651                 case IBMVFC_AE_LS_LINK_DOWN:
2652                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2653                         break;
2654                 case IBMVFC_AE_LS_LINK_DEAD:
2655                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2656                         break;
2657                 case IBMVFC_AE_LS_LINK_UP:
2658                 case IBMVFC_AE_LS_LINK_BOUNCED:
2659                 default:
2660                         vhost->events_to_log |= IBMVFC_AE_LINKUP;
2661                         vhost->delay_init = 1;
2662                         __ibmvfc_reset_host(vhost);
2663                         break;
2664                 }
2665
2666                 break;
2667         case IBMVFC_AE_LINK_UP:
2668                 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2669                 vhost->delay_init = 1;
2670                 __ibmvfc_reset_host(vhost);
2671                 break;
2672         case IBMVFC_AE_SCN_FABRIC:
2673         case IBMVFC_AE_SCN_DOMAIN:
2674                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2675                 if (vhost->state < IBMVFC_HALTED) {
2676                         vhost->delay_init = 1;
2677                         __ibmvfc_reset_host(vhost);
2678                 }
2679                 break;
2680         case IBMVFC_AE_SCN_NPORT:
2681         case IBMVFC_AE_SCN_GROUP:
2682                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2683                 ibmvfc_reinit_host(vhost);
2684                 break;
2685         case IBMVFC_AE_ELS_LOGO:
2686         case IBMVFC_AE_ELS_PRLO:
2687         case IBMVFC_AE_ELS_PLOGI:
2688                 list_for_each_entry(tgt, &vhost->targets, queue) {
2689                         if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2690                                 break;
2691                         if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
2692                                 continue;
2693                         if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
2694                                 continue;
2695                         if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
2696                                 continue;
2697                         if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
2698                                 tgt->logo_rcvd = 1;
2699                         if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
2700                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2701                                 ibmvfc_reinit_host(vhost);
2702                         }
2703                 }
2704                 break;
2705         case IBMVFC_AE_LINK_DOWN:
2706         case IBMVFC_AE_ADAPTER_FAILED:
2707                 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2708                 break;
2709         case IBMVFC_AE_LINK_DEAD:
2710                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2711                 break;
2712         case IBMVFC_AE_HALT:
2713                 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2714                 break;
2715         default:
2716                 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
2717                 break;
2718         }
2719 }
2720
2721 /**
2722  * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2723  * @crq:        Command/Response queue
2724  * @vhost:      ibmvfc host struct
2725  *
2726  **/
2727 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2728 {
2729         long rc;
2730         struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
2731
2732         switch (crq->valid) {
2733         case IBMVFC_CRQ_INIT_RSP:
2734                 switch (crq->format) {
2735                 case IBMVFC_CRQ_INIT:
2736                         dev_info(vhost->dev, "Partner initialized\n");
2737                         /* Send back a response */
2738                         rc = ibmvfc_send_crq_init_complete(vhost);
2739                         if (rc == 0)
2740                                 ibmvfc_init_host(vhost);
2741                         else
2742                                 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2743                         break;
2744                 case IBMVFC_CRQ_INIT_COMPLETE:
2745                         dev_info(vhost->dev, "Partner initialization complete\n");
2746                         ibmvfc_init_host(vhost);
2747                         break;
2748                 default:
2749                         dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2750                 }
2751                 return;
2752         case IBMVFC_CRQ_XPORT_EVENT:
2753                 vhost->state = IBMVFC_NO_CRQ;
2754                 vhost->logged_in = 0;
2755                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2756                 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2757                         /* We need to re-setup the interpartition connection */
2758                         dev_info(vhost->dev, "Re-enabling adapter\n");
2759                         vhost->client_migrated = 1;
2760                         ibmvfc_purge_requests(vhost, DID_REQUEUE);
2761                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2762                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
2763                 } else {
2764                         dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2765                         ibmvfc_purge_requests(vhost, DID_ERROR);
2766                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2767                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
2768                 }
2769                 return;
2770         case IBMVFC_CRQ_CMD_RSP:
2771                 break;
2772         default:
2773                 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2774                 return;
2775         }
2776
2777         if (crq->format == IBMVFC_ASYNC_EVENT)
2778                 return;
2779
2780         /* The only kind of payload CRQs we should get are responses to
2781          * things we send. Make sure this response is to something we
2782          * actually sent
2783          */
2784         if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2785                 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
2786                         crq->ioba);
2787                 return;
2788         }
2789
2790         if (unlikely(atomic_read(&evt->free))) {
2791                 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
2792                         crq->ioba);
2793                 return;
2794         }
2795
2796         del_timer(&evt->timer);
2797         list_del(&evt->queue);
2798         ibmvfc_trc_end(evt);
2799         evt->done(evt);
2800 }
2801
2802 /**
2803  * ibmvfc_scan_finished - Check if the device scan is done.
2804  * @shost:      scsi host struct
2805  * @time:       current elapsed time
2806  *
2807  * Returns:
2808  *      0 if scan is not done / 1 if scan is done
2809  **/
2810 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2811 {
2812         unsigned long flags;
2813         struct ibmvfc_host *vhost = shost_priv(shost);
2814         int done = 0;
2815
2816         spin_lock_irqsave(shost->host_lock, flags);
2817         if (time >= (init_timeout * HZ)) {
2818                 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2819                          "continuing initialization\n", init_timeout);
2820                 done = 1;
2821         }
2822
2823         if (vhost->scan_complete)
2824                 done = 1;
2825         spin_unlock_irqrestore(shost->host_lock, flags);
2826         return done;
2827 }
2828
2829 /**
2830  * ibmvfc_slave_alloc - Setup the device's task set value
2831  * @sdev:       struct scsi_device device to configure
2832  *
2833  * Set the device's task set value so that error handling works as
2834  * expected.
2835  *
2836  * Returns:
2837  *      0 on success / -ENXIO if device does not exist
2838  **/
2839 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2840 {
2841         struct Scsi_Host *shost = sdev->host;
2842         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2843         struct ibmvfc_host *vhost = shost_priv(shost);
2844         unsigned long flags = 0;
2845
2846         if (!rport || fc_remote_port_chkready(rport))
2847                 return -ENXIO;
2848
2849         spin_lock_irqsave(shost->host_lock, flags);
2850         sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2851         spin_unlock_irqrestore(shost->host_lock, flags);
2852         return 0;
2853 }
2854
2855 /**
2856  * ibmvfc_target_alloc - Setup the target's task set value
2857  * @starget:    struct scsi_target
2858  *
2859  * Set the target's task set value so that error handling works as
2860  * expected.
2861  *
2862  * Returns:
2863  *      0 on success / -ENXIO if device does not exist
2864  **/
2865 static int ibmvfc_target_alloc(struct scsi_target *starget)
2866 {
2867         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2868         struct ibmvfc_host *vhost = shost_priv(shost);
2869         unsigned long flags = 0;
2870
2871         spin_lock_irqsave(shost->host_lock, flags);
2872         starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2873         spin_unlock_irqrestore(shost->host_lock, flags);
2874         return 0;
2875 }
2876
2877 /**
2878  * ibmvfc_slave_configure - Configure the device
2879  * @sdev:       struct scsi_device device to configure
2880  *
2881  * Enable allow_restart for a device if it is a disk. Adjust the
2882  * queue_depth here also.
2883  *
2884  * Returns:
2885  *      0
2886  **/
2887 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2888 {
2889         struct Scsi_Host *shost = sdev->host;
2890         unsigned long flags = 0;
2891
2892         spin_lock_irqsave(shost->host_lock, flags);
2893         if (sdev->type == TYPE_DISK)
2894                 sdev->allow_restart = 1;
2895         spin_unlock_irqrestore(shost->host_lock, flags);
2896         return 0;
2897 }
2898
2899 /**
2900  * ibmvfc_change_queue_depth - Change the device's queue depth
2901  * @sdev:       scsi device struct
2902  * @qdepth:     depth to set
2903  * @reason:     calling context
2904  *
2905  * Return value:
2906  *      actual depth set
2907  **/
2908 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2909 {
2910         if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2911                 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2912
2913         return scsi_change_queue_depth(sdev, qdepth);
2914 }
2915
2916 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2917                                                  struct device_attribute *attr, char *buf)
2918 {
2919         struct Scsi_Host *shost = class_to_shost(dev);
2920         struct ibmvfc_host *vhost = shost_priv(shost);
2921
2922         return snprintf(buf, PAGE_SIZE, "%s\n",
2923                         vhost->login_buf->resp.partition_name);
2924 }
2925
2926 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2927                                             struct device_attribute *attr, char *buf)
2928 {
2929         struct Scsi_Host *shost = class_to_shost(dev);
2930         struct ibmvfc_host *vhost = shost_priv(shost);
2931
2932         return snprintf(buf, PAGE_SIZE, "%s\n",
2933                         vhost->login_buf->resp.device_name);
2934 }
2935
2936 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2937                                          struct device_attribute *attr, char *buf)
2938 {
2939         struct Scsi_Host *shost = class_to_shost(dev);
2940         struct ibmvfc_host *vhost = shost_priv(shost);
2941
2942         return snprintf(buf, PAGE_SIZE, "%s\n",
2943                         vhost->login_buf->resp.port_loc_code);
2944 }
2945
2946 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2947                                          struct device_attribute *attr, char *buf)
2948 {
2949         struct Scsi_Host *shost = class_to_shost(dev);
2950         struct ibmvfc_host *vhost = shost_priv(shost);
2951
2952         return snprintf(buf, PAGE_SIZE, "%s\n",
2953                         vhost->login_buf->resp.drc_name);
2954 }
2955
2956 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2957                                              struct device_attribute *attr, char *buf)
2958 {
2959         struct Scsi_Host *shost = class_to_shost(dev);
2960         struct ibmvfc_host *vhost = shost_priv(shost);
2961         return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2962 }
2963
2964 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
2965                                              struct device_attribute *attr, char *buf)
2966 {
2967         struct Scsi_Host *shost = class_to_shost(dev);
2968         struct ibmvfc_host *vhost = shost_priv(shost);
2969         return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
2970 }
2971
2972 /**
2973  * ibmvfc_show_log_level - Show the adapter's error logging level
2974  * @dev:        class device struct
2975  * @buf:        buffer
2976  *
2977  * Return value:
2978  *      number of bytes printed to buffer
2979  **/
2980 static ssize_t ibmvfc_show_log_level(struct device *dev,
2981                                      struct device_attribute *attr, char *buf)
2982 {
2983         struct Scsi_Host *shost = class_to_shost(dev);
2984         struct ibmvfc_host *vhost = shost_priv(shost);
2985         unsigned long flags = 0;
2986         int len;
2987
2988         spin_lock_irqsave(shost->host_lock, flags);
2989         len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2990         spin_unlock_irqrestore(shost->host_lock, flags);
2991         return len;
2992 }
2993
2994 /**
2995  * ibmvfc_store_log_level - Change the adapter's error logging level
2996  * @dev:        class device struct
2997  * @buf:        buffer
2998  *
2999  * Return value:
3000  *      number of bytes printed to buffer
3001  **/
3002 static ssize_t ibmvfc_store_log_level(struct device *dev,
3003                                       struct device_attribute *attr,
3004                                       const char *buf, size_t count)
3005 {
3006         struct Scsi_Host *shost = class_to_shost(dev);
3007         struct ibmvfc_host *vhost = shost_priv(shost);
3008         unsigned long flags = 0;
3009
3010         spin_lock_irqsave(shost->host_lock, flags);
3011         vhost->log_level = simple_strtoul(buf, NULL, 10);
3012         spin_unlock_irqrestore(shost->host_lock, flags);
3013         return strlen(buf);
3014 }
3015
3016 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3017 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3018 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3019 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3020 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3021 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3022 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3023                    ibmvfc_show_log_level, ibmvfc_store_log_level);
3024
3025 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3026 /**
3027  * ibmvfc_read_trace - Dump the adapter trace
3028  * @filp:               open sysfs file
3029  * @kobj:               kobject struct
3030  * @bin_attr:   bin_attribute struct
3031  * @buf:                buffer
3032  * @off:                offset
3033  * @count:              buffer size
3034  *
3035  * Return value:
3036  *      number of bytes printed to buffer
3037  **/
3038 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3039                                  struct bin_attribute *bin_attr,
3040                                  char *buf, loff_t off, size_t count)
3041 {
3042         struct device *dev = container_of(kobj, struct device, kobj);
3043         struct Scsi_Host *shost = class_to_shost(dev);
3044         struct ibmvfc_host *vhost = shost_priv(shost);
3045         unsigned long flags = 0;
3046         int size = IBMVFC_TRACE_SIZE;
3047         char *src = (char *)vhost->trace;
3048
3049         if (off > size)
3050                 return 0;
3051         if (off + count > size) {
3052                 size -= off;
3053                 count = size;
3054         }
3055
3056         spin_lock_irqsave(shost->host_lock, flags);
3057         memcpy(buf, &src[off], count);
3058         spin_unlock_irqrestore(shost->host_lock, flags);
3059         return count;
3060 }
3061
3062 static struct bin_attribute ibmvfc_trace_attr = {
3063         .attr = {
3064                 .name = "trace",
3065                 .mode = S_IRUGO,
3066         },
3067         .size = 0,
3068         .read = ibmvfc_read_trace,
3069 };
3070 #endif
3071
3072 static struct device_attribute *ibmvfc_attrs[] = {
3073         &dev_attr_partition_name,
3074         &dev_attr_device_name,
3075         &dev_attr_port_loc_code,
3076         &dev_attr_drc_name,
3077         &dev_attr_npiv_version,
3078         &dev_attr_capabilities,
3079         &dev_attr_log_level,
3080         NULL
3081 };
3082
3083 static struct scsi_host_template driver_template = {
3084         .module = THIS_MODULE,
3085         .name = "IBM POWER Virtual FC Adapter",
3086         .proc_name = IBMVFC_NAME,
3087         .queuecommand = ibmvfc_queuecommand,
3088         .eh_timed_out = fc_eh_timed_out,
3089         .eh_abort_handler = ibmvfc_eh_abort_handler,
3090         .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3091         .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3092         .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3093         .slave_alloc = ibmvfc_slave_alloc,
3094         .slave_configure = ibmvfc_slave_configure,
3095         .target_alloc = ibmvfc_target_alloc,
3096         .scan_finished = ibmvfc_scan_finished,
3097         .change_queue_depth = ibmvfc_change_queue_depth,
3098         .cmd_per_lun = 16,
3099         .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3100         .this_id = -1,
3101         .sg_tablesize = SG_ALL,
3102         .max_sectors = IBMVFC_MAX_SECTORS,
3103         .shost_attrs = ibmvfc_attrs,
3104         .track_queue_depth = 1,
3105 };
3106
3107 /**
3108  * ibmvfc_next_async_crq - Returns the next entry in async queue
3109  * @vhost:      ibmvfc host struct
3110  *
3111  * Returns:
3112  *      Pointer to next entry in queue / NULL if empty
3113  **/
3114 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3115 {
3116         struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3117         struct ibmvfc_async_crq *crq;
3118
3119         crq = &async_crq->msgs[async_crq->cur];
3120         if (crq->valid & 0x80) {
3121                 if (++async_crq->cur == async_crq->size)
3122                         async_crq->cur = 0;
3123                 rmb();
3124         } else
3125                 crq = NULL;
3126
3127         return crq;
3128 }
3129
3130 /**
3131  * ibmvfc_next_crq - Returns the next entry in message queue
3132  * @vhost:      ibmvfc host struct
3133  *
3134  * Returns:
3135  *      Pointer to next entry in queue / NULL if empty
3136  **/
3137 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3138 {
3139         struct ibmvfc_crq_queue *queue = &vhost->crq;
3140         struct ibmvfc_crq *crq;
3141
3142         crq = &queue->msgs[queue->cur];
3143         if (crq->valid & 0x80) {
3144                 if (++queue->cur == queue->size)
3145                         queue->cur = 0;
3146                 rmb();
3147         } else
3148                 crq = NULL;
3149
3150         return crq;
3151 }
3152
3153 /**
3154  * ibmvfc_interrupt - Interrupt handler
3155  * @irq:                number of irq to handle, not used
3156  * @dev_instance: ibmvfc_host that received interrupt
3157  *
3158  * Returns:
3159  *      IRQ_HANDLED
3160  **/
3161 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3162 {
3163         struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3164         unsigned long flags;
3165
3166         spin_lock_irqsave(vhost->host->host_lock, flags);
3167         vio_disable_interrupts(to_vio_dev(vhost->dev));
3168         tasklet_schedule(&vhost->tasklet);
3169         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3170         return IRQ_HANDLED;
3171 }
3172
3173 /**
3174  * ibmvfc_tasklet - Interrupt handler tasklet
3175  * @data:               ibmvfc host struct
3176  *
3177  * Returns:
3178  *      Nothing
3179  **/
3180 static void ibmvfc_tasklet(void *data)
3181 {
3182         struct ibmvfc_host *vhost = data;
3183         struct vio_dev *vdev = to_vio_dev(vhost->dev);
3184         struct ibmvfc_crq *crq;
3185         struct ibmvfc_async_crq *async;
3186         unsigned long flags;
3187         int done = 0;
3188
3189         spin_lock_irqsave(vhost->host->host_lock, flags);
3190         while (!done) {
3191                 /* Pull all the valid messages off the async CRQ */
3192                 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3193                         ibmvfc_handle_async(async, vhost);
3194                         async->valid = 0;
3195                         wmb();
3196                 }
3197
3198                 /* Pull all the valid messages off the CRQ */
3199                 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3200                         ibmvfc_handle_crq(crq, vhost);
3201                         crq->valid = 0;
3202                         wmb();
3203                 }
3204
3205                 vio_enable_interrupts(vdev);
3206                 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3207                         vio_disable_interrupts(vdev);
3208                         ibmvfc_handle_async(async, vhost);
3209                         async->valid = 0;
3210                         wmb();
3211                 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3212                         vio_disable_interrupts(vdev);
3213                         ibmvfc_handle_crq(crq, vhost);
3214                         crq->valid = 0;
3215                         wmb();
3216                 } else
3217                         done = 1;
3218         }
3219
3220         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3221 }
3222
3223 /**
3224  * ibmvfc_init_tgt - Set the next init job step for the target
3225  * @tgt:                ibmvfc target struct
3226  * @job_step:   job step to perform
3227  *
3228  **/
3229 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3230                             void (*job_step) (struct ibmvfc_target *))
3231 {
3232         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
3233         tgt->job_step = job_step;
3234         wake_up(&tgt->vhost->work_wait_q);
3235 }
3236
3237 /**
3238  * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3239  * @tgt:                ibmvfc target struct
3240  * @job_step:   initialization job step
3241  *
3242  * Returns: 1 if step will be retried / 0 if not
3243  *
3244  **/
3245 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3246                                   void (*job_step) (struct ibmvfc_target *))
3247 {
3248         if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3249                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3250                 wake_up(&tgt->vhost->work_wait_q);
3251                 return 0;
3252         } else
3253                 ibmvfc_init_tgt(tgt, job_step);
3254         return 1;
3255 }
3256
3257 /* Defined in FC-LS */
3258 static const struct {
3259         int code;
3260         int retry;
3261         int logged_in;
3262 } prli_rsp [] = {
3263         { 0, 1, 0 },
3264         { 1, 0, 1 },
3265         { 2, 1, 0 },
3266         { 3, 1, 0 },
3267         { 4, 0, 0 },
3268         { 5, 0, 0 },
3269         { 6, 0, 1 },
3270         { 7, 0, 0 },
3271         { 8, 1, 0 },
3272 };
3273
3274 /**
3275  * ibmvfc_get_prli_rsp - Find PRLI response index
3276  * @flags:      PRLI response flags
3277  *
3278  **/
3279 static int ibmvfc_get_prli_rsp(u16 flags)
3280 {
3281         int i;
3282         int code = (flags & 0x0f00) >> 8;
3283
3284         for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3285                 if (prli_rsp[i].code == code)
3286                         return i;
3287
3288         return 0;
3289 }
3290
3291 /**
3292  * ibmvfc_tgt_prli_done - Completion handler for Process Login
3293  * @evt:        ibmvfc event struct
3294  *
3295  **/
3296 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3297 {
3298         struct ibmvfc_target *tgt = evt->tgt;
3299         struct ibmvfc_host *vhost = evt->vhost;
3300         struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3301         struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3302         u32 status = be16_to_cpu(rsp->common.status);
3303         int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3304
3305         vhost->discovery_threads--;
3306         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3307         switch (status) {
3308         case IBMVFC_MAD_SUCCESS:
3309                 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3310                         parms->type, parms->flags, parms->service_parms);
3311
3312                 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3313                         index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3314                         if (prli_rsp[index].logged_in) {
3315                                 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3316                                         tgt->need_login = 0;
3317                                         tgt->ids.roles = 0;
3318                                         if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3319                                                 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3320                                         if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3321                                                 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3322                                         tgt->add_rport = 1;
3323                                 } else
3324                                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3325                         } else if (prli_rsp[index].retry)
3326                                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3327                         else
3328                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3329                 } else
3330                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3331                 break;
3332         case IBMVFC_MAD_DRIVER_FAILED:
3333                 break;
3334         case IBMVFC_MAD_CRQ_ERROR:
3335                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3336                 break;
3337         case IBMVFC_MAD_FAILED:
3338         default:
3339                 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3340                      be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3341                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3342                 else if (tgt->logo_rcvd)
3343                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3344                 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3345                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3346                 else
3347                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3348
3349                 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3350                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3351                         rsp->status, rsp->error, status);
3352                 break;
3353         }
3354
3355         kref_put(&tgt->kref, ibmvfc_release_tgt);
3356         ibmvfc_free_event(evt);
3357         wake_up(&vhost->work_wait_q);
3358 }
3359
3360 /**
3361  * ibmvfc_tgt_send_prli - Send a process login
3362  * @tgt:        ibmvfc target struct
3363  *
3364  **/
3365 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3366 {
3367         struct ibmvfc_process_login *prli;
3368         struct ibmvfc_host *vhost = tgt->vhost;
3369         struct ibmvfc_event *evt;
3370
3371         if (vhost->discovery_threads >= disc_threads)
3372                 return;
3373
3374         kref_get(&tgt->kref);
3375         evt = ibmvfc_get_event(vhost);
3376         vhost->discovery_threads++;
3377         ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3378         evt->tgt = tgt;
3379         prli = &evt->iu.prli;
3380         memset(prli, 0, sizeof(*prli));
3381         prli->common.version = cpu_to_be32(1);
3382         prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3383         prli->common.length = cpu_to_be16(sizeof(*prli));
3384         prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3385
3386         prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3387         prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3388         prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3389         prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3390
3391         if (cls3_error)
3392                 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3393
3394         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3395         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3396                 vhost->discovery_threads--;
3397                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3398                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3399         } else
3400                 tgt_dbg(tgt, "Sent process login\n");
3401 }
3402
3403 /**
3404  * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3405  * @evt:        ibmvfc event struct
3406  *
3407  **/
3408 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3409 {
3410         struct ibmvfc_target *tgt = evt->tgt;
3411         struct ibmvfc_host *vhost = evt->vhost;
3412         struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3413         u32 status = be16_to_cpu(rsp->common.status);
3414         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3415
3416         vhost->discovery_threads--;
3417         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3418         switch (status) {
3419         case IBMVFC_MAD_SUCCESS:
3420                 tgt_dbg(tgt, "Port Login succeeded\n");
3421                 if (tgt->ids.port_name &&
3422                     tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3423                         vhost->reinit = 1;
3424                         tgt_dbg(tgt, "Port re-init required\n");
3425                         break;
3426                 }
3427                 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3428                 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3429                 tgt->ids.port_id = tgt->scsi_id;
3430                 memcpy(&tgt->service_parms, &rsp->service_parms,
3431                        sizeof(tgt->service_parms));
3432                 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3433                        sizeof(tgt->service_parms_change));
3434                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3435                 break;
3436         case IBMVFC_MAD_DRIVER_FAILED:
3437                 break;
3438         case IBMVFC_MAD_CRQ_ERROR:
3439                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3440                 break;
3441         case IBMVFC_MAD_FAILED:
3442         default:
3443                 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3444                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3445                 else
3446                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3447
3448                 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3449                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), rsp->status, rsp->error,
3450                         ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), rsp->fc_type,
3451                         ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), rsp->fc_explain, status);
3452                 break;
3453         }
3454
3455         kref_put(&tgt->kref, ibmvfc_release_tgt);
3456         ibmvfc_free_event(evt);
3457         wake_up(&vhost->work_wait_q);
3458 }
3459
3460 /**
3461  * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3462  * @tgt:        ibmvfc target struct
3463  *
3464  **/
3465 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3466 {
3467         struct ibmvfc_port_login *plogi;
3468         struct ibmvfc_host *vhost = tgt->vhost;
3469         struct ibmvfc_event *evt;
3470
3471         if (vhost->discovery_threads >= disc_threads)
3472                 return;
3473
3474         kref_get(&tgt->kref);
3475         tgt->logo_rcvd = 0;
3476         evt = ibmvfc_get_event(vhost);
3477         vhost->discovery_threads++;
3478         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3479         ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3480         evt->tgt = tgt;
3481         plogi = &evt->iu.plogi;
3482         memset(plogi, 0, sizeof(*plogi));
3483         plogi->common.version = cpu_to_be32(1);
3484         plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3485         plogi->common.length = cpu_to_be16(sizeof(*plogi));
3486         plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
3487
3488         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3489                 vhost->discovery_threads--;
3490                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3491                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3492         } else
3493                 tgt_dbg(tgt, "Sent port login\n");
3494 }
3495
3496 /**
3497  * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3498  * @evt:        ibmvfc event struct
3499  *
3500  **/
3501 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3502 {
3503         struct ibmvfc_target *tgt = evt->tgt;
3504         struct ibmvfc_host *vhost = evt->vhost;
3505         struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3506         u32 status = be16_to_cpu(rsp->common.status);
3507
3508         vhost->discovery_threads--;
3509         ibmvfc_free_event(evt);
3510         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3511
3512         switch (status) {
3513         case IBMVFC_MAD_SUCCESS:
3514                 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3515                 break;
3516         case IBMVFC_MAD_DRIVER_FAILED:
3517                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3518                 wake_up(&vhost->work_wait_q);
3519                 return;
3520         case IBMVFC_MAD_FAILED:
3521         default:
3522                 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3523                 break;
3524         }
3525
3526         if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
3527                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3528         else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
3529                  tgt->scsi_id != tgt->new_scsi_id)
3530                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3531         kref_put(&tgt->kref, ibmvfc_release_tgt);
3532         wake_up(&vhost->work_wait_q);
3533 }
3534
3535 /**
3536  * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3537  * @tgt:                ibmvfc target struct
3538  *
3539  **/
3540 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3541 {
3542         struct ibmvfc_implicit_logout *mad;
3543         struct ibmvfc_host *vhost = tgt->vhost;
3544         struct ibmvfc_event *evt;
3545
3546         if (vhost->discovery_threads >= disc_threads)
3547                 return;
3548
3549         kref_get(&tgt->kref);
3550         evt = ibmvfc_get_event(vhost);
3551         vhost->discovery_threads++;
3552         ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3553         evt->tgt = tgt;
3554         mad = &evt->iu.implicit_logout;
3555         memset(mad, 0, sizeof(*mad));
3556         mad->common.version = cpu_to_be32(1);
3557         mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3558         mad->common.length = cpu_to_be16(sizeof(*mad));
3559         mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
3560
3561         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3562         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3563                 vhost->discovery_threads--;
3564                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3565                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3566         } else
3567                 tgt_dbg(tgt, "Sent Implicit Logout\n");
3568 }
3569
3570 /**
3571  * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3572  * @mad:        ibmvfc passthru mad struct
3573  * @tgt:        ibmvfc target struct
3574  *
3575  * Returns:
3576  *      1 if PLOGI needed / 0 if PLOGI not needed
3577  **/
3578 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3579                                     struct ibmvfc_target *tgt)
3580 {
3581         if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
3582                 return 1;
3583         if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
3584                 return 1;
3585         if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
3586                 return 1;
3587         return 0;
3588 }
3589
3590 /**
3591  * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3592  * @evt:        ibmvfc event struct
3593  *
3594  **/
3595 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3596 {
3597         struct ibmvfc_target *tgt = evt->tgt;
3598         struct ibmvfc_host *vhost = evt->vhost;
3599         struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3600         u32 status = be16_to_cpu(mad->common.status);
3601         u8 fc_reason, fc_explain;
3602
3603         vhost->discovery_threads--;
3604         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3605         del_timer(&tgt->timer);
3606
3607         switch (status) {
3608         case IBMVFC_MAD_SUCCESS:
3609                 tgt_dbg(tgt, "ADISC succeeded\n");
3610                 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3611                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3612                 break;
3613         case IBMVFC_MAD_DRIVER_FAILED:
3614                 break;
3615         case IBMVFC_MAD_FAILED:
3616         default:
3617                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3618                 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3619                 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
3620                 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3621                          ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
3622                          mad->iu.status, mad->iu.error,
3623                          ibmvfc_get_fc_type(fc_reason), fc_reason,
3624                          ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3625                 break;
3626         }
3627
3628         kref_put(&tgt->kref, ibmvfc_release_tgt);
3629         ibmvfc_free_event(evt);
3630         wake_up(&vhost->work_wait_q);
3631 }
3632
3633 /**
3634  * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3635  * @evt:                ibmvfc event struct
3636  *
3637  **/
3638 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3639 {
3640         struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3641
3642         memset(mad, 0, sizeof(*mad));
3643         mad->common.version = cpu_to_be32(1);
3644         mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3645         mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3646         mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3647                 offsetof(struct ibmvfc_passthru_mad, iu));
3648         mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3649         mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3650         mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3651         mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3652                 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3653                 offsetof(struct ibmvfc_passthru_fc_iu, payload));
3654         mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3655         mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3656                 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3657                 offsetof(struct ibmvfc_passthru_fc_iu, response));
3658         mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
3659 }
3660
3661 /**
3662  * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3663  * @evt:                ibmvfc event struct
3664  *
3665  * Just cleanup this event struct. Everything else is handled by
3666  * the ADISC completion handler. If the ADISC never actually comes
3667  * back, we still have the timer running on the ADISC event struct
3668  * which will fire and cause the CRQ to get reset.
3669  *
3670  **/
3671 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3672 {
3673         struct ibmvfc_host *vhost = evt->vhost;
3674         struct ibmvfc_target *tgt = evt->tgt;
3675
3676         tgt_dbg(tgt, "ADISC cancel complete\n");
3677         vhost->abort_threads--;
3678         ibmvfc_free_event(evt);
3679         kref_put(&tgt->kref, ibmvfc_release_tgt);
3680         wake_up(&vhost->work_wait_q);
3681 }
3682
3683 /**
3684  * ibmvfc_adisc_timeout - Handle an ADISC timeout
3685  * @tgt:                ibmvfc target struct
3686  *
3687  * If an ADISC times out, send a cancel. If the cancel times
3688  * out, reset the CRQ. When the ADISC comes back as cancelled,
3689  * log back into the target.
3690  **/
3691 static void ibmvfc_adisc_timeout(struct timer_list *t)
3692 {
3693         struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
3694         struct ibmvfc_host *vhost = tgt->vhost;
3695         struct ibmvfc_event *evt;
3696         struct ibmvfc_tmf *tmf;
3697         unsigned long flags;
3698         int rc;
3699
3700         tgt_dbg(tgt, "ADISC timeout\n");
3701         spin_lock_irqsave(vhost->host->host_lock, flags);
3702         if (vhost->abort_threads >= disc_threads ||
3703             tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3704             vhost->state != IBMVFC_INITIALIZING ||
3705             vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3706                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3707                 return;
3708         }
3709
3710         vhost->abort_threads++;
3711         kref_get(&tgt->kref);
3712         evt = ibmvfc_get_event(vhost);
3713         ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3714
3715         evt->tgt = tgt;
3716         tmf = &evt->iu.tmf;
3717         memset(tmf, 0, sizeof(*tmf));
3718         tmf->common.version = cpu_to_be32(1);
3719         tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3720         tmf->common.length = cpu_to_be16(sizeof(*tmf));
3721         tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3722         tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
3723
3724         rc = ibmvfc_send_event(evt, vhost, default_timeout);
3725
3726         if (rc) {
3727                 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3728                 vhost->abort_threads--;
3729                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3730                 __ibmvfc_reset_host(vhost);
3731         } else
3732                 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3733         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3734 }
3735
3736 /**
3737  * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3738  * @tgt:                ibmvfc target struct
3739  *
3740  * When sending an ADISC we end up with two timers running. The
3741  * first timer is the timer in the ibmvfc target struct. If this
3742  * fires, we send a cancel to the target. The second timer is the
3743  * timer on the ibmvfc event for the ADISC, which is longer. If that
3744  * fires, it means the ADISC timed out and our attempt to cancel it
3745  * also failed, so we need to reset the CRQ.
3746  **/
3747 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3748 {
3749         struct ibmvfc_passthru_mad *mad;
3750         struct ibmvfc_host *vhost = tgt->vhost;
3751         struct ibmvfc_event *evt;
3752
3753         if (vhost->discovery_threads >= disc_threads)
3754                 return;
3755
3756         kref_get(&tgt->kref);
3757         evt = ibmvfc_get_event(vhost);
3758         vhost->discovery_threads++;
3759         ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3760         evt->tgt = tgt;
3761
3762         ibmvfc_init_passthru(evt);
3763         mad = &evt->iu.passthru;
3764         mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
3765         mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
3766         mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
3767
3768         mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
3769         memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3770                sizeof(vhost->login_buf->resp.port_name));
3771         memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3772                sizeof(vhost->login_buf->resp.node_name));
3773         mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
3774
3775         if (timer_pending(&tgt->timer))
3776                 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
3777         else {
3778                 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
3779                 add_timer(&tgt->timer);
3780         }
3781
3782         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3783         if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
3784                 vhost->discovery_threads--;
3785                 del_timer(&tgt->timer);
3786                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3787                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3788         } else
3789                 tgt_dbg(tgt, "Sent ADISC\n");
3790 }
3791
3792 /**
3793  * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3794  * @evt:        ibmvfc event struct
3795  *
3796  **/
3797 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3798 {
3799         struct ibmvfc_target *tgt = evt->tgt;
3800         struct ibmvfc_host *vhost = evt->vhost;
3801         struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3802         u32 status = be16_to_cpu(rsp->common.status);
3803         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3804
3805         vhost->discovery_threads--;
3806         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3807         switch (status) {
3808         case IBMVFC_MAD_SUCCESS:
3809                 tgt_dbg(tgt, "Query Target succeeded\n");
3810                 tgt->new_scsi_id = be64_to_cpu(rsp->scsi_id);
3811                 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
3812                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3813                 else
3814                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
3815                 break;
3816         case IBMVFC_MAD_DRIVER_FAILED:
3817                 break;
3818         case IBMVFC_MAD_CRQ_ERROR:
3819                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3820                 break;
3821         case IBMVFC_MAD_FAILED:
3822         default:
3823                 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3824                     be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3825                     be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
3826                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3827                 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3828                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3829                 else
3830                         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3831
3832                 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3833                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3834                         rsp->status, rsp->error, ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)),
3835                         rsp->fc_type, ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)),
3836                         rsp->fc_explain, status);
3837                 break;
3838         }
3839
3840         kref_put(&tgt->kref, ibmvfc_release_tgt);
3841         ibmvfc_free_event(evt);
3842         wake_up(&vhost->work_wait_q);
3843 }
3844
3845 /**
3846  * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3847  * @tgt:        ibmvfc target struct
3848  *
3849  **/
3850 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3851 {
3852         struct ibmvfc_query_tgt *query_tgt;
3853         struct ibmvfc_host *vhost = tgt->vhost;
3854         struct ibmvfc_event *evt;
3855
3856         if (vhost->discovery_threads >= disc_threads)
3857                 return;
3858
3859         kref_get(&tgt->kref);
3860         evt = ibmvfc_get_event(vhost);
3861         vhost->discovery_threads++;
3862         evt->tgt = tgt;
3863         ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3864         query_tgt = &evt->iu.query_tgt;
3865         memset(query_tgt, 0, sizeof(*query_tgt));
3866         query_tgt->common.version = cpu_to_be32(1);
3867         query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
3868         query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
3869         query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
3870
3871         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3872         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3873                 vhost->discovery_threads--;
3874                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3875                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3876         } else
3877                 tgt_dbg(tgt, "Sent Query Target\n");
3878 }
3879
3880 /**
3881  * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3882  * @vhost:              ibmvfc host struct
3883  * @scsi_id:    SCSI ID to allocate target for
3884  *
3885  * Returns:
3886  *      0 on success / other on failure
3887  **/
3888 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3889 {
3890         struct ibmvfc_target *tgt;
3891         unsigned long flags;
3892
3893         spin_lock_irqsave(vhost->host->host_lock, flags);
3894         list_for_each_entry(tgt, &vhost->targets, queue) {
3895                 if (tgt->scsi_id == scsi_id) {
3896                         if (tgt->need_login)
3897                                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3898                         goto unlock_out;
3899                 }
3900         }
3901         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3902
3903         tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
3904         memset(tgt, 0, sizeof(*tgt));
3905         tgt->scsi_id = scsi_id;
3906         tgt->new_scsi_id = scsi_id;
3907         tgt->vhost = vhost;
3908         tgt->need_login = 1;
3909         tgt->cancel_key = vhost->task_set++;
3910         timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
3911         kref_init(&tgt->kref);
3912         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3913         spin_lock_irqsave(vhost->host->host_lock, flags);
3914         list_add_tail(&tgt->queue, &vhost->targets);
3915
3916 unlock_out:
3917         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3918         return 0;
3919 }
3920
3921 /**
3922  * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3923  * @vhost:              ibmvfc host struct
3924  *
3925  * Returns:
3926  *      0 on success / other on failure
3927  **/
3928 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3929 {
3930         int i, rc;
3931
3932         for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3933                 rc = ibmvfc_alloc_target(vhost,
3934                                          be32_to_cpu(vhost->disc_buf->scsi_id[i]) &
3935                                          IBMVFC_DISC_TGT_SCSI_ID_MASK);
3936
3937         return rc;
3938 }
3939
3940 /**
3941  * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3942  * @evt:        ibmvfc event struct
3943  *
3944  **/
3945 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3946 {
3947         struct ibmvfc_host *vhost = evt->vhost;
3948         struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3949         u32 mad_status = be16_to_cpu(rsp->common.status);
3950         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3951
3952         switch (mad_status) {
3953         case IBMVFC_MAD_SUCCESS:
3954                 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3955                 vhost->num_targets = be32_to_cpu(rsp->num_written);
3956                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3957                 break;
3958         case IBMVFC_MAD_FAILED:
3959                 level += ibmvfc_retry_host_init(vhost);
3960                 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
3961                            ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3962                            rsp->status, rsp->error);
3963                 break;
3964         case IBMVFC_MAD_DRIVER_FAILED:
3965                 break;
3966         default:
3967                 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3968                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3969                 break;
3970         }
3971
3972         ibmvfc_free_event(evt);
3973         wake_up(&vhost->work_wait_q);
3974 }
3975
3976 /**
3977  * ibmvfc_discover_targets - Send Discover Targets MAD
3978  * @vhost:      ibmvfc host struct
3979  *
3980  **/
3981 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3982 {
3983         struct ibmvfc_discover_targets *mad;
3984         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3985
3986         ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3987         mad = &evt->iu.discover_targets;
3988         memset(mad, 0, sizeof(*mad));
3989         mad->common.version = cpu_to_be32(1);
3990         mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
3991         mad->common.length = cpu_to_be16(sizeof(*mad));
3992         mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
3993         mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
3994         mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
3995         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3996
3997         if (!ibmvfc_send_event(evt, vhost, default_timeout))
3998                 ibmvfc_dbg(vhost, "Sent discover targets\n");
3999         else
4000                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4001 }
4002
4003 /**
4004  * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4005  * @evt:        ibmvfc event struct
4006  *
4007  **/
4008 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4009 {
4010         struct ibmvfc_host *vhost = evt->vhost;
4011         u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4012         struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4013         unsigned int npiv_max_sectors;
4014         int level = IBMVFC_DEFAULT_LOG_LEVEL;
4015
4016         switch (mad_status) {
4017         case IBMVFC_MAD_SUCCESS:
4018                 ibmvfc_free_event(evt);
4019                 break;
4020         case IBMVFC_MAD_FAILED:
4021                 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4022                         level += ibmvfc_retry_host_init(vhost);
4023                 else
4024                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4025                 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4026                            ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4027                                                 rsp->status, rsp->error);
4028                 ibmvfc_free_event(evt);
4029                 return;
4030         case IBMVFC_MAD_CRQ_ERROR:
4031                 ibmvfc_retry_host_init(vhost);
4032         case IBMVFC_MAD_DRIVER_FAILED:
4033                 ibmvfc_free_event(evt);
4034                 return;
4035         default:
4036                 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4037                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4038                 ibmvfc_free_event(evt);
4039                 return;
4040         }
4041
4042         vhost->client_migrated = 0;
4043
4044         if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4045                 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4046                         rsp->flags);
4047                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4048                 wake_up(&vhost->work_wait_q);
4049                 return;
4050         }
4051
4052         if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
4053                 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4054                         rsp->max_cmds);
4055                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4056                 wake_up(&vhost->work_wait_q);
4057                 return;
4058         }
4059
4060         vhost->logged_in = 1;
4061         npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
4062         dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4063                  rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4064                  rsp->drc_name, npiv_max_sectors);
4065
4066         fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4067         fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4068         fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4069         fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
4070         fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4071         fc_host_supported_classes(vhost->host) = 0;
4072         if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
4073                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4074         if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
4075                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4076         if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
4077                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4078         fc_host_maxframe_size(vhost->host) =
4079                 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
4080
4081         vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
4082         vhost->host->max_sectors = npiv_max_sectors;
4083         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4084         wake_up(&vhost->work_wait_q);
4085 }
4086
4087 /**
4088  * ibmvfc_npiv_login - Sends NPIV login
4089  * @vhost:      ibmvfc host struct
4090  *
4091  **/
4092 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4093 {
4094         struct ibmvfc_npiv_login_mad *mad;
4095         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4096
4097         ibmvfc_gather_partition_info(vhost);
4098         ibmvfc_set_login_info(vhost);
4099         ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4100
4101         memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4102         mad = &evt->iu.npiv_login;
4103         memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4104         mad->common.version = cpu_to_be32(1);
4105         mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4106         mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4107         mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4108         mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
4109
4110         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4111
4112         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4113                 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4114         else
4115                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4116 };
4117
4118 /**
4119  * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4120  * @vhost:              ibmvfc host struct
4121  *
4122  **/
4123 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4124 {
4125         struct ibmvfc_host *vhost = evt->vhost;
4126         u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
4127
4128         ibmvfc_free_event(evt);
4129
4130         switch (mad_status) {
4131         case IBMVFC_MAD_SUCCESS:
4132                 if (list_empty(&vhost->sent) &&
4133                     vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
4134                         ibmvfc_init_host(vhost);
4135                         return;
4136                 }
4137                 break;
4138         case IBMVFC_MAD_FAILED:
4139         case IBMVFC_MAD_NOT_SUPPORTED:
4140         case IBMVFC_MAD_CRQ_ERROR:
4141         case IBMVFC_MAD_DRIVER_FAILED:
4142         default:
4143                 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4144                 break;
4145         }
4146
4147         ibmvfc_hard_reset_host(vhost);
4148 }
4149
4150 /**
4151  * ibmvfc_npiv_logout - Issue an NPIV Logout
4152  * @vhost:              ibmvfc host struct
4153  *
4154  **/
4155 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4156 {
4157         struct ibmvfc_npiv_logout_mad *mad;
4158         struct ibmvfc_event *evt;
4159
4160         evt = ibmvfc_get_event(vhost);
4161         ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4162
4163         mad = &evt->iu.npiv_logout;
4164         memset(mad, 0, sizeof(*mad));
4165         mad->common.version = cpu_to_be32(1);
4166         mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4167         mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
4168
4169         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4170
4171         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4172                 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4173         else
4174                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4175 }
4176
4177 /**
4178  * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4179  * @vhost:              ibmvfc host struct
4180  *
4181  * Returns:
4182  *      1 if work to do / 0 if not
4183  **/
4184 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4185 {
4186         struct ibmvfc_target *tgt;
4187
4188         list_for_each_entry(tgt, &vhost->targets, queue) {
4189                 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4190                     tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4191                         return 1;
4192         }
4193
4194         return 0;
4195 }
4196
4197 /**
4198  * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4199  * @vhost:              ibmvfc host struct
4200  *
4201  * Returns:
4202  *      1 if work to do / 0 if not
4203  **/
4204 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4205 {
4206         struct ibmvfc_target *tgt;
4207
4208         if (kthread_should_stop())
4209                 return 1;
4210         switch (vhost->action) {
4211         case IBMVFC_HOST_ACTION_NONE:
4212         case IBMVFC_HOST_ACTION_INIT_WAIT:
4213         case IBMVFC_HOST_ACTION_LOGO_WAIT:
4214                 return 0;
4215         case IBMVFC_HOST_ACTION_TGT_INIT:
4216         case IBMVFC_HOST_ACTION_QUERY_TGTS:
4217                 if (vhost->discovery_threads == disc_threads)
4218                         return 0;
4219                 list_for_each_entry(tgt, &vhost->targets, queue)
4220                         if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4221                                 return 1;
4222                 list_for_each_entry(tgt, &vhost->targets, queue)
4223                         if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4224                                 return 0;
4225                 return 1;
4226         case IBMVFC_HOST_ACTION_LOGO:
4227         case IBMVFC_HOST_ACTION_INIT:
4228         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4229         case IBMVFC_HOST_ACTION_TGT_DEL:
4230         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4231         case IBMVFC_HOST_ACTION_QUERY:
4232         case IBMVFC_HOST_ACTION_RESET:
4233         case IBMVFC_HOST_ACTION_REENABLE:
4234         default:
4235                 break;
4236         }
4237
4238         return 1;
4239 }
4240
4241 /**
4242  * ibmvfc_work_to_do - Is there task level work to do?
4243  * @vhost:              ibmvfc host struct
4244  *
4245  * Returns:
4246  *      1 if work to do / 0 if not
4247  **/
4248 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4249 {
4250         unsigned long flags;
4251         int rc;
4252
4253         spin_lock_irqsave(vhost->host->host_lock, flags);
4254         rc = __ibmvfc_work_to_do(vhost);
4255         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4256         return rc;
4257 }
4258
4259 /**
4260  * ibmvfc_log_ae - Log async events if necessary
4261  * @vhost:              ibmvfc host struct
4262  * @events:             events to log
4263  *
4264  **/
4265 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4266 {
4267         if (events & IBMVFC_AE_RSCN)
4268                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4269         if ((events & IBMVFC_AE_LINKDOWN) &&
4270             vhost->state >= IBMVFC_HALTED)
4271                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4272         if ((events & IBMVFC_AE_LINKUP) &&
4273             vhost->state == IBMVFC_INITIALIZING)
4274                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4275 }
4276
4277 /**
4278  * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4279  * @tgt:                ibmvfc target struct
4280  *
4281  **/
4282 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4283 {
4284         struct ibmvfc_host *vhost = tgt->vhost;
4285         struct fc_rport *rport;
4286         unsigned long flags;
4287
4288         tgt_dbg(tgt, "Adding rport\n");
4289         rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4290         spin_lock_irqsave(vhost->host->host_lock, flags);
4291
4292         if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4293                 tgt_dbg(tgt, "Deleting rport\n");
4294                 list_del(&tgt->queue);
4295                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4296                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4297                 fc_remote_port_delete(rport);
4298                 del_timer_sync(&tgt->timer);
4299                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4300                 return;
4301         } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4302                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4303                 return;
4304         }
4305
4306         if (rport) {
4307                 tgt_dbg(tgt, "rport add succeeded\n");
4308                 tgt->rport = rport;
4309                 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
4310                 rport->supported_classes = 0;
4311                 tgt->target_id = rport->scsi_target_id;
4312                 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
4313                         rport->supported_classes |= FC_COS_CLASS1;
4314                 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
4315                         rport->supported_classes |= FC_COS_CLASS2;
4316                 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
4317                         rport->supported_classes |= FC_COS_CLASS3;
4318                 if (rport->rqst_q)
4319                         blk_queue_max_segments(rport->rqst_q, 1);
4320         } else
4321                 tgt_dbg(tgt, "rport add failed\n");
4322         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4323 }
4324
4325 /**
4326  * ibmvfc_do_work - Do task level work
4327  * @vhost:              ibmvfc host struct
4328  *
4329  **/
4330 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4331 {
4332         struct ibmvfc_target *tgt;
4333         unsigned long flags;
4334         struct fc_rport *rport;
4335         int rc;
4336
4337         ibmvfc_log_ae(vhost, vhost->events_to_log);
4338         spin_lock_irqsave(vhost->host->host_lock, flags);
4339         vhost->events_to_log = 0;
4340         switch (vhost->action) {
4341         case IBMVFC_HOST_ACTION_NONE:
4342         case IBMVFC_HOST_ACTION_LOGO_WAIT:
4343         case IBMVFC_HOST_ACTION_INIT_WAIT:
4344                 break;
4345         case IBMVFC_HOST_ACTION_RESET:
4346                 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4347                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4348                 rc = ibmvfc_reset_crq(vhost);
4349                 spin_lock_irqsave(vhost->host->host_lock, flags);
4350                 if (rc == H_CLOSED)
4351                         vio_enable_interrupts(to_vio_dev(vhost->dev));
4352                 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4353                     (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
4354                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4355                         dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4356                 }
4357                 break;
4358         case IBMVFC_HOST_ACTION_REENABLE:
4359                 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4360                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4361                 rc = ibmvfc_reenable_crq_queue(vhost);
4362                 spin_lock_irqsave(vhost->host->host_lock, flags);
4363                 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4364                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4365                         dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4366                 }
4367                 break;
4368         case IBMVFC_HOST_ACTION_LOGO:
4369                 vhost->job_step(vhost);
4370                 break;
4371         case IBMVFC_HOST_ACTION_INIT:
4372                 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
4373                 if (vhost->delay_init) {
4374                         vhost->delay_init = 0;
4375                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4376                         ssleep(15);
4377                         return;
4378                 } else
4379                         vhost->job_step(vhost);
4380                 break;
4381         case IBMVFC_HOST_ACTION_QUERY:
4382                 list_for_each_entry(tgt, &vhost->targets, queue)
4383                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4384                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4385                 break;
4386         case IBMVFC_HOST_ACTION_QUERY_TGTS:
4387                 list_for_each_entry(tgt, &vhost->targets, queue) {
4388                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4389                                 tgt->job_step(tgt);
4390                                 break;
4391                         }
4392                 }
4393
4394                 if (!ibmvfc_dev_init_to_do(vhost))
4395                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4396                 break;
4397         case IBMVFC_HOST_ACTION_TGT_DEL:
4398         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4399                 list_for_each_entry(tgt, &vhost->targets, queue) {
4400                         if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4401                                 tgt_dbg(tgt, "Deleting rport\n");
4402                                 rport = tgt->rport;
4403                                 tgt->rport = NULL;
4404                                 list_del(&tgt->queue);
4405                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4406                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4407                                 if (rport)
4408                                         fc_remote_port_delete(rport);
4409                                 del_timer_sync(&tgt->timer);
4410                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4411                                 return;
4412                         }
4413                 }
4414
4415                 if (vhost->state == IBMVFC_INITIALIZING) {
4416                         if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
4417                                 if (vhost->reinit) {
4418                                         vhost->reinit = 0;
4419                                         scsi_block_requests(vhost->host);
4420                                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4421                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4422                                 } else {
4423                                         ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4424                                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4425                                         wake_up(&vhost->init_wait_q);
4426                                         schedule_work(&vhost->rport_add_work_q);
4427                                         vhost->init_retries = 0;
4428                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4429                                         scsi_unblock_requests(vhost->host);
4430                                 }
4431
4432                                 return;
4433                         } else {
4434                                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4435                                 vhost->job_step = ibmvfc_discover_targets;
4436                         }
4437                 } else {
4438                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4439                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4440                         scsi_unblock_requests(vhost->host);
4441                         wake_up(&vhost->init_wait_q);
4442                         return;
4443                 }
4444                 break;
4445         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4446                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4447                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4448                 ibmvfc_alloc_targets(vhost);
4449                 spin_lock_irqsave(vhost->host->host_lock, flags);
4450                 break;
4451         case IBMVFC_HOST_ACTION_TGT_INIT:
4452                 list_for_each_entry(tgt, &vhost->targets, queue) {
4453                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4454                                 tgt->job_step(tgt);
4455                                 break;
4456                         }
4457                 }
4458
4459                 if (!ibmvfc_dev_init_to_do(vhost))
4460                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
4461                 break;
4462         default:
4463                 break;
4464         }
4465
4466         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4467 }
4468
4469 /**
4470  * ibmvfc_work - Do task level work
4471  * @data:               ibmvfc host struct
4472  *
4473  * Returns:
4474  *      zero
4475  **/
4476 static int ibmvfc_work(void *data)
4477 {
4478         struct ibmvfc_host *vhost = data;
4479         int rc;
4480
4481         set_user_nice(current, MIN_NICE);
4482
4483         while (1) {
4484                 rc = wait_event_interruptible(vhost->work_wait_q,
4485                                               ibmvfc_work_to_do(vhost));
4486
4487                 BUG_ON(rc);
4488
4489                 if (kthread_should_stop())
4490                         break;
4491
4492                 ibmvfc_do_work(vhost);
4493         }
4494
4495         ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4496         return 0;
4497 }
4498
4499 /**
4500  * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4501  * @vhost:      ibmvfc host struct
4502  *
4503  * Allocates a page for messages, maps it for dma, and registers
4504  * the crq with the hypervisor.
4505  *
4506  * Return value:
4507  *      zero on success / other on failure
4508  **/
4509 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4510 {
4511         int rc, retrc = -ENOMEM;
4512         struct device *dev = vhost->dev;
4513         struct vio_dev *vdev = to_vio_dev(dev);
4514         struct ibmvfc_crq_queue *crq = &vhost->crq;
4515
4516         ENTER;
4517         crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4518
4519         if (!crq->msgs)
4520                 return -ENOMEM;
4521
4522         crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4523         crq->msg_token = dma_map_single(dev, crq->msgs,
4524                                         PAGE_SIZE, DMA_BIDIRECTIONAL);
4525
4526         if (dma_mapping_error(dev, crq->msg_token))
4527                 goto map_failed;
4528
4529         retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4530                                         crq->msg_token, PAGE_SIZE);
4531
4532         if (rc == H_RESOURCE)
4533                 /* maybe kexecing and resource is busy. try a reset */
4534                 retrc = rc = ibmvfc_reset_crq(vhost);
4535
4536         if (rc == H_CLOSED)
4537                 dev_warn(dev, "Partner adapter not ready\n");
4538         else if (rc) {
4539                 dev_warn(dev, "Error %d opening adapter\n", rc);
4540                 goto reg_crq_failed;
4541         }
4542
4543         retrc = 0;
4544
4545         tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4546
4547         if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4548                 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4549                 goto req_irq_failed;
4550         }
4551
4552         if ((rc = vio_enable_interrupts(vdev))) {
4553                 dev_err(dev, "Error %d enabling interrupts\n", rc);
4554                 goto req_irq_failed;
4555         }
4556
4557         crq->cur = 0;
4558         LEAVE;
4559         return retrc;
4560
4561 req_irq_failed:
4562         tasklet_kill(&vhost->tasklet);
4563         do {
4564                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4565         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4566 reg_crq_failed:
4567         dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4568 map_failed:
4569         free_page((unsigned long)crq->msgs);
4570         return retrc;
4571 }
4572
4573 /**
4574  * ibmvfc_free_mem - Free memory for vhost
4575  * @vhost:      ibmvfc host struct
4576  *
4577  * Return value:
4578  *      none
4579  **/
4580 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4581 {
4582         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4583
4584         ENTER;
4585         mempool_destroy(vhost->tgt_pool);
4586         kfree(vhost->trace);
4587         dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4588                           vhost->disc_buf_dma);
4589         dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4590                           vhost->login_buf, vhost->login_buf_dma);
4591         dma_pool_destroy(vhost->sg_pool);
4592         dma_unmap_single(vhost->dev, async_q->msg_token,
4593                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4594         free_page((unsigned long)async_q->msgs);
4595         LEAVE;
4596 }
4597
4598 /**
4599  * ibmvfc_alloc_mem - Allocate memory for vhost
4600  * @vhost:      ibmvfc host struct
4601  *
4602  * Return value:
4603  *      0 on success / non-zero on failure
4604  **/
4605 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4606 {
4607         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4608         struct device *dev = vhost->dev;
4609
4610         ENTER;
4611         async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4612         if (!async_q->msgs) {
4613                 dev_err(dev, "Couldn't allocate async queue.\n");
4614                 goto nomem;
4615         }
4616
4617         async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4618         async_q->msg_token = dma_map_single(dev, async_q->msgs,
4619                                             async_q->size * sizeof(*async_q->msgs),
4620                                             DMA_BIDIRECTIONAL);
4621
4622         if (dma_mapping_error(dev, async_q->msg_token)) {
4623                 dev_err(dev, "Failed to map async queue\n");
4624                 goto free_async_crq;
4625         }
4626
4627         vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4628                                          SG_ALL * sizeof(struct srp_direct_buf),
4629                                          sizeof(struct srp_direct_buf), 0);
4630
4631         if (!vhost->sg_pool) {
4632                 dev_err(dev, "Failed to allocate sg pool\n");
4633                 goto unmap_async_crq;
4634         }
4635
4636         vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4637                                               &vhost->login_buf_dma, GFP_KERNEL);
4638
4639         if (!vhost->login_buf) {
4640                 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4641                 goto free_sg_pool;
4642         }
4643
4644         vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
4645         vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
4646                                              &vhost->disc_buf_dma, GFP_KERNEL);
4647
4648         if (!vhost->disc_buf) {
4649                 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
4650                 goto free_login_buffer;
4651         }
4652
4653         vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
4654                                sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
4655
4656         if (!vhost->trace)
4657                 goto free_disc_buffer;
4658
4659         vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
4660                                                       sizeof(struct ibmvfc_target));
4661
4662         if (!vhost->tgt_pool) {
4663                 dev_err(dev, "Couldn't allocate target memory pool\n");
4664                 goto free_trace;
4665         }
4666
4667         LEAVE;
4668         return 0;
4669
4670 free_trace:
4671         kfree(vhost->trace);
4672 free_disc_buffer:
4673         dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
4674                           vhost->disc_buf_dma);
4675 free_login_buffer:
4676         dma_free_coherent(dev, sizeof(*vhost->login_buf),
4677                           vhost->login_buf, vhost->login_buf_dma);
4678 free_sg_pool:
4679         dma_pool_destroy(vhost->sg_pool);
4680 unmap_async_crq:
4681         dma_unmap_single(dev, async_q->msg_token,
4682                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4683 free_async_crq:
4684         free_page((unsigned long)async_q->msgs);
4685 nomem:
4686         LEAVE;
4687         return -ENOMEM;
4688 }
4689
4690 /**
4691  * ibmvfc_rport_add_thread - Worker thread for rport adds
4692  * @work:       work struct
4693  *
4694  **/
4695 static void ibmvfc_rport_add_thread(struct work_struct *work)
4696 {
4697         struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
4698                                                  rport_add_work_q);
4699         struct ibmvfc_target *tgt;
4700         struct fc_rport *rport;
4701         unsigned long flags;
4702         int did_work;
4703
4704         ENTER;
4705         spin_lock_irqsave(vhost->host->host_lock, flags);
4706         do {
4707                 did_work = 0;
4708                 if (vhost->state != IBMVFC_ACTIVE)
4709                         break;
4710
4711                 list_for_each_entry(tgt, &vhost->targets, queue) {
4712                         if (tgt->add_rport) {
4713                                 did_work = 1;
4714                                 tgt->add_rport = 0;
4715                                 kref_get(&tgt->kref);
4716                                 rport = tgt->rport;
4717                                 if (!rport) {
4718                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4719                                         ibmvfc_tgt_add_rport(tgt);
4720                                 } else if (get_device(&rport->dev)) {
4721                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4722                                         tgt_dbg(tgt, "Setting rport roles\n");
4723                                         fc_remote_port_rolechg(rport, tgt->ids.roles);
4724                                         put_device(&rport->dev);
4725                                 } else {
4726                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4727                                 }
4728
4729                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4730                                 spin_lock_irqsave(vhost->host->host_lock, flags);
4731                                 break;
4732                         }
4733                 }
4734         } while(did_work);
4735
4736         if (vhost->state == IBMVFC_ACTIVE)
4737                 vhost->scan_complete = 1;
4738         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4739         LEAVE;
4740 }
4741
4742 /**
4743  * ibmvfc_probe - Adapter hot plug add entry point
4744  * @vdev:       vio device struct
4745  * @id: vio device id struct
4746  *
4747  * Return value:
4748  *      0 on success / non-zero on failure
4749  **/
4750 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4751 {
4752         struct ibmvfc_host *vhost;
4753         struct Scsi_Host *shost;
4754         struct device *dev = &vdev->dev;
4755         int rc = -ENOMEM;
4756
4757         ENTER;
4758         shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
4759         if (!shost) {
4760                 dev_err(dev, "Couldn't allocate host data\n");
4761                 goto out;
4762         }
4763
4764         shost->transportt = ibmvfc_transport_template;
4765         shost->can_queue = max_requests;
4766         shost->max_lun = max_lun;
4767         shost->max_id = max_targets;
4768         shost->max_sectors = IBMVFC_MAX_SECTORS;
4769         shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
4770         shost->unique_id = shost->host_no;
4771
4772         vhost = shost_priv(shost);
4773         INIT_LIST_HEAD(&vhost->sent);
4774         INIT_LIST_HEAD(&vhost->free);
4775         INIT_LIST_HEAD(&vhost->targets);
4776         sprintf(vhost->name, IBMVFC_NAME);
4777         vhost->host = shost;
4778         vhost->dev = dev;
4779         vhost->partition_number = -1;
4780         vhost->log_level = log_level;
4781         vhost->task_set = 1;
4782         strcpy(vhost->partition_name, "UNKNOWN");
4783         init_waitqueue_head(&vhost->work_wait_q);
4784         init_waitqueue_head(&vhost->init_wait_q);
4785         INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
4786         mutex_init(&vhost->passthru_mutex);
4787
4788         if ((rc = ibmvfc_alloc_mem(vhost)))
4789                 goto free_scsi_host;
4790
4791         vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
4792                                          shost->host_no);
4793
4794         if (IS_ERR(vhost->work_thread)) {
4795                 dev_err(dev, "Couldn't create kernel thread: %ld\n",
4796                         PTR_ERR(vhost->work_thread));
4797                 goto free_host_mem;
4798         }
4799
4800         if ((rc = ibmvfc_init_crq(vhost))) {
4801                 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4802                 goto kill_kthread;
4803         }
4804
4805         if ((rc = ibmvfc_init_event_pool(vhost))) {
4806                 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4807                 goto release_crq;
4808         }
4809
4810         if ((rc = scsi_add_host(shost, dev)))
4811                 goto release_event_pool;
4812
4813         fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
4814
4815         if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4816                                            &ibmvfc_trace_attr))) {
4817                 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4818                 goto remove_shost;
4819         }
4820
4821         if (shost_to_fc_host(shost)->rqst_q)
4822                 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
4823         dev_set_drvdata(dev, vhost);
4824         spin_lock(&ibmvfc_driver_lock);
4825         list_add_tail(&vhost->queue, &ibmvfc_head);
4826         spin_unlock(&ibmvfc_driver_lock);
4827
4828         ibmvfc_send_crq_init(vhost);
4829         scsi_scan_host(shost);
4830         return 0;
4831
4832 remove_shost:
4833         scsi_remove_host(shost);
4834 release_event_pool:
4835         ibmvfc_free_event_pool(vhost);
4836 release_crq:
4837         ibmvfc_release_crq_queue(vhost);
4838 kill_kthread:
4839         kthread_stop(vhost->work_thread);
4840 free_host_mem:
4841         ibmvfc_free_mem(vhost);
4842 free_scsi_host:
4843         scsi_host_put(shost);
4844 out:
4845         LEAVE;
4846         return rc;
4847 }
4848
4849 /**
4850  * ibmvfc_remove - Adapter hot plug remove entry point
4851  * @vdev:       vio device struct
4852  *
4853  * Return value:
4854  *      0
4855  **/
4856 static int ibmvfc_remove(struct vio_dev *vdev)
4857 {
4858         struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4859         unsigned long flags;
4860
4861         ENTER;
4862         ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
4863
4864         spin_lock_irqsave(vhost->host->host_lock, flags);
4865         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
4866         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4867
4868         ibmvfc_wait_while_resetting(vhost);
4869         ibmvfc_release_crq_queue(vhost);
4870         kthread_stop(vhost->work_thread);
4871         fc_remove_host(vhost->host);
4872         scsi_remove_host(vhost->host);
4873
4874         spin_lock_irqsave(vhost->host->host_lock, flags);
4875         ibmvfc_purge_requests(vhost, DID_ERROR);
4876         ibmvfc_free_event_pool(vhost);
4877         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4878
4879         ibmvfc_free_mem(vhost);
4880         spin_lock(&ibmvfc_driver_lock);
4881         list_del(&vhost->queue);
4882         spin_unlock(&ibmvfc_driver_lock);
4883         scsi_host_put(vhost->host);
4884         LEAVE;
4885         return 0;
4886 }
4887
4888 /**
4889  * ibmvfc_resume - Resume from suspend
4890  * @dev:        device struct
4891  *
4892  * We may have lost an interrupt across suspend/resume, so kick the
4893  * interrupt handler
4894  *
4895  */
4896 static int ibmvfc_resume(struct device *dev)
4897 {
4898         unsigned long flags;
4899         struct ibmvfc_host *vhost = dev_get_drvdata(dev);
4900         struct vio_dev *vdev = to_vio_dev(dev);
4901
4902         spin_lock_irqsave(vhost->host->host_lock, flags);
4903         vio_disable_interrupts(vdev);
4904         tasklet_schedule(&vhost->tasklet);
4905         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4906         return 0;
4907 }
4908
4909 /**
4910  * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4911  * @vdev:       vio device struct
4912  *
4913  * Return value:
4914  *      Number of bytes the driver will need to DMA map at the same time in
4915  *      order to perform well.
4916  */
4917 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4918 {
4919         unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4920         return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4921 }
4922
4923 static const struct vio_device_id ibmvfc_device_table[] = {
4924         {"fcp", "IBM,vfc-client"},
4925         { "", "" }
4926 };
4927 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4928
4929 static const struct dev_pm_ops ibmvfc_pm_ops = {
4930         .resume = ibmvfc_resume
4931 };
4932
4933 static struct vio_driver ibmvfc_driver = {
4934         .id_table = ibmvfc_device_table,
4935         .probe = ibmvfc_probe,
4936         .remove = ibmvfc_remove,
4937         .get_desired_dma = ibmvfc_get_desired_dma,
4938         .name = IBMVFC_NAME,
4939         .pm = &ibmvfc_pm_ops,
4940 };
4941
4942 static struct fc_function_template ibmvfc_transport_functions = {
4943         .show_host_fabric_name = 1,
4944         .show_host_node_name = 1,
4945         .show_host_port_name = 1,
4946         .show_host_supported_classes = 1,
4947         .show_host_port_type = 1,
4948         .show_host_port_id = 1,
4949         .show_host_maxframe_size = 1,
4950
4951         .get_host_port_state = ibmvfc_get_host_port_state,
4952         .show_host_port_state = 1,
4953
4954         .get_host_speed = ibmvfc_get_host_speed,
4955         .show_host_speed = 1,
4956
4957         .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4958         .terminate_rport_io = ibmvfc_terminate_rport_io,
4959
4960         .show_rport_maxframe_size = 1,
4961         .show_rport_supported_classes = 1,
4962
4963         .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4964         .show_rport_dev_loss_tmo = 1,
4965
4966         .get_starget_node_name = ibmvfc_get_starget_node_name,
4967         .show_starget_node_name = 1,
4968
4969         .get_starget_port_name = ibmvfc_get_starget_port_name,
4970         .show_starget_port_name = 1,
4971
4972         .get_starget_port_id = ibmvfc_get_starget_port_id,
4973         .show_starget_port_id = 1,
4974
4975         .bsg_request = ibmvfc_bsg_request,
4976         .bsg_timeout = ibmvfc_bsg_timeout,
4977 };
4978
4979 /**
4980  * ibmvfc_module_init - Initialize the ibmvfc module
4981  *
4982  * Return value:
4983  *      0 on success / other on failure
4984  **/
4985 static int __init ibmvfc_module_init(void)
4986 {
4987         int rc;
4988
4989         if (!firmware_has_feature(FW_FEATURE_VIO))
4990                 return -ENODEV;
4991
4992         printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4993                IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4994
4995         ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4996         if (!ibmvfc_transport_template)
4997                 return -ENOMEM;
4998
4999         rc = vio_register_driver(&ibmvfc_driver);
5000         if (rc)
5001                 fc_release_transport(ibmvfc_transport_template);
5002         return rc;
5003 }
5004
5005 /**
5006  * ibmvfc_module_exit - Teardown the ibmvfc module
5007  *
5008  * Return value:
5009  *      nothing
5010  **/
5011 static void __exit ibmvfc_module_exit(void)
5012 {
5013         vio_unregister_driver(&ibmvfc_driver);
5014         fc_release_transport(ibmvfc_transport_template);
5015 }
5016
5017 module_init(ibmvfc_module_init);
5018 module_exit(ibmvfc_module_exit);