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