blob: 182c8e75aaf01fa20268f7c3c77e68c4c169eb25 [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>
42#include "ibmvfc.h"
43
44static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
45static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
46static unsigned int max_lun = IBMVFC_MAX_LUN;
47static unsigned int max_targets = IBMVFC_MAX_TARGETS;
48static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
49static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
50static unsigned int dev_loss_tmo = IBMVFC_DEV_LOSS_TMO;
51static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
52static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
53static LIST_HEAD(ibmvfc_head);
54static DEFINE_SPINLOCK(ibmvfc_driver_lock);
55static struct scsi_transport_template *ibmvfc_transport_template;
56
57MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
58MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
59MODULE_LICENSE("GPL");
60MODULE_VERSION(IBMVFC_DRIVER_VERSION);
61
62module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
63MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
64 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
65module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
66MODULE_PARM_DESC(default_timeout,
67 "Default timeout in seconds for initialization and EH commands. "
68 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
69module_param_named(max_requests, max_requests, uint, S_IRUGO);
70MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
71 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
72module_param_named(max_lun, max_lun, uint, S_IRUGO);
73MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
74 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
75module_param_named(max_targets, max_targets, uint, S_IRUGO);
76MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
77 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
Brian King545ef9a2009-03-20 15:44:37 -050078module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
Brian King072b91f2008-07-01 13:14:30 -050079MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
80 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
81module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
82MODULE_PARM_DESC(debug, "Enable driver debug information. "
83 "[Default=" __stringify(IBMVFC_DEBUG) "]");
84module_param_named(dev_loss_tmo, dev_loss_tmo, uint, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(dev_loss_tmo, "Maximum number of seconds that the FC "
86 "transport should insulate the loss of a remote port. Once this "
87 "value is exceeded, the scsi target is removed. "
88 "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO) "]");
89module_param_named(log_level, log_level, uint, 0);
90MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
91 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
92
93static const struct {
94 u16 status;
95 u16 error;
96 u8 result;
97 u8 retry;
98 int log;
99 char *name;
100} cmd_status [] = {
101 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
102 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
103 { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
Brian King752b3232008-12-15 17:09:05 -0600104 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
Brian King072b91f2008-07-01 13:14:30 -0500105 { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
106 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
107 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
108 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
109 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
110 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
111 { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
112 { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
113 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 0, 0, "link halted" },
114 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
115
116 { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
117 { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
Brian King752b3232008-12-15 17:09:05 -0600118 { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
119 { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
Brian King072b91f2008-07-01 13:14:30 -0500120 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
Brian King752b3232008-12-15 17:09:05 -0600121 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
122 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
Brian King072b91f2008-07-01 13:14:30 -0500123 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
Brian King646d3852008-11-14 13:33:53 -0600124 { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
Brian King072b91f2008-07-01 13:14:30 -0500125 { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
126
127 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
128 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
129 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
130 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
131 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
132 { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
133 { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
134 { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
135 { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
136 { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
137 { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
138
139 { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
140};
141
142static void ibmvfc_npiv_login(struct ibmvfc_host *);
143static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
144static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
145static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
146
147static const char *unknown_error = "unknown error";
148
149#ifdef CONFIG_SCSI_IBMVFC_TRACE
150/**
151 * ibmvfc_trc_start - Log a start trace entry
152 * @evt: ibmvfc event struct
153 *
154 **/
155static void ibmvfc_trc_start(struct ibmvfc_event *evt)
156{
157 struct ibmvfc_host *vhost = evt->vhost;
158 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
159 struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
160 struct ibmvfc_trace_entry *entry;
161
162 entry = &vhost->trace[vhost->trace_index++];
163 entry->evt = evt;
164 entry->time = jiffies;
165 entry->fmt = evt->crq.format;
166 entry->type = IBMVFC_TRC_START;
167
168 switch (entry->fmt) {
169 case IBMVFC_CMD_FORMAT:
170 entry->op_code = vfc_cmd->iu.cdb[0];
171 entry->scsi_id = vfc_cmd->tgt_scsi_id;
172 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
173 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
174 entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
175 break;
176 case IBMVFC_MAD_FORMAT:
177 entry->op_code = mad->opcode;
178 break;
179 default:
180 break;
181 };
182}
183
184/**
185 * ibmvfc_trc_end - Log an end trace entry
186 * @evt: ibmvfc event struct
187 *
188 **/
189static void ibmvfc_trc_end(struct ibmvfc_event *evt)
190{
191 struct ibmvfc_host *vhost = evt->vhost;
192 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
193 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
194 struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
195
196 entry->evt = evt;
197 entry->time = jiffies;
198 entry->fmt = evt->crq.format;
199 entry->type = IBMVFC_TRC_END;
200
201 switch (entry->fmt) {
202 case IBMVFC_CMD_FORMAT:
203 entry->op_code = vfc_cmd->iu.cdb[0];
204 entry->scsi_id = vfc_cmd->tgt_scsi_id;
205 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
206 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
207 entry->u.end.status = vfc_cmd->status;
208 entry->u.end.error = vfc_cmd->error;
209 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
210 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
211 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
212 break;
213 case IBMVFC_MAD_FORMAT:
214 entry->op_code = mad->opcode;
215 entry->u.end.status = mad->status;
216 break;
217 default:
218 break;
219
220 };
221}
222
223#else
224#define ibmvfc_trc_start(evt) do { } while (0)
225#define ibmvfc_trc_end(evt) do { } while (0)
226#endif
227
228/**
229 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
230 * @status: status / error class
231 * @error: error
232 *
233 * Return value:
234 * index into cmd_status / -EINVAL on failure
235 **/
236static int ibmvfc_get_err_index(u16 status, u16 error)
237{
238 int i;
239
240 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
241 if ((cmd_status[i].status & status) == cmd_status[i].status &&
242 cmd_status[i].error == error)
243 return i;
244
245 return -EINVAL;
246}
247
248/**
249 * ibmvfc_get_cmd_error - Find the error description for the fcp response
250 * @status: status / error class
251 * @error: error
252 *
253 * Return value:
254 * error description string
255 **/
256static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
257{
258 int rc = ibmvfc_get_err_index(status, error);
259 if (rc >= 0)
260 return cmd_status[rc].name;
261 return unknown_error;
262}
263
264/**
265 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
266 * @vfc_cmd: ibmvfc command struct
267 *
268 * Return value:
269 * SCSI result value to return for completed command
270 **/
271static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
272{
273 int err;
274 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
275 int fc_rsp_len = rsp->fcp_rsp_len;
276
277 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
Brian King4a2837d2009-05-28 16:17:22 -0500278 ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
Brian King072b91f2008-07-01 13:14:30 -0500279 rsp->data.info.rsp_code))
280 return DID_ERROR << 16;
281
Brian King072b91f2008-07-01 13:14:30 -0500282 err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
283 if (err >= 0)
284 return rsp->scsi_status | (cmd_status[err].result << 16);
285 return rsp->scsi_status | (DID_ERROR << 16);
286}
287
288/**
289 * ibmvfc_retry_cmd - Determine if error status is retryable
290 * @status: status / error class
291 * @error: error
292 *
293 * Return value:
294 * 1 if error should be retried / 0 if it should not
295 **/
296static int ibmvfc_retry_cmd(u16 status, u16 error)
297{
298 int rc = ibmvfc_get_err_index(status, error);
299
300 if (rc >= 0)
301 return cmd_status[rc].retry;
302 return 1;
303}
304
305static const char *unknown_fc_explain = "unknown fc explain";
306
307static const struct {
308 u16 fc_explain;
309 char *name;
310} ls_explain [] = {
311 { 0x00, "no additional explanation" },
312 { 0x01, "service parameter error - options" },
313 { 0x03, "service parameter error - initiator control" },
314 { 0x05, "service parameter error - recipient control" },
315 { 0x07, "service parameter error - received data field size" },
316 { 0x09, "service parameter error - concurrent seq" },
317 { 0x0B, "service parameter error - credit" },
318 { 0x0D, "invalid N_Port/F_Port_Name" },
319 { 0x0E, "invalid node/Fabric Name" },
320 { 0x0F, "invalid common service parameters" },
321 { 0x11, "invalid association header" },
322 { 0x13, "association header required" },
323 { 0x15, "invalid originator S_ID" },
324 { 0x17, "invalid OX_ID-RX-ID combination" },
325 { 0x19, "command (request) already in progress" },
326 { 0x1E, "N_Port Login requested" },
327 { 0x1F, "Invalid N_Port_ID" },
328};
329
330static const struct {
331 u16 fc_explain;
332 char *name;
333} gs_explain [] = {
334 { 0x00, "no additional explanation" },
335 { 0x01, "port identifier not registered" },
336 { 0x02, "port name not registered" },
337 { 0x03, "node name not registered" },
338 { 0x04, "class of service not registered" },
339 { 0x06, "initial process associator not registered" },
340 { 0x07, "FC-4 TYPEs not registered" },
341 { 0x08, "symbolic port name not registered" },
342 { 0x09, "symbolic node name not registered" },
343 { 0x0A, "port type not registered" },
344 { 0xF0, "authorization exception" },
345 { 0xF1, "authentication exception" },
346 { 0xF2, "data base full" },
347 { 0xF3, "data base empty" },
348 { 0xF4, "processing request" },
349 { 0xF5, "unable to verify connection" },
350 { 0xF6, "devices not in a common zone" },
351};
352
353/**
354 * ibmvfc_get_ls_explain - Return the FC Explain description text
355 * @status: FC Explain status
356 *
357 * Returns:
358 * error string
359 **/
360static const char *ibmvfc_get_ls_explain(u16 status)
361{
362 int i;
363
364 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
365 if (ls_explain[i].fc_explain == status)
366 return ls_explain[i].name;
367
368 return unknown_fc_explain;
369}
370
371/**
372 * ibmvfc_get_gs_explain - Return the FC Explain description text
373 * @status: FC Explain status
374 *
375 * Returns:
376 * error string
377 **/
378static const char *ibmvfc_get_gs_explain(u16 status)
379{
380 int i;
381
382 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
383 if (gs_explain[i].fc_explain == status)
384 return gs_explain[i].name;
385
386 return unknown_fc_explain;
387}
388
389static const struct {
390 enum ibmvfc_fc_type fc_type;
391 char *name;
392} fc_type [] = {
393 { IBMVFC_FABRIC_REJECT, "fabric reject" },
394 { IBMVFC_PORT_REJECT, "port reject" },
395 { IBMVFC_LS_REJECT, "ELS reject" },
396 { IBMVFC_FABRIC_BUSY, "fabric busy" },
397 { IBMVFC_PORT_BUSY, "port busy" },
398 { IBMVFC_BASIC_REJECT, "basic reject" },
399};
400
401static const char *unknown_fc_type = "unknown fc type";
402
403/**
404 * ibmvfc_get_fc_type - Return the FC Type description text
405 * @status: FC Type error status
406 *
407 * Returns:
408 * error string
409 **/
410static const char *ibmvfc_get_fc_type(u16 status)
411{
412 int i;
413
414 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
415 if (fc_type[i].fc_type == status)
416 return fc_type[i].name;
417
418 return unknown_fc_type;
419}
420
421/**
422 * ibmvfc_set_tgt_action - Set the next init action for the target
423 * @tgt: ibmvfc target struct
424 * @action: action to perform
425 *
426 **/
427static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
428 enum ibmvfc_target_action action)
429{
430 switch (tgt->action) {
431 case IBMVFC_TGT_ACTION_DEL_RPORT:
432 break;
433 default:
434 tgt->action = action;
435 break;
436 }
437}
438
439/**
440 * ibmvfc_set_host_state - Set the state for the host
441 * @vhost: ibmvfc host struct
442 * @state: state to set host to
443 *
444 * Returns:
445 * 0 if state changed / non-zero if not changed
446 **/
447static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
448 enum ibmvfc_host_state state)
449{
450 int rc = 0;
451
452 switch (vhost->state) {
453 case IBMVFC_HOST_OFFLINE:
454 rc = -EINVAL;
455 break;
456 default:
457 vhost->state = state;
458 break;
459 };
460
461 return rc;
462}
463
464/**
465 * ibmvfc_set_host_action - Set the next init action for the host
466 * @vhost: ibmvfc host struct
467 * @action: action to perform
468 *
469 **/
470static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
471 enum ibmvfc_host_action action)
472{
473 switch (action) {
474 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
475 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
476 vhost->action = action;
477 break;
478 case IBMVFC_HOST_ACTION_INIT_WAIT:
479 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
480 vhost->action = action;
481 break;
482 case IBMVFC_HOST_ACTION_QUERY:
483 switch (vhost->action) {
484 case IBMVFC_HOST_ACTION_INIT_WAIT:
485 case IBMVFC_HOST_ACTION_NONE:
486 case IBMVFC_HOST_ACTION_TGT_ADD:
487 vhost->action = action;
488 break;
489 default:
490 break;
491 };
492 break;
493 case IBMVFC_HOST_ACTION_TGT_INIT:
494 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
495 vhost->action = action;
496 break;
497 case IBMVFC_HOST_ACTION_INIT:
498 case IBMVFC_HOST_ACTION_TGT_DEL:
499 case IBMVFC_HOST_ACTION_QUERY_TGTS:
Brian King10e79492008-10-29 08:46:45 -0500500 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -0500501 case IBMVFC_HOST_ACTION_TGT_ADD:
502 case IBMVFC_HOST_ACTION_NONE:
503 default:
504 vhost->action = action;
505 break;
506 };
507}
508
509/**
510 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
511 * @vhost: ibmvfc host struct
512 *
513 * Return value:
514 * nothing
515 **/
516static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
517{
518 if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
Brian King2d0da2a2008-07-22 08:31:42 -0500519 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
520 scsi_block_requests(vhost->host);
521 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
522 }
Brian King072b91f2008-07-01 13:14:30 -0500523 } else
524 vhost->reinit = 1;
525
526 wake_up(&vhost->work_wait_q);
527}
528
529/**
530 * ibmvfc_link_down - Handle a link down event from the adapter
531 * @vhost: ibmvfc host struct
532 * @state: ibmvfc host state to enter
533 *
534 **/
535static void ibmvfc_link_down(struct ibmvfc_host *vhost,
536 enum ibmvfc_host_state state)
537{
538 struct ibmvfc_target *tgt;
539
540 ENTER;
541 scsi_block_requests(vhost->host);
542 list_for_each_entry(tgt, &vhost->targets, queue)
543 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
544 ibmvfc_set_host_state(vhost, state);
545 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
546 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
547 wake_up(&vhost->work_wait_q);
548 LEAVE;
549}
550
551/**
552 * ibmvfc_init_host - Start host initialization
553 * @vhost: ibmvfc host struct
Brian Kingcf6f10d2008-08-15 10:59:23 -0500554 * @relogin: is this a re-login?
Brian King072b91f2008-07-01 13:14:30 -0500555 *
556 * Return value:
557 * nothing
558 **/
Brian Kingcf6f10d2008-08-15 10:59:23 -0500559static void ibmvfc_init_host(struct ibmvfc_host *vhost, int relogin)
Brian King072b91f2008-07-01 13:14:30 -0500560{
561 struct ibmvfc_target *tgt;
562
563 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
Brian King1c41fa822008-12-03 11:02:54 -0600564 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -0500565 dev_err(vhost->dev,
566 "Host initialization retries exceeded. Taking adapter offline\n");
567 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
568 return;
569 }
570 }
571
572 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
Brian Kingcf6f10d2008-08-15 10:59:23 -0500573 if (!relogin) {
574 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
575 vhost->async_crq.cur = 0;
576 }
577
Brian King072b91f2008-07-01 13:14:30 -0500578 list_for_each_entry(tgt, &vhost->targets, queue)
579 tgt->need_login = 1;
580 scsi_block_requests(vhost->host);
581 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
582 vhost->job_step = ibmvfc_npiv_login;
583 wake_up(&vhost->work_wait_q);
584 }
585}
586
587/**
588 * ibmvfc_send_crq - Send a CRQ
589 * @vhost: ibmvfc host struct
590 * @word1: the first 64 bits of the data
591 * @word2: the second 64 bits of the data
592 *
593 * Return value:
594 * 0 on success / other on failure
595 **/
596static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
597{
598 struct vio_dev *vdev = to_vio_dev(vhost->dev);
599 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
600}
601
602/**
603 * ibmvfc_send_crq_init - Send a CRQ init message
604 * @vhost: ibmvfc host struct
605 *
606 * Return value:
607 * 0 on success / other on failure
608 **/
609static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
610{
611 ibmvfc_dbg(vhost, "Sending CRQ init\n");
612 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
613}
614
615/**
616 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
617 * @vhost: ibmvfc host struct
618 *
619 * Return value:
620 * 0 on success / other on failure
621 **/
622static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
623{
624 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
625 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
626}
627
628/**
629 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
630 * @vhost: ibmvfc host struct
631 *
632 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
633 * the crq with the hypervisor.
634 **/
635static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
636{
637 long rc;
638 struct vio_dev *vdev = to_vio_dev(vhost->dev);
639 struct ibmvfc_crq_queue *crq = &vhost->crq;
640
641 ibmvfc_dbg(vhost, "Releasing CRQ\n");
642 free_irq(vdev->irq, vhost);
Brian King039a0892009-03-20 15:44:35 -0500643 tasklet_kill(&vhost->tasklet);
Brian King072b91f2008-07-01 13:14:30 -0500644 do {
645 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
646 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
647
648 vhost->state = IBMVFC_NO_CRQ;
649 dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
650 free_page((unsigned long)crq->msgs);
651}
652
653/**
654 * ibmvfc_reenable_crq_queue - reenables the CRQ
655 * @vhost: ibmvfc host struct
656 *
657 * Return value:
658 * 0 on success / other on failure
659 **/
660static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
661{
662 int rc;
663 struct vio_dev *vdev = to_vio_dev(vhost->dev);
664
665 /* Re-enable the CRQ */
666 do {
667 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
668 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
669
670 if (rc)
671 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
672
673 return rc;
674}
675
676/**
677 * ibmvfc_reset_crq - resets a crq after a failure
678 * @vhost: ibmvfc host struct
679 *
680 * Return value:
681 * 0 on success / other on failure
682 **/
683static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
684{
685 int rc;
686 struct vio_dev *vdev = to_vio_dev(vhost->dev);
687 struct ibmvfc_crq_queue *crq = &vhost->crq;
688
689 /* Close the CRQ */
690 do {
691 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
692 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
693
694 vhost->state = IBMVFC_NO_CRQ;
695 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
696
697 /* Clean out the queue */
698 memset(crq->msgs, 0, PAGE_SIZE);
699 crq->cur = 0;
700
701 /* And re-open it again */
702 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
703 crq->msg_token, PAGE_SIZE);
704
705 if (rc == H_CLOSED)
706 /* Adapter is good, but other end is not ready */
707 dev_warn(vhost->dev, "Partner adapter not ready\n");
708 else if (rc != 0)
709 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
710
711 return rc;
712}
713
714/**
715 * ibmvfc_valid_event - Determines if event is valid.
716 * @pool: event_pool that contains the event
717 * @evt: ibmvfc event to be checked for validity
718 *
719 * Return value:
720 * 1 if event is valid / 0 if event is not valid
721 **/
722static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
723 struct ibmvfc_event *evt)
724{
725 int index = evt - pool->events;
726 if (index < 0 || index >= pool->size) /* outside of bounds */
727 return 0;
728 if (evt != pool->events + index) /* unaligned */
729 return 0;
730 return 1;
731}
732
733/**
734 * ibmvfc_free_event - Free the specified event
735 * @evt: ibmvfc_event to be freed
736 *
737 **/
738static void ibmvfc_free_event(struct ibmvfc_event *evt)
739{
740 struct ibmvfc_host *vhost = evt->vhost;
741 struct ibmvfc_event_pool *pool = &vhost->pool;
742
743 BUG_ON(!ibmvfc_valid_event(pool, evt));
744 BUG_ON(atomic_inc_return(&evt->free) != 1);
745 list_add_tail(&evt->queue, &vhost->free);
746}
747
748/**
749 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
750 * @evt: ibmvfc event struct
751 *
752 * This function does not setup any error status, that must be done
753 * before this function gets called.
754 **/
755static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
756{
757 struct scsi_cmnd *cmnd = evt->cmnd;
758
759 if (cmnd) {
760 scsi_dma_unmap(cmnd);
761 cmnd->scsi_done(cmnd);
762 }
763
Brian Kingad8dcff2008-10-29 08:46:41 -0500764 if (evt->eh_comp)
765 complete(evt->eh_comp);
766
Brian King072b91f2008-07-01 13:14:30 -0500767 ibmvfc_free_event(evt);
768}
769
770/**
771 * ibmvfc_fail_request - Fail request with specified error code
772 * @evt: ibmvfc event struct
773 * @error_code: error code to fail request with
774 *
775 * Return value:
776 * none
777 **/
778static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
779{
780 if (evt->cmnd) {
781 evt->cmnd->result = (error_code << 16);
782 evt->done = ibmvfc_scsi_eh_done;
783 } else
784 evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
785
786 list_del(&evt->queue);
787 del_timer(&evt->timer);
788 ibmvfc_trc_end(evt);
789 evt->done(evt);
790}
791
792/**
793 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
794 * @vhost: ibmvfc host struct
795 * @error_code: error code to fail requests with
796 *
797 * Return value:
798 * none
799 **/
800static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
801{
802 struct ibmvfc_event *evt, *pos;
803
804 ibmvfc_dbg(vhost, "Purging all requests\n");
805 list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
806 ibmvfc_fail_request(evt, error_code);
807}
808
809/**
810 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
811 * @vhost: struct ibmvfc host to reset
812 **/
813static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
814{
815 int rc;
816
817 scsi_block_requests(vhost->host);
818 ibmvfc_purge_requests(vhost, DID_ERROR);
819 if ((rc = ibmvfc_reset_crq(vhost)) ||
820 (rc = ibmvfc_send_crq_init(vhost)) ||
821 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
822 dev_err(vhost->dev, "Error after reset rc=%d\n", rc);
823 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
824 } else
825 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
826}
827
828/**
829 * ibmvfc_reset_host - Reset the connection to the server
830 * @vhost: struct ibmvfc host to reset
831 **/
832static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
833{
834 unsigned long flags;
835
836 spin_lock_irqsave(vhost->host->host_lock, flags);
837 __ibmvfc_reset_host(vhost);
838 spin_unlock_irqrestore(vhost->host->host_lock, flags);
839}
840
841/**
842 * ibmvfc_retry_host_init - Retry host initialization if allowed
843 * @vhost: ibmvfc host struct
844 *
Brian King7d0e4622009-05-28 16:17:26 -0500845 * Returns: 1 if init will be retried / 0 if not
846 *
Brian King072b91f2008-07-01 13:14:30 -0500847 **/
Brian King7d0e4622009-05-28 16:17:26 -0500848static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
Brian King072b91f2008-07-01 13:14:30 -0500849{
Brian King7d0e4622009-05-28 16:17:26 -0500850 int retry = 0;
851
Brian King072b91f2008-07-01 13:14:30 -0500852 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
Brian King1c41fa822008-12-03 11:02:54 -0600853 vhost->delay_init = 1;
854 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -0500855 dev_err(vhost->dev,
856 "Host initialization retries exceeded. Taking adapter offline\n");
857 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
Brian King1c41fa822008-12-03 11:02:54 -0600858 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
Brian King072b91f2008-07-01 13:14:30 -0500859 __ibmvfc_reset_host(vhost);
Brian King7d0e4622009-05-28 16:17:26 -0500860 else {
Brian King072b91f2008-07-01 13:14:30 -0500861 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
Brian King7d0e4622009-05-28 16:17:26 -0500862 retry = 1;
863 }
Brian King072b91f2008-07-01 13:14:30 -0500864 }
865
866 wake_up(&vhost->work_wait_q);
Brian King7d0e4622009-05-28 16:17:26 -0500867 return retry;
Brian King072b91f2008-07-01 13:14:30 -0500868}
869
870/**
Brian Kingb3c10489c2008-07-22 08:31:41 -0500871 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
Brian King072b91f2008-07-01 13:14:30 -0500872 * @starget: scsi target struct
873 *
874 * Return value:
875 * ibmvfc_target struct / NULL if not found
876 **/
Brian Kingb3c10489c2008-07-22 08:31:41 -0500877static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
Brian King072b91f2008-07-01 13:14:30 -0500878{
879 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
880 struct ibmvfc_host *vhost = shost_priv(shost);
881 struct ibmvfc_target *tgt;
882
883 list_for_each_entry(tgt, &vhost->targets, queue)
Brian Kingb3c10489c2008-07-22 08:31:41 -0500884 if (tgt->target_id == starget->id) {
885 kref_get(&tgt->kref);
Brian King072b91f2008-07-01 13:14:30 -0500886 return tgt;
Brian Kingb3c10489c2008-07-22 08:31:41 -0500887 }
Brian King072b91f2008-07-01 13:14:30 -0500888 return NULL;
889}
890
891/**
Brian Kingb3c10489c2008-07-22 08:31:41 -0500892 * ibmvfc_get_target - Find the specified scsi_target
Brian King072b91f2008-07-01 13:14:30 -0500893 * @starget: scsi target struct
894 *
895 * Return value:
896 * ibmvfc_target struct / NULL if not found
897 **/
Brian Kingb3c10489c2008-07-22 08:31:41 -0500898static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
Brian King072b91f2008-07-01 13:14:30 -0500899{
900 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
901 struct ibmvfc_target *tgt;
902 unsigned long flags;
903
904 spin_lock_irqsave(shost->host_lock, flags);
Brian Kingb3c10489c2008-07-22 08:31:41 -0500905 tgt = __ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -0500906 spin_unlock_irqrestore(shost->host_lock, flags);
907 return tgt;
908}
909
910/**
911 * ibmvfc_get_host_speed - Get host port speed
912 * @shost: scsi host struct
913 *
914 * Return value:
915 * none
916 **/
917static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
918{
919 struct ibmvfc_host *vhost = shost_priv(shost);
920 unsigned long flags;
921
922 spin_lock_irqsave(shost->host_lock, flags);
923 if (vhost->state == IBMVFC_ACTIVE) {
924 switch (vhost->login_buf->resp.link_speed / 100) {
925 case 1:
926 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
927 break;
928 case 2:
929 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
930 break;
931 case 4:
932 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
933 break;
934 case 8:
935 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
936 break;
937 case 10:
938 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
939 break;
940 case 16:
941 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
942 break;
943 default:
Stephen Rothwell775a42e2009-01-06 14:59:00 +0000944 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
Brian King072b91f2008-07-01 13:14:30 -0500945 vhost->login_buf->resp.link_speed / 100);
946 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
947 break;
948 }
949 } else
950 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
951 spin_unlock_irqrestore(shost->host_lock, flags);
952}
953
954/**
955 * ibmvfc_get_host_port_state - Get host port state
956 * @shost: scsi host struct
957 *
958 * Return value:
959 * none
960 **/
961static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
962{
963 struct ibmvfc_host *vhost = shost_priv(shost);
964 unsigned long flags;
965
966 spin_lock_irqsave(shost->host_lock, flags);
967 switch (vhost->state) {
968 case IBMVFC_INITIALIZING:
969 case IBMVFC_ACTIVE:
970 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
971 break;
972 case IBMVFC_LINK_DOWN:
973 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
974 break;
975 case IBMVFC_LINK_DEAD:
976 case IBMVFC_HOST_OFFLINE:
977 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
978 break;
979 case IBMVFC_HALTED:
980 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
981 break;
Brian King0ae808e2008-07-22 08:31:39 -0500982 case IBMVFC_NO_CRQ:
983 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
984 break;
Brian King072b91f2008-07-01 13:14:30 -0500985 default:
986 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
987 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
988 break;
989 }
990 spin_unlock_irqrestore(shost->host_lock, flags);
991}
992
993/**
994 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
995 * @rport: rport struct
996 * @timeout: timeout value
997 *
998 * Return value:
999 * none
1000 **/
1001static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1002{
1003 if (timeout)
1004 rport->dev_loss_tmo = timeout;
1005 else
1006 rport->dev_loss_tmo = 1;
1007}
1008
1009/**
Brian Kingb3c10489c2008-07-22 08:31:41 -05001010 * ibmvfc_release_tgt - Free memory allocated for a target
1011 * @kref: kref struct
1012 *
1013 **/
1014static void ibmvfc_release_tgt(struct kref *kref)
1015{
1016 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1017 kfree(tgt);
1018}
1019
1020/**
Brian King072b91f2008-07-01 13:14:30 -05001021 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1022 * @starget: scsi target struct
1023 *
1024 * Return value:
1025 * none
1026 **/
1027static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1028{
Brian Kingb3c10489c2008-07-22 08:31:41 -05001029 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -05001030 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
Brian Kingb3c10489c2008-07-22 08:31:41 -05001031 if (tgt)
1032 kref_put(&tgt->kref, ibmvfc_release_tgt);
Brian King072b91f2008-07-01 13:14:30 -05001033}
1034
1035/**
1036 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1037 * @starget: scsi target struct
1038 *
1039 * Return value:
1040 * none
1041 **/
1042static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1043{
Brian Kingb3c10489c2008-07-22 08:31:41 -05001044 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -05001045 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
Brian Kingb3c10489c2008-07-22 08:31:41 -05001046 if (tgt)
1047 kref_put(&tgt->kref, ibmvfc_release_tgt);
Brian King072b91f2008-07-01 13:14:30 -05001048}
1049
1050/**
1051 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1052 * @starget: scsi target struct
1053 *
1054 * Return value:
1055 * none
1056 **/
1057static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1058{
Brian Kingb3c10489c2008-07-22 08:31:41 -05001059 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
Brian King072b91f2008-07-01 13:14:30 -05001060 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
Brian Kingb3c10489c2008-07-22 08:31:41 -05001061 if (tgt)
1062 kref_put(&tgt->kref, ibmvfc_release_tgt);
Brian King072b91f2008-07-01 13:14:30 -05001063}
1064
1065/**
1066 * ibmvfc_wait_while_resetting - Wait while the host resets
1067 * @vhost: ibmvfc host struct
1068 *
1069 * Return value:
1070 * 0 on success / other on failure
1071 **/
1072static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1073{
1074 long timeout = wait_event_timeout(vhost->init_wait_q,
Brian King3eddc562008-08-15 10:59:21 -05001075 ((vhost->state == IBMVFC_ACTIVE ||
1076 vhost->state == IBMVFC_HOST_OFFLINE ||
1077 vhost->state == IBMVFC_LINK_DEAD) &&
1078 vhost->action == IBMVFC_HOST_ACTION_NONE),
Brian King072b91f2008-07-01 13:14:30 -05001079 (init_timeout * HZ));
1080
1081 return timeout ? 0 : -EIO;
1082}
1083
1084/**
1085 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1086 * @shost: scsi host struct
1087 *
1088 * Return value:
1089 * 0 on success / other on failure
1090 **/
1091static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1092{
1093 struct ibmvfc_host *vhost = shost_priv(shost);
1094
1095 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1096 ibmvfc_reset_host(vhost);
1097 return ibmvfc_wait_while_resetting(vhost);
1098}
1099
1100/**
1101 * ibmvfc_gather_partition_info - Gather info about the LPAR
1102 *
1103 * Return value:
1104 * none
1105 **/
1106static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1107{
1108 struct device_node *rootdn;
1109 const char *name;
1110 const unsigned int *num;
1111
1112 rootdn = of_find_node_by_path("/");
1113 if (!rootdn)
1114 return;
1115
1116 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1117 if (name)
1118 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1119 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1120 if (num)
1121 vhost->partition_number = *num;
1122 of_node_put(rootdn);
1123}
1124
1125/**
1126 * ibmvfc_set_login_info - Setup info for NPIV login
1127 * @vhost: ibmvfc host struct
1128 *
1129 * Return value:
1130 * none
1131 **/
1132static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1133{
1134 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1135 struct device_node *of_node = vhost->dev->archdata.of_node;
1136 const char *location;
1137
1138 memset(login_info, 0, sizeof(*login_info));
1139
1140 login_info->ostype = IBMVFC_OS_LINUX;
1141 login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
1142 login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
1143 login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
1144 login_info->partition_num = vhost->partition_number;
1145 login_info->vfc_frame_version = 1;
1146 login_info->fcp_version = 3;
1147 if (vhost->client_migrated)
1148 login_info->flags = IBMVFC_CLIENT_MIGRATED;
1149
1150 login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1151 login_info->capabilities = IBMVFC_CAN_MIGRATE;
1152 login_info->async.va = vhost->async_crq.msg_token;
Brian King52d7e862008-07-22 08:31:46 -05001153 login_info->async.len = vhost->async_crq.size * sizeof(*vhost->async_crq.msgs);
Brian King072b91f2008-07-01 13:14:30 -05001154 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1155 strncpy(login_info->device_name,
Kay Sievers71610f52008-12-03 22:41:36 +01001156 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
Brian King072b91f2008-07-01 13:14:30 -05001157
1158 location = of_get_property(of_node, "ibm,loc-code", NULL);
Kay Sievers71610f52008-12-03 22:41:36 +01001159 location = location ? location : dev_name(vhost->dev);
Brian King072b91f2008-07-01 13:14:30 -05001160 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1161}
1162
1163/**
1164 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1165 * @vhost: ibmvfc host who owns the event pool
1166 *
1167 * Returns zero on success.
1168 **/
1169static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1170{
1171 int i;
1172 struct ibmvfc_event_pool *pool = &vhost->pool;
1173
1174 ENTER;
1175 pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1176 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1177 if (!pool->events)
1178 return -ENOMEM;
1179
1180 pool->iu_storage = dma_alloc_coherent(vhost->dev,
1181 pool->size * sizeof(*pool->iu_storage),
1182 &pool->iu_token, 0);
1183
1184 if (!pool->iu_storage) {
1185 kfree(pool->events);
1186 return -ENOMEM;
1187 }
1188
1189 for (i = 0; i < pool->size; ++i) {
1190 struct ibmvfc_event *evt = &pool->events[i];
1191 atomic_set(&evt->free, 1);
1192 evt->crq.valid = 0x80;
1193 evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
1194 evt->xfer_iu = pool->iu_storage + i;
1195 evt->vhost = vhost;
1196 evt->ext_list = NULL;
1197 list_add_tail(&evt->queue, &vhost->free);
1198 }
1199
1200 LEAVE;
1201 return 0;
1202}
1203
1204/**
1205 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1206 * @vhost: ibmvfc host who owns the event pool
1207 *
1208 **/
1209static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1210{
1211 int i;
1212 struct ibmvfc_event_pool *pool = &vhost->pool;
1213
1214 ENTER;
1215 for (i = 0; i < pool->size; ++i) {
1216 list_del(&pool->events[i].queue);
1217 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1218 if (pool->events[i].ext_list)
1219 dma_pool_free(vhost->sg_pool,
1220 pool->events[i].ext_list,
1221 pool->events[i].ext_list_token);
1222 }
1223
1224 kfree(pool->events);
1225 dma_free_coherent(vhost->dev,
1226 pool->size * sizeof(*pool->iu_storage),
1227 pool->iu_storage, pool->iu_token);
1228 LEAVE;
1229}
1230
1231/**
1232 * ibmvfc_get_event - Gets the next free event in pool
1233 * @vhost: ibmvfc host struct
1234 *
1235 * Returns a free event from the pool.
1236 **/
1237static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1238{
1239 struct ibmvfc_event *evt;
1240
1241 BUG_ON(list_empty(&vhost->free));
1242 evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1243 atomic_set(&evt->free, 0);
1244 list_del(&evt->queue);
1245 return evt;
1246}
1247
1248/**
1249 * ibmvfc_init_event - Initialize fields in an event struct that are always
1250 * required.
1251 * @evt: The event
1252 * @done: Routine to call when the event is responded to
1253 * @format: SRP or MAD format
1254 **/
1255static void ibmvfc_init_event(struct ibmvfc_event *evt,
1256 void (*done) (struct ibmvfc_event *), u8 format)
1257{
1258 evt->cmnd = NULL;
1259 evt->sync_iu = NULL;
1260 evt->crq.format = format;
1261 evt->done = done;
Brian Kingad8dcff2008-10-29 08:46:41 -05001262 evt->eh_comp = NULL;
Brian King072b91f2008-07-01 13:14:30 -05001263}
1264
1265/**
1266 * ibmvfc_map_sg_list - Initialize scatterlist
1267 * @scmd: scsi command struct
1268 * @nseg: number of scatterlist segments
1269 * @md: memory descriptor list to initialize
1270 **/
1271static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1272 struct srp_direct_buf *md)
1273{
1274 int i;
1275 struct scatterlist *sg;
1276
1277 scsi_for_each_sg(scmd, sg, nseg, i) {
1278 md[i].va = sg_dma_address(sg);
1279 md[i].len = sg_dma_len(sg);
1280 md[i].key = 0;
1281 }
1282}
1283
1284/**
1285 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1286 * @scmd: Scsi_Cmnd with the scatterlist
1287 * @evt: ibmvfc event struct
1288 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1289 * @dev: device for which to map dma memory
1290 *
1291 * Returns:
1292 * 0 on success / non-zero on failure
1293 **/
1294static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1295 struct ibmvfc_event *evt,
1296 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1297{
1298
1299 int sg_mapped;
1300 struct srp_direct_buf *data = &vfc_cmd->ioba;
1301 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1302
1303 sg_mapped = scsi_dma_map(scmd);
1304 if (!sg_mapped) {
1305 vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
1306 return 0;
1307 } else if (unlikely(sg_mapped < 0)) {
1308 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1309 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1310 return sg_mapped;
1311 }
1312
1313 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1314 vfc_cmd->flags |= IBMVFC_WRITE;
1315 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1316 } else {
1317 vfc_cmd->flags |= IBMVFC_READ;
1318 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1319 }
1320
1321 if (sg_mapped == 1) {
1322 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1323 return 0;
1324 }
1325
1326 vfc_cmd->flags |= IBMVFC_SCATTERLIST;
1327
1328 if (!evt->ext_list) {
1329 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1330 &evt->ext_list_token);
1331
1332 if (!evt->ext_list) {
Brian King64b840d2009-01-22 15:45:38 -06001333 scsi_dma_unmap(scmd);
1334 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1335 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
Brian King072b91f2008-07-01 13:14:30 -05001336 return -ENOMEM;
1337 }
1338 }
1339
1340 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1341
1342 data->va = evt->ext_list_token;
1343 data->len = sg_mapped * sizeof(struct srp_direct_buf);
1344 data->key = 0;
1345 return 0;
1346}
1347
1348/**
1349 * ibmvfc_timeout - Internal command timeout handler
1350 * @evt: struct ibmvfc_event that timed out
1351 *
1352 * Called when an internally generated command times out
1353 **/
1354static void ibmvfc_timeout(struct ibmvfc_event *evt)
1355{
1356 struct ibmvfc_host *vhost = evt->vhost;
1357 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1358 ibmvfc_reset_host(vhost);
1359}
1360
1361/**
1362 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1363 * @evt: event to be sent
1364 * @vhost: ibmvfc host struct
1365 * @timeout: timeout in seconds - 0 means do not time command
1366 *
1367 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1368 **/
1369static int ibmvfc_send_event(struct ibmvfc_event *evt,
1370 struct ibmvfc_host *vhost, unsigned long timeout)
1371{
1372 u64 *crq_as_u64 = (u64 *) &evt->crq;
1373 int rc;
1374
1375 /* Copy the IU into the transfer area */
1376 *evt->xfer_iu = evt->iu;
1377 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1378 evt->xfer_iu->cmd.tag = (u64)evt;
1379 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1380 evt->xfer_iu->mad_common.tag = (u64)evt;
1381 else
1382 BUG();
1383
1384 list_add_tail(&evt->queue, &vhost->sent);
1385 init_timer(&evt->timer);
1386
1387 if (timeout) {
1388 evt->timer.data = (unsigned long) evt;
1389 evt->timer.expires = jiffies + (timeout * HZ);
1390 evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1391 add_timer(&evt->timer);
1392 }
1393
Brian Kinga528ab72008-12-03 11:02:56 -06001394 mb();
1395
Brian King072b91f2008-07-01 13:14:30 -05001396 if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
1397 list_del(&evt->queue);
1398 del_timer(&evt->timer);
1399
1400 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1401 * Firmware will send a CRQ with a transport event (0xFF) to
1402 * tell this client what has happened to the transport. This
1403 * will be handled in ibmvfc_handle_crq()
1404 */
1405 if (rc == H_CLOSED) {
1406 if (printk_ratelimit())
1407 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1408 if (evt->cmnd)
1409 scsi_dma_unmap(evt->cmnd);
1410 ibmvfc_free_event(evt);
1411 return SCSI_MLQUEUE_HOST_BUSY;
1412 }
1413
1414 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1415 if (evt->cmnd) {
1416 evt->cmnd->result = DID_ERROR << 16;
1417 evt->done = ibmvfc_scsi_eh_done;
1418 } else
1419 evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
1420
1421 evt->done(evt);
1422 } else
1423 ibmvfc_trc_start(evt);
1424
1425 return 0;
1426}
1427
1428/**
1429 * ibmvfc_log_error - Log an error for the failed command if appropriate
1430 * @evt: ibmvfc event to log
1431 *
1432 **/
1433static void ibmvfc_log_error(struct ibmvfc_event *evt)
1434{
1435 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1436 struct ibmvfc_host *vhost = evt->vhost;
1437 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1438 struct scsi_cmnd *cmnd = evt->cmnd;
1439 const char *err = unknown_error;
1440 int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
1441 int logerr = 0;
1442 int rsp_code = 0;
1443
1444 if (index >= 0) {
1445 logerr = cmd_status[index].log;
1446 err = cmd_status[index].name;
1447 }
1448
Brian King0ae808e2008-07-22 08:31:39 -05001449 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
Brian King072b91f2008-07-01 13:14:30 -05001450 return;
1451
1452 if (rsp->flags & FCP_RSP_LEN_VALID)
1453 rsp_code = rsp->data.info.rsp_code;
1454
1455 scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1456 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1457 cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1458 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1459}
1460
1461/**
1462 * ibmvfc_scsi_done - Handle responses from commands
1463 * @evt: ibmvfc event to be handled
1464 *
1465 * Used as a callback when sending scsi cmds.
1466 **/
1467static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1468{
1469 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1470 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1471 struct scsi_cmnd *cmnd = evt->cmnd;
Brian King2bac4062008-08-15 10:59:26 -05001472 u32 rsp_len = 0;
1473 u32 sense_len = rsp->fcp_sense_len;
Brian King072b91f2008-07-01 13:14:30 -05001474
1475 if (cmnd) {
1476 if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
1477 scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
1478 else if (rsp->flags & FCP_RESID_UNDER)
1479 scsi_set_resid(cmnd, rsp->fcp_resid);
1480 else
1481 scsi_set_resid(cmnd, 0);
1482
1483 if (vfc_cmd->status) {
1484 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1485
1486 if (rsp->flags & FCP_RSP_LEN_VALID)
1487 rsp_len = rsp->fcp_rsp_len;
1488 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1489 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
Brian King2bac4062008-08-15 10:59:26 -05001490 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
Brian King072b91f2008-07-01 13:14:30 -05001491 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
Brian King646d3852008-11-14 13:33:53 -06001492 if ((vfc_cmd->status & IBMVFC_VIOS_FAILURE) && (vfc_cmd->error == IBMVFC_PLOGI_REQUIRED))
1493 ibmvfc_reinit_host(evt->vhost);
Brian King072b91f2008-07-01 13:14:30 -05001494
Brian King50119da2008-10-29 08:46:36 -05001495 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1496 cmnd->result = (DID_ERROR << 16);
1497
Brian King072b91f2008-07-01 13:14:30 -05001498 ibmvfc_log_error(evt);
1499 }
1500
1501 if (!cmnd->result &&
1502 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1503 cmnd->result = (DID_ERROR << 16);
1504
1505 scsi_dma_unmap(cmnd);
1506 cmnd->scsi_done(cmnd);
1507 }
1508
Brian Kingad8dcff2008-10-29 08:46:41 -05001509 if (evt->eh_comp)
1510 complete(evt->eh_comp);
1511
Brian King072b91f2008-07-01 13:14:30 -05001512 ibmvfc_free_event(evt);
1513}
1514
1515/**
1516 * ibmvfc_host_chkready - Check if the host can accept commands
1517 * @vhost: struct ibmvfc host
1518 *
1519 * Returns:
1520 * 1 if host can accept command / 0 if not
1521 **/
1522static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1523{
1524 int result = 0;
1525
1526 switch (vhost->state) {
1527 case IBMVFC_LINK_DEAD:
1528 case IBMVFC_HOST_OFFLINE:
1529 result = DID_NO_CONNECT << 16;
1530 break;
1531 case IBMVFC_NO_CRQ:
1532 case IBMVFC_INITIALIZING:
1533 case IBMVFC_HALTED:
1534 case IBMVFC_LINK_DOWN:
1535 result = DID_REQUEUE << 16;
1536 break;
1537 case IBMVFC_ACTIVE:
1538 result = 0;
1539 break;
1540 };
1541
1542 return result;
1543}
1544
1545/**
1546 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1547 * @cmnd: struct scsi_cmnd to be executed
1548 * @done: Callback function to be called when cmnd is completed
1549 *
1550 * Returns:
1551 * 0 on success / other on failure
1552 **/
1553static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
1554 void (*done) (struct scsi_cmnd *))
1555{
1556 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1557 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1558 struct ibmvfc_cmd *vfc_cmd;
1559 struct ibmvfc_event *evt;
1560 u8 tag[2];
1561 int rc;
1562
1563 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1564 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1565 cmnd->result = rc;
1566 done(cmnd);
1567 return 0;
1568 }
1569
1570 cmnd->result = (DID_OK << 16);
1571 evt = ibmvfc_get_event(vhost);
1572 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1573 evt->cmnd = cmnd;
1574 cmnd->scsi_done = done;
1575 vfc_cmd = &evt->iu.cmd;
1576 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1577 vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1578 vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
1579 vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
1580 vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
1581 vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
1582 vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
1583 vfc_cmd->tgt_scsi_id = rport->port_id;
Brian King072b91f2008-07-01 13:14:30 -05001584 vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
1585 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1586 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1587
1588 if (scsi_populate_tag_msg(cmnd, tag)) {
1589 vfc_cmd->task_tag = tag[1];
1590 switch (tag[0]) {
1591 case MSG_SIMPLE_TAG:
1592 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1593 break;
1594 case MSG_HEAD_TAG:
1595 vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
1596 break;
1597 case MSG_ORDERED_TAG:
1598 vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
1599 break;
1600 };
1601 }
1602
1603 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1604 return ibmvfc_send_event(evt, vhost, 0);
1605
1606 ibmvfc_free_event(evt);
1607 if (rc == -ENOMEM)
1608 return SCSI_MLQUEUE_HOST_BUSY;
1609
1610 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1611 scmd_printk(KERN_ERR, cmnd,
1612 "Failed to map DMA buffer for command. rc=%d\n", rc);
1613
1614 cmnd->result = DID_ERROR << 16;
1615 done(cmnd);
1616 return 0;
1617}
1618
1619/**
1620 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1621 * @evt: ibmvfc event struct
1622 *
1623 **/
1624static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1625{
1626 /* copy the response back */
1627 if (evt->sync_iu)
1628 *evt->sync_iu = *evt->xfer_iu;
1629
1630 complete(&evt->comp);
1631}
1632
1633/**
1634 * ibmvfc_reset_device - Reset the device with the specified reset type
1635 * @sdev: scsi device to reset
1636 * @type: reset type
1637 * @desc: reset type description for log messages
1638 *
1639 * Returns:
1640 * 0 on success / other on failure
1641 **/
1642static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1643{
1644 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1645 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1646 struct ibmvfc_cmd *tmf;
Brian King50ed9a02008-10-29 08:46:47 -05001647 struct ibmvfc_event *evt = NULL;
Brian King072b91f2008-07-01 13:14:30 -05001648 union ibmvfc_iu rsp_iu;
1649 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1650 int rsp_rc = -EBUSY;
1651 unsigned long flags;
1652 int rsp_code = 0;
1653
1654 spin_lock_irqsave(vhost->host->host_lock, flags);
1655 if (vhost->state == IBMVFC_ACTIVE) {
1656 evt = ibmvfc_get_event(vhost);
1657 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1658
1659 tmf = &evt->iu.cmd;
1660 memset(tmf, 0, sizeof(*tmf));
1661 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1662 tmf->resp.len = sizeof(tmf->rsp);
1663 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1664 tmf->payload_len = sizeof(tmf->iu);
1665 tmf->resp_len = sizeof(tmf->rsp);
1666 tmf->cancel_key = (unsigned long)sdev->hostdata;
1667 tmf->tgt_scsi_id = rport->port_id;
1668 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1669 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1670 tmf->iu.tmf_flags = type;
1671 evt->sync_iu = &rsp_iu;
1672
1673 init_completion(&evt->comp);
1674 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1675 }
1676 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1677
1678 if (rsp_rc != 0) {
1679 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
1680 desc, rsp_rc);
1681 return -EIO;
1682 }
1683
1684 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
1685 wait_for_completion(&evt->comp);
1686
1687 if (rsp_iu.cmd.status) {
1688 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1689 rsp_code = fc_rsp->data.info.rsp_code;
1690
1691 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
1692 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1693 desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1694 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1695 fc_rsp->scsi_status);
1696 rsp_rc = -EIO;
1697 } else
1698 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
1699
1700 spin_lock_irqsave(vhost->host->host_lock, flags);
1701 ibmvfc_free_event(evt);
1702 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1703 return rsp_rc;
1704}
1705
1706/**
1707 * ibmvfc_abort_task_set - Abort outstanding commands to the device
1708 * @sdev: scsi device to abort commands
1709 *
1710 * This sends an Abort Task Set to the VIOS for the specified device. This does
1711 * NOT send any cancel to the VIOS. That must be done separately.
1712 *
1713 * Returns:
1714 * 0 on success / other on failure
1715 **/
1716static int ibmvfc_abort_task_set(struct scsi_device *sdev)
1717{
1718 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1719 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1720 struct ibmvfc_cmd *tmf;
1721 struct ibmvfc_event *evt, *found_evt;
1722 union ibmvfc_iu rsp_iu;
1723 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1724 int rsp_rc = -EBUSY;
1725 unsigned long flags;
1726 int rsp_code = 0;
1727
1728 spin_lock_irqsave(vhost->host->host_lock, flags);
1729 found_evt = NULL;
1730 list_for_each_entry(evt, &vhost->sent, queue) {
1731 if (evt->cmnd && evt->cmnd->device == sdev) {
1732 found_evt = evt;
1733 break;
1734 }
1735 }
1736
1737 if (!found_evt) {
1738 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1739 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
1740 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1741 return 0;
1742 }
1743
1744 if (vhost->state == IBMVFC_ACTIVE) {
1745 evt = ibmvfc_get_event(vhost);
1746 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1747
1748 tmf = &evt->iu.cmd;
1749 memset(tmf, 0, sizeof(*tmf));
1750 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1751 tmf->resp.len = sizeof(tmf->rsp);
1752 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1753 tmf->payload_len = sizeof(tmf->iu);
1754 tmf->resp_len = sizeof(tmf->rsp);
1755 tmf->cancel_key = (unsigned long)sdev->hostdata;
1756 tmf->tgt_scsi_id = rport->port_id;
1757 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1758 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1759 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
1760 evt->sync_iu = &rsp_iu;
1761
1762 init_completion(&evt->comp);
1763 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1764 }
1765
1766 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1767
1768 if (rsp_rc != 0) {
1769 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
1770 return -EIO;
1771 }
1772
1773 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
1774 wait_for_completion(&evt->comp);
1775
1776 if (rsp_iu.cmd.status) {
1777 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1778 rsp_code = fc_rsp->data.info.rsp_code;
1779
1780 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
1781 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1782 ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1783 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1784 fc_rsp->scsi_status);
1785 rsp_rc = -EIO;
1786 } else
1787 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
1788
1789 spin_lock_irqsave(vhost->host->host_lock, flags);
1790 ibmvfc_free_event(evt);
1791 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1792 return rsp_rc;
1793}
1794
1795/**
1796 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
1797 * @sdev: scsi device to cancel commands
1798 * @type: type of error recovery being performed
1799 *
1800 * This sends a cancel to the VIOS for the specified device. This does
1801 * NOT send any abort to the actual device. That must be done separately.
1802 *
1803 * Returns:
1804 * 0 on success / other on failure
1805 **/
1806static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
1807{
1808 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian Kingad8dcff2008-10-29 08:46:41 -05001809 struct scsi_target *starget = scsi_target(sdev);
1810 struct fc_rport *rport = starget_to_rport(starget);
Brian King072b91f2008-07-01 13:14:30 -05001811 struct ibmvfc_tmf *tmf;
1812 struct ibmvfc_event *evt, *found_evt;
1813 union ibmvfc_iu rsp;
1814 int rsp_rc = -EBUSY;
1815 unsigned long flags;
1816 u16 status;
1817
1818 ENTER;
1819 spin_lock_irqsave(vhost->host->host_lock, flags);
1820 found_evt = NULL;
1821 list_for_each_entry(evt, &vhost->sent, queue) {
1822 if (evt->cmnd && evt->cmnd->device == sdev) {
1823 found_evt = evt;
1824 break;
1825 }
1826 }
1827
1828 if (!found_evt) {
1829 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1830 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
1831 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1832 return 0;
1833 }
1834
1835 if (vhost->state == IBMVFC_ACTIVE) {
1836 evt = ibmvfc_get_event(vhost);
1837 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1838
1839 tmf = &evt->iu.tmf;
1840 memset(tmf, 0, sizeof(*tmf));
1841 tmf->common.version = 1;
1842 tmf->common.opcode = IBMVFC_TMF_MAD;
1843 tmf->common.length = sizeof(*tmf);
1844 tmf->scsi_id = rport->port_id;
1845 int_to_scsilun(sdev->lun, &tmf->lun);
1846 tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
1847 tmf->cancel_key = (unsigned long)sdev->hostdata;
Brian Kingad8dcff2008-10-29 08:46:41 -05001848 tmf->my_cancel_key = (unsigned long)starget->hostdata;
Brian King072b91f2008-07-01 13:14:30 -05001849
1850 evt->sync_iu = &rsp;
1851 init_completion(&evt->comp);
1852 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1853 }
1854
1855 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1856
1857 if (rsp_rc != 0) {
1858 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
1859 return -EIO;
1860 }
1861
1862 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
1863
1864 wait_for_completion(&evt->comp);
1865 status = rsp.mad_common.status;
1866 spin_lock_irqsave(vhost->host->host_lock, flags);
1867 ibmvfc_free_event(evt);
1868 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1869
1870 if (status != IBMVFC_MAD_SUCCESS) {
1871 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
1872 return -EIO;
1873 }
1874
1875 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
1876 return 0;
1877}
1878
1879/**
Brian Kingad8dcff2008-10-29 08:46:41 -05001880 * ibmvfc_match_target - Match function for specified target
1881 * @evt: ibmvfc event struct
1882 * @device: device to match (starget)
1883 *
1884 * Returns:
1885 * 1 if event matches starget / 0 if event does not match starget
1886 **/
1887static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
1888{
1889 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
1890 return 1;
1891 return 0;
1892}
1893
1894/**
1895 * ibmvfc_match_lun - Match function for specified LUN
1896 * @evt: ibmvfc event struct
1897 * @device: device to match (sdev)
1898 *
1899 * Returns:
1900 * 1 if event matches sdev / 0 if event does not match sdev
1901 **/
1902static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
1903{
1904 if (evt->cmnd && evt->cmnd->device == device)
1905 return 1;
1906 return 0;
1907}
1908
1909/**
1910 * ibmvfc_wait_for_ops - Wait for ops to complete
1911 * @vhost: ibmvfc host struct
1912 * @device: device to match (starget or sdev)
1913 * @match: match function
1914 *
1915 * Returns:
1916 * SUCCESS / FAILED
1917 **/
1918static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
1919 int (*match) (struct ibmvfc_event *, void *))
1920{
1921 struct ibmvfc_event *evt;
1922 DECLARE_COMPLETION_ONSTACK(comp);
1923 int wait;
1924 unsigned long flags;
1925 signed long timeout = init_timeout * HZ;
1926
1927 ENTER;
1928 do {
1929 wait = 0;
1930 spin_lock_irqsave(vhost->host->host_lock, flags);
1931 list_for_each_entry(evt, &vhost->sent, queue) {
1932 if (match(evt, device)) {
1933 evt->eh_comp = &comp;
1934 wait++;
1935 }
1936 }
1937 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1938
1939 if (wait) {
1940 timeout = wait_for_completion_timeout(&comp, timeout);
1941
1942 if (!timeout) {
1943 wait = 0;
1944 spin_lock_irqsave(vhost->host->host_lock, flags);
1945 list_for_each_entry(evt, &vhost->sent, queue) {
1946 if (match(evt, device)) {
1947 evt->eh_comp = NULL;
1948 wait++;
1949 }
1950 }
1951 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1952 if (wait)
1953 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
1954 LEAVE;
1955 return wait ? FAILED : SUCCESS;
1956 }
1957 }
1958 } while (wait);
1959
1960 LEAVE;
1961 return SUCCESS;
1962}
1963
1964/**
Brian King072b91f2008-07-01 13:14:30 -05001965 * ibmvfc_eh_abort_handler - Abort a command
1966 * @cmd: scsi command to abort
1967 *
1968 * Returns:
1969 * SUCCESS / FAILED
1970 **/
1971static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
1972{
Brian Kingad8dcff2008-10-29 08:46:41 -05001973 struct scsi_device *sdev = cmd->device;
1974 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King072b91f2008-07-01 13:14:30 -05001975 int cancel_rc, abort_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05001976 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05001977
1978 ENTER;
1979 ibmvfc_wait_while_resetting(vhost);
Brian Kingad8dcff2008-10-29 08:46:41 -05001980 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
1981 abort_rc = ibmvfc_abort_task_set(sdev);
Brian King072b91f2008-07-01 13:14:30 -05001982
Brian Kingad8dcff2008-10-29 08:46:41 -05001983 if (!cancel_rc && !abort_rc)
1984 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05001985
1986 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05001987 return rc;
Brian King072b91f2008-07-01 13:14:30 -05001988}
1989
1990/**
1991 * ibmvfc_eh_device_reset_handler - Reset a single LUN
1992 * @cmd: scsi command struct
1993 *
1994 * Returns:
1995 * SUCCESS / FAILED
1996 **/
1997static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
1998{
Brian Kingad8dcff2008-10-29 08:46:41 -05001999 struct scsi_device *sdev = cmd->device;
2000 struct ibmvfc_host *vhost = shost_priv(sdev->host);
Brian King072b91f2008-07-01 13:14:30 -05002001 int cancel_rc, reset_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002002 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002003
2004 ENTER;
2005 ibmvfc_wait_while_resetting(vhost);
Brian Kingad8dcff2008-10-29 08:46:41 -05002006 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2007 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
Brian King072b91f2008-07-01 13:14:30 -05002008
Brian Kingad8dcff2008-10-29 08:46:41 -05002009 if (!cancel_rc && !reset_rc)
2010 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
Brian King072b91f2008-07-01 13:14:30 -05002011
2012 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002013 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002014}
2015
2016/**
2017 * ibmvfc_dev_cancel_all - Device iterated cancel all function
2018 * @sdev: scsi device struct
2019 * @data: return code
2020 *
2021 **/
2022static void ibmvfc_dev_cancel_all(struct scsi_device *sdev, void *data)
2023{
2024 unsigned long *rc = data;
2025 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2026}
2027
2028/**
2029 * ibmvfc_dev_abort_all - Device iterated abort task set function
2030 * @sdev: scsi device struct
2031 * @data: return code
2032 *
2033 **/
2034static void ibmvfc_dev_abort_all(struct scsi_device *sdev, void *data)
2035{
2036 unsigned long *rc = data;
2037 *rc |= ibmvfc_abort_task_set(sdev);
2038}
2039
2040/**
2041 * ibmvfc_eh_target_reset_handler - Reset the target
2042 * @cmd: scsi command struct
2043 *
2044 * Returns:
2045 * SUCCESS / FAILED
2046 **/
2047static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2048{
Brian Kingad8dcff2008-10-29 08:46:41 -05002049 struct scsi_device *sdev = cmd->device;
2050 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2051 struct scsi_target *starget = scsi_target(sdev);
Brian King072b91f2008-07-01 13:14:30 -05002052 int reset_rc;
Brian Kingad8dcff2008-10-29 08:46:41 -05002053 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002054 unsigned long cancel_rc = 0;
Brian King072b91f2008-07-01 13:14:30 -05002055
2056 ENTER;
2057 ibmvfc_wait_while_resetting(vhost);
2058 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
Brian Kingad8dcff2008-10-29 08:46:41 -05002059 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
Brian King072b91f2008-07-01 13:14:30 -05002060
Brian Kingad8dcff2008-10-29 08:46:41 -05002061 if (!cancel_rc && !reset_rc)
2062 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
Brian King072b91f2008-07-01 13:14:30 -05002063
2064 LEAVE;
Brian Kingad8dcff2008-10-29 08:46:41 -05002065 return rc;
Brian King072b91f2008-07-01 13:14:30 -05002066}
2067
2068/**
2069 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2070 * @cmd: struct scsi_cmnd having problems
2071 *
2072 **/
2073static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2074{
2075 int rc;
2076 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2077
2078 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2079 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2080 return rc ? FAILED : SUCCESS;
2081}
2082
2083/**
2084 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2085 * @rport: rport struct
2086 *
2087 * Return value:
2088 * none
2089 **/
2090static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2091{
2092 struct scsi_target *starget = to_scsi_target(&rport->dev);
2093 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2094 struct ibmvfc_host *vhost = shost_priv(shost);
Brian King072b91f2008-07-01 13:14:30 -05002095 unsigned long cancel_rc = 0;
2096 unsigned long abort_rc = 0;
Brian Kingad8dcff2008-10-29 08:46:41 -05002097 int rc = FAILED;
Brian King072b91f2008-07-01 13:14:30 -05002098
2099 ENTER;
2100 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2101 starget_for_each_device(starget, &abort_rc, ibmvfc_dev_abort_all);
2102
Brian Kingad8dcff2008-10-29 08:46:41 -05002103 if (!cancel_rc && !abort_rc)
2104 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2105
2106 if (rc == FAILED)
Brian King072b91f2008-07-01 13:14:30 -05002107 ibmvfc_issue_fc_host_lip(shost);
Brian King072b91f2008-07-01 13:14:30 -05002108 LEAVE;
2109}
2110
2111static const struct {
2112 enum ibmvfc_async_event ae;
2113 const char *desc;
2114} ae_desc [] = {
2115 { IBMVFC_AE_ELS_PLOGI, "PLOGI" },
2116 { IBMVFC_AE_ELS_LOGO, "LOGO" },
2117 { IBMVFC_AE_ELS_PRLO, "PRLO" },
2118 { IBMVFC_AE_SCN_NPORT, "N-Port SCN" },
2119 { IBMVFC_AE_SCN_GROUP, "Group SCN" },
2120 { IBMVFC_AE_SCN_DOMAIN, "Domain SCN" },
2121 { IBMVFC_AE_SCN_FABRIC, "Fabric SCN" },
2122 { IBMVFC_AE_LINK_UP, "Link Up" },
2123 { IBMVFC_AE_LINK_DOWN, "Link Down" },
2124 { IBMVFC_AE_LINK_DEAD, "Link Dead" },
2125 { IBMVFC_AE_HALT, "Halt" },
2126 { IBMVFC_AE_RESUME, "Resume" },
2127 { IBMVFC_AE_ADAPTER_FAILED, "Adapter Failed" },
2128};
2129
2130static const char *unknown_ae = "Unknown async";
2131
2132/**
2133 * ibmvfc_get_ae_desc - Get text description for async event
2134 * @ae: async event
2135 *
2136 **/
2137static const char *ibmvfc_get_ae_desc(u64 ae)
2138{
2139 int i;
2140
2141 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2142 if (ae_desc[i].ae == ae)
2143 return ae_desc[i].desc;
2144
2145 return unknown_ae;
2146}
2147
2148/**
2149 * ibmvfc_handle_async - Handle an async event from the adapter
2150 * @crq: crq to process
2151 * @vhost: ibmvfc host struct
2152 *
2153 **/
2154static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2155 struct ibmvfc_host *vhost)
2156{
2157 const char *desc = ibmvfc_get_ae_desc(crq->event);
2158
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002159 ibmvfc_log(vhost, 3, "%s event received. scsi_id: %llx, wwpn: %llx,"
2160 " node_name: %llx\n", desc, crq->scsi_id, crq->wwpn, crq->node_name);
Brian King072b91f2008-07-01 13:14:30 -05002161
2162 switch (crq->event) {
2163 case IBMVFC_AE_LINK_UP:
2164 case IBMVFC_AE_RESUME:
2165 vhost->events_to_log |= IBMVFC_AE_LINKUP;
Brian Kingd2131b32008-12-18 09:26:51 -06002166 vhost->delay_init = 1;
2167 __ibmvfc_reset_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002168 break;
2169 case IBMVFC_AE_SCN_FABRIC:
Brian Kingd2131b32008-12-18 09:26:51 -06002170 case IBMVFC_AE_SCN_DOMAIN:
Brian King072b91f2008-07-01 13:14:30 -05002171 vhost->events_to_log |= IBMVFC_AE_RSCN;
Brian Kingd2131b32008-12-18 09:26:51 -06002172 vhost->delay_init = 1;
2173 __ibmvfc_reset_host(vhost);
Brian King072b91f2008-07-01 13:14:30 -05002174 break;
2175 case IBMVFC_AE_SCN_NPORT:
2176 case IBMVFC_AE_SCN_GROUP:
Brian King072b91f2008-07-01 13:14:30 -05002177 vhost->events_to_log |= IBMVFC_AE_RSCN;
2178 case IBMVFC_AE_ELS_LOGO:
2179 case IBMVFC_AE_ELS_PRLO:
2180 case IBMVFC_AE_ELS_PLOGI:
2181 ibmvfc_reinit_host(vhost);
2182 break;
2183 case IBMVFC_AE_LINK_DOWN:
2184 case IBMVFC_AE_ADAPTER_FAILED:
2185 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2186 break;
2187 case IBMVFC_AE_LINK_DEAD:
2188 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2189 break;
2190 case IBMVFC_AE_HALT:
2191 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2192 break;
2193 default:
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002194 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
Brian King072b91f2008-07-01 13:14:30 -05002195 break;
2196 };
2197}
2198
2199/**
2200 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2201 * @crq: Command/Response queue
2202 * @vhost: ibmvfc host struct
2203 *
2204 **/
2205static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2206{
2207 long rc;
2208 struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2209
2210 switch (crq->valid) {
2211 case IBMVFC_CRQ_INIT_RSP:
2212 switch (crq->format) {
2213 case IBMVFC_CRQ_INIT:
2214 dev_info(vhost->dev, "Partner initialized\n");
2215 /* Send back a response */
2216 rc = ibmvfc_send_crq_init_complete(vhost);
2217 if (rc == 0)
Brian Kingcf6f10d2008-08-15 10:59:23 -05002218 ibmvfc_init_host(vhost, 0);
Brian King072b91f2008-07-01 13:14:30 -05002219 else
2220 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2221 break;
2222 case IBMVFC_CRQ_INIT_COMPLETE:
2223 dev_info(vhost->dev, "Partner initialization complete\n");
Brian Kingcf6f10d2008-08-15 10:59:23 -05002224 ibmvfc_init_host(vhost, 0);
Brian King072b91f2008-07-01 13:14:30 -05002225 break;
2226 default:
2227 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2228 }
2229 return;
2230 case IBMVFC_CRQ_XPORT_EVENT:
2231 vhost->state = IBMVFC_NO_CRQ;
2232 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2233 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2234 /* We need to re-setup the interpartition connection */
2235 dev_info(vhost->dev, "Re-enabling adapter\n");
2236 vhost->client_migrated = 1;
2237 ibmvfc_purge_requests(vhost, DID_REQUEUE);
2238 if ((rc = ibmvfc_reenable_crq_queue(vhost)) ||
2239 (rc = ibmvfc_send_crq_init(vhost))) {
2240 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2241 dev_err(vhost->dev, "Error after enable (rc=%ld)\n", rc);
2242 } else
2243 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2244 } else {
2245 dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2246
2247 ibmvfc_purge_requests(vhost, DID_ERROR);
2248 if ((rc = ibmvfc_reset_crq(vhost)) ||
2249 (rc = ibmvfc_send_crq_init(vhost))) {
2250 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2251 dev_err(vhost->dev, "Error after reset (rc=%ld)\n", rc);
2252 } else
2253 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2254 }
2255 return;
2256 case IBMVFC_CRQ_CMD_RSP:
2257 break;
2258 default:
2259 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2260 return;
2261 }
2262
2263 if (crq->format == IBMVFC_ASYNC_EVENT)
2264 return;
2265
2266 /* The only kind of payload CRQs we should get are responses to
2267 * things we send. Make sure this response is to something we
2268 * actually sent
2269 */
2270 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002271 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
Brian King072b91f2008-07-01 13:14:30 -05002272 crq->ioba);
2273 return;
2274 }
2275
2276 if (unlikely(atomic_read(&evt->free))) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00002277 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
Brian King072b91f2008-07-01 13:14:30 -05002278 crq->ioba);
2279 return;
2280 }
2281
2282 del_timer(&evt->timer);
2283 list_del(&evt->queue);
2284 ibmvfc_trc_end(evt);
2285 evt->done(evt);
2286}
2287
2288/**
2289 * ibmvfc_scan_finished - Check if the device scan is done.
2290 * @shost: scsi host struct
2291 * @time: current elapsed time
2292 *
2293 * Returns:
2294 * 0 if scan is not done / 1 if scan is done
2295 **/
2296static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2297{
2298 unsigned long flags;
2299 struct ibmvfc_host *vhost = shost_priv(shost);
2300 int done = 0;
2301
2302 spin_lock_irqsave(shost->host_lock, flags);
2303 if (time >= (init_timeout * HZ)) {
2304 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2305 "continuing initialization\n", init_timeout);
2306 done = 1;
2307 }
2308
2309 if (vhost->state != IBMVFC_NO_CRQ && vhost->action == IBMVFC_HOST_ACTION_NONE)
2310 done = 1;
2311 spin_unlock_irqrestore(shost->host_lock, flags);
2312 return done;
2313}
2314
2315/**
2316 * ibmvfc_slave_alloc - Setup the device's task set value
2317 * @sdev: struct scsi_device device to configure
2318 *
2319 * Set the device's task set value so that error handling works as
2320 * expected.
2321 *
2322 * Returns:
2323 * 0 on success / -ENXIO if device does not exist
2324 **/
2325static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2326{
2327 struct Scsi_Host *shost = sdev->host;
2328 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2329 struct ibmvfc_host *vhost = shost_priv(shost);
2330 unsigned long flags = 0;
2331
2332 if (!rport || fc_remote_port_chkready(rport))
2333 return -ENXIO;
2334
2335 spin_lock_irqsave(shost->host_lock, flags);
2336 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2337 spin_unlock_irqrestore(shost->host_lock, flags);
2338 return 0;
2339}
2340
2341/**
Brian Kingad8dcff2008-10-29 08:46:41 -05002342 * ibmvfc_target_alloc - Setup the target's task set value
2343 * @starget: struct scsi_target
2344 *
2345 * Set the target's task set value so that error handling works as
2346 * expected.
2347 *
2348 * Returns:
2349 * 0 on success / -ENXIO if device does not exist
2350 **/
2351static int ibmvfc_target_alloc(struct scsi_target *starget)
2352{
2353 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2354 struct ibmvfc_host *vhost = shost_priv(shost);
2355 unsigned long flags = 0;
2356
2357 spin_lock_irqsave(shost->host_lock, flags);
2358 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2359 spin_unlock_irqrestore(shost->host_lock, flags);
2360 return 0;
2361}
2362
2363/**
Brian King072b91f2008-07-01 13:14:30 -05002364 * ibmvfc_slave_configure - Configure the device
2365 * @sdev: struct scsi_device device to configure
2366 *
2367 * Enable allow_restart for a device if it is a disk. Adjust the
2368 * queue_depth here also.
2369 *
2370 * Returns:
2371 * 0
2372 **/
2373static int ibmvfc_slave_configure(struct scsi_device *sdev)
2374{
2375 struct Scsi_Host *shost = sdev->host;
2376 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
2377 unsigned long flags = 0;
2378
2379 spin_lock_irqsave(shost->host_lock, flags);
2380 if (sdev->type == TYPE_DISK)
2381 sdev->allow_restart = 1;
2382
2383 if (sdev->tagged_supported) {
2384 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2385 scsi_activate_tcq(sdev, sdev->queue_depth);
2386 } else
2387 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2388
2389 rport->dev_loss_tmo = dev_loss_tmo;
2390 spin_unlock_irqrestore(shost->host_lock, flags);
2391 return 0;
2392}
2393
2394/**
2395 * ibmvfc_change_queue_depth - Change the device's queue depth
2396 * @sdev: scsi device struct
2397 * @qdepth: depth to set
2398 *
2399 * Return value:
2400 * actual depth set
2401 **/
2402static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2403{
2404 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2405 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2406
2407 scsi_adjust_queue_depth(sdev, 0, qdepth);
2408 return sdev->queue_depth;
2409}
2410
2411/**
2412 * ibmvfc_change_queue_type - Change the device's queue type
2413 * @sdev: scsi device struct
2414 * @tag_type: type of tags to use
2415 *
2416 * Return value:
2417 * actual queue type set
2418 **/
2419static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2420{
2421 if (sdev->tagged_supported) {
2422 scsi_set_tag_type(sdev, tag_type);
2423
2424 if (tag_type)
2425 scsi_activate_tcq(sdev, sdev->queue_depth);
2426 else
2427 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2428 } else
2429 tag_type = 0;
2430
2431 return tag_type;
2432}
2433
2434static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2435 struct device_attribute *attr, char *buf)
2436{
2437 struct Scsi_Host *shost = class_to_shost(dev);
2438 struct ibmvfc_host *vhost = shost_priv(shost);
2439
2440 return snprintf(buf, PAGE_SIZE, "%s\n",
2441 vhost->login_buf->resp.partition_name);
2442}
2443
Brian King072b91f2008-07-01 13:14:30 -05002444static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2445 struct device_attribute *attr, char *buf)
2446{
2447 struct Scsi_Host *shost = class_to_shost(dev);
2448 struct ibmvfc_host *vhost = shost_priv(shost);
2449
2450 return snprintf(buf, PAGE_SIZE, "%s\n",
2451 vhost->login_buf->resp.device_name);
2452}
2453
Brian King072b91f2008-07-01 13:14:30 -05002454static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2455 struct device_attribute *attr, char *buf)
2456{
2457 struct Scsi_Host *shost = class_to_shost(dev);
2458 struct ibmvfc_host *vhost = shost_priv(shost);
2459
2460 return snprintf(buf, PAGE_SIZE, "%s\n",
2461 vhost->login_buf->resp.port_loc_code);
2462}
2463
Brian King072b91f2008-07-01 13:14:30 -05002464static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2465 struct device_attribute *attr, char *buf)
2466{
2467 struct Scsi_Host *shost = class_to_shost(dev);
2468 struct ibmvfc_host *vhost = shost_priv(shost);
2469
2470 return snprintf(buf, PAGE_SIZE, "%s\n",
2471 vhost->login_buf->resp.drc_name);
2472}
2473
Brian King072b91f2008-07-01 13:14:30 -05002474static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2475 struct device_attribute *attr, char *buf)
2476{
2477 struct Scsi_Host *shost = class_to_shost(dev);
2478 struct ibmvfc_host *vhost = shost_priv(shost);
2479 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2480}
2481
Brian King072b91f2008-07-01 13:14:30 -05002482/**
2483 * ibmvfc_show_log_level - Show the adapter's error logging level
2484 * @dev: class device struct
2485 * @buf: buffer
2486 *
2487 * Return value:
2488 * number of bytes printed to buffer
2489 **/
2490static ssize_t ibmvfc_show_log_level(struct device *dev,
2491 struct device_attribute *attr, char *buf)
2492{
2493 struct Scsi_Host *shost = class_to_shost(dev);
2494 struct ibmvfc_host *vhost = shost_priv(shost);
2495 unsigned long flags = 0;
2496 int len;
2497
2498 spin_lock_irqsave(shost->host_lock, flags);
2499 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2500 spin_unlock_irqrestore(shost->host_lock, flags);
2501 return len;
2502}
2503
2504/**
2505 * ibmvfc_store_log_level - Change the adapter's error logging level
2506 * @dev: class device struct
2507 * @buf: buffer
2508 *
2509 * Return value:
2510 * number of bytes printed to buffer
2511 **/
2512static ssize_t ibmvfc_store_log_level(struct device *dev,
2513 struct device_attribute *attr,
2514 const char *buf, size_t count)
2515{
2516 struct Scsi_Host *shost = class_to_shost(dev);
2517 struct ibmvfc_host *vhost = shost_priv(shost);
2518 unsigned long flags = 0;
2519
2520 spin_lock_irqsave(shost->host_lock, flags);
2521 vhost->log_level = simple_strtoul(buf, NULL, 10);
2522 spin_unlock_irqrestore(shost->host_lock, flags);
2523 return strlen(buf);
2524}
2525
Brian King85e23992009-05-28 16:17:25 -05002526static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
2527static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
2528static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
2529static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
2530static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
2531static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
2532 ibmvfc_show_log_level, ibmvfc_store_log_level);
Brian King072b91f2008-07-01 13:14:30 -05002533
2534#ifdef CONFIG_SCSI_IBMVFC_TRACE
2535/**
2536 * ibmvfc_read_trace - Dump the adapter trace
2537 * @kobj: kobject struct
2538 * @bin_attr: bin_attribute struct
2539 * @buf: buffer
2540 * @off: offset
2541 * @count: buffer size
2542 *
2543 * Return value:
2544 * number of bytes printed to buffer
2545 **/
2546static ssize_t ibmvfc_read_trace(struct kobject *kobj,
2547 struct bin_attribute *bin_attr,
2548 char *buf, loff_t off, size_t count)
2549{
2550 struct device *dev = container_of(kobj, struct device, kobj);
2551 struct Scsi_Host *shost = class_to_shost(dev);
2552 struct ibmvfc_host *vhost = shost_priv(shost);
2553 unsigned long flags = 0;
2554 int size = IBMVFC_TRACE_SIZE;
2555 char *src = (char *)vhost->trace;
2556
2557 if (off > size)
2558 return 0;
2559 if (off + count > size) {
2560 size -= off;
2561 count = size;
2562 }
2563
2564 spin_lock_irqsave(shost->host_lock, flags);
2565 memcpy(buf, &src[off], count);
2566 spin_unlock_irqrestore(shost->host_lock, flags);
2567 return count;
2568}
2569
2570static struct bin_attribute ibmvfc_trace_attr = {
2571 .attr = {
2572 .name = "trace",
2573 .mode = S_IRUGO,
2574 },
2575 .size = 0,
2576 .read = ibmvfc_read_trace,
2577};
2578#endif
2579
2580static struct device_attribute *ibmvfc_attrs[] = {
Brian King85e23992009-05-28 16:17:25 -05002581 &dev_attr_partition_name,
2582 &dev_attr_device_name,
2583 &dev_attr_port_loc_code,
2584 &dev_attr_drc_name,
2585 &dev_attr_npiv_version,
2586 &dev_attr_log_level,
Brian King072b91f2008-07-01 13:14:30 -05002587 NULL
2588};
2589
2590static struct scsi_host_template driver_template = {
2591 .module = THIS_MODULE,
2592 .name = "IBM POWER Virtual FC Adapter",
2593 .proc_name = IBMVFC_NAME,
2594 .queuecommand = ibmvfc_queuecommand,
2595 .eh_abort_handler = ibmvfc_eh_abort_handler,
2596 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
2597 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
2598 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
2599 .slave_alloc = ibmvfc_slave_alloc,
2600 .slave_configure = ibmvfc_slave_configure,
Brian Kingad8dcff2008-10-29 08:46:41 -05002601 .target_alloc = ibmvfc_target_alloc,
Brian King072b91f2008-07-01 13:14:30 -05002602 .scan_finished = ibmvfc_scan_finished,
2603 .change_queue_depth = ibmvfc_change_queue_depth,
2604 .change_queue_type = ibmvfc_change_queue_type,
2605 .cmd_per_lun = 16,
2606 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
2607 .this_id = -1,
2608 .sg_tablesize = SG_ALL,
2609 .max_sectors = IBMVFC_MAX_SECTORS,
2610 .use_clustering = ENABLE_CLUSTERING,
2611 .shost_attrs = ibmvfc_attrs,
2612};
2613
2614/**
2615 * ibmvfc_next_async_crq - Returns the next entry in async queue
2616 * @vhost: ibmvfc host struct
2617 *
2618 * Returns:
2619 * Pointer to next entry in queue / NULL if empty
2620 **/
2621static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
2622{
2623 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
2624 struct ibmvfc_async_crq *crq;
2625
2626 crq = &async_crq->msgs[async_crq->cur];
2627 if (crq->valid & 0x80) {
2628 if (++async_crq->cur == async_crq->size)
2629 async_crq->cur = 0;
2630 } else
2631 crq = NULL;
2632
2633 return crq;
2634}
2635
2636/**
2637 * ibmvfc_next_crq - Returns the next entry in message queue
2638 * @vhost: ibmvfc host struct
2639 *
2640 * Returns:
2641 * Pointer to next entry in queue / NULL if empty
2642 **/
2643static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
2644{
2645 struct ibmvfc_crq_queue *queue = &vhost->crq;
2646 struct ibmvfc_crq *crq;
2647
2648 crq = &queue->msgs[queue->cur];
2649 if (crq->valid & 0x80) {
2650 if (++queue->cur == queue->size)
2651 queue->cur = 0;
2652 } else
2653 crq = NULL;
2654
2655 return crq;
2656}
2657
2658/**
2659 * ibmvfc_interrupt - Interrupt handler
2660 * @irq: number of irq to handle, not used
2661 * @dev_instance: ibmvfc_host that received interrupt
2662 *
2663 * Returns:
2664 * IRQ_HANDLED
2665 **/
2666static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
2667{
2668 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
Brian King039a0892009-03-20 15:44:35 -05002669 unsigned long flags;
2670
2671 spin_lock_irqsave(vhost->host->host_lock, flags);
2672 vio_disable_interrupts(to_vio_dev(vhost->dev));
2673 tasklet_schedule(&vhost->tasklet);
2674 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2675 return IRQ_HANDLED;
2676}
2677
2678/**
2679 * ibmvfc_tasklet - Interrupt handler tasklet
2680 * @data: ibmvfc host struct
2681 *
2682 * Returns:
2683 * Nothing
2684 **/
2685static void ibmvfc_tasklet(void *data)
2686{
2687 struct ibmvfc_host *vhost = data;
Brian King072b91f2008-07-01 13:14:30 -05002688 struct vio_dev *vdev = to_vio_dev(vhost->dev);
2689 struct ibmvfc_crq *crq;
2690 struct ibmvfc_async_crq *async;
2691 unsigned long flags;
2692 int done = 0;
2693
2694 spin_lock_irqsave(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05002695 while (!done) {
2696 /* Pull all the valid messages off the CRQ */
2697 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2698 ibmvfc_handle_crq(crq, vhost);
2699 crq->valid = 0;
2700 }
2701
2702 /* Pull all the valid messages off the async CRQ */
2703 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2704 ibmvfc_handle_async(async, vhost);
2705 async->valid = 0;
2706 }
2707
2708 vio_enable_interrupts(vdev);
2709 if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2710 vio_disable_interrupts(vdev);
2711 ibmvfc_handle_crq(crq, vhost);
2712 crq->valid = 0;
2713 } else if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2714 vio_disable_interrupts(vdev);
2715 ibmvfc_handle_async(async, vhost);
Brian King4081b772008-11-14 13:33:50 -06002716 async->valid = 0;
Brian King072b91f2008-07-01 13:14:30 -05002717 } else
2718 done = 1;
2719 }
2720
2721 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian King072b91f2008-07-01 13:14:30 -05002722}
2723
2724/**
2725 * ibmvfc_init_tgt - Set the next init job step for the target
2726 * @tgt: ibmvfc target struct
2727 * @job_step: job step to perform
2728 *
2729 **/
2730static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
2731 void (*job_step) (struct ibmvfc_target *))
2732{
2733 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
2734 tgt->job_step = job_step;
2735 wake_up(&tgt->vhost->work_wait_q);
2736}
2737
2738/**
2739 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
2740 * @tgt: ibmvfc target struct
2741 * @job_step: initialization job step
2742 *
Brian King7d0e4622009-05-28 16:17:26 -05002743 * Returns: 1 if step will be retried / 0 if not
2744 *
Brian King072b91f2008-07-01 13:14:30 -05002745 **/
Brian King7d0e4622009-05-28 16:17:26 -05002746static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
Brian King072b91f2008-07-01 13:14:30 -05002747 void (*job_step) (struct ibmvfc_target *))
2748{
Brian King1c41fa822008-12-03 11:02:54 -06002749 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
Brian King072b91f2008-07-01 13:14:30 -05002750 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2751 wake_up(&tgt->vhost->work_wait_q);
Brian King7d0e4622009-05-28 16:17:26 -05002752 return 0;
Brian King072b91f2008-07-01 13:14:30 -05002753 } else
2754 ibmvfc_init_tgt(tgt, job_step);
Brian King7d0e4622009-05-28 16:17:26 -05002755 return 1;
Brian King072b91f2008-07-01 13:14:30 -05002756}
2757
Brian Kinga3b7aea2009-02-02 18:39:40 -06002758/* Defined in FC-LS */
2759static const struct {
2760 int code;
2761 int retry;
2762 int logged_in;
2763} prli_rsp [] = {
2764 { 0, 1, 0 },
2765 { 1, 0, 1 },
2766 { 2, 1, 0 },
2767 { 3, 1, 0 },
2768 { 4, 0, 0 },
2769 { 5, 0, 0 },
2770 { 6, 0, 1 },
2771 { 7, 0, 0 },
2772 { 8, 1, 0 },
2773};
2774
2775/**
2776 * ibmvfc_get_prli_rsp - Find PRLI response index
2777 * @flags: PRLI response flags
2778 *
2779 **/
2780static int ibmvfc_get_prli_rsp(u16 flags)
2781{
2782 int i;
2783 int code = (flags & 0x0f00) >> 8;
2784
2785 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
2786 if (prli_rsp[i].code == code)
2787 return i;
2788
2789 return 0;
2790}
2791
Brian King072b91f2008-07-01 13:14:30 -05002792/**
Brian King072b91f2008-07-01 13:14:30 -05002793 * ibmvfc_tgt_prli_done - Completion handler for Process Login
2794 * @evt: ibmvfc event struct
2795 *
2796 **/
2797static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
2798{
2799 struct ibmvfc_target *tgt = evt->tgt;
2800 struct ibmvfc_host *vhost = evt->vhost;
2801 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
Brian Kinga3b7aea2009-02-02 18:39:40 -06002802 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
Brian King072b91f2008-07-01 13:14:30 -05002803 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05002804 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05002805
2806 vhost->discovery_threads--;
2807 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2808 switch (status) {
2809 case IBMVFC_MAD_SUCCESS:
Brian Kinga3b7aea2009-02-02 18:39:40 -06002810 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
2811 parms->type, parms->flags, parms->service_parms);
2812
2813 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
2814 index = ibmvfc_get_prli_rsp(parms->flags);
2815 if (prli_rsp[index].logged_in) {
2816 if (parms->flags & IBMVFC_PRLI_EST_IMG_PAIR) {
2817 tgt->need_login = 0;
2818 tgt->ids.roles = 0;
2819 if (parms->service_parms & IBMVFC_PRLI_TARGET_FUNC)
2820 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
2821 if (parms->service_parms & IBMVFC_PRLI_INITIATOR_FUNC)
2822 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
2823 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_ADD_RPORT);
2824 } else
2825 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2826 } else if (prli_rsp[index].retry)
2827 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2828 else
2829 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2830 } else
2831 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King072b91f2008-07-01 13:14:30 -05002832 break;
2833 case IBMVFC_MAD_DRIVER_FAILED:
2834 break;
2835 case IBMVFC_MAD_CRQ_ERROR:
2836 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2837 break;
2838 case IBMVFC_MAD_FAILED:
2839 default:
Brian King072b91f2008-07-01 13:14:30 -05002840 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05002841 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
Brian King10e79492008-10-29 08:46:45 -05002842 else
2843 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05002844
2845 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
2846 ibmvfc_get_cmd_error(rsp->status, rsp->error),
2847 rsp->status, rsp->error, status);
Brian King072b91f2008-07-01 13:14:30 -05002848 break;
2849 };
2850
2851 kref_put(&tgt->kref, ibmvfc_release_tgt);
2852 ibmvfc_free_event(evt);
2853 wake_up(&vhost->work_wait_q);
2854}
2855
2856/**
2857 * ibmvfc_tgt_send_prli - Send a process login
2858 * @tgt: ibmvfc target struct
2859 *
2860 **/
2861static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
2862{
2863 struct ibmvfc_process_login *prli;
2864 struct ibmvfc_host *vhost = tgt->vhost;
2865 struct ibmvfc_event *evt;
2866
2867 if (vhost->discovery_threads >= disc_threads)
2868 return;
2869
2870 kref_get(&tgt->kref);
2871 evt = ibmvfc_get_event(vhost);
2872 vhost->discovery_threads++;
2873 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
2874 evt->tgt = tgt;
2875 prli = &evt->iu.prli;
2876 memset(prli, 0, sizeof(*prli));
2877 prli->common.version = 1;
2878 prli->common.opcode = IBMVFC_PROCESS_LOGIN;
2879 prli->common.length = sizeof(*prli);
2880 prli->scsi_id = tgt->scsi_id;
2881
2882 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
2883 prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
2884 prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
2885
2886 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2887 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2888 vhost->discovery_threads--;
2889 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2890 kref_put(&tgt->kref, ibmvfc_release_tgt);
2891 } else
2892 tgt_dbg(tgt, "Sent process login\n");
2893}
2894
2895/**
2896 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
2897 * @evt: ibmvfc event struct
2898 *
2899 **/
2900static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
2901{
2902 struct ibmvfc_target *tgt = evt->tgt;
2903 struct ibmvfc_host *vhost = evt->vhost;
2904 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
2905 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05002906 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05002907
2908 vhost->discovery_threads--;
2909 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2910 switch (status) {
2911 case IBMVFC_MAD_SUCCESS:
2912 tgt_dbg(tgt, "Port Login succeeded\n");
2913 if (tgt->ids.port_name &&
2914 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
2915 vhost->reinit = 1;
2916 tgt_dbg(tgt, "Port re-init required\n");
2917 break;
2918 }
2919 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
2920 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
2921 tgt->ids.port_id = tgt->scsi_id;
Brian King072b91f2008-07-01 13:14:30 -05002922 memcpy(&tgt->service_parms, &rsp->service_parms,
2923 sizeof(tgt->service_parms));
2924 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
2925 sizeof(tgt->service_parms_change));
2926 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
2927 break;
2928 case IBMVFC_MAD_DRIVER_FAILED:
2929 break;
2930 case IBMVFC_MAD_CRQ_ERROR:
2931 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2932 break;
2933 case IBMVFC_MAD_FAILED:
2934 default:
Brian King7d0e4622009-05-28 16:17:26 -05002935 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2936 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2937 else
2938 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2939
2940 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 -05002941 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
2942 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
2943 ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05002944 break;
2945 };
2946
2947 kref_put(&tgt->kref, ibmvfc_release_tgt);
2948 ibmvfc_free_event(evt);
2949 wake_up(&vhost->work_wait_q);
2950}
2951
2952/**
2953 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
2954 * @tgt: ibmvfc target struct
2955 *
2956 **/
2957static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
2958{
2959 struct ibmvfc_port_login *plogi;
2960 struct ibmvfc_host *vhost = tgt->vhost;
2961 struct ibmvfc_event *evt;
2962
2963 if (vhost->discovery_threads >= disc_threads)
2964 return;
2965
2966 kref_get(&tgt->kref);
2967 evt = ibmvfc_get_event(vhost);
2968 vhost->discovery_threads++;
2969 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2970 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
2971 evt->tgt = tgt;
2972 plogi = &evt->iu.plogi;
2973 memset(plogi, 0, sizeof(*plogi));
2974 plogi->common.version = 1;
2975 plogi->common.opcode = IBMVFC_PORT_LOGIN;
2976 plogi->common.length = sizeof(*plogi);
2977 plogi->scsi_id = tgt->scsi_id;
2978
2979 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2980 vhost->discovery_threads--;
2981 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2982 kref_put(&tgt->kref, ibmvfc_release_tgt);
2983 } else
2984 tgt_dbg(tgt, "Sent port login\n");
2985}
2986
2987/**
2988 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
2989 * @evt: ibmvfc event struct
2990 *
2991 **/
2992static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
2993{
2994 struct ibmvfc_target *tgt = evt->tgt;
2995 struct ibmvfc_host *vhost = evt->vhost;
2996 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
2997 u32 status = rsp->common.status;
2998
2999 vhost->discovery_threads--;
3000 ibmvfc_free_event(evt);
3001 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3002
3003 switch (status) {
3004 case IBMVFC_MAD_SUCCESS:
3005 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3006 break;
3007 case IBMVFC_MAD_DRIVER_FAILED:
3008 kref_put(&tgt->kref, ibmvfc_release_tgt);
3009 wake_up(&vhost->work_wait_q);
3010 return;
3011 case IBMVFC_MAD_FAILED:
3012 default:
3013 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3014 break;
3015 };
3016
3017 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
3018 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3019 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
3020 tgt->scsi_id != tgt->new_scsi_id)
3021 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3022 kref_put(&tgt->kref, ibmvfc_release_tgt);
3023 wake_up(&vhost->work_wait_q);
3024}
3025
3026/**
3027 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3028 * @tgt: ibmvfc target struct
3029 *
3030 **/
3031static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3032{
3033 struct ibmvfc_implicit_logout *mad;
3034 struct ibmvfc_host *vhost = tgt->vhost;
3035 struct ibmvfc_event *evt;
3036
3037 if (vhost->discovery_threads >= disc_threads)
3038 return;
3039
3040 kref_get(&tgt->kref);
3041 evt = ibmvfc_get_event(vhost);
3042 vhost->discovery_threads++;
3043 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3044 evt->tgt = tgt;
3045 mad = &evt->iu.implicit_logout;
3046 memset(mad, 0, sizeof(*mad));
3047 mad->common.version = 1;
3048 mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
3049 mad->common.length = sizeof(*mad);
3050 mad->old_scsi_id = tgt->scsi_id;
3051
3052 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3053 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3054 vhost->discovery_threads--;
3055 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3056 kref_put(&tgt->kref, ibmvfc_release_tgt);
3057 } else
3058 tgt_dbg(tgt, "Sent Implicit Logout\n");
3059}
3060
3061/**
Brian King989b8542008-07-22 08:31:47 -05003062 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3063 * @mad: ibmvfc passthru mad struct
3064 * @tgt: ibmvfc target struct
3065 *
3066 * Returns:
3067 * 1 if PLOGI needed / 0 if PLOGI not needed
3068 **/
3069static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3070 struct ibmvfc_target *tgt)
3071{
3072 if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3073 sizeof(tgt->ids.port_name)))
3074 return 1;
3075 if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3076 sizeof(tgt->ids.node_name)))
3077 return 1;
3078 if (mad->fc_iu.response[6] != tgt->scsi_id)
3079 return 1;
3080 return 0;
3081}
3082
3083/**
3084 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3085 * @evt: ibmvfc event struct
3086 *
3087 **/
3088static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3089{
3090 struct ibmvfc_target *tgt = evt->tgt;
3091 struct ibmvfc_host *vhost = evt->vhost;
3092 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3093 u32 status = mad->common.status;
3094 u8 fc_reason, fc_explain;
3095
3096 vhost->discovery_threads--;
3097 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
Brian King10501e12009-03-20 15:44:39 -05003098 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003099
3100 switch (status) {
3101 case IBMVFC_MAD_SUCCESS:
3102 tgt_dbg(tgt, "ADISC succeeded\n");
3103 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3104 tgt->need_login = 1;
3105 break;
3106 case IBMVFC_MAD_DRIVER_FAILED:
3107 break;
3108 case IBMVFC_MAD_FAILED:
3109 default:
3110 tgt->need_login = 1;
3111 fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
3112 fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
3113 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3114 ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
3115 mad->iu.status, mad->iu.error,
3116 ibmvfc_get_fc_type(fc_reason), fc_reason,
3117 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3118 break;
3119 };
3120
3121 kref_put(&tgt->kref, ibmvfc_release_tgt);
3122 ibmvfc_free_event(evt);
3123 wake_up(&vhost->work_wait_q);
3124}
3125
3126/**
3127 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3128 * @evt: ibmvfc event struct
3129 *
3130 **/
3131static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3132{
3133 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3134
3135 memset(mad, 0, sizeof(*mad));
3136 mad->common.version = 1;
3137 mad->common.opcode = IBMVFC_PASSTHRU;
3138 mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
3139 mad->cmd_ioba.va = (u64)evt->crq.ioba +
3140 offsetof(struct ibmvfc_passthru_mad, iu);
3141 mad->cmd_ioba.len = sizeof(mad->iu);
3142 mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
3143 mad->iu.rsp_len = sizeof(mad->fc_iu.response);
3144 mad->iu.cmd.va = (u64)evt->crq.ioba +
3145 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3146 offsetof(struct ibmvfc_passthru_fc_iu, payload);
3147 mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
3148 mad->iu.rsp.va = (u64)evt->crq.ioba +
3149 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3150 offsetof(struct ibmvfc_passthru_fc_iu, response);
3151 mad->iu.rsp.len = sizeof(mad->fc_iu.response);
3152}
3153
3154/**
Brian King10501e12009-03-20 15:44:39 -05003155 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3156 * @evt: ibmvfc event struct
3157 *
3158 * Just cleanup this event struct. Everything else is handled by
3159 * the ADISC completion handler. If the ADISC never actually comes
3160 * back, we still have the timer running on the ADISC event struct
3161 * which will fire and cause the CRQ to get reset.
3162 *
3163 **/
3164static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3165{
3166 struct ibmvfc_host *vhost = evt->vhost;
3167 struct ibmvfc_target *tgt = evt->tgt;
3168
3169 tgt_dbg(tgt, "ADISC cancel complete\n");
3170 vhost->abort_threads--;
3171 ibmvfc_free_event(evt);
3172 kref_put(&tgt->kref, ibmvfc_release_tgt);
3173 wake_up(&vhost->work_wait_q);
3174}
3175
3176/**
3177 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3178 * @tgt: ibmvfc target struct
3179 *
3180 * If an ADISC times out, send a cancel. If the cancel times
3181 * out, reset the CRQ. When the ADISC comes back as cancelled,
3182 * log back into the target.
3183 **/
3184static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
3185{
3186 struct ibmvfc_host *vhost = tgt->vhost;
3187 struct ibmvfc_event *evt;
3188 struct ibmvfc_tmf *tmf;
3189 unsigned long flags;
3190 int rc;
3191
3192 tgt_dbg(tgt, "ADISC timeout\n");
3193 spin_lock_irqsave(vhost->host->host_lock, flags);
3194 if (vhost->abort_threads >= disc_threads ||
3195 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3196 vhost->state != IBMVFC_INITIALIZING ||
3197 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3198 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3199 return;
3200 }
3201
3202 vhost->abort_threads++;
3203 kref_get(&tgt->kref);
3204 evt = ibmvfc_get_event(vhost);
3205 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3206
3207 evt->tgt = tgt;
3208 tmf = &evt->iu.tmf;
3209 memset(tmf, 0, sizeof(*tmf));
3210 tmf->common.version = 1;
3211 tmf->common.opcode = IBMVFC_TMF_MAD;
3212 tmf->common.length = sizeof(*tmf);
3213 tmf->scsi_id = tgt->scsi_id;
3214 tmf->cancel_key = tgt->cancel_key;
3215
3216 rc = ibmvfc_send_event(evt, vhost, default_timeout);
3217
3218 if (rc) {
3219 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3220 vhost->abort_threads--;
3221 kref_put(&tgt->kref, ibmvfc_release_tgt);
3222 __ibmvfc_reset_host(vhost);
3223 } else
3224 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3225 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3226}
3227
3228/**
Brian King989b8542008-07-22 08:31:47 -05003229 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3230 * @tgt: ibmvfc target struct
3231 *
Brian King10501e12009-03-20 15:44:39 -05003232 * When sending an ADISC we end up with two timers running. The
3233 * first timer is the timer in the ibmvfc target struct. If this
3234 * fires, we send a cancel to the target. The second timer is the
3235 * timer on the ibmvfc event for the ADISC, which is longer. If that
3236 * fires, it means the ADISC timed out and our attempt to cancel it
3237 * also failed, so we need to reset the CRQ.
Brian King989b8542008-07-22 08:31:47 -05003238 **/
3239static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3240{
3241 struct ibmvfc_passthru_mad *mad;
3242 struct ibmvfc_host *vhost = tgt->vhost;
3243 struct ibmvfc_event *evt;
3244
3245 if (vhost->discovery_threads >= disc_threads)
3246 return;
3247
3248 kref_get(&tgt->kref);
3249 evt = ibmvfc_get_event(vhost);
3250 vhost->discovery_threads++;
3251 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3252 evt->tgt = tgt;
3253
3254 ibmvfc_init_passthru(evt);
3255 mad = &evt->iu.passthru;
3256 mad->iu.flags = IBMVFC_FC_ELS;
3257 mad->iu.scsi_id = tgt->scsi_id;
Brian King10501e12009-03-20 15:44:39 -05003258 mad->iu.cancel_key = tgt->cancel_key;
Brian King989b8542008-07-22 08:31:47 -05003259
3260 mad->fc_iu.payload[0] = IBMVFC_ADISC;
3261 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3262 sizeof(vhost->login_buf->resp.port_name));
3263 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3264 sizeof(vhost->login_buf->resp.node_name));
3265 mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
3266
Brian King10501e12009-03-20 15:44:39 -05003267 if (timer_pending(&tgt->timer))
3268 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
3269 else {
3270 tgt->timer.data = (unsigned long) tgt;
3271 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
3272 tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
3273 add_timer(&tgt->timer);
3274 }
3275
Brian King989b8542008-07-22 08:31:47 -05003276 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
Brian King10501e12009-03-20 15:44:39 -05003277 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
Brian King989b8542008-07-22 08:31:47 -05003278 vhost->discovery_threads--;
Brian King10501e12009-03-20 15:44:39 -05003279 del_timer(&tgt->timer);
Brian King989b8542008-07-22 08:31:47 -05003280 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3281 kref_put(&tgt->kref, ibmvfc_release_tgt);
3282 } else
3283 tgt_dbg(tgt, "Sent ADISC\n");
3284}
3285
3286/**
Brian King072b91f2008-07-01 13:14:30 -05003287 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3288 * @evt: ibmvfc event struct
3289 *
3290 **/
3291static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3292{
3293 struct ibmvfc_target *tgt = evt->tgt;
3294 struct ibmvfc_host *vhost = evt->vhost;
3295 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3296 u32 status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003297 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003298
3299 vhost->discovery_threads--;
3300 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3301 switch (status) {
3302 case IBMVFC_MAD_SUCCESS:
3303 tgt_dbg(tgt, "Query Target succeeded\n");
3304 tgt->new_scsi_id = rsp->scsi_id;
3305 if (rsp->scsi_id != tgt->scsi_id)
3306 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
Brian King989b8542008-07-22 08:31:47 -05003307 else
3308 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
Brian King072b91f2008-07-01 13:14:30 -05003309 break;
3310 case IBMVFC_MAD_DRIVER_FAILED:
3311 break;
3312 case IBMVFC_MAD_CRQ_ERROR:
3313 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3314 break;
3315 case IBMVFC_MAD_FAILED:
3316 default:
Brian King072b91f2008-07-01 13:14:30 -05003317 if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3318 rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3319 rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
3320 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3321 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05003322 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
Brian King10e79492008-10-29 08:46:45 -05003323 else
3324 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
Brian King7d0e4622009-05-28 16:17:26 -05003325
3326 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3327 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3328 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3329 ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
Brian King072b91f2008-07-01 13:14:30 -05003330 break;
3331 };
3332
3333 kref_put(&tgt->kref, ibmvfc_release_tgt);
3334 ibmvfc_free_event(evt);
3335 wake_up(&vhost->work_wait_q);
3336}
3337
3338/**
3339 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3340 * @tgt: ibmvfc target struct
3341 *
3342 **/
3343static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3344{
3345 struct ibmvfc_query_tgt *query_tgt;
3346 struct ibmvfc_host *vhost = tgt->vhost;
3347 struct ibmvfc_event *evt;
3348
3349 if (vhost->discovery_threads >= disc_threads)
3350 return;
3351
3352 kref_get(&tgt->kref);
3353 evt = ibmvfc_get_event(vhost);
3354 vhost->discovery_threads++;
3355 evt->tgt = tgt;
3356 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3357 query_tgt = &evt->iu.query_tgt;
3358 memset(query_tgt, 0, sizeof(*query_tgt));
3359 query_tgt->common.version = 1;
3360 query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
3361 query_tgt->common.length = sizeof(*query_tgt);
3362 query_tgt->wwpn = tgt->ids.port_name;
3363
3364 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3365 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3366 vhost->discovery_threads--;
3367 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3368 kref_put(&tgt->kref, ibmvfc_release_tgt);
3369 } else
3370 tgt_dbg(tgt, "Sent Query Target\n");
3371}
3372
3373/**
3374 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3375 * @vhost: ibmvfc host struct
3376 * @scsi_id: SCSI ID to allocate target for
3377 *
3378 * Returns:
3379 * 0 on success / other on failure
3380 **/
3381static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3382{
3383 struct ibmvfc_target *tgt;
3384 unsigned long flags;
3385
3386 spin_lock_irqsave(vhost->host->host_lock, flags);
3387 list_for_each_entry(tgt, &vhost->targets, queue) {
3388 if (tgt->scsi_id == scsi_id) {
3389 if (tgt->need_login)
3390 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3391 goto unlock_out;
3392 }
3393 }
3394 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3395
Brian King7270b9b2009-05-28 16:17:24 -05003396 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
Brian King072b91f2008-07-01 13:14:30 -05003397 if (!tgt) {
Stephen Rothwell775a42e2009-01-06 14:59:00 +00003398 dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
Brian King072b91f2008-07-01 13:14:30 -05003399 scsi_id);
3400 return -ENOMEM;
3401 }
3402
Brian King0883e3b2009-02-04 16:13:12 -06003403 memset(tgt, 0, sizeof(*tgt));
Brian King072b91f2008-07-01 13:14:30 -05003404 tgt->scsi_id = scsi_id;
3405 tgt->new_scsi_id = scsi_id;
3406 tgt->vhost = vhost;
3407 tgt->need_login = 1;
Brian King10501e12009-03-20 15:44:39 -05003408 tgt->cancel_key = vhost->task_set++;
3409 init_timer(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05003410 kref_init(&tgt->kref);
3411 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3412 spin_lock_irqsave(vhost->host->host_lock, flags);
3413 list_add_tail(&tgt->queue, &vhost->targets);
3414
3415unlock_out:
3416 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3417 return 0;
3418}
3419
3420/**
3421 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3422 * @vhost: ibmvfc host struct
3423 *
3424 * Returns:
3425 * 0 on success / other on failure
3426 **/
3427static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3428{
3429 int i, rc;
3430
3431 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3432 rc = ibmvfc_alloc_target(vhost,
3433 vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3434
3435 return rc;
3436}
3437
3438/**
3439 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3440 * @evt: ibmvfc event struct
3441 *
3442 **/
3443static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3444{
3445 struct ibmvfc_host *vhost = evt->vhost;
3446 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3447 u32 mad_status = rsp->common.status;
Brian King7d0e4622009-05-28 16:17:26 -05003448 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003449
3450 switch (mad_status) {
3451 case IBMVFC_MAD_SUCCESS:
3452 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3453 vhost->num_targets = rsp->num_written;
3454 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3455 break;
3456 case IBMVFC_MAD_FAILED:
Brian King7d0e4622009-05-28 16:17:26 -05003457 level += ibmvfc_retry_host_init(vhost);
3458 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
3459 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05003460 break;
3461 case IBMVFC_MAD_DRIVER_FAILED:
3462 break;
3463 default:
3464 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3465 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3466 break;
3467 }
3468
3469 ibmvfc_free_event(evt);
3470 wake_up(&vhost->work_wait_q);
3471}
3472
3473/**
3474 * ibmvfc_discover_targets - Send Discover Targets MAD
3475 * @vhost: ibmvfc host struct
3476 *
3477 **/
3478static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3479{
3480 struct ibmvfc_discover_targets *mad;
3481 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3482
3483 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3484 mad = &evt->iu.discover_targets;
3485 memset(mad, 0, sizeof(*mad));
3486 mad->common.version = 1;
3487 mad->common.opcode = IBMVFC_DISC_TARGETS;
3488 mad->common.length = sizeof(*mad);
3489 mad->bufflen = vhost->disc_buf_sz;
3490 mad->buffer.va = vhost->disc_buf_dma;
3491 mad->buffer.len = vhost->disc_buf_sz;
3492 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3493
3494 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3495 ibmvfc_dbg(vhost, "Sent discover targets\n");
3496 else
3497 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3498}
3499
3500/**
3501 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3502 * @evt: ibmvfc event struct
3503 *
3504 **/
3505static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3506{
3507 struct ibmvfc_host *vhost = evt->vhost;
3508 u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3509 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3510 unsigned int npiv_max_sectors;
Brian King7d0e4622009-05-28 16:17:26 -05003511 int level = IBMVFC_DEFAULT_LOG_LEVEL;
Brian King072b91f2008-07-01 13:14:30 -05003512
3513 switch (mad_status) {
3514 case IBMVFC_MAD_SUCCESS:
3515 ibmvfc_free_event(evt);
3516 break;
3517 case IBMVFC_MAD_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05003518 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
Brian King7d0e4622009-05-28 16:17:26 -05003519 level += ibmvfc_retry_host_init(vhost);
Brian King072b91f2008-07-01 13:14:30 -05003520 else
3521 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
Brian King7d0e4622009-05-28 16:17:26 -05003522 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
3523 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
Brian King072b91f2008-07-01 13:14:30 -05003524 ibmvfc_free_event(evt);
3525 return;
3526 case IBMVFC_MAD_CRQ_ERROR:
3527 ibmvfc_retry_host_init(vhost);
3528 case IBMVFC_MAD_DRIVER_FAILED:
3529 ibmvfc_free_event(evt);
3530 return;
3531 default:
3532 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
3533 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3534 ibmvfc_free_event(evt);
3535 return;
3536 }
3537
3538 vhost->client_migrated = 0;
3539
3540 if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
3541 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
3542 rsp->flags);
3543 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3544 wake_up(&vhost->work_wait_q);
3545 return;
3546 }
3547
3548 if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
3549 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
3550 rsp->max_cmds);
3551 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3552 wake_up(&vhost->work_wait_q);
3553 return;
3554 }
3555
3556 npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
3557 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
3558 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
3559 rsp->drc_name, npiv_max_sectors);
3560
3561 fc_host_fabric_name(vhost->host) = rsp->node_name;
3562 fc_host_node_name(vhost->host) = rsp->node_name;
3563 fc_host_port_name(vhost->host) = rsp->port_name;
3564 fc_host_port_id(vhost->host) = rsp->scsi_id;
3565 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
3566 fc_host_supported_classes(vhost->host) = 0;
3567 if (rsp->service_parms.class1_parms[0] & 0x80000000)
3568 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
3569 if (rsp->service_parms.class2_parms[0] & 0x80000000)
3570 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
3571 if (rsp->service_parms.class3_parms[0] & 0x80000000)
3572 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
3573 fc_host_maxframe_size(vhost->host) =
3574 rsp->service_parms.common.bb_rcv_sz & 0x0fff;
3575
3576 vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
3577 vhost->host->max_sectors = npiv_max_sectors;
3578 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3579 wake_up(&vhost->work_wait_q);
3580}
3581
3582/**
3583 * ibmvfc_npiv_login - Sends NPIV login
3584 * @vhost: ibmvfc host struct
3585 *
3586 **/
3587static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
3588{
3589 struct ibmvfc_npiv_login_mad *mad;
3590 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3591
3592 ibmvfc_gather_partition_info(vhost);
3593 ibmvfc_set_login_info(vhost);
3594 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
3595
3596 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
3597 mad = &evt->iu.npiv_login;
3598 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
3599 mad->common.version = 1;
3600 mad->common.opcode = IBMVFC_NPIV_LOGIN;
3601 mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
3602 mad->buffer.va = vhost->login_buf_dma;
3603 mad->buffer.len = sizeof(*vhost->login_buf);
3604
Brian King072b91f2008-07-01 13:14:30 -05003605 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3606
3607 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3608 ibmvfc_dbg(vhost, "Sent NPIV login\n");
3609 else
3610 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3611};
3612
3613/**
3614 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
3615 * @vhost: ibmvfc host struct
3616 *
3617 * Returns:
3618 * 1 if work to do / 0 if not
3619 **/
3620static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
3621{
3622 struct ibmvfc_target *tgt;
3623
3624 list_for_each_entry(tgt, &vhost->targets, queue) {
3625 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
3626 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3627 return 1;
3628 }
3629
3630 return 0;
3631}
3632
3633/**
3634 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
3635 * @vhost: ibmvfc host struct
3636 *
3637 * Returns:
3638 * 1 if work to do / 0 if not
3639 **/
3640static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3641{
3642 struct ibmvfc_target *tgt;
3643
3644 if (kthread_should_stop())
3645 return 1;
3646 switch (vhost->action) {
3647 case IBMVFC_HOST_ACTION_NONE:
3648 case IBMVFC_HOST_ACTION_INIT_WAIT:
3649 return 0;
3650 case IBMVFC_HOST_ACTION_TGT_INIT:
3651 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3652 if (vhost->discovery_threads == disc_threads)
3653 return 0;
3654 list_for_each_entry(tgt, &vhost->targets, queue)
3655 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
3656 return 1;
3657 list_for_each_entry(tgt, &vhost->targets, queue)
3658 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3659 return 0;
3660 return 1;
3661 case IBMVFC_HOST_ACTION_INIT:
3662 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3663 case IBMVFC_HOST_ACTION_TGT_ADD:
3664 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05003665 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05003666 case IBMVFC_HOST_ACTION_QUERY:
3667 default:
3668 break;
3669 };
3670
3671 return 1;
3672}
3673
3674/**
3675 * ibmvfc_work_to_do - Is there task level work to do?
3676 * @vhost: ibmvfc host struct
3677 *
3678 * Returns:
3679 * 1 if work to do / 0 if not
3680 **/
3681static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3682{
3683 unsigned long flags;
3684 int rc;
3685
3686 spin_lock_irqsave(vhost->host->host_lock, flags);
3687 rc = __ibmvfc_work_to_do(vhost);
3688 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3689 return rc;
3690}
3691
3692/**
3693 * ibmvfc_log_ae - Log async events if necessary
3694 * @vhost: ibmvfc host struct
3695 * @events: events to log
3696 *
3697 **/
3698static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
3699{
3700 if (events & IBMVFC_AE_RSCN)
3701 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
3702 if ((events & IBMVFC_AE_LINKDOWN) &&
3703 vhost->state >= IBMVFC_HALTED)
3704 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
3705 if ((events & IBMVFC_AE_LINKUP) &&
3706 vhost->state == IBMVFC_INITIALIZING)
3707 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
3708}
3709
3710/**
3711 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
3712 * @tgt: ibmvfc target struct
3713 *
3714 **/
3715static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
3716{
3717 struct ibmvfc_host *vhost = tgt->vhost;
Brian King0883e3b2009-02-04 16:13:12 -06003718 struct fc_rport *rport = tgt->rport;
Brian King072b91f2008-07-01 13:14:30 -05003719 unsigned long flags;
3720
Brian King0883e3b2009-02-04 16:13:12 -06003721 if (rport) {
3722 tgt_dbg(tgt, "Setting rport roles\n");
3723 fc_remote_port_rolechg(rport, tgt->ids.roles);
3724 spin_lock_irqsave(vhost->host->host_lock, flags);
3725 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3726 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3727 return;
3728 }
3729
Brian King072b91f2008-07-01 13:14:30 -05003730 tgt_dbg(tgt, "Adding rport\n");
3731 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
3732 spin_lock_irqsave(vhost->host->host_lock, flags);
3733 tgt->rport = rport;
3734 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3735 if (rport) {
3736 tgt_dbg(tgt, "rport add succeeded\n");
3737 rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
3738 rport->supported_classes = 0;
Brian King52d7e862008-07-22 08:31:46 -05003739 tgt->target_id = rport->scsi_target_id;
Brian King072b91f2008-07-01 13:14:30 -05003740 if (tgt->service_parms.class1_parms[0] & 0x80000000)
3741 rport->supported_classes |= FC_COS_CLASS1;
3742 if (tgt->service_parms.class2_parms[0] & 0x80000000)
3743 rport->supported_classes |= FC_COS_CLASS2;
3744 if (tgt->service_parms.class3_parms[0] & 0x80000000)
3745 rport->supported_classes |= FC_COS_CLASS3;
3746 } else
3747 tgt_dbg(tgt, "rport add failed\n");
3748 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3749}
3750
3751/**
3752 * ibmvfc_do_work - Do task level work
3753 * @vhost: ibmvfc host struct
3754 *
3755 **/
3756static void ibmvfc_do_work(struct ibmvfc_host *vhost)
3757{
3758 struct ibmvfc_target *tgt;
3759 unsigned long flags;
3760 struct fc_rport *rport;
3761
3762 ibmvfc_log_ae(vhost, vhost->events_to_log);
3763 spin_lock_irqsave(vhost->host->host_lock, flags);
3764 vhost->events_to_log = 0;
3765 switch (vhost->action) {
3766 case IBMVFC_HOST_ACTION_NONE:
3767 case IBMVFC_HOST_ACTION_INIT_WAIT:
3768 break;
3769 case IBMVFC_HOST_ACTION_INIT:
3770 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
Brian King1c41fa822008-12-03 11:02:54 -06003771 if (vhost->delay_init) {
3772 vhost->delay_init = 0;
3773 spin_unlock_irqrestore(vhost->host->host_lock, flags);
Brian Kingd2131b32008-12-18 09:26:51 -06003774 ssleep(15);
Brian King1c41fa822008-12-03 11:02:54 -06003775 return;
3776 } else
3777 vhost->job_step(vhost);
Brian King072b91f2008-07-01 13:14:30 -05003778 break;
3779 case IBMVFC_HOST_ACTION_QUERY:
3780 list_for_each_entry(tgt, &vhost->targets, queue)
3781 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
3782 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
3783 break;
3784 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3785 list_for_each_entry(tgt, &vhost->targets, queue) {
3786 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3787 tgt->job_step(tgt);
3788 break;
3789 }
3790 }
3791
3792 if (!ibmvfc_dev_init_to_do(vhost))
3793 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
3794 break;
3795 case IBMVFC_HOST_ACTION_TGT_DEL:
Brian King10e79492008-10-29 08:46:45 -05003796 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
Brian King072b91f2008-07-01 13:14:30 -05003797 list_for_each_entry(tgt, &vhost->targets, queue) {
3798 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3799 tgt_dbg(tgt, "Deleting rport\n");
3800 rport = tgt->rport;
3801 tgt->rport = NULL;
3802 list_del(&tgt->queue);
3803 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3804 if (rport)
3805 fc_remote_port_delete(rport);
Brian King10501e12009-03-20 15:44:39 -05003806 del_timer_sync(&tgt->timer);
Brian King072b91f2008-07-01 13:14:30 -05003807 kref_put(&tgt->kref, ibmvfc_release_tgt);
3808 return;
3809 }
3810 }
3811
3812 if (vhost->state == IBMVFC_INITIALIZING) {
Brian King10e79492008-10-29 08:46:45 -05003813 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
3814 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
3815 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_ADD);
3816 vhost->init_retries = 0;
3817 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3818 scsi_unblock_requests(vhost->host);
3819 return;
3820 } else {
3821 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
3822 vhost->job_step = ibmvfc_discover_targets;
3823 }
Brian King072b91f2008-07-01 13:14:30 -05003824 } else {
3825 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3826 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3827 scsi_unblock_requests(vhost->host);
3828 wake_up(&vhost->init_wait_q);
3829 return;
3830 }
3831 break;
3832 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3833 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
3834 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3835 ibmvfc_alloc_targets(vhost);
3836 spin_lock_irqsave(vhost->host->host_lock, flags);
3837 break;
3838 case IBMVFC_HOST_ACTION_TGT_INIT:
3839 list_for_each_entry(tgt, &vhost->targets, queue) {
3840 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3841 tgt->job_step(tgt);
3842 break;
3843 }
3844 }
3845
Brian King10e79492008-10-29 08:46:45 -05003846 if (!ibmvfc_dev_init_to_do(vhost))
3847 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
Brian King072b91f2008-07-01 13:14:30 -05003848 break;
3849 case IBMVFC_HOST_ACTION_TGT_ADD:
3850 list_for_each_entry(tgt, &vhost->targets, queue) {
3851 if (tgt->action == IBMVFC_TGT_ACTION_ADD_RPORT) {
3852 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3853 ibmvfc_tgt_add_rport(tgt);
3854 return;
Brian King072b91f2008-07-01 13:14:30 -05003855 }
3856 }
3857
Brian King915be022008-08-15 10:59:22 -05003858 if (vhost->reinit && !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
Brian King072b91f2008-07-01 13:14:30 -05003859 vhost->reinit = 0;
Brian King915be022008-08-15 10:59:22 -05003860 scsi_block_requests(vhost->host);
Brian King072b91f2008-07-01 13:14:30 -05003861 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3862 } else {
3863 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3864 wake_up(&vhost->init_wait_q);
3865 }
3866 break;
3867 default:
3868 break;
3869 };
3870
3871 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3872}
3873
3874/**
3875 * ibmvfc_work - Do task level work
3876 * @data: ibmvfc host struct
3877 *
3878 * Returns:
3879 * zero
3880 **/
3881static int ibmvfc_work(void *data)
3882{
3883 struct ibmvfc_host *vhost = data;
3884 int rc;
3885
3886 set_user_nice(current, -20);
3887
3888 while (1) {
3889 rc = wait_event_interruptible(vhost->work_wait_q,
3890 ibmvfc_work_to_do(vhost));
3891
3892 BUG_ON(rc);
3893
3894 if (kthread_should_stop())
3895 break;
3896
3897 ibmvfc_do_work(vhost);
3898 }
3899
3900 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
3901 return 0;
3902}
3903
3904/**
3905 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
3906 * @vhost: ibmvfc host struct
3907 *
3908 * Allocates a page for messages, maps it for dma, and registers
3909 * the crq with the hypervisor.
3910 *
3911 * Return value:
3912 * zero on success / other on failure
3913 **/
3914static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
3915{
3916 int rc, retrc = -ENOMEM;
3917 struct device *dev = vhost->dev;
3918 struct vio_dev *vdev = to_vio_dev(dev);
3919 struct ibmvfc_crq_queue *crq = &vhost->crq;
3920
3921 ENTER;
3922 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
3923
3924 if (!crq->msgs)
3925 return -ENOMEM;
3926
3927 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3928 crq->msg_token = dma_map_single(dev, crq->msgs,
3929 PAGE_SIZE, DMA_BIDIRECTIONAL);
3930
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07003931 if (dma_mapping_error(dev, crq->msg_token))
Brian King072b91f2008-07-01 13:14:30 -05003932 goto map_failed;
3933
3934 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3935 crq->msg_token, PAGE_SIZE);
3936
3937 if (rc == H_RESOURCE)
3938 /* maybe kexecing and resource is busy. try a reset */
3939 retrc = rc = ibmvfc_reset_crq(vhost);
3940
3941 if (rc == H_CLOSED)
3942 dev_warn(dev, "Partner adapter not ready\n");
3943 else if (rc) {
3944 dev_warn(dev, "Error %d opening adapter\n", rc);
3945 goto reg_crq_failed;
3946 }
3947
3948 retrc = 0;
3949
Brian King039a0892009-03-20 15:44:35 -05003950 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
3951
Brian King072b91f2008-07-01 13:14:30 -05003952 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
3953 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
3954 goto req_irq_failed;
3955 }
3956
3957 if ((rc = vio_enable_interrupts(vdev))) {
3958 dev_err(dev, "Error %d enabling interrupts\n", rc);
3959 goto req_irq_failed;
3960 }
3961
3962 crq->cur = 0;
3963 LEAVE;
3964 return retrc;
3965
3966req_irq_failed:
Brian King039a0892009-03-20 15:44:35 -05003967 tasklet_kill(&vhost->tasklet);
Brian King072b91f2008-07-01 13:14:30 -05003968 do {
3969 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3970 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3971reg_crq_failed:
3972 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3973map_failed:
3974 free_page((unsigned long)crq->msgs);
3975 return retrc;
3976}
3977
3978/**
3979 * ibmvfc_free_mem - Free memory for vhost
3980 * @vhost: ibmvfc host struct
3981 *
3982 * Return value:
3983 * none
3984 **/
3985static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
3986{
3987 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3988
3989 ENTER;
3990 mempool_destroy(vhost->tgt_pool);
3991 kfree(vhost->trace);
3992 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
3993 vhost->disc_buf_dma);
3994 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
3995 vhost->login_buf, vhost->login_buf_dma);
3996 dma_pool_destroy(vhost->sg_pool);
3997 dma_unmap_single(vhost->dev, async_q->msg_token,
3998 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3999 free_page((unsigned long)async_q->msgs);
4000 LEAVE;
4001}
4002
4003/**
4004 * ibmvfc_alloc_mem - Allocate memory for vhost
4005 * @vhost: ibmvfc host struct
4006 *
4007 * Return value:
4008 * 0 on success / non-zero on failure
4009 **/
4010static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4011{
4012 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4013 struct device *dev = vhost->dev;
4014
4015 ENTER;
4016 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4017 if (!async_q->msgs) {
4018 dev_err(dev, "Couldn't allocate async queue.\n");
4019 goto nomem;
4020 }
4021
4022 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4023 async_q->msg_token = dma_map_single(dev, async_q->msgs,
4024 async_q->size * sizeof(*async_q->msgs),
4025 DMA_BIDIRECTIONAL);
4026
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07004027 if (dma_mapping_error(dev, async_q->msg_token)) {
Brian King072b91f2008-07-01 13:14:30 -05004028 dev_err(dev, "Failed to map async queue\n");
4029 goto free_async_crq;
4030 }
4031
4032 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4033 SG_ALL * sizeof(struct srp_direct_buf),
4034 sizeof(struct srp_direct_buf), 0);
4035
4036 if (!vhost->sg_pool) {
4037 dev_err(dev, "Failed to allocate sg pool\n");
4038 goto unmap_async_crq;
4039 }
4040
4041 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4042 &vhost->login_buf_dma, GFP_KERNEL);
4043
4044 if (!vhost->login_buf) {
4045 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4046 goto free_sg_pool;
4047 }
4048
4049 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
4050 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
4051 &vhost->disc_buf_dma, GFP_KERNEL);
4052
4053 if (!vhost->disc_buf) {
4054 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
4055 goto free_login_buffer;
4056 }
4057
4058 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
4059 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
4060
4061 if (!vhost->trace)
4062 goto free_disc_buffer;
4063
4064 vhost->tgt_pool = mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
4065 sizeof(struct ibmvfc_target));
4066
4067 if (!vhost->tgt_pool) {
4068 dev_err(dev, "Couldn't allocate target memory pool\n");
4069 goto free_trace;
4070 }
4071
4072 LEAVE;
4073 return 0;
4074
4075free_trace:
4076 kfree(vhost->trace);
4077free_disc_buffer:
4078 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
4079 vhost->disc_buf_dma);
4080free_login_buffer:
4081 dma_free_coherent(dev, sizeof(*vhost->login_buf),
4082 vhost->login_buf, vhost->login_buf_dma);
4083free_sg_pool:
4084 dma_pool_destroy(vhost->sg_pool);
4085unmap_async_crq:
4086 dma_unmap_single(dev, async_q->msg_token,
4087 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4088free_async_crq:
4089 free_page((unsigned long)async_q->msgs);
4090nomem:
4091 LEAVE;
4092 return -ENOMEM;
4093}
4094
4095/**
4096 * ibmvfc_probe - Adapter hot plug add entry point
4097 * @vdev: vio device struct
4098 * @id: vio device id struct
4099 *
4100 * Return value:
4101 * 0 on success / non-zero on failure
4102 **/
4103static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4104{
4105 struct ibmvfc_host *vhost;
4106 struct Scsi_Host *shost;
4107 struct device *dev = &vdev->dev;
4108 int rc = -ENOMEM;
4109
4110 ENTER;
4111 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
4112 if (!shost) {
4113 dev_err(dev, "Couldn't allocate host data\n");
4114 goto out;
4115 }
4116
4117 shost->transportt = ibmvfc_transport_template;
4118 shost->can_queue = max_requests;
4119 shost->max_lun = max_lun;
4120 shost->max_id = max_targets;
4121 shost->max_sectors = IBMVFC_MAX_SECTORS;
4122 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
4123 shost->unique_id = shost->host_no;
4124
4125 vhost = shost_priv(shost);
4126 INIT_LIST_HEAD(&vhost->sent);
4127 INIT_LIST_HEAD(&vhost->free);
4128 INIT_LIST_HEAD(&vhost->targets);
4129 sprintf(vhost->name, IBMVFC_NAME);
4130 vhost->host = shost;
4131 vhost->dev = dev;
4132 vhost->partition_number = -1;
4133 vhost->log_level = log_level;
Brian King10501e12009-03-20 15:44:39 -05004134 vhost->task_set = 1;
Brian King072b91f2008-07-01 13:14:30 -05004135 strcpy(vhost->partition_name, "UNKNOWN");
4136 init_waitqueue_head(&vhost->work_wait_q);
4137 init_waitqueue_head(&vhost->init_wait_q);
4138
4139 if ((rc = ibmvfc_alloc_mem(vhost)))
4140 goto free_scsi_host;
4141
4142 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
4143 shost->host_no);
4144
4145 if (IS_ERR(vhost->work_thread)) {
4146 dev_err(dev, "Couldn't create kernel thread: %ld\n",
4147 PTR_ERR(vhost->work_thread));
4148 goto free_host_mem;
4149 }
4150
4151 if ((rc = ibmvfc_init_crq(vhost))) {
4152 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4153 goto kill_kthread;
4154 }
4155
4156 if ((rc = ibmvfc_init_event_pool(vhost))) {
4157 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4158 goto release_crq;
4159 }
4160
4161 if ((rc = scsi_add_host(shost, dev)))
4162 goto release_event_pool;
4163
4164 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4165 &ibmvfc_trace_attr))) {
4166 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4167 goto remove_shost;
4168 }
4169
4170 dev_set_drvdata(dev, vhost);
4171 spin_lock(&ibmvfc_driver_lock);
4172 list_add_tail(&vhost->queue, &ibmvfc_head);
4173 spin_unlock(&ibmvfc_driver_lock);
4174
4175 ibmvfc_send_crq_init(vhost);
4176 scsi_scan_host(shost);
4177 return 0;
4178
4179remove_shost:
4180 scsi_remove_host(shost);
4181release_event_pool:
4182 ibmvfc_free_event_pool(vhost);
4183release_crq:
4184 ibmvfc_release_crq_queue(vhost);
4185kill_kthread:
4186 kthread_stop(vhost->work_thread);
4187free_host_mem:
4188 ibmvfc_free_mem(vhost);
4189free_scsi_host:
4190 scsi_host_put(shost);
4191out:
4192 LEAVE;
4193 return rc;
4194}
4195
4196/**
4197 * ibmvfc_remove - Adapter hot plug remove entry point
4198 * @vdev: vio device struct
4199 *
4200 * Return value:
4201 * 0
4202 **/
4203static int ibmvfc_remove(struct vio_dev *vdev)
4204{
4205 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4206 unsigned long flags;
4207
4208 ENTER;
4209 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
Brian King2d0da2a2008-07-22 08:31:42 -05004210 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
4211 ibmvfc_wait_while_resetting(vhost);
4212 ibmvfc_release_crq_queue(vhost);
Brian King072b91f2008-07-01 13:14:30 -05004213 kthread_stop(vhost->work_thread);
4214 fc_remove_host(vhost->host);
4215 scsi_remove_host(vhost->host);
Brian King072b91f2008-07-01 13:14:30 -05004216
4217 spin_lock_irqsave(vhost->host->host_lock, flags);
4218 ibmvfc_purge_requests(vhost, DID_ERROR);
4219 ibmvfc_free_event_pool(vhost);
4220 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4221
4222 ibmvfc_free_mem(vhost);
4223 spin_lock(&ibmvfc_driver_lock);
4224 list_del(&vhost->queue);
4225 spin_unlock(&ibmvfc_driver_lock);
4226 scsi_host_put(vhost->host);
4227 LEAVE;
4228 return 0;
4229}
4230
Brian King39c1ffe2008-07-24 04:35:48 +10004231/**
4232 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4233 * @vdev: vio device struct
4234 *
4235 * Return value:
4236 * Number of bytes the driver will need to DMA map at the same time in
4237 * order to perform well.
4238 */
4239static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4240{
4241 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4242 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4243}
4244
Brian King072b91f2008-07-01 13:14:30 -05004245static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
4246 {"fcp", "IBM,vfc-client"},
4247 { "", "" }
4248};
4249MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4250
4251static struct vio_driver ibmvfc_driver = {
4252 .id_table = ibmvfc_device_table,
4253 .probe = ibmvfc_probe,
4254 .remove = ibmvfc_remove,
Brian King39c1ffe2008-07-24 04:35:48 +10004255 .get_desired_dma = ibmvfc_get_desired_dma,
Brian King072b91f2008-07-01 13:14:30 -05004256 .driver = {
4257 .name = IBMVFC_NAME,
4258 .owner = THIS_MODULE,
4259 }
4260};
4261
4262static struct fc_function_template ibmvfc_transport_functions = {
4263 .show_host_fabric_name = 1,
4264 .show_host_node_name = 1,
4265 .show_host_port_name = 1,
4266 .show_host_supported_classes = 1,
4267 .show_host_port_type = 1,
4268 .show_host_port_id = 1,
Brian King9ab36102009-03-20 15:44:38 -05004269 .show_host_maxframe_size = 1,
Brian King072b91f2008-07-01 13:14:30 -05004270
4271 .get_host_port_state = ibmvfc_get_host_port_state,
4272 .show_host_port_state = 1,
4273
4274 .get_host_speed = ibmvfc_get_host_speed,
4275 .show_host_speed = 1,
4276
4277 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4278 .terminate_rport_io = ibmvfc_terminate_rport_io,
4279
4280 .show_rport_maxframe_size = 1,
4281 .show_rport_supported_classes = 1,
4282
4283 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4284 .show_rport_dev_loss_tmo = 1,
4285
4286 .get_starget_node_name = ibmvfc_get_starget_node_name,
4287 .show_starget_node_name = 1,
4288
4289 .get_starget_port_name = ibmvfc_get_starget_port_name,
4290 .show_starget_port_name = 1,
4291
4292 .get_starget_port_id = ibmvfc_get_starget_port_id,
4293 .show_starget_port_id = 1,
4294};
4295
4296/**
4297 * ibmvfc_module_init - Initialize the ibmvfc module
4298 *
4299 * Return value:
4300 * 0 on success / other on failure
4301 **/
4302static int __init ibmvfc_module_init(void)
4303{
4304 int rc;
4305
4306 if (!firmware_has_feature(FW_FEATURE_VIO))
4307 return -ENODEV;
4308
4309 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4310 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4311
4312 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4313 if (!ibmvfc_transport_template)
4314 return -ENOMEM;
4315
4316 rc = vio_register_driver(&ibmvfc_driver);
4317 if (rc)
4318 fc_release_transport(ibmvfc_transport_template);
4319 return rc;
4320}
4321
4322/**
4323 * ibmvfc_module_exit - Teardown the ibmvfc module
4324 *
4325 * Return value:
4326 * nothing
4327 **/
4328static void __exit ibmvfc_module_exit(void)
4329{
4330 vio_unregister_driver(&ibmvfc_driver);
4331 fc_release_transport(ibmvfc_transport_template);
4332}
4333
4334module_init(ibmvfc_module_init);
4335module_exit(ibmvfc_module_exit);