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