blob: 4a680ce774ca093d64bf612acddc728337aaf092 [file] [log] [blame]
Brian King072b91f2008-07-01 13:14:30 -05001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Brian King072b91f2008-07-01 13:14:30 -050032#include <linux/of.h>
Brian Kingb0f4d4c2010-02-21 10:37:58 -060033#include <linux/pm.h>
Brian King072b91f2008-07-01 13:14:30 -050034#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>
Brian Kingd31429e2009-10-19 15:07:54 -050044#include <scsi/scsi_bsg_fc.h>
Brian King072b91f2008-07-01 13:14:30 -050045#include "ibmvfc.h"
46
47static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
48static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
Hannes Reinecke1abf6352014-06-25 15:27:38 +020049static u64 max_lun = IBMVFC_MAX_LUN;
Brian King072b91f2008-07-01 13:14:30 -050050static 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;
Brian King072b91f2008-07-01 13:14:30 -050053static 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) "]");
Hannes Reinecke1abf6352014-06-25 15:27:38 +020074module_param_named(max_lun, max_lun, ullong, S_IRUGO);
Brian King072b91f2008-07-01 13:14:30 -050075MODULE_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) "]");
Brian King545ef9a2009-03-20 15:44:37 -050080module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
Brian King072b91f2008-07-01 13:14:30 -050081MODULE_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) "]");
Brian King072b91f2008-07-01 13:14:30 -050086module_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" },
Brian King752b3232008-12-15 17:09:05 -0600101 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
Brian King072b91f2008-07-01 13:14:30 -0500102 { 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" },
Brian King497f9c52009-05-28 16:17:30 -0500110 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
Brian King072b91f2008-07-01 13:14:30 -0500111 { 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" },
Brian King752b3232008-12-15 17:09:05 -0600115 { 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" },
Brian King072b91f2008-07-01 13:14:30 -0500117 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
Brian King752b3232008-12-15 17:09:05 -0600118 { 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" },
Brian King072b91f2008-07-01 13:14:30 -0500120 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
Brian King646d3852008-11-14 13:33:53 -0600121 { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
Brian King072b91f2008-07-01 13:14:30 -0500122 { 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 *);
Brian King79111d02009-05-28 16:17:29 -0500143static void ibmvfc_npiv_logout(struct ibmvfc_host *);
Brian King072b91f2008-07-01 13:14:30 -0500144
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];
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500169 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
Brian King072b91f2008-07-01 13:14:30 -0500170 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
171 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500172 entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
Brian King072b91f2008-07-01 13:14:30 -0500173 break;
174 case IBMVFC_MAD_FORMAT:
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500175 entry->op_code = be32_to_cpu(mad->opcode);
Brian King072b91f2008-07-01 13:14:30 -0500176 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];
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500202 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
Brian King072b91f2008-07-01 13:14:30 -0500203 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
204 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500205 entry->u.end.status = be16_to_cpu(vfc_cmd->status);
206 entry->u.end.error = be16_to_cpu(vfc_cmd->error);
Brian King072b91f2008-07-01 13:14:30 -0500207 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:
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500212 entry->op_code = be32_to_cpu(mad->opcode);
213 entry->u.end.status = be16_to_cpu(mad->status);
Brian King072b91f2008-07-01 13:14:30 -0500214 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;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500273 int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
Brian King072b91f2008-07-01 13:14:30 -0500274
275 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
Brian King4a2837d2009-05-28 16:17:22 -0500276 ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
Brian King072b91f2008-07-01 13:14:30 -0500277 rsp->data.info.rsp_code))
278 return DID_ERROR << 16;
279
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500280 err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
Brian King072b91f2008-07-01 13:14:30 -0500281 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:
Brian Kingd5da3042010-08-05 16:38:31 -0500430 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT)
431 tgt->action = action;
432 case IBMVFC_TGT_ACTION_DELETED_RPORT:
Brian King072b91f2008-07-01 13:14:30 -0500433 break;
434 default:
Brian King43c8da92009-05-28 16:17:28 -0500435 if (action == IBMVFC_TGT_ACTION_DEL_RPORT)
436 tgt->add_rport = 0;
Brian King072b91f2008-07-01 13:14:30 -0500437 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;
Brian King79111d02009-05-28 16:17:29 -0500481 case IBMVFC_HOST_ACTION_LOGO_WAIT:
482 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
483 vhost->action = action;
484 break;
Brian King072b91f2008-07-01 13:14:30 -0500485 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:
Brian King43c8da92009-05-28 16:17:28 -0500493 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -0500494 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:
Brian King73ee5d82010-06-17 13:55:13 -0500506 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:
Brian King072b91f2008-07-01 13:14:30 -0500516 case IBMVFC_HOST_ACTION_QUERY_TGTS:
Brian King10e79492008-10-29 08:46:45 -0500517 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -0500518 case IBMVFC_HOST_ACTION_NONE:
Brian King73ee5d82010-06-17 13:55:13 -0500519 case IBMVFC_HOST_ACTION_RESET:
520 case IBMVFC_HOST_ACTION_REENABLE:
Brian King072b91f2008-07-01 13:14:30 -0500521 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) {
Brian King2d0da2a2008-07-22 08:31:42 -0500537 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 }
Brian King072b91f2008-07-01 13:14:30 -0500541 } 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 **/
Brian King861890c2009-10-19 15:07:49 -0500576static void ibmvfc_init_host(struct ibmvfc_host *vhost)
Brian King072b91f2008-07-01 13:14:30 -0500577{
578 struct ibmvfc_target *tgt;
579
580 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
Brian King1c41fa822008-12-03 11:02:54 -0600581 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -0500582 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)) {
Brian King861890c2009-10-19 15:07:49 -0500590 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
591 vhost->async_crq.cur = 0;
Brian Kingcf6f10d2008-08-15 10:59:23 -0500592
Brian King072b91f2008-07-01 13:14:30 -0500593 list_for_each_entry(tgt, &vhost->targets, queue)
Brian King5e471672009-05-28 16:17:32 -0500594 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King072b91f2008-07-01 13:14:30 -0500595 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{
Brian King73ee5d82010-06-17 13:55:13 -0500652 long rc = 0;
Brian King072b91f2008-07-01 13:14:30 -0500653 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);
Brian King039a0892009-03-20 15:44:35 -0500658 tasklet_kill(&vhost->tasklet);
Brian King072b91f2008-07-01 13:14:30 -0500659 do {
Brian King73ee5d82010-06-17 13:55:13 -0500660 if (rc)
661 msleep(100);
Brian King072b91f2008-07-01 13:14:30 -0500662 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;
Brian King79111d02009-05-28 16:17:29 -0500666 vhost->logged_in = 0;
Brian King072b91f2008-07-01 13:14:30 -0500667 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{
Brian King73ee5d82010-06-17 13:55:13 -0500680 int rc = 0;
Brian King072b91f2008-07-01 13:14:30 -0500681 struct vio_dev *vdev = to_vio_dev(vhost->dev);
682
683 /* Re-enable the CRQ */
684 do {
Brian King73ee5d82010-06-17 13:55:13 -0500685 if (rc)
686 msleep(100);
Brian King072b91f2008-07-01 13:14:30 -0500687 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{
Brian King73ee5d82010-06-17 13:55:13 -0500705 int rc = 0;
706 unsigned long flags;
Brian King072b91f2008-07-01 13:14:30 -0500707 struct vio_dev *vdev = to_vio_dev(vhost->dev);
708 struct ibmvfc_crq_queue *crq = &vhost->crq;
709
710 /* Close the CRQ */
711 do {
Brian King73ee5d82010-06-17 13:55:13 -0500712 if (rc)
713 msleep(100);
Brian King072b91f2008-07-01 13:14:30 -0500714 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
715 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
716
Brian King73ee5d82010-06-17 13:55:13 -0500717 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -0500718 vhost->state = IBMVFC_NO_CRQ;
Brian King79111d02009-05-28 16:17:29 -0500719 vhost->logged_in = 0;
Brian King072b91f2008-07-01 13:14:30 -0500720 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);
Brian King73ee5d82010-06-17 13:55:13 -0500735 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -0500736
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
Brian Kingad8dcff2008-10-29 08:46:41 -0500790 if (evt->eh_comp)
791 complete(evt->eh_comp);
792
Brian King072b91f2008-07-01 13:14:30 -0500793 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
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500810 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
Brian King072b91f2008-07-01 13:14:30 -0500811
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/**
Brian King79111d02009-05-28 16:17:29 -0500836 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
Brian King072b91f2008-07-01 13:14:30 -0500837 * @vhost: struct ibmvfc host to reset
838 **/
Brian King79111d02009-05-28 16:17:29 -0500839static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
Brian King072b91f2008-07-01 13:14:30 -0500840{
Brian King072b91f2008-07-01 13:14:30 -0500841 ibmvfc_purge_requests(vhost, DID_ERROR);
Brian King73ee5d82010-06-17 13:55:13 -0500842 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
843 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
Brian King072b91f2008-07-01 13:14:30 -0500844}
845
846/**
Brian King79111d02009-05-28 16:17:29 -0500847 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
Brian King072b91f2008-07-01 13:14:30 -0500848 * @vhost: struct ibmvfc host to reset
849 **/
Brian King79111d02009-05-28 16:17:29 -0500850static 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 **/
Brian King072b91f2008-07-01 13:14:30 -0500866static 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 *
Brian King7d0e4622009-05-28 16:17:26 -0500879 * Returns: 1 if init will be retried / 0 if not
880 *
Brian King072b91f2008-07-01 13:14:30 -0500881 **/
Brian King7d0e4622009-05-28 16:17:26 -0500882static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
Brian King072b91f2008-07-01 13:14:30 -0500883{
Brian King7d0e4622009-05-28 16:17:26 -0500884 int retry = 0;
885
Brian King072b91f2008-07-01 13:14:30 -0500886 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
Brian King1c41fa822008-12-03 11:02:54 -0600887 vhost->delay_init = 1;
888 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -0500889 dev_err(vhost->dev,
890 "Host initialization retries exceeded. Taking adapter offline\n");
891 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
Brian King1c41fa822008-12-03 11:02:54 -0600892 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
Brian King072b91f2008-07-01 13:14:30 -0500893 __ibmvfc_reset_host(vhost);
Brian King7d0e4622009-05-28 16:17:26 -0500894 else {
Brian King072b91f2008-07-01 13:14:30 -0500895 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
Brian King7d0e4622009-05-28 16:17:26 -0500896 retry = 1;
897 }
Brian King072b91f2008-07-01 13:14:30 -0500898 }
899
900 wake_up(&vhost->work_wait_q);
Brian King7d0e4622009-05-28 16:17:26 -0500901 return retry;
Brian King072b91f2008-07-01 13:14:30 -0500902}
903
904/**
Brian Kingb3c10489c2008-07-22 08:31:41 -0500905 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
Brian King072b91f2008-07-01 13:14:30 -0500906 * @starget: scsi target struct
907 *
908 * Return value:
909 * ibmvfc_target struct / NULL if not found
910 **/
Brian Kingb3c10489c2008-07-22 08:31:41 -0500911static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
Brian King072b91f2008-07-01 13:14:30 -0500912{
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)
Brian Kingb3c10489c2008-07-22 08:31:41 -0500918 if (tgt->target_id == starget->id) {
919 kref_get(&tgt->kref);
Brian King072b91f2008-07-01 13:14:30 -0500920 return tgt;
Brian Kingb3c10489c2008-07-22 08:31:41 -0500921 }
Brian King072b91f2008-07-01 13:14:30 -0500922 return NULL;
923}
924
925/**
Brian Kingb3c10489c2008-07-22 08:31:41 -0500926 * ibmvfc_get_target - Find the specified scsi_target
Brian King072b91f2008-07-01 13:14:30 -0500927 * @starget: scsi target struct
928 *
929 * Return value:
930 * ibmvfc_target struct / NULL if not found
931 **/
Brian Kingb3c10489c2008-07-22 08:31:41 -0500932static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
Brian King072b91f2008-07-01 13:14:30 -0500933{
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);
Brian Kingb3c10489c2008-07-22 08:31:41 -0500939 tgt = __ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -0500940 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) {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500958 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
Brian King072b91f2008-07-01 13:14:30 -0500959 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:
Stephen Rothwell775a42e2009-01-06 14:59:00 +0000978 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -0500979 be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
Brian King072b91f2008-07-01 13:14:30 -0500980 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;
Brian King0ae808e2008-07-22 08:31:39 -05001016 case IBMVFC_NO_CRQ:
1017 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1018 break;
Brian King072b91f2008-07-01 13:14:30 -05001019 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
1043/**
Brian Kingb3c10489c2008-07-22 08:31:41 -05001044 * 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
1054/**
Brian King072b91f2008-07-01 13:14:30 -05001055 * 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{
Brian Kingb3c10489c2008-07-22 08:31:41 -05001063 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -05001064 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
Brian Kingb3c10489c2008-07-22 08:31:41 -05001065 if (tgt)
1066 kref_put(&tgt->kref, ibmvfc_release_tgt);
Brian King072b91f2008-07-01 13:14:30 -05001067}
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{
Brian Kingb3c10489c2008-07-22 08:31:41 -05001078 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -05001079 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
Brian Kingb3c10489c2008-07-22 08:31:41 -05001080 if (tgt)
1081 kref_put(&tgt->kref, ibmvfc_release_tgt);
Brian King072b91f2008-07-01 13:14:30 -05001082}
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{
Brian Kingb3c10489c2008-07-22 08:31:41 -05001093 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -05001094 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
Brian Kingb3c10489c2008-07-22 08:31:41 -05001095 if (tgt)
1096 kref_put(&tgt->kref, ibmvfc_release_tgt);
Brian King072b91f2008-07-01 13:14:30 -05001097}
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,
Brian King3eddc562008-08-15 10:59:21 -05001109 ((vhost->state == IBMVFC_ACTIVE ||
1110 vhost->state == IBMVFC_HOST_OFFLINE ||
1111 vhost->state == IBMVFC_LINK_DEAD) &&
1112 vhost->action == IBMVFC_HOST_ACTION_NONE),
Brian King072b91f2008-07-01 13:14:30 -05001113 (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;
Grant Likely61c7a082010-04-13 16:12:29 -07001169 struct device_node *of_node = vhost->dev->of_node;
Brian King072b91f2008-07-01 13:14:30 -05001170 const char *location;
1171
1172 memset(login_info, 0, sizeof(*login_info));
1173
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001174 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);
Brian King072b91f2008-07-01 13:14:30 -05001182 if (vhost->client_migrated)
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001183 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
Brian King072b91f2008-07-01 13:14:30 -05001184
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001185 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));
Brian King072b91f2008-07-01 13:14:30 -05001189 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1190 strncpy(login_info->device_name,
Kay Sievers71610f52008-12-03 22:41:36 +01001191 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
Brian King072b91f2008-07-01 13:14:30 -05001192
1193 location = of_get_property(of_node, "ibm,loc-code", NULL);
Kay Sievers71610f52008-12-03 22:41:36 +01001194 location = location ? location : dev_name(vhost->dev);
Brian King072b91f2008-07-01 13:14:30 -05001195 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;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001228 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
Brian King072b91f2008-07-01 13:14:30 -05001229 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;
Brian Kingad8dcff2008-10-29 08:46:41 -05001297 evt->eh_comp = NULL;
Brian King072b91f2008-07-01 13:14:30 -05001298}
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) {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001313 md[i].va = cpu_to_be64(sg_dma_address(sg));
1314 md[i].len = cpu_to_be32(sg_dma_len(sg));
Brian King072b91f2008-07-01 13:14:30 -05001315 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) {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001340 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
Brian King072b91f2008-07-01 13:14:30 -05001341 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) {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001349 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
Brian King072b91f2008-07-01 13:14:30 -05001350 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1351 } else {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001352 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
Brian King072b91f2008-07-01 13:14:30 -05001353 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
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001361 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
Brian King072b91f2008-07-01 13:14:30 -05001362
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) {
Brian King64b840d2009-01-22 15:45:38 -06001368 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");
Brian King072b91f2008-07-01 13:14:30 -05001371 return -ENOMEM;
1372 }
1373 }
1374
1375 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1376
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001377 data->va = cpu_to_be64(evt->ext_list_token);
1378 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
Brian King072b91f2008-07-01 13:14:30 -05001379 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{
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001407 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
Brian King072b91f2008-07-01 13:14:30 -05001408 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)
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001413 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
Brian King072b91f2008-07-01 13:14:30 -05001414 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001415 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
Brian King072b91f2008-07-01 13:14:30 -05001416 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
Brian Kinga528ab72008-12-03 11:02:56 -06001429 mb();
1430
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001431 if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1432 be64_to_cpu(crq_as_u64[1])))) {
Brian King072b91f2008-07-01 13:14:30 -05001433 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
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001455 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
Brian King072b91f2008-07-01 13:14:30 -05001456
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;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001476 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
Brian King072b91f2008-07-01 13:14:30 -05001477 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
Brian King0ae808e2008-07-22 08:31:39 -05001485 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
Brian King072b91f2008-07-01 13:14:30 -05001486 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
1497/**
Brian King6d29cc52009-05-28 16:17:33 -05001498 * 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
1518/**
Brian King072b91f2008-07-01 13:14:30 -05001519 * 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;
Brian King2bac4062008-08-15 10:59:26 -05001529 u32 rsp_len = 0;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001530 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
Brian King072b91f2008-07-01 13:14:30 -05001531
1532 if (cmnd) {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001533 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1534 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
Brian King072b91f2008-07-01 13:14:30 -05001535 else if (rsp->flags & FCP_RESID_UNDER)
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001536 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
Brian King072b91f2008-07-01 13:14:30 -05001537 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)
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001544 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
Brian King072b91f2008-07-01 13:14:30 -05001545 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1546 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
Brian King2bac4062008-08-15 10:59:26 -05001547 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
Brian King072b91f2008-07-01 13:14:30 -05001548 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001549 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1550 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
Brian King6d29cc52009-05-28 16:17:33 -05001551 ibmvfc_relogin(cmnd->device);
Brian King072b91f2008-07-01 13:14:30 -05001552
Brian King50119da2008-10-29 08:46:36 -05001553 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1554 cmnd->result = (DID_ERROR << 16);
1555
Brian King072b91f2008-07-01 13:14:30 -05001556 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
Brian Kingad8dcff2008-10-29 08:46:41 -05001567 if (evt->eh_comp)
1568 complete(evt->eh_comp);
1569
Brian King072b91f2008-07-01 13:14:30 -05001570 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 **/
Jeff Garzikf2812332010-11-16 02:10:29 -05001611static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
Brian King072b91f2008-07-01 13:14:30 -05001612 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;
Brian King072b91f2008-07-01 13:14:30 -05001618 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));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001634 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));
Brian King072b91f2008-07-01 13:14:30 -05001642 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1643 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1644
Christoph Hellwig50668632014-10-30 14:30:06 +01001645 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;
Brian King072b91f2008-07-01 13:14:30 -05001648 }
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
Jeff Garzikf2812332010-11-16 02:10:29 -05001666static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1667
Brian King072b91f2008-07-01 13:14:30 -05001668/**
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
1682/**
Brian Kingd31429e2009-10-19 15:07:54 -05001683 * 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));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001726 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);
Brian Kingd31429e2009-10-19 15:07:54 -05001732 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));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001783 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);
Brian Kingd31429e2009-10-19 15:07:54 -05001787 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));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001898 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));
Brian Kingd31429e2009-10-19 15:07:54 -05001901
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001902 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));
Brian Kingd31429e2009-10-19 15:07:54 -05001905
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001906 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);
Brian Kingd31429e2009-10-19 15:07:54 -05001910
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001911 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);
Brian Kingd31429e2009-10-19 15:07:54 -05001918
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
1952/**
Brian King072b91f2008-07-01 13:14:30 -05001953 * 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;
Brian King50ed9a02008-10-29 08:46:47 -05001966 struct ibmvfc_event *evt = NULL;
Brian King072b91f2008-07-01 13:14:30 -05001967 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));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001980 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);
Brian King072b91f2008-07-01 13:14:30 -05001987 int_to_scsilun(sdev->lun, &tmf->iu.lun);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05001988 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
Brian King072b91f2008-07-01 13:14:30 -05001989 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
Brian King230934a2009-10-19 15:07:47 -05002006 if (rsp_iu.cmd.status)
2007 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2008
2009 if (rsp_code) {
Brian King072b91f2008-07-01 13:14:30 -05002010 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) "
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002014 "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)),
Brian King072b91f2008-07-01 13:14:30 -05002016 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/**
Brian Kingd2fab5c2010-08-05 16:38:34 -05002029 * ibmvfc_match_rport - Match function for specified remote port
2030 * @evt: ibmvfc event struct
2031 * @device: device to match (rport)
Brian King072b91f2008-07-01 13:14:30 -05002032 *
2033 * Returns:
Brian Kingd2fab5c2010-08-05 16:38:34 -05002034 * 1 if event matches rport / 0 if event does not match rport
Brian King072b91f2008-07-01 13:14:30 -05002035 **/
Brian Kingd2fab5c2010-08-05 16:38:34 -05002036static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
Brian King072b91f2008-07-01 13:14:30 -05002037{
Brian Kingd2fab5c2010-08-05 16:38:34 -05002038 struct fc_rport *cmd_rport;
Brian King072b91f2008-07-01 13:14:30 -05002039
Brian Kingd2fab5c2010-08-05 16:38:34 -05002040 if (evt->cmnd) {
2041 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2042 if (cmd_rport == rport)
2043 return 1;
Brian King072b91f2008-07-01 13:14:30 -05002044 }
Brian King072b91f2008-07-01 13:14:30 -05002045 return 0;
2046}
2047
2048/**
Brian Kingad8dcff2008-10-29 08:46:41 -05002049 * 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}
2062
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}
2077
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;
Brian Kingdaa142d2010-04-20 14:21:35 -05002094 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
Brian Kingad8dcff2008-10-29 08:46:41 -05002095
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);
2107
2108 if (wait) {
2109 timeout = wait_for_completion_timeout(&comp, timeout);
2110
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);
2128
2129 LEAVE;
2130 return SUCCESS;
2131}
2132
2133/**
Brian Kingd2fab5c2010-08-05 16:38:34 -05002134 * 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);
2147 struct scsi_target *starget = scsi_target(sdev);
2148 struct fc_rport *rport = starget_to_rport(starget);
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
Brian King55d29bf2013-04-12 08:25:17 -05002173 if (vhost->logged_in) {
Brian Kingd2fab5c2010-08-05 16:38:34 -05002174 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));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002179 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);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002183 int_to_scsilun(sdev->lun, &tmf->lun);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002184 if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
Brian King90f725d2013-04-12 08:25:18 -05002185 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
Brian King55d29bf2013-04-12 08:25:17 -05002186 if (vhost->state == IBMVFC_ACTIVE)
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002187 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
Brian King55d29bf2013-04-12 08:25:17 -05002188 else
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002189 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);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002192
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);
Brian Kingc281e322013-08-20 11:08:42 -05002202 /* 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;
Brian Kingd2fab5c2010-08-05 16:38:34 -05002206 }
2207
2208 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2209
2210 wait_for_completion(&evt->comp);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002211 status = be16_to_cpu(rsp.mad_common.status);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002212 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);
Brian Kingc281e322013-08-20 11:08:42 -05002218 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 };
Brian Kingd2fab5c2010-08-05 16:38:34 -05002227 }
2228
2229 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2230 return 0;
2231}
2232
2233/**
2234 * ibmvfc_match_key - Match function for specified cancel key
2235 * @evt: ibmvfc event struct
2236 * @key: cancel key to match
2237 *
2238 * Returns:
2239 * 1 if event matches key / 0 if event does not match key
2240 **/
2241static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2242{
2243 unsigned long cancel_key = (unsigned long)key;
2244
2245 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002246 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
Brian Kingd2fab5c2010-08-05 16:38:34 -05002247 return 1;
2248 return 0;
2249}
2250
2251/**
Brian Kinga077c7f2012-08-24 16:50:59 -05002252 * 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
2266/**
Brian Kingd2fab5c2010-08-05 16:38:34 -05002267 * 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.
2272 *
2273 * Returns:
2274 * 0 on success / other on failure
2275 **/
2276static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2277{
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;
2287
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;
2294 }
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");
2300 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2301 return 0;
2302 }
2303
2304 if (vhost->state == IBMVFC_ACTIVE) {
2305 evt = ibmvfc_get_event(vhost);
2306 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2307
2308 tmf = &evt->iu.cmd;
2309 memset(tmf, 0, sizeof(*tmf));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002310 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);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002317 int_to_scsilun(sdev->lun, &tmf->iu.lun);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002318 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
Brian Kingd2fab5c2010-08-05 16:38:34 -05002319 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) {
Brian Kingf8804b72013-04-12 08:25:15 -05002337 rc = ibmvfc_cancel_all(sdev, 0);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002338 if (!rc) {
2339 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2340 if (rc == SUCCESS)
2341 rc = 0;
2342 }
2343
2344 if (rc) {
2345 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2346 ibmvfc_reset_host(vhost);
Brian Kinga077c7f2012-08-24 16:50:59 -05002347 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
Brian Kingd2fab5c2010-08-05 16:38:34 -05002361 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",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002374 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
Brian Kingd2fab5c2010-08-05 16:38:34 -05002375 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;
2386}
2387
2388/**
Brian King072b91f2008-07-01 13:14:30 -05002389 * ibmvfc_eh_abort_handler - Abort a command
2390 * @cmd: scsi command to abort
2391 *
2392 * Returns:
Brian King93631b42013-04-12 08:25:16 -05002393 * SUCCESS / FAST_IO_FAIL / FAILED
Brian King072b91f2008-07-01 13:14:30 -05002394 **/
2395static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2396{
Brian Kingad8dcff2008-10-29 08:46:41 -05002397 struct scsi_device *sdev = cmd->device;
2398 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King55d29bf2013-04-12 08:25:17 -05002399 int cancel_rc, block_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002400 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002401
2402 ENTER;
Brian King93631b42013-04-12 08:25:16 -05002403 block_rc = fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002404 ibmvfc_wait_while_resetting(vhost);
Brian King93631b42013-04-12 08:25:16 -05002405 if (block_rc != FAST_IO_FAIL) {
2406 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
Brian King55d29bf2013-04-12 08:25:17 -05002407 ibmvfc_abort_task_set(sdev);
Brian King93631b42013-04-12 08:25:16 -05002408 } else
Brian King90f725d2013-04-12 08:25:18 -05002409 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
Brian King072b91f2008-07-01 13:14:30 -05002410
Brian King55d29bf2013-04-12 08:25:17 -05002411 if (!cancel_rc)
Brian Kingad8dcff2008-10-29 08:46:41 -05002412 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05002413
Brian King93631b42013-04-12 08:25:16 -05002414 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2415 rc = FAST_IO_FAIL;
2416
Brian King072b91f2008-07-01 13:14:30 -05002417 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002418 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002419}
2420
2421/**
2422 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2423 * @cmd: scsi command struct
2424 *
2425 * Returns:
Brian King93631b42013-04-12 08:25:16 -05002426 * SUCCESS / FAST_IO_FAIL / FAILED
Brian King072b91f2008-07-01 13:14:30 -05002427 **/
2428static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2429{
Brian Kingad8dcff2008-10-29 08:46:41 -05002430 struct scsi_device *sdev = cmd->device;
2431 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King93631b42013-04-12 08:25:16 -05002432 int cancel_rc, block_rc, reset_rc = 0;
Brian Kingad8dcff2008-10-29 08:46:41 -05002433 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002434
2435 ENTER;
Brian King93631b42013-04-12 08:25:16 -05002436 block_rc = fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002437 ibmvfc_wait_while_resetting(vhost);
Brian King93631b42013-04-12 08:25:16 -05002438 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
Brian King90f725d2013-04-12 08:25:18 -05002442 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
Brian King072b91f2008-07-01 13:14:30 -05002443
Brian Kingad8dcff2008-10-29 08:46:41 -05002444 if (!cancel_rc && !reset_rc)
2445 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05002446
Brian King93631b42013-04-12 08:25:16 -05002447 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2448 rc = FAST_IO_FAIL;
2449
Brian King072b91f2008-07-01 13:14:30 -05002450 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002451 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002452}
2453
2454/**
Brian King93631b42013-04-12 08:25:16 -05002455 * 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;
Brian King90f725d2013-04-12 08:25:18 -05002463 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
Brian King93631b42013-04-12 08:25:16 -05002464}
2465
2466/**
Brian King4a5c4a52009-10-19 15:07:53 -05002467 * 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)
Brian King072b91f2008-07-01 13:14:30 -05002473{
2474 unsigned long *rc = data;
2475 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2476}
2477
2478/**
Brian King072b91f2008-07-01 13:14:30 -05002479 * ibmvfc_eh_target_reset_handler - Reset the target
2480 * @cmd: scsi command struct
2481 *
2482 * Returns:
Brian King93631b42013-04-12 08:25:16 -05002483 * SUCCESS / FAST_IO_FAIL / FAILED
Brian King072b91f2008-07-01 13:14:30 -05002484 **/
2485static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2486{
Brian Kingad8dcff2008-10-29 08:46:41 -05002487 struct scsi_device *sdev = cmd->device;
2488 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2489 struct scsi_target *starget = scsi_target(sdev);
Brian King93631b42013-04-12 08:25:16 -05002490 int block_rc;
2491 int reset_rc = 0;
Brian Kingad8dcff2008-10-29 08:46:41 -05002492 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002493 unsigned long cancel_rc = 0;
Brian King072b91f2008-07-01 13:14:30 -05002494
2495 ENTER;
Brian King93631b42013-04-12 08:25:16 -05002496 block_rc = fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002497 ibmvfc_wait_while_resetting(vhost);
Brian King93631b42013-04-12 08:25:16 -05002498 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);
Brian King072b91f2008-07-01 13:14:30 -05002503
Brian Kingad8dcff2008-10-29 08:46:41 -05002504 if (!cancel_rc && !reset_rc)
2505 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
Brian King072b91f2008-07-01 13:14:30 -05002506
Brian King93631b42013-04-12 08:25:16 -05002507 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2508 rc = FAST_IO_FAIL;
2509
Brian King072b91f2008-07-01 13:14:30 -05002510 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002511 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002512}
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{
Brian King93631b42013-04-12 08:25:16 -05002521 int rc, block_rc;
Brian King072b91f2008-07-01 13:14:30 -05002522 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2523
Brian King93631b42013-04-12 08:25:16 -05002524 block_rc = fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002525 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2526 rc = ibmvfc_issue_fc_host_lip(vhost->host);
Brian King93631b42013-04-12 08:25:16 -05002527
2528 if (block_rc == FAST_IO_FAIL)
2529 return FAST_IO_FAIL;
2530
Brian King072b91f2008-07-01 13:14:30 -05002531 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{
Brian Kingd2fab5c2010-08-05 16:38:34 -05002543 struct Scsi_Host *shost = rport_to_shost(rport);
Brian King072b91f2008-07-01 13:14:30 -05002544 struct ibmvfc_host *vhost = shost_priv(shost);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002545 struct fc_rport *dev_rport;
2546 struct scsi_device *sdev;
2547 unsigned long rc;
Brian King072b91f2008-07-01 13:14:30 -05002548
2549 ENTER;
Brian Kingd2fab5c2010-08-05 16:38:34 -05002550 shost_for_each_device(sdev, shost) {
2551 dev_rport = starget_to_rport(scsi_target(sdev));
2552 if (dev_rport != rport)
2553 continue;
Brian King90f725d2013-04-12 08:25:18 -05002554 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002555 }
Brian King072b91f2008-07-01 13:14:30 -05002556
Brian Kingd2fab5c2010-08-05 16:38:34 -05002557 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
Brian Kingad8dcff2008-10-29 08:46:41 -05002558
2559 if (rc == FAILED)
Brian King072b91f2008-07-01 13:14:30 -05002560 ibmvfc_issue_fc_host_lip(shost);
Brian King072b91f2008-07-01 13:14:30 -05002561 LEAVE;
2562}
2563
Brian Kingd99e5f42010-09-09 16:20:42 -05002564static const struct ibmvfc_async_desc ae_desc [] = {
Robert Jennings402c6ee2010-12-09 14:03:59 -06002565 { "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 },
Brian King072b91f2008-07-01 13:14:30 -05002578};
2579
Brian Kingd99e5f42010-09-09 16:20:42 -05002580static const struct ibmvfc_async_desc unknown_ae = {
Robert Jennings402c6ee2010-12-09 14:03:59 -06002581 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
Brian Kingd99e5f42010-09-09 16:20:42 -05002582};
Brian King072b91f2008-07-01 13:14:30 -05002583
2584/**
2585 * ibmvfc_get_ae_desc - Get text description for async event
2586 * @ae: async event
2587 *
2588 **/
Brian Kingd99e5f42010-09-09 16:20:42 -05002589static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
Brian King072b91f2008-07-01 13:14:30 -05002590{
2591 int i;
2592
2593 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2594 if (ae_desc[i].ae == ae)
Brian Kingd99e5f42010-09-09 16:20:42 -05002595 return &ae_desc[i];
Brian King072b91f2008-07-01 13:14:30 -05002596
Brian Kingd99e5f42010-09-09 16:20:42 -05002597 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;
2622
2623 return "";
Brian King072b91f2008-07-01 13:14:30 -05002624}
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{
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002635 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
Brian King6d29cc52009-05-28 16:17:33 -05002636 struct ibmvfc_target *tgt;
Brian King072b91f2008-07-01 13:14:30 -05002637
Brian Kingd99e5f42010-09-09 16:20:42 -05002638 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
Tyrel Datwyler21367e22016-02-11 16:24:35 -06002639 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
2640 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
Brian Kingd99e5f42010-09-09 16:20:42 -05002641 ibmvfc_get_link_state(crq->link_state));
Brian King072b91f2008-07-01 13:14:30 -05002642
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002643 switch (be64_to_cpu(crq->event)) {
Brian King072b91f2008-07-01 13:14:30 -05002644 case IBMVFC_AE_RESUME:
Brian King497f9c52009-05-28 16:17:30 -05002645 switch (crq->link_state) {
2646 case IBMVFC_AE_LS_LINK_DOWN:
2647 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2648 break;
2649 case IBMVFC_AE_LS_LINK_DEAD:
2650 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2651 break;
2652 case IBMVFC_AE_LS_LINK_UP:
2653 case IBMVFC_AE_LS_LINK_BOUNCED:
2654 default:
2655 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2656 vhost->delay_init = 1;
2657 __ibmvfc_reset_host(vhost);
2658 break;
2659 };
2660
2661 break;
2662 case IBMVFC_AE_LINK_UP:
Brian King072b91f2008-07-01 13:14:30 -05002663 vhost->events_to_log |= IBMVFC_AE_LINKUP;
Brian Kingd2131b32008-12-18 09:26:51 -06002664 vhost->delay_init = 1;
2665 __ibmvfc_reset_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002666 break;
2667 case IBMVFC_AE_SCN_FABRIC:
Brian Kingd2131b32008-12-18 09:26:51 -06002668 case IBMVFC_AE_SCN_DOMAIN:
Brian King072b91f2008-07-01 13:14:30 -05002669 vhost->events_to_log |= IBMVFC_AE_RSCN;
Brian King5cdf1622012-08-24 16:51:01 -05002670 if (vhost->state < IBMVFC_HALTED) {
2671 vhost->delay_init = 1;
2672 __ibmvfc_reset_host(vhost);
2673 }
Brian King072b91f2008-07-01 13:14:30 -05002674 break;
2675 case IBMVFC_AE_SCN_NPORT:
2676 case IBMVFC_AE_SCN_GROUP:
Brian King072b91f2008-07-01 13:14:30 -05002677 vhost->events_to_log |= IBMVFC_AE_RSCN;
Brian King6d29cc52009-05-28 16:17:33 -05002678 ibmvfc_reinit_host(vhost);
2679 break;
Brian King072b91f2008-07-01 13:14:30 -05002680 case IBMVFC_AE_ELS_LOGO:
2681 case IBMVFC_AE_ELS_PRLO:
2682 case IBMVFC_AE_ELS_PLOGI:
Brian King6d29cc52009-05-28 16:17:33 -05002683 list_for_each_entry(tgt, &vhost->targets, queue) {
2684 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2685 break;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002686 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
Brian King6d29cc52009-05-28 16:17:33 -05002687 continue;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002688 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
Brian King6d29cc52009-05-28 16:17:33 -05002689 continue;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002690 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
Brian King6d29cc52009-05-28 16:17:33 -05002691 continue;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002692 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
Brian King017b2ae2009-06-18 09:06:55 -05002693 tgt->logo_rcvd = 1;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002694 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
Brian King017b2ae2009-06-18 09:06:55 -05002695 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2696 ibmvfc_reinit_host(vhost);
2697 }
Brian King6d29cc52009-05-28 16:17:33 -05002698 }
Brian King072b91f2008-07-01 13:14:30 -05002699 break;
2700 case IBMVFC_AE_LINK_DOWN:
2701 case IBMVFC_AE_ADAPTER_FAILED:
2702 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2703 break;
2704 case IBMVFC_AE_LINK_DEAD:
2705 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2706 break;
2707 case IBMVFC_AE_HALT:
2708 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2709 break;
2710 default:
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002711 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
Brian King072b91f2008-07-01 13:14:30 -05002712 break;
2713 };
2714}
2715
2716/**
2717 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2718 * @crq: Command/Response queue
2719 * @vhost: ibmvfc host struct
2720 *
2721 **/
2722static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2723{
2724 long rc;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05002725 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
Brian King072b91f2008-07-01 13:14:30 -05002726
2727 switch (crq->valid) {
2728 case IBMVFC_CRQ_INIT_RSP:
2729 switch (crq->format) {
2730 case IBMVFC_CRQ_INIT:
2731 dev_info(vhost->dev, "Partner initialized\n");
2732 /* Send back a response */
2733 rc = ibmvfc_send_crq_init_complete(vhost);
2734 if (rc == 0)
Brian King861890c2009-10-19 15:07:49 -05002735 ibmvfc_init_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002736 else
2737 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2738 break;
2739 case IBMVFC_CRQ_INIT_COMPLETE:
2740 dev_info(vhost->dev, "Partner initialization complete\n");
Brian King861890c2009-10-19 15:07:49 -05002741 ibmvfc_init_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002742 break;
2743 default:
2744 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2745 }
2746 return;
2747 case IBMVFC_CRQ_XPORT_EVENT:
2748 vhost->state = IBMVFC_NO_CRQ;
Brian King79111d02009-05-28 16:17:29 -05002749 vhost->logged_in = 0;
Brian King072b91f2008-07-01 13:14:30 -05002750 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2751 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2752 /* We need to re-setup the interpartition connection */
2753 dev_info(vhost->dev, "Re-enabling adapter\n");
2754 vhost->client_migrated = 1;
2755 ibmvfc_purge_requests(vhost, DID_REQUEUE);
Brian King73ee5d82010-06-17 13:55:13 -05002756 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2757 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
Brian King072b91f2008-07-01 13:14:30 -05002758 } else {
2759 dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
Brian King072b91f2008-07-01 13:14:30 -05002760 ibmvfc_purge_requests(vhost, DID_ERROR);
Brian King73ee5d82010-06-17 13:55:13 -05002761 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2762 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
Brian King072b91f2008-07-01 13:14:30 -05002763 }
2764 return;
2765 case IBMVFC_CRQ_CMD_RSP:
2766 break;
2767 default:
2768 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2769 return;
2770 }
2771
2772 if (crq->format == IBMVFC_ASYNC_EVENT)
2773 return;
2774
2775 /* The only kind of payload CRQs we should get are responses to
2776 * things we send. Make sure this response is to something we
2777 * actually sent
2778 */
2779 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002780 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
Brian King072b91f2008-07-01 13:14:30 -05002781 crq->ioba);
2782 return;
2783 }
2784
2785 if (unlikely(atomic_read(&evt->free))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002786 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
Brian King072b91f2008-07-01 13:14:30 -05002787 crq->ioba);
2788 return;
2789 }
2790
2791 del_timer(&evt->timer);
2792 list_del(&evt->queue);
2793 ibmvfc_trc_end(evt);
2794 evt->done(evt);
2795}
2796
2797/**
2798 * ibmvfc_scan_finished - Check if the device scan is done.
2799 * @shost: scsi host struct
2800 * @time: current elapsed time
2801 *
2802 * Returns:
2803 * 0 if scan is not done / 1 if scan is done
2804 **/
2805static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2806{
2807 unsigned long flags;
2808 struct ibmvfc_host *vhost = shost_priv(shost);
2809 int done = 0;
2810
2811 spin_lock_irqsave(shost->host_lock, flags);
2812 if (time >= (init_timeout * HZ)) {
2813 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2814 "continuing initialization\n", init_timeout);
2815 done = 1;
2816 }
2817
Brian King43c8da92009-05-28 16:17:28 -05002818 if (vhost->scan_complete)
Brian King072b91f2008-07-01 13:14:30 -05002819 done = 1;
2820 spin_unlock_irqrestore(shost->host_lock, flags);
2821 return done;
2822}
2823
2824/**
2825 * ibmvfc_slave_alloc - Setup the device's task set value
2826 * @sdev: struct scsi_device device to configure
2827 *
2828 * Set the device's task set value so that error handling works as
2829 * expected.
2830 *
2831 * Returns:
2832 * 0 on success / -ENXIO if device does not exist
2833 **/
2834static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2835{
2836 struct Scsi_Host *shost = sdev->host;
2837 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2838 struct ibmvfc_host *vhost = shost_priv(shost);
2839 unsigned long flags = 0;
2840
2841 if (!rport || fc_remote_port_chkready(rport))
2842 return -ENXIO;
2843
2844 spin_lock_irqsave(shost->host_lock, flags);
2845 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2846 spin_unlock_irqrestore(shost->host_lock, flags);
2847 return 0;
2848}
2849
2850/**
Brian Kingad8dcff2008-10-29 08:46:41 -05002851 * ibmvfc_target_alloc - Setup the target's task set value
2852 * @starget: struct scsi_target
2853 *
2854 * Set the target's task set value so that error handling works as
2855 * expected.
2856 *
2857 * Returns:
2858 * 0 on success / -ENXIO if device does not exist
2859 **/
2860static int ibmvfc_target_alloc(struct scsi_target *starget)
2861{
2862 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2863 struct ibmvfc_host *vhost = shost_priv(shost);
2864 unsigned long flags = 0;
2865
2866 spin_lock_irqsave(shost->host_lock, flags);
2867 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2868 spin_unlock_irqrestore(shost->host_lock, flags);
2869 return 0;
2870}
2871
2872/**
Brian King072b91f2008-07-01 13:14:30 -05002873 * ibmvfc_slave_configure - Configure the device
2874 * @sdev: struct scsi_device device to configure
2875 *
2876 * Enable allow_restart for a device if it is a disk. Adjust the
2877 * queue_depth here also.
2878 *
2879 * Returns:
2880 * 0
2881 **/
2882static int ibmvfc_slave_configure(struct scsi_device *sdev)
2883{
2884 struct Scsi_Host *shost = sdev->host;
Brian King072b91f2008-07-01 13:14:30 -05002885 unsigned long flags = 0;
2886
2887 spin_lock_irqsave(shost->host_lock, flags);
2888 if (sdev->type == TYPE_DISK)
2889 sdev->allow_restart = 1;
Brian King072b91f2008-07-01 13:14:30 -05002890 spin_unlock_irqrestore(shost->host_lock, flags);
2891 return 0;
2892}
2893
2894/**
2895 * ibmvfc_change_queue_depth - Change the device's queue depth
2896 * @sdev: scsi device struct
2897 * @qdepth: depth to set
Mike Christiee881a172009-10-15 17:46:39 -07002898 * @reason: calling context
Brian King072b91f2008-07-01 13:14:30 -05002899 *
2900 * Return value:
2901 * actual depth set
2902 **/
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002903static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
Brian King072b91f2008-07-01 13:14:30 -05002904{
2905 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2906 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2907
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002908 return scsi_change_queue_depth(sdev, qdepth);
Brian King072b91f2008-07-01 13:14:30 -05002909}
2910
Brian King072b91f2008-07-01 13:14:30 -05002911static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2912 struct device_attribute *attr, char *buf)
2913{
2914 struct Scsi_Host *shost = class_to_shost(dev);
2915 struct ibmvfc_host *vhost = shost_priv(shost);
2916
2917 return snprintf(buf, PAGE_SIZE, "%s\n",
2918 vhost->login_buf->resp.partition_name);
2919}
2920
Brian King072b91f2008-07-01 13:14:30 -05002921static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2922 struct device_attribute *attr, char *buf)
2923{
2924 struct Scsi_Host *shost = class_to_shost(dev);
2925 struct ibmvfc_host *vhost = shost_priv(shost);
2926
2927 return snprintf(buf, PAGE_SIZE, "%s\n",
2928 vhost->login_buf->resp.device_name);
2929}
2930
Brian King072b91f2008-07-01 13:14:30 -05002931static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2932 struct device_attribute *attr, char *buf)
2933{
2934 struct Scsi_Host *shost = class_to_shost(dev);
2935 struct ibmvfc_host *vhost = shost_priv(shost);
2936
2937 return snprintf(buf, PAGE_SIZE, "%s\n",
2938 vhost->login_buf->resp.port_loc_code);
2939}
2940
Brian King072b91f2008-07-01 13:14:30 -05002941static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2942 struct device_attribute *attr, char *buf)
2943{
2944 struct Scsi_Host *shost = class_to_shost(dev);
2945 struct ibmvfc_host *vhost = shost_priv(shost);
2946
2947 return snprintf(buf, PAGE_SIZE, "%s\n",
2948 vhost->login_buf->resp.drc_name);
2949}
2950
Brian King072b91f2008-07-01 13:14:30 -05002951static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2952 struct device_attribute *attr, char *buf)
2953{
2954 struct Scsi_Host *shost = class_to_shost(dev);
2955 struct ibmvfc_host *vhost = shost_priv(shost);
2956 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2957}
2958
Brian King497f9c52009-05-28 16:17:30 -05002959static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
2960 struct device_attribute *attr, char *buf)
2961{
2962 struct Scsi_Host *shost = class_to_shost(dev);
2963 struct ibmvfc_host *vhost = shost_priv(shost);
2964 return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
2965}
2966
Brian King072b91f2008-07-01 13:14:30 -05002967/**
2968 * ibmvfc_show_log_level - Show the adapter's error logging level
2969 * @dev: class device struct
2970 * @buf: buffer
2971 *
2972 * Return value:
2973 * number of bytes printed to buffer
2974 **/
2975static ssize_t ibmvfc_show_log_level(struct device *dev,
2976 struct device_attribute *attr, char *buf)
2977{
2978 struct Scsi_Host *shost = class_to_shost(dev);
2979 struct ibmvfc_host *vhost = shost_priv(shost);
2980 unsigned long flags = 0;
2981 int len;
2982
2983 spin_lock_irqsave(shost->host_lock, flags);
2984 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2985 spin_unlock_irqrestore(shost->host_lock, flags);
2986 return len;
2987}
2988
2989/**
2990 * ibmvfc_store_log_level - Change the adapter's error logging level
2991 * @dev: class device struct
2992 * @buf: buffer
2993 *
2994 * Return value:
2995 * number of bytes printed to buffer
2996 **/
2997static ssize_t ibmvfc_store_log_level(struct device *dev,
2998 struct device_attribute *attr,
2999 const char *buf, size_t count)
3000{
3001 struct Scsi_Host *shost = class_to_shost(dev);
3002 struct ibmvfc_host *vhost = shost_priv(shost);
3003 unsigned long flags = 0;
3004
3005 spin_lock_irqsave(shost->host_lock, flags);
3006 vhost->log_level = simple_strtoul(buf, NULL, 10);
3007 spin_unlock_irqrestore(shost->host_lock, flags);
3008 return strlen(buf);
3009}
3010
Brian King85e23992009-05-28 16:17:25 -05003011static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3012static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3013static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3014static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3015static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
Brian King497f9c52009-05-28 16:17:30 -05003016static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
Brian King85e23992009-05-28 16:17:25 -05003017static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3018 ibmvfc_show_log_level, ibmvfc_store_log_level);
Brian King072b91f2008-07-01 13:14:30 -05003019
3020#ifdef CONFIG_SCSI_IBMVFC_TRACE
3021/**
3022 * ibmvfc_read_trace - Dump the adapter trace
Chris Wright2c3c8be2010-05-12 18:28:57 -07003023 * @filp: open sysfs file
Brian King072b91f2008-07-01 13:14:30 -05003024 * @kobj: kobject struct
3025 * @bin_attr: bin_attribute struct
3026 * @buf: buffer
3027 * @off: offset
3028 * @count: buffer size
3029 *
3030 * Return value:
3031 * number of bytes printed to buffer
3032 **/
Chris Wright2c3c8be2010-05-12 18:28:57 -07003033static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
Brian King072b91f2008-07-01 13:14:30 -05003034 struct bin_attribute *bin_attr,
3035 char *buf, loff_t off, size_t count)
3036{
3037 struct device *dev = container_of(kobj, struct device, kobj);
3038 struct Scsi_Host *shost = class_to_shost(dev);
3039 struct ibmvfc_host *vhost = shost_priv(shost);
3040 unsigned long flags = 0;
3041 int size = IBMVFC_TRACE_SIZE;
3042 char *src = (char *)vhost->trace;
3043
3044 if (off > size)
3045 return 0;
3046 if (off + count > size) {
3047 size -= off;
3048 count = size;
3049 }
3050
3051 spin_lock_irqsave(shost->host_lock, flags);
3052 memcpy(buf, &src[off], count);
3053 spin_unlock_irqrestore(shost->host_lock, flags);
3054 return count;
3055}
3056
3057static struct bin_attribute ibmvfc_trace_attr = {
3058 .attr = {
3059 .name = "trace",
3060 .mode = S_IRUGO,
3061 },
3062 .size = 0,
3063 .read = ibmvfc_read_trace,
3064};
3065#endif
3066
3067static struct device_attribute *ibmvfc_attrs[] = {
Brian King85e23992009-05-28 16:17:25 -05003068 &dev_attr_partition_name,
3069 &dev_attr_device_name,
3070 &dev_attr_port_loc_code,
3071 &dev_attr_drc_name,
3072 &dev_attr_npiv_version,
Brian King497f9c52009-05-28 16:17:30 -05003073 &dev_attr_capabilities,
Brian King85e23992009-05-28 16:17:25 -05003074 &dev_attr_log_level,
Brian King072b91f2008-07-01 13:14:30 -05003075 NULL
3076};
3077
3078static struct scsi_host_template driver_template = {
3079 .module = THIS_MODULE,
3080 .name = "IBM POWER Virtual FC Adapter",
3081 .proc_name = IBMVFC_NAME,
3082 .queuecommand = ibmvfc_queuecommand,
3083 .eh_abort_handler = ibmvfc_eh_abort_handler,
3084 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3085 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3086 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3087 .slave_alloc = ibmvfc_slave_alloc,
3088 .slave_configure = ibmvfc_slave_configure,
Brian Kingad8dcff2008-10-29 08:46:41 -05003089 .target_alloc = ibmvfc_target_alloc,
Brian King072b91f2008-07-01 13:14:30 -05003090 .scan_finished = ibmvfc_scan_finished,
3091 .change_queue_depth = ibmvfc_change_queue_depth,
Brian King072b91f2008-07-01 13:14:30 -05003092 .cmd_per_lun = 16,
3093 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3094 .this_id = -1,
3095 .sg_tablesize = SG_ALL,
3096 .max_sectors = IBMVFC_MAX_SECTORS,
3097 .use_clustering = ENABLE_CLUSTERING,
3098 .shost_attrs = ibmvfc_attrs,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01003099 .track_queue_depth = 1,
Brian King072b91f2008-07-01 13:14:30 -05003100};
3101
3102/**
3103 * ibmvfc_next_async_crq - Returns the next entry in async queue
3104 * @vhost: ibmvfc host struct
3105 *
3106 * Returns:
3107 * Pointer to next entry in queue / NULL if empty
3108 **/
3109static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3110{
3111 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3112 struct ibmvfc_async_crq *crq;
3113
3114 crq = &async_crq->msgs[async_crq->cur];
3115 if (crq->valid & 0x80) {
3116 if (++async_crq->cur == async_crq->size)
3117 async_crq->cur = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003118 rmb();
Brian King072b91f2008-07-01 13:14:30 -05003119 } else
3120 crq = NULL;
3121
3122 return crq;
3123}
3124
3125/**
3126 * ibmvfc_next_crq - Returns the next entry in message queue
3127 * @vhost: ibmvfc host struct
3128 *
3129 * Returns:
3130 * Pointer to next entry in queue / NULL if empty
3131 **/
3132static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3133{
3134 struct ibmvfc_crq_queue *queue = &vhost->crq;
3135 struct ibmvfc_crq *crq;
3136
3137 crq = &queue->msgs[queue->cur];
3138 if (crq->valid & 0x80) {
3139 if (++queue->cur == queue->size)
3140 queue->cur = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003141 rmb();
Brian King072b91f2008-07-01 13:14:30 -05003142 } else
3143 crq = NULL;
3144
3145 return crq;
3146}
3147
3148/**
3149 * ibmvfc_interrupt - Interrupt handler
3150 * @irq: number of irq to handle, not used
3151 * @dev_instance: ibmvfc_host that received interrupt
3152 *
3153 * Returns:
3154 * IRQ_HANDLED
3155 **/
3156static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3157{
3158 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
Brian King039a0892009-03-20 15:44:35 -05003159 unsigned long flags;
3160
3161 spin_lock_irqsave(vhost->host->host_lock, flags);
3162 vio_disable_interrupts(to_vio_dev(vhost->dev));
3163 tasklet_schedule(&vhost->tasklet);
3164 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3165 return IRQ_HANDLED;
3166}
3167
3168/**
3169 * ibmvfc_tasklet - Interrupt handler tasklet
3170 * @data: ibmvfc host struct
3171 *
3172 * Returns:
3173 * Nothing
3174 **/
3175static void ibmvfc_tasklet(void *data)
3176{
3177 struct ibmvfc_host *vhost = data;
Brian King072b91f2008-07-01 13:14:30 -05003178 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3179 struct ibmvfc_crq *crq;
3180 struct ibmvfc_async_crq *async;
3181 unsigned long flags;
3182 int done = 0;
3183
3184 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05003185 while (!done) {
Brian King072b91f2008-07-01 13:14:30 -05003186 /* Pull all the valid messages off the async CRQ */
3187 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3188 ibmvfc_handle_async(async, vhost);
3189 async->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003190 wmb();
Brian King072b91f2008-07-01 13:14:30 -05003191 }
3192
Brian Kingf1d7fb72009-06-18 09:06:52 -05003193 /* Pull all the valid messages off the CRQ */
3194 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
Brian King072b91f2008-07-01 13:14:30 -05003195 ibmvfc_handle_crq(crq, vhost);
3196 crq->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003197 wmb();
Brian Kingf1d7fb72009-06-18 09:06:52 -05003198 }
3199
3200 vio_enable_interrupts(vdev);
3201 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
Brian King072b91f2008-07-01 13:14:30 -05003202 vio_disable_interrupts(vdev);
3203 ibmvfc_handle_async(async, vhost);
Brian King4081b772008-11-14 13:33:50 -06003204 async->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003205 wmb();
Brian Kingf1d7fb72009-06-18 09:06:52 -05003206 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3207 vio_disable_interrupts(vdev);
3208 ibmvfc_handle_crq(crq, vhost);
3209 crq->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003210 wmb();
Brian King072b91f2008-07-01 13:14:30 -05003211 } else
3212 done = 1;
3213 }
3214
3215 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05003216}
3217
3218/**
3219 * ibmvfc_init_tgt - Set the next init job step for the target
3220 * @tgt: ibmvfc target struct
3221 * @job_step: job step to perform
3222 *
3223 **/
3224static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3225 void (*job_step) (struct ibmvfc_target *))
3226{
3227 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
3228 tgt->job_step = job_step;
3229 wake_up(&tgt->vhost->work_wait_q);
3230}
3231
3232/**
3233 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3234 * @tgt: ibmvfc target struct
3235 * @job_step: initialization job step
3236 *
Brian King7d0e4622009-05-28 16:17:26 -05003237 * Returns: 1 if step will be retried / 0 if not
3238 *
Brian King072b91f2008-07-01 13:14:30 -05003239 **/
Brian King7d0e4622009-05-28 16:17:26 -05003240static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
Brian King072b91f2008-07-01 13:14:30 -05003241 void (*job_step) (struct ibmvfc_target *))
3242{
Brian King1c41fa822008-12-03 11:02:54 -06003243 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -05003244 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3245 wake_up(&tgt->vhost->work_wait_q);
Brian King7d0e4622009-05-28 16:17:26 -05003246 return 0;
Brian King072b91f2008-07-01 13:14:30 -05003247 } else
3248 ibmvfc_init_tgt(tgt, job_step);
Brian King7d0e4622009-05-28 16:17:26 -05003249 return 1;
Brian King072b91f2008-07-01 13:14:30 -05003250}
3251
Brian Kinga3b7aea2009-02-02 18:39:40 -06003252/* Defined in FC-LS */
3253static const struct {
3254 int code;
3255 int retry;
3256 int logged_in;
3257} prli_rsp [] = {
3258 { 0, 1, 0 },
3259 { 1, 0, 1 },
3260 { 2, 1, 0 },
3261 { 3, 1, 0 },
3262 { 4, 0, 0 },
3263 { 5, 0, 0 },
3264 { 6, 0, 1 },
3265 { 7, 0, 0 },
3266 { 8, 1, 0 },
3267};
3268
3269/**
3270 * ibmvfc_get_prli_rsp - Find PRLI response index
3271 * @flags: PRLI response flags
3272 *
3273 **/
3274static int ibmvfc_get_prli_rsp(u16 flags)
3275{
3276 int i;
3277 int code = (flags & 0x0f00) >> 8;
3278
3279 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3280 if (prli_rsp[i].code == code)
3281 return i;
3282
3283 return 0;
3284}
3285
Brian King072b91f2008-07-01 13:14:30 -05003286/**
Brian King072b91f2008-07-01 13:14:30 -05003287 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3288 * @evt: ibmvfc event struct
3289 *
3290 **/
3291static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3292{
3293 struct ibmvfc_target *tgt = evt->tgt;
3294 struct ibmvfc_host *vhost = evt->vhost;
3295 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
Brian Kinga3b7aea2009-02-02 18:39:40 -06003296 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003297 u32 status = be16_to_cpu(rsp->common.status);
Brian King7d0e4622009-05-28 16:17:26 -05003298 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003299
3300 vhost->discovery_threads--;
3301 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3302 switch (status) {
3303 case IBMVFC_MAD_SUCCESS:
Brian Kinga3b7aea2009-02-02 18:39:40 -06003304 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3305 parms->type, parms->flags, parms->service_parms);
3306
3307 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003308 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
Brian Kinga3b7aea2009-02-02 18:39:40 -06003309 if (prli_rsp[index].logged_in) {
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003310 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
Brian Kinga3b7aea2009-02-02 18:39:40 -06003311 tgt->need_login = 0;
3312 tgt->ids.roles = 0;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003313 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
Brian Kinga3b7aea2009-02-02 18:39:40 -06003314 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003315 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
Brian Kinga3b7aea2009-02-02 18:39:40 -06003316 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
Brian King43c8da92009-05-28 16:17:28 -05003317 tgt->add_rport = 1;
Brian Kinga3b7aea2009-02-02 18:39:40 -06003318 } else
3319 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3320 } else if (prli_rsp[index].retry)
3321 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3322 else
3323 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3324 } else
3325 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King072b91f2008-07-01 13:14:30 -05003326 break;
3327 case IBMVFC_MAD_DRIVER_FAILED:
3328 break;
3329 case IBMVFC_MAD_CRQ_ERROR:
3330 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3331 break;
3332 case IBMVFC_MAD_FAILED:
3333 default:
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003334 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3335 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
Brian King017b2ae2009-06-18 09:06:55 -05003336 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3337 else if (tgt->logo_rcvd)
3338 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003339 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
Brian King7d0e4622009-05-28 16:17:26 -05003340 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
Brian King10e79492008-10-29 08:46:45 -05003341 else
3342 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05003343
3344 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003345 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
Brian King7d0e4622009-05-28 16:17:26 -05003346 rsp->status, rsp->error, status);
Brian King072b91f2008-07-01 13:14:30 -05003347 break;
3348 };
3349
3350 kref_put(&tgt->kref, ibmvfc_release_tgt);
3351 ibmvfc_free_event(evt);
3352 wake_up(&vhost->work_wait_q);
3353}
3354
3355/**
3356 * ibmvfc_tgt_send_prli - Send a process login
3357 * @tgt: ibmvfc target struct
3358 *
3359 **/
3360static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3361{
3362 struct ibmvfc_process_login *prli;
3363 struct ibmvfc_host *vhost = tgt->vhost;
3364 struct ibmvfc_event *evt;
3365
3366 if (vhost->discovery_threads >= disc_threads)
3367 return;
3368
3369 kref_get(&tgt->kref);
3370 evt = ibmvfc_get_event(vhost);
3371 vhost->discovery_threads++;
3372 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3373 evt->tgt = tgt;
3374 prli = &evt->iu.prli;
3375 memset(prli, 0, sizeof(*prli));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003376 prli->common.version = cpu_to_be32(1);
3377 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3378 prli->common.length = cpu_to_be16(sizeof(*prli));
3379 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
Brian King072b91f2008-07-01 13:14:30 -05003380
3381 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003382 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3383 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
Tyrel Datwylerbb5a5052016-08-03 16:36:52 -05003384 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
Brian King072b91f2008-07-01 13:14:30 -05003385
3386 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3387 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3388 vhost->discovery_threads--;
3389 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3390 kref_put(&tgt->kref, ibmvfc_release_tgt);
3391 } else
3392 tgt_dbg(tgt, "Sent process login\n");
3393}
3394
3395/**
3396 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3397 * @evt: ibmvfc event struct
3398 *
3399 **/
3400static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3401{
3402 struct ibmvfc_target *tgt = evt->tgt;
3403 struct ibmvfc_host *vhost = evt->vhost;
3404 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003405 u32 status = be16_to_cpu(rsp->common.status);
Brian King7d0e4622009-05-28 16:17:26 -05003406 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003407
3408 vhost->discovery_threads--;
3409 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3410 switch (status) {
3411 case IBMVFC_MAD_SUCCESS:
3412 tgt_dbg(tgt, "Port Login succeeded\n");
3413 if (tgt->ids.port_name &&
3414 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3415 vhost->reinit = 1;
3416 tgt_dbg(tgt, "Port re-init required\n");
3417 break;
3418 }
3419 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3420 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3421 tgt->ids.port_id = tgt->scsi_id;
Brian King072b91f2008-07-01 13:14:30 -05003422 memcpy(&tgt->service_parms, &rsp->service_parms,
3423 sizeof(tgt->service_parms));
3424 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3425 sizeof(tgt->service_parms_change));
3426 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3427 break;
3428 case IBMVFC_MAD_DRIVER_FAILED:
3429 break;
3430 case IBMVFC_MAD_CRQ_ERROR:
3431 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3432 break;
3433 case IBMVFC_MAD_FAILED:
3434 default:
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003435 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
Brian King7d0e4622009-05-28 16:17:26 -05003436 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3437 else
3438 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3439
3440 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003441 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), rsp->status, rsp->error,
3442 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), rsp->fc_type,
3443 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05003444 break;
3445 };
3446
3447 kref_put(&tgt->kref, ibmvfc_release_tgt);
3448 ibmvfc_free_event(evt);
3449 wake_up(&vhost->work_wait_q);
3450}
3451
3452/**
3453 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3454 * @tgt: ibmvfc target struct
3455 *
3456 **/
3457static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3458{
3459 struct ibmvfc_port_login *plogi;
3460 struct ibmvfc_host *vhost = tgt->vhost;
3461 struct ibmvfc_event *evt;
3462
3463 if (vhost->discovery_threads >= disc_threads)
3464 return;
3465
3466 kref_get(&tgt->kref);
Brian King017b2ae2009-06-18 09:06:55 -05003467 tgt->logo_rcvd = 0;
Brian King072b91f2008-07-01 13:14:30 -05003468 evt = ibmvfc_get_event(vhost);
3469 vhost->discovery_threads++;
3470 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3471 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3472 evt->tgt = tgt;
3473 plogi = &evt->iu.plogi;
3474 memset(plogi, 0, sizeof(*plogi));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003475 plogi->common.version = cpu_to_be32(1);
3476 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3477 plogi->common.length = cpu_to_be16(sizeof(*plogi));
3478 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
Brian King072b91f2008-07-01 13:14:30 -05003479
3480 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3481 vhost->discovery_threads--;
3482 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3483 kref_put(&tgt->kref, ibmvfc_release_tgt);
3484 } else
3485 tgt_dbg(tgt, "Sent port login\n");
3486}
3487
3488/**
3489 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3490 * @evt: ibmvfc event struct
3491 *
3492 **/
3493static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3494{
3495 struct ibmvfc_target *tgt = evt->tgt;
3496 struct ibmvfc_host *vhost = evt->vhost;
3497 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003498 u32 status = be16_to_cpu(rsp->common.status);
Brian King072b91f2008-07-01 13:14:30 -05003499
3500 vhost->discovery_threads--;
3501 ibmvfc_free_event(evt);
3502 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3503
3504 switch (status) {
3505 case IBMVFC_MAD_SUCCESS:
3506 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3507 break;
3508 case IBMVFC_MAD_DRIVER_FAILED:
3509 kref_put(&tgt->kref, ibmvfc_release_tgt);
3510 wake_up(&vhost->work_wait_q);
3511 return;
3512 case IBMVFC_MAD_FAILED:
3513 default:
3514 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3515 break;
3516 };
3517
3518 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
3519 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3520 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
3521 tgt->scsi_id != tgt->new_scsi_id)
3522 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3523 kref_put(&tgt->kref, ibmvfc_release_tgt);
3524 wake_up(&vhost->work_wait_q);
3525}
3526
3527/**
3528 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3529 * @tgt: ibmvfc target struct
3530 *
3531 **/
3532static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3533{
3534 struct ibmvfc_implicit_logout *mad;
3535 struct ibmvfc_host *vhost = tgt->vhost;
3536 struct ibmvfc_event *evt;
3537
3538 if (vhost->discovery_threads >= disc_threads)
3539 return;
3540
3541 kref_get(&tgt->kref);
3542 evt = ibmvfc_get_event(vhost);
3543 vhost->discovery_threads++;
3544 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3545 evt->tgt = tgt;
3546 mad = &evt->iu.implicit_logout;
3547 memset(mad, 0, sizeof(*mad));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003548 mad->common.version = cpu_to_be32(1);
3549 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3550 mad->common.length = cpu_to_be16(sizeof(*mad));
3551 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
Brian King072b91f2008-07-01 13:14:30 -05003552
3553 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3554 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3555 vhost->discovery_threads--;
3556 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3557 kref_put(&tgt->kref, ibmvfc_release_tgt);
3558 } else
3559 tgt_dbg(tgt, "Sent Implicit Logout\n");
3560}
3561
3562/**
Brian King989b8542008-07-22 08:31:47 -05003563 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3564 * @mad: ibmvfc passthru mad struct
3565 * @tgt: ibmvfc target struct
3566 *
3567 * Returns:
3568 * 1 if PLOGI needed / 0 if PLOGI not needed
3569 **/
3570static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3571 struct ibmvfc_target *tgt)
3572{
3573 if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3574 sizeof(tgt->ids.port_name)))
3575 return 1;
3576 if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3577 sizeof(tgt->ids.node_name)))
3578 return 1;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003579 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
Brian King989b8542008-07-22 08:31:47 -05003580 return 1;
3581 return 0;
3582}
3583
3584/**
3585 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3586 * @evt: ibmvfc event struct
3587 *
3588 **/
3589static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3590{
3591 struct ibmvfc_target *tgt = evt->tgt;
3592 struct ibmvfc_host *vhost = evt->vhost;
3593 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003594 u32 status = be16_to_cpu(mad->common.status);
Brian King989b8542008-07-22 08:31:47 -05003595 u8 fc_reason, fc_explain;
3596
3597 vhost->discovery_threads--;
3598 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
Brian King10501e12009-03-20 15:44:39 -05003599 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003600
3601 switch (status) {
3602 case IBMVFC_MAD_SUCCESS:
3603 tgt_dbg(tgt, "ADISC succeeded\n");
3604 if (ibmvfc_adisc_needs_plogi(mad, tgt))
Brian King5e471672009-05-28 16:17:32 -05003605 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King989b8542008-07-22 08:31:47 -05003606 break;
3607 case IBMVFC_MAD_DRIVER_FAILED:
3608 break;
3609 case IBMVFC_MAD_FAILED:
3610 default:
Brian King5e471672009-05-28 16:17:32 -05003611 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003612 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3613 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
Brian King989b8542008-07-22 08:31:47 -05003614 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003615 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
Brian King989b8542008-07-22 08:31:47 -05003616 mad->iu.status, mad->iu.error,
3617 ibmvfc_get_fc_type(fc_reason), fc_reason,
3618 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3619 break;
3620 };
3621
3622 kref_put(&tgt->kref, ibmvfc_release_tgt);
3623 ibmvfc_free_event(evt);
3624 wake_up(&vhost->work_wait_q);
3625}
3626
3627/**
3628 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3629 * @evt: ibmvfc event struct
3630 *
3631 **/
3632static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3633{
3634 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3635
3636 memset(mad, 0, sizeof(*mad));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003637 mad->common.version = cpu_to_be32(1);
3638 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3639 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3640 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3641 offsetof(struct ibmvfc_passthru_mad, iu));
3642 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3643 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3644 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3645 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
Brian King989b8542008-07-22 08:31:47 -05003646 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003647 offsetof(struct ibmvfc_passthru_fc_iu, payload));
3648 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3649 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
Brian King989b8542008-07-22 08:31:47 -05003650 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003651 offsetof(struct ibmvfc_passthru_fc_iu, response));
3652 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
Brian King989b8542008-07-22 08:31:47 -05003653}
3654
3655/**
Brian King10501e12009-03-20 15:44:39 -05003656 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3657 * @evt: ibmvfc event struct
3658 *
3659 * Just cleanup this event struct. Everything else is handled by
3660 * the ADISC completion handler. If the ADISC never actually comes
3661 * back, we still have the timer running on the ADISC event struct
3662 * which will fire and cause the CRQ to get reset.
3663 *
3664 **/
3665static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3666{
3667 struct ibmvfc_host *vhost = evt->vhost;
3668 struct ibmvfc_target *tgt = evt->tgt;
3669
3670 tgt_dbg(tgt, "ADISC cancel complete\n");
3671 vhost->abort_threads--;
3672 ibmvfc_free_event(evt);
3673 kref_put(&tgt->kref, ibmvfc_release_tgt);
3674 wake_up(&vhost->work_wait_q);
3675}
3676
3677/**
3678 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3679 * @tgt: ibmvfc target struct
3680 *
3681 * If an ADISC times out, send a cancel. If the cancel times
3682 * out, reset the CRQ. When the ADISC comes back as cancelled,
3683 * log back into the target.
3684 **/
3685static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
3686{
3687 struct ibmvfc_host *vhost = tgt->vhost;
3688 struct ibmvfc_event *evt;
3689 struct ibmvfc_tmf *tmf;
3690 unsigned long flags;
3691 int rc;
3692
3693 tgt_dbg(tgt, "ADISC timeout\n");
3694 spin_lock_irqsave(vhost->host->host_lock, flags);
3695 if (vhost->abort_threads >= disc_threads ||
3696 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3697 vhost->state != IBMVFC_INITIALIZING ||
3698 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3699 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3700 return;
3701 }
3702
3703 vhost->abort_threads++;
3704 kref_get(&tgt->kref);
3705 evt = ibmvfc_get_event(vhost);
3706 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3707
3708 evt->tgt = tgt;
3709 tmf = &evt->iu.tmf;
3710 memset(tmf, 0, sizeof(*tmf));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003711 tmf->common.version = cpu_to_be32(1);
3712 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3713 tmf->common.length = cpu_to_be16(sizeof(*tmf));
3714 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3715 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
Brian King10501e12009-03-20 15:44:39 -05003716
3717 rc = ibmvfc_send_event(evt, vhost, default_timeout);
3718
3719 if (rc) {
3720 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3721 vhost->abort_threads--;
3722 kref_put(&tgt->kref, ibmvfc_release_tgt);
3723 __ibmvfc_reset_host(vhost);
3724 } else
3725 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3726 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3727}
3728
3729/**
Brian King989b8542008-07-22 08:31:47 -05003730 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3731 * @tgt: ibmvfc target struct
3732 *
Brian King10501e12009-03-20 15:44:39 -05003733 * When sending an ADISC we end up with two timers running. The
3734 * first timer is the timer in the ibmvfc target struct. If this
3735 * fires, we send a cancel to the target. The second timer is the
3736 * timer on the ibmvfc event for the ADISC, which is longer. If that
3737 * fires, it means the ADISC timed out and our attempt to cancel it
3738 * also failed, so we need to reset the CRQ.
Brian King989b8542008-07-22 08:31:47 -05003739 **/
3740static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3741{
3742 struct ibmvfc_passthru_mad *mad;
3743 struct ibmvfc_host *vhost = tgt->vhost;
3744 struct ibmvfc_event *evt;
3745
3746 if (vhost->discovery_threads >= disc_threads)
3747 return;
3748
3749 kref_get(&tgt->kref);
3750 evt = ibmvfc_get_event(vhost);
3751 vhost->discovery_threads++;
3752 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3753 evt->tgt = tgt;
3754
3755 ibmvfc_init_passthru(evt);
3756 mad = &evt->iu.passthru;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003757 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
3758 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
3759 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
Brian King989b8542008-07-22 08:31:47 -05003760
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003761 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
Brian King989b8542008-07-22 08:31:47 -05003762 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3763 sizeof(vhost->login_buf->resp.port_name));
3764 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3765 sizeof(vhost->login_buf->resp.node_name));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003766 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
Brian King989b8542008-07-22 08:31:47 -05003767
Brian King10501e12009-03-20 15:44:39 -05003768 if (timer_pending(&tgt->timer))
3769 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
3770 else {
3771 tgt->timer.data = (unsigned long) tgt;
3772 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
3773 tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
3774 add_timer(&tgt->timer);
3775 }
3776
Brian King989b8542008-07-22 08:31:47 -05003777 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
Brian King10501e12009-03-20 15:44:39 -05003778 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
Brian King989b8542008-07-22 08:31:47 -05003779 vhost->discovery_threads--;
Brian King10501e12009-03-20 15:44:39 -05003780 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003781 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3782 kref_put(&tgt->kref, ibmvfc_release_tgt);
3783 } else
3784 tgt_dbg(tgt, "Sent ADISC\n");
3785}
3786
3787/**
Brian King072b91f2008-07-01 13:14:30 -05003788 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3789 * @evt: ibmvfc event struct
3790 *
3791 **/
3792static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3793{
3794 struct ibmvfc_target *tgt = evt->tgt;
3795 struct ibmvfc_host *vhost = evt->vhost;
3796 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003797 u32 status = be16_to_cpu(rsp->common.status);
Brian King7d0e4622009-05-28 16:17:26 -05003798 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003799
3800 vhost->discovery_threads--;
3801 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3802 switch (status) {
3803 case IBMVFC_MAD_SUCCESS:
3804 tgt_dbg(tgt, "Query Target succeeded\n");
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003805 tgt->new_scsi_id = be64_to_cpu(rsp->scsi_id);
3806 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
Brian King072b91f2008-07-01 13:14:30 -05003807 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
Brian King989b8542008-07-22 08:31:47 -05003808 else
3809 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
Brian King072b91f2008-07-01 13:14:30 -05003810 break;
3811 case IBMVFC_MAD_DRIVER_FAILED:
3812 break;
3813 case IBMVFC_MAD_CRQ_ERROR:
3814 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3815 break;
3816 case IBMVFC_MAD_FAILED:
3817 default:
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003818 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3819 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3820 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
Brian King072b91f2008-07-01 13:14:30 -05003821 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003822 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
Brian King7d0e4622009-05-28 16:17:26 -05003823 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
Brian King10e79492008-10-29 08:46:45 -05003824 else
3825 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05003826
3827 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003828 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3829 rsp->status, rsp->error, ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)),
3830 rsp->fc_type, ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)),
3831 rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05003832 break;
3833 };
3834
3835 kref_put(&tgt->kref, ibmvfc_release_tgt);
3836 ibmvfc_free_event(evt);
3837 wake_up(&vhost->work_wait_q);
3838}
3839
3840/**
3841 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3842 * @tgt: ibmvfc target struct
3843 *
3844 **/
3845static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3846{
3847 struct ibmvfc_query_tgt *query_tgt;
3848 struct ibmvfc_host *vhost = tgt->vhost;
3849 struct ibmvfc_event *evt;
3850
3851 if (vhost->discovery_threads >= disc_threads)
3852 return;
3853
3854 kref_get(&tgt->kref);
3855 evt = ibmvfc_get_event(vhost);
3856 vhost->discovery_threads++;
3857 evt->tgt = tgt;
3858 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3859 query_tgt = &evt->iu.query_tgt;
3860 memset(query_tgt, 0, sizeof(*query_tgt));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003861 query_tgt->common.version = cpu_to_be32(1);
3862 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
3863 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
3864 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
Brian King072b91f2008-07-01 13:14:30 -05003865
3866 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3867 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3868 vhost->discovery_threads--;
3869 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3870 kref_put(&tgt->kref, ibmvfc_release_tgt);
3871 } else
3872 tgt_dbg(tgt, "Sent Query Target\n");
3873}
3874
3875/**
3876 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3877 * @vhost: ibmvfc host struct
3878 * @scsi_id: SCSI ID to allocate target for
3879 *
3880 * Returns:
3881 * 0 on success / other on failure
3882 **/
3883static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3884{
3885 struct ibmvfc_target *tgt;
3886 unsigned long flags;
3887
3888 spin_lock_irqsave(vhost->host->host_lock, flags);
3889 list_for_each_entry(tgt, &vhost->targets, queue) {
3890 if (tgt->scsi_id == scsi_id) {
3891 if (tgt->need_login)
3892 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3893 goto unlock_out;
3894 }
3895 }
3896 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3897
Brian King7270b9b2009-05-28 16:17:24 -05003898 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
Brian King072b91f2008-07-01 13:14:30 -05003899 if (!tgt) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00003900 dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
Brian King072b91f2008-07-01 13:14:30 -05003901 scsi_id);
3902 return -ENOMEM;
3903 }
3904
Brian King0883e3b2009-02-04 16:13:12 -06003905 memset(tgt, 0, sizeof(*tgt));
Brian King072b91f2008-07-01 13:14:30 -05003906 tgt->scsi_id = scsi_id;
3907 tgt->new_scsi_id = scsi_id;
3908 tgt->vhost = vhost;
3909 tgt->need_login = 1;
Brian King10501e12009-03-20 15:44:39 -05003910 tgt->cancel_key = vhost->task_set++;
3911 init_timer(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05003912 kref_init(&tgt->kref);
3913 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3914 spin_lock_irqsave(vhost->host->host_lock, flags);
3915 list_add_tail(&tgt->queue, &vhost->targets);
3916
3917unlock_out:
3918 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3919 return 0;
3920}
3921
3922/**
3923 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3924 * @vhost: ibmvfc host struct
3925 *
3926 * Returns:
3927 * 0 on success / other on failure
3928 **/
3929static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3930{
3931 int i, rc;
3932
3933 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3934 rc = ibmvfc_alloc_target(vhost,
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003935 be32_to_cpu(vhost->disc_buf->scsi_id[i]) &
3936 IBMVFC_DISC_TGT_SCSI_ID_MASK);
Brian King072b91f2008-07-01 13:14:30 -05003937
3938 return rc;
3939}
3940
3941/**
3942 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3943 * @evt: ibmvfc event struct
3944 *
3945 **/
3946static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3947{
3948 struct ibmvfc_host *vhost = evt->vhost;
3949 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003950 u32 mad_status = be16_to_cpu(rsp->common.status);
Brian King7d0e4622009-05-28 16:17:26 -05003951 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003952
3953 switch (mad_status) {
3954 case IBMVFC_MAD_SUCCESS:
3955 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003956 vhost->num_targets = be32_to_cpu(rsp->num_written);
Brian King072b91f2008-07-01 13:14:30 -05003957 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3958 break;
3959 case IBMVFC_MAD_FAILED:
Brian King7d0e4622009-05-28 16:17:26 -05003960 level += ibmvfc_retry_host_init(vhost);
3961 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003962 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3963 rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05003964 break;
3965 case IBMVFC_MAD_DRIVER_FAILED:
3966 break;
3967 default:
3968 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3969 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3970 break;
3971 }
3972
3973 ibmvfc_free_event(evt);
3974 wake_up(&vhost->work_wait_q);
3975}
3976
3977/**
3978 * ibmvfc_discover_targets - Send Discover Targets MAD
3979 * @vhost: ibmvfc host struct
3980 *
3981 **/
3982static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3983{
3984 struct ibmvfc_discover_targets *mad;
3985 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3986
3987 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3988 mad = &evt->iu.discover_targets;
3989 memset(mad, 0, sizeof(*mad));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05003990 mad->common.version = cpu_to_be32(1);
3991 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
3992 mad->common.length = cpu_to_be16(sizeof(*mad));
3993 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
3994 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
3995 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
Brian King072b91f2008-07-01 13:14:30 -05003996 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3997
3998 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3999 ibmvfc_dbg(vhost, "Sent discover targets\n");
4000 else
4001 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4002}
4003
4004/**
4005 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4006 * @evt: ibmvfc event struct
4007 *
4008 **/
4009static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4010{
4011 struct ibmvfc_host *vhost = evt->vhost;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004012 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
Brian King072b91f2008-07-01 13:14:30 -05004013 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4014 unsigned int npiv_max_sectors;
Brian King7d0e4622009-05-28 16:17:26 -05004015 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05004016
4017 switch (mad_status) {
4018 case IBMVFC_MAD_SUCCESS:
4019 ibmvfc_free_event(evt);
4020 break;
4021 case IBMVFC_MAD_FAILED:
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004022 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
Brian King7d0e4622009-05-28 16:17:26 -05004023 level += ibmvfc_retry_host_init(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004024 else
4025 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
Brian King7d0e4622009-05-28 16:17:26 -05004026 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004027 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4028 rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05004029 ibmvfc_free_event(evt);
4030 return;
4031 case IBMVFC_MAD_CRQ_ERROR:
4032 ibmvfc_retry_host_init(vhost);
4033 case IBMVFC_MAD_DRIVER_FAILED:
4034 ibmvfc_free_event(evt);
4035 return;
4036 default:
4037 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4038 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4039 ibmvfc_free_event(evt);
4040 return;
4041 }
4042
4043 vhost->client_migrated = 0;
4044
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004045 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
Brian King072b91f2008-07-01 13:14:30 -05004046 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4047 rsp->flags);
4048 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4049 wake_up(&vhost->work_wait_q);
4050 return;
4051 }
4052
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004053 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
Brian King072b91f2008-07-01 13:14:30 -05004054 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4055 rsp->max_cmds);
4056 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4057 wake_up(&vhost->work_wait_q);
4058 return;
4059 }
4060
Brian King79111d02009-05-28 16:17:29 -05004061 vhost->logged_in = 1;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004062 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
Brian King072b91f2008-07-01 13:14:30 -05004063 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4064 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4065 rsp->drc_name, npiv_max_sectors);
4066
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004067 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4068 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4069 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4070 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
Brian King072b91f2008-07-01 13:14:30 -05004071 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4072 fc_host_supported_classes(vhost->host) = 0;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004073 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
Brian King072b91f2008-07-01 13:14:30 -05004074 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004075 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
Brian King072b91f2008-07-01 13:14:30 -05004076 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004077 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
Brian King072b91f2008-07-01 13:14:30 -05004078 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4079 fc_host_maxframe_size(vhost->host) =
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004080 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
Brian King072b91f2008-07-01 13:14:30 -05004081
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004082 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
Brian King072b91f2008-07-01 13:14:30 -05004083 vhost->host->max_sectors = npiv_max_sectors;
4084 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4085 wake_up(&vhost->work_wait_q);
4086}
4087
4088/**
4089 * ibmvfc_npiv_login - Sends NPIV login
4090 * @vhost: ibmvfc host struct
4091 *
4092 **/
4093static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4094{
4095 struct ibmvfc_npiv_login_mad *mad;
4096 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4097
4098 ibmvfc_gather_partition_info(vhost);
4099 ibmvfc_set_login_info(vhost);
4100 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4101
4102 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4103 mad = &evt->iu.npiv_login;
4104 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004105 mad->common.version = cpu_to_be32(1);
4106 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4107 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4108 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4109 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
Brian King072b91f2008-07-01 13:14:30 -05004110
Brian King072b91f2008-07-01 13:14:30 -05004111 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4112
4113 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4114 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4115 else
4116 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4117};
4118
4119/**
Brian King79111d02009-05-28 16:17:29 -05004120 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4121 * @vhost: ibmvfc host struct
4122 *
4123 **/
4124static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4125{
4126 struct ibmvfc_host *vhost = evt->vhost;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004127 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
Brian King79111d02009-05-28 16:17:29 -05004128
4129 ibmvfc_free_event(evt);
4130
4131 switch (mad_status) {
4132 case IBMVFC_MAD_SUCCESS:
4133 if (list_empty(&vhost->sent) &&
4134 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
Brian King861890c2009-10-19 15:07:49 -05004135 ibmvfc_init_host(vhost);
Brian King79111d02009-05-28 16:17:29 -05004136 return;
4137 }
4138 break;
4139 case IBMVFC_MAD_FAILED:
4140 case IBMVFC_MAD_NOT_SUPPORTED:
4141 case IBMVFC_MAD_CRQ_ERROR:
4142 case IBMVFC_MAD_DRIVER_FAILED:
4143 default:
4144 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4145 break;
4146 }
4147
4148 ibmvfc_hard_reset_host(vhost);
4149}
4150
4151/**
4152 * ibmvfc_npiv_logout - Issue an NPIV Logout
4153 * @vhost: ibmvfc host struct
4154 *
4155 **/
4156static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4157{
4158 struct ibmvfc_npiv_logout_mad *mad;
4159 struct ibmvfc_event *evt;
4160
4161 evt = ibmvfc_get_event(vhost);
4162 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4163
4164 mad = &evt->iu.npiv_logout;
4165 memset(mad, 0, sizeof(*mad));
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004166 mad->common.version = cpu_to_be32(1);
4167 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4168 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
Brian King79111d02009-05-28 16:17:29 -05004169
4170 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4171
4172 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4173 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4174 else
4175 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4176}
4177
4178/**
Brian King072b91f2008-07-01 13:14:30 -05004179 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4180 * @vhost: ibmvfc host struct
4181 *
4182 * Returns:
4183 * 1 if work to do / 0 if not
4184 **/
4185static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4186{
4187 struct ibmvfc_target *tgt;
4188
4189 list_for_each_entry(tgt, &vhost->targets, queue) {
4190 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4191 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4192 return 1;
4193 }
4194
4195 return 0;
4196}
4197
4198/**
4199 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4200 * @vhost: ibmvfc host struct
4201 *
4202 * Returns:
4203 * 1 if work to do / 0 if not
4204 **/
4205static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4206{
4207 struct ibmvfc_target *tgt;
4208
4209 if (kthread_should_stop())
4210 return 1;
4211 switch (vhost->action) {
4212 case IBMVFC_HOST_ACTION_NONE:
4213 case IBMVFC_HOST_ACTION_INIT_WAIT:
Brian King79111d02009-05-28 16:17:29 -05004214 case IBMVFC_HOST_ACTION_LOGO_WAIT:
Brian King072b91f2008-07-01 13:14:30 -05004215 return 0;
4216 case IBMVFC_HOST_ACTION_TGT_INIT:
4217 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4218 if (vhost->discovery_threads == disc_threads)
4219 return 0;
4220 list_for_each_entry(tgt, &vhost->targets, queue)
4221 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4222 return 1;
4223 list_for_each_entry(tgt, &vhost->targets, queue)
4224 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4225 return 0;
4226 return 1;
Brian King79111d02009-05-28 16:17:29 -05004227 case IBMVFC_HOST_ACTION_LOGO:
Brian King072b91f2008-07-01 13:14:30 -05004228 case IBMVFC_HOST_ACTION_INIT:
4229 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
Brian King072b91f2008-07-01 13:14:30 -05004230 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05004231 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05004232 case IBMVFC_HOST_ACTION_QUERY:
Brian King73ee5d82010-06-17 13:55:13 -05004233 case IBMVFC_HOST_ACTION_RESET:
4234 case IBMVFC_HOST_ACTION_REENABLE:
Brian King072b91f2008-07-01 13:14:30 -05004235 default:
4236 break;
4237 };
4238
4239 return 1;
4240}
4241
4242/**
4243 * ibmvfc_work_to_do - Is there task level work to do?
4244 * @vhost: ibmvfc host struct
4245 *
4246 * Returns:
4247 * 1 if work to do / 0 if not
4248 **/
4249static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4250{
4251 unsigned long flags;
4252 int rc;
4253
4254 spin_lock_irqsave(vhost->host->host_lock, flags);
4255 rc = __ibmvfc_work_to_do(vhost);
4256 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4257 return rc;
4258}
4259
4260/**
4261 * ibmvfc_log_ae - Log async events if necessary
4262 * @vhost: ibmvfc host struct
4263 * @events: events to log
4264 *
4265 **/
4266static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4267{
4268 if (events & IBMVFC_AE_RSCN)
4269 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4270 if ((events & IBMVFC_AE_LINKDOWN) &&
4271 vhost->state >= IBMVFC_HALTED)
4272 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4273 if ((events & IBMVFC_AE_LINKUP) &&
4274 vhost->state == IBMVFC_INITIALIZING)
4275 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4276}
4277
4278/**
4279 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4280 * @tgt: ibmvfc target struct
4281 *
4282 **/
4283static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4284{
4285 struct ibmvfc_host *vhost = tgt->vhost;
Brian King43c8da92009-05-28 16:17:28 -05004286 struct fc_rport *rport;
Brian King072b91f2008-07-01 13:14:30 -05004287 unsigned long flags;
4288
4289 tgt_dbg(tgt, "Adding rport\n");
4290 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4291 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King43c8da92009-05-28 16:17:28 -05004292
4293 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4294 tgt_dbg(tgt, "Deleting rport\n");
4295 list_del(&tgt->queue);
Brian Kingd5da3042010-08-05 16:38:31 -05004296 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
Brian King43c8da92009-05-28 16:17:28 -05004297 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4298 fc_remote_port_delete(rport);
4299 del_timer_sync(&tgt->timer);
4300 kref_put(&tgt->kref, ibmvfc_release_tgt);
4301 return;
Brian Kingd5da3042010-08-05 16:38:31 -05004302 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4303 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4304 return;
Brian King43c8da92009-05-28 16:17:28 -05004305 }
4306
Brian King072b91f2008-07-01 13:14:30 -05004307 if (rport) {
4308 tgt_dbg(tgt, "rport add succeeded\n");
Brian King43c8da92009-05-28 16:17:28 -05004309 tgt->rport = rport;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004310 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
Brian King072b91f2008-07-01 13:14:30 -05004311 rport->supported_classes = 0;
Brian King52d7e862008-07-22 08:31:46 -05004312 tgt->target_id = rport->scsi_target_id;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004313 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
Brian King072b91f2008-07-01 13:14:30 -05004314 rport->supported_classes |= FC_COS_CLASS1;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004315 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
Brian King072b91f2008-07-01 13:14:30 -05004316 rport->supported_classes |= FC_COS_CLASS2;
Tyrel Datwyler0aab6c32014-06-26 19:03:55 -05004317 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
Brian King072b91f2008-07-01 13:14:30 -05004318 rport->supported_classes |= FC_COS_CLASS3;
Brian Kingd31429e2009-10-19 15:07:54 -05004319 if (rport->rqst_q)
Martin K. Petersen8a783622010-02-26 00:20:39 -05004320 blk_queue_max_segments(rport->rqst_q, 1);
Brian King072b91f2008-07-01 13:14:30 -05004321 } else
4322 tgt_dbg(tgt, "rport add failed\n");
4323 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4324}
4325
4326/**
4327 * ibmvfc_do_work - Do task level work
4328 * @vhost: ibmvfc host struct
4329 *
4330 **/
4331static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4332{
4333 struct ibmvfc_target *tgt;
4334 unsigned long flags;
4335 struct fc_rport *rport;
Brian King73ee5d82010-06-17 13:55:13 -05004336 int rc;
Brian King072b91f2008-07-01 13:14:30 -05004337
4338 ibmvfc_log_ae(vhost, vhost->events_to_log);
4339 spin_lock_irqsave(vhost->host->host_lock, flags);
4340 vhost->events_to_log = 0;
4341 switch (vhost->action) {
4342 case IBMVFC_HOST_ACTION_NONE:
Brian King79111d02009-05-28 16:17:29 -05004343 case IBMVFC_HOST_ACTION_LOGO_WAIT:
Brian King072b91f2008-07-01 13:14:30 -05004344 case IBMVFC_HOST_ACTION_INIT_WAIT:
4345 break;
Brian King73ee5d82010-06-17 13:55:13 -05004346 case IBMVFC_HOST_ACTION_RESET:
4347 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4348 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4349 rc = ibmvfc_reset_crq(vhost);
4350 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian Kingd24099d2010-09-21 10:17:11 -05004351 if (rc == H_CLOSED)
4352 vio_enable_interrupts(to_vio_dev(vhost->dev));
Brian King38553562011-06-03 08:23:20 -05004353 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4354 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
Brian King73ee5d82010-06-17 13:55:13 -05004355 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4356 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4357 }
4358 break;
4359 case IBMVFC_HOST_ACTION_REENABLE:
4360 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4361 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4362 rc = ibmvfc_reenable_crq_queue(vhost);
4363 spin_lock_irqsave(vhost->host->host_lock, flags);
4364 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4365 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4366 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4367 }
4368 break;
Brian King79111d02009-05-28 16:17:29 -05004369 case IBMVFC_HOST_ACTION_LOGO:
4370 vhost->job_step(vhost);
4371 break;
Brian King072b91f2008-07-01 13:14:30 -05004372 case IBMVFC_HOST_ACTION_INIT:
4373 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
Brian King1c41fa822008-12-03 11:02:54 -06004374 if (vhost->delay_init) {
4375 vhost->delay_init = 0;
4376 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian Kingd2131b32008-12-18 09:26:51 -06004377 ssleep(15);
Brian King1c41fa822008-12-03 11:02:54 -06004378 return;
4379 } else
4380 vhost->job_step(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004381 break;
4382 case IBMVFC_HOST_ACTION_QUERY:
4383 list_for_each_entry(tgt, &vhost->targets, queue)
4384 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4385 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4386 break;
4387 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4388 list_for_each_entry(tgt, &vhost->targets, queue) {
4389 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4390 tgt->job_step(tgt);
4391 break;
4392 }
4393 }
4394
4395 if (!ibmvfc_dev_init_to_do(vhost))
4396 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4397 break;
4398 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05004399 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05004400 list_for_each_entry(tgt, &vhost->targets, queue) {
4401 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4402 tgt_dbg(tgt, "Deleting rport\n");
4403 rport = tgt->rport;
4404 tgt->rport = NULL;
4405 list_del(&tgt->queue);
Brian Kingd5da3042010-08-05 16:38:31 -05004406 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
Brian King072b91f2008-07-01 13:14:30 -05004407 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4408 if (rport)
4409 fc_remote_port_delete(rport);
Brian King10501e12009-03-20 15:44:39 -05004410 del_timer_sync(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05004411 kref_put(&tgt->kref, ibmvfc_release_tgt);
4412 return;
4413 }
4414 }
4415
4416 if (vhost->state == IBMVFC_INITIALIZING) {
Brian King10e79492008-10-29 08:46:45 -05004417 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
Brian King43c8da92009-05-28 16:17:28 -05004418 if (vhost->reinit) {
4419 vhost->reinit = 0;
4420 scsi_block_requests(vhost->host);
4421 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4422 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4423 } else {
4424 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4425 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4426 wake_up(&vhost->init_wait_q);
4427 schedule_work(&vhost->rport_add_work_q);
4428 vhost->init_retries = 0;
4429 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4430 scsi_unblock_requests(vhost->host);
4431 }
4432
Brian King10e79492008-10-29 08:46:45 -05004433 return;
4434 } else {
4435 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4436 vhost->job_step = ibmvfc_discover_targets;
4437 }
Brian King072b91f2008-07-01 13:14:30 -05004438 } else {
4439 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4440 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4441 scsi_unblock_requests(vhost->host);
4442 wake_up(&vhost->init_wait_q);
4443 return;
4444 }
4445 break;
4446 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4447 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4448 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4449 ibmvfc_alloc_targets(vhost);
4450 spin_lock_irqsave(vhost->host->host_lock, flags);
4451 break;
4452 case IBMVFC_HOST_ACTION_TGT_INIT:
4453 list_for_each_entry(tgt, &vhost->targets, queue) {
4454 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4455 tgt->job_step(tgt);
4456 break;
4457 }
4458 }
4459
Brian King10e79492008-10-29 08:46:45 -05004460 if (!ibmvfc_dev_init_to_do(vhost))
4461 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
Brian King072b91f2008-07-01 13:14:30 -05004462 break;
Brian King072b91f2008-07-01 13:14:30 -05004463 default:
4464 break;
4465 };
4466
4467 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4468}
4469
4470/**
4471 * ibmvfc_work - Do task level work
4472 * @data: ibmvfc host struct
4473 *
4474 * Returns:
4475 * zero
4476 **/
4477static int ibmvfc_work(void *data)
4478{
4479 struct ibmvfc_host *vhost = data;
4480 int rc;
4481
Dongsheng Yang8698a742014-03-11 18:09:12 +08004482 set_user_nice(current, MIN_NICE);
Brian King072b91f2008-07-01 13:14:30 -05004483
4484 while (1) {
4485 rc = wait_event_interruptible(vhost->work_wait_q,
4486 ibmvfc_work_to_do(vhost));
4487
4488 BUG_ON(rc);
4489
4490 if (kthread_should_stop())
4491 break;
4492
4493 ibmvfc_do_work(vhost);
4494 }
4495
4496 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4497 return 0;
4498}
4499
4500/**
4501 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4502 * @vhost: ibmvfc host struct
4503 *
4504 * Allocates a page for messages, maps it for dma, and registers
4505 * the crq with the hypervisor.
4506 *
4507 * Return value:
4508 * zero on success / other on failure
4509 **/
4510static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4511{
4512 int rc, retrc = -ENOMEM;
4513 struct device *dev = vhost->dev;
4514 struct vio_dev *vdev = to_vio_dev(dev);
4515 struct ibmvfc_crq_queue *crq = &vhost->crq;
4516
4517 ENTER;
4518 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4519
4520 if (!crq->msgs)
4521 return -ENOMEM;
4522
4523 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4524 crq->msg_token = dma_map_single(dev, crq->msgs,
4525 PAGE_SIZE, DMA_BIDIRECTIONAL);
4526
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07004527 if (dma_mapping_error(dev, crq->msg_token))
Brian King072b91f2008-07-01 13:14:30 -05004528 goto map_failed;
4529
4530 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4531 crq->msg_token, PAGE_SIZE);
4532
4533 if (rc == H_RESOURCE)
4534 /* maybe kexecing and resource is busy. try a reset */
4535 retrc = rc = ibmvfc_reset_crq(vhost);
4536
4537 if (rc == H_CLOSED)
4538 dev_warn(dev, "Partner adapter not ready\n");
4539 else if (rc) {
4540 dev_warn(dev, "Error %d opening adapter\n", rc);
4541 goto reg_crq_failed;
4542 }
4543
4544 retrc = 0;
4545
Brian King039a0892009-03-20 15:44:35 -05004546 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4547
Brian King072b91f2008-07-01 13:14:30 -05004548 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4549 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4550 goto req_irq_failed;
4551 }
4552
4553 if ((rc = vio_enable_interrupts(vdev))) {
4554 dev_err(dev, "Error %d enabling interrupts\n", rc);
4555 goto req_irq_failed;
4556 }
4557
4558 crq->cur = 0;
4559 LEAVE;
4560 return retrc;
4561
4562req_irq_failed:
Brian King039a0892009-03-20 15:44:35 -05004563 tasklet_kill(&vhost->tasklet);
Brian King072b91f2008-07-01 13:14:30 -05004564 do {
4565 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4566 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4567reg_crq_failed:
4568 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4569map_failed:
4570 free_page((unsigned long)crq->msgs);
4571 return retrc;
4572}
4573
4574/**
4575 * ibmvfc_free_mem - Free memory for vhost
4576 * @vhost: ibmvfc host struct
4577 *
4578 * Return value:
4579 * none
4580 **/
4581static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4582{
4583 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4584
4585 ENTER;
4586 mempool_destroy(vhost->tgt_pool);
4587 kfree(vhost->trace);
4588 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4589 vhost->disc_buf_dma);
4590 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4591 vhost->login_buf, vhost->login_buf_dma);
4592 dma_pool_destroy(vhost->sg_pool);
4593 dma_unmap_single(vhost->dev, async_q->msg_token,
4594 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4595 free_page((unsigned long)async_q->msgs);
4596 LEAVE;
4597}
4598
4599/**
4600 * ibmvfc_alloc_mem - Allocate memory for vhost
4601 * @vhost: ibmvfc host struct
4602 *
4603 * Return value:
4604 * 0 on success / non-zero on failure
4605 **/
4606static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4607{
4608 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4609 struct device *dev = vhost->dev;
4610
4611 ENTER;
4612 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4613 if (!async_q->msgs) {
4614 dev_err(dev, "Couldn't allocate async queue.\n");
4615 goto nomem;
4616 }
4617
4618 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4619 async_q->msg_token = dma_map_single(dev, async_q->msgs,
4620 async_q->size * sizeof(*async_q->msgs),
4621 DMA_BIDIRECTIONAL);
4622
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07004623 if (dma_mapping_error(dev, async_q->msg_token)) {
Brian King072b91f2008-07-01 13:14:30 -05004624 dev_err(dev, "Failed to map async queue\n");
4625 goto free_async_crq;
4626 }
4627
4628 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4629 SG_ALL * sizeof(struct srp_direct_buf),
4630 sizeof(struct srp_direct_buf), 0);
4631
4632 if (!vhost->sg_pool) {
4633 dev_err(dev, "Failed to allocate sg pool\n");
4634 goto unmap_async_crq;
4635 }
4636
4637 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4638 &vhost->login_buf_dma, GFP_KERNEL);
4639
4640 if (!vhost->login_buf) {
4641 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4642 goto free_sg_pool;
4643 }
4644
4645 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
4646 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
4647 &vhost->disc_buf_dma, GFP_KERNEL);
4648
4649 if (!vhost->disc_buf) {
4650 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
4651 goto free_login_buffer;
4652 }
4653
4654 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
4655 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
4656
4657 if (!vhost->trace)
4658 goto free_disc_buffer;
4659
Sage Weild6886692009-08-03 13:54:47 -07004660 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
Brian King072b91f2008-07-01 13:14:30 -05004661 sizeof(struct ibmvfc_target));
4662
4663 if (!vhost->tgt_pool) {
4664 dev_err(dev, "Couldn't allocate target memory pool\n");
4665 goto free_trace;
4666 }
4667
4668 LEAVE;
4669 return 0;
4670
4671free_trace:
4672 kfree(vhost->trace);
4673free_disc_buffer:
4674 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
4675 vhost->disc_buf_dma);
4676free_login_buffer:
4677 dma_free_coherent(dev, sizeof(*vhost->login_buf),
4678 vhost->login_buf, vhost->login_buf_dma);
4679free_sg_pool:
4680 dma_pool_destroy(vhost->sg_pool);
4681unmap_async_crq:
4682 dma_unmap_single(dev, async_q->msg_token,
4683 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4684free_async_crq:
4685 free_page((unsigned long)async_q->msgs);
4686nomem:
4687 LEAVE;
4688 return -ENOMEM;
4689}
4690
4691/**
Brian King43c8da92009-05-28 16:17:28 -05004692 * ibmvfc_rport_add_thread - Worker thread for rport adds
4693 * @work: work struct
4694 *
4695 **/
4696static void ibmvfc_rport_add_thread(struct work_struct *work)
4697{
4698 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
4699 rport_add_work_q);
4700 struct ibmvfc_target *tgt;
4701 struct fc_rport *rport;
4702 unsigned long flags;
4703 int did_work;
4704
4705 ENTER;
4706 spin_lock_irqsave(vhost->host->host_lock, flags);
4707 do {
4708 did_work = 0;
4709 if (vhost->state != IBMVFC_ACTIVE)
4710 break;
4711
4712 list_for_each_entry(tgt, &vhost->targets, queue) {
4713 if (tgt->add_rport) {
4714 did_work = 1;
4715 tgt->add_rport = 0;
4716 kref_get(&tgt->kref);
4717 rport = tgt->rport;
4718 if (!rport) {
4719 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4720 ibmvfc_tgt_add_rport(tgt);
4721 } else if (get_device(&rport->dev)) {
4722 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4723 tgt_dbg(tgt, "Setting rport roles\n");
4724 fc_remote_port_rolechg(rport, tgt->ids.roles);
4725 put_device(&rport->dev);
Dan Carpenter00738872016-07-15 14:18:57 +03004726 } else {
4727 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian King43c8da92009-05-28 16:17:28 -05004728 }
4729
4730 kref_put(&tgt->kref, ibmvfc_release_tgt);
4731 spin_lock_irqsave(vhost->host->host_lock, flags);
4732 break;
4733 }
4734 }
4735 } while(did_work);
4736
4737 if (vhost->state == IBMVFC_ACTIVE)
4738 vhost->scan_complete = 1;
4739 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4740 LEAVE;
4741}
4742
4743/**
Brian King072b91f2008-07-01 13:14:30 -05004744 * ibmvfc_probe - Adapter hot plug add entry point
4745 * @vdev: vio device struct
4746 * @id: vio device id struct
4747 *
4748 * Return value:
4749 * 0 on success / non-zero on failure
4750 **/
4751static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4752{
4753 struct ibmvfc_host *vhost;
4754 struct Scsi_Host *shost;
4755 struct device *dev = &vdev->dev;
4756 int rc = -ENOMEM;
4757
4758 ENTER;
4759 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
4760 if (!shost) {
4761 dev_err(dev, "Couldn't allocate host data\n");
4762 goto out;
4763 }
4764
4765 shost->transportt = ibmvfc_transport_template;
4766 shost->can_queue = max_requests;
4767 shost->max_lun = max_lun;
4768 shost->max_id = max_targets;
4769 shost->max_sectors = IBMVFC_MAX_SECTORS;
4770 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
4771 shost->unique_id = shost->host_no;
4772
4773 vhost = shost_priv(shost);
4774 INIT_LIST_HEAD(&vhost->sent);
4775 INIT_LIST_HEAD(&vhost->free);
4776 INIT_LIST_HEAD(&vhost->targets);
4777 sprintf(vhost->name, IBMVFC_NAME);
4778 vhost->host = shost;
4779 vhost->dev = dev;
4780 vhost->partition_number = -1;
4781 vhost->log_level = log_level;
Brian King10501e12009-03-20 15:44:39 -05004782 vhost->task_set = 1;
Brian King072b91f2008-07-01 13:14:30 -05004783 strcpy(vhost->partition_name, "UNKNOWN");
4784 init_waitqueue_head(&vhost->work_wait_q);
4785 init_waitqueue_head(&vhost->init_wait_q);
Brian King43c8da92009-05-28 16:17:28 -05004786 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
Brian Kingd31429e2009-10-19 15:07:54 -05004787 mutex_init(&vhost->passthru_mutex);
Brian King072b91f2008-07-01 13:14:30 -05004788
4789 if ((rc = ibmvfc_alloc_mem(vhost)))
4790 goto free_scsi_host;
4791
4792 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
4793 shost->host_no);
4794
4795 if (IS_ERR(vhost->work_thread)) {
4796 dev_err(dev, "Couldn't create kernel thread: %ld\n",
4797 PTR_ERR(vhost->work_thread));
4798 goto free_host_mem;
4799 }
4800
4801 if ((rc = ibmvfc_init_crq(vhost))) {
4802 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4803 goto kill_kthread;
4804 }
4805
4806 if ((rc = ibmvfc_init_event_pool(vhost))) {
4807 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4808 goto release_crq;
4809 }
4810
4811 if ((rc = scsi_add_host(shost, dev)))
4812 goto release_event_pool;
4813
Mike Christiea5110f22010-09-15 16:52:29 -05004814 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
4815
Brian King072b91f2008-07-01 13:14:30 -05004816 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4817 &ibmvfc_trace_attr))) {
4818 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4819 goto remove_shost;
4820 }
4821
Brian Kingd31429e2009-10-19 15:07:54 -05004822 if (shost_to_fc_host(shost)->rqst_q)
Martin K. Petersen8a783622010-02-26 00:20:39 -05004823 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
Brian King072b91f2008-07-01 13:14:30 -05004824 dev_set_drvdata(dev, vhost);
4825 spin_lock(&ibmvfc_driver_lock);
4826 list_add_tail(&vhost->queue, &ibmvfc_head);
4827 spin_unlock(&ibmvfc_driver_lock);
4828
4829 ibmvfc_send_crq_init(vhost);
4830 scsi_scan_host(shost);
4831 return 0;
4832
4833remove_shost:
4834 scsi_remove_host(shost);
4835release_event_pool:
4836 ibmvfc_free_event_pool(vhost);
4837release_crq:
4838 ibmvfc_release_crq_queue(vhost);
4839kill_kthread:
4840 kthread_stop(vhost->work_thread);
4841free_host_mem:
4842 ibmvfc_free_mem(vhost);
4843free_scsi_host:
4844 scsi_host_put(shost);
4845out:
4846 LEAVE;
4847 return rc;
4848}
4849
4850/**
4851 * ibmvfc_remove - Adapter hot plug remove entry point
4852 * @vdev: vio device struct
4853 *
4854 * Return value:
4855 * 0
4856 **/
4857static int ibmvfc_remove(struct vio_dev *vdev)
4858{
4859 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4860 unsigned long flags;
4861
4862 ENTER;
4863 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
Brian King70431102009-10-19 15:07:48 -05004864
4865 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King2d0da2a2008-07-22 08:31:42 -05004866 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
Brian King70431102009-10-19 15:07:48 -05004867 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4868
Brian King2d0da2a2008-07-22 08:31:42 -05004869 ibmvfc_wait_while_resetting(vhost);
4870 ibmvfc_release_crq_queue(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004871 kthread_stop(vhost->work_thread);
4872 fc_remove_host(vhost->host);
4873 scsi_remove_host(vhost->host);
Brian King072b91f2008-07-01 13:14:30 -05004874
4875 spin_lock_irqsave(vhost->host->host_lock, flags);
4876 ibmvfc_purge_requests(vhost, DID_ERROR);
4877 ibmvfc_free_event_pool(vhost);
4878 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4879
4880 ibmvfc_free_mem(vhost);
4881 spin_lock(&ibmvfc_driver_lock);
4882 list_del(&vhost->queue);
4883 spin_unlock(&ibmvfc_driver_lock);
4884 scsi_host_put(vhost->host);
4885 LEAVE;
4886 return 0;
4887}
4888
Brian King39c1ffe2008-07-24 04:35:48 +10004889/**
Brian Kingb0f4d4c2010-02-21 10:37:58 -06004890 * ibmvfc_resume - Resume from suspend
4891 * @dev: device struct
4892 *
4893 * We may have lost an interrupt across suspend/resume, so kick the
4894 * interrupt handler
4895 *
4896 */
4897static int ibmvfc_resume(struct device *dev)
4898{
4899 unsigned long flags;
4900 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
4901 struct vio_dev *vdev = to_vio_dev(dev);
4902
4903 spin_lock_irqsave(vhost->host->host_lock, flags);
4904 vio_disable_interrupts(vdev);
4905 tasklet_schedule(&vhost->tasklet);
4906 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4907 return 0;
4908}
4909
4910/**
Brian King39c1ffe2008-07-24 04:35:48 +10004911 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4912 * @vdev: vio device struct
4913 *
4914 * Return value:
4915 * Number of bytes the driver will need to DMA map at the same time in
4916 * order to perform well.
4917 */
4918static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4919{
4920 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4921 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4922}
4923
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004924static struct vio_device_id ibmvfc_device_table[] = {
Brian King072b91f2008-07-01 13:14:30 -05004925 {"fcp", "IBM,vfc-client"},
4926 { "", "" }
4927};
4928MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4929
Brian Kingb0f4d4c2010-02-21 10:37:58 -06004930static struct dev_pm_ops ibmvfc_pm_ops = {
4931 .resume = ibmvfc_resume
4932};
4933
Brian King072b91f2008-07-01 13:14:30 -05004934static struct vio_driver ibmvfc_driver = {
4935 .id_table = ibmvfc_device_table,
4936 .probe = ibmvfc_probe,
4937 .remove = ibmvfc_remove,
Brian King39c1ffe2008-07-24 04:35:48 +10004938 .get_desired_dma = ibmvfc_get_desired_dma,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00004939 .name = IBMVFC_NAME,
4940 .pm = &ibmvfc_pm_ops,
Brian King072b91f2008-07-01 13:14:30 -05004941};
4942
4943static struct fc_function_template ibmvfc_transport_functions = {
4944 .show_host_fabric_name = 1,
4945 .show_host_node_name = 1,
4946 .show_host_port_name = 1,
4947 .show_host_supported_classes = 1,
4948 .show_host_port_type = 1,
4949 .show_host_port_id = 1,
Brian King9ab36102009-03-20 15:44:38 -05004950 .show_host_maxframe_size = 1,
Brian King072b91f2008-07-01 13:14:30 -05004951
4952 .get_host_port_state = ibmvfc_get_host_port_state,
4953 .show_host_port_state = 1,
4954
4955 .get_host_speed = ibmvfc_get_host_speed,
4956 .show_host_speed = 1,
4957
4958 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4959 .terminate_rport_io = ibmvfc_terminate_rport_io,
4960
4961 .show_rport_maxframe_size = 1,
4962 .show_rport_supported_classes = 1,
4963
4964 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4965 .show_rport_dev_loss_tmo = 1,
4966
4967 .get_starget_node_name = ibmvfc_get_starget_node_name,
4968 .show_starget_node_name = 1,
4969
4970 .get_starget_port_name = ibmvfc_get_starget_port_name,
4971 .show_starget_port_name = 1,
4972
4973 .get_starget_port_id = ibmvfc_get_starget_port_id,
4974 .show_starget_port_id = 1,
Brian Kingd31429e2009-10-19 15:07:54 -05004975
4976 .bsg_request = ibmvfc_bsg_request,
4977 .bsg_timeout = ibmvfc_bsg_timeout,
Brian King072b91f2008-07-01 13:14:30 -05004978};
4979
4980/**
4981 * ibmvfc_module_init - Initialize the ibmvfc module
4982 *
4983 * Return value:
4984 * 0 on success / other on failure
4985 **/
4986static int __init ibmvfc_module_init(void)
4987{
4988 int rc;
4989
4990 if (!firmware_has_feature(FW_FEATURE_VIO))
4991 return -ENODEV;
4992
4993 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4994 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4995
4996 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4997 if (!ibmvfc_transport_template)
4998 return -ENOMEM;
4999
5000 rc = vio_register_driver(&ibmvfc_driver);
5001 if (rc)
5002 fc_release_transport(ibmvfc_transport_template);
5003 return rc;
5004}
5005
5006/**
5007 * ibmvfc_module_exit - Teardown the ibmvfc module
5008 *
5009 * Return value:
5010 * nothing
5011 **/
5012static void __exit ibmvfc_module_exit(void)
5013{
5014 vio_unregister_driver(&ibmvfc_driver);
5015 fc_release_transport(ibmvfc_transport_template);
5016}
5017
5018module_init(ibmvfc_module_init);
5019module_exit(ibmvfc_module_exit);