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