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