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