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