blob: 4f73daccc9f1849e28bb75a369accbbad27e9d77 [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/**
Brian Kinga077c7f2012-08-24 16:50:59 -05002245 * ibmvfc_match_evt - Match function for specified event
2246 * @evt: ibmvfc event struct
2247 * @match: event to match
2248 *
2249 * Returns:
2250 * 1 if event matches key / 0 if event does not match key
2251 **/
2252static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2253{
2254 if (evt == match)
2255 return 1;
2256 return 0;
2257}
2258
2259/**
Brian Kingd2fab5c2010-08-05 16:38:34 -05002260 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2261 * @sdev: scsi device to abort commands
2262 *
2263 * This sends an Abort Task Set to the VIOS for the specified device. This does
2264 * NOT send any cancel to the VIOS. That must be done separately.
2265 *
2266 * Returns:
2267 * 0 on success / other on failure
2268 **/
2269static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2270{
2271 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2272 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2273 struct ibmvfc_cmd *tmf;
2274 struct ibmvfc_event *evt, *found_evt;
2275 union ibmvfc_iu rsp_iu;
2276 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2277 int rc, rsp_rc = -EBUSY;
2278 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2279 int rsp_code = 0;
2280
2281 spin_lock_irqsave(vhost->host->host_lock, flags);
2282 found_evt = NULL;
2283 list_for_each_entry(evt, &vhost->sent, queue) {
2284 if (evt->cmnd && evt->cmnd->device == sdev) {
2285 found_evt = evt;
2286 break;
2287 }
2288 }
2289
2290 if (!found_evt) {
2291 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2292 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2293 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2294 return 0;
2295 }
2296
2297 if (vhost->state == IBMVFC_ACTIVE) {
2298 evt = ibmvfc_get_event(vhost);
2299 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2300
2301 tmf = &evt->iu.cmd;
2302 memset(tmf, 0, sizeof(*tmf));
2303 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
2304 tmf->resp.len = sizeof(tmf->rsp);
2305 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
2306 tmf->payload_len = sizeof(tmf->iu);
2307 tmf->resp_len = sizeof(tmf->rsp);
2308 tmf->cancel_key = (unsigned long)sdev->hostdata;
2309 tmf->tgt_scsi_id = rport->port_id;
2310 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2311 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
2312 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2313 evt->sync_iu = &rsp_iu;
2314
2315 init_completion(&evt->comp);
2316 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2317 }
2318
2319 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2320
2321 if (rsp_rc != 0) {
2322 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2323 return -EIO;
2324 }
2325
2326 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2327 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2328
2329 if (!timeout) {
2330 rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2331 if (!rc) {
2332 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2333 if (rc == SUCCESS)
2334 rc = 0;
2335 }
2336
2337 if (rc) {
2338 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2339 ibmvfc_reset_host(vhost);
Brian Kinga077c7f2012-08-24 16:50:59 -05002340 rsp_rc = -EIO;
2341 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2342
2343 if (rc == SUCCESS)
2344 rsp_rc = 0;
2345
2346 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2347 if (rc != SUCCESS) {
2348 spin_lock_irqsave(vhost->host->host_lock, flags);
2349 ibmvfc_hard_reset_host(vhost);
2350 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2351 rsp_rc = 0;
2352 }
2353
Brian Kingd2fab5c2010-08-05 16:38:34 -05002354 goto out;
2355 }
2356 }
2357
2358 if (rsp_iu.cmd.status)
2359 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2360
2361 if (rsp_code) {
2362 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2363 rsp_code = fc_rsp->data.info.rsp_code;
2364
2365 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2366 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2367 ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
2368 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
2369 fc_rsp->scsi_status);
2370 rsp_rc = -EIO;
2371 } else
2372 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2373
2374out:
2375 spin_lock_irqsave(vhost->host->host_lock, flags);
2376 ibmvfc_free_event(evt);
2377 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2378 return rsp_rc;
2379}
2380
2381/**
Brian King072b91f2008-07-01 13:14:30 -05002382 * ibmvfc_eh_abort_handler - Abort a command
2383 * @cmd: scsi command to abort
2384 *
2385 * Returns:
2386 * SUCCESS / FAILED
2387 **/
2388static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2389{
Brian Kingad8dcff2008-10-29 08:46:41 -05002390 struct scsi_device *sdev = cmd->device;
2391 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King072b91f2008-07-01 13:14:30 -05002392 int cancel_rc, abort_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002393 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002394
2395 ENTER;
Brian King3f014242010-06-17 13:55:15 -05002396 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002397 ibmvfc_wait_while_resetting(vhost);
Brian Kingad8dcff2008-10-29 08:46:41 -05002398 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2399 abort_rc = ibmvfc_abort_task_set(sdev);
Brian King072b91f2008-07-01 13:14:30 -05002400
Brian Kingad8dcff2008-10-29 08:46:41 -05002401 if (!cancel_rc && !abort_rc)
2402 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05002403
2404 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002405 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002406}
2407
2408/**
2409 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2410 * @cmd: scsi command struct
2411 *
2412 * Returns:
2413 * SUCCESS / FAILED
2414 **/
2415static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2416{
Brian Kingad8dcff2008-10-29 08:46:41 -05002417 struct scsi_device *sdev = cmd->device;
2418 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King072b91f2008-07-01 13:14:30 -05002419 int cancel_rc, reset_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002420 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002421
2422 ENTER;
Brian King3f014242010-06-17 13:55:15 -05002423 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002424 ibmvfc_wait_while_resetting(vhost);
Brian Kingad8dcff2008-10-29 08:46:41 -05002425 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2426 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
Brian King072b91f2008-07-01 13:14:30 -05002427
Brian Kingad8dcff2008-10-29 08:46:41 -05002428 if (!cancel_rc && !reset_rc)
2429 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05002430
2431 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002432 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002433}
2434
2435/**
Brian King4a5c4a52009-10-19 15:07:53 -05002436 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2437 * @sdev: scsi device struct
2438 * @data: return code
2439 *
2440 **/
2441static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
Brian King072b91f2008-07-01 13:14:30 -05002442{
2443 unsigned long *rc = data;
2444 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2445}
2446
2447/**
Brian King072b91f2008-07-01 13:14:30 -05002448 * ibmvfc_eh_target_reset_handler - Reset the target
2449 * @cmd: scsi command struct
2450 *
2451 * Returns:
2452 * SUCCESS / FAILED
2453 **/
2454static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2455{
Brian Kingad8dcff2008-10-29 08:46:41 -05002456 struct scsi_device *sdev = cmd->device;
2457 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2458 struct scsi_target *starget = scsi_target(sdev);
Brian King072b91f2008-07-01 13:14:30 -05002459 int reset_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002460 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002461 unsigned long cancel_rc = 0;
Brian King072b91f2008-07-01 13:14:30 -05002462
2463 ENTER;
Brian King3f014242010-06-17 13:55:15 -05002464 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002465 ibmvfc_wait_while_resetting(vhost);
Brian King4a5c4a52009-10-19 15:07:53 -05002466 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
Brian Kingad8dcff2008-10-29 08:46:41 -05002467 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
Brian King072b91f2008-07-01 13:14:30 -05002468
Brian Kingad8dcff2008-10-29 08:46:41 -05002469 if (!cancel_rc && !reset_rc)
2470 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
Brian King072b91f2008-07-01 13:14:30 -05002471
2472 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002473 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002474}
2475
2476/**
2477 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2478 * @cmd: struct scsi_cmnd having problems
2479 *
2480 **/
2481static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2482{
2483 int rc;
2484 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2485
Brian King3f014242010-06-17 13:55:15 -05002486 fc_block_scsi_eh(cmd);
Brian King072b91f2008-07-01 13:14:30 -05002487 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2488 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2489 return rc ? FAILED : SUCCESS;
2490}
2491
2492/**
2493 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2494 * @rport: rport struct
2495 *
2496 * Return value:
2497 * none
2498 **/
2499static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2500{
Brian Kingd2fab5c2010-08-05 16:38:34 -05002501 struct Scsi_Host *shost = rport_to_shost(rport);
Brian King072b91f2008-07-01 13:14:30 -05002502 struct ibmvfc_host *vhost = shost_priv(shost);
Brian Kingd2fab5c2010-08-05 16:38:34 -05002503 struct fc_rport *dev_rport;
2504 struct scsi_device *sdev;
2505 unsigned long rc;
Brian King072b91f2008-07-01 13:14:30 -05002506
2507 ENTER;
Brian Kingd2fab5c2010-08-05 16:38:34 -05002508 shost_for_each_device(sdev, shost) {
2509 dev_rport = starget_to_rport(scsi_target(sdev));
2510 if (dev_rport != rport)
2511 continue;
2512 ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2513 ibmvfc_abort_task_set(sdev);
2514 }
Brian King072b91f2008-07-01 13:14:30 -05002515
Brian Kingd2fab5c2010-08-05 16:38:34 -05002516 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
Brian Kingad8dcff2008-10-29 08:46:41 -05002517
2518 if (rc == FAILED)
Brian King072b91f2008-07-01 13:14:30 -05002519 ibmvfc_issue_fc_host_lip(shost);
Brian King072b91f2008-07-01 13:14:30 -05002520 LEAVE;
2521}
2522
Brian Kingd99e5f42010-09-09 16:20:42 -05002523static const struct ibmvfc_async_desc ae_desc [] = {
Robert Jennings402c6ee2010-12-09 14:03:59 -06002524 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2525 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2526 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2527 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2528 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2529 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
2530 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
2531 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
2532 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
2533 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
2534 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
2535 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
2536 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
Brian King072b91f2008-07-01 13:14:30 -05002537};
2538
Brian Kingd99e5f42010-09-09 16:20:42 -05002539static const struct ibmvfc_async_desc unknown_ae = {
Robert Jennings402c6ee2010-12-09 14:03:59 -06002540 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
Brian Kingd99e5f42010-09-09 16:20:42 -05002541};
Brian King072b91f2008-07-01 13:14:30 -05002542
2543/**
2544 * ibmvfc_get_ae_desc - Get text description for async event
2545 * @ae: async event
2546 *
2547 **/
Brian Kingd99e5f42010-09-09 16:20:42 -05002548static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
Brian King072b91f2008-07-01 13:14:30 -05002549{
2550 int i;
2551
2552 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2553 if (ae_desc[i].ae == ae)
Brian Kingd99e5f42010-09-09 16:20:42 -05002554 return &ae_desc[i];
Brian King072b91f2008-07-01 13:14:30 -05002555
Brian Kingd99e5f42010-09-09 16:20:42 -05002556 return &unknown_ae;
2557}
2558
2559static const struct {
2560 enum ibmvfc_ae_link_state state;
2561 const char *desc;
2562} link_desc [] = {
2563 { IBMVFC_AE_LS_LINK_UP, " link up" },
2564 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
2565 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
2566 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
2567};
2568
2569/**
2570 * ibmvfc_get_link_state - Get text description for link state
2571 * @state: link state
2572 *
2573 **/
2574static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2575{
2576 int i;
2577
2578 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2579 if (link_desc[i].state == state)
2580 return link_desc[i].desc;
2581
2582 return "";
Brian King072b91f2008-07-01 13:14:30 -05002583}
2584
2585/**
2586 * ibmvfc_handle_async - Handle an async event from the adapter
2587 * @crq: crq to process
2588 * @vhost: ibmvfc host struct
2589 *
2590 **/
2591static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2592 struct ibmvfc_host *vhost)
2593{
Brian Kingd99e5f42010-09-09 16:20:42 -05002594 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(crq->event);
Brian King6d29cc52009-05-28 16:17:33 -05002595 struct ibmvfc_target *tgt;
Brian King072b91f2008-07-01 13:14:30 -05002596
Brian Kingd99e5f42010-09-09 16:20:42 -05002597 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2598 " node_name: %llx%s\n", desc->desc, crq->scsi_id, crq->wwpn, crq->node_name,
2599 ibmvfc_get_link_state(crq->link_state));
Brian King072b91f2008-07-01 13:14:30 -05002600
2601 switch (crq->event) {
Brian King072b91f2008-07-01 13:14:30 -05002602 case IBMVFC_AE_RESUME:
Brian King497f9c52009-05-28 16:17:30 -05002603 switch (crq->link_state) {
2604 case IBMVFC_AE_LS_LINK_DOWN:
2605 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2606 break;
2607 case IBMVFC_AE_LS_LINK_DEAD:
2608 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2609 break;
2610 case IBMVFC_AE_LS_LINK_UP:
2611 case IBMVFC_AE_LS_LINK_BOUNCED:
2612 default:
2613 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2614 vhost->delay_init = 1;
2615 __ibmvfc_reset_host(vhost);
2616 break;
2617 };
2618
2619 break;
2620 case IBMVFC_AE_LINK_UP:
Brian King072b91f2008-07-01 13:14:30 -05002621 vhost->events_to_log |= IBMVFC_AE_LINKUP;
Brian Kingd2131b32008-12-18 09:26:51 -06002622 vhost->delay_init = 1;
2623 __ibmvfc_reset_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002624 break;
2625 case IBMVFC_AE_SCN_FABRIC:
Brian Kingd2131b32008-12-18 09:26:51 -06002626 case IBMVFC_AE_SCN_DOMAIN:
Brian King072b91f2008-07-01 13:14:30 -05002627 vhost->events_to_log |= IBMVFC_AE_RSCN;
Brian Kingd2131b32008-12-18 09:26:51 -06002628 vhost->delay_init = 1;
2629 __ibmvfc_reset_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002630 break;
2631 case IBMVFC_AE_SCN_NPORT:
2632 case IBMVFC_AE_SCN_GROUP:
Brian King072b91f2008-07-01 13:14:30 -05002633 vhost->events_to_log |= IBMVFC_AE_RSCN;
Brian King6d29cc52009-05-28 16:17:33 -05002634 ibmvfc_reinit_host(vhost);
2635 break;
Brian King072b91f2008-07-01 13:14:30 -05002636 case IBMVFC_AE_ELS_LOGO:
2637 case IBMVFC_AE_ELS_PRLO:
2638 case IBMVFC_AE_ELS_PLOGI:
Brian King6d29cc52009-05-28 16:17:33 -05002639 list_for_each_entry(tgt, &vhost->targets, queue) {
2640 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2641 break;
2642 if (crq->scsi_id && tgt->scsi_id != crq->scsi_id)
2643 continue;
2644 if (crq->wwpn && tgt->ids.port_name != crq->wwpn)
2645 continue;
2646 if (crq->node_name && tgt->ids.node_name != crq->node_name)
2647 continue;
Brian King017b2ae2009-06-18 09:06:55 -05002648 if (tgt->need_login && crq->event == IBMVFC_AE_ELS_LOGO)
2649 tgt->logo_rcvd = 1;
2650 if (!tgt->need_login || crq->event == IBMVFC_AE_ELS_PLOGI) {
2651 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2652 ibmvfc_reinit_host(vhost);
2653 }
Brian King6d29cc52009-05-28 16:17:33 -05002654 }
Brian King072b91f2008-07-01 13:14:30 -05002655 break;
2656 case IBMVFC_AE_LINK_DOWN:
2657 case IBMVFC_AE_ADAPTER_FAILED:
2658 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2659 break;
2660 case IBMVFC_AE_LINK_DEAD:
2661 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2662 break;
2663 case IBMVFC_AE_HALT:
2664 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2665 break;
2666 default:
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002667 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
Brian King072b91f2008-07-01 13:14:30 -05002668 break;
2669 };
2670}
2671
2672/**
2673 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2674 * @crq: Command/Response queue
2675 * @vhost: ibmvfc host struct
2676 *
2677 **/
2678static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2679{
2680 long rc;
2681 struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2682
2683 switch (crq->valid) {
2684 case IBMVFC_CRQ_INIT_RSP:
2685 switch (crq->format) {
2686 case IBMVFC_CRQ_INIT:
2687 dev_info(vhost->dev, "Partner initialized\n");
2688 /* Send back a response */
2689 rc = ibmvfc_send_crq_init_complete(vhost);
2690 if (rc == 0)
Brian King861890c2009-10-19 15:07:49 -05002691 ibmvfc_init_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002692 else
2693 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2694 break;
2695 case IBMVFC_CRQ_INIT_COMPLETE:
2696 dev_info(vhost->dev, "Partner initialization complete\n");
Brian King861890c2009-10-19 15:07:49 -05002697 ibmvfc_init_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002698 break;
2699 default:
2700 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2701 }
2702 return;
2703 case IBMVFC_CRQ_XPORT_EVENT:
2704 vhost->state = IBMVFC_NO_CRQ;
Brian King79111d02009-05-28 16:17:29 -05002705 vhost->logged_in = 0;
Brian King072b91f2008-07-01 13:14:30 -05002706 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2707 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2708 /* We need to re-setup the interpartition connection */
2709 dev_info(vhost->dev, "Re-enabling adapter\n");
2710 vhost->client_migrated = 1;
2711 ibmvfc_purge_requests(vhost, DID_REQUEUE);
Brian King73ee5d82010-06-17 13:55:13 -05002712 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2713 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
Brian King072b91f2008-07-01 13:14:30 -05002714 } else {
2715 dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
Brian King072b91f2008-07-01 13:14:30 -05002716 ibmvfc_purge_requests(vhost, DID_ERROR);
Brian King73ee5d82010-06-17 13:55:13 -05002717 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2718 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
Brian King072b91f2008-07-01 13:14:30 -05002719 }
2720 return;
2721 case IBMVFC_CRQ_CMD_RSP:
2722 break;
2723 default:
2724 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2725 return;
2726 }
2727
2728 if (crq->format == IBMVFC_ASYNC_EVENT)
2729 return;
2730
2731 /* The only kind of payload CRQs we should get are responses to
2732 * things we send. Make sure this response is to something we
2733 * actually sent
2734 */
2735 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002736 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
Brian King072b91f2008-07-01 13:14:30 -05002737 crq->ioba);
2738 return;
2739 }
2740
2741 if (unlikely(atomic_read(&evt->free))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002742 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
Brian King072b91f2008-07-01 13:14:30 -05002743 crq->ioba);
2744 return;
2745 }
2746
2747 del_timer(&evt->timer);
2748 list_del(&evt->queue);
2749 ibmvfc_trc_end(evt);
2750 evt->done(evt);
2751}
2752
2753/**
2754 * ibmvfc_scan_finished - Check if the device scan is done.
2755 * @shost: scsi host struct
2756 * @time: current elapsed time
2757 *
2758 * Returns:
2759 * 0 if scan is not done / 1 if scan is done
2760 **/
2761static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2762{
2763 unsigned long flags;
2764 struct ibmvfc_host *vhost = shost_priv(shost);
2765 int done = 0;
2766
2767 spin_lock_irqsave(shost->host_lock, flags);
2768 if (time >= (init_timeout * HZ)) {
2769 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2770 "continuing initialization\n", init_timeout);
2771 done = 1;
2772 }
2773
Brian King43c8da92009-05-28 16:17:28 -05002774 if (vhost->scan_complete)
Brian King072b91f2008-07-01 13:14:30 -05002775 done = 1;
2776 spin_unlock_irqrestore(shost->host_lock, flags);
2777 return done;
2778}
2779
2780/**
2781 * ibmvfc_slave_alloc - Setup the device's task set value
2782 * @sdev: struct scsi_device device to configure
2783 *
2784 * Set the device's task set value so that error handling works as
2785 * expected.
2786 *
2787 * Returns:
2788 * 0 on success / -ENXIO if device does not exist
2789 **/
2790static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2791{
2792 struct Scsi_Host *shost = sdev->host;
2793 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2794 struct ibmvfc_host *vhost = shost_priv(shost);
2795 unsigned long flags = 0;
2796
2797 if (!rport || fc_remote_port_chkready(rport))
2798 return -ENXIO;
2799
2800 spin_lock_irqsave(shost->host_lock, flags);
2801 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2802 spin_unlock_irqrestore(shost->host_lock, flags);
2803 return 0;
2804}
2805
2806/**
Brian Kingad8dcff2008-10-29 08:46:41 -05002807 * ibmvfc_target_alloc - Setup the target's task set value
2808 * @starget: struct scsi_target
2809 *
2810 * Set the target's task set value so that error handling works as
2811 * expected.
2812 *
2813 * Returns:
2814 * 0 on success / -ENXIO if device does not exist
2815 **/
2816static int ibmvfc_target_alloc(struct scsi_target *starget)
2817{
2818 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2819 struct ibmvfc_host *vhost = shost_priv(shost);
2820 unsigned long flags = 0;
2821
2822 spin_lock_irqsave(shost->host_lock, flags);
2823 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2824 spin_unlock_irqrestore(shost->host_lock, flags);
2825 return 0;
2826}
2827
2828/**
Brian King072b91f2008-07-01 13:14:30 -05002829 * ibmvfc_slave_configure - Configure the device
2830 * @sdev: struct scsi_device device to configure
2831 *
2832 * Enable allow_restart for a device if it is a disk. Adjust the
2833 * queue_depth here also.
2834 *
2835 * Returns:
2836 * 0
2837 **/
2838static int ibmvfc_slave_configure(struct scsi_device *sdev)
2839{
2840 struct Scsi_Host *shost = sdev->host;
Brian King072b91f2008-07-01 13:14:30 -05002841 unsigned long flags = 0;
2842
2843 spin_lock_irqsave(shost->host_lock, flags);
2844 if (sdev->type == TYPE_DISK)
2845 sdev->allow_restart = 1;
2846
2847 if (sdev->tagged_supported) {
2848 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2849 scsi_activate_tcq(sdev, sdev->queue_depth);
2850 } else
2851 scsi_deactivate_tcq(sdev, sdev->queue_depth);
Brian King072b91f2008-07-01 13:14:30 -05002852 spin_unlock_irqrestore(shost->host_lock, flags);
2853 return 0;
2854}
2855
2856/**
2857 * ibmvfc_change_queue_depth - Change the device's queue depth
2858 * @sdev: scsi device struct
2859 * @qdepth: depth to set
Mike Christiee881a172009-10-15 17:46:39 -07002860 * @reason: calling context
Brian King072b91f2008-07-01 13:14:30 -05002861 *
2862 * Return value:
2863 * actual depth set
2864 **/
Mike Christiee881a172009-10-15 17:46:39 -07002865static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth,
2866 int reason)
Brian King072b91f2008-07-01 13:14:30 -05002867{
Mike Christiee881a172009-10-15 17:46:39 -07002868 if (reason != SCSI_QDEPTH_DEFAULT)
2869 return -EOPNOTSUPP;
2870
Brian King072b91f2008-07-01 13:14:30 -05002871 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2872 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2873
2874 scsi_adjust_queue_depth(sdev, 0, qdepth);
2875 return sdev->queue_depth;
2876}
2877
2878/**
2879 * ibmvfc_change_queue_type - Change the device's queue type
2880 * @sdev: scsi device struct
2881 * @tag_type: type of tags to use
2882 *
2883 * Return value:
2884 * actual queue type set
2885 **/
2886static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2887{
2888 if (sdev->tagged_supported) {
2889 scsi_set_tag_type(sdev, tag_type);
2890
2891 if (tag_type)
2892 scsi_activate_tcq(sdev, sdev->queue_depth);
2893 else
2894 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2895 } else
2896 tag_type = 0;
2897
2898 return tag_type;
2899}
2900
2901static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2902 struct device_attribute *attr, char *buf)
2903{
2904 struct Scsi_Host *shost = class_to_shost(dev);
2905 struct ibmvfc_host *vhost = shost_priv(shost);
2906
2907 return snprintf(buf, PAGE_SIZE, "%s\n",
2908 vhost->login_buf->resp.partition_name);
2909}
2910
Brian King072b91f2008-07-01 13:14:30 -05002911static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2912 struct device_attribute *attr, char *buf)
2913{
2914 struct Scsi_Host *shost = class_to_shost(dev);
2915 struct ibmvfc_host *vhost = shost_priv(shost);
2916
2917 return snprintf(buf, PAGE_SIZE, "%s\n",
2918 vhost->login_buf->resp.device_name);
2919}
2920
Brian King072b91f2008-07-01 13:14:30 -05002921static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2922 struct device_attribute *attr, char *buf)
2923{
2924 struct Scsi_Host *shost = class_to_shost(dev);
2925 struct ibmvfc_host *vhost = shost_priv(shost);
2926
2927 return snprintf(buf, PAGE_SIZE, "%s\n",
2928 vhost->login_buf->resp.port_loc_code);
2929}
2930
Brian King072b91f2008-07-01 13:14:30 -05002931static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2932 struct device_attribute *attr, char *buf)
2933{
2934 struct Scsi_Host *shost = class_to_shost(dev);
2935 struct ibmvfc_host *vhost = shost_priv(shost);
2936
2937 return snprintf(buf, PAGE_SIZE, "%s\n",
2938 vhost->login_buf->resp.drc_name);
2939}
2940
Brian King072b91f2008-07-01 13:14:30 -05002941static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2942 struct device_attribute *attr, char *buf)
2943{
2944 struct Scsi_Host *shost = class_to_shost(dev);
2945 struct ibmvfc_host *vhost = shost_priv(shost);
2946 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2947}
2948
Brian King497f9c52009-05-28 16:17:30 -05002949static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
2950 struct device_attribute *attr, char *buf)
2951{
2952 struct Scsi_Host *shost = class_to_shost(dev);
2953 struct ibmvfc_host *vhost = shost_priv(shost);
2954 return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
2955}
2956
Brian King072b91f2008-07-01 13:14:30 -05002957/**
2958 * ibmvfc_show_log_level - Show the adapter's error logging level
2959 * @dev: class device struct
2960 * @buf: buffer
2961 *
2962 * Return value:
2963 * number of bytes printed to buffer
2964 **/
2965static ssize_t ibmvfc_show_log_level(struct device *dev,
2966 struct device_attribute *attr, char *buf)
2967{
2968 struct Scsi_Host *shost = class_to_shost(dev);
2969 struct ibmvfc_host *vhost = shost_priv(shost);
2970 unsigned long flags = 0;
2971 int len;
2972
2973 spin_lock_irqsave(shost->host_lock, flags);
2974 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2975 spin_unlock_irqrestore(shost->host_lock, flags);
2976 return len;
2977}
2978
2979/**
2980 * ibmvfc_store_log_level - Change the adapter's error logging level
2981 * @dev: class device struct
2982 * @buf: buffer
2983 *
2984 * Return value:
2985 * number of bytes printed to buffer
2986 **/
2987static ssize_t ibmvfc_store_log_level(struct device *dev,
2988 struct device_attribute *attr,
2989 const char *buf, size_t count)
2990{
2991 struct Scsi_Host *shost = class_to_shost(dev);
2992 struct ibmvfc_host *vhost = shost_priv(shost);
2993 unsigned long flags = 0;
2994
2995 spin_lock_irqsave(shost->host_lock, flags);
2996 vhost->log_level = simple_strtoul(buf, NULL, 10);
2997 spin_unlock_irqrestore(shost->host_lock, flags);
2998 return strlen(buf);
2999}
3000
Brian King85e23992009-05-28 16:17:25 -05003001static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3002static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3003static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3004static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3005static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
Brian King497f9c52009-05-28 16:17:30 -05003006static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
Brian King85e23992009-05-28 16:17:25 -05003007static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3008 ibmvfc_show_log_level, ibmvfc_store_log_level);
Brian King072b91f2008-07-01 13:14:30 -05003009
3010#ifdef CONFIG_SCSI_IBMVFC_TRACE
3011/**
3012 * ibmvfc_read_trace - Dump the adapter trace
Chris Wright2c3c8be2010-05-12 18:28:57 -07003013 * @filp: open sysfs file
Brian King072b91f2008-07-01 13:14:30 -05003014 * @kobj: kobject struct
3015 * @bin_attr: bin_attribute struct
3016 * @buf: buffer
3017 * @off: offset
3018 * @count: buffer size
3019 *
3020 * Return value:
3021 * number of bytes printed to buffer
3022 **/
Chris Wright2c3c8be2010-05-12 18:28:57 -07003023static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
Brian King072b91f2008-07-01 13:14:30 -05003024 struct bin_attribute *bin_attr,
3025 char *buf, loff_t off, size_t count)
3026{
3027 struct device *dev = container_of(kobj, struct device, kobj);
3028 struct Scsi_Host *shost = class_to_shost(dev);
3029 struct ibmvfc_host *vhost = shost_priv(shost);
3030 unsigned long flags = 0;
3031 int size = IBMVFC_TRACE_SIZE;
3032 char *src = (char *)vhost->trace;
3033
3034 if (off > size)
3035 return 0;
3036 if (off + count > size) {
3037 size -= off;
3038 count = size;
3039 }
3040
3041 spin_lock_irqsave(shost->host_lock, flags);
3042 memcpy(buf, &src[off], count);
3043 spin_unlock_irqrestore(shost->host_lock, flags);
3044 return count;
3045}
3046
3047static struct bin_attribute ibmvfc_trace_attr = {
3048 .attr = {
3049 .name = "trace",
3050 .mode = S_IRUGO,
3051 },
3052 .size = 0,
3053 .read = ibmvfc_read_trace,
3054};
3055#endif
3056
3057static struct device_attribute *ibmvfc_attrs[] = {
Brian King85e23992009-05-28 16:17:25 -05003058 &dev_attr_partition_name,
3059 &dev_attr_device_name,
3060 &dev_attr_port_loc_code,
3061 &dev_attr_drc_name,
3062 &dev_attr_npiv_version,
Brian King497f9c52009-05-28 16:17:30 -05003063 &dev_attr_capabilities,
Brian King85e23992009-05-28 16:17:25 -05003064 &dev_attr_log_level,
Brian King072b91f2008-07-01 13:14:30 -05003065 NULL
3066};
3067
3068static struct scsi_host_template driver_template = {
3069 .module = THIS_MODULE,
3070 .name = "IBM POWER Virtual FC Adapter",
3071 .proc_name = IBMVFC_NAME,
3072 .queuecommand = ibmvfc_queuecommand,
3073 .eh_abort_handler = ibmvfc_eh_abort_handler,
3074 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3075 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3076 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3077 .slave_alloc = ibmvfc_slave_alloc,
3078 .slave_configure = ibmvfc_slave_configure,
Brian Kingad8dcff2008-10-29 08:46:41 -05003079 .target_alloc = ibmvfc_target_alloc,
Brian King072b91f2008-07-01 13:14:30 -05003080 .scan_finished = ibmvfc_scan_finished,
3081 .change_queue_depth = ibmvfc_change_queue_depth,
3082 .change_queue_type = ibmvfc_change_queue_type,
3083 .cmd_per_lun = 16,
3084 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3085 .this_id = -1,
3086 .sg_tablesize = SG_ALL,
3087 .max_sectors = IBMVFC_MAX_SECTORS,
3088 .use_clustering = ENABLE_CLUSTERING,
3089 .shost_attrs = ibmvfc_attrs,
3090};
3091
3092/**
3093 * ibmvfc_next_async_crq - Returns the next entry in async queue
3094 * @vhost: ibmvfc host struct
3095 *
3096 * Returns:
3097 * Pointer to next entry in queue / NULL if empty
3098 **/
3099static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3100{
3101 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3102 struct ibmvfc_async_crq *crq;
3103
3104 crq = &async_crq->msgs[async_crq->cur];
3105 if (crq->valid & 0x80) {
3106 if (++async_crq->cur == async_crq->size)
3107 async_crq->cur = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003108 rmb();
Brian King072b91f2008-07-01 13:14:30 -05003109 } else
3110 crq = NULL;
3111
3112 return crq;
3113}
3114
3115/**
3116 * ibmvfc_next_crq - Returns the next entry in message queue
3117 * @vhost: ibmvfc host struct
3118 *
3119 * Returns:
3120 * Pointer to next entry in queue / NULL if empty
3121 **/
3122static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3123{
3124 struct ibmvfc_crq_queue *queue = &vhost->crq;
3125 struct ibmvfc_crq *crq;
3126
3127 crq = &queue->msgs[queue->cur];
3128 if (crq->valid & 0x80) {
3129 if (++queue->cur == queue->size)
3130 queue->cur = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003131 rmb();
Brian King072b91f2008-07-01 13:14:30 -05003132 } else
3133 crq = NULL;
3134
3135 return crq;
3136}
3137
3138/**
3139 * ibmvfc_interrupt - Interrupt handler
3140 * @irq: number of irq to handle, not used
3141 * @dev_instance: ibmvfc_host that received interrupt
3142 *
3143 * Returns:
3144 * IRQ_HANDLED
3145 **/
3146static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3147{
3148 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
Brian King039a0892009-03-20 15:44:35 -05003149 unsigned long flags;
3150
3151 spin_lock_irqsave(vhost->host->host_lock, flags);
3152 vio_disable_interrupts(to_vio_dev(vhost->dev));
3153 tasklet_schedule(&vhost->tasklet);
3154 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3155 return IRQ_HANDLED;
3156}
3157
3158/**
3159 * ibmvfc_tasklet - Interrupt handler tasklet
3160 * @data: ibmvfc host struct
3161 *
3162 * Returns:
3163 * Nothing
3164 **/
3165static void ibmvfc_tasklet(void *data)
3166{
3167 struct ibmvfc_host *vhost = data;
Brian King072b91f2008-07-01 13:14:30 -05003168 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3169 struct ibmvfc_crq *crq;
3170 struct ibmvfc_async_crq *async;
3171 unsigned long flags;
3172 int done = 0;
3173
3174 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05003175 while (!done) {
Brian King072b91f2008-07-01 13:14:30 -05003176 /* Pull all the valid messages off the async CRQ */
3177 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3178 ibmvfc_handle_async(async, vhost);
3179 async->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003180 wmb();
Brian King072b91f2008-07-01 13:14:30 -05003181 }
3182
Brian Kingf1d7fb72009-06-18 09:06:52 -05003183 /* Pull all the valid messages off the CRQ */
3184 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
Brian King072b91f2008-07-01 13:14:30 -05003185 ibmvfc_handle_crq(crq, vhost);
3186 crq->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003187 wmb();
Brian Kingf1d7fb72009-06-18 09:06:52 -05003188 }
3189
3190 vio_enable_interrupts(vdev);
3191 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
Brian King072b91f2008-07-01 13:14:30 -05003192 vio_disable_interrupts(vdev);
3193 ibmvfc_handle_async(async, vhost);
Brian King4081b772008-11-14 13:33:50 -06003194 async->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003195 wmb();
Brian Kingf1d7fb72009-06-18 09:06:52 -05003196 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3197 vio_disable_interrupts(vdev);
3198 ibmvfc_handle_crq(crq, vhost);
3199 crq->valid = 0;
Brian Kingf5832fa2010-04-20 14:21:33 -05003200 wmb();
Brian King072b91f2008-07-01 13:14:30 -05003201 } else
3202 done = 1;
3203 }
3204
3205 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05003206}
3207
3208/**
3209 * ibmvfc_init_tgt - Set the next init job step for the target
3210 * @tgt: ibmvfc target struct
3211 * @job_step: job step to perform
3212 *
3213 **/
3214static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3215 void (*job_step) (struct ibmvfc_target *))
3216{
3217 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
3218 tgt->job_step = job_step;
3219 wake_up(&tgt->vhost->work_wait_q);
3220}
3221
3222/**
3223 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3224 * @tgt: ibmvfc target struct
3225 * @job_step: initialization job step
3226 *
Brian King7d0e4622009-05-28 16:17:26 -05003227 * Returns: 1 if step will be retried / 0 if not
3228 *
Brian King072b91f2008-07-01 13:14:30 -05003229 **/
Brian King7d0e4622009-05-28 16:17:26 -05003230static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
Brian King072b91f2008-07-01 13:14:30 -05003231 void (*job_step) (struct ibmvfc_target *))
3232{
Brian King1c41fa822008-12-03 11:02:54 -06003233 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -05003234 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3235 wake_up(&tgt->vhost->work_wait_q);
Brian King7d0e4622009-05-28 16:17:26 -05003236 return 0;
Brian King072b91f2008-07-01 13:14:30 -05003237 } else
3238 ibmvfc_init_tgt(tgt, job_step);
Brian King7d0e4622009-05-28 16:17:26 -05003239 return 1;
Brian King072b91f2008-07-01 13:14:30 -05003240}
3241
Brian Kinga3b7aea2009-02-02 18:39:40 -06003242/* Defined in FC-LS */
3243static const struct {
3244 int code;
3245 int retry;
3246 int logged_in;
3247} prli_rsp [] = {
3248 { 0, 1, 0 },
3249 { 1, 0, 1 },
3250 { 2, 1, 0 },
3251 { 3, 1, 0 },
3252 { 4, 0, 0 },
3253 { 5, 0, 0 },
3254 { 6, 0, 1 },
3255 { 7, 0, 0 },
3256 { 8, 1, 0 },
3257};
3258
3259/**
3260 * ibmvfc_get_prli_rsp - Find PRLI response index
3261 * @flags: PRLI response flags
3262 *
3263 **/
3264static int ibmvfc_get_prli_rsp(u16 flags)
3265{
3266 int i;
3267 int code = (flags & 0x0f00) >> 8;
3268
3269 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3270 if (prli_rsp[i].code == code)
3271 return i;
3272
3273 return 0;
3274}
3275
Brian King072b91f2008-07-01 13:14:30 -05003276/**
Brian King072b91f2008-07-01 13:14:30 -05003277 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3278 * @evt: ibmvfc event struct
3279 *
3280 **/
3281static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3282{
3283 struct ibmvfc_target *tgt = evt->tgt;
3284 struct ibmvfc_host *vhost = evt->vhost;
3285 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
Brian Kinga3b7aea2009-02-02 18:39:40 -06003286 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
Brian King072b91f2008-07-01 13:14:30 -05003287 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003288 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003289
3290 vhost->discovery_threads--;
3291 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3292 switch (status) {
3293 case IBMVFC_MAD_SUCCESS:
Brian Kinga3b7aea2009-02-02 18:39:40 -06003294 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3295 parms->type, parms->flags, parms->service_parms);
3296
3297 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3298 index = ibmvfc_get_prli_rsp(parms->flags);
3299 if (prli_rsp[index].logged_in) {
3300 if (parms->flags & IBMVFC_PRLI_EST_IMG_PAIR) {
3301 tgt->need_login = 0;
3302 tgt->ids.roles = 0;
3303 if (parms->service_parms & IBMVFC_PRLI_TARGET_FUNC)
3304 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3305 if (parms->service_parms & IBMVFC_PRLI_INITIATOR_FUNC)
3306 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
Brian King43c8da92009-05-28 16:17:28 -05003307 tgt->add_rport = 1;
Brian Kinga3b7aea2009-02-02 18:39:40 -06003308 } else
3309 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3310 } else if (prli_rsp[index].retry)
3311 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3312 else
3313 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3314 } else
3315 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King072b91f2008-07-01 13:14:30 -05003316 break;
3317 case IBMVFC_MAD_DRIVER_FAILED:
3318 break;
3319 case IBMVFC_MAD_CRQ_ERROR:
3320 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3321 break;
3322 case IBMVFC_MAD_FAILED:
3323 default:
Brian King017b2ae2009-06-18 09:06:55 -05003324 if ((rsp->status & IBMVFC_VIOS_FAILURE) && rsp->error == IBMVFC_PLOGI_REQUIRED)
3325 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3326 else if (tgt->logo_rcvd)
3327 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3328 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05003329 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
Brian King10e79492008-10-29 08:46:45 -05003330 else
3331 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05003332
3333 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3334 ibmvfc_get_cmd_error(rsp->status, rsp->error),
3335 rsp->status, rsp->error, status);
Brian King072b91f2008-07-01 13:14:30 -05003336 break;
3337 };
3338
3339 kref_put(&tgt->kref, ibmvfc_release_tgt);
3340 ibmvfc_free_event(evt);
3341 wake_up(&vhost->work_wait_q);
3342}
3343
3344/**
3345 * ibmvfc_tgt_send_prli - Send a process login
3346 * @tgt: ibmvfc target struct
3347 *
3348 **/
3349static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3350{
3351 struct ibmvfc_process_login *prli;
3352 struct ibmvfc_host *vhost = tgt->vhost;
3353 struct ibmvfc_event *evt;
3354
3355 if (vhost->discovery_threads >= disc_threads)
3356 return;
3357
3358 kref_get(&tgt->kref);
3359 evt = ibmvfc_get_event(vhost);
3360 vhost->discovery_threads++;
3361 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3362 evt->tgt = tgt;
3363 prli = &evt->iu.prli;
3364 memset(prli, 0, sizeof(*prli));
3365 prli->common.version = 1;
3366 prli->common.opcode = IBMVFC_PROCESS_LOGIN;
3367 prli->common.length = sizeof(*prli);
3368 prli->scsi_id = tgt->scsi_id;
3369
3370 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3371 prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
3372 prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
3373
3374 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3375 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3376 vhost->discovery_threads--;
3377 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3378 kref_put(&tgt->kref, ibmvfc_release_tgt);
3379 } else
3380 tgt_dbg(tgt, "Sent process login\n");
3381}
3382
3383/**
3384 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3385 * @evt: ibmvfc event struct
3386 *
3387 **/
3388static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3389{
3390 struct ibmvfc_target *tgt = evt->tgt;
3391 struct ibmvfc_host *vhost = evt->vhost;
3392 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3393 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003394 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003395
3396 vhost->discovery_threads--;
3397 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3398 switch (status) {
3399 case IBMVFC_MAD_SUCCESS:
3400 tgt_dbg(tgt, "Port Login succeeded\n");
3401 if (tgt->ids.port_name &&
3402 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3403 vhost->reinit = 1;
3404 tgt_dbg(tgt, "Port re-init required\n");
3405 break;
3406 }
3407 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3408 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3409 tgt->ids.port_id = tgt->scsi_id;
Brian King072b91f2008-07-01 13:14:30 -05003410 memcpy(&tgt->service_parms, &rsp->service_parms,
3411 sizeof(tgt->service_parms));
3412 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3413 sizeof(tgt->service_parms_change));
3414 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3415 break;
3416 case IBMVFC_MAD_DRIVER_FAILED:
3417 break;
3418 case IBMVFC_MAD_CRQ_ERROR:
3419 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3420 break;
3421 case IBMVFC_MAD_FAILED:
3422 default:
Brian King7d0e4622009-05-28 16:17:26 -05003423 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3424 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3425 else
3426 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3427
3428 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 -05003429 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3430 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3431 ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05003432 break;
3433 };
3434
3435 kref_put(&tgt->kref, ibmvfc_release_tgt);
3436 ibmvfc_free_event(evt);
3437 wake_up(&vhost->work_wait_q);
3438}
3439
3440/**
3441 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3442 * @tgt: ibmvfc target struct
3443 *
3444 **/
3445static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3446{
3447 struct ibmvfc_port_login *plogi;
3448 struct ibmvfc_host *vhost = tgt->vhost;
3449 struct ibmvfc_event *evt;
3450
3451 if (vhost->discovery_threads >= disc_threads)
3452 return;
3453
3454 kref_get(&tgt->kref);
Brian King017b2ae2009-06-18 09:06:55 -05003455 tgt->logo_rcvd = 0;
Brian King072b91f2008-07-01 13:14:30 -05003456 evt = ibmvfc_get_event(vhost);
3457 vhost->discovery_threads++;
3458 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3459 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3460 evt->tgt = tgt;
3461 plogi = &evt->iu.plogi;
3462 memset(plogi, 0, sizeof(*plogi));
3463 plogi->common.version = 1;
3464 plogi->common.opcode = IBMVFC_PORT_LOGIN;
3465 plogi->common.length = sizeof(*plogi);
3466 plogi->scsi_id = tgt->scsi_id;
3467
3468 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3469 vhost->discovery_threads--;
3470 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3471 kref_put(&tgt->kref, ibmvfc_release_tgt);
3472 } else
3473 tgt_dbg(tgt, "Sent port login\n");
3474}
3475
3476/**
3477 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3478 * @evt: ibmvfc event struct
3479 *
3480 **/
3481static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3482{
3483 struct ibmvfc_target *tgt = evt->tgt;
3484 struct ibmvfc_host *vhost = evt->vhost;
3485 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3486 u32 status = rsp->common.status;
3487
3488 vhost->discovery_threads--;
3489 ibmvfc_free_event(evt);
3490 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3491
3492 switch (status) {
3493 case IBMVFC_MAD_SUCCESS:
3494 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3495 break;
3496 case IBMVFC_MAD_DRIVER_FAILED:
3497 kref_put(&tgt->kref, ibmvfc_release_tgt);
3498 wake_up(&vhost->work_wait_q);
3499 return;
3500 case IBMVFC_MAD_FAILED:
3501 default:
3502 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3503 break;
3504 };
3505
3506 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
3507 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3508 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
3509 tgt->scsi_id != tgt->new_scsi_id)
3510 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3511 kref_put(&tgt->kref, ibmvfc_release_tgt);
3512 wake_up(&vhost->work_wait_q);
3513}
3514
3515/**
3516 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3517 * @tgt: ibmvfc target struct
3518 *
3519 **/
3520static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3521{
3522 struct ibmvfc_implicit_logout *mad;
3523 struct ibmvfc_host *vhost = tgt->vhost;
3524 struct ibmvfc_event *evt;
3525
3526 if (vhost->discovery_threads >= disc_threads)
3527 return;
3528
3529 kref_get(&tgt->kref);
3530 evt = ibmvfc_get_event(vhost);
3531 vhost->discovery_threads++;
3532 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3533 evt->tgt = tgt;
3534 mad = &evt->iu.implicit_logout;
3535 memset(mad, 0, sizeof(*mad));
3536 mad->common.version = 1;
3537 mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
3538 mad->common.length = sizeof(*mad);
3539 mad->old_scsi_id = tgt->scsi_id;
3540
3541 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3542 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3543 vhost->discovery_threads--;
3544 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3545 kref_put(&tgt->kref, ibmvfc_release_tgt);
3546 } else
3547 tgt_dbg(tgt, "Sent Implicit Logout\n");
3548}
3549
3550/**
Brian King989b8542008-07-22 08:31:47 -05003551 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3552 * @mad: ibmvfc passthru mad struct
3553 * @tgt: ibmvfc target struct
3554 *
3555 * Returns:
3556 * 1 if PLOGI needed / 0 if PLOGI not needed
3557 **/
3558static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3559 struct ibmvfc_target *tgt)
3560{
3561 if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3562 sizeof(tgt->ids.port_name)))
3563 return 1;
3564 if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3565 sizeof(tgt->ids.node_name)))
3566 return 1;
3567 if (mad->fc_iu.response[6] != tgt->scsi_id)
3568 return 1;
3569 return 0;
3570}
3571
3572/**
3573 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3574 * @evt: ibmvfc event struct
3575 *
3576 **/
3577static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3578{
3579 struct ibmvfc_target *tgt = evt->tgt;
3580 struct ibmvfc_host *vhost = evt->vhost;
3581 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3582 u32 status = mad->common.status;
3583 u8 fc_reason, fc_explain;
3584
3585 vhost->discovery_threads--;
3586 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
Brian King10501e12009-03-20 15:44:39 -05003587 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003588
3589 switch (status) {
3590 case IBMVFC_MAD_SUCCESS:
3591 tgt_dbg(tgt, "ADISC succeeded\n");
3592 if (ibmvfc_adisc_needs_plogi(mad, tgt))
Brian King5e471672009-05-28 16:17:32 -05003593 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King989b8542008-07-22 08:31:47 -05003594 break;
3595 case IBMVFC_MAD_DRIVER_FAILED:
3596 break;
3597 case IBMVFC_MAD_FAILED:
3598 default:
Brian King5e471672009-05-28 16:17:32 -05003599 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King989b8542008-07-22 08:31:47 -05003600 fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
3601 fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
3602 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3603 ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
3604 mad->iu.status, mad->iu.error,
3605 ibmvfc_get_fc_type(fc_reason), fc_reason,
3606 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3607 break;
3608 };
3609
3610 kref_put(&tgt->kref, ibmvfc_release_tgt);
3611 ibmvfc_free_event(evt);
3612 wake_up(&vhost->work_wait_q);
3613}
3614
3615/**
3616 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3617 * @evt: ibmvfc event struct
3618 *
3619 **/
3620static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3621{
3622 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3623
3624 memset(mad, 0, sizeof(*mad));
3625 mad->common.version = 1;
3626 mad->common.opcode = IBMVFC_PASSTHRU;
3627 mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
3628 mad->cmd_ioba.va = (u64)evt->crq.ioba +
3629 offsetof(struct ibmvfc_passthru_mad, iu);
3630 mad->cmd_ioba.len = sizeof(mad->iu);
3631 mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
3632 mad->iu.rsp_len = sizeof(mad->fc_iu.response);
3633 mad->iu.cmd.va = (u64)evt->crq.ioba +
3634 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3635 offsetof(struct ibmvfc_passthru_fc_iu, payload);
3636 mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
3637 mad->iu.rsp.va = (u64)evt->crq.ioba +
3638 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3639 offsetof(struct ibmvfc_passthru_fc_iu, response);
3640 mad->iu.rsp.len = sizeof(mad->fc_iu.response);
3641}
3642
3643/**
Brian King10501e12009-03-20 15:44:39 -05003644 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3645 * @evt: ibmvfc event struct
3646 *
3647 * Just cleanup this event struct. Everything else is handled by
3648 * the ADISC completion handler. If the ADISC never actually comes
3649 * back, we still have the timer running on the ADISC event struct
3650 * which will fire and cause the CRQ to get reset.
3651 *
3652 **/
3653static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3654{
3655 struct ibmvfc_host *vhost = evt->vhost;
3656 struct ibmvfc_target *tgt = evt->tgt;
3657
3658 tgt_dbg(tgt, "ADISC cancel complete\n");
3659 vhost->abort_threads--;
3660 ibmvfc_free_event(evt);
3661 kref_put(&tgt->kref, ibmvfc_release_tgt);
3662 wake_up(&vhost->work_wait_q);
3663}
3664
3665/**
3666 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3667 * @tgt: ibmvfc target struct
3668 *
3669 * If an ADISC times out, send a cancel. If the cancel times
3670 * out, reset the CRQ. When the ADISC comes back as cancelled,
3671 * log back into the target.
3672 **/
3673static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
3674{
3675 struct ibmvfc_host *vhost = tgt->vhost;
3676 struct ibmvfc_event *evt;
3677 struct ibmvfc_tmf *tmf;
3678 unsigned long flags;
3679 int rc;
3680
3681 tgt_dbg(tgt, "ADISC timeout\n");
3682 spin_lock_irqsave(vhost->host->host_lock, flags);
3683 if (vhost->abort_threads >= disc_threads ||
3684 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3685 vhost->state != IBMVFC_INITIALIZING ||
3686 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3687 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3688 return;
3689 }
3690
3691 vhost->abort_threads++;
3692 kref_get(&tgt->kref);
3693 evt = ibmvfc_get_event(vhost);
3694 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3695
3696 evt->tgt = tgt;
3697 tmf = &evt->iu.tmf;
3698 memset(tmf, 0, sizeof(*tmf));
3699 tmf->common.version = 1;
3700 tmf->common.opcode = IBMVFC_TMF_MAD;
3701 tmf->common.length = sizeof(*tmf);
3702 tmf->scsi_id = tgt->scsi_id;
3703 tmf->cancel_key = tgt->cancel_key;
3704
3705 rc = ibmvfc_send_event(evt, vhost, default_timeout);
3706
3707 if (rc) {
3708 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3709 vhost->abort_threads--;
3710 kref_put(&tgt->kref, ibmvfc_release_tgt);
3711 __ibmvfc_reset_host(vhost);
3712 } else
3713 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3714 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3715}
3716
3717/**
Brian King989b8542008-07-22 08:31:47 -05003718 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3719 * @tgt: ibmvfc target struct
3720 *
Brian King10501e12009-03-20 15:44:39 -05003721 * When sending an ADISC we end up with two timers running. The
3722 * first timer is the timer in the ibmvfc target struct. If this
3723 * fires, we send a cancel to the target. The second timer is the
3724 * timer on the ibmvfc event for the ADISC, which is longer. If that
3725 * fires, it means the ADISC timed out and our attempt to cancel it
3726 * also failed, so we need to reset the CRQ.
Brian King989b8542008-07-22 08:31:47 -05003727 **/
3728static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3729{
3730 struct ibmvfc_passthru_mad *mad;
3731 struct ibmvfc_host *vhost = tgt->vhost;
3732 struct ibmvfc_event *evt;
3733
3734 if (vhost->discovery_threads >= disc_threads)
3735 return;
3736
3737 kref_get(&tgt->kref);
3738 evt = ibmvfc_get_event(vhost);
3739 vhost->discovery_threads++;
3740 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3741 evt->tgt = tgt;
3742
3743 ibmvfc_init_passthru(evt);
3744 mad = &evt->iu.passthru;
3745 mad->iu.flags = IBMVFC_FC_ELS;
3746 mad->iu.scsi_id = tgt->scsi_id;
Brian King10501e12009-03-20 15:44:39 -05003747 mad->iu.cancel_key = tgt->cancel_key;
Brian King989b8542008-07-22 08:31:47 -05003748
3749 mad->fc_iu.payload[0] = IBMVFC_ADISC;
3750 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3751 sizeof(vhost->login_buf->resp.port_name));
3752 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3753 sizeof(vhost->login_buf->resp.node_name));
3754 mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
3755
Brian King10501e12009-03-20 15:44:39 -05003756 if (timer_pending(&tgt->timer))
3757 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
3758 else {
3759 tgt->timer.data = (unsigned long) tgt;
3760 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
3761 tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
3762 add_timer(&tgt->timer);
3763 }
3764
Brian King989b8542008-07-22 08:31:47 -05003765 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
Brian King10501e12009-03-20 15:44:39 -05003766 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
Brian King989b8542008-07-22 08:31:47 -05003767 vhost->discovery_threads--;
Brian King10501e12009-03-20 15:44:39 -05003768 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003769 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3770 kref_put(&tgt->kref, ibmvfc_release_tgt);
3771 } else
3772 tgt_dbg(tgt, "Sent ADISC\n");
3773}
3774
3775/**
Brian King072b91f2008-07-01 13:14:30 -05003776 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3777 * @evt: ibmvfc event struct
3778 *
3779 **/
3780static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3781{
3782 struct ibmvfc_target *tgt = evt->tgt;
3783 struct ibmvfc_host *vhost = evt->vhost;
3784 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3785 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003786 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003787
3788 vhost->discovery_threads--;
3789 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3790 switch (status) {
3791 case IBMVFC_MAD_SUCCESS:
3792 tgt_dbg(tgt, "Query Target succeeded\n");
3793 tgt->new_scsi_id = rsp->scsi_id;
3794 if (rsp->scsi_id != tgt->scsi_id)
3795 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
Brian King989b8542008-07-22 08:31:47 -05003796 else
3797 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
Brian King072b91f2008-07-01 13:14:30 -05003798 break;
3799 case IBMVFC_MAD_DRIVER_FAILED:
3800 break;
3801 case IBMVFC_MAD_CRQ_ERROR:
3802 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3803 break;
3804 case IBMVFC_MAD_FAILED:
3805 default:
Brian King072b91f2008-07-01 13:14:30 -05003806 if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3807 rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3808 rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
3809 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3810 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05003811 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
Brian King10e79492008-10-29 08:46:45 -05003812 else
3813 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05003814
3815 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3816 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3817 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3818 ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05003819 break;
3820 };
3821
3822 kref_put(&tgt->kref, ibmvfc_release_tgt);
3823 ibmvfc_free_event(evt);
3824 wake_up(&vhost->work_wait_q);
3825}
3826
3827/**
3828 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3829 * @tgt: ibmvfc target struct
3830 *
3831 **/
3832static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3833{
3834 struct ibmvfc_query_tgt *query_tgt;
3835 struct ibmvfc_host *vhost = tgt->vhost;
3836 struct ibmvfc_event *evt;
3837
3838 if (vhost->discovery_threads >= disc_threads)
3839 return;
3840
3841 kref_get(&tgt->kref);
3842 evt = ibmvfc_get_event(vhost);
3843 vhost->discovery_threads++;
3844 evt->tgt = tgt;
3845 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3846 query_tgt = &evt->iu.query_tgt;
3847 memset(query_tgt, 0, sizeof(*query_tgt));
3848 query_tgt->common.version = 1;
3849 query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
3850 query_tgt->common.length = sizeof(*query_tgt);
3851 query_tgt->wwpn = tgt->ids.port_name;
3852
3853 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3854 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3855 vhost->discovery_threads--;
3856 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3857 kref_put(&tgt->kref, ibmvfc_release_tgt);
3858 } else
3859 tgt_dbg(tgt, "Sent Query Target\n");
3860}
3861
3862/**
3863 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3864 * @vhost: ibmvfc host struct
3865 * @scsi_id: SCSI ID to allocate target for
3866 *
3867 * Returns:
3868 * 0 on success / other on failure
3869 **/
3870static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3871{
3872 struct ibmvfc_target *tgt;
3873 unsigned long flags;
3874
3875 spin_lock_irqsave(vhost->host->host_lock, flags);
3876 list_for_each_entry(tgt, &vhost->targets, queue) {
3877 if (tgt->scsi_id == scsi_id) {
3878 if (tgt->need_login)
3879 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3880 goto unlock_out;
3881 }
3882 }
3883 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3884
Brian King7270b9b2009-05-28 16:17:24 -05003885 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
Brian King072b91f2008-07-01 13:14:30 -05003886 if (!tgt) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00003887 dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
Brian King072b91f2008-07-01 13:14:30 -05003888 scsi_id);
3889 return -ENOMEM;
3890 }
3891
Brian King0883e3b2009-02-04 16:13:12 -06003892 memset(tgt, 0, sizeof(*tgt));
Brian King072b91f2008-07-01 13:14:30 -05003893 tgt->scsi_id = scsi_id;
3894 tgt->new_scsi_id = scsi_id;
3895 tgt->vhost = vhost;
3896 tgt->need_login = 1;
Brian King10501e12009-03-20 15:44:39 -05003897 tgt->cancel_key = vhost->task_set++;
3898 init_timer(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05003899 kref_init(&tgt->kref);
3900 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3901 spin_lock_irqsave(vhost->host->host_lock, flags);
3902 list_add_tail(&tgt->queue, &vhost->targets);
3903
3904unlock_out:
3905 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3906 return 0;
3907}
3908
3909/**
3910 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3911 * @vhost: ibmvfc host struct
3912 *
3913 * Returns:
3914 * 0 on success / other on failure
3915 **/
3916static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3917{
3918 int i, rc;
3919
3920 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3921 rc = ibmvfc_alloc_target(vhost,
3922 vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3923
3924 return rc;
3925}
3926
3927/**
3928 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3929 * @evt: ibmvfc event struct
3930 *
3931 **/
3932static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3933{
3934 struct ibmvfc_host *vhost = evt->vhost;
3935 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3936 u32 mad_status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003937 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003938
3939 switch (mad_status) {
3940 case IBMVFC_MAD_SUCCESS:
3941 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3942 vhost->num_targets = rsp->num_written;
3943 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3944 break;
3945 case IBMVFC_MAD_FAILED:
Brian King7d0e4622009-05-28 16:17:26 -05003946 level += ibmvfc_retry_host_init(vhost);
3947 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
3948 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05003949 break;
3950 case IBMVFC_MAD_DRIVER_FAILED:
3951 break;
3952 default:
3953 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3954 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3955 break;
3956 }
3957
3958 ibmvfc_free_event(evt);
3959 wake_up(&vhost->work_wait_q);
3960}
3961
3962/**
3963 * ibmvfc_discover_targets - Send Discover Targets MAD
3964 * @vhost: ibmvfc host struct
3965 *
3966 **/
3967static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3968{
3969 struct ibmvfc_discover_targets *mad;
3970 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3971
3972 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3973 mad = &evt->iu.discover_targets;
3974 memset(mad, 0, sizeof(*mad));
3975 mad->common.version = 1;
3976 mad->common.opcode = IBMVFC_DISC_TARGETS;
3977 mad->common.length = sizeof(*mad);
3978 mad->bufflen = vhost->disc_buf_sz;
3979 mad->buffer.va = vhost->disc_buf_dma;
3980 mad->buffer.len = vhost->disc_buf_sz;
3981 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3982
3983 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3984 ibmvfc_dbg(vhost, "Sent discover targets\n");
3985 else
3986 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3987}
3988
3989/**
3990 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3991 * @evt: ibmvfc event struct
3992 *
3993 **/
3994static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3995{
3996 struct ibmvfc_host *vhost = evt->vhost;
3997 u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3998 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3999 unsigned int npiv_max_sectors;
Brian King7d0e4622009-05-28 16:17:26 -05004000 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05004001
4002 switch (mad_status) {
4003 case IBMVFC_MAD_SUCCESS:
4004 ibmvfc_free_event(evt);
4005 break;
4006 case IBMVFC_MAD_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05004007 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05004008 level += ibmvfc_retry_host_init(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004009 else
4010 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
Brian King7d0e4622009-05-28 16:17:26 -05004011 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4012 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05004013 ibmvfc_free_event(evt);
4014 return;
4015 case IBMVFC_MAD_CRQ_ERROR:
4016 ibmvfc_retry_host_init(vhost);
4017 case IBMVFC_MAD_DRIVER_FAILED:
4018 ibmvfc_free_event(evt);
4019 return;
4020 default:
4021 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4022 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4023 ibmvfc_free_event(evt);
4024 return;
4025 }
4026
4027 vhost->client_migrated = 0;
4028
4029 if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
4030 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4031 rsp->flags);
4032 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4033 wake_up(&vhost->work_wait_q);
4034 return;
4035 }
4036
4037 if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
4038 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4039 rsp->max_cmds);
4040 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4041 wake_up(&vhost->work_wait_q);
4042 return;
4043 }
4044
Brian King79111d02009-05-28 16:17:29 -05004045 vhost->logged_in = 1;
Brian King072b91f2008-07-01 13:14:30 -05004046 npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
4047 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4048 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4049 rsp->drc_name, npiv_max_sectors);
4050
4051 fc_host_fabric_name(vhost->host) = rsp->node_name;
4052 fc_host_node_name(vhost->host) = rsp->node_name;
4053 fc_host_port_name(vhost->host) = rsp->port_name;
4054 fc_host_port_id(vhost->host) = rsp->scsi_id;
4055 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4056 fc_host_supported_classes(vhost->host) = 0;
4057 if (rsp->service_parms.class1_parms[0] & 0x80000000)
4058 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4059 if (rsp->service_parms.class2_parms[0] & 0x80000000)
4060 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4061 if (rsp->service_parms.class3_parms[0] & 0x80000000)
4062 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4063 fc_host_maxframe_size(vhost->host) =
4064 rsp->service_parms.common.bb_rcv_sz & 0x0fff;
4065
4066 vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
4067 vhost->host->max_sectors = npiv_max_sectors;
4068 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4069 wake_up(&vhost->work_wait_q);
4070}
4071
4072/**
4073 * ibmvfc_npiv_login - Sends NPIV login
4074 * @vhost: ibmvfc host struct
4075 *
4076 **/
4077static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4078{
4079 struct ibmvfc_npiv_login_mad *mad;
4080 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4081
4082 ibmvfc_gather_partition_info(vhost);
4083 ibmvfc_set_login_info(vhost);
4084 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4085
4086 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4087 mad = &evt->iu.npiv_login;
4088 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4089 mad->common.version = 1;
4090 mad->common.opcode = IBMVFC_NPIV_LOGIN;
4091 mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
4092 mad->buffer.va = vhost->login_buf_dma;
4093 mad->buffer.len = sizeof(*vhost->login_buf);
4094
Brian King072b91f2008-07-01 13:14:30 -05004095 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4096
4097 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4098 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4099 else
4100 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4101};
4102
4103/**
Brian King79111d02009-05-28 16:17:29 -05004104 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4105 * @vhost: ibmvfc host struct
4106 *
4107 **/
4108static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4109{
4110 struct ibmvfc_host *vhost = evt->vhost;
4111 u32 mad_status = evt->xfer_iu->npiv_logout.common.status;
4112
4113 ibmvfc_free_event(evt);
4114
4115 switch (mad_status) {
4116 case IBMVFC_MAD_SUCCESS:
4117 if (list_empty(&vhost->sent) &&
4118 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
Brian King861890c2009-10-19 15:07:49 -05004119 ibmvfc_init_host(vhost);
Brian King79111d02009-05-28 16:17:29 -05004120 return;
4121 }
4122 break;
4123 case IBMVFC_MAD_FAILED:
4124 case IBMVFC_MAD_NOT_SUPPORTED:
4125 case IBMVFC_MAD_CRQ_ERROR:
4126 case IBMVFC_MAD_DRIVER_FAILED:
4127 default:
4128 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4129 break;
4130 }
4131
4132 ibmvfc_hard_reset_host(vhost);
4133}
4134
4135/**
4136 * ibmvfc_npiv_logout - Issue an NPIV Logout
4137 * @vhost: ibmvfc host struct
4138 *
4139 **/
4140static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4141{
4142 struct ibmvfc_npiv_logout_mad *mad;
4143 struct ibmvfc_event *evt;
4144
4145 evt = ibmvfc_get_event(vhost);
4146 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4147
4148 mad = &evt->iu.npiv_logout;
4149 memset(mad, 0, sizeof(*mad));
4150 mad->common.version = 1;
4151 mad->common.opcode = IBMVFC_NPIV_LOGOUT;
4152 mad->common.length = sizeof(struct ibmvfc_npiv_logout_mad);
4153
4154 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4155
4156 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4157 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4158 else
4159 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4160}
4161
4162/**
Brian King072b91f2008-07-01 13:14:30 -05004163 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4164 * @vhost: ibmvfc host struct
4165 *
4166 * Returns:
4167 * 1 if work to do / 0 if not
4168 **/
4169static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4170{
4171 struct ibmvfc_target *tgt;
4172
4173 list_for_each_entry(tgt, &vhost->targets, queue) {
4174 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4175 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4176 return 1;
4177 }
4178
4179 return 0;
4180}
4181
4182/**
4183 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4184 * @vhost: ibmvfc host struct
4185 *
4186 * Returns:
4187 * 1 if work to do / 0 if not
4188 **/
4189static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4190{
4191 struct ibmvfc_target *tgt;
4192
4193 if (kthread_should_stop())
4194 return 1;
4195 switch (vhost->action) {
4196 case IBMVFC_HOST_ACTION_NONE:
4197 case IBMVFC_HOST_ACTION_INIT_WAIT:
Brian King79111d02009-05-28 16:17:29 -05004198 case IBMVFC_HOST_ACTION_LOGO_WAIT:
Brian King072b91f2008-07-01 13:14:30 -05004199 return 0;
4200 case IBMVFC_HOST_ACTION_TGT_INIT:
4201 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4202 if (vhost->discovery_threads == disc_threads)
4203 return 0;
4204 list_for_each_entry(tgt, &vhost->targets, queue)
4205 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4206 return 1;
4207 list_for_each_entry(tgt, &vhost->targets, queue)
4208 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4209 return 0;
4210 return 1;
Brian King79111d02009-05-28 16:17:29 -05004211 case IBMVFC_HOST_ACTION_LOGO:
Brian King072b91f2008-07-01 13:14:30 -05004212 case IBMVFC_HOST_ACTION_INIT:
4213 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
Brian King072b91f2008-07-01 13:14:30 -05004214 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05004215 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05004216 case IBMVFC_HOST_ACTION_QUERY:
Brian King73ee5d82010-06-17 13:55:13 -05004217 case IBMVFC_HOST_ACTION_RESET:
4218 case IBMVFC_HOST_ACTION_REENABLE:
Brian King072b91f2008-07-01 13:14:30 -05004219 default:
4220 break;
4221 };
4222
4223 return 1;
4224}
4225
4226/**
4227 * ibmvfc_work_to_do - Is there task level work to do?
4228 * @vhost: ibmvfc host struct
4229 *
4230 * Returns:
4231 * 1 if work to do / 0 if not
4232 **/
4233static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4234{
4235 unsigned long flags;
4236 int rc;
4237
4238 spin_lock_irqsave(vhost->host->host_lock, flags);
4239 rc = __ibmvfc_work_to_do(vhost);
4240 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4241 return rc;
4242}
4243
4244/**
4245 * ibmvfc_log_ae - Log async events if necessary
4246 * @vhost: ibmvfc host struct
4247 * @events: events to log
4248 *
4249 **/
4250static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4251{
4252 if (events & IBMVFC_AE_RSCN)
4253 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4254 if ((events & IBMVFC_AE_LINKDOWN) &&
4255 vhost->state >= IBMVFC_HALTED)
4256 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4257 if ((events & IBMVFC_AE_LINKUP) &&
4258 vhost->state == IBMVFC_INITIALIZING)
4259 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4260}
4261
4262/**
4263 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4264 * @tgt: ibmvfc target struct
4265 *
4266 **/
4267static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4268{
4269 struct ibmvfc_host *vhost = tgt->vhost;
Brian King43c8da92009-05-28 16:17:28 -05004270 struct fc_rport *rport;
Brian King072b91f2008-07-01 13:14:30 -05004271 unsigned long flags;
4272
4273 tgt_dbg(tgt, "Adding rport\n");
4274 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4275 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King43c8da92009-05-28 16:17:28 -05004276
4277 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4278 tgt_dbg(tgt, "Deleting rport\n");
4279 list_del(&tgt->queue);
Brian Kingd5da3042010-08-05 16:38:31 -05004280 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
Brian King43c8da92009-05-28 16:17:28 -05004281 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4282 fc_remote_port_delete(rport);
4283 del_timer_sync(&tgt->timer);
4284 kref_put(&tgt->kref, ibmvfc_release_tgt);
4285 return;
Brian Kingd5da3042010-08-05 16:38:31 -05004286 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4287 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4288 return;
Brian King43c8da92009-05-28 16:17:28 -05004289 }
4290
Brian King072b91f2008-07-01 13:14:30 -05004291 if (rport) {
4292 tgt_dbg(tgt, "rport add succeeded\n");
Brian King43c8da92009-05-28 16:17:28 -05004293 tgt->rport = rport;
Brian King072b91f2008-07-01 13:14:30 -05004294 rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
4295 rport->supported_classes = 0;
Brian King52d7e862008-07-22 08:31:46 -05004296 tgt->target_id = rport->scsi_target_id;
Brian King072b91f2008-07-01 13:14:30 -05004297 if (tgt->service_parms.class1_parms[0] & 0x80000000)
4298 rport->supported_classes |= FC_COS_CLASS1;
4299 if (tgt->service_parms.class2_parms[0] & 0x80000000)
4300 rport->supported_classes |= FC_COS_CLASS2;
4301 if (tgt->service_parms.class3_parms[0] & 0x80000000)
4302 rport->supported_classes |= FC_COS_CLASS3;
Brian Kingd31429e2009-10-19 15:07:54 -05004303 if (rport->rqst_q)
Martin K. Petersen8a783622010-02-26 00:20:39 -05004304 blk_queue_max_segments(rport->rqst_q, 1);
Brian King072b91f2008-07-01 13:14:30 -05004305 } else
4306 tgt_dbg(tgt, "rport add failed\n");
4307 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4308}
4309
4310/**
4311 * ibmvfc_do_work - Do task level work
4312 * @vhost: ibmvfc host struct
4313 *
4314 **/
4315static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4316{
4317 struct ibmvfc_target *tgt;
4318 unsigned long flags;
4319 struct fc_rport *rport;
Brian King73ee5d82010-06-17 13:55:13 -05004320 int rc;
Brian King072b91f2008-07-01 13:14:30 -05004321
4322 ibmvfc_log_ae(vhost, vhost->events_to_log);
4323 spin_lock_irqsave(vhost->host->host_lock, flags);
4324 vhost->events_to_log = 0;
4325 switch (vhost->action) {
4326 case IBMVFC_HOST_ACTION_NONE:
Brian King79111d02009-05-28 16:17:29 -05004327 case IBMVFC_HOST_ACTION_LOGO_WAIT:
Brian King072b91f2008-07-01 13:14:30 -05004328 case IBMVFC_HOST_ACTION_INIT_WAIT:
4329 break;
Brian King73ee5d82010-06-17 13:55:13 -05004330 case IBMVFC_HOST_ACTION_RESET:
4331 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4332 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4333 rc = ibmvfc_reset_crq(vhost);
4334 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian Kingd24099d2010-09-21 10:17:11 -05004335 if (rc == H_CLOSED)
4336 vio_enable_interrupts(to_vio_dev(vhost->dev));
Brian King38553562011-06-03 08:23:20 -05004337 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4338 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
Brian King73ee5d82010-06-17 13:55:13 -05004339 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4340 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4341 }
4342 break;
4343 case IBMVFC_HOST_ACTION_REENABLE:
4344 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4345 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4346 rc = ibmvfc_reenable_crq_queue(vhost);
4347 spin_lock_irqsave(vhost->host->host_lock, flags);
4348 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4349 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4350 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4351 }
4352 break;
Brian King79111d02009-05-28 16:17:29 -05004353 case IBMVFC_HOST_ACTION_LOGO:
4354 vhost->job_step(vhost);
4355 break;
Brian King072b91f2008-07-01 13:14:30 -05004356 case IBMVFC_HOST_ACTION_INIT:
4357 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
Brian King1c41fa822008-12-03 11:02:54 -06004358 if (vhost->delay_init) {
4359 vhost->delay_init = 0;
4360 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian Kingd2131b32008-12-18 09:26:51 -06004361 ssleep(15);
Brian King1c41fa822008-12-03 11:02:54 -06004362 return;
4363 } else
4364 vhost->job_step(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004365 break;
4366 case IBMVFC_HOST_ACTION_QUERY:
4367 list_for_each_entry(tgt, &vhost->targets, queue)
4368 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4369 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4370 break;
4371 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4372 list_for_each_entry(tgt, &vhost->targets, queue) {
4373 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4374 tgt->job_step(tgt);
4375 break;
4376 }
4377 }
4378
4379 if (!ibmvfc_dev_init_to_do(vhost))
4380 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4381 break;
4382 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05004383 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05004384 list_for_each_entry(tgt, &vhost->targets, queue) {
4385 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4386 tgt_dbg(tgt, "Deleting rport\n");
4387 rport = tgt->rport;
4388 tgt->rport = NULL;
4389 list_del(&tgt->queue);
Brian Kingd5da3042010-08-05 16:38:31 -05004390 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
Brian King072b91f2008-07-01 13:14:30 -05004391 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4392 if (rport)
4393 fc_remote_port_delete(rport);
Brian King10501e12009-03-20 15:44:39 -05004394 del_timer_sync(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05004395 kref_put(&tgt->kref, ibmvfc_release_tgt);
4396 return;
4397 }
4398 }
4399
4400 if (vhost->state == IBMVFC_INITIALIZING) {
Brian King10e79492008-10-29 08:46:45 -05004401 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
Brian King43c8da92009-05-28 16:17:28 -05004402 if (vhost->reinit) {
4403 vhost->reinit = 0;
4404 scsi_block_requests(vhost->host);
4405 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4406 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4407 } else {
4408 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4409 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4410 wake_up(&vhost->init_wait_q);
4411 schedule_work(&vhost->rport_add_work_q);
4412 vhost->init_retries = 0;
4413 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4414 scsi_unblock_requests(vhost->host);
4415 }
4416
Brian King10e79492008-10-29 08:46:45 -05004417 return;
4418 } else {
4419 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4420 vhost->job_step = ibmvfc_discover_targets;
4421 }
Brian King072b91f2008-07-01 13:14:30 -05004422 } else {
4423 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4424 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4425 scsi_unblock_requests(vhost->host);
4426 wake_up(&vhost->init_wait_q);
4427 return;
4428 }
4429 break;
4430 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4431 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4432 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4433 ibmvfc_alloc_targets(vhost);
4434 spin_lock_irqsave(vhost->host->host_lock, flags);
4435 break;
4436 case IBMVFC_HOST_ACTION_TGT_INIT:
4437 list_for_each_entry(tgt, &vhost->targets, queue) {
4438 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4439 tgt->job_step(tgt);
4440 break;
4441 }
4442 }
4443
Brian King10e79492008-10-29 08:46:45 -05004444 if (!ibmvfc_dev_init_to_do(vhost))
4445 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
Brian King072b91f2008-07-01 13:14:30 -05004446 break;
Brian King072b91f2008-07-01 13:14:30 -05004447 default:
4448 break;
4449 };
4450
4451 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4452}
4453
4454/**
4455 * ibmvfc_work - Do task level work
4456 * @data: ibmvfc host struct
4457 *
4458 * Returns:
4459 * zero
4460 **/
4461static int ibmvfc_work(void *data)
4462{
4463 struct ibmvfc_host *vhost = data;
4464 int rc;
4465
4466 set_user_nice(current, -20);
4467
4468 while (1) {
4469 rc = wait_event_interruptible(vhost->work_wait_q,
4470 ibmvfc_work_to_do(vhost));
4471
4472 BUG_ON(rc);
4473
4474 if (kthread_should_stop())
4475 break;
4476
4477 ibmvfc_do_work(vhost);
4478 }
4479
4480 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4481 return 0;
4482}
4483
4484/**
4485 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4486 * @vhost: ibmvfc host struct
4487 *
4488 * Allocates a page for messages, maps it for dma, and registers
4489 * the crq with the hypervisor.
4490 *
4491 * Return value:
4492 * zero on success / other on failure
4493 **/
4494static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4495{
4496 int rc, retrc = -ENOMEM;
4497 struct device *dev = vhost->dev;
4498 struct vio_dev *vdev = to_vio_dev(dev);
4499 struct ibmvfc_crq_queue *crq = &vhost->crq;
4500
4501 ENTER;
4502 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4503
4504 if (!crq->msgs)
4505 return -ENOMEM;
4506
4507 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4508 crq->msg_token = dma_map_single(dev, crq->msgs,
4509 PAGE_SIZE, DMA_BIDIRECTIONAL);
4510
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07004511 if (dma_mapping_error(dev, crq->msg_token))
Brian King072b91f2008-07-01 13:14:30 -05004512 goto map_failed;
4513
4514 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4515 crq->msg_token, PAGE_SIZE);
4516
4517 if (rc == H_RESOURCE)
4518 /* maybe kexecing and resource is busy. try a reset */
4519 retrc = rc = ibmvfc_reset_crq(vhost);
4520
4521 if (rc == H_CLOSED)
4522 dev_warn(dev, "Partner adapter not ready\n");
4523 else if (rc) {
4524 dev_warn(dev, "Error %d opening adapter\n", rc);
4525 goto reg_crq_failed;
4526 }
4527
4528 retrc = 0;
4529
Brian King039a0892009-03-20 15:44:35 -05004530 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4531
Brian King072b91f2008-07-01 13:14:30 -05004532 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4533 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4534 goto req_irq_failed;
4535 }
4536
4537 if ((rc = vio_enable_interrupts(vdev))) {
4538 dev_err(dev, "Error %d enabling interrupts\n", rc);
4539 goto req_irq_failed;
4540 }
4541
4542 crq->cur = 0;
4543 LEAVE;
4544 return retrc;
4545
4546req_irq_failed:
Brian King039a0892009-03-20 15:44:35 -05004547 tasklet_kill(&vhost->tasklet);
Brian King072b91f2008-07-01 13:14:30 -05004548 do {
4549 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4550 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4551reg_crq_failed:
4552 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4553map_failed:
4554 free_page((unsigned long)crq->msgs);
4555 return retrc;
4556}
4557
4558/**
4559 * ibmvfc_free_mem - Free memory for vhost
4560 * @vhost: ibmvfc host struct
4561 *
4562 * Return value:
4563 * none
4564 **/
4565static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4566{
4567 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4568
4569 ENTER;
4570 mempool_destroy(vhost->tgt_pool);
4571 kfree(vhost->trace);
4572 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4573 vhost->disc_buf_dma);
4574 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4575 vhost->login_buf, vhost->login_buf_dma);
4576 dma_pool_destroy(vhost->sg_pool);
4577 dma_unmap_single(vhost->dev, async_q->msg_token,
4578 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4579 free_page((unsigned long)async_q->msgs);
4580 LEAVE;
4581}
4582
4583/**
4584 * ibmvfc_alloc_mem - Allocate memory for vhost
4585 * @vhost: ibmvfc host struct
4586 *
4587 * Return value:
4588 * 0 on success / non-zero on failure
4589 **/
4590static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4591{
4592 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4593 struct device *dev = vhost->dev;
4594
4595 ENTER;
4596 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4597 if (!async_q->msgs) {
4598 dev_err(dev, "Couldn't allocate async queue.\n");
4599 goto nomem;
4600 }
4601
4602 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4603 async_q->msg_token = dma_map_single(dev, async_q->msgs,
4604 async_q->size * sizeof(*async_q->msgs),
4605 DMA_BIDIRECTIONAL);
4606
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07004607 if (dma_mapping_error(dev, async_q->msg_token)) {
Brian King072b91f2008-07-01 13:14:30 -05004608 dev_err(dev, "Failed to map async queue\n");
4609 goto free_async_crq;
4610 }
4611
4612 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4613 SG_ALL * sizeof(struct srp_direct_buf),
4614 sizeof(struct srp_direct_buf), 0);
4615
4616 if (!vhost->sg_pool) {
4617 dev_err(dev, "Failed to allocate sg pool\n");
4618 goto unmap_async_crq;
4619 }
4620
4621 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4622 &vhost->login_buf_dma, GFP_KERNEL);
4623
4624 if (!vhost->login_buf) {
4625 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4626 goto free_sg_pool;
4627 }
4628
4629 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
4630 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
4631 &vhost->disc_buf_dma, GFP_KERNEL);
4632
4633 if (!vhost->disc_buf) {
4634 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
4635 goto free_login_buffer;
4636 }
4637
4638 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
4639 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
4640
4641 if (!vhost->trace)
4642 goto free_disc_buffer;
4643
Sage Weild6886692009-08-03 13:54:47 -07004644 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
Brian King072b91f2008-07-01 13:14:30 -05004645 sizeof(struct ibmvfc_target));
4646
4647 if (!vhost->tgt_pool) {
4648 dev_err(dev, "Couldn't allocate target memory pool\n");
4649 goto free_trace;
4650 }
4651
4652 LEAVE;
4653 return 0;
4654
4655free_trace:
4656 kfree(vhost->trace);
4657free_disc_buffer:
4658 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
4659 vhost->disc_buf_dma);
4660free_login_buffer:
4661 dma_free_coherent(dev, sizeof(*vhost->login_buf),
4662 vhost->login_buf, vhost->login_buf_dma);
4663free_sg_pool:
4664 dma_pool_destroy(vhost->sg_pool);
4665unmap_async_crq:
4666 dma_unmap_single(dev, async_q->msg_token,
4667 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4668free_async_crq:
4669 free_page((unsigned long)async_q->msgs);
4670nomem:
4671 LEAVE;
4672 return -ENOMEM;
4673}
4674
4675/**
Brian King43c8da92009-05-28 16:17:28 -05004676 * ibmvfc_rport_add_thread - Worker thread for rport adds
4677 * @work: work struct
4678 *
4679 **/
4680static void ibmvfc_rport_add_thread(struct work_struct *work)
4681{
4682 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
4683 rport_add_work_q);
4684 struct ibmvfc_target *tgt;
4685 struct fc_rport *rport;
4686 unsigned long flags;
4687 int did_work;
4688
4689 ENTER;
4690 spin_lock_irqsave(vhost->host->host_lock, flags);
4691 do {
4692 did_work = 0;
4693 if (vhost->state != IBMVFC_ACTIVE)
4694 break;
4695
4696 list_for_each_entry(tgt, &vhost->targets, queue) {
4697 if (tgt->add_rport) {
4698 did_work = 1;
4699 tgt->add_rport = 0;
4700 kref_get(&tgt->kref);
4701 rport = tgt->rport;
4702 if (!rport) {
4703 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4704 ibmvfc_tgt_add_rport(tgt);
4705 } else if (get_device(&rport->dev)) {
4706 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4707 tgt_dbg(tgt, "Setting rport roles\n");
4708 fc_remote_port_rolechg(rport, tgt->ids.roles);
4709 put_device(&rport->dev);
4710 }
4711
4712 kref_put(&tgt->kref, ibmvfc_release_tgt);
4713 spin_lock_irqsave(vhost->host->host_lock, flags);
4714 break;
4715 }
4716 }
4717 } while(did_work);
4718
4719 if (vhost->state == IBMVFC_ACTIVE)
4720 vhost->scan_complete = 1;
4721 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4722 LEAVE;
4723}
4724
4725/**
Brian King072b91f2008-07-01 13:14:30 -05004726 * ibmvfc_probe - Adapter hot plug add entry point
4727 * @vdev: vio device struct
4728 * @id: vio device id struct
4729 *
4730 * Return value:
4731 * 0 on success / non-zero on failure
4732 **/
4733static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4734{
4735 struct ibmvfc_host *vhost;
4736 struct Scsi_Host *shost;
4737 struct device *dev = &vdev->dev;
4738 int rc = -ENOMEM;
4739
4740 ENTER;
4741 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
4742 if (!shost) {
4743 dev_err(dev, "Couldn't allocate host data\n");
4744 goto out;
4745 }
4746
4747 shost->transportt = ibmvfc_transport_template;
4748 shost->can_queue = max_requests;
4749 shost->max_lun = max_lun;
4750 shost->max_id = max_targets;
4751 shost->max_sectors = IBMVFC_MAX_SECTORS;
4752 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
4753 shost->unique_id = shost->host_no;
4754
4755 vhost = shost_priv(shost);
4756 INIT_LIST_HEAD(&vhost->sent);
4757 INIT_LIST_HEAD(&vhost->free);
4758 INIT_LIST_HEAD(&vhost->targets);
4759 sprintf(vhost->name, IBMVFC_NAME);
4760 vhost->host = shost;
4761 vhost->dev = dev;
4762 vhost->partition_number = -1;
4763 vhost->log_level = log_level;
Brian King10501e12009-03-20 15:44:39 -05004764 vhost->task_set = 1;
Brian King072b91f2008-07-01 13:14:30 -05004765 strcpy(vhost->partition_name, "UNKNOWN");
4766 init_waitqueue_head(&vhost->work_wait_q);
4767 init_waitqueue_head(&vhost->init_wait_q);
Brian King43c8da92009-05-28 16:17:28 -05004768 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
Brian Kingd31429e2009-10-19 15:07:54 -05004769 mutex_init(&vhost->passthru_mutex);
Brian King072b91f2008-07-01 13:14:30 -05004770
4771 if ((rc = ibmvfc_alloc_mem(vhost)))
4772 goto free_scsi_host;
4773
4774 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
4775 shost->host_no);
4776
4777 if (IS_ERR(vhost->work_thread)) {
4778 dev_err(dev, "Couldn't create kernel thread: %ld\n",
4779 PTR_ERR(vhost->work_thread));
4780 goto free_host_mem;
4781 }
4782
4783 if ((rc = ibmvfc_init_crq(vhost))) {
4784 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4785 goto kill_kthread;
4786 }
4787
4788 if ((rc = ibmvfc_init_event_pool(vhost))) {
4789 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4790 goto release_crq;
4791 }
4792
4793 if ((rc = scsi_add_host(shost, dev)))
4794 goto release_event_pool;
4795
Mike Christiea5110f22010-09-15 16:52:29 -05004796 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
4797
Brian King072b91f2008-07-01 13:14:30 -05004798 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4799 &ibmvfc_trace_attr))) {
4800 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4801 goto remove_shost;
4802 }
4803
Brian Kingd31429e2009-10-19 15:07:54 -05004804 if (shost_to_fc_host(shost)->rqst_q)
Martin K. Petersen8a783622010-02-26 00:20:39 -05004805 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
Brian King072b91f2008-07-01 13:14:30 -05004806 dev_set_drvdata(dev, vhost);
4807 spin_lock(&ibmvfc_driver_lock);
4808 list_add_tail(&vhost->queue, &ibmvfc_head);
4809 spin_unlock(&ibmvfc_driver_lock);
4810
4811 ibmvfc_send_crq_init(vhost);
4812 scsi_scan_host(shost);
4813 return 0;
4814
4815remove_shost:
4816 scsi_remove_host(shost);
4817release_event_pool:
4818 ibmvfc_free_event_pool(vhost);
4819release_crq:
4820 ibmvfc_release_crq_queue(vhost);
4821kill_kthread:
4822 kthread_stop(vhost->work_thread);
4823free_host_mem:
4824 ibmvfc_free_mem(vhost);
4825free_scsi_host:
4826 scsi_host_put(shost);
4827out:
4828 LEAVE;
4829 return rc;
4830}
4831
4832/**
4833 * ibmvfc_remove - Adapter hot plug remove entry point
4834 * @vdev: vio device struct
4835 *
4836 * Return value:
4837 * 0
4838 **/
4839static int ibmvfc_remove(struct vio_dev *vdev)
4840{
4841 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4842 unsigned long flags;
4843
4844 ENTER;
4845 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
Brian King70431102009-10-19 15:07:48 -05004846
4847 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King2d0da2a2008-07-22 08:31:42 -05004848 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
Brian King70431102009-10-19 15:07:48 -05004849 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4850
Brian King2d0da2a2008-07-22 08:31:42 -05004851 ibmvfc_wait_while_resetting(vhost);
4852 ibmvfc_release_crq_queue(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004853 kthread_stop(vhost->work_thread);
4854 fc_remove_host(vhost->host);
4855 scsi_remove_host(vhost->host);
Brian King072b91f2008-07-01 13:14:30 -05004856
4857 spin_lock_irqsave(vhost->host->host_lock, flags);
4858 ibmvfc_purge_requests(vhost, DID_ERROR);
4859 ibmvfc_free_event_pool(vhost);
4860 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4861
4862 ibmvfc_free_mem(vhost);
4863 spin_lock(&ibmvfc_driver_lock);
4864 list_del(&vhost->queue);
4865 spin_unlock(&ibmvfc_driver_lock);
4866 scsi_host_put(vhost->host);
4867 LEAVE;
4868 return 0;
4869}
4870
Brian King39c1ffe2008-07-24 04:35:48 +10004871/**
Brian Kingb0f4d4c2010-02-21 10:37:58 -06004872 * ibmvfc_resume - Resume from suspend
4873 * @dev: device struct
4874 *
4875 * We may have lost an interrupt across suspend/resume, so kick the
4876 * interrupt handler
4877 *
4878 */
4879static int ibmvfc_resume(struct device *dev)
4880{
4881 unsigned long flags;
4882 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
4883 struct vio_dev *vdev = to_vio_dev(dev);
4884
4885 spin_lock_irqsave(vhost->host->host_lock, flags);
4886 vio_disable_interrupts(vdev);
4887 tasklet_schedule(&vhost->tasklet);
4888 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4889 return 0;
4890}
4891
4892/**
Brian King39c1ffe2008-07-24 04:35:48 +10004893 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4894 * @vdev: vio device struct
4895 *
4896 * Return value:
4897 * Number of bytes the driver will need to DMA map at the same time in
4898 * order to perform well.
4899 */
4900static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4901{
4902 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4903 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4904}
4905
Brian King072b91f2008-07-01 13:14:30 -05004906static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
4907 {"fcp", "IBM,vfc-client"},
4908 { "", "" }
4909};
4910MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4911
Brian Kingb0f4d4c2010-02-21 10:37:58 -06004912static struct dev_pm_ops ibmvfc_pm_ops = {
4913 .resume = ibmvfc_resume
4914};
4915
Brian King072b91f2008-07-01 13:14:30 -05004916static struct vio_driver ibmvfc_driver = {
4917 .id_table = ibmvfc_device_table,
4918 .probe = ibmvfc_probe,
4919 .remove = ibmvfc_remove,
Brian King39c1ffe2008-07-24 04:35:48 +10004920 .get_desired_dma = ibmvfc_get_desired_dma,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00004921 .name = IBMVFC_NAME,
4922 .pm = &ibmvfc_pm_ops,
Brian King072b91f2008-07-01 13:14:30 -05004923};
4924
4925static struct fc_function_template ibmvfc_transport_functions = {
4926 .show_host_fabric_name = 1,
4927 .show_host_node_name = 1,
4928 .show_host_port_name = 1,
4929 .show_host_supported_classes = 1,
4930 .show_host_port_type = 1,
4931 .show_host_port_id = 1,
Brian King9ab36102009-03-20 15:44:38 -05004932 .show_host_maxframe_size = 1,
Brian King072b91f2008-07-01 13:14:30 -05004933
4934 .get_host_port_state = ibmvfc_get_host_port_state,
4935 .show_host_port_state = 1,
4936
4937 .get_host_speed = ibmvfc_get_host_speed,
4938 .show_host_speed = 1,
4939
4940 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4941 .terminate_rport_io = ibmvfc_terminate_rport_io,
4942
4943 .show_rport_maxframe_size = 1,
4944 .show_rport_supported_classes = 1,
4945
4946 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4947 .show_rport_dev_loss_tmo = 1,
4948
4949 .get_starget_node_name = ibmvfc_get_starget_node_name,
4950 .show_starget_node_name = 1,
4951
4952 .get_starget_port_name = ibmvfc_get_starget_port_name,
4953 .show_starget_port_name = 1,
4954
4955 .get_starget_port_id = ibmvfc_get_starget_port_id,
4956 .show_starget_port_id = 1,
Brian Kingd31429e2009-10-19 15:07:54 -05004957
4958 .bsg_request = ibmvfc_bsg_request,
4959 .bsg_timeout = ibmvfc_bsg_timeout,
Brian King072b91f2008-07-01 13:14:30 -05004960};
4961
4962/**
4963 * ibmvfc_module_init - Initialize the ibmvfc module
4964 *
4965 * Return value:
4966 * 0 on success / other on failure
4967 **/
4968static int __init ibmvfc_module_init(void)
4969{
4970 int rc;
4971
4972 if (!firmware_has_feature(FW_FEATURE_VIO))
4973 return -ENODEV;
4974
4975 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4976 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4977
4978 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4979 if (!ibmvfc_transport_template)
4980 return -ENOMEM;
4981
4982 rc = vio_register_driver(&ibmvfc_driver);
4983 if (rc)
4984 fc_release_transport(ibmvfc_transport_template);
4985 return rc;
4986}
4987
4988/**
4989 * ibmvfc_module_exit - Teardown the ibmvfc module
4990 *
4991 * Return value:
4992 * nothing
4993 **/
4994static void __exit ibmvfc_module_exit(void)
4995{
4996 vio_unregister_driver(&ibmvfc_driver);
4997 fc_release_transport(ibmvfc_transport_template);
4998}
4999
5000module_init(ibmvfc_module_init);
5001module_exit(ibmvfc_module_exit);