blob: b7650613b8c2606caef87d83a6b0e2a74cc7ca09 [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;
49static unsigned int max_lun = IBMVFC_MAX_LUN;
50static unsigned int max_targets = IBMVFC_MAX_TARGETS;
51static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
52static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
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) "]");
74module_param_named(max_lun, max_lun, uint, S_IRUGO);
75MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
76 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
77module_param_named(max_targets, max_targets, uint, S_IRUGO);
78MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
79 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
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];
169 entry->scsi_id = vfc_cmd->tgt_scsi_id;
170 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
171 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
172 entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
173 break;
174 case IBMVFC_MAD_FORMAT:
175 entry->op_code = mad->opcode;
176 break;
177 default:
178 break;
179 };
180}
181
182/**
183 * ibmvfc_trc_end - Log an end trace entry
184 * @evt: ibmvfc event struct
185 *
186 **/
187static void ibmvfc_trc_end(struct ibmvfc_event *evt)
188{
189 struct ibmvfc_host *vhost = evt->vhost;
190 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
191 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
192 struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
193
194 entry->evt = evt;
195 entry->time = jiffies;
196 entry->fmt = evt->crq.format;
197 entry->type = IBMVFC_TRC_END;
198
199 switch (entry->fmt) {
200 case IBMVFC_CMD_FORMAT:
201 entry->op_code = vfc_cmd->iu.cdb[0];
202 entry->scsi_id = vfc_cmd->tgt_scsi_id;
203 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
204 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
205 entry->u.end.status = vfc_cmd->status;
206 entry->u.end.error = vfc_cmd->error;
207 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
208 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
209 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
210 break;
211 case IBMVFC_MAD_FORMAT:
212 entry->op_code = mad->opcode;
213 entry->u.end.status = mad->status;
214 break;
215 default:
216 break;
217
218 };
219}
220
221#else
222#define ibmvfc_trc_start(evt) do { } while (0)
223#define ibmvfc_trc_end(evt) do { } while (0)
224#endif
225
226/**
227 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
228 * @status: status / error class
229 * @error: error
230 *
231 * Return value:
232 * index into cmd_status / -EINVAL on failure
233 **/
234static int ibmvfc_get_err_index(u16 status, u16 error)
235{
236 int i;
237
238 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
239 if ((cmd_status[i].status & status) == cmd_status[i].status &&
240 cmd_status[i].error == error)
241 return i;
242
243 return -EINVAL;
244}
245
246/**
247 * ibmvfc_get_cmd_error - Find the error description for the fcp response
248 * @status: status / error class
249 * @error: error
250 *
251 * Return value:
252 * error description string
253 **/
254static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
255{
256 int rc = ibmvfc_get_err_index(status, error);
257 if (rc >= 0)
258 return cmd_status[rc].name;
259 return unknown_error;
260}
261
262/**
263 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
264 * @vfc_cmd: ibmvfc command struct
265 *
266 * Return value:
267 * SCSI result value to return for completed command
268 **/
269static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
270{
271 int err;
272 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
273 int fc_rsp_len = rsp->fcp_rsp_len;
274
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
Brian King072b91f2008-07-01 13:14:30 -0500280 err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
281 if (err >= 0)
282 return rsp->scsi_status | (cmd_status[err].result << 16);
283 return rsp->scsi_status | (DID_ERROR << 16);
284}
285
286/**
287 * ibmvfc_retry_cmd - Determine if error status is retryable
288 * @status: status / error class
289 * @error: error
290 *
291 * Return value:
292 * 1 if error should be retried / 0 if it should not
293 **/
294static int ibmvfc_retry_cmd(u16 status, u16 error)
295{
296 int rc = ibmvfc_get_err_index(status, error);
297
298 if (rc >= 0)
299 return cmd_status[rc].retry;
300 return 1;
301}
302
303static const char *unknown_fc_explain = "unknown fc explain";
304
305static const struct {
306 u16 fc_explain;
307 char *name;
308} ls_explain [] = {
309 { 0x00, "no additional explanation" },
310 { 0x01, "service parameter error - options" },
311 { 0x03, "service parameter error - initiator control" },
312 { 0x05, "service parameter error - recipient control" },
313 { 0x07, "service parameter error - received data field size" },
314 { 0x09, "service parameter error - concurrent seq" },
315 { 0x0B, "service parameter error - credit" },
316 { 0x0D, "invalid N_Port/F_Port_Name" },
317 { 0x0E, "invalid node/Fabric Name" },
318 { 0x0F, "invalid common service parameters" },
319 { 0x11, "invalid association header" },
320 { 0x13, "association header required" },
321 { 0x15, "invalid originator S_ID" },
322 { 0x17, "invalid OX_ID-RX-ID combination" },
323 { 0x19, "command (request) already in progress" },
324 { 0x1E, "N_Port Login requested" },
325 { 0x1F, "Invalid N_Port_ID" },
326};
327
328static const struct {
329 u16 fc_explain;
330 char *name;
331} gs_explain [] = {
332 { 0x00, "no additional explanation" },
333 { 0x01, "port identifier not registered" },
334 { 0x02, "port name not registered" },
335 { 0x03, "node name not registered" },
336 { 0x04, "class of service not registered" },
337 { 0x06, "initial process associator not registered" },
338 { 0x07, "FC-4 TYPEs not registered" },
339 { 0x08, "symbolic port name not registered" },
340 { 0x09, "symbolic node name not registered" },
341 { 0x0A, "port type not registered" },
342 { 0xF0, "authorization exception" },
343 { 0xF1, "authentication exception" },
344 { 0xF2, "data base full" },
345 { 0xF3, "data base empty" },
346 { 0xF4, "processing request" },
347 { 0xF5, "unable to verify connection" },
348 { 0xF6, "devices not in a common zone" },
349};
350
351/**
352 * ibmvfc_get_ls_explain - Return the FC Explain description text
353 * @status: FC Explain status
354 *
355 * Returns:
356 * error string
357 **/
358static const char *ibmvfc_get_ls_explain(u16 status)
359{
360 int i;
361
362 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
363 if (ls_explain[i].fc_explain == status)
364 return ls_explain[i].name;
365
366 return unknown_fc_explain;
367}
368
369/**
370 * ibmvfc_get_gs_explain - Return the FC Explain description text
371 * @status: FC Explain status
372 *
373 * Returns:
374 * error string
375 **/
376static const char *ibmvfc_get_gs_explain(u16 status)
377{
378 int i;
379
380 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
381 if (gs_explain[i].fc_explain == status)
382 return gs_explain[i].name;
383
384 return unknown_fc_explain;
385}
386
387static const struct {
388 enum ibmvfc_fc_type fc_type;
389 char *name;
390} fc_type [] = {
391 { IBMVFC_FABRIC_REJECT, "fabric reject" },
392 { IBMVFC_PORT_REJECT, "port reject" },
393 { IBMVFC_LS_REJECT, "ELS reject" },
394 { IBMVFC_FABRIC_BUSY, "fabric busy" },
395 { IBMVFC_PORT_BUSY, "port busy" },
396 { IBMVFC_BASIC_REJECT, "basic reject" },
397};
398
399static const char *unknown_fc_type = "unknown fc type";
400
401/**
402 * ibmvfc_get_fc_type - Return the FC Type description text
403 * @status: FC Type error status
404 *
405 * Returns:
406 * error string
407 **/
408static const char *ibmvfc_get_fc_type(u16 status)
409{
410 int i;
411
412 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
413 if (fc_type[i].fc_type == status)
414 return fc_type[i].name;
415
416 return unknown_fc_type;
417}
418
419/**
420 * ibmvfc_set_tgt_action - Set the next init action for the target
421 * @tgt: ibmvfc target struct
422 * @action: action to perform
423 *
424 **/
425static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
426 enum ibmvfc_target_action action)
427{
428 switch (tgt->action) {
429 case IBMVFC_TGT_ACTION_DEL_RPORT:
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
810 evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
811
812 list_del(&evt->queue);
813 del_timer(&evt->timer);
814 ibmvfc_trc_end(evt);
815 evt->done(evt);
816}
817
818/**
819 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
820 * @vhost: ibmvfc host struct
821 * @error_code: error code to fail requests with
822 *
823 * Return value:
824 * none
825 **/
826static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
827{
828 struct ibmvfc_event *evt, *pos;
829
830 ibmvfc_dbg(vhost, "Purging all requests\n");
831 list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
832 ibmvfc_fail_request(evt, error_code);
833}
834
835/**
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) {
958 switch (vhost->login_buf->resp.link_speed / 100) {
959 case 1:
960 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
961 break;
962 case 2:
963 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
964 break;
965 case 4:
966 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
967 break;
968 case 8:
969 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
970 break;
971 case 10:
972 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
973 break;
974 case 16:
975 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
976 break;
977 default:
Stephen Rothwell775a42e2009-01-06 14:59:00 +0000978 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
Brian King072b91f2008-07-01 13:14:30 -0500979 vhost->login_buf->resp.link_speed / 100);
980 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
981 break;
982 }
983 } else
984 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
985 spin_unlock_irqrestore(shost->host_lock, flags);
986}
987
988/**
989 * ibmvfc_get_host_port_state - Get host port state
990 * @shost: scsi host struct
991 *
992 * Return value:
993 * none
994 **/
995static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
996{
997 struct ibmvfc_host *vhost = shost_priv(shost);
998 unsigned long flags;
999
1000 spin_lock_irqsave(shost->host_lock, flags);
1001 switch (vhost->state) {
1002 case IBMVFC_INITIALIZING:
1003 case IBMVFC_ACTIVE:
1004 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1005 break;
1006 case IBMVFC_LINK_DOWN:
1007 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1008 break;
1009 case IBMVFC_LINK_DEAD:
1010 case IBMVFC_HOST_OFFLINE:
1011 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1012 break;
1013 case IBMVFC_HALTED:
1014 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1015 break;
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
1174 login_info->ostype = IBMVFC_OS_LINUX;
1175 login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
1176 login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
1177 login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
1178 login_info->partition_num = vhost->partition_number;
1179 login_info->vfc_frame_version = 1;
1180 login_info->fcp_version = 3;
Brian King497f9c52009-05-28 16:17:30 -05001181 login_info->flags = IBMVFC_FLUSH_ON_HALT;
Brian King072b91f2008-07-01 13:14:30 -05001182 if (vhost->client_migrated)
Brian King497f9c52009-05-28 16:17:30 -05001183 login_info->flags |= IBMVFC_CLIENT_MIGRATED;
Brian King072b91f2008-07-01 13:14:30 -05001184
1185 login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1186 login_info->capabilities = IBMVFC_CAN_MIGRATE;
1187 login_info->async.va = vhost->async_crq.msg_token;
Brian King52d7e862008-07-22 08:31:46 -05001188 login_info->async.len = 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;
1228 evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
1229 evt->xfer_iu = pool->iu_storage + i;
1230 evt->vhost = vhost;
1231 evt->ext_list = NULL;
1232 list_add_tail(&evt->queue, &vhost->free);
1233 }
1234
1235 LEAVE;
1236 return 0;
1237}
1238
1239/**
1240 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1241 * @vhost: ibmvfc host who owns the event pool
1242 *
1243 **/
1244static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1245{
1246 int i;
1247 struct ibmvfc_event_pool *pool = &vhost->pool;
1248
1249 ENTER;
1250 for (i = 0; i < pool->size; ++i) {
1251 list_del(&pool->events[i].queue);
1252 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1253 if (pool->events[i].ext_list)
1254 dma_pool_free(vhost->sg_pool,
1255 pool->events[i].ext_list,
1256 pool->events[i].ext_list_token);
1257 }
1258
1259 kfree(pool->events);
1260 dma_free_coherent(vhost->dev,
1261 pool->size * sizeof(*pool->iu_storage),
1262 pool->iu_storage, pool->iu_token);
1263 LEAVE;
1264}
1265
1266/**
1267 * ibmvfc_get_event - Gets the next free event in pool
1268 * @vhost: ibmvfc host struct
1269 *
1270 * Returns a free event from the pool.
1271 **/
1272static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1273{
1274 struct ibmvfc_event *evt;
1275
1276 BUG_ON(list_empty(&vhost->free));
1277 evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1278 atomic_set(&evt->free, 0);
1279 list_del(&evt->queue);
1280 return evt;
1281}
1282
1283/**
1284 * ibmvfc_init_event - Initialize fields in an event struct that are always
1285 * required.
1286 * @evt: The event
1287 * @done: Routine to call when the event is responded to
1288 * @format: SRP or MAD format
1289 **/
1290static void ibmvfc_init_event(struct ibmvfc_event *evt,
1291 void (*done) (struct ibmvfc_event *), u8 format)
1292{
1293 evt->cmnd = NULL;
1294 evt->sync_iu = NULL;
1295 evt->crq.format = format;
1296 evt->done = done;
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) {
1313 md[i].va = sg_dma_address(sg);
1314 md[i].len = sg_dma_len(sg);
1315 md[i].key = 0;
1316 }
1317}
1318
1319/**
1320 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1321 * @scmd: Scsi_Cmnd with the scatterlist
1322 * @evt: ibmvfc event struct
1323 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1324 * @dev: device for which to map dma memory
1325 *
1326 * Returns:
1327 * 0 on success / non-zero on failure
1328 **/
1329static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1330 struct ibmvfc_event *evt,
1331 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1332{
1333
1334 int sg_mapped;
1335 struct srp_direct_buf *data = &vfc_cmd->ioba;
1336 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1337
1338 sg_mapped = scsi_dma_map(scmd);
1339 if (!sg_mapped) {
1340 vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
1341 return 0;
1342 } else if (unlikely(sg_mapped < 0)) {
1343 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1344 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1345 return sg_mapped;
1346 }
1347
1348 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1349 vfc_cmd->flags |= IBMVFC_WRITE;
1350 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1351 } else {
1352 vfc_cmd->flags |= IBMVFC_READ;
1353 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1354 }
1355
1356 if (sg_mapped == 1) {
1357 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1358 return 0;
1359 }
1360
1361 vfc_cmd->flags |= IBMVFC_SCATTERLIST;
1362
1363 if (!evt->ext_list) {
1364 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1365 &evt->ext_list_token);
1366
1367 if (!evt->ext_list) {
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
1377 data->va = evt->ext_list_token;
1378 data->len = sg_mapped * sizeof(struct srp_direct_buf);
1379 data->key = 0;
1380 return 0;
1381}
1382
1383/**
1384 * ibmvfc_timeout - Internal command timeout handler
1385 * @evt: struct ibmvfc_event that timed out
1386 *
1387 * Called when an internally generated command times out
1388 **/
1389static void ibmvfc_timeout(struct ibmvfc_event *evt)
1390{
1391 struct ibmvfc_host *vhost = evt->vhost;
1392 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1393 ibmvfc_reset_host(vhost);
1394}
1395
1396/**
1397 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1398 * @evt: event to be sent
1399 * @vhost: ibmvfc host struct
1400 * @timeout: timeout in seconds - 0 means do not time command
1401 *
1402 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1403 **/
1404static int ibmvfc_send_event(struct ibmvfc_event *evt,
1405 struct ibmvfc_host *vhost, unsigned long timeout)
1406{
1407 u64 *crq_as_u64 = (u64 *) &evt->crq;
1408 int rc;
1409
1410 /* Copy the IU into the transfer area */
1411 *evt->xfer_iu = evt->iu;
1412 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1413 evt->xfer_iu->cmd.tag = (u64)evt;
1414 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1415 evt->xfer_iu->mad_common.tag = (u64)evt;
1416 else
1417 BUG();
1418
1419 list_add_tail(&evt->queue, &vhost->sent);
1420 init_timer(&evt->timer);
1421
1422 if (timeout) {
1423 evt->timer.data = (unsigned long) evt;
1424 evt->timer.expires = jiffies + (timeout * HZ);
1425 evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1426 add_timer(&evt->timer);
1427 }
1428
Brian Kinga528ab72008-12-03 11:02:56 -06001429 mb();
1430
Brian King072b91f2008-07-01 13:14:30 -05001431 if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
1432 list_del(&evt->queue);
1433 del_timer(&evt->timer);
1434
1435 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1436 * Firmware will send a CRQ with a transport event (0xFF) to
1437 * tell this client what has happened to the transport. This
1438 * will be handled in ibmvfc_handle_crq()
1439 */
1440 if (rc == H_CLOSED) {
1441 if (printk_ratelimit())
1442 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1443 if (evt->cmnd)
1444 scsi_dma_unmap(evt->cmnd);
1445 ibmvfc_free_event(evt);
1446 return SCSI_MLQUEUE_HOST_BUSY;
1447 }
1448
1449 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1450 if (evt->cmnd) {
1451 evt->cmnd->result = DID_ERROR << 16;
1452 evt->done = ibmvfc_scsi_eh_done;
1453 } else
1454 evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
1455
1456 evt->done(evt);
1457 } else
1458 ibmvfc_trc_start(evt);
1459
1460 return 0;
1461}
1462
1463/**
1464 * ibmvfc_log_error - Log an error for the failed command if appropriate
1465 * @evt: ibmvfc event to log
1466 *
1467 **/
1468static void ibmvfc_log_error(struct ibmvfc_event *evt)
1469{
1470 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1471 struct ibmvfc_host *vhost = evt->vhost;
1472 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1473 struct scsi_cmnd *cmnd = evt->cmnd;
1474 const char *err = unknown_error;
1475 int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
1476 int logerr = 0;
1477 int rsp_code = 0;
1478
1479 if (index >= 0) {
1480 logerr = cmd_status[index].log;
1481 err = cmd_status[index].name;
1482 }
1483
Brian King0ae808e2008-07-22 08:31:39 -05001484 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
Brian King072b91f2008-07-01 13:14:30 -05001485 return;
1486
1487 if (rsp->flags & FCP_RSP_LEN_VALID)
1488 rsp_code = rsp->data.info.rsp_code;
1489
1490 scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1491 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1492 cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1493 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1494}
1495
1496/**
Brian King6d29cc52009-05-28 16:17:33 -05001497 * ibmvfc_relogin - Log back into the specified device
1498 * @sdev: scsi device struct
1499 *
1500 **/
1501static void ibmvfc_relogin(struct scsi_device *sdev)
1502{
1503 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1504 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1505 struct ibmvfc_target *tgt;
1506
1507 list_for_each_entry(tgt, &vhost->targets, queue) {
1508 if (rport == tgt->rport) {
1509 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
1510 break;
1511 }
1512 }
1513
1514 ibmvfc_reinit_host(vhost);
1515}
1516
1517/**
Brian King072b91f2008-07-01 13:14:30 -05001518 * ibmvfc_scsi_done - Handle responses from commands
1519 * @evt: ibmvfc event to be handled
1520 *
1521 * Used as a callback when sending scsi cmds.
1522 **/
1523static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1524{
1525 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1526 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1527 struct scsi_cmnd *cmnd = evt->cmnd;
Brian King2bac4062008-08-15 10:59:26 -05001528 u32 rsp_len = 0;
1529 u32 sense_len = rsp->fcp_sense_len;
Brian King072b91f2008-07-01 13:14:30 -05001530
1531 if (cmnd) {
1532 if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
1533 scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
1534 else if (rsp->flags & FCP_RESID_UNDER)
1535 scsi_set_resid(cmnd, rsp->fcp_resid);
1536 else
1537 scsi_set_resid(cmnd, 0);
1538
1539 if (vfc_cmd->status) {
1540 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1541
1542 if (rsp->flags & FCP_RSP_LEN_VALID)
1543 rsp_len = rsp->fcp_rsp_len;
1544 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1545 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
Brian King2bac4062008-08-15 10:59:26 -05001546 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
Brian King072b91f2008-07-01 13:14:30 -05001547 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
Brian King646d3852008-11-14 13:33:53 -06001548 if ((vfc_cmd->status & IBMVFC_VIOS_FAILURE) && (vfc_cmd->error == IBMVFC_PLOGI_REQUIRED))
Brian King6d29cc52009-05-28 16:17:33 -05001549 ibmvfc_relogin(cmnd->device);
Brian King072b91f2008-07-01 13:14:30 -05001550
Brian King50119da2008-10-29 08:46:36 -05001551 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1552 cmnd->result = (DID_ERROR << 16);
1553
Brian King072b91f2008-07-01 13:14:30 -05001554 ibmvfc_log_error(evt);
1555 }
1556
1557 if (!cmnd->result &&
1558 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1559 cmnd->result = (DID_ERROR << 16);
1560
1561 scsi_dma_unmap(cmnd);
1562 cmnd->scsi_done(cmnd);
1563 }
1564
Brian Kingad8dcff2008-10-29 08:46:41 -05001565 if (evt->eh_comp)
1566 complete(evt->eh_comp);
1567
Brian King072b91f2008-07-01 13:14:30 -05001568 ibmvfc_free_event(evt);
1569}
1570
1571/**
1572 * ibmvfc_host_chkready - Check if the host can accept commands
1573 * @vhost: struct ibmvfc host
1574 *
1575 * Returns:
1576 * 1 if host can accept command / 0 if not
1577 **/
1578static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1579{
1580 int result = 0;
1581
1582 switch (vhost->state) {
1583 case IBMVFC_LINK_DEAD:
1584 case IBMVFC_HOST_OFFLINE:
1585 result = DID_NO_CONNECT << 16;
1586 break;
1587 case IBMVFC_NO_CRQ:
1588 case IBMVFC_INITIALIZING:
1589 case IBMVFC_HALTED:
1590 case IBMVFC_LINK_DOWN:
1591 result = DID_REQUEUE << 16;
1592 break;
1593 case IBMVFC_ACTIVE:
1594 result = 0;
1595 break;
1596 };
1597
1598 return result;
1599}
1600
1601/**
1602 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1603 * @cmnd: struct scsi_cmnd to be executed
1604 * @done: Callback function to be called when cmnd is completed
1605 *
1606 * Returns:
1607 * 0 on success / other on failure
1608 **/
Jeff Garzikf2812332010-11-16 02:10:29 -05001609static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
Brian King072b91f2008-07-01 13:14:30 -05001610 void (*done) (struct scsi_cmnd *))
1611{
1612 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1613 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1614 struct ibmvfc_cmd *vfc_cmd;
1615 struct ibmvfc_event *evt;
1616 u8 tag[2];
1617 int rc;
1618
1619 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1620 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1621 cmnd->result = rc;
1622 done(cmnd);
1623 return 0;
1624 }
1625
1626 cmnd->result = (DID_OK << 16);
1627 evt = ibmvfc_get_event(vhost);
1628 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1629 evt->cmnd = cmnd;
1630 cmnd->scsi_done = done;
1631 vfc_cmd = &evt->iu.cmd;
1632 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1633 vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1634 vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
1635 vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
1636 vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
1637 vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
1638 vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
1639 vfc_cmd->tgt_scsi_id = rport->port_id;
Brian King072b91f2008-07-01 13:14:30 -05001640 vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
1641 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1642 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1643
1644 if (scsi_populate_tag_msg(cmnd, tag)) {
1645 vfc_cmd->task_tag = tag[1];
1646 switch (tag[0]) {
1647 case MSG_SIMPLE_TAG:
1648 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1649 break;
1650 case MSG_HEAD_TAG:
1651 vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
1652 break;
1653 case MSG_ORDERED_TAG:
1654 vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
1655 break;
1656 };
1657 }
1658
1659 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1660 return ibmvfc_send_event(evt, vhost, 0);
1661
1662 ibmvfc_free_event(evt);
1663 if (rc == -ENOMEM)
1664 return SCSI_MLQUEUE_HOST_BUSY;
1665
1666 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1667 scmd_printk(KERN_ERR, cmnd,
1668 "Failed to map DMA buffer for command. rc=%d\n", rc);
1669
1670 cmnd->result = DID_ERROR << 16;
1671 done(cmnd);
1672 return 0;
1673}
1674
Jeff Garzikf2812332010-11-16 02:10:29 -05001675static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1676
Brian King072b91f2008-07-01 13:14:30 -05001677/**
1678 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1679 * @evt: ibmvfc event struct
1680 *
1681 **/
1682static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1683{
1684 /* copy the response back */
1685 if (evt->sync_iu)
1686 *evt->sync_iu = *evt->xfer_iu;
1687
1688 complete(&evt->comp);
1689}
1690
1691/**
Brian Kingd31429e2009-10-19 15:07:54 -05001692 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1693 * @evt: struct ibmvfc_event
1694 *
1695 **/
1696static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1697{
1698 struct ibmvfc_host *vhost = evt->vhost;
1699
1700 ibmvfc_free_event(evt);
1701 vhost->aborting_passthru = 0;
1702 dev_info(vhost->dev, "Passthru command cancelled\n");
1703}
1704
1705/**
1706 * ibmvfc_bsg_timeout - Handle a BSG timeout
1707 * @job: struct fc_bsg_job that timed out
1708 *
1709 * Returns:
1710 * 0 on success / other on failure
1711 **/
1712static int ibmvfc_bsg_timeout(struct fc_bsg_job *job)
1713{
1714 struct ibmvfc_host *vhost = shost_priv(job->shost);
1715 unsigned long port_id = (unsigned long)job->dd_data;
1716 struct ibmvfc_event *evt;
1717 struct ibmvfc_tmf *tmf;
1718 unsigned long flags;
1719 int rc;
1720
1721 ENTER;
1722 spin_lock_irqsave(vhost->host->host_lock, flags);
1723 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
1724 __ibmvfc_reset_host(vhost);
1725 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1726 return 0;
1727 }
1728
1729 vhost->aborting_passthru = 1;
1730 evt = ibmvfc_get_event(vhost);
1731 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
1732
1733 tmf = &evt->iu.tmf;
1734 memset(tmf, 0, sizeof(*tmf));
1735 tmf->common.version = 1;
1736 tmf->common.opcode = IBMVFC_TMF_MAD;
1737 tmf->common.length = sizeof(*tmf);
1738 tmf->scsi_id = port_id;
1739 tmf->cancel_key = IBMVFC_PASSTHRU_CANCEL_KEY;
1740 tmf->my_cancel_key = IBMVFC_INTERNAL_CANCEL_KEY;
1741 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1742
1743 if (rc != 0) {
1744 vhost->aborting_passthru = 0;
1745 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
1746 rc = -EIO;
1747 } else
1748 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
1749 port_id);
1750
1751 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1752
1753 LEAVE;
1754 return rc;
1755}
1756
1757/**
1758 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1759 * @vhost: struct ibmvfc_host to send command
1760 * @port_id: port ID to send command
1761 *
1762 * Returns:
1763 * 0 on success / other on failure
1764 **/
1765static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
1766{
1767 struct ibmvfc_port_login *plogi;
1768 struct ibmvfc_target *tgt;
1769 struct ibmvfc_event *evt;
1770 union ibmvfc_iu rsp_iu;
1771 unsigned long flags;
1772 int rc = 0, issue_login = 1;
1773
1774 ENTER;
1775 spin_lock_irqsave(vhost->host->host_lock, flags);
1776 list_for_each_entry(tgt, &vhost->targets, queue) {
1777 if (tgt->scsi_id == port_id) {
1778 issue_login = 0;
1779 break;
1780 }
1781 }
1782
1783 if (!issue_login)
1784 goto unlock_out;
1785 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
1786 goto unlock_out;
1787
1788 evt = ibmvfc_get_event(vhost);
1789 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1790 plogi = &evt->iu.plogi;
1791 memset(plogi, 0, sizeof(*plogi));
1792 plogi->common.version = 1;
1793 plogi->common.opcode = IBMVFC_PORT_LOGIN;
1794 plogi->common.length = sizeof(*plogi);
1795 plogi->scsi_id = port_id;
1796 evt->sync_iu = &rsp_iu;
1797 init_completion(&evt->comp);
1798
1799 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1800 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1801
1802 if (rc)
1803 return -EIO;
1804
1805 wait_for_completion(&evt->comp);
1806
1807 if (rsp_iu.plogi.common.status)
1808 rc = -EIO;
1809
1810 spin_lock_irqsave(vhost->host->host_lock, flags);
1811 ibmvfc_free_event(evt);
1812unlock_out:
1813 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1814 LEAVE;
1815 return rc;
1816}
1817
1818/**
1819 * ibmvfc_bsg_request - Handle a BSG request
1820 * @job: struct fc_bsg_job to be executed
1821 *
1822 * Returns:
1823 * 0 on success / other on failure
1824 **/
1825static int ibmvfc_bsg_request(struct fc_bsg_job *job)
1826{
1827 struct ibmvfc_host *vhost = shost_priv(job->shost);
1828 struct fc_rport *rport = job->rport;
1829 struct ibmvfc_passthru_mad *mad;
1830 struct ibmvfc_event *evt;
1831 union ibmvfc_iu rsp_iu;
1832 unsigned long flags, port_id = -1;
1833 unsigned int code = job->request->msgcode;
1834 int rc = 0, req_seg, rsp_seg, issue_login = 0;
1835 u32 fc_flags, rsp_len;
1836
1837 ENTER;
1838 job->reply->reply_payload_rcv_len = 0;
1839 if (rport)
1840 port_id = rport->port_id;
1841
1842 switch (code) {
1843 case FC_BSG_HST_ELS_NOLOGIN:
1844 port_id = (job->request->rqst_data.h_els.port_id[0] << 16) |
1845 (job->request->rqst_data.h_els.port_id[1] << 8) |
1846 job->request->rqst_data.h_els.port_id[2];
1847 case FC_BSG_RPT_ELS:
1848 fc_flags = IBMVFC_FC_ELS;
1849 break;
1850 case FC_BSG_HST_CT:
1851 issue_login = 1;
1852 port_id = (job->request->rqst_data.h_ct.port_id[0] << 16) |
1853 (job->request->rqst_data.h_ct.port_id[1] << 8) |
1854 job->request->rqst_data.h_ct.port_id[2];
1855 case FC_BSG_RPT_CT:
1856 fc_flags = IBMVFC_FC_CT_IU;
1857 break;
1858 default:
1859 return -ENOTSUPP;
1860 };
1861
1862 if (port_id == -1)
1863 return -EINVAL;
1864 if (!mutex_trylock(&vhost->passthru_mutex))
1865 return -EBUSY;
1866
1867 job->dd_data = (void *)port_id;
1868 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
1869 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1870
1871 if (!req_seg) {
1872 mutex_unlock(&vhost->passthru_mutex);
1873 return -ENOMEM;
1874 }
1875
1876 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
1877 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1878
1879 if (!rsp_seg) {
1880 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1881 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1882 mutex_unlock(&vhost->passthru_mutex);
1883 return -ENOMEM;
1884 }
1885
1886 if (req_seg > 1 || rsp_seg > 1) {
1887 rc = -EINVAL;
1888 goto out;
1889 }
1890
1891 if (issue_login)
1892 rc = ibmvfc_bsg_plogi(vhost, port_id);
1893
1894 spin_lock_irqsave(vhost->host->host_lock, flags);
1895
1896 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
1897 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1898 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1899 goto out;
1900 }
1901
1902 evt = ibmvfc_get_event(vhost);
1903 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1904 mad = &evt->iu.passthru;
1905
1906 memset(mad, 0, sizeof(*mad));
1907 mad->common.version = 1;
1908 mad->common.opcode = IBMVFC_PASSTHRU;
1909 mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
1910
1911 mad->cmd_ioba.va = (u64)evt->crq.ioba +
1912 offsetof(struct ibmvfc_passthru_mad, iu);
1913 mad->cmd_ioba.len = sizeof(mad->iu);
1914
1915 mad->iu.cmd_len = job->request_payload.payload_len;
1916 mad->iu.rsp_len = job->reply_payload.payload_len;
1917 mad->iu.flags = fc_flags;
1918 mad->iu.cancel_key = IBMVFC_PASSTHRU_CANCEL_KEY;
1919
1920 mad->iu.cmd.va = sg_dma_address(job->request_payload.sg_list);
1921 mad->iu.cmd.len = sg_dma_len(job->request_payload.sg_list);
1922 mad->iu.rsp.va = sg_dma_address(job->reply_payload.sg_list);
1923 mad->iu.rsp.len = sg_dma_len(job->reply_payload.sg_list);
1924 mad->iu.scsi_id = port_id;
1925 mad->iu.tag = (u64)evt;
1926 rsp_len = mad->iu.rsp.len;
1927
1928 evt->sync_iu = &rsp_iu;
1929 init_completion(&evt->comp);
1930 rc = ibmvfc_send_event(evt, vhost, 0);
1931 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1932
1933 if (rc) {
1934 rc = -EIO;
1935 goto out;
1936 }
1937
1938 wait_for_completion(&evt->comp);
1939
1940 if (rsp_iu.passthru.common.status)
1941 rc = -EIO;
1942 else
1943 job->reply->reply_payload_rcv_len = rsp_len;
1944
1945 spin_lock_irqsave(vhost->host->host_lock, flags);
1946 ibmvfc_free_event(evt);
1947 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1948 job->reply->result = rc;
1949 job->job_done(job);
1950 rc = 0;
1951out:
1952 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1953 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1954 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
1955 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1956 mutex_unlock(&vhost->passthru_mutex);
1957 LEAVE;
1958 return rc;
1959}
1960
1961/**
Brian King072b91f2008-07-01 13:14:30 -05001962 * ibmvfc_reset_device - Reset the device with the specified reset type
1963 * @sdev: scsi device to reset
1964 * @type: reset type
1965 * @desc: reset type description for log messages
1966 *
1967 * Returns:
1968 * 0 on success / other on failure
1969 **/
1970static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1971{
1972 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1973 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1974 struct ibmvfc_cmd *tmf;
Brian King50ed9a02008-10-29 08:46:47 -05001975 struct ibmvfc_event *evt = NULL;
Brian King072b91f2008-07-01 13:14:30 -05001976 union ibmvfc_iu rsp_iu;
1977 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1978 int rsp_rc = -EBUSY;
1979 unsigned long flags;
1980 int rsp_code = 0;
1981
1982 spin_lock_irqsave(vhost->host->host_lock, flags);
1983 if (vhost->state == IBMVFC_ACTIVE) {
1984 evt = ibmvfc_get_event(vhost);
1985 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1986
1987 tmf = &evt->iu.cmd;
1988 memset(tmf, 0, sizeof(*tmf));
1989 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1990 tmf->resp.len = sizeof(tmf->rsp);
1991 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1992 tmf->payload_len = sizeof(tmf->iu);
1993 tmf->resp_len = sizeof(tmf->rsp);
1994 tmf->cancel_key = (unsigned long)sdev->hostdata;
1995 tmf->tgt_scsi_id = rport->port_id;
1996 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1997 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1998 tmf->iu.tmf_flags = type;
1999 evt->sync_iu = &rsp_iu;
2000
2001 init_completion(&evt->comp);
2002 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2003 }
2004 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2005
2006 if (rsp_rc != 0) {
2007 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2008 desc, rsp_rc);
2009 return -EIO;
2010 }
2011
2012 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2013 wait_for_completion(&evt->comp);
2014
Brian King230934a2009-10-19 15:07:47 -05002015 if (rsp_iu.cmd.status)
2016 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2017
2018 if (rsp_code) {
Brian King072b91f2008-07-01 13:14:30 -05002019 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2020 rsp_code = fc_rsp->data.info.rsp_code;
2021
2022 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2023 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2024 desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
2025 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
2026 fc_rsp->scsi_status);
2027 rsp_rc = -EIO;
2028 } else
2029 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2030
2031 spin_lock_irqsave(vhost->host->host_lock, flags);
2032 ibmvfc_free_event(evt);
2033 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2034 return rsp_rc;
2035}
2036
2037/**
Brian Kingd2fab5c2010-08-05 16:38:34 -05002038 * ibmvfc_match_rport - Match function for specified remote port
2039 * @evt: ibmvfc event struct
2040 * @device: device to match (rport)
Brian King072b91f2008-07-01 13:14:30 -05002041 *
2042 * Returns:
Brian Kingd2fab5c2010-08-05 16:38:34 -05002043 * 1 if event matches rport / 0 if event does not match rport
Brian King072b91f2008-07-01 13:14:30 -05002044 **/
Brian Kingd2fab5c2010-08-05 16:38:34 -05002045static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
Brian King072b91f2008-07-01 13:14:30 -05002046{
Brian Kingd2fab5c2010-08-05 16:38:34 -05002047 struct fc_rport *cmd_rport;
Brian King072b91f2008-07-01 13:14:30 -05002048
Brian Kingd2fab5c2010-08-05 16:38:34 -05002049 if (evt->cmnd) {
2050 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2051 if (cmd_rport == rport)
2052 return 1;
Brian King072b91f2008-07-01 13:14:30 -05002053 }
Brian King072b91f2008-07-01 13:14:30 -05002054 return 0;
2055}
2056
2057/**
Brian Kingad8dcff2008-10-29 08:46:41 -05002058 * ibmvfc_match_target - Match function for specified target
2059 * @evt: ibmvfc event struct
2060 * @device: device to match (starget)
2061 *
2062 * Returns:
2063 * 1 if event matches starget / 0 if event does not match starget
2064 **/
2065static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2066{
2067 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2068 return 1;
2069 return 0;
2070}
2071
2072/**
2073 * ibmvfc_match_lun - Match function for specified LUN
2074 * @evt: ibmvfc event struct
2075 * @device: device to match (sdev)
2076 *
2077 * Returns:
2078 * 1 if event matches sdev / 0 if event does not match sdev
2079 **/
2080static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2081{
2082 if (evt->cmnd && evt->cmnd->device == device)
2083 return 1;
2084 return 0;
2085}
2086
2087/**
2088 * ibmvfc_wait_for_ops - Wait for ops to complete
2089 * @vhost: ibmvfc host struct
2090 * @device: device to match (starget or sdev)
2091 * @match: match function
2092 *
2093 * Returns:
2094 * SUCCESS / FAILED
2095 **/
2096static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2097 int (*match) (struct ibmvfc_event *, void *))
2098{
2099 struct ibmvfc_event *evt;
2100 DECLARE_COMPLETION_ONSTACK(comp);
2101 int wait;
2102 unsigned long flags;
Brian Kingdaa142d2010-04-20 14:21:35 -05002103 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
Brian Kingad8dcff2008-10-29 08:46:41 -05002104
2105 ENTER;
2106 do {
2107 wait = 0;
2108 spin_lock_irqsave(vhost->host->host_lock, flags);
2109 list_for_each_entry(evt, &vhost->sent, queue) {
2110 if (match(evt, device)) {
2111 evt->eh_comp = &comp;
2112 wait++;
2113 }
2114 }
2115 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2116
2117 if (wait) {
2118 timeout = wait_for_completion_timeout(&comp, timeout);
2119
2120 if (!timeout) {
2121 wait = 0;
2122 spin_lock_irqsave(vhost->host->host_lock, flags);
2123 list_for_each_entry(evt, &vhost->sent, queue) {
2124 if (match(evt, device)) {
2125 evt->eh_comp = NULL;
2126 wait++;
2127 }
2128 }
2129 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2130 if (wait)
2131 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2132 LEAVE;
2133 return wait ? FAILED : SUCCESS;
2134 }
2135 }
2136 } while (wait);
2137
2138 LEAVE;
2139 return SUCCESS;
2140}
2141
2142/**
Brian Kingd2fab5c2010-08-05 16:38:34 -05002143 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2144 * @sdev: scsi device to cancel commands
2145 * @type: type of error recovery being performed
2146 *
2147 * This sends a cancel to the VIOS for the specified device. This does
2148 * NOT send any abort to the actual device. That must be done separately.
2149 *
2150 * Returns:
2151 * 0 on success / other on failure
2152 **/
2153static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2154{
2155 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2156 struct scsi_target *starget = scsi_target(sdev);
2157 struct fc_rport *rport = starget_to_rport(starget);
2158 struct ibmvfc_tmf *tmf;
2159 struct ibmvfc_event *evt, *found_evt;
2160 union ibmvfc_iu rsp;
2161 int rsp_rc = -EBUSY;
2162 unsigned long flags;
2163 u16 status;
2164
2165 ENTER;
2166 spin_lock_irqsave(vhost->host->host_lock, flags);
2167 found_evt = NULL;
2168 list_for_each_entry(evt, &vhost->sent, queue) {
2169 if (evt->cmnd && evt->cmnd->device == sdev) {
2170 found_evt = evt;
2171 break;
2172 }
2173 }
2174
2175 if (!found_evt) {
2176 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2177 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2178 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2179 return 0;
2180 }
2181
2182 if (vhost->state == IBMVFC_ACTIVE) {
2183 evt = ibmvfc_get_event(vhost);
2184 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2185
2186 tmf = &evt->iu.tmf;
2187 memset(tmf, 0, sizeof(*tmf));
2188 tmf->common.version = 1;
2189 tmf->common.opcode = IBMVFC_TMF_MAD;
2190 tmf->common.length = sizeof(*tmf);
2191 tmf->scsi_id = rport->port_id;
2192 int_to_scsilun(sdev->lun, &tmf->lun);
2193 tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
2194 tmf->cancel_key = (unsigned long)sdev->hostdata;
2195 tmf->my_cancel_key = (unsigned long)starget->hostdata;
2196
2197 evt->sync_iu = &rsp;
2198 init_completion(&evt->comp);
2199 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2200 }
2201
2202 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2203
2204 if (rsp_rc != 0) {
2205 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2206 return -EIO;
2207 }
2208
2209 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2210
2211 wait_for_completion(&evt->comp);
2212 status = rsp.mad_common.status;
2213 spin_lock_irqsave(vhost->host->host_lock, flags);
2214 ibmvfc_free_event(evt);
2215 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2216
2217 if (status != IBMVFC_MAD_SUCCESS) {
2218 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2219 return -EIO;
2220 }
2221
2222 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2223 return 0;
2224}
2225
2226/**
2227 * ibmvfc_match_key - Match function for specified cancel key
2228 * @evt: ibmvfc event struct
2229 * @key: cancel key to match
2230 *
2231 * Returns:
2232 * 1 if event matches key / 0 if event does not match key
2233 **/
2234static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2235{
2236 unsigned long cancel_key = (unsigned long)key;
2237
2238 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2239 evt->iu.cmd.cancel_key == cancel_key)
2240 return 1;
2241 return 0;
2242}
2243
2244/**
2245 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2246 * @sdev: scsi device to abort commands
2247 *
2248 * This sends an Abort Task Set to the VIOS for the specified device. This does
2249 * NOT send any cancel to the VIOS. That must be done separately.
2250 *
2251 * Returns:
2252 * 0 on success / other on failure
2253 **/
2254static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2255{
2256 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2257 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2258 struct ibmvfc_cmd *tmf;
2259 struct ibmvfc_event *evt, *found_evt;
2260 union ibmvfc_iu rsp_iu;
2261 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2262 int rc, rsp_rc = -EBUSY;
2263 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2264 int rsp_code = 0;
2265
2266 spin_lock_irqsave(vhost->host->host_lock, flags);
2267 found_evt = NULL;
2268 list_for_each_entry(evt, &vhost->sent, queue) {
2269 if (evt->cmnd && evt->cmnd->device == sdev) {
2270 found_evt = evt;
2271 break;
2272 }
2273 }
2274
2275 if (!found_evt) {
2276 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2277 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2278 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2279 return 0;
2280 }
2281
2282 if (vhost->state == IBMVFC_ACTIVE) {
2283 evt = ibmvfc_get_event(vhost);
2284 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2285
2286 tmf = &evt->iu.cmd;
2287 memset(tmf, 0, sizeof(*tmf));
2288 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
2289 tmf->resp.len = sizeof(tmf->rsp);
2290 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
2291 tmf->payload_len = sizeof(tmf->iu);
2292 tmf->resp_len = sizeof(tmf->rsp);
2293 tmf->cancel_key = (unsigned long)sdev->hostdata;
2294 tmf->tgt_scsi_id = rport->port_id;
2295 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2296 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
2297 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2298 evt->sync_iu = &rsp_iu;
2299
2300 init_completion(&evt->comp);
2301 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2302 }
2303
2304 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2305
2306 if (rsp_rc != 0) {
2307 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2308 return -EIO;
2309 }
2310
2311 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2312 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2313
2314 if (!timeout) {
2315 rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2316 if (!rc) {
2317 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2318 if (rc == SUCCESS)
2319 rc = 0;
2320 }
2321
2322 if (rc) {
2323 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2324 ibmvfc_reset_host(vhost);
2325 rsp_rc = 0;
2326 goto out;
2327 }
2328 }
2329
2330 if (rsp_iu.cmd.status)
2331 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2332
2333 if (rsp_code) {
2334 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2335 rsp_code = fc_rsp->data.info.rsp_code;
2336
2337 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2338 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2339 ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
2340 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
2341 fc_rsp->scsi_status);
2342 rsp_rc = -EIO;
2343 } else
2344 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2345
2346out:
2347 spin_lock_irqsave(vhost->host->host_lock, flags);
2348 ibmvfc_free_event(evt);
2349 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2350 return rsp_rc;
2351}
2352
2353/**
Brian King072b91f2008-07-01 13:14:30 -05002354 * ibmvfc_eh_abort_handler - Abort a command
2355 * @cmd: scsi command to abort
2356 *
2357 * Returns:
2358 * SUCCESS / FAILED
2359 **/
2360static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2361{
Brian Kingad8dcff2008-10-29 08:46:41 -05002362 struct scsi_device *sdev = cmd->device;
2363 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King072b91f2008-07-01 13:14:30 -05002364 int cancel_rc, abort_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002365 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002366
2367 ENTER;
Brian King3f014242010-06-17 13:55:15 -05002368 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002369 ibmvfc_wait_while_resetting(vhost);
Brian Kingad8dcff2008-10-29 08:46:41 -05002370 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2371 abort_rc = ibmvfc_abort_task_set(sdev);
Brian King072b91f2008-07-01 13:14:30 -05002372
Brian Kingad8dcff2008-10-29 08:46:41 -05002373 if (!cancel_rc && !abort_rc)
2374 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05002375
2376 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002377 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002378}
2379
2380/**
2381 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2382 * @cmd: scsi command struct
2383 *
2384 * Returns:
2385 * SUCCESS / FAILED
2386 **/
2387static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2388{
Brian Kingad8dcff2008-10-29 08:46:41 -05002389 struct scsi_device *sdev = cmd->device;
2390 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King072b91f2008-07-01 13:14:30 -05002391 int cancel_rc, reset_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002392 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002393
2394 ENTER;
Brian King3f014242010-06-17 13:55:15 -05002395 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002396 ibmvfc_wait_while_resetting(vhost);
Brian Kingad8dcff2008-10-29 08:46:41 -05002397 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2398 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
Brian King072b91f2008-07-01 13:14:30 -05002399
Brian Kingad8dcff2008-10-29 08:46:41 -05002400 if (!cancel_rc && !reset_rc)
2401 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05002402
2403 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002404 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002405}
2406
2407/**
Brian King4a5c4a52009-10-19 15:07:53 -05002408 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2409 * @sdev: scsi device struct
2410 * @data: return code
2411 *
2412 **/
2413static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
Brian King072b91f2008-07-01 13:14:30 -05002414{
2415 unsigned long *rc = data;
2416 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2417}
2418
2419/**
Brian King072b91f2008-07-01 13:14:30 -05002420 * ibmvfc_eh_target_reset_handler - Reset the target
2421 * @cmd: scsi command struct
2422 *
2423 * Returns:
2424 * SUCCESS / FAILED
2425 **/
2426static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2427{
Brian Kingad8dcff2008-10-29 08:46:41 -05002428 struct scsi_device *sdev = cmd->device;
2429 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2430 struct scsi_target *starget = scsi_target(sdev);
Brian King072b91f2008-07-01 13:14:30 -05002431 int reset_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002432 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002433 unsigned long cancel_rc = 0;
Brian King072b91f2008-07-01 13:14:30 -05002434
2435 ENTER;
Brian King3f014242010-06-17 13:55:15 -05002436 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002437 ibmvfc_wait_while_resetting(vhost);
Brian King4a5c4a52009-10-19 15:07:53 -05002438 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
Brian Kingad8dcff2008-10-29 08:46:41 -05002439 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
Brian King072b91f2008-07-01 13:14:30 -05002440
Brian Kingad8dcff2008-10-29 08:46:41 -05002441 if (!cancel_rc && !reset_rc)
2442 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
Brian King072b91f2008-07-01 13:14:30 -05002443
2444 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002445 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002446}
2447
2448/**
2449 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2450 * @cmd: struct scsi_cmnd having problems
2451 *
2452 **/
2453static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2454{
2455 int rc;
2456 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2457
Brian King3f014242010-06-17 13:55:15 -05002458 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002459 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2460 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2461 return rc ? FAILED : SUCCESS;
2462}
2463
2464/**
2465 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2466 * @rport: rport struct
2467 *
2468 * Return value:
2469 * none
2470 **/
2471static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2472{
Brian Kingd2fab5c2010-08-05 16:38:34 -05002473 struct Scsi_Host *shost = rport_to_shost(rport);
Brian King072b91f2008-07-01 13:14:30 -05002474 struct ibmvfc_host *vhost = shost_priv(shost);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002475 struct fc_rport *dev_rport;
2476 struct scsi_device *sdev;
2477 unsigned long rc;
Brian King072b91f2008-07-01 13:14:30 -05002478
2479 ENTER;
Brian Kingd2fab5c2010-08-05 16:38:34 -05002480 shost_for_each_device(sdev, shost) {
2481 dev_rport = starget_to_rport(scsi_target(sdev));
2482 if (dev_rport != rport)
2483 continue;
2484 ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2485 ibmvfc_abort_task_set(sdev);
2486 }
Brian King072b91f2008-07-01 13:14:30 -05002487
Brian Kingd2fab5c2010-08-05 16:38:34 -05002488 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
Brian Kingad8dcff2008-10-29 08:46:41 -05002489
2490 if (rc == FAILED)
Brian King072b91f2008-07-01 13:14:30 -05002491 ibmvfc_issue_fc_host_lip(shost);
Brian King072b91f2008-07-01 13:14:30 -05002492 LEAVE;
2493}
2494
Brian Kingd99e5f42010-09-09 16:20:42 -05002495static const struct ibmvfc_async_desc ae_desc [] = {
Robert Jennings402c6ee2010-12-09 14:03:59 -06002496 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2497 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2498 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2499 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2500 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2501 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
2502 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
2503 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
2504 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
2505 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
2506 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
2507 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
2508 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
Brian King072b91f2008-07-01 13:14:30 -05002509};
2510
Brian Kingd99e5f42010-09-09 16:20:42 -05002511static const struct ibmvfc_async_desc unknown_ae = {
Robert Jennings402c6ee2010-12-09 14:03:59 -06002512 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
Brian Kingd99e5f42010-09-09 16:20:42 -05002513};
Brian King072b91f2008-07-01 13:14:30 -05002514
2515/**
2516 * ibmvfc_get_ae_desc - Get text description for async event
2517 * @ae: async event
2518 *
2519 **/
Brian Kingd99e5f42010-09-09 16:20:42 -05002520static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
Brian King072b91f2008-07-01 13:14:30 -05002521{
2522 int i;
2523
2524 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2525 if (ae_desc[i].ae == ae)
Brian Kingd99e5f42010-09-09 16:20:42 -05002526 return &ae_desc[i];
Brian King072b91f2008-07-01 13:14:30 -05002527
Brian Kingd99e5f42010-09-09 16:20:42 -05002528 return &unknown_ae;
2529}
2530
2531static const struct {
2532 enum ibmvfc_ae_link_state state;
2533 const char *desc;
2534} link_desc [] = {
2535 { IBMVFC_AE_LS_LINK_UP, " link up" },
2536 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
2537 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
2538 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
2539};
2540
2541/**
2542 * ibmvfc_get_link_state - Get text description for link state
2543 * @state: link state
2544 *
2545 **/
2546static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2547{
2548 int i;
2549
2550 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2551 if (link_desc[i].state == state)
2552 return link_desc[i].desc;
2553
2554 return "";
Brian King072b91f2008-07-01 13:14:30 -05002555}
2556
2557/**
2558 * ibmvfc_handle_async - Handle an async event from the adapter
2559 * @crq: crq to process
2560 * @vhost: ibmvfc host struct
2561 *
2562 **/
2563static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2564 struct ibmvfc_host *vhost)
2565{
Brian Kingd99e5f42010-09-09 16:20:42 -05002566 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(crq->event);
Brian King6d29cc52009-05-28 16:17:33 -05002567 struct ibmvfc_target *tgt;
Brian King072b91f2008-07-01 13:14:30 -05002568
Brian Kingd99e5f42010-09-09 16:20:42 -05002569 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2570 " node_name: %llx%s\n", desc->desc, crq->scsi_id, crq->wwpn, crq->node_name,
2571 ibmvfc_get_link_state(crq->link_state));
Brian King072b91f2008-07-01 13:14:30 -05002572
2573 switch (crq->event) {
Brian King072b91f2008-07-01 13:14:30 -05002574 case IBMVFC_AE_RESUME:
Brian King497f9c52009-05-28 16:17:30 -05002575 switch (crq->link_state) {
2576 case IBMVFC_AE_LS_LINK_DOWN:
2577 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2578 break;
2579 case IBMVFC_AE_LS_LINK_DEAD:
2580 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2581 break;
2582 case IBMVFC_AE_LS_LINK_UP:
2583 case IBMVFC_AE_LS_LINK_BOUNCED:
2584 default:
2585 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2586 vhost->delay_init = 1;
2587 __ibmvfc_reset_host(vhost);
2588 break;
2589 };
2590
2591 break;
2592 case IBMVFC_AE_LINK_UP:
Brian King072b91f2008-07-01 13:14:30 -05002593 vhost->events_to_log |= IBMVFC_AE_LINKUP;
Brian Kingd2131b32008-12-18 09:26:51 -06002594 vhost->delay_init = 1;
2595 __ibmvfc_reset_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002596 break;
2597 case IBMVFC_AE_SCN_FABRIC:
Brian Kingd2131b32008-12-18 09:26:51 -06002598 case IBMVFC_AE_SCN_DOMAIN:
Brian King072b91f2008-07-01 13:14:30 -05002599 vhost->events_to_log |= IBMVFC_AE_RSCN;
Brian Kingd2131b32008-12-18 09:26:51 -06002600 vhost->delay_init = 1;
2601 __ibmvfc_reset_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002602 break;
2603 case IBMVFC_AE_SCN_NPORT:
2604 case IBMVFC_AE_SCN_GROUP:
Brian King072b91f2008-07-01 13:14:30 -05002605 vhost->events_to_log |= IBMVFC_AE_RSCN;
Brian King6d29cc52009-05-28 16:17:33 -05002606 ibmvfc_reinit_host(vhost);
2607 break;
Brian King072b91f2008-07-01 13:14:30 -05002608 case IBMVFC_AE_ELS_LOGO:
2609 case IBMVFC_AE_ELS_PRLO:
2610 case IBMVFC_AE_ELS_PLOGI:
Brian King6d29cc52009-05-28 16:17:33 -05002611 list_for_each_entry(tgt, &vhost->targets, queue) {
2612 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2613 break;
2614 if (crq->scsi_id && tgt->scsi_id != crq->scsi_id)
2615 continue;
2616 if (crq->wwpn && tgt->ids.port_name != crq->wwpn)
2617 continue;
2618 if (crq->node_name && tgt->ids.node_name != crq->node_name)
2619 continue;
Brian King017b2ae2009-06-18 09:06:55 -05002620 if (tgt->need_login && crq->event == IBMVFC_AE_ELS_LOGO)
2621 tgt->logo_rcvd = 1;
2622 if (!tgt->need_login || crq->event == IBMVFC_AE_ELS_PLOGI) {
2623 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2624 ibmvfc_reinit_host(vhost);
2625 }
Brian King6d29cc52009-05-28 16:17:33 -05002626 }
Brian King072b91f2008-07-01 13:14:30 -05002627 break;
2628 case IBMVFC_AE_LINK_DOWN:
2629 case IBMVFC_AE_ADAPTER_FAILED:
2630 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2631 break;
2632 case IBMVFC_AE_LINK_DEAD:
2633 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2634 break;
2635 case IBMVFC_AE_HALT:
2636 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2637 break;
2638 default:
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002639 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
Brian King072b91f2008-07-01 13:14:30 -05002640 break;
2641 };
2642}
2643
2644/**
2645 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2646 * @crq: Command/Response queue
2647 * @vhost: ibmvfc host struct
2648 *
2649 **/
2650static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2651{
2652 long rc;
2653 struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2654
2655 switch (crq->valid) {
2656 case IBMVFC_CRQ_INIT_RSP:
2657 switch (crq->format) {
2658 case IBMVFC_CRQ_INIT:
2659 dev_info(vhost->dev, "Partner initialized\n");
2660 /* Send back a response */
2661 rc = ibmvfc_send_crq_init_complete(vhost);
2662 if (rc == 0)
Brian King861890c2009-10-19 15:07:49 -05002663 ibmvfc_init_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002664 else
2665 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2666 break;
2667 case IBMVFC_CRQ_INIT_COMPLETE:
2668 dev_info(vhost->dev, "Partner initialization complete\n");
Brian King861890c2009-10-19 15:07:49 -05002669 ibmvfc_init_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002670 break;
2671 default:
2672 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2673 }
2674 return;
2675 case IBMVFC_CRQ_XPORT_EVENT:
2676 vhost->state = IBMVFC_NO_CRQ;
Brian King79111d02009-05-28 16:17:29 -05002677 vhost->logged_in = 0;
Brian King072b91f2008-07-01 13:14:30 -05002678 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2679 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2680 /* We need to re-setup the interpartition connection */
2681 dev_info(vhost->dev, "Re-enabling adapter\n");
2682 vhost->client_migrated = 1;
2683 ibmvfc_purge_requests(vhost, DID_REQUEUE);
Brian King73ee5d82010-06-17 13:55:13 -05002684 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2685 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
Brian King072b91f2008-07-01 13:14:30 -05002686 } else {
2687 dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
Brian King072b91f2008-07-01 13:14:30 -05002688 ibmvfc_purge_requests(vhost, DID_ERROR);
Brian King73ee5d82010-06-17 13:55:13 -05002689 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2690 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
Brian King072b91f2008-07-01 13:14:30 -05002691 }
2692 return;
2693 case IBMVFC_CRQ_CMD_RSP:
2694 break;
2695 default:
2696 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2697 return;
2698 }
2699
2700 if (crq->format == IBMVFC_ASYNC_EVENT)
2701 return;
2702
2703 /* The only kind of payload CRQs we should get are responses to
2704 * things we send. Make sure this response is to something we
2705 * actually sent
2706 */
2707 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002708 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
Brian King072b91f2008-07-01 13:14:30 -05002709 crq->ioba);
2710 return;
2711 }
2712
2713 if (unlikely(atomic_read(&evt->free))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002714 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
Brian King072b91f2008-07-01 13:14:30 -05002715 crq->ioba);
2716 return;
2717 }
2718
2719 del_timer(&evt->timer);
2720 list_del(&evt->queue);
2721 ibmvfc_trc_end(evt);
2722 evt->done(evt);
2723}
2724
2725/**
2726 * ibmvfc_scan_finished - Check if the device scan is done.
2727 * @shost: scsi host struct
2728 * @time: current elapsed time
2729 *
2730 * Returns:
2731 * 0 if scan is not done / 1 if scan is done
2732 **/
2733static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2734{
2735 unsigned long flags;
2736 struct ibmvfc_host *vhost = shost_priv(shost);
2737 int done = 0;
2738
2739 spin_lock_irqsave(shost->host_lock, flags);
2740 if (time >= (init_timeout * HZ)) {
2741 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2742 "continuing initialization\n", init_timeout);
2743 done = 1;
2744 }
2745
Brian King43c8da92009-05-28 16:17:28 -05002746 if (vhost->scan_complete)
Brian King072b91f2008-07-01 13:14:30 -05002747 done = 1;
2748 spin_unlock_irqrestore(shost->host_lock, flags);
2749 return done;
2750}
2751
2752/**
2753 * ibmvfc_slave_alloc - Setup the device's task set value
2754 * @sdev: struct scsi_device device to configure
2755 *
2756 * Set the device's task set value so that error handling works as
2757 * expected.
2758 *
2759 * Returns:
2760 * 0 on success / -ENXIO if device does not exist
2761 **/
2762static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2763{
2764 struct Scsi_Host *shost = sdev->host;
2765 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2766 struct ibmvfc_host *vhost = shost_priv(shost);
2767 unsigned long flags = 0;
2768
2769 if (!rport || fc_remote_port_chkready(rport))
2770 return -ENXIO;
2771
2772 spin_lock_irqsave(shost->host_lock, flags);
2773 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2774 spin_unlock_irqrestore(shost->host_lock, flags);
2775 return 0;
2776}
2777
2778/**
Brian Kingad8dcff2008-10-29 08:46:41 -05002779 * ibmvfc_target_alloc - Setup the target's task set value
2780 * @starget: struct scsi_target
2781 *
2782 * Set the target's task set value so that error handling works as
2783 * expected.
2784 *
2785 * Returns:
2786 * 0 on success / -ENXIO if device does not exist
2787 **/
2788static int ibmvfc_target_alloc(struct scsi_target *starget)
2789{
2790 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2791 struct ibmvfc_host *vhost = shost_priv(shost);
2792 unsigned long flags = 0;
2793
2794 spin_lock_irqsave(shost->host_lock, flags);
2795 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2796 spin_unlock_irqrestore(shost->host_lock, flags);
2797 return 0;
2798}
2799
2800/**
Brian King072b91f2008-07-01 13:14:30 -05002801 * ibmvfc_slave_configure - Configure the device
2802 * @sdev: struct scsi_device device to configure
2803 *
2804 * Enable allow_restart for a device if it is a disk. Adjust the
2805 * queue_depth here also.
2806 *
2807 * Returns:
2808 * 0
2809 **/
2810static int ibmvfc_slave_configure(struct scsi_device *sdev)
2811{
2812 struct Scsi_Host *shost = sdev->host;
Brian King072b91f2008-07-01 13:14:30 -05002813 unsigned long flags = 0;
2814
2815 spin_lock_irqsave(shost->host_lock, flags);
2816 if (sdev->type == TYPE_DISK)
2817 sdev->allow_restart = 1;
2818
2819 if (sdev->tagged_supported) {
2820 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2821 scsi_activate_tcq(sdev, sdev->queue_depth);
2822 } else
2823 scsi_deactivate_tcq(sdev, sdev->queue_depth);
Brian King072b91f2008-07-01 13:14:30 -05002824 spin_unlock_irqrestore(shost->host_lock, flags);
2825 return 0;
2826}
2827
2828/**
2829 * ibmvfc_change_queue_depth - Change the device's queue depth
2830 * @sdev: scsi device struct
2831 * @qdepth: depth to set
Mike Christiee881a172009-10-15 17:46:39 -07002832 * @reason: calling context
Brian King072b91f2008-07-01 13:14:30 -05002833 *
2834 * Return value:
2835 * actual depth set
2836 **/
Mike Christiee881a172009-10-15 17:46:39 -07002837static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth,
2838 int reason)
Brian King072b91f2008-07-01 13:14:30 -05002839{
Mike Christiee881a172009-10-15 17:46:39 -07002840 if (reason != SCSI_QDEPTH_DEFAULT)
2841 return -EOPNOTSUPP;
2842
Brian King072b91f2008-07-01 13:14:30 -05002843 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2844 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2845
2846 scsi_adjust_queue_depth(sdev, 0, qdepth);
2847 return sdev->queue_depth;
2848}
2849
2850/**
2851 * ibmvfc_change_queue_type - Change the device's queue type
2852 * @sdev: scsi device struct
2853 * @tag_type: type of tags to use
2854 *
2855 * Return value:
2856 * actual queue type set
2857 **/
2858static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2859{
2860 if (sdev->tagged_supported) {
2861 scsi_set_tag_type(sdev, tag_type);
2862
2863 if (tag_type)
2864 scsi_activate_tcq(sdev, sdev->queue_depth);
2865 else
2866 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2867 } else
2868 tag_type = 0;
2869
2870 return tag_type;
2871}
2872
2873static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2874 struct device_attribute *attr, char *buf)
2875{
2876 struct Scsi_Host *shost = class_to_shost(dev);
2877 struct ibmvfc_host *vhost = shost_priv(shost);
2878
2879 return snprintf(buf, PAGE_SIZE, "%s\n",
2880 vhost->login_buf->resp.partition_name);
2881}
2882
Brian King072b91f2008-07-01 13:14:30 -05002883static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2884 struct device_attribute *attr, char *buf)
2885{
2886 struct Scsi_Host *shost = class_to_shost(dev);
2887 struct ibmvfc_host *vhost = shost_priv(shost);
2888
2889 return snprintf(buf, PAGE_SIZE, "%s\n",
2890 vhost->login_buf->resp.device_name);
2891}
2892
Brian King072b91f2008-07-01 13:14:30 -05002893static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2894 struct device_attribute *attr, char *buf)
2895{
2896 struct Scsi_Host *shost = class_to_shost(dev);
2897 struct ibmvfc_host *vhost = shost_priv(shost);
2898
2899 return snprintf(buf, PAGE_SIZE, "%s\n",
2900 vhost->login_buf->resp.port_loc_code);
2901}
2902
Brian King072b91f2008-07-01 13:14:30 -05002903static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2904 struct device_attribute *attr, char *buf)
2905{
2906 struct Scsi_Host *shost = class_to_shost(dev);
2907 struct ibmvfc_host *vhost = shost_priv(shost);
2908
2909 return snprintf(buf, PAGE_SIZE, "%s\n",
2910 vhost->login_buf->resp.drc_name);
2911}
2912
Brian King072b91f2008-07-01 13:14:30 -05002913static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2914 struct device_attribute *attr, char *buf)
2915{
2916 struct Scsi_Host *shost = class_to_shost(dev);
2917 struct ibmvfc_host *vhost = shost_priv(shost);
2918 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2919}
2920
Brian King497f9c52009-05-28 16:17:30 -05002921static ssize_t ibmvfc_show_host_capabilities(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 return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
2927}
2928
Brian King072b91f2008-07-01 13:14:30 -05002929/**
2930 * ibmvfc_show_log_level - Show the adapter's error logging level
2931 * @dev: class device struct
2932 * @buf: buffer
2933 *
2934 * Return value:
2935 * number of bytes printed to buffer
2936 **/
2937static ssize_t ibmvfc_show_log_level(struct device *dev,
2938 struct device_attribute *attr, char *buf)
2939{
2940 struct Scsi_Host *shost = class_to_shost(dev);
2941 struct ibmvfc_host *vhost = shost_priv(shost);
2942 unsigned long flags = 0;
2943 int len;
2944
2945 spin_lock_irqsave(shost->host_lock, flags);
2946 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2947 spin_unlock_irqrestore(shost->host_lock, flags);
2948 return len;
2949}
2950
2951/**
2952 * ibmvfc_store_log_level - Change the adapter's error logging level
2953 * @dev: class device struct
2954 * @buf: buffer
2955 *
2956 * Return value:
2957 * number of bytes printed to buffer
2958 **/
2959static ssize_t ibmvfc_store_log_level(struct device *dev,
2960 struct device_attribute *attr,
2961 const char *buf, size_t count)
2962{
2963 struct Scsi_Host *shost = class_to_shost(dev);
2964 struct ibmvfc_host *vhost = shost_priv(shost);
2965 unsigned long flags = 0;
2966
2967 spin_lock_irqsave(shost->host_lock, flags);
2968 vhost->log_level = simple_strtoul(buf, NULL, 10);
2969 spin_unlock_irqrestore(shost->host_lock, flags);
2970 return strlen(buf);
2971}
2972
Brian King85e23992009-05-28 16:17:25 -05002973static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
2974static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
2975static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
2976static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
2977static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
Brian King497f9c52009-05-28 16:17:30 -05002978static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
Brian King85e23992009-05-28 16:17:25 -05002979static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
2980 ibmvfc_show_log_level, ibmvfc_store_log_level);
Brian King072b91f2008-07-01 13:14:30 -05002981
2982#ifdef CONFIG_SCSI_IBMVFC_TRACE
2983/**
2984 * ibmvfc_read_trace - Dump the adapter trace
Chris Wright2c3c8be2010-05-12 18:28:57 -07002985 * @filp: open sysfs file
Brian King072b91f2008-07-01 13:14:30 -05002986 * @kobj: kobject struct
2987 * @bin_attr: bin_attribute struct
2988 * @buf: buffer
2989 * @off: offset
2990 * @count: buffer size
2991 *
2992 * Return value:
2993 * number of bytes printed to buffer
2994 **/
Chris Wright2c3c8be2010-05-12 18:28:57 -07002995static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
Brian King072b91f2008-07-01 13:14:30 -05002996 struct bin_attribute *bin_attr,
2997 char *buf, loff_t off, size_t count)
2998{
2999 struct device *dev = container_of(kobj, struct device, kobj);
3000 struct Scsi_Host *shost = class_to_shost(dev);
3001 struct ibmvfc_host *vhost = shost_priv(shost);
3002 unsigned long flags = 0;
3003 int size = IBMVFC_TRACE_SIZE;
3004 char *src = (char *)vhost->trace;
3005
3006 if (off > size)
3007 return 0;
3008 if (off + count > size) {
3009 size -= off;
3010 count = size;
3011 }
3012
3013 spin_lock_irqsave(shost->host_lock, flags);
3014 memcpy(buf, &src[off], count);
3015 spin_unlock_irqrestore(shost->host_lock, flags);
3016 return count;
3017}
3018
3019static struct bin_attribute ibmvfc_trace_attr = {
3020 .attr = {
3021 .name = "trace",
3022 .mode = S_IRUGO,
3023 },
3024 .size = 0,
3025 .read = ibmvfc_read_trace,
3026};
3027#endif
3028
3029static struct device_attribute *ibmvfc_attrs[] = {
Brian King85e23992009-05-28 16:17:25 -05003030 &dev_attr_partition_name,
3031 &dev_attr_device_name,
3032 &dev_attr_port_loc_code,
3033 &dev_attr_drc_name,
3034 &dev_attr_npiv_version,
Brian King497f9c52009-05-28 16:17:30 -05003035 &dev_attr_capabilities,
Brian King85e23992009-05-28 16:17:25 -05003036 &dev_attr_log_level,
Brian King072b91f2008-07-01 13:14:30 -05003037 NULL
3038};
3039
3040static struct scsi_host_template driver_template = {
3041 .module = THIS_MODULE,
3042 .name = "IBM POWER Virtual FC Adapter",
3043 .proc_name = IBMVFC_NAME,
3044 .queuecommand = ibmvfc_queuecommand,
3045 .eh_abort_handler = ibmvfc_eh_abort_handler,
3046 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3047 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3048 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3049 .slave_alloc = ibmvfc_slave_alloc,
3050 .slave_configure = ibmvfc_slave_configure,
Brian Kingad8dcff2008-10-29 08:46:41 -05003051 .target_alloc = ibmvfc_target_alloc,
Brian King072b91f2008-07-01 13:14:30 -05003052 .scan_finished = ibmvfc_scan_finished,
3053 .change_queue_depth = ibmvfc_change_queue_depth,
3054 .change_queue_type = ibmvfc_change_queue_type,
3055 .cmd_per_lun = 16,
3056 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3057 .this_id = -1,
3058 .sg_tablesize = SG_ALL,
3059 .max_sectors = IBMVFC_MAX_SECTORS,
3060 .use_clustering = ENABLE_CLUSTERING,
3061 .shost_attrs = ibmvfc_attrs,
3062};
3063
3064/**
3065 * ibmvfc_next_async_crq - Returns the next entry in async queue
3066 * @vhost: ibmvfc host struct
3067 *
3068 * Returns:
3069 * Pointer to next entry in queue / NULL if empty
3070 **/
3071static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3072{
3073 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3074 struct ibmvfc_async_crq *crq;
3075
3076 crq = &async_crq->msgs[async_crq->cur];
3077 if (crq->valid & 0x80) {
3078 if (++async_crq->cur == async_crq->size)
3079 async_crq->cur = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003080 rmb();
Brian King072b91f2008-07-01 13:14:30 -05003081 } else
3082 crq = NULL;
3083
3084 return crq;
3085}
3086
3087/**
3088 * ibmvfc_next_crq - Returns the next entry in message queue
3089 * @vhost: ibmvfc host struct
3090 *
3091 * Returns:
3092 * Pointer to next entry in queue / NULL if empty
3093 **/
3094static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3095{
3096 struct ibmvfc_crq_queue *queue = &vhost->crq;
3097 struct ibmvfc_crq *crq;
3098
3099 crq = &queue->msgs[queue->cur];
3100 if (crq->valid & 0x80) {
3101 if (++queue->cur == queue->size)
3102 queue->cur = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003103 rmb();
Brian King072b91f2008-07-01 13:14:30 -05003104 } else
3105 crq = NULL;
3106
3107 return crq;
3108}
3109
3110/**
3111 * ibmvfc_interrupt - Interrupt handler
3112 * @irq: number of irq to handle, not used
3113 * @dev_instance: ibmvfc_host that received interrupt
3114 *
3115 * Returns:
3116 * IRQ_HANDLED
3117 **/
3118static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3119{
3120 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
Brian King039a0892009-03-20 15:44:35 -05003121 unsigned long flags;
3122
3123 spin_lock_irqsave(vhost->host->host_lock, flags);
3124 vio_disable_interrupts(to_vio_dev(vhost->dev));
3125 tasklet_schedule(&vhost->tasklet);
3126 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3127 return IRQ_HANDLED;
3128}
3129
3130/**
3131 * ibmvfc_tasklet - Interrupt handler tasklet
3132 * @data: ibmvfc host struct
3133 *
3134 * Returns:
3135 * Nothing
3136 **/
3137static void ibmvfc_tasklet(void *data)
3138{
3139 struct ibmvfc_host *vhost = data;
Brian King072b91f2008-07-01 13:14:30 -05003140 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3141 struct ibmvfc_crq *crq;
3142 struct ibmvfc_async_crq *async;
3143 unsigned long flags;
3144 int done = 0;
3145
3146 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05003147 while (!done) {
Brian King072b91f2008-07-01 13:14:30 -05003148 /* Pull all the valid messages off the async CRQ */
3149 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3150 ibmvfc_handle_async(async, vhost);
3151 async->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003152 wmb();
Brian King072b91f2008-07-01 13:14:30 -05003153 }
3154
Brian Kingf1d7fb72009-06-18 09:06:52 -05003155 /* Pull all the valid messages off the CRQ */
3156 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
Brian King072b91f2008-07-01 13:14:30 -05003157 ibmvfc_handle_crq(crq, vhost);
3158 crq->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003159 wmb();
Brian Kingf1d7fb72009-06-18 09:06:52 -05003160 }
3161
3162 vio_enable_interrupts(vdev);
3163 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
Brian King072b91f2008-07-01 13:14:30 -05003164 vio_disable_interrupts(vdev);
3165 ibmvfc_handle_async(async, vhost);
Brian King4081b772008-11-14 13:33:50 -06003166 async->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003167 wmb();
Brian Kingf1d7fb72009-06-18 09:06:52 -05003168 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3169 vio_disable_interrupts(vdev);
3170 ibmvfc_handle_crq(crq, vhost);
3171 crq->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003172 wmb();
Brian King072b91f2008-07-01 13:14:30 -05003173 } else
3174 done = 1;
3175 }
3176
3177 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05003178}
3179
3180/**
3181 * ibmvfc_init_tgt - Set the next init job step for the target
3182 * @tgt: ibmvfc target struct
3183 * @job_step: job step to perform
3184 *
3185 **/
3186static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3187 void (*job_step) (struct ibmvfc_target *))
3188{
3189 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
3190 tgt->job_step = job_step;
3191 wake_up(&tgt->vhost->work_wait_q);
3192}
3193
3194/**
3195 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3196 * @tgt: ibmvfc target struct
3197 * @job_step: initialization job step
3198 *
Brian King7d0e4622009-05-28 16:17:26 -05003199 * Returns: 1 if step will be retried / 0 if not
3200 *
Brian King072b91f2008-07-01 13:14:30 -05003201 **/
Brian King7d0e4622009-05-28 16:17:26 -05003202static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
Brian King072b91f2008-07-01 13:14:30 -05003203 void (*job_step) (struct ibmvfc_target *))
3204{
Brian King1c41fa822008-12-03 11:02:54 -06003205 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -05003206 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3207 wake_up(&tgt->vhost->work_wait_q);
Brian King7d0e4622009-05-28 16:17:26 -05003208 return 0;
Brian King072b91f2008-07-01 13:14:30 -05003209 } else
3210 ibmvfc_init_tgt(tgt, job_step);
Brian King7d0e4622009-05-28 16:17:26 -05003211 return 1;
Brian King072b91f2008-07-01 13:14:30 -05003212}
3213
Brian Kinga3b7aea2009-02-02 18:39:40 -06003214/* Defined in FC-LS */
3215static const struct {
3216 int code;
3217 int retry;
3218 int logged_in;
3219} prli_rsp [] = {
3220 { 0, 1, 0 },
3221 { 1, 0, 1 },
3222 { 2, 1, 0 },
3223 { 3, 1, 0 },
3224 { 4, 0, 0 },
3225 { 5, 0, 0 },
3226 { 6, 0, 1 },
3227 { 7, 0, 0 },
3228 { 8, 1, 0 },
3229};
3230
3231/**
3232 * ibmvfc_get_prli_rsp - Find PRLI response index
3233 * @flags: PRLI response flags
3234 *
3235 **/
3236static int ibmvfc_get_prli_rsp(u16 flags)
3237{
3238 int i;
3239 int code = (flags & 0x0f00) >> 8;
3240
3241 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3242 if (prli_rsp[i].code == code)
3243 return i;
3244
3245 return 0;
3246}
3247
Brian King072b91f2008-07-01 13:14:30 -05003248/**
Brian King072b91f2008-07-01 13:14:30 -05003249 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3250 * @evt: ibmvfc event struct
3251 *
3252 **/
3253static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3254{
3255 struct ibmvfc_target *tgt = evt->tgt;
3256 struct ibmvfc_host *vhost = evt->vhost;
3257 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
Brian Kinga3b7aea2009-02-02 18:39:40 -06003258 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
Brian King072b91f2008-07-01 13:14:30 -05003259 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003260 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003261
3262 vhost->discovery_threads--;
3263 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3264 switch (status) {
3265 case IBMVFC_MAD_SUCCESS:
Brian Kinga3b7aea2009-02-02 18:39:40 -06003266 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3267 parms->type, parms->flags, parms->service_parms);
3268
3269 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3270 index = ibmvfc_get_prli_rsp(parms->flags);
3271 if (prli_rsp[index].logged_in) {
3272 if (parms->flags & IBMVFC_PRLI_EST_IMG_PAIR) {
3273 tgt->need_login = 0;
3274 tgt->ids.roles = 0;
3275 if (parms->service_parms & IBMVFC_PRLI_TARGET_FUNC)
3276 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3277 if (parms->service_parms & IBMVFC_PRLI_INITIATOR_FUNC)
3278 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
Brian King43c8da92009-05-28 16:17:28 -05003279 tgt->add_rport = 1;
Brian Kinga3b7aea2009-02-02 18:39:40 -06003280 } else
3281 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3282 } else if (prli_rsp[index].retry)
3283 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3284 else
3285 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3286 } else
3287 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King072b91f2008-07-01 13:14:30 -05003288 break;
3289 case IBMVFC_MAD_DRIVER_FAILED:
3290 break;
3291 case IBMVFC_MAD_CRQ_ERROR:
3292 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3293 break;
3294 case IBMVFC_MAD_FAILED:
3295 default:
Brian King017b2ae2009-06-18 09:06:55 -05003296 if ((rsp->status & IBMVFC_VIOS_FAILURE) && rsp->error == IBMVFC_PLOGI_REQUIRED)
3297 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3298 else if (tgt->logo_rcvd)
3299 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3300 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05003301 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
Brian King10e79492008-10-29 08:46:45 -05003302 else
3303 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05003304
3305 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3306 ibmvfc_get_cmd_error(rsp->status, rsp->error),
3307 rsp->status, rsp->error, status);
Brian King072b91f2008-07-01 13:14:30 -05003308 break;
3309 };
3310
3311 kref_put(&tgt->kref, ibmvfc_release_tgt);
3312 ibmvfc_free_event(evt);
3313 wake_up(&vhost->work_wait_q);
3314}
3315
3316/**
3317 * ibmvfc_tgt_send_prli - Send a process login
3318 * @tgt: ibmvfc target struct
3319 *
3320 **/
3321static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3322{
3323 struct ibmvfc_process_login *prli;
3324 struct ibmvfc_host *vhost = tgt->vhost;
3325 struct ibmvfc_event *evt;
3326
3327 if (vhost->discovery_threads >= disc_threads)
3328 return;
3329
3330 kref_get(&tgt->kref);
3331 evt = ibmvfc_get_event(vhost);
3332 vhost->discovery_threads++;
3333 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3334 evt->tgt = tgt;
3335 prli = &evt->iu.prli;
3336 memset(prli, 0, sizeof(*prli));
3337 prli->common.version = 1;
3338 prli->common.opcode = IBMVFC_PROCESS_LOGIN;
3339 prli->common.length = sizeof(*prli);
3340 prli->scsi_id = tgt->scsi_id;
3341
3342 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3343 prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
3344 prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
3345
3346 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3347 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3348 vhost->discovery_threads--;
3349 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3350 kref_put(&tgt->kref, ibmvfc_release_tgt);
3351 } else
3352 tgt_dbg(tgt, "Sent process login\n");
3353}
3354
3355/**
3356 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3357 * @evt: ibmvfc event struct
3358 *
3359 **/
3360static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3361{
3362 struct ibmvfc_target *tgt = evt->tgt;
3363 struct ibmvfc_host *vhost = evt->vhost;
3364 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3365 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003366 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003367
3368 vhost->discovery_threads--;
3369 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3370 switch (status) {
3371 case IBMVFC_MAD_SUCCESS:
3372 tgt_dbg(tgt, "Port Login succeeded\n");
3373 if (tgt->ids.port_name &&
3374 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3375 vhost->reinit = 1;
3376 tgt_dbg(tgt, "Port re-init required\n");
3377 break;
3378 }
3379 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3380 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3381 tgt->ids.port_id = tgt->scsi_id;
Brian King072b91f2008-07-01 13:14:30 -05003382 memcpy(&tgt->service_parms, &rsp->service_parms,
3383 sizeof(tgt->service_parms));
3384 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3385 sizeof(tgt->service_parms_change));
3386 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3387 break;
3388 case IBMVFC_MAD_DRIVER_FAILED:
3389 break;
3390 case IBMVFC_MAD_CRQ_ERROR:
3391 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3392 break;
3393 case IBMVFC_MAD_FAILED:
3394 default:
Brian King7d0e4622009-05-28 16:17:26 -05003395 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3396 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3397 else
3398 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3399
3400 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
Brian King072b91f2008-07-01 13:14:30 -05003401 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3402 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3403 ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05003404 break;
3405 };
3406
3407 kref_put(&tgt->kref, ibmvfc_release_tgt);
3408 ibmvfc_free_event(evt);
3409 wake_up(&vhost->work_wait_q);
3410}
3411
3412/**
3413 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3414 * @tgt: ibmvfc target struct
3415 *
3416 **/
3417static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3418{
3419 struct ibmvfc_port_login *plogi;
3420 struct ibmvfc_host *vhost = tgt->vhost;
3421 struct ibmvfc_event *evt;
3422
3423 if (vhost->discovery_threads >= disc_threads)
3424 return;
3425
3426 kref_get(&tgt->kref);
Brian King017b2ae2009-06-18 09:06:55 -05003427 tgt->logo_rcvd = 0;
Brian King072b91f2008-07-01 13:14:30 -05003428 evt = ibmvfc_get_event(vhost);
3429 vhost->discovery_threads++;
3430 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3431 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3432 evt->tgt = tgt;
3433 plogi = &evt->iu.plogi;
3434 memset(plogi, 0, sizeof(*plogi));
3435 plogi->common.version = 1;
3436 plogi->common.opcode = IBMVFC_PORT_LOGIN;
3437 plogi->common.length = sizeof(*plogi);
3438 plogi->scsi_id = tgt->scsi_id;
3439
3440 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3441 vhost->discovery_threads--;
3442 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3443 kref_put(&tgt->kref, ibmvfc_release_tgt);
3444 } else
3445 tgt_dbg(tgt, "Sent port login\n");
3446}
3447
3448/**
3449 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3450 * @evt: ibmvfc event struct
3451 *
3452 **/
3453static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3454{
3455 struct ibmvfc_target *tgt = evt->tgt;
3456 struct ibmvfc_host *vhost = evt->vhost;
3457 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3458 u32 status = rsp->common.status;
3459
3460 vhost->discovery_threads--;
3461 ibmvfc_free_event(evt);
3462 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3463
3464 switch (status) {
3465 case IBMVFC_MAD_SUCCESS:
3466 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3467 break;
3468 case IBMVFC_MAD_DRIVER_FAILED:
3469 kref_put(&tgt->kref, ibmvfc_release_tgt);
3470 wake_up(&vhost->work_wait_q);
3471 return;
3472 case IBMVFC_MAD_FAILED:
3473 default:
3474 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3475 break;
3476 };
3477
3478 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
3479 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3480 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
3481 tgt->scsi_id != tgt->new_scsi_id)
3482 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3483 kref_put(&tgt->kref, ibmvfc_release_tgt);
3484 wake_up(&vhost->work_wait_q);
3485}
3486
3487/**
3488 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3489 * @tgt: ibmvfc target struct
3490 *
3491 **/
3492static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3493{
3494 struct ibmvfc_implicit_logout *mad;
3495 struct ibmvfc_host *vhost = tgt->vhost;
3496 struct ibmvfc_event *evt;
3497
3498 if (vhost->discovery_threads >= disc_threads)
3499 return;
3500
3501 kref_get(&tgt->kref);
3502 evt = ibmvfc_get_event(vhost);
3503 vhost->discovery_threads++;
3504 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3505 evt->tgt = tgt;
3506 mad = &evt->iu.implicit_logout;
3507 memset(mad, 0, sizeof(*mad));
3508 mad->common.version = 1;
3509 mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
3510 mad->common.length = sizeof(*mad);
3511 mad->old_scsi_id = tgt->scsi_id;
3512
3513 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3514 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3515 vhost->discovery_threads--;
3516 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3517 kref_put(&tgt->kref, ibmvfc_release_tgt);
3518 } else
3519 tgt_dbg(tgt, "Sent Implicit Logout\n");
3520}
3521
3522/**
Brian King989b8542008-07-22 08:31:47 -05003523 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3524 * @mad: ibmvfc passthru mad struct
3525 * @tgt: ibmvfc target struct
3526 *
3527 * Returns:
3528 * 1 if PLOGI needed / 0 if PLOGI not needed
3529 **/
3530static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3531 struct ibmvfc_target *tgt)
3532{
3533 if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3534 sizeof(tgt->ids.port_name)))
3535 return 1;
3536 if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3537 sizeof(tgt->ids.node_name)))
3538 return 1;
3539 if (mad->fc_iu.response[6] != tgt->scsi_id)
3540 return 1;
3541 return 0;
3542}
3543
3544/**
3545 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3546 * @evt: ibmvfc event struct
3547 *
3548 **/
3549static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3550{
3551 struct ibmvfc_target *tgt = evt->tgt;
3552 struct ibmvfc_host *vhost = evt->vhost;
3553 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3554 u32 status = mad->common.status;
3555 u8 fc_reason, fc_explain;
3556
3557 vhost->discovery_threads--;
3558 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
Brian King10501e12009-03-20 15:44:39 -05003559 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003560
3561 switch (status) {
3562 case IBMVFC_MAD_SUCCESS:
3563 tgt_dbg(tgt, "ADISC succeeded\n");
3564 if (ibmvfc_adisc_needs_plogi(mad, tgt))
Brian King5e471672009-05-28 16:17:32 -05003565 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King989b8542008-07-22 08:31:47 -05003566 break;
3567 case IBMVFC_MAD_DRIVER_FAILED:
3568 break;
3569 case IBMVFC_MAD_FAILED:
3570 default:
Brian King5e471672009-05-28 16:17:32 -05003571 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King989b8542008-07-22 08:31:47 -05003572 fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
3573 fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
3574 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3575 ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
3576 mad->iu.status, mad->iu.error,
3577 ibmvfc_get_fc_type(fc_reason), fc_reason,
3578 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3579 break;
3580 };
3581
3582 kref_put(&tgt->kref, ibmvfc_release_tgt);
3583 ibmvfc_free_event(evt);
3584 wake_up(&vhost->work_wait_q);
3585}
3586
3587/**
3588 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3589 * @evt: ibmvfc event struct
3590 *
3591 **/
3592static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3593{
3594 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3595
3596 memset(mad, 0, sizeof(*mad));
3597 mad->common.version = 1;
3598 mad->common.opcode = IBMVFC_PASSTHRU;
3599 mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
3600 mad->cmd_ioba.va = (u64)evt->crq.ioba +
3601 offsetof(struct ibmvfc_passthru_mad, iu);
3602 mad->cmd_ioba.len = sizeof(mad->iu);
3603 mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
3604 mad->iu.rsp_len = sizeof(mad->fc_iu.response);
3605 mad->iu.cmd.va = (u64)evt->crq.ioba +
3606 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3607 offsetof(struct ibmvfc_passthru_fc_iu, payload);
3608 mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
3609 mad->iu.rsp.va = (u64)evt->crq.ioba +
3610 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3611 offsetof(struct ibmvfc_passthru_fc_iu, response);
3612 mad->iu.rsp.len = sizeof(mad->fc_iu.response);
3613}
3614
3615/**
Brian King10501e12009-03-20 15:44:39 -05003616 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3617 * @evt: ibmvfc event struct
3618 *
3619 * Just cleanup this event struct. Everything else is handled by
3620 * the ADISC completion handler. If the ADISC never actually comes
3621 * back, we still have the timer running on the ADISC event struct
3622 * which will fire and cause the CRQ to get reset.
3623 *
3624 **/
3625static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3626{
3627 struct ibmvfc_host *vhost = evt->vhost;
3628 struct ibmvfc_target *tgt = evt->tgt;
3629
3630 tgt_dbg(tgt, "ADISC cancel complete\n");
3631 vhost->abort_threads--;
3632 ibmvfc_free_event(evt);
3633 kref_put(&tgt->kref, ibmvfc_release_tgt);
3634 wake_up(&vhost->work_wait_q);
3635}
3636
3637/**
3638 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3639 * @tgt: ibmvfc target struct
3640 *
3641 * If an ADISC times out, send a cancel. If the cancel times
3642 * out, reset the CRQ. When the ADISC comes back as cancelled,
3643 * log back into the target.
3644 **/
3645static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
3646{
3647 struct ibmvfc_host *vhost = tgt->vhost;
3648 struct ibmvfc_event *evt;
3649 struct ibmvfc_tmf *tmf;
3650 unsigned long flags;
3651 int rc;
3652
3653 tgt_dbg(tgt, "ADISC timeout\n");
3654 spin_lock_irqsave(vhost->host->host_lock, flags);
3655 if (vhost->abort_threads >= disc_threads ||
3656 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3657 vhost->state != IBMVFC_INITIALIZING ||
3658 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3659 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3660 return;
3661 }
3662
3663 vhost->abort_threads++;
3664 kref_get(&tgt->kref);
3665 evt = ibmvfc_get_event(vhost);
3666 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3667
3668 evt->tgt = tgt;
3669 tmf = &evt->iu.tmf;
3670 memset(tmf, 0, sizeof(*tmf));
3671 tmf->common.version = 1;
3672 tmf->common.opcode = IBMVFC_TMF_MAD;
3673 tmf->common.length = sizeof(*tmf);
3674 tmf->scsi_id = tgt->scsi_id;
3675 tmf->cancel_key = tgt->cancel_key;
3676
3677 rc = ibmvfc_send_event(evt, vhost, default_timeout);
3678
3679 if (rc) {
3680 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3681 vhost->abort_threads--;
3682 kref_put(&tgt->kref, ibmvfc_release_tgt);
3683 __ibmvfc_reset_host(vhost);
3684 } else
3685 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3686 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3687}
3688
3689/**
Brian King989b8542008-07-22 08:31:47 -05003690 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3691 * @tgt: ibmvfc target struct
3692 *
Brian King10501e12009-03-20 15:44:39 -05003693 * When sending an ADISC we end up with two timers running. The
3694 * first timer is the timer in the ibmvfc target struct. If this
3695 * fires, we send a cancel to the target. The second timer is the
3696 * timer on the ibmvfc event for the ADISC, which is longer. If that
3697 * fires, it means the ADISC timed out and our attempt to cancel it
3698 * also failed, so we need to reset the CRQ.
Brian King989b8542008-07-22 08:31:47 -05003699 **/
3700static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3701{
3702 struct ibmvfc_passthru_mad *mad;
3703 struct ibmvfc_host *vhost = tgt->vhost;
3704 struct ibmvfc_event *evt;
3705
3706 if (vhost->discovery_threads >= disc_threads)
3707 return;
3708
3709 kref_get(&tgt->kref);
3710 evt = ibmvfc_get_event(vhost);
3711 vhost->discovery_threads++;
3712 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3713 evt->tgt = tgt;
3714
3715 ibmvfc_init_passthru(evt);
3716 mad = &evt->iu.passthru;
3717 mad->iu.flags = IBMVFC_FC_ELS;
3718 mad->iu.scsi_id = tgt->scsi_id;
Brian King10501e12009-03-20 15:44:39 -05003719 mad->iu.cancel_key = tgt->cancel_key;
Brian King989b8542008-07-22 08:31:47 -05003720
3721 mad->fc_iu.payload[0] = IBMVFC_ADISC;
3722 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3723 sizeof(vhost->login_buf->resp.port_name));
3724 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3725 sizeof(vhost->login_buf->resp.node_name));
3726 mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
3727
Brian King10501e12009-03-20 15:44:39 -05003728 if (timer_pending(&tgt->timer))
3729 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
3730 else {
3731 tgt->timer.data = (unsigned long) tgt;
3732 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
3733 tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
3734 add_timer(&tgt->timer);
3735 }
3736
Brian King989b8542008-07-22 08:31:47 -05003737 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
Brian King10501e12009-03-20 15:44:39 -05003738 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
Brian King989b8542008-07-22 08:31:47 -05003739 vhost->discovery_threads--;
Brian King10501e12009-03-20 15:44:39 -05003740 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003741 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3742 kref_put(&tgt->kref, ibmvfc_release_tgt);
3743 } else
3744 tgt_dbg(tgt, "Sent ADISC\n");
3745}
3746
3747/**
Brian King072b91f2008-07-01 13:14:30 -05003748 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3749 * @evt: ibmvfc event struct
3750 *
3751 **/
3752static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3753{
3754 struct ibmvfc_target *tgt = evt->tgt;
3755 struct ibmvfc_host *vhost = evt->vhost;
3756 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3757 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003758 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003759
3760 vhost->discovery_threads--;
3761 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3762 switch (status) {
3763 case IBMVFC_MAD_SUCCESS:
3764 tgt_dbg(tgt, "Query Target succeeded\n");
3765 tgt->new_scsi_id = rsp->scsi_id;
3766 if (rsp->scsi_id != tgt->scsi_id)
3767 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
Brian King989b8542008-07-22 08:31:47 -05003768 else
3769 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
Brian King072b91f2008-07-01 13:14:30 -05003770 break;
3771 case IBMVFC_MAD_DRIVER_FAILED:
3772 break;
3773 case IBMVFC_MAD_CRQ_ERROR:
3774 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3775 break;
3776 case IBMVFC_MAD_FAILED:
3777 default:
Brian King072b91f2008-07-01 13:14:30 -05003778 if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3779 rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3780 rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
3781 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3782 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05003783 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
Brian King10e79492008-10-29 08:46:45 -05003784 else
3785 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05003786
3787 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3788 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3789 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3790 ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05003791 break;
3792 };
3793
3794 kref_put(&tgt->kref, ibmvfc_release_tgt);
3795 ibmvfc_free_event(evt);
3796 wake_up(&vhost->work_wait_q);
3797}
3798
3799/**
3800 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3801 * @tgt: ibmvfc target struct
3802 *
3803 **/
3804static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3805{
3806 struct ibmvfc_query_tgt *query_tgt;
3807 struct ibmvfc_host *vhost = tgt->vhost;
3808 struct ibmvfc_event *evt;
3809
3810 if (vhost->discovery_threads >= disc_threads)
3811 return;
3812
3813 kref_get(&tgt->kref);
3814 evt = ibmvfc_get_event(vhost);
3815 vhost->discovery_threads++;
3816 evt->tgt = tgt;
3817 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3818 query_tgt = &evt->iu.query_tgt;
3819 memset(query_tgt, 0, sizeof(*query_tgt));
3820 query_tgt->common.version = 1;
3821 query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
3822 query_tgt->common.length = sizeof(*query_tgt);
3823 query_tgt->wwpn = tgt->ids.port_name;
3824
3825 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3826 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3827 vhost->discovery_threads--;
3828 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3829 kref_put(&tgt->kref, ibmvfc_release_tgt);
3830 } else
3831 tgt_dbg(tgt, "Sent Query Target\n");
3832}
3833
3834/**
3835 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3836 * @vhost: ibmvfc host struct
3837 * @scsi_id: SCSI ID to allocate target for
3838 *
3839 * Returns:
3840 * 0 on success / other on failure
3841 **/
3842static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3843{
3844 struct ibmvfc_target *tgt;
3845 unsigned long flags;
3846
3847 spin_lock_irqsave(vhost->host->host_lock, flags);
3848 list_for_each_entry(tgt, &vhost->targets, queue) {
3849 if (tgt->scsi_id == scsi_id) {
3850 if (tgt->need_login)
3851 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3852 goto unlock_out;
3853 }
3854 }
3855 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3856
Brian King7270b9b2009-05-28 16:17:24 -05003857 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
Brian King072b91f2008-07-01 13:14:30 -05003858 if (!tgt) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00003859 dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
Brian King072b91f2008-07-01 13:14:30 -05003860 scsi_id);
3861 return -ENOMEM;
3862 }
3863
Brian King0883e3b2009-02-04 16:13:12 -06003864 memset(tgt, 0, sizeof(*tgt));
Brian King072b91f2008-07-01 13:14:30 -05003865 tgt->scsi_id = scsi_id;
3866 tgt->new_scsi_id = scsi_id;
3867 tgt->vhost = vhost;
3868 tgt->need_login = 1;
Brian King10501e12009-03-20 15:44:39 -05003869 tgt->cancel_key = vhost->task_set++;
3870 init_timer(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05003871 kref_init(&tgt->kref);
3872 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3873 spin_lock_irqsave(vhost->host->host_lock, flags);
3874 list_add_tail(&tgt->queue, &vhost->targets);
3875
3876unlock_out:
3877 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3878 return 0;
3879}
3880
3881/**
3882 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3883 * @vhost: ibmvfc host struct
3884 *
3885 * Returns:
3886 * 0 on success / other on failure
3887 **/
3888static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3889{
3890 int i, rc;
3891
3892 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3893 rc = ibmvfc_alloc_target(vhost,
3894 vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3895
3896 return rc;
3897}
3898
3899/**
3900 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3901 * @evt: ibmvfc event struct
3902 *
3903 **/
3904static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3905{
3906 struct ibmvfc_host *vhost = evt->vhost;
3907 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3908 u32 mad_status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003909 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003910
3911 switch (mad_status) {
3912 case IBMVFC_MAD_SUCCESS:
3913 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3914 vhost->num_targets = rsp->num_written;
3915 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3916 break;
3917 case IBMVFC_MAD_FAILED:
Brian King7d0e4622009-05-28 16:17:26 -05003918 level += ibmvfc_retry_host_init(vhost);
3919 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
3920 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05003921 break;
3922 case IBMVFC_MAD_DRIVER_FAILED:
3923 break;
3924 default:
3925 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3926 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3927 break;
3928 }
3929
3930 ibmvfc_free_event(evt);
3931 wake_up(&vhost->work_wait_q);
3932}
3933
3934/**
3935 * ibmvfc_discover_targets - Send Discover Targets MAD
3936 * @vhost: ibmvfc host struct
3937 *
3938 **/
3939static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3940{
3941 struct ibmvfc_discover_targets *mad;
3942 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3943
3944 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3945 mad = &evt->iu.discover_targets;
3946 memset(mad, 0, sizeof(*mad));
3947 mad->common.version = 1;
3948 mad->common.opcode = IBMVFC_DISC_TARGETS;
3949 mad->common.length = sizeof(*mad);
3950 mad->bufflen = vhost->disc_buf_sz;
3951 mad->buffer.va = vhost->disc_buf_dma;
3952 mad->buffer.len = vhost->disc_buf_sz;
3953 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3954
3955 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3956 ibmvfc_dbg(vhost, "Sent discover targets\n");
3957 else
3958 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3959}
3960
3961/**
3962 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3963 * @evt: ibmvfc event struct
3964 *
3965 **/
3966static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3967{
3968 struct ibmvfc_host *vhost = evt->vhost;
3969 u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3970 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3971 unsigned int npiv_max_sectors;
Brian King7d0e4622009-05-28 16:17:26 -05003972 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003973
3974 switch (mad_status) {
3975 case IBMVFC_MAD_SUCCESS:
3976 ibmvfc_free_event(evt);
3977 break;
3978 case IBMVFC_MAD_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05003979 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05003980 level += ibmvfc_retry_host_init(vhost);
Brian King072b91f2008-07-01 13:14:30 -05003981 else
3982 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
Brian King7d0e4622009-05-28 16:17:26 -05003983 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
3984 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05003985 ibmvfc_free_event(evt);
3986 return;
3987 case IBMVFC_MAD_CRQ_ERROR:
3988 ibmvfc_retry_host_init(vhost);
3989 case IBMVFC_MAD_DRIVER_FAILED:
3990 ibmvfc_free_event(evt);
3991 return;
3992 default:
3993 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
3994 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3995 ibmvfc_free_event(evt);
3996 return;
3997 }
3998
3999 vhost->client_migrated = 0;
4000
4001 if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
4002 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4003 rsp->flags);
4004 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4005 wake_up(&vhost->work_wait_q);
4006 return;
4007 }
4008
4009 if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
4010 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4011 rsp->max_cmds);
4012 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4013 wake_up(&vhost->work_wait_q);
4014 return;
4015 }
4016
Brian King79111d02009-05-28 16:17:29 -05004017 vhost->logged_in = 1;
Brian King072b91f2008-07-01 13:14:30 -05004018 npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
4019 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4020 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4021 rsp->drc_name, npiv_max_sectors);
4022
4023 fc_host_fabric_name(vhost->host) = rsp->node_name;
4024 fc_host_node_name(vhost->host) = rsp->node_name;
4025 fc_host_port_name(vhost->host) = rsp->port_name;
4026 fc_host_port_id(vhost->host) = rsp->scsi_id;
4027 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4028 fc_host_supported_classes(vhost->host) = 0;
4029 if (rsp->service_parms.class1_parms[0] & 0x80000000)
4030 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4031 if (rsp->service_parms.class2_parms[0] & 0x80000000)
4032 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4033 if (rsp->service_parms.class3_parms[0] & 0x80000000)
4034 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4035 fc_host_maxframe_size(vhost->host) =
4036 rsp->service_parms.common.bb_rcv_sz & 0x0fff;
4037
4038 vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
4039 vhost->host->max_sectors = npiv_max_sectors;
4040 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4041 wake_up(&vhost->work_wait_q);
4042}
4043
4044/**
4045 * ibmvfc_npiv_login - Sends NPIV login
4046 * @vhost: ibmvfc host struct
4047 *
4048 **/
4049static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4050{
4051 struct ibmvfc_npiv_login_mad *mad;
4052 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4053
4054 ibmvfc_gather_partition_info(vhost);
4055 ibmvfc_set_login_info(vhost);
4056 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4057
4058 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4059 mad = &evt->iu.npiv_login;
4060 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4061 mad->common.version = 1;
4062 mad->common.opcode = IBMVFC_NPIV_LOGIN;
4063 mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
4064 mad->buffer.va = vhost->login_buf_dma;
4065 mad->buffer.len = sizeof(*vhost->login_buf);
4066
Brian King072b91f2008-07-01 13:14:30 -05004067 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4068
4069 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4070 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4071 else
4072 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4073};
4074
4075/**
Brian King79111d02009-05-28 16:17:29 -05004076 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4077 * @vhost: ibmvfc host struct
4078 *
4079 **/
4080static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4081{
4082 struct ibmvfc_host *vhost = evt->vhost;
4083 u32 mad_status = evt->xfer_iu->npiv_logout.common.status;
4084
4085 ibmvfc_free_event(evt);
4086
4087 switch (mad_status) {
4088 case IBMVFC_MAD_SUCCESS:
4089 if (list_empty(&vhost->sent) &&
4090 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
Brian King861890c2009-10-19 15:07:49 -05004091 ibmvfc_init_host(vhost);
Brian King79111d02009-05-28 16:17:29 -05004092 return;
4093 }
4094 break;
4095 case IBMVFC_MAD_FAILED:
4096 case IBMVFC_MAD_NOT_SUPPORTED:
4097 case IBMVFC_MAD_CRQ_ERROR:
4098 case IBMVFC_MAD_DRIVER_FAILED:
4099 default:
4100 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4101 break;
4102 }
4103
4104 ibmvfc_hard_reset_host(vhost);
4105}
4106
4107/**
4108 * ibmvfc_npiv_logout - Issue an NPIV Logout
4109 * @vhost: ibmvfc host struct
4110 *
4111 **/
4112static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4113{
4114 struct ibmvfc_npiv_logout_mad *mad;
4115 struct ibmvfc_event *evt;
4116
4117 evt = ibmvfc_get_event(vhost);
4118 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4119
4120 mad = &evt->iu.npiv_logout;
4121 memset(mad, 0, sizeof(*mad));
4122 mad->common.version = 1;
4123 mad->common.opcode = IBMVFC_NPIV_LOGOUT;
4124 mad->common.length = sizeof(struct ibmvfc_npiv_logout_mad);
4125
4126 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4127
4128 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4129 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4130 else
4131 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4132}
4133
4134/**
Brian King072b91f2008-07-01 13:14:30 -05004135 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4136 * @vhost: ibmvfc host struct
4137 *
4138 * Returns:
4139 * 1 if work to do / 0 if not
4140 **/
4141static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4142{
4143 struct ibmvfc_target *tgt;
4144
4145 list_for_each_entry(tgt, &vhost->targets, queue) {
4146 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4147 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4148 return 1;
4149 }
4150
4151 return 0;
4152}
4153
4154/**
4155 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4156 * @vhost: ibmvfc host struct
4157 *
4158 * Returns:
4159 * 1 if work to do / 0 if not
4160 **/
4161static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4162{
4163 struct ibmvfc_target *tgt;
4164
4165 if (kthread_should_stop())
4166 return 1;
4167 switch (vhost->action) {
4168 case IBMVFC_HOST_ACTION_NONE:
4169 case IBMVFC_HOST_ACTION_INIT_WAIT:
Brian King79111d02009-05-28 16:17:29 -05004170 case IBMVFC_HOST_ACTION_LOGO_WAIT:
Brian King072b91f2008-07-01 13:14:30 -05004171 return 0;
4172 case IBMVFC_HOST_ACTION_TGT_INIT:
4173 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4174 if (vhost->discovery_threads == disc_threads)
4175 return 0;
4176 list_for_each_entry(tgt, &vhost->targets, queue)
4177 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4178 return 1;
4179 list_for_each_entry(tgt, &vhost->targets, queue)
4180 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4181 return 0;
4182 return 1;
Brian King79111d02009-05-28 16:17:29 -05004183 case IBMVFC_HOST_ACTION_LOGO:
Brian King072b91f2008-07-01 13:14:30 -05004184 case IBMVFC_HOST_ACTION_INIT:
4185 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
Brian King072b91f2008-07-01 13:14:30 -05004186 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05004187 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05004188 case IBMVFC_HOST_ACTION_QUERY:
Brian King73ee5d82010-06-17 13:55:13 -05004189 case IBMVFC_HOST_ACTION_RESET:
4190 case IBMVFC_HOST_ACTION_REENABLE:
Brian King072b91f2008-07-01 13:14:30 -05004191 default:
4192 break;
4193 };
4194
4195 return 1;
4196}
4197
4198/**
4199 * ibmvfc_work_to_do - Is there task level work to do?
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 unsigned long flags;
4208 int rc;
4209
4210 spin_lock_irqsave(vhost->host->host_lock, flags);
4211 rc = __ibmvfc_work_to_do(vhost);
4212 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4213 return rc;
4214}
4215
4216/**
4217 * ibmvfc_log_ae - Log async events if necessary
4218 * @vhost: ibmvfc host struct
4219 * @events: events to log
4220 *
4221 **/
4222static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4223{
4224 if (events & IBMVFC_AE_RSCN)
4225 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4226 if ((events & IBMVFC_AE_LINKDOWN) &&
4227 vhost->state >= IBMVFC_HALTED)
4228 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4229 if ((events & IBMVFC_AE_LINKUP) &&
4230 vhost->state == IBMVFC_INITIALIZING)
4231 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4232}
4233
4234/**
4235 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4236 * @tgt: ibmvfc target struct
4237 *
4238 **/
4239static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4240{
4241 struct ibmvfc_host *vhost = tgt->vhost;
Brian King43c8da92009-05-28 16:17:28 -05004242 struct fc_rport *rport;
Brian King072b91f2008-07-01 13:14:30 -05004243 unsigned long flags;
4244
4245 tgt_dbg(tgt, "Adding rport\n");
4246 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4247 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King43c8da92009-05-28 16:17:28 -05004248
4249 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4250 tgt_dbg(tgt, "Deleting rport\n");
4251 list_del(&tgt->queue);
Brian Kingd5da3042010-08-05 16:38:31 -05004252 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
Brian King43c8da92009-05-28 16:17:28 -05004253 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4254 fc_remote_port_delete(rport);
4255 del_timer_sync(&tgt->timer);
4256 kref_put(&tgt->kref, ibmvfc_release_tgt);
4257 return;
Brian Kingd5da3042010-08-05 16:38:31 -05004258 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4259 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4260 return;
Brian King43c8da92009-05-28 16:17:28 -05004261 }
4262
Brian King072b91f2008-07-01 13:14:30 -05004263 if (rport) {
4264 tgt_dbg(tgt, "rport add succeeded\n");
Brian King43c8da92009-05-28 16:17:28 -05004265 tgt->rport = rport;
Brian King072b91f2008-07-01 13:14:30 -05004266 rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
4267 rport->supported_classes = 0;
Brian King52d7e862008-07-22 08:31:46 -05004268 tgt->target_id = rport->scsi_target_id;
Brian King072b91f2008-07-01 13:14:30 -05004269 if (tgt->service_parms.class1_parms[0] & 0x80000000)
4270 rport->supported_classes |= FC_COS_CLASS1;
4271 if (tgt->service_parms.class2_parms[0] & 0x80000000)
4272 rport->supported_classes |= FC_COS_CLASS2;
4273 if (tgt->service_parms.class3_parms[0] & 0x80000000)
4274 rport->supported_classes |= FC_COS_CLASS3;
Brian Kingd31429e2009-10-19 15:07:54 -05004275 if (rport->rqst_q)
Martin K. Petersen8a783622010-02-26 00:20:39 -05004276 blk_queue_max_segments(rport->rqst_q, 1);
Brian King072b91f2008-07-01 13:14:30 -05004277 } else
4278 tgt_dbg(tgt, "rport add failed\n");
4279 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4280}
4281
4282/**
4283 * ibmvfc_do_work - Do task level work
4284 * @vhost: ibmvfc host struct
4285 *
4286 **/
4287static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4288{
4289 struct ibmvfc_target *tgt;
4290 unsigned long flags;
4291 struct fc_rport *rport;
Brian King73ee5d82010-06-17 13:55:13 -05004292 int rc;
Brian King072b91f2008-07-01 13:14:30 -05004293
4294 ibmvfc_log_ae(vhost, vhost->events_to_log);
4295 spin_lock_irqsave(vhost->host->host_lock, flags);
4296 vhost->events_to_log = 0;
4297 switch (vhost->action) {
4298 case IBMVFC_HOST_ACTION_NONE:
Brian King79111d02009-05-28 16:17:29 -05004299 case IBMVFC_HOST_ACTION_LOGO_WAIT:
Brian King072b91f2008-07-01 13:14:30 -05004300 case IBMVFC_HOST_ACTION_INIT_WAIT:
4301 break;
Brian King73ee5d82010-06-17 13:55:13 -05004302 case IBMVFC_HOST_ACTION_RESET:
4303 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4304 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4305 rc = ibmvfc_reset_crq(vhost);
4306 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian Kingd24099d2010-09-21 10:17:11 -05004307 if (rc == H_CLOSED)
4308 vio_enable_interrupts(to_vio_dev(vhost->dev));
4309 else if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4310 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
Brian King73ee5d82010-06-17 13:55:13 -05004311 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4312 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4313 }
4314 break;
4315 case IBMVFC_HOST_ACTION_REENABLE:
4316 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4317 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4318 rc = ibmvfc_reenable_crq_queue(vhost);
4319 spin_lock_irqsave(vhost->host->host_lock, flags);
4320 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4321 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4322 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4323 }
4324 break;
Brian King79111d02009-05-28 16:17:29 -05004325 case IBMVFC_HOST_ACTION_LOGO:
4326 vhost->job_step(vhost);
4327 break;
Brian King072b91f2008-07-01 13:14:30 -05004328 case IBMVFC_HOST_ACTION_INIT:
4329 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
Brian King1c41fa822008-12-03 11:02:54 -06004330 if (vhost->delay_init) {
4331 vhost->delay_init = 0;
4332 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian Kingd2131b32008-12-18 09:26:51 -06004333 ssleep(15);
Brian King1c41fa822008-12-03 11:02:54 -06004334 return;
4335 } else
4336 vhost->job_step(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004337 break;
4338 case IBMVFC_HOST_ACTION_QUERY:
4339 list_for_each_entry(tgt, &vhost->targets, queue)
4340 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4341 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4342 break;
4343 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4344 list_for_each_entry(tgt, &vhost->targets, queue) {
4345 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4346 tgt->job_step(tgt);
4347 break;
4348 }
4349 }
4350
4351 if (!ibmvfc_dev_init_to_do(vhost))
4352 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4353 break;
4354 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05004355 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05004356 list_for_each_entry(tgt, &vhost->targets, queue) {
4357 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4358 tgt_dbg(tgt, "Deleting rport\n");
4359 rport = tgt->rport;
4360 tgt->rport = NULL;
4361 list_del(&tgt->queue);
Brian Kingd5da3042010-08-05 16:38:31 -05004362 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
Brian King072b91f2008-07-01 13:14:30 -05004363 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4364 if (rport)
4365 fc_remote_port_delete(rport);
Brian King10501e12009-03-20 15:44:39 -05004366 del_timer_sync(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05004367 kref_put(&tgt->kref, ibmvfc_release_tgt);
4368 return;
4369 }
4370 }
4371
4372 if (vhost->state == IBMVFC_INITIALIZING) {
Brian King10e79492008-10-29 08:46:45 -05004373 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
Brian King43c8da92009-05-28 16:17:28 -05004374 if (vhost->reinit) {
4375 vhost->reinit = 0;
4376 scsi_block_requests(vhost->host);
4377 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4378 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4379 } else {
4380 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4381 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4382 wake_up(&vhost->init_wait_q);
4383 schedule_work(&vhost->rport_add_work_q);
4384 vhost->init_retries = 0;
4385 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4386 scsi_unblock_requests(vhost->host);
4387 }
4388
Brian King10e79492008-10-29 08:46:45 -05004389 return;
4390 } else {
4391 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4392 vhost->job_step = ibmvfc_discover_targets;
4393 }
Brian King072b91f2008-07-01 13:14:30 -05004394 } else {
4395 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4396 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4397 scsi_unblock_requests(vhost->host);
4398 wake_up(&vhost->init_wait_q);
4399 return;
4400 }
4401 break;
4402 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4403 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4404 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4405 ibmvfc_alloc_targets(vhost);
4406 spin_lock_irqsave(vhost->host->host_lock, flags);
4407 break;
4408 case IBMVFC_HOST_ACTION_TGT_INIT:
4409 list_for_each_entry(tgt, &vhost->targets, queue) {
4410 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4411 tgt->job_step(tgt);
4412 break;
4413 }
4414 }
4415
Brian King10e79492008-10-29 08:46:45 -05004416 if (!ibmvfc_dev_init_to_do(vhost))
4417 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
Brian King072b91f2008-07-01 13:14:30 -05004418 break;
Brian King072b91f2008-07-01 13:14:30 -05004419 default:
4420 break;
4421 };
4422
4423 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4424}
4425
4426/**
4427 * ibmvfc_work - Do task level work
4428 * @data: ibmvfc host struct
4429 *
4430 * Returns:
4431 * zero
4432 **/
4433static int ibmvfc_work(void *data)
4434{
4435 struct ibmvfc_host *vhost = data;
4436 int rc;
4437
4438 set_user_nice(current, -20);
4439
4440 while (1) {
4441 rc = wait_event_interruptible(vhost->work_wait_q,
4442 ibmvfc_work_to_do(vhost));
4443
4444 BUG_ON(rc);
4445
4446 if (kthread_should_stop())
4447 break;
4448
4449 ibmvfc_do_work(vhost);
4450 }
4451
4452 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4453 return 0;
4454}
4455
4456/**
4457 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4458 * @vhost: ibmvfc host struct
4459 *
4460 * Allocates a page for messages, maps it for dma, and registers
4461 * the crq with the hypervisor.
4462 *
4463 * Return value:
4464 * zero on success / other on failure
4465 **/
4466static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4467{
4468 int rc, retrc = -ENOMEM;
4469 struct device *dev = vhost->dev;
4470 struct vio_dev *vdev = to_vio_dev(dev);
4471 struct ibmvfc_crq_queue *crq = &vhost->crq;
4472
4473 ENTER;
4474 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4475
4476 if (!crq->msgs)
4477 return -ENOMEM;
4478
4479 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4480 crq->msg_token = dma_map_single(dev, crq->msgs,
4481 PAGE_SIZE, DMA_BIDIRECTIONAL);
4482
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07004483 if (dma_mapping_error(dev, crq->msg_token))
Brian King072b91f2008-07-01 13:14:30 -05004484 goto map_failed;
4485
4486 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4487 crq->msg_token, PAGE_SIZE);
4488
4489 if (rc == H_RESOURCE)
4490 /* maybe kexecing and resource is busy. try a reset */
4491 retrc = rc = ibmvfc_reset_crq(vhost);
4492
4493 if (rc == H_CLOSED)
4494 dev_warn(dev, "Partner adapter not ready\n");
4495 else if (rc) {
4496 dev_warn(dev, "Error %d opening adapter\n", rc);
4497 goto reg_crq_failed;
4498 }
4499
4500 retrc = 0;
4501
Brian King039a0892009-03-20 15:44:35 -05004502 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4503
Brian King072b91f2008-07-01 13:14:30 -05004504 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4505 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4506 goto req_irq_failed;
4507 }
4508
4509 if ((rc = vio_enable_interrupts(vdev))) {
4510 dev_err(dev, "Error %d enabling interrupts\n", rc);
4511 goto req_irq_failed;
4512 }
4513
4514 crq->cur = 0;
4515 LEAVE;
4516 return retrc;
4517
4518req_irq_failed:
Brian King039a0892009-03-20 15:44:35 -05004519 tasklet_kill(&vhost->tasklet);
Brian King072b91f2008-07-01 13:14:30 -05004520 do {
4521 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4522 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4523reg_crq_failed:
4524 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4525map_failed:
4526 free_page((unsigned long)crq->msgs);
4527 return retrc;
4528}
4529
4530/**
4531 * ibmvfc_free_mem - Free memory for vhost
4532 * @vhost: ibmvfc host struct
4533 *
4534 * Return value:
4535 * none
4536 **/
4537static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4538{
4539 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4540
4541 ENTER;
4542 mempool_destroy(vhost->tgt_pool);
4543 kfree(vhost->trace);
4544 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4545 vhost->disc_buf_dma);
4546 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4547 vhost->login_buf, vhost->login_buf_dma);
4548 dma_pool_destroy(vhost->sg_pool);
4549 dma_unmap_single(vhost->dev, async_q->msg_token,
4550 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4551 free_page((unsigned long)async_q->msgs);
4552 LEAVE;
4553}
4554
4555/**
4556 * ibmvfc_alloc_mem - Allocate memory for vhost
4557 * @vhost: ibmvfc host struct
4558 *
4559 * Return value:
4560 * 0 on success / non-zero on failure
4561 **/
4562static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4563{
4564 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4565 struct device *dev = vhost->dev;
4566
4567 ENTER;
4568 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4569 if (!async_q->msgs) {
4570 dev_err(dev, "Couldn't allocate async queue.\n");
4571 goto nomem;
4572 }
4573
4574 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4575 async_q->msg_token = dma_map_single(dev, async_q->msgs,
4576 async_q->size * sizeof(*async_q->msgs),
4577 DMA_BIDIRECTIONAL);
4578
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07004579 if (dma_mapping_error(dev, async_q->msg_token)) {
Brian King072b91f2008-07-01 13:14:30 -05004580 dev_err(dev, "Failed to map async queue\n");
4581 goto free_async_crq;
4582 }
4583
4584 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4585 SG_ALL * sizeof(struct srp_direct_buf),
4586 sizeof(struct srp_direct_buf), 0);
4587
4588 if (!vhost->sg_pool) {
4589 dev_err(dev, "Failed to allocate sg pool\n");
4590 goto unmap_async_crq;
4591 }
4592
4593 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4594 &vhost->login_buf_dma, GFP_KERNEL);
4595
4596 if (!vhost->login_buf) {
4597 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4598 goto free_sg_pool;
4599 }
4600
4601 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
4602 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
4603 &vhost->disc_buf_dma, GFP_KERNEL);
4604
4605 if (!vhost->disc_buf) {
4606 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
4607 goto free_login_buffer;
4608 }
4609
4610 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
4611 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
4612
4613 if (!vhost->trace)
4614 goto free_disc_buffer;
4615
Sage Weild6886692009-08-03 13:54:47 -07004616 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
Brian King072b91f2008-07-01 13:14:30 -05004617 sizeof(struct ibmvfc_target));
4618
4619 if (!vhost->tgt_pool) {
4620 dev_err(dev, "Couldn't allocate target memory pool\n");
4621 goto free_trace;
4622 }
4623
4624 LEAVE;
4625 return 0;
4626
4627free_trace:
4628 kfree(vhost->trace);
4629free_disc_buffer:
4630 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
4631 vhost->disc_buf_dma);
4632free_login_buffer:
4633 dma_free_coherent(dev, sizeof(*vhost->login_buf),
4634 vhost->login_buf, vhost->login_buf_dma);
4635free_sg_pool:
4636 dma_pool_destroy(vhost->sg_pool);
4637unmap_async_crq:
4638 dma_unmap_single(dev, async_q->msg_token,
4639 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4640free_async_crq:
4641 free_page((unsigned long)async_q->msgs);
4642nomem:
4643 LEAVE;
4644 return -ENOMEM;
4645}
4646
4647/**
Brian King43c8da92009-05-28 16:17:28 -05004648 * ibmvfc_rport_add_thread - Worker thread for rport adds
4649 * @work: work struct
4650 *
4651 **/
4652static void ibmvfc_rport_add_thread(struct work_struct *work)
4653{
4654 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
4655 rport_add_work_q);
4656 struct ibmvfc_target *tgt;
4657 struct fc_rport *rport;
4658 unsigned long flags;
4659 int did_work;
4660
4661 ENTER;
4662 spin_lock_irqsave(vhost->host->host_lock, flags);
4663 do {
4664 did_work = 0;
4665 if (vhost->state != IBMVFC_ACTIVE)
4666 break;
4667
4668 list_for_each_entry(tgt, &vhost->targets, queue) {
4669 if (tgt->add_rport) {
4670 did_work = 1;
4671 tgt->add_rport = 0;
4672 kref_get(&tgt->kref);
4673 rport = tgt->rport;
4674 if (!rport) {
4675 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4676 ibmvfc_tgt_add_rport(tgt);
4677 } else if (get_device(&rport->dev)) {
4678 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4679 tgt_dbg(tgt, "Setting rport roles\n");
4680 fc_remote_port_rolechg(rport, tgt->ids.roles);
4681 put_device(&rport->dev);
4682 }
4683
4684 kref_put(&tgt->kref, ibmvfc_release_tgt);
4685 spin_lock_irqsave(vhost->host->host_lock, flags);
4686 break;
4687 }
4688 }
4689 } while(did_work);
4690
4691 if (vhost->state == IBMVFC_ACTIVE)
4692 vhost->scan_complete = 1;
4693 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4694 LEAVE;
4695}
4696
4697/**
Brian King072b91f2008-07-01 13:14:30 -05004698 * ibmvfc_probe - Adapter hot plug add entry point
4699 * @vdev: vio device struct
4700 * @id: vio device id struct
4701 *
4702 * Return value:
4703 * 0 on success / non-zero on failure
4704 **/
4705static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4706{
4707 struct ibmvfc_host *vhost;
4708 struct Scsi_Host *shost;
4709 struct device *dev = &vdev->dev;
4710 int rc = -ENOMEM;
4711
4712 ENTER;
4713 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
4714 if (!shost) {
4715 dev_err(dev, "Couldn't allocate host data\n");
4716 goto out;
4717 }
4718
4719 shost->transportt = ibmvfc_transport_template;
4720 shost->can_queue = max_requests;
4721 shost->max_lun = max_lun;
4722 shost->max_id = max_targets;
4723 shost->max_sectors = IBMVFC_MAX_SECTORS;
4724 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
4725 shost->unique_id = shost->host_no;
4726
4727 vhost = shost_priv(shost);
4728 INIT_LIST_HEAD(&vhost->sent);
4729 INIT_LIST_HEAD(&vhost->free);
4730 INIT_LIST_HEAD(&vhost->targets);
4731 sprintf(vhost->name, IBMVFC_NAME);
4732 vhost->host = shost;
4733 vhost->dev = dev;
4734 vhost->partition_number = -1;
4735 vhost->log_level = log_level;
Brian King10501e12009-03-20 15:44:39 -05004736 vhost->task_set = 1;
Brian King072b91f2008-07-01 13:14:30 -05004737 strcpy(vhost->partition_name, "UNKNOWN");
4738 init_waitqueue_head(&vhost->work_wait_q);
4739 init_waitqueue_head(&vhost->init_wait_q);
Brian King43c8da92009-05-28 16:17:28 -05004740 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
Brian Kingd31429e2009-10-19 15:07:54 -05004741 mutex_init(&vhost->passthru_mutex);
Brian King072b91f2008-07-01 13:14:30 -05004742
4743 if ((rc = ibmvfc_alloc_mem(vhost)))
4744 goto free_scsi_host;
4745
4746 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
4747 shost->host_no);
4748
4749 if (IS_ERR(vhost->work_thread)) {
4750 dev_err(dev, "Couldn't create kernel thread: %ld\n",
4751 PTR_ERR(vhost->work_thread));
4752 goto free_host_mem;
4753 }
4754
4755 if ((rc = ibmvfc_init_crq(vhost))) {
4756 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4757 goto kill_kthread;
4758 }
4759
4760 if ((rc = ibmvfc_init_event_pool(vhost))) {
4761 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4762 goto release_crq;
4763 }
4764
4765 if ((rc = scsi_add_host(shost, dev)))
4766 goto release_event_pool;
4767
Mike Christiea5110f22010-09-15 16:52:29 -05004768 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
4769
Brian King072b91f2008-07-01 13:14:30 -05004770 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4771 &ibmvfc_trace_attr))) {
4772 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4773 goto remove_shost;
4774 }
4775
Brian Kingd31429e2009-10-19 15:07:54 -05004776 if (shost_to_fc_host(shost)->rqst_q)
Martin K. Petersen8a783622010-02-26 00:20:39 -05004777 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
Brian King072b91f2008-07-01 13:14:30 -05004778 dev_set_drvdata(dev, vhost);
4779 spin_lock(&ibmvfc_driver_lock);
4780 list_add_tail(&vhost->queue, &ibmvfc_head);
4781 spin_unlock(&ibmvfc_driver_lock);
4782
4783 ibmvfc_send_crq_init(vhost);
4784 scsi_scan_host(shost);
4785 return 0;
4786
4787remove_shost:
4788 scsi_remove_host(shost);
4789release_event_pool:
4790 ibmvfc_free_event_pool(vhost);
4791release_crq:
4792 ibmvfc_release_crq_queue(vhost);
4793kill_kthread:
4794 kthread_stop(vhost->work_thread);
4795free_host_mem:
4796 ibmvfc_free_mem(vhost);
4797free_scsi_host:
4798 scsi_host_put(shost);
4799out:
4800 LEAVE;
4801 return rc;
4802}
4803
4804/**
4805 * ibmvfc_remove - Adapter hot plug remove entry point
4806 * @vdev: vio device struct
4807 *
4808 * Return value:
4809 * 0
4810 **/
4811static int ibmvfc_remove(struct vio_dev *vdev)
4812{
4813 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4814 unsigned long flags;
4815
4816 ENTER;
4817 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
Brian King70431102009-10-19 15:07:48 -05004818
4819 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King2d0da2a2008-07-22 08:31:42 -05004820 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
Brian King70431102009-10-19 15:07:48 -05004821 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4822
Brian King2d0da2a2008-07-22 08:31:42 -05004823 ibmvfc_wait_while_resetting(vhost);
4824 ibmvfc_release_crq_queue(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004825 kthread_stop(vhost->work_thread);
4826 fc_remove_host(vhost->host);
4827 scsi_remove_host(vhost->host);
Brian King072b91f2008-07-01 13:14:30 -05004828
4829 spin_lock_irqsave(vhost->host->host_lock, flags);
4830 ibmvfc_purge_requests(vhost, DID_ERROR);
4831 ibmvfc_free_event_pool(vhost);
4832 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4833
4834 ibmvfc_free_mem(vhost);
4835 spin_lock(&ibmvfc_driver_lock);
4836 list_del(&vhost->queue);
4837 spin_unlock(&ibmvfc_driver_lock);
4838 scsi_host_put(vhost->host);
4839 LEAVE;
4840 return 0;
4841}
4842
Brian King39c1ffe2008-07-24 04:35:48 +10004843/**
Brian Kingb0f4d4c2010-02-21 10:37:58 -06004844 * ibmvfc_resume - Resume from suspend
4845 * @dev: device struct
4846 *
4847 * We may have lost an interrupt across suspend/resume, so kick the
4848 * interrupt handler
4849 *
4850 */
4851static int ibmvfc_resume(struct device *dev)
4852{
4853 unsigned long flags;
4854 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
4855 struct vio_dev *vdev = to_vio_dev(dev);
4856
4857 spin_lock_irqsave(vhost->host->host_lock, flags);
4858 vio_disable_interrupts(vdev);
4859 tasklet_schedule(&vhost->tasklet);
4860 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4861 return 0;
4862}
4863
4864/**
Brian King39c1ffe2008-07-24 04:35:48 +10004865 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4866 * @vdev: vio device struct
4867 *
4868 * Return value:
4869 * Number of bytes the driver will need to DMA map at the same time in
4870 * order to perform well.
4871 */
4872static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4873{
4874 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4875 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4876}
4877
Brian King072b91f2008-07-01 13:14:30 -05004878static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
4879 {"fcp", "IBM,vfc-client"},
4880 { "", "" }
4881};
4882MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4883
Brian Kingb0f4d4c2010-02-21 10:37:58 -06004884static struct dev_pm_ops ibmvfc_pm_ops = {
4885 .resume = ibmvfc_resume
4886};
4887
Brian King072b91f2008-07-01 13:14:30 -05004888static struct vio_driver ibmvfc_driver = {
4889 .id_table = ibmvfc_device_table,
4890 .probe = ibmvfc_probe,
4891 .remove = ibmvfc_remove,
Brian King39c1ffe2008-07-24 04:35:48 +10004892 .get_desired_dma = ibmvfc_get_desired_dma,
Brian King072b91f2008-07-01 13:14:30 -05004893 .driver = {
4894 .name = IBMVFC_NAME,
4895 .owner = THIS_MODULE,
Brian Kingb0f4d4c2010-02-21 10:37:58 -06004896 .pm = &ibmvfc_pm_ops,
Brian King072b91f2008-07-01 13:14:30 -05004897 }
4898};
4899
4900static struct fc_function_template ibmvfc_transport_functions = {
4901 .show_host_fabric_name = 1,
4902 .show_host_node_name = 1,
4903 .show_host_port_name = 1,
4904 .show_host_supported_classes = 1,
4905 .show_host_port_type = 1,
4906 .show_host_port_id = 1,
Brian King9ab36102009-03-20 15:44:38 -05004907 .show_host_maxframe_size = 1,
Brian King072b91f2008-07-01 13:14:30 -05004908
4909 .get_host_port_state = ibmvfc_get_host_port_state,
4910 .show_host_port_state = 1,
4911
4912 .get_host_speed = ibmvfc_get_host_speed,
4913 .show_host_speed = 1,
4914
4915 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4916 .terminate_rport_io = ibmvfc_terminate_rport_io,
4917
4918 .show_rport_maxframe_size = 1,
4919 .show_rport_supported_classes = 1,
4920
4921 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4922 .show_rport_dev_loss_tmo = 1,
4923
4924 .get_starget_node_name = ibmvfc_get_starget_node_name,
4925 .show_starget_node_name = 1,
4926
4927 .get_starget_port_name = ibmvfc_get_starget_port_name,
4928 .show_starget_port_name = 1,
4929
4930 .get_starget_port_id = ibmvfc_get_starget_port_id,
4931 .show_starget_port_id = 1,
Brian Kingd31429e2009-10-19 15:07:54 -05004932
4933 .bsg_request = ibmvfc_bsg_request,
4934 .bsg_timeout = ibmvfc_bsg_timeout,
Brian King072b91f2008-07-01 13:14:30 -05004935};
4936
4937/**
4938 * ibmvfc_module_init - Initialize the ibmvfc module
4939 *
4940 * Return value:
4941 * 0 on success / other on failure
4942 **/
4943static int __init ibmvfc_module_init(void)
4944{
4945 int rc;
4946
4947 if (!firmware_has_feature(FW_FEATURE_VIO))
4948 return -ENODEV;
4949
4950 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4951 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4952
4953 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4954 if (!ibmvfc_transport_template)
4955 return -ENOMEM;
4956
4957 rc = vio_register_driver(&ibmvfc_driver);
4958 if (rc)
4959 fc_release_transport(ibmvfc_transport_template);
4960 return rc;
4961}
4962
4963/**
4964 * ibmvfc_module_exit - Teardown the ibmvfc module
4965 *
4966 * Return value:
4967 * nothing
4968 **/
4969static void __exit ibmvfc_module_exit(void)
4970{
4971 vio_unregister_driver(&ibmvfc_driver);
4972 fc_release_transport(ibmvfc_transport_template);
4973}
4974
4975module_init(ibmvfc_module_init);
4976module_exit(ibmvfc_module_exit);