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