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