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