]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/ibmvscsi/ibmvfc.c
[SCSI] ibmvfc: Fixup command response translation
[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:
936 ibmvfc_log(vhost, 3, "Unknown port speed: %ld Gbit\n",
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) {
1325 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1326 return -ENOMEM;
1327 }
1328 }
1329
1330 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1331
1332 data->va = evt->ext_list_token;
1333 data->len = sg_mapped * sizeof(struct srp_direct_buf);
1334 data->key = 0;
1335 return 0;
1336}
1337
1338/**
1339 * ibmvfc_timeout - Internal command timeout handler
1340 * @evt: struct ibmvfc_event that timed out
1341 *
1342 * Called when an internally generated command times out
1343 **/
1344static void ibmvfc_timeout(struct ibmvfc_event *evt)
1345{
1346 struct ibmvfc_host *vhost = evt->vhost;
1347 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1348 ibmvfc_reset_host(vhost);
1349}
1350
1351/**
1352 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1353 * @evt: event to be sent
1354 * @vhost: ibmvfc host struct
1355 * @timeout: timeout in seconds - 0 means do not time command
1356 *
1357 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1358 **/
1359static int ibmvfc_send_event(struct ibmvfc_event *evt,
1360 struct ibmvfc_host *vhost, unsigned long timeout)
1361{
1362 u64 *crq_as_u64 = (u64 *) &evt->crq;
1363 int rc;
1364
1365 /* Copy the IU into the transfer area */
1366 *evt->xfer_iu = evt->iu;
1367 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1368 evt->xfer_iu->cmd.tag = (u64)evt;
1369 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1370 evt->xfer_iu->mad_common.tag = (u64)evt;
1371 else
1372 BUG();
1373
1374 list_add_tail(&evt->queue, &vhost->sent);
1375 init_timer(&evt->timer);
1376
1377 if (timeout) {
1378 evt->timer.data = (unsigned long) evt;
1379 evt->timer.expires = jiffies + (timeout * HZ);
1380 evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1381 add_timer(&evt->timer);
1382 }
1383
a528ab7a
BK
1384 mb();
1385
072b91f9
BK
1386 if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
1387 list_del(&evt->queue);
1388 del_timer(&evt->timer);
1389
1390 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1391 * Firmware will send a CRQ with a transport event (0xFF) to
1392 * tell this client what has happened to the transport. This
1393 * will be handled in ibmvfc_handle_crq()
1394 */
1395 if (rc == H_CLOSED) {
1396 if (printk_ratelimit())
1397 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1398 if (evt->cmnd)
1399 scsi_dma_unmap(evt->cmnd);
1400 ibmvfc_free_event(evt);
1401 return SCSI_MLQUEUE_HOST_BUSY;
1402 }
1403
1404 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1405 if (evt->cmnd) {
1406 evt->cmnd->result = DID_ERROR << 16;
1407 evt->done = ibmvfc_scsi_eh_done;
1408 } else
1409 evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
1410
1411 evt->done(evt);
1412 } else
1413 ibmvfc_trc_start(evt);
1414
1415 return 0;
1416}
1417
1418/**
1419 * ibmvfc_log_error - Log an error for the failed command if appropriate
1420 * @evt: ibmvfc event to log
1421 *
1422 **/
1423static void ibmvfc_log_error(struct ibmvfc_event *evt)
1424{
1425 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1426 struct ibmvfc_host *vhost = evt->vhost;
1427 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1428 struct scsi_cmnd *cmnd = evt->cmnd;
1429 const char *err = unknown_error;
1430 int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
1431 int logerr = 0;
1432 int rsp_code = 0;
1433
1434 if (index >= 0) {
1435 logerr = cmd_status[index].log;
1436 err = cmd_status[index].name;
1437 }
1438
0ae808e0 1439 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
072b91f9
BK
1440 return;
1441
1442 if (rsp->flags & FCP_RSP_LEN_VALID)
1443 rsp_code = rsp->data.info.rsp_code;
1444
1445 scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1446 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1447 cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1448 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1449}
1450
1451/**
1452 * ibmvfc_scsi_done - Handle responses from commands
1453 * @evt: ibmvfc event to be handled
1454 *
1455 * Used as a callback when sending scsi cmds.
1456 **/
1457static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1458{
1459 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1460 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1461 struct scsi_cmnd *cmnd = evt->cmnd;
2bac406d
BK
1462 u32 rsp_len = 0;
1463 u32 sense_len = rsp->fcp_sense_len;
072b91f9
BK
1464
1465 if (cmnd) {
1466 if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
1467 scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
1468 else if (rsp->flags & FCP_RESID_UNDER)
1469 scsi_set_resid(cmnd, rsp->fcp_resid);
1470 else
1471 scsi_set_resid(cmnd, 0);
1472
1473 if (vfc_cmd->status) {
1474 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1475
1476 if (rsp->flags & FCP_RSP_LEN_VALID)
1477 rsp_len = rsp->fcp_rsp_len;
1478 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1479 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
2bac406d 1480 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
072b91f9 1481 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
646d3857
BK
1482 if ((vfc_cmd->status & IBMVFC_VIOS_FAILURE) && (vfc_cmd->error == IBMVFC_PLOGI_REQUIRED))
1483 ibmvfc_reinit_host(evt->vhost);
072b91f9 1484
50119dad
BK
1485 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1486 cmnd->result = (DID_ERROR << 16);
1487
072b91f9
BK
1488 ibmvfc_log_error(evt);
1489 }
1490
1491 if (!cmnd->result &&
1492 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1493 cmnd->result = (DID_ERROR << 16);
1494
1495 scsi_dma_unmap(cmnd);
1496 cmnd->scsi_done(cmnd);
1497 }
1498
ad8dcffa
BK
1499 if (evt->eh_comp)
1500 complete(evt->eh_comp);
1501
072b91f9
BK
1502 ibmvfc_free_event(evt);
1503}
1504
1505/**
1506 * ibmvfc_host_chkready - Check if the host can accept commands
1507 * @vhost: struct ibmvfc host
1508 *
1509 * Returns:
1510 * 1 if host can accept command / 0 if not
1511 **/
1512static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1513{
1514 int result = 0;
1515
1516 switch (vhost->state) {
1517 case IBMVFC_LINK_DEAD:
1518 case IBMVFC_HOST_OFFLINE:
1519 result = DID_NO_CONNECT << 16;
1520 break;
1521 case IBMVFC_NO_CRQ:
1522 case IBMVFC_INITIALIZING:
1523 case IBMVFC_HALTED:
1524 case IBMVFC_LINK_DOWN:
1525 result = DID_REQUEUE << 16;
1526 break;
1527 case IBMVFC_ACTIVE:
1528 result = 0;
1529 break;
1530 };
1531
1532 return result;
1533}
1534
1535/**
1536 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1537 * @cmnd: struct scsi_cmnd to be executed
1538 * @done: Callback function to be called when cmnd is completed
1539 *
1540 * Returns:
1541 * 0 on success / other on failure
1542 **/
1543static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
1544 void (*done) (struct scsi_cmnd *))
1545{
1546 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1547 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1548 struct ibmvfc_cmd *vfc_cmd;
1549 struct ibmvfc_event *evt;
1550 u8 tag[2];
1551 int rc;
1552
1553 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1554 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1555 cmnd->result = rc;
1556 done(cmnd);
1557 return 0;
1558 }
1559
1560 cmnd->result = (DID_OK << 16);
1561 evt = ibmvfc_get_event(vhost);
1562 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1563 evt->cmnd = cmnd;
1564 cmnd->scsi_done = done;
1565 vfc_cmd = &evt->iu.cmd;
1566 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1567 vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1568 vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
1569 vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
1570 vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
1571 vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
1572 vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
1573 vfc_cmd->tgt_scsi_id = rport->port_id;
1574 if ((rport->supported_classes & FC_COS_CLASS3) &&
1575 (fc_host_supported_classes(vhost->host) & FC_COS_CLASS3))
1576 vfc_cmd->flags = IBMVFC_CLASS_3_ERR;
1577 vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
1578 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1579 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1580
1581 if (scsi_populate_tag_msg(cmnd, tag)) {
1582 vfc_cmd->task_tag = tag[1];
1583 switch (tag[0]) {
1584 case MSG_SIMPLE_TAG:
1585 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1586 break;
1587 case MSG_HEAD_TAG:
1588 vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
1589 break;
1590 case MSG_ORDERED_TAG:
1591 vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
1592 break;
1593 };
1594 }
1595
1596 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1597 return ibmvfc_send_event(evt, vhost, 0);
1598
1599 ibmvfc_free_event(evt);
1600 if (rc == -ENOMEM)
1601 return SCSI_MLQUEUE_HOST_BUSY;
1602
1603 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1604 scmd_printk(KERN_ERR, cmnd,
1605 "Failed to map DMA buffer for command. rc=%d\n", rc);
1606
1607 cmnd->result = DID_ERROR << 16;
1608 done(cmnd);
1609 return 0;
1610}
1611
1612/**
1613 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1614 * @evt: ibmvfc event struct
1615 *
1616 **/
1617static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1618{
1619 /* copy the response back */
1620 if (evt->sync_iu)
1621 *evt->sync_iu = *evt->xfer_iu;
1622
1623 complete(&evt->comp);
1624}
1625
1626/**
1627 * ibmvfc_reset_device - Reset the device with the specified reset type
1628 * @sdev: scsi device to reset
1629 * @type: reset type
1630 * @desc: reset type description for log messages
1631 *
1632 * Returns:
1633 * 0 on success / other on failure
1634 **/
1635static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1636{
1637 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1638 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1639 struct ibmvfc_cmd *tmf;
50ed9a00 1640 struct ibmvfc_event *evt = NULL;
072b91f9
BK
1641 union ibmvfc_iu rsp_iu;
1642 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1643 int rsp_rc = -EBUSY;
1644 unsigned long flags;
1645 int rsp_code = 0;
1646
1647 spin_lock_irqsave(vhost->host->host_lock, flags);
1648 if (vhost->state == IBMVFC_ACTIVE) {
1649 evt = ibmvfc_get_event(vhost);
1650 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1651
1652 tmf = &evt->iu.cmd;
1653 memset(tmf, 0, sizeof(*tmf));
1654 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1655 tmf->resp.len = sizeof(tmf->rsp);
1656 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1657 tmf->payload_len = sizeof(tmf->iu);
1658 tmf->resp_len = sizeof(tmf->rsp);
1659 tmf->cancel_key = (unsigned long)sdev->hostdata;
1660 tmf->tgt_scsi_id = rport->port_id;
1661 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1662 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1663 tmf->iu.tmf_flags = type;
1664 evt->sync_iu = &rsp_iu;
1665
1666 init_completion(&evt->comp);
1667 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1668 }
1669 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1670
1671 if (rsp_rc != 0) {
1672 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
1673 desc, rsp_rc);
1674 return -EIO;
1675 }
1676
1677 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
1678 wait_for_completion(&evt->comp);
1679
1680 if (rsp_iu.cmd.status) {
1681 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1682 rsp_code = fc_rsp->data.info.rsp_code;
1683
1684 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
1685 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1686 desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1687 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1688 fc_rsp->scsi_status);
1689 rsp_rc = -EIO;
1690 } else
1691 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
1692
1693 spin_lock_irqsave(vhost->host->host_lock, flags);
1694 ibmvfc_free_event(evt);
1695 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1696 return rsp_rc;
1697}
1698
1699/**
1700 * ibmvfc_abort_task_set - Abort outstanding commands to the device
1701 * @sdev: scsi device to abort commands
1702 *
1703 * This sends an Abort Task Set to the VIOS for the specified device. This does
1704 * NOT send any cancel to the VIOS. That must be done separately.
1705 *
1706 * Returns:
1707 * 0 on success / other on failure
1708 **/
1709static int ibmvfc_abort_task_set(struct scsi_device *sdev)
1710{
1711 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1712 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1713 struct ibmvfc_cmd *tmf;
1714 struct ibmvfc_event *evt, *found_evt;
1715 union ibmvfc_iu rsp_iu;
1716 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1717 int rsp_rc = -EBUSY;
1718 unsigned long flags;
1719 int rsp_code = 0;
1720
1721 spin_lock_irqsave(vhost->host->host_lock, flags);
1722 found_evt = NULL;
1723 list_for_each_entry(evt, &vhost->sent, queue) {
1724 if (evt->cmnd && evt->cmnd->device == sdev) {
1725 found_evt = evt;
1726 break;
1727 }
1728 }
1729
1730 if (!found_evt) {
1731 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1732 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
1733 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1734 return 0;
1735 }
1736
1737 if (vhost->state == IBMVFC_ACTIVE) {
1738 evt = ibmvfc_get_event(vhost);
1739 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1740
1741 tmf = &evt->iu.cmd;
1742 memset(tmf, 0, sizeof(*tmf));
1743 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1744 tmf->resp.len = sizeof(tmf->rsp);
1745 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1746 tmf->payload_len = sizeof(tmf->iu);
1747 tmf->resp_len = sizeof(tmf->rsp);
1748 tmf->cancel_key = (unsigned long)sdev->hostdata;
1749 tmf->tgt_scsi_id = rport->port_id;
1750 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1751 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1752 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
1753 evt->sync_iu = &rsp_iu;
1754
1755 init_completion(&evt->comp);
1756 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1757 }
1758
1759 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1760
1761 if (rsp_rc != 0) {
1762 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
1763 return -EIO;
1764 }
1765
1766 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
1767 wait_for_completion(&evt->comp);
1768
1769 if (rsp_iu.cmd.status) {
1770 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1771 rsp_code = fc_rsp->data.info.rsp_code;
1772
1773 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
1774 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1775 ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1776 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1777 fc_rsp->scsi_status);
1778 rsp_rc = -EIO;
1779 } else
1780 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
1781
1782 spin_lock_irqsave(vhost->host->host_lock, flags);
1783 ibmvfc_free_event(evt);
1784 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1785 return rsp_rc;
1786}
1787
1788/**
1789 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
1790 * @sdev: scsi device to cancel commands
1791 * @type: type of error recovery being performed
1792 *
1793 * This sends a cancel to the VIOS for the specified device. This does
1794 * NOT send any abort to the actual device. That must be done separately.
1795 *
1796 * Returns:
1797 * 0 on success / other on failure
1798 **/
1799static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
1800{
1801 struct ibmvfc_host *vhost = shost_priv(sdev->host);
ad8dcffa
BK
1802 struct scsi_target *starget = scsi_target(sdev);
1803 struct fc_rport *rport = starget_to_rport(starget);
072b91f9
BK
1804 struct ibmvfc_tmf *tmf;
1805 struct ibmvfc_event *evt, *found_evt;
1806 union ibmvfc_iu rsp;
1807 int rsp_rc = -EBUSY;
1808 unsigned long flags;
1809 u16 status;
1810
1811 ENTER;
1812 spin_lock_irqsave(vhost->host->host_lock, flags);
1813 found_evt = NULL;
1814 list_for_each_entry(evt, &vhost->sent, queue) {
1815 if (evt->cmnd && evt->cmnd->device == sdev) {
1816 found_evt = evt;
1817 break;
1818 }
1819 }
1820
1821 if (!found_evt) {
1822 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1823 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
1824 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1825 return 0;
1826 }
1827
1828 if (vhost->state == IBMVFC_ACTIVE) {
1829 evt = ibmvfc_get_event(vhost);
1830 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1831
1832 tmf = &evt->iu.tmf;
1833 memset(tmf, 0, sizeof(*tmf));
1834 tmf->common.version = 1;
1835 tmf->common.opcode = IBMVFC_TMF_MAD;
1836 tmf->common.length = sizeof(*tmf);
1837 tmf->scsi_id = rport->port_id;
1838 int_to_scsilun(sdev->lun, &tmf->lun);
1839 tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
1840 tmf->cancel_key = (unsigned long)sdev->hostdata;
ad8dcffa 1841 tmf->my_cancel_key = (unsigned long)starget->hostdata;
072b91f9
BK
1842
1843 evt->sync_iu = &rsp;
1844 init_completion(&evt->comp);
1845 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1846 }
1847
1848 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1849
1850 if (rsp_rc != 0) {
1851 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
1852 return -EIO;
1853 }
1854
1855 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
1856
1857 wait_for_completion(&evt->comp);
1858 status = rsp.mad_common.status;
1859 spin_lock_irqsave(vhost->host->host_lock, flags);
1860 ibmvfc_free_event(evt);
1861 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1862
1863 if (status != IBMVFC_MAD_SUCCESS) {
1864 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
1865 return -EIO;
1866 }
1867
1868 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
1869 return 0;
1870}
1871
ad8dcffa
BK
1872/**
1873 * ibmvfc_match_target - Match function for specified target
1874 * @evt: ibmvfc event struct
1875 * @device: device to match (starget)
1876 *
1877 * Returns:
1878 * 1 if event matches starget / 0 if event does not match starget
1879 **/
1880static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
1881{
1882 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
1883 return 1;
1884 return 0;
1885}
1886
1887/**
1888 * ibmvfc_match_lun - Match function for specified LUN
1889 * @evt: ibmvfc event struct
1890 * @device: device to match (sdev)
1891 *
1892 * Returns:
1893 * 1 if event matches sdev / 0 if event does not match sdev
1894 **/
1895static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
1896{
1897 if (evt->cmnd && evt->cmnd->device == device)
1898 return 1;
1899 return 0;
1900}
1901
1902/**
1903 * ibmvfc_wait_for_ops - Wait for ops to complete
1904 * @vhost: ibmvfc host struct
1905 * @device: device to match (starget or sdev)
1906 * @match: match function
1907 *
1908 * Returns:
1909 * SUCCESS / FAILED
1910 **/
1911static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
1912 int (*match) (struct ibmvfc_event *, void *))
1913{
1914 struct ibmvfc_event *evt;
1915 DECLARE_COMPLETION_ONSTACK(comp);
1916 int wait;
1917 unsigned long flags;
1918 signed long timeout = init_timeout * HZ;
1919
1920 ENTER;
1921 do {
1922 wait = 0;
1923 spin_lock_irqsave(vhost->host->host_lock, flags);
1924 list_for_each_entry(evt, &vhost->sent, queue) {
1925 if (match(evt, device)) {
1926 evt->eh_comp = &comp;
1927 wait++;
1928 }
1929 }
1930 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1931
1932 if (wait) {
1933 timeout = wait_for_completion_timeout(&comp, timeout);
1934
1935 if (!timeout) {
1936 wait = 0;
1937 spin_lock_irqsave(vhost->host->host_lock, flags);
1938 list_for_each_entry(evt, &vhost->sent, queue) {
1939 if (match(evt, device)) {
1940 evt->eh_comp = NULL;
1941 wait++;
1942 }
1943 }
1944 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1945 if (wait)
1946 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
1947 LEAVE;
1948 return wait ? FAILED : SUCCESS;
1949 }
1950 }
1951 } while (wait);
1952
1953 LEAVE;
1954 return SUCCESS;
1955}
1956
072b91f9
BK
1957/**
1958 * ibmvfc_eh_abort_handler - Abort a command
1959 * @cmd: scsi command to abort
1960 *
1961 * Returns:
1962 * SUCCESS / FAILED
1963 **/
1964static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
1965{
ad8dcffa
BK
1966 struct scsi_device *sdev = cmd->device;
1967 struct ibmvfc_host *vhost = shost_priv(sdev->host);
072b91f9 1968 int cancel_rc, abort_rc;
ad8dcffa 1969 int rc = FAILED;
072b91f9
BK
1970
1971 ENTER;
1972 ibmvfc_wait_while_resetting(vhost);
ad8dcffa
BK
1973 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
1974 abort_rc = ibmvfc_abort_task_set(sdev);
072b91f9 1975
ad8dcffa
BK
1976 if (!cancel_rc && !abort_rc)
1977 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
072b91f9
BK
1978
1979 LEAVE;
ad8dcffa 1980 return rc;
072b91f9
BK
1981}
1982
1983/**
1984 * ibmvfc_eh_device_reset_handler - Reset a single LUN
1985 * @cmd: scsi command struct
1986 *
1987 * Returns:
1988 * SUCCESS / FAILED
1989 **/
1990static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
1991{
ad8dcffa
BK
1992 struct scsi_device *sdev = cmd->device;
1993 struct ibmvfc_host *vhost = shost_priv(sdev->host);
072b91f9 1994 int cancel_rc, reset_rc;
ad8dcffa 1995 int rc = FAILED;
072b91f9
BK
1996
1997 ENTER;
1998 ibmvfc_wait_while_resetting(vhost);
ad8dcffa
BK
1999 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2000 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
072b91f9 2001
ad8dcffa
BK
2002 if (!cancel_rc && !reset_rc)
2003 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
072b91f9
BK
2004
2005 LEAVE;
ad8dcffa 2006 return rc;
072b91f9
BK
2007}
2008
2009/**
2010 * ibmvfc_dev_cancel_all - Device iterated cancel all function
2011 * @sdev: scsi device struct
2012 * @data: return code
2013 *
2014 **/
2015static void ibmvfc_dev_cancel_all(struct scsi_device *sdev, void *data)
2016{
2017 unsigned long *rc = data;
2018 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2019}
2020
2021/**
2022 * ibmvfc_dev_abort_all - Device iterated abort task set function
2023 * @sdev: scsi device struct
2024 * @data: return code
2025 *
2026 **/
2027static void ibmvfc_dev_abort_all(struct scsi_device *sdev, void *data)
2028{
2029 unsigned long *rc = data;
2030 *rc |= ibmvfc_abort_task_set(sdev);
2031}
2032
2033/**
2034 * ibmvfc_eh_target_reset_handler - Reset the target
2035 * @cmd: scsi command struct
2036 *
2037 * Returns:
2038 * SUCCESS / FAILED
2039 **/
2040static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2041{
ad8dcffa
BK
2042 struct scsi_device *sdev = cmd->device;
2043 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2044 struct scsi_target *starget = scsi_target(sdev);
072b91f9 2045 int reset_rc;
ad8dcffa 2046 int rc = FAILED;
072b91f9 2047 unsigned long cancel_rc = 0;
072b91f9
BK
2048
2049 ENTER;
2050 ibmvfc_wait_while_resetting(vhost);
2051 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
ad8dcffa 2052 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
072b91f9 2053
ad8dcffa
BK
2054 if (!cancel_rc && !reset_rc)
2055 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
072b91f9
BK
2056
2057 LEAVE;
ad8dcffa 2058 return rc;
072b91f9
BK
2059}
2060
2061/**
2062 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2063 * @cmd: struct scsi_cmnd having problems
2064 *
2065 **/
2066static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2067{
2068 int rc;
2069 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2070
2071 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2072 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2073 return rc ? FAILED : SUCCESS;
2074}
2075
2076/**
2077 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2078 * @rport: rport struct
2079 *
2080 * Return value:
2081 * none
2082 **/
2083static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2084{
2085 struct scsi_target *starget = to_scsi_target(&rport->dev);
2086 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2087 struct ibmvfc_host *vhost = shost_priv(shost);
072b91f9
BK
2088 unsigned long cancel_rc = 0;
2089 unsigned long abort_rc = 0;
ad8dcffa 2090 int rc = FAILED;
072b91f9
BK
2091
2092 ENTER;
2093 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2094 starget_for_each_device(starget, &abort_rc, ibmvfc_dev_abort_all);
2095
ad8dcffa
BK
2096 if (!cancel_rc && !abort_rc)
2097 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2098
2099 if (rc == FAILED)
072b91f9 2100 ibmvfc_issue_fc_host_lip(shost);
072b91f9
BK
2101 LEAVE;
2102}
2103
2104static const struct {
2105 enum ibmvfc_async_event ae;
2106 const char *desc;
2107} ae_desc [] = {
2108 { IBMVFC_AE_ELS_PLOGI, "PLOGI" },
2109 { IBMVFC_AE_ELS_LOGO, "LOGO" },
2110 { IBMVFC_AE_ELS_PRLO, "PRLO" },
2111 { IBMVFC_AE_SCN_NPORT, "N-Port SCN" },
2112 { IBMVFC_AE_SCN_GROUP, "Group SCN" },
2113 { IBMVFC_AE_SCN_DOMAIN, "Domain SCN" },
2114 { IBMVFC_AE_SCN_FABRIC, "Fabric SCN" },
2115 { IBMVFC_AE_LINK_UP, "Link Up" },
2116 { IBMVFC_AE_LINK_DOWN, "Link Down" },
2117 { IBMVFC_AE_LINK_DEAD, "Link Dead" },
2118 { IBMVFC_AE_HALT, "Halt" },
2119 { IBMVFC_AE_RESUME, "Resume" },
2120 { IBMVFC_AE_ADAPTER_FAILED, "Adapter Failed" },
2121};
2122
2123static const char *unknown_ae = "Unknown async";
2124
2125/**
2126 * ibmvfc_get_ae_desc - Get text description for async event
2127 * @ae: async event
2128 *
2129 **/
2130static const char *ibmvfc_get_ae_desc(u64 ae)
2131{
2132 int i;
2133
2134 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2135 if (ae_desc[i].ae == ae)
2136 return ae_desc[i].desc;
2137
2138 return unknown_ae;
2139}
2140
2141/**
2142 * ibmvfc_handle_async - Handle an async event from the adapter
2143 * @crq: crq to process
2144 * @vhost: ibmvfc host struct
2145 *
2146 **/
2147static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2148 struct ibmvfc_host *vhost)
2149{
2150 const char *desc = ibmvfc_get_ae_desc(crq->event);
2151
3e339946
BK
2152 ibmvfc_log(vhost, 3, "%s event received. scsi_id: %lx, wwpn: %lx,"
2153 " node_name: %lx\n", desc, crq->scsi_id, crq->wwpn, crq->node_name);
072b91f9
BK
2154
2155 switch (crq->event) {
2156 case IBMVFC_AE_LINK_UP:
2157 case IBMVFC_AE_RESUME:
2158 vhost->events_to_log |= IBMVFC_AE_LINKUP;
d2131b33
BK
2159 vhost->delay_init = 1;
2160 __ibmvfc_reset_host(vhost);
072b91f9
BK
2161 break;
2162 case IBMVFC_AE_SCN_FABRIC:
d2131b33 2163 case IBMVFC_AE_SCN_DOMAIN:
072b91f9 2164 vhost->events_to_log |= IBMVFC_AE_RSCN;
d2131b33
BK
2165 vhost->delay_init = 1;
2166 __ibmvfc_reset_host(vhost);
072b91f9
BK
2167 break;
2168 case IBMVFC_AE_SCN_NPORT:
2169 case IBMVFC_AE_SCN_GROUP:
072b91f9
BK
2170 vhost->events_to_log |= IBMVFC_AE_RSCN;
2171 case IBMVFC_AE_ELS_LOGO:
2172 case IBMVFC_AE_ELS_PRLO:
2173 case IBMVFC_AE_ELS_PLOGI:
2174 ibmvfc_reinit_host(vhost);
2175 break;
2176 case IBMVFC_AE_LINK_DOWN:
2177 case IBMVFC_AE_ADAPTER_FAILED:
2178 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2179 break;
2180 case IBMVFC_AE_LINK_DEAD:
2181 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2182 break;
2183 case IBMVFC_AE_HALT:
2184 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2185 break;
2186 default:
2187 dev_err(vhost->dev, "Unknown async event received: %ld\n", crq->event);
2188 break;
2189 };
2190}
2191
2192/**
2193 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2194 * @crq: Command/Response queue
2195 * @vhost: ibmvfc host struct
2196 *
2197 **/
2198static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2199{
2200 long rc;
2201 struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2202
2203 switch (crq->valid) {
2204 case IBMVFC_CRQ_INIT_RSP:
2205 switch (crq->format) {
2206 case IBMVFC_CRQ_INIT:
2207 dev_info(vhost->dev, "Partner initialized\n");
2208 /* Send back a response */
2209 rc = ibmvfc_send_crq_init_complete(vhost);
2210 if (rc == 0)
cf6f10d7 2211 ibmvfc_init_host(vhost, 0);
072b91f9
BK
2212 else
2213 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2214 break;
2215 case IBMVFC_CRQ_INIT_COMPLETE:
2216 dev_info(vhost->dev, "Partner initialization complete\n");
cf6f10d7 2217 ibmvfc_init_host(vhost, 0);
072b91f9
BK
2218 break;
2219 default:
2220 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2221 }
2222 return;
2223 case IBMVFC_CRQ_XPORT_EVENT:
2224 vhost->state = IBMVFC_NO_CRQ;
2225 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2226 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2227 /* We need to re-setup the interpartition connection */
2228 dev_info(vhost->dev, "Re-enabling adapter\n");
2229 vhost->client_migrated = 1;
2230 ibmvfc_purge_requests(vhost, DID_REQUEUE);
2231 if ((rc = ibmvfc_reenable_crq_queue(vhost)) ||
2232 (rc = ibmvfc_send_crq_init(vhost))) {
2233 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2234 dev_err(vhost->dev, "Error after enable (rc=%ld)\n", rc);
2235 } else
2236 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2237 } else {
2238 dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2239
2240 ibmvfc_purge_requests(vhost, DID_ERROR);
2241 if ((rc = ibmvfc_reset_crq(vhost)) ||
2242 (rc = ibmvfc_send_crq_init(vhost))) {
2243 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2244 dev_err(vhost->dev, "Error after reset (rc=%ld)\n", rc);
2245 } else
2246 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2247 }
2248 return;
2249 case IBMVFC_CRQ_CMD_RSP:
2250 break;
2251 default:
2252 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2253 return;
2254 }
2255
2256 if (crq->format == IBMVFC_ASYNC_EVENT)
2257 return;
2258
2259 /* The only kind of payload CRQs we should get are responses to
2260 * things we send. Make sure this response is to something we
2261 * actually sent
2262 */
2263 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2264 dev_err(vhost->dev, "Returned correlation_token 0x%08lx is invalid!\n",
2265 crq->ioba);
2266 return;
2267 }
2268
2269 if (unlikely(atomic_read(&evt->free))) {
2270 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08lx!\n",
2271 crq->ioba);
2272 return;
2273 }
2274
2275 del_timer(&evt->timer);
2276 list_del(&evt->queue);
2277 ibmvfc_trc_end(evt);
2278 evt->done(evt);
2279}
2280
2281/**
2282 * ibmvfc_scan_finished - Check if the device scan is done.
2283 * @shost: scsi host struct
2284 * @time: current elapsed time
2285 *
2286 * Returns:
2287 * 0 if scan is not done / 1 if scan is done
2288 **/
2289static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2290{
2291 unsigned long flags;
2292 struct ibmvfc_host *vhost = shost_priv(shost);
2293 int done = 0;
2294
2295 spin_lock_irqsave(shost->host_lock, flags);
2296 if (time >= (init_timeout * HZ)) {
2297 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2298 "continuing initialization\n", init_timeout);
2299 done = 1;
2300 }
2301
2302 if (vhost->state != IBMVFC_NO_CRQ && vhost->action == IBMVFC_HOST_ACTION_NONE)
2303 done = 1;
2304 spin_unlock_irqrestore(shost->host_lock, flags);
2305 return done;
2306}
2307
2308/**
2309 * ibmvfc_slave_alloc - Setup the device's task set value
2310 * @sdev: struct scsi_device device to configure
2311 *
2312 * Set the device's task set value so that error handling works as
2313 * expected.
2314 *
2315 * Returns:
2316 * 0 on success / -ENXIO if device does not exist
2317 **/
2318static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2319{
2320 struct Scsi_Host *shost = sdev->host;
2321 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2322 struct ibmvfc_host *vhost = shost_priv(shost);
2323 unsigned long flags = 0;
2324
2325 if (!rport || fc_remote_port_chkready(rport))
2326 return -ENXIO;
2327
2328 spin_lock_irqsave(shost->host_lock, flags);
2329 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2330 spin_unlock_irqrestore(shost->host_lock, flags);
2331 return 0;
2332}
2333
ad8dcffa
BK
2334/**
2335 * ibmvfc_target_alloc - Setup the target's task set value
2336 * @starget: struct scsi_target
2337 *
2338 * Set the target's task set value so that error handling works as
2339 * expected.
2340 *
2341 * Returns:
2342 * 0 on success / -ENXIO if device does not exist
2343 **/
2344static int ibmvfc_target_alloc(struct scsi_target *starget)
2345{
2346 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2347 struct ibmvfc_host *vhost = shost_priv(shost);
2348 unsigned long flags = 0;
2349
2350 spin_lock_irqsave(shost->host_lock, flags);
2351 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2352 spin_unlock_irqrestore(shost->host_lock, flags);
2353 return 0;
2354}
2355
072b91f9
BK
2356/**
2357 * ibmvfc_slave_configure - Configure the device
2358 * @sdev: struct scsi_device device to configure
2359 *
2360 * Enable allow_restart for a device if it is a disk. Adjust the
2361 * queue_depth here also.
2362 *
2363 * Returns:
2364 * 0
2365 **/
2366static int ibmvfc_slave_configure(struct scsi_device *sdev)
2367{
2368 struct Scsi_Host *shost = sdev->host;
2369 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
2370 unsigned long flags = 0;
2371
2372 spin_lock_irqsave(shost->host_lock, flags);
2373 if (sdev->type == TYPE_DISK)
2374 sdev->allow_restart = 1;
2375
2376 if (sdev->tagged_supported) {
2377 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2378 scsi_activate_tcq(sdev, sdev->queue_depth);
2379 } else
2380 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2381
2382 rport->dev_loss_tmo = dev_loss_tmo;
2383 spin_unlock_irqrestore(shost->host_lock, flags);
2384 return 0;
2385}
2386
2387/**
2388 * ibmvfc_change_queue_depth - Change the device's queue depth
2389 * @sdev: scsi device struct
2390 * @qdepth: depth to set
2391 *
2392 * Return value:
2393 * actual depth set
2394 **/
2395static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2396{
2397 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2398 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2399
2400 scsi_adjust_queue_depth(sdev, 0, qdepth);
2401 return sdev->queue_depth;
2402}
2403
2404/**
2405 * ibmvfc_change_queue_type - Change the device's queue type
2406 * @sdev: scsi device struct
2407 * @tag_type: type of tags to use
2408 *
2409 * Return value:
2410 * actual queue type set
2411 **/
2412static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2413{
2414 if (sdev->tagged_supported) {
2415 scsi_set_tag_type(sdev, tag_type);
2416
2417 if (tag_type)
2418 scsi_activate_tcq(sdev, sdev->queue_depth);
2419 else
2420 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2421 } else
2422 tag_type = 0;
2423
2424 return tag_type;
2425}
2426
2427static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2428 struct device_attribute *attr, char *buf)
2429{
2430 struct Scsi_Host *shost = class_to_shost(dev);
2431 struct ibmvfc_host *vhost = shost_priv(shost);
2432
2433 return snprintf(buf, PAGE_SIZE, "%s\n",
2434 vhost->login_buf->resp.partition_name);
2435}
2436
2437static struct device_attribute ibmvfc_host_partition_name = {
2438 .attr = {
2439 .name = "partition_name",
2440 .mode = S_IRUGO,
2441 },
2442 .show = ibmvfc_show_host_partition_name,
2443};
2444
2445static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2446 struct device_attribute *attr, char *buf)
2447{
2448 struct Scsi_Host *shost = class_to_shost(dev);
2449 struct ibmvfc_host *vhost = shost_priv(shost);
2450
2451 return snprintf(buf, PAGE_SIZE, "%s\n",
2452 vhost->login_buf->resp.device_name);
2453}
2454
2455static struct device_attribute ibmvfc_host_device_name = {
2456 .attr = {
2457 .name = "device_name",
2458 .mode = S_IRUGO,
2459 },
2460 .show = ibmvfc_show_host_device_name,
2461};
2462
2463static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2464 struct device_attribute *attr, char *buf)
2465{
2466 struct Scsi_Host *shost = class_to_shost(dev);
2467 struct ibmvfc_host *vhost = shost_priv(shost);
2468
2469 return snprintf(buf, PAGE_SIZE, "%s\n",
2470 vhost->login_buf->resp.port_loc_code);
2471}
2472
2473static struct device_attribute ibmvfc_host_loc_code = {
2474 .attr = {
2475 .name = "port_loc_code",
2476 .mode = S_IRUGO,
2477 },
2478 .show = ibmvfc_show_host_loc_code,
2479};
2480
2481static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2482 struct device_attribute *attr, char *buf)
2483{
2484 struct Scsi_Host *shost = class_to_shost(dev);
2485 struct ibmvfc_host *vhost = shost_priv(shost);
2486
2487 return snprintf(buf, PAGE_SIZE, "%s\n",
2488 vhost->login_buf->resp.drc_name);
2489}
2490
2491static struct device_attribute ibmvfc_host_drc_name = {
2492 .attr = {
2493 .name = "drc_name",
2494 .mode = S_IRUGO,
2495 },
2496 .show = ibmvfc_show_host_drc_name,
2497};
2498
2499static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2500 struct device_attribute *attr, char *buf)
2501{
2502 struct Scsi_Host *shost = class_to_shost(dev);
2503 struct ibmvfc_host *vhost = shost_priv(shost);
2504 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2505}
2506
2507static struct device_attribute ibmvfc_host_npiv_version = {
2508 .attr = {
2509 .name = "npiv_version",
2510 .mode = S_IRUGO,
2511 },
2512 .show = ibmvfc_show_host_npiv_version,
2513};
2514
2515/**
2516 * ibmvfc_show_log_level - Show the adapter's error logging level
2517 * @dev: class device struct
2518 * @buf: buffer
2519 *
2520 * Return value:
2521 * number of bytes printed to buffer
2522 **/
2523static ssize_t ibmvfc_show_log_level(struct device *dev,
2524 struct device_attribute *attr, char *buf)
2525{
2526 struct Scsi_Host *shost = class_to_shost(dev);
2527 struct ibmvfc_host *vhost = shost_priv(shost);
2528 unsigned long flags = 0;
2529 int len;
2530
2531 spin_lock_irqsave(shost->host_lock, flags);
2532 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2533 spin_unlock_irqrestore(shost->host_lock, flags);
2534 return len;
2535}
2536
2537/**
2538 * ibmvfc_store_log_level - Change the adapter's error logging level
2539 * @dev: class device struct
2540 * @buf: buffer
2541 *
2542 * Return value:
2543 * number of bytes printed to buffer
2544 **/
2545static ssize_t ibmvfc_store_log_level(struct device *dev,
2546 struct device_attribute *attr,
2547 const char *buf, size_t count)
2548{
2549 struct Scsi_Host *shost = class_to_shost(dev);
2550 struct ibmvfc_host *vhost = shost_priv(shost);
2551 unsigned long flags = 0;
2552
2553 spin_lock_irqsave(shost->host_lock, flags);
2554 vhost->log_level = simple_strtoul(buf, NULL, 10);
2555 spin_unlock_irqrestore(shost->host_lock, flags);
2556 return strlen(buf);
2557}
2558
2559static struct device_attribute ibmvfc_log_level_attr = {
2560 .attr = {
2561 .name = "log_level",
2562 .mode = S_IRUGO | S_IWUSR,
2563 },
2564 .show = ibmvfc_show_log_level,
2565 .store = ibmvfc_store_log_level
2566};
2567
2568#ifdef CONFIG_SCSI_IBMVFC_TRACE
2569/**
2570 * ibmvfc_read_trace - Dump the adapter trace
2571 * @kobj: kobject struct
2572 * @bin_attr: bin_attribute struct
2573 * @buf: buffer
2574 * @off: offset
2575 * @count: buffer size
2576 *
2577 * Return value:
2578 * number of bytes printed to buffer
2579 **/
2580static ssize_t ibmvfc_read_trace(struct kobject *kobj,
2581 struct bin_attribute *bin_attr,
2582 char *buf, loff_t off, size_t count)
2583{
2584 struct device *dev = container_of(kobj, struct device, kobj);
2585 struct Scsi_Host *shost = class_to_shost(dev);
2586 struct ibmvfc_host *vhost = shost_priv(shost);
2587 unsigned long flags = 0;
2588 int size = IBMVFC_TRACE_SIZE;
2589 char *src = (char *)vhost->trace;
2590
2591 if (off > size)
2592 return 0;
2593 if (off + count > size) {
2594 size -= off;
2595 count = size;
2596 }
2597
2598 spin_lock_irqsave(shost->host_lock, flags);
2599 memcpy(buf, &src[off], count);
2600 spin_unlock_irqrestore(shost->host_lock, flags);
2601 return count;
2602}
2603
2604static struct bin_attribute ibmvfc_trace_attr = {
2605 .attr = {
2606 .name = "trace",
2607 .mode = S_IRUGO,
2608 },
2609 .size = 0,
2610 .read = ibmvfc_read_trace,
2611};
2612#endif
2613
2614static struct device_attribute *ibmvfc_attrs[] = {
2615 &ibmvfc_host_partition_name,
2616 &ibmvfc_host_device_name,
2617 &ibmvfc_host_loc_code,
2618 &ibmvfc_host_drc_name,
2619 &ibmvfc_host_npiv_version,
2620 &ibmvfc_log_level_attr,
2621 NULL
2622};
2623
2624static struct scsi_host_template driver_template = {
2625 .module = THIS_MODULE,
2626 .name = "IBM POWER Virtual FC Adapter",
2627 .proc_name = IBMVFC_NAME,
2628 .queuecommand = ibmvfc_queuecommand,
2629 .eh_abort_handler = ibmvfc_eh_abort_handler,
2630 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
2631 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
2632 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
2633 .slave_alloc = ibmvfc_slave_alloc,
2634 .slave_configure = ibmvfc_slave_configure,
ad8dcffa 2635 .target_alloc = ibmvfc_target_alloc,
072b91f9
BK
2636 .scan_finished = ibmvfc_scan_finished,
2637 .change_queue_depth = ibmvfc_change_queue_depth,
2638 .change_queue_type = ibmvfc_change_queue_type,
2639 .cmd_per_lun = 16,
2640 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
2641 .this_id = -1,
2642 .sg_tablesize = SG_ALL,
2643 .max_sectors = IBMVFC_MAX_SECTORS,
2644 .use_clustering = ENABLE_CLUSTERING,
2645 .shost_attrs = ibmvfc_attrs,
2646};
2647
2648/**
2649 * ibmvfc_next_async_crq - Returns the next entry in async queue
2650 * @vhost: ibmvfc host struct
2651 *
2652 * Returns:
2653 * Pointer to next entry in queue / NULL if empty
2654 **/
2655static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
2656{
2657 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
2658 struct ibmvfc_async_crq *crq;
2659
2660 crq = &async_crq->msgs[async_crq->cur];
2661 if (crq->valid & 0x80) {
2662 if (++async_crq->cur == async_crq->size)
2663 async_crq->cur = 0;
2664 } else
2665 crq = NULL;
2666
2667 return crq;
2668}
2669
2670/**
2671 * ibmvfc_next_crq - Returns the next entry in message queue
2672 * @vhost: ibmvfc host struct
2673 *
2674 * Returns:
2675 * Pointer to next entry in queue / NULL if empty
2676 **/
2677static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
2678{
2679 struct ibmvfc_crq_queue *queue = &vhost->crq;
2680 struct ibmvfc_crq *crq;
2681
2682 crq = &queue->msgs[queue->cur];
2683 if (crq->valid & 0x80) {
2684 if (++queue->cur == queue->size)
2685 queue->cur = 0;
2686 } else
2687 crq = NULL;
2688
2689 return crq;
2690}
2691
2692/**
2693 * ibmvfc_interrupt - Interrupt handler
2694 * @irq: number of irq to handle, not used
2695 * @dev_instance: ibmvfc_host that received interrupt
2696 *
2697 * Returns:
2698 * IRQ_HANDLED
2699 **/
2700static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
2701{
2702 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
2703 struct vio_dev *vdev = to_vio_dev(vhost->dev);
2704 struct ibmvfc_crq *crq;
2705 struct ibmvfc_async_crq *async;
2706 unsigned long flags;
2707 int done = 0;
2708
2709 spin_lock_irqsave(vhost->host->host_lock, flags);
2710 vio_disable_interrupts(to_vio_dev(vhost->dev));
2711 while (!done) {
2712 /* Pull all the valid messages off the CRQ */
2713 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2714 ibmvfc_handle_crq(crq, vhost);
2715 crq->valid = 0;
2716 }
2717
2718 /* Pull all the valid messages off the async CRQ */
2719 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2720 ibmvfc_handle_async(async, vhost);
2721 async->valid = 0;
2722 }
2723
2724 vio_enable_interrupts(vdev);
2725 if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2726 vio_disable_interrupts(vdev);
2727 ibmvfc_handle_crq(crq, vhost);
2728 crq->valid = 0;
2729 } else if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2730 vio_disable_interrupts(vdev);
2731 ibmvfc_handle_async(async, vhost);
4081b77c 2732 async->valid = 0;
072b91f9
BK
2733 } else
2734 done = 1;
2735 }
2736
2737 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2738 return IRQ_HANDLED;
2739}
2740
2741/**
2742 * ibmvfc_init_tgt - Set the next init job step for the target
2743 * @tgt: ibmvfc target struct
2744 * @job_step: job step to perform
2745 *
2746 **/
2747static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
2748 void (*job_step) (struct ibmvfc_target *))
2749{
2750 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
2751 tgt->job_step = job_step;
2752 wake_up(&tgt->vhost->work_wait_q);
2753}
2754
2755/**
2756 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
2757 * @tgt: ibmvfc target struct
2758 * @job_step: initialization job step
2759 *
2760 **/
2761static void ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
2762 void (*job_step) (struct ibmvfc_target *))
2763{
1c41fa82 2764 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
072b91f9
BK
2765 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2766 wake_up(&tgt->vhost->work_wait_q);
2767 } else
2768 ibmvfc_init_tgt(tgt, job_step);
2769}
2770
072b91f9
BK
2771/**
2772 * ibmvfc_tgt_prli_done - Completion handler for Process Login
2773 * @evt: ibmvfc event struct
2774 *
2775 **/
2776static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
2777{
2778 struct ibmvfc_target *tgt = evt->tgt;
2779 struct ibmvfc_host *vhost = evt->vhost;
2780 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
2781 u32 status = rsp->common.status;
2782
2783 vhost->discovery_threads--;
2784 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2785 switch (status) {
2786 case IBMVFC_MAD_SUCCESS:
2787 tgt_dbg(tgt, "Process Login succeeded\n");
2788 tgt->need_login = 0;
2789 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_ADD_RPORT);
2790 break;
2791 case IBMVFC_MAD_DRIVER_FAILED:
2792 break;
2793 case IBMVFC_MAD_CRQ_ERROR:
2794 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2795 break;
2796 case IBMVFC_MAD_FAILED:
2797 default:
2798 tgt_err(tgt, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
2799 ibmvfc_get_cmd_error(rsp->status, rsp->error),
2800 rsp->status, rsp->error, status);
2801 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2802 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
10e79499
BK
2803 else
2804 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
072b91f9
BK
2805 break;
2806 };
2807
2808 kref_put(&tgt->kref, ibmvfc_release_tgt);
2809 ibmvfc_free_event(evt);
2810 wake_up(&vhost->work_wait_q);
2811}
2812
2813/**
2814 * ibmvfc_tgt_send_prli - Send a process login
2815 * @tgt: ibmvfc target struct
2816 *
2817 **/
2818static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
2819{
2820 struct ibmvfc_process_login *prli;
2821 struct ibmvfc_host *vhost = tgt->vhost;
2822 struct ibmvfc_event *evt;
2823
2824 if (vhost->discovery_threads >= disc_threads)
2825 return;
2826
2827 kref_get(&tgt->kref);
2828 evt = ibmvfc_get_event(vhost);
2829 vhost->discovery_threads++;
2830 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
2831 evt->tgt = tgt;
2832 prli = &evt->iu.prli;
2833 memset(prli, 0, sizeof(*prli));
2834 prli->common.version = 1;
2835 prli->common.opcode = IBMVFC_PROCESS_LOGIN;
2836 prli->common.length = sizeof(*prli);
2837 prli->scsi_id = tgt->scsi_id;
2838
2839 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
2840 prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
2841 prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
2842
2843 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2844 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2845 vhost->discovery_threads--;
2846 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2847 kref_put(&tgt->kref, ibmvfc_release_tgt);
2848 } else
2849 tgt_dbg(tgt, "Sent process login\n");
2850}
2851
2852/**
2853 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
2854 * @evt: ibmvfc event struct
2855 *
2856 **/
2857static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
2858{
2859 struct ibmvfc_target *tgt = evt->tgt;
2860 struct ibmvfc_host *vhost = evt->vhost;
2861 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
2862 u32 status = rsp->common.status;
2863
2864 vhost->discovery_threads--;
2865 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2866 switch (status) {
2867 case IBMVFC_MAD_SUCCESS:
2868 tgt_dbg(tgt, "Port Login succeeded\n");
2869 if (tgt->ids.port_name &&
2870 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
2871 vhost->reinit = 1;
2872 tgt_dbg(tgt, "Port re-init required\n");
2873 break;
2874 }
2875 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
2876 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
2877 tgt->ids.port_id = tgt->scsi_id;
2878 tgt->ids.roles = FC_PORT_ROLE_FCP_TARGET;
2879 memcpy(&tgt->service_parms, &rsp->service_parms,
2880 sizeof(tgt->service_parms));
2881 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
2882 sizeof(tgt->service_parms_change));
2883 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
2884 break;
2885 case IBMVFC_MAD_DRIVER_FAILED:
2886 break;
2887 case IBMVFC_MAD_CRQ_ERROR:
2888 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2889 break;
2890 case IBMVFC_MAD_FAILED:
2891 default:
2892 tgt_err(tgt, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2893 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
2894 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
2895 ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
2896
2897 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2898 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
10e79499
BK
2899 else
2900 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
072b91f9
BK
2901 break;
2902 };
2903
2904 kref_put(&tgt->kref, ibmvfc_release_tgt);
2905 ibmvfc_free_event(evt);
2906 wake_up(&vhost->work_wait_q);
2907}
2908
2909/**
2910 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
2911 * @tgt: ibmvfc target struct
2912 *
2913 **/
2914static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
2915{
2916 struct ibmvfc_port_login *plogi;
2917 struct ibmvfc_host *vhost = tgt->vhost;
2918 struct ibmvfc_event *evt;
2919
2920 if (vhost->discovery_threads >= disc_threads)
2921 return;
2922
2923 kref_get(&tgt->kref);
2924 evt = ibmvfc_get_event(vhost);
2925 vhost->discovery_threads++;
2926 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2927 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
2928 evt->tgt = tgt;
2929 plogi = &evt->iu.plogi;
2930 memset(plogi, 0, sizeof(*plogi));
2931 plogi->common.version = 1;
2932 plogi->common.opcode = IBMVFC_PORT_LOGIN;
2933 plogi->common.length = sizeof(*plogi);
2934 plogi->scsi_id = tgt->scsi_id;
2935
2936 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2937 vhost->discovery_threads--;
2938 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2939 kref_put(&tgt->kref, ibmvfc_release_tgt);
2940 } else
2941 tgt_dbg(tgt, "Sent port login\n");
2942}
2943
2944/**
2945 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
2946 * @evt: ibmvfc event struct
2947 *
2948 **/
2949static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
2950{
2951 struct ibmvfc_target *tgt = evt->tgt;
2952 struct ibmvfc_host *vhost = evt->vhost;
2953 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
2954 u32 status = rsp->common.status;
2955
2956 vhost->discovery_threads--;
2957 ibmvfc_free_event(evt);
2958 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2959
2960 switch (status) {
2961 case IBMVFC_MAD_SUCCESS:
2962 tgt_dbg(tgt, "Implicit Logout succeeded\n");
2963 break;
2964 case IBMVFC_MAD_DRIVER_FAILED:
2965 kref_put(&tgt->kref, ibmvfc_release_tgt);
2966 wake_up(&vhost->work_wait_q);
2967 return;
2968 case IBMVFC_MAD_FAILED:
2969 default:
2970 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
2971 break;
2972 };
2973
2974 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
2975 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
2976 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
2977 tgt->scsi_id != tgt->new_scsi_id)
2978 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2979 kref_put(&tgt->kref, ibmvfc_release_tgt);
2980 wake_up(&vhost->work_wait_q);
2981}
2982
2983/**
2984 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
2985 * @tgt: ibmvfc target struct
2986 *
2987 **/
2988static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
2989{
2990 struct ibmvfc_implicit_logout *mad;
2991 struct ibmvfc_host *vhost = tgt->vhost;
2992 struct ibmvfc_event *evt;
2993
2994 if (vhost->discovery_threads >= disc_threads)
2995 return;
2996
2997 kref_get(&tgt->kref);
2998 evt = ibmvfc_get_event(vhost);
2999 vhost->discovery_threads++;
3000 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3001 evt->tgt = tgt;
3002 mad = &evt->iu.implicit_logout;
3003 memset(mad, 0, sizeof(*mad));
3004 mad->common.version = 1;
3005 mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
3006 mad->common.length = sizeof(*mad);
3007 mad->old_scsi_id = tgt->scsi_id;
3008
3009 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3010 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3011 vhost->discovery_threads--;
3012 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3013 kref_put(&tgt->kref, ibmvfc_release_tgt);
3014 } else
3015 tgt_dbg(tgt, "Sent Implicit Logout\n");
3016}
3017
989b8545
BK
3018/**
3019 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3020 * @mad: ibmvfc passthru mad struct
3021 * @tgt: ibmvfc target struct
3022 *
3023 * Returns:
3024 * 1 if PLOGI needed / 0 if PLOGI not needed
3025 **/
3026static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3027 struct ibmvfc_target *tgt)
3028{
3029 if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3030 sizeof(tgt->ids.port_name)))
3031 return 1;
3032 if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3033 sizeof(tgt->ids.node_name)))
3034 return 1;
3035 if (mad->fc_iu.response[6] != tgt->scsi_id)
3036 return 1;
3037 return 0;
3038}
3039
3040/**
3041 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3042 * @evt: ibmvfc event struct
3043 *
3044 **/
3045static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3046{
3047 struct ibmvfc_target *tgt = evt->tgt;
3048 struct ibmvfc_host *vhost = evt->vhost;
3049 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3050 u32 status = mad->common.status;
3051 u8 fc_reason, fc_explain;
3052
3053 vhost->discovery_threads--;
3054 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3055
3056 switch (status) {
3057 case IBMVFC_MAD_SUCCESS:
3058 tgt_dbg(tgt, "ADISC succeeded\n");
3059 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3060 tgt->need_login = 1;
3061 break;
3062 case IBMVFC_MAD_DRIVER_FAILED:
3063 break;
3064 case IBMVFC_MAD_FAILED:
3065 default:
3066 tgt->need_login = 1;
3067 fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
3068 fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
3069 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3070 ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
3071 mad->iu.status, mad->iu.error,
3072 ibmvfc_get_fc_type(fc_reason), fc_reason,
3073 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3074 break;
3075 };
3076
3077 kref_put(&tgt->kref, ibmvfc_release_tgt);
3078 ibmvfc_free_event(evt);
3079 wake_up(&vhost->work_wait_q);
3080}
3081
3082/**
3083 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3084 * @evt: ibmvfc event struct
3085 *
3086 **/
3087static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3088{
3089 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3090
3091 memset(mad, 0, sizeof(*mad));
3092 mad->common.version = 1;
3093 mad->common.opcode = IBMVFC_PASSTHRU;
3094 mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
3095 mad->cmd_ioba.va = (u64)evt->crq.ioba +
3096 offsetof(struct ibmvfc_passthru_mad, iu);
3097 mad->cmd_ioba.len = sizeof(mad->iu);
3098 mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
3099 mad->iu.rsp_len = sizeof(mad->fc_iu.response);
3100 mad->iu.cmd.va = (u64)evt->crq.ioba +
3101 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3102 offsetof(struct ibmvfc_passthru_fc_iu, payload);
3103 mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
3104 mad->iu.rsp.va = (u64)evt->crq.ioba +
3105 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3106 offsetof(struct ibmvfc_passthru_fc_iu, response);
3107 mad->iu.rsp.len = sizeof(mad->fc_iu.response);
3108}
3109
3110/**
3111 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3112 * @tgt: ibmvfc target struct
3113 *
3114 **/
3115static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3116{
3117 struct ibmvfc_passthru_mad *mad;
3118 struct ibmvfc_host *vhost = tgt->vhost;
3119 struct ibmvfc_event *evt;
3120
3121 if (vhost->discovery_threads >= disc_threads)
3122 return;
3123
3124 kref_get(&tgt->kref);
3125 evt = ibmvfc_get_event(vhost);
3126 vhost->discovery_threads++;
3127 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3128 evt->tgt = tgt;
3129
3130 ibmvfc_init_passthru(evt);
3131 mad = &evt->iu.passthru;
3132 mad->iu.flags = IBMVFC_FC_ELS;
3133 mad->iu.scsi_id = tgt->scsi_id;
3134
3135 mad->fc_iu.payload[0] = IBMVFC_ADISC;
3136 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3137 sizeof(vhost->login_buf->resp.port_name));
3138 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3139 sizeof(vhost->login_buf->resp.node_name));
3140 mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
3141
3142 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3143 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3144 vhost->discovery_threads--;
3145 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3146 kref_put(&tgt->kref, ibmvfc_release_tgt);
3147 } else
3148 tgt_dbg(tgt, "Sent ADISC\n");
3149}
3150
072b91f9
BK
3151/**
3152 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3153 * @evt: ibmvfc event struct
3154 *
3155 **/
3156static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3157{
3158 struct ibmvfc_target *tgt = evt->tgt;
3159 struct ibmvfc_host *vhost = evt->vhost;
3160 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3161 u32 status = rsp->common.status;
3162
3163 vhost->discovery_threads--;
3164 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3165 switch (status) {
3166 case IBMVFC_MAD_SUCCESS:
3167 tgt_dbg(tgt, "Query Target succeeded\n");
3168 tgt->new_scsi_id = rsp->scsi_id;
3169 if (rsp->scsi_id != tgt->scsi_id)
3170 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
989b8545
BK
3171 else
3172 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
072b91f9
BK
3173 break;
3174 case IBMVFC_MAD_DRIVER_FAILED:
3175 break;
3176 case IBMVFC_MAD_CRQ_ERROR:
3177 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3178 break;
3179 case IBMVFC_MAD_FAILED:
3180 default:
3181 tgt_err(tgt, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3182 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3183 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3184 ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
3185
3186 if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3187 rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3188 rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
3189 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3190 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3191 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
10e79499
BK
3192 else
3193 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
072b91f9
BK
3194 break;
3195 };
3196
3197 kref_put(&tgt->kref, ibmvfc_release_tgt);
3198 ibmvfc_free_event(evt);
3199 wake_up(&vhost->work_wait_q);
3200}
3201
3202/**
3203 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3204 * @tgt: ibmvfc target struct
3205 *
3206 **/
3207static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3208{
3209 struct ibmvfc_query_tgt *query_tgt;
3210 struct ibmvfc_host *vhost = tgt->vhost;
3211 struct ibmvfc_event *evt;
3212
3213 if (vhost->discovery_threads >= disc_threads)
3214 return;
3215
3216 kref_get(&tgt->kref);
3217 evt = ibmvfc_get_event(vhost);
3218 vhost->discovery_threads++;
3219 evt->tgt = tgt;
3220 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3221 query_tgt = &evt->iu.query_tgt;
3222 memset(query_tgt, 0, sizeof(*query_tgt));
3223 query_tgt->common.version = 1;
3224 query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
3225 query_tgt->common.length = sizeof(*query_tgt);
3226 query_tgt->wwpn = tgt->ids.port_name;
3227
3228 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3229 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3230 vhost->discovery_threads--;
3231 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3232 kref_put(&tgt->kref, ibmvfc_release_tgt);
3233 } else
3234 tgt_dbg(tgt, "Sent Query Target\n");
3235}
3236
3237/**
3238 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3239 * @vhost: ibmvfc host struct
3240 * @scsi_id: SCSI ID to allocate target for
3241 *
3242 * Returns:
3243 * 0 on success / other on failure
3244 **/
3245static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3246{
3247 struct ibmvfc_target *tgt;
3248 unsigned long flags;
3249
3250 spin_lock_irqsave(vhost->host->host_lock, flags);
3251 list_for_each_entry(tgt, &vhost->targets, queue) {
3252 if (tgt->scsi_id == scsi_id) {
3253 if (tgt->need_login)
3254 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3255 goto unlock_out;
3256 }
3257 }
3258 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3259
3260 tgt = mempool_alloc(vhost->tgt_pool, GFP_KERNEL);
3261 if (!tgt) {
3262 dev_err(vhost->dev, "Target allocation failure for scsi id %08lx\n",
3263 scsi_id);
3264 return -ENOMEM;
3265 }
3266
3267 tgt->scsi_id = scsi_id;
3268 tgt->new_scsi_id = scsi_id;
3269 tgt->vhost = vhost;
3270 tgt->need_login = 1;
3271 kref_init(&tgt->kref);
3272 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3273 spin_lock_irqsave(vhost->host->host_lock, flags);
3274 list_add_tail(&tgt->queue, &vhost->targets);
3275
3276unlock_out:
3277 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3278 return 0;
3279}
3280
3281/**
3282 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3283 * @vhost: ibmvfc host struct
3284 *
3285 * Returns:
3286 * 0 on success / other on failure
3287 **/
3288static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3289{
3290 int i, rc;
3291
3292 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3293 rc = ibmvfc_alloc_target(vhost,
3294 vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3295
3296 return rc;
3297}
3298
3299/**
3300 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3301 * @evt: ibmvfc event struct
3302 *
3303 **/
3304static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3305{
3306 struct ibmvfc_host *vhost = evt->vhost;
3307 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3308 u32 mad_status = rsp->common.status;
3309
3310 switch (mad_status) {
3311 case IBMVFC_MAD_SUCCESS:
3312 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3313 vhost->num_targets = rsp->num_written;
3314 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3315 break;
3316 case IBMVFC_MAD_FAILED:
3317 dev_err(vhost->dev, "Discover Targets failed: %s (%x:%x)\n",
3318 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3319 ibmvfc_retry_host_init(vhost);
3320 break;
3321 case IBMVFC_MAD_DRIVER_FAILED:
3322 break;
3323 default:
3324 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3325 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3326 break;
3327 }
3328
3329 ibmvfc_free_event(evt);
3330 wake_up(&vhost->work_wait_q);
3331}
3332
3333/**
3334 * ibmvfc_discover_targets - Send Discover Targets MAD
3335 * @vhost: ibmvfc host struct
3336 *
3337 **/
3338static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3339{
3340 struct ibmvfc_discover_targets *mad;
3341 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3342
3343 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3344 mad = &evt->iu.discover_targets;
3345 memset(mad, 0, sizeof(*mad));
3346 mad->common.version = 1;
3347 mad->common.opcode = IBMVFC_DISC_TARGETS;
3348 mad->common.length = sizeof(*mad);
3349 mad->bufflen = vhost->disc_buf_sz;
3350 mad->buffer.va = vhost->disc_buf_dma;
3351 mad->buffer.len = vhost->disc_buf_sz;
3352 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3353
3354 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3355 ibmvfc_dbg(vhost, "Sent discover targets\n");
3356 else
3357 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3358}
3359
3360/**
3361 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3362 * @evt: ibmvfc event struct
3363 *
3364 **/
3365static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3366{
3367 struct ibmvfc_host *vhost = evt->vhost;
3368 u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3369 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3370 unsigned int npiv_max_sectors;
3371
3372 switch (mad_status) {
3373 case IBMVFC_MAD_SUCCESS:
3374 ibmvfc_free_event(evt);
3375 break;
3376 case IBMVFC_MAD_FAILED:
3377 dev_err(vhost->dev, "NPIV Login failed: %s (%x:%x)\n",
3378 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3379 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3380 ibmvfc_retry_host_init(vhost);
3381 else
3382 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3383 ibmvfc_free_event(evt);
3384 return;
3385 case IBMVFC_MAD_CRQ_ERROR:
3386 ibmvfc_retry_host_init(vhost);
3387 case IBMVFC_MAD_DRIVER_FAILED:
3388 ibmvfc_free_event(evt);
3389 return;
3390 default:
3391 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
3392 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3393 ibmvfc_free_event(evt);
3394 return;
3395 }
3396
3397 vhost->client_migrated = 0;
3398
3399 if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
3400 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
3401 rsp->flags);
3402 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3403 wake_up(&vhost->work_wait_q);
3404 return;
3405 }
3406
3407 if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
3408 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
3409 rsp->max_cmds);
3410 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3411 wake_up(&vhost->work_wait_q);
3412 return;
3413 }
3414
3415 npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
3416 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
3417 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
3418 rsp->drc_name, npiv_max_sectors);
3419
3420 fc_host_fabric_name(vhost->host) = rsp->node_name;
3421 fc_host_node_name(vhost->host) = rsp->node_name;
3422 fc_host_port_name(vhost->host) = rsp->port_name;
3423 fc_host_port_id(vhost->host) = rsp->scsi_id;
3424 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
3425 fc_host_supported_classes(vhost->host) = 0;
3426 if (rsp->service_parms.class1_parms[0] & 0x80000000)
3427 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
3428 if (rsp->service_parms.class2_parms[0] & 0x80000000)
3429 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
3430 if (rsp->service_parms.class3_parms[0] & 0x80000000)
3431 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
3432 fc_host_maxframe_size(vhost->host) =
3433 rsp->service_parms.common.bb_rcv_sz & 0x0fff;
3434
3435 vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
3436 vhost->host->max_sectors = npiv_max_sectors;
3437 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3438 wake_up(&vhost->work_wait_q);
3439}
3440
3441/**
3442 * ibmvfc_npiv_login - Sends NPIV login
3443 * @vhost: ibmvfc host struct
3444 *
3445 **/
3446static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
3447{
3448 struct ibmvfc_npiv_login_mad *mad;
3449 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3450
3451 ibmvfc_gather_partition_info(vhost);
3452 ibmvfc_set_login_info(vhost);
3453 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
3454
3455 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
3456 mad = &evt->iu.npiv_login;
3457 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
3458 mad->common.version = 1;
3459 mad->common.opcode = IBMVFC_NPIV_LOGIN;
3460 mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
3461 mad->buffer.va = vhost->login_buf_dma;
3462 mad->buffer.len = sizeof(*vhost->login_buf);
3463
072b91f9
BK
3464 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3465
3466 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3467 ibmvfc_dbg(vhost, "Sent NPIV login\n");
3468 else
3469 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3470};
3471
3472/**
3473 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
3474 * @vhost: ibmvfc host struct
3475 *
3476 * Returns:
3477 * 1 if work to do / 0 if not
3478 **/
3479static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
3480{
3481 struct ibmvfc_target *tgt;
3482
3483 list_for_each_entry(tgt, &vhost->targets, queue) {
3484 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
3485 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3486 return 1;
3487 }
3488
3489 return 0;
3490}
3491
3492/**
3493 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
3494 * @vhost: ibmvfc host struct
3495 *
3496 * Returns:
3497 * 1 if work to do / 0 if not
3498 **/
3499static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3500{
3501 struct ibmvfc_target *tgt;
3502
3503 if (kthread_should_stop())
3504 return 1;
3505 switch (vhost->action) {
3506 case IBMVFC_HOST_ACTION_NONE:
3507 case IBMVFC_HOST_ACTION_INIT_WAIT:
3508 return 0;
3509 case IBMVFC_HOST_ACTION_TGT_INIT:
3510 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3511 if (vhost->discovery_threads == disc_threads)
3512 return 0;
3513 list_for_each_entry(tgt, &vhost->targets, queue)
3514 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
3515 return 1;
3516 list_for_each_entry(tgt, &vhost->targets, queue)
3517 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3518 return 0;
3519 return 1;
3520 case IBMVFC_HOST_ACTION_INIT:
3521 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3522 case IBMVFC_HOST_ACTION_TGT_ADD:
3523 case IBMVFC_HOST_ACTION_TGT_DEL:
10e79499 3524 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
072b91f9
BK
3525 case IBMVFC_HOST_ACTION_QUERY:
3526 default:
3527 break;
3528 };
3529
3530 return 1;
3531}
3532
3533/**
3534 * ibmvfc_work_to_do - Is there task level work to do?
3535 * @vhost: ibmvfc host struct
3536 *
3537 * Returns:
3538 * 1 if work to do / 0 if not
3539 **/
3540static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3541{
3542 unsigned long flags;
3543 int rc;
3544
3545 spin_lock_irqsave(vhost->host->host_lock, flags);
3546 rc = __ibmvfc_work_to_do(vhost);
3547 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3548 return rc;
3549}
3550
3551/**
3552 * ibmvfc_log_ae - Log async events if necessary
3553 * @vhost: ibmvfc host struct
3554 * @events: events to log
3555 *
3556 **/
3557static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
3558{
3559 if (events & IBMVFC_AE_RSCN)
3560 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
3561 if ((events & IBMVFC_AE_LINKDOWN) &&
3562 vhost->state >= IBMVFC_HALTED)
3563 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
3564 if ((events & IBMVFC_AE_LINKUP) &&
3565 vhost->state == IBMVFC_INITIALIZING)
3566 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
3567}
3568
3569/**
3570 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
3571 * @tgt: ibmvfc target struct
3572 *
3573 **/
3574static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
3575{
3576 struct ibmvfc_host *vhost = tgt->vhost;
3577 struct fc_rport *rport;
3578 unsigned long flags;
3579
3580 tgt_dbg(tgt, "Adding rport\n");
3581 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
3582 spin_lock_irqsave(vhost->host->host_lock, flags);
3583 tgt->rport = rport;
3584 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3585 if (rport) {
3586 tgt_dbg(tgt, "rport add succeeded\n");
3587 rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
3588 rport->supported_classes = 0;
52d7e861 3589 tgt->target_id = rport->scsi_target_id;
072b91f9
BK
3590 if (tgt->service_parms.class1_parms[0] & 0x80000000)
3591 rport->supported_classes |= FC_COS_CLASS1;
3592 if (tgt->service_parms.class2_parms[0] & 0x80000000)
3593 rport->supported_classes |= FC_COS_CLASS2;
3594 if (tgt->service_parms.class3_parms[0] & 0x80000000)
3595 rport->supported_classes |= FC_COS_CLASS3;
3596 } else
3597 tgt_dbg(tgt, "rport add failed\n");
3598 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3599}
3600
3601/**
3602 * ibmvfc_do_work - Do task level work
3603 * @vhost: ibmvfc host struct
3604 *
3605 **/
3606static void ibmvfc_do_work(struct ibmvfc_host *vhost)
3607{
3608 struct ibmvfc_target *tgt;
3609 unsigned long flags;
3610 struct fc_rport *rport;
3611
3612 ibmvfc_log_ae(vhost, vhost->events_to_log);
3613 spin_lock_irqsave(vhost->host->host_lock, flags);
3614 vhost->events_to_log = 0;
3615 switch (vhost->action) {
3616 case IBMVFC_HOST_ACTION_NONE:
3617 case IBMVFC_HOST_ACTION_INIT_WAIT:
3618 break;
3619 case IBMVFC_HOST_ACTION_INIT:
3620 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
1c41fa82
BK
3621 if (vhost->delay_init) {
3622 vhost->delay_init = 0;
3623 spin_unlock_irqrestore(vhost->host->host_lock, flags);
d2131b33 3624 ssleep(15);
1c41fa82
BK
3625 return;
3626 } else
3627 vhost->job_step(vhost);
072b91f9
BK
3628 break;
3629 case IBMVFC_HOST_ACTION_QUERY:
3630 list_for_each_entry(tgt, &vhost->targets, queue)
3631 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
3632 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
3633 break;
3634 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3635 list_for_each_entry(tgt, &vhost->targets, queue) {
3636 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3637 tgt->job_step(tgt);
3638 break;
3639 }
3640 }
3641
3642 if (!ibmvfc_dev_init_to_do(vhost))
3643 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
3644 break;
3645 case IBMVFC_HOST_ACTION_TGT_DEL:
10e79499 3646 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
072b91f9
BK
3647 list_for_each_entry(tgt, &vhost->targets, queue) {
3648 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3649 tgt_dbg(tgt, "Deleting rport\n");
3650 rport = tgt->rport;
3651 tgt->rport = NULL;
3652 list_del(&tgt->queue);
3653 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3654 if (rport)
3655 fc_remote_port_delete(rport);
3656 kref_put(&tgt->kref, ibmvfc_release_tgt);
3657 return;
3658 }
3659 }
3660
3661 if (vhost->state == IBMVFC_INITIALIZING) {
10e79499
BK
3662 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
3663 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
3664 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_ADD);
3665 vhost->init_retries = 0;
3666 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3667 scsi_unblock_requests(vhost->host);
3668 return;
3669 } else {
3670 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
3671 vhost->job_step = ibmvfc_discover_targets;
3672 }
072b91f9
BK
3673 } else {
3674 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3675 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3676 scsi_unblock_requests(vhost->host);
3677 wake_up(&vhost->init_wait_q);
3678 return;
3679 }
3680 break;
3681 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3682 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
3683 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3684 ibmvfc_alloc_targets(vhost);
3685 spin_lock_irqsave(vhost->host->host_lock, flags);
3686 break;
3687 case IBMVFC_HOST_ACTION_TGT_INIT:
3688 list_for_each_entry(tgt, &vhost->targets, queue) {
3689 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3690 tgt->job_step(tgt);
3691 break;
3692 }
3693 }
3694
10e79499
BK
3695 if (!ibmvfc_dev_init_to_do(vhost))
3696 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
072b91f9
BK
3697 break;
3698 case IBMVFC_HOST_ACTION_TGT_ADD:
3699 list_for_each_entry(tgt, &vhost->targets, queue) {
3700 if (tgt->action == IBMVFC_TGT_ACTION_ADD_RPORT) {
3701 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3702 ibmvfc_tgt_add_rport(tgt);
3703 return;
072b91f9
BK
3704 }
3705 }
3706
915be024 3707 if (vhost->reinit && !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
072b91f9 3708 vhost->reinit = 0;
915be024 3709 scsi_block_requests(vhost->host);
072b91f9
BK
3710 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3711 } else {
3712 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3713 wake_up(&vhost->init_wait_q);
3714 }
3715 break;
3716 default:
3717 break;
3718 };
3719
3720 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3721}
3722
3723/**
3724 * ibmvfc_work - Do task level work
3725 * @data: ibmvfc host struct
3726 *
3727 * Returns:
3728 * zero
3729 **/
3730static int ibmvfc_work(void *data)
3731{
3732 struct ibmvfc_host *vhost = data;
3733 int rc;
3734
3735 set_user_nice(current, -20);
3736
3737 while (1) {
3738 rc = wait_event_interruptible(vhost->work_wait_q,
3739 ibmvfc_work_to_do(vhost));
3740
3741 BUG_ON(rc);
3742
3743 if (kthread_should_stop())
3744 break;
3745
3746 ibmvfc_do_work(vhost);
3747 }
3748
3749 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
3750 return 0;
3751}
3752
3753/**
3754 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
3755 * @vhost: ibmvfc host struct
3756 *
3757 * Allocates a page for messages, maps it for dma, and registers
3758 * the crq with the hypervisor.
3759 *
3760 * Return value:
3761 * zero on success / other on failure
3762 **/
3763static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
3764{
3765 int rc, retrc = -ENOMEM;
3766 struct device *dev = vhost->dev;
3767 struct vio_dev *vdev = to_vio_dev(dev);
3768 struct ibmvfc_crq_queue *crq = &vhost->crq;
3769
3770 ENTER;
3771 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
3772
3773 if (!crq->msgs)
3774 return -ENOMEM;
3775
3776 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3777 crq->msg_token = dma_map_single(dev, crq->msgs,
3778 PAGE_SIZE, DMA_BIDIRECTIONAL);
3779
8d8bb39b 3780 if (dma_mapping_error(dev, crq->msg_token))
072b91f9
BK
3781 goto map_failed;
3782
3783 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3784 crq->msg_token, PAGE_SIZE);
3785
3786 if (rc == H_RESOURCE)
3787 /* maybe kexecing and resource is busy. try a reset */
3788 retrc = rc = ibmvfc_reset_crq(vhost);
3789
3790 if (rc == H_CLOSED)
3791 dev_warn(dev, "Partner adapter not ready\n");
3792 else if (rc) {
3793 dev_warn(dev, "Error %d opening adapter\n", rc);
3794 goto reg_crq_failed;
3795 }
3796
3797 retrc = 0;
3798
3799 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
3800 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
3801 goto req_irq_failed;
3802 }
3803
3804 if ((rc = vio_enable_interrupts(vdev))) {
3805 dev_err(dev, "Error %d enabling interrupts\n", rc);
3806 goto req_irq_failed;
3807 }
3808
3809 crq->cur = 0;
3810 LEAVE;
3811 return retrc;
3812
3813req_irq_failed:
3814 do {
3815 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3816 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3817reg_crq_failed:
3818 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3819map_failed:
3820 free_page((unsigned long)crq->msgs);
3821 return retrc;
3822}
3823
3824/**
3825 * ibmvfc_free_mem - Free memory for vhost
3826 * @vhost: ibmvfc host struct
3827 *
3828 * Return value:
3829 * none
3830 **/
3831static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
3832{
3833 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3834
3835 ENTER;
3836 mempool_destroy(vhost->tgt_pool);
3837 kfree(vhost->trace);
3838 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
3839 vhost->disc_buf_dma);
3840 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
3841 vhost->login_buf, vhost->login_buf_dma);
3842 dma_pool_destroy(vhost->sg_pool);
3843 dma_unmap_single(vhost->dev, async_q->msg_token,
3844 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3845 free_page((unsigned long)async_q->msgs);
3846 LEAVE;
3847}
3848
3849/**
3850 * ibmvfc_alloc_mem - Allocate memory for vhost
3851 * @vhost: ibmvfc host struct
3852 *
3853 * Return value:
3854 * 0 on success / non-zero on failure
3855 **/
3856static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
3857{
3858 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3859 struct device *dev = vhost->dev;
3860
3861 ENTER;
3862 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
3863 if (!async_q->msgs) {
3864 dev_err(dev, "Couldn't allocate async queue.\n");
3865 goto nomem;
3866 }
3867
3868 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
3869 async_q->msg_token = dma_map_single(dev, async_q->msgs,
3870 async_q->size * sizeof(*async_q->msgs),
3871 DMA_BIDIRECTIONAL);
3872
8d8bb39b 3873 if (dma_mapping_error(dev, async_q->msg_token)) {
072b91f9
BK
3874 dev_err(dev, "Failed to map async queue\n");
3875 goto free_async_crq;
3876 }
3877
3878 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
3879 SG_ALL * sizeof(struct srp_direct_buf),
3880 sizeof(struct srp_direct_buf), 0);
3881
3882 if (!vhost->sg_pool) {
3883 dev_err(dev, "Failed to allocate sg pool\n");
3884 goto unmap_async_crq;
3885 }
3886
3887 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
3888 &vhost->login_buf_dma, GFP_KERNEL);
3889
3890 if (!vhost->login_buf) {
3891 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
3892 goto free_sg_pool;
3893 }
3894
3895 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
3896 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
3897 &vhost->disc_buf_dma, GFP_KERNEL);
3898
3899 if (!vhost->disc_buf) {
3900 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
3901 goto free_login_buffer;
3902 }
3903
3904 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
3905 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
3906
3907 if (!vhost->trace)
3908 goto free_disc_buffer;
3909
3910 vhost->tgt_pool = mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
3911 sizeof(struct ibmvfc_target));
3912
3913 if (!vhost->tgt_pool) {
3914 dev_err(dev, "Couldn't allocate target memory pool\n");
3915 goto free_trace;
3916 }
3917
3918 LEAVE;
3919 return 0;
3920
3921free_trace:
3922 kfree(vhost->trace);
3923free_disc_buffer:
3924 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
3925 vhost->disc_buf_dma);
3926free_login_buffer:
3927 dma_free_coherent(dev, sizeof(*vhost->login_buf),
3928 vhost->login_buf, vhost->login_buf_dma);
3929free_sg_pool:
3930 dma_pool_destroy(vhost->sg_pool);
3931unmap_async_crq:
3932 dma_unmap_single(dev, async_q->msg_token,
3933 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3934free_async_crq:
3935 free_page((unsigned long)async_q->msgs);
3936nomem:
3937 LEAVE;
3938 return -ENOMEM;
3939}
3940
3941/**
3942 * ibmvfc_probe - Adapter hot plug add entry point
3943 * @vdev: vio device struct
3944 * @id: vio device id struct
3945 *
3946 * Return value:
3947 * 0 on success / non-zero on failure
3948 **/
3949static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
3950{
3951 struct ibmvfc_host *vhost;
3952 struct Scsi_Host *shost;
3953 struct device *dev = &vdev->dev;
3954 int rc = -ENOMEM;
3955
3956 ENTER;
3957 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
3958 if (!shost) {
3959 dev_err(dev, "Couldn't allocate host data\n");
3960 goto out;
3961 }
3962
3963 shost->transportt = ibmvfc_transport_template;
3964 shost->can_queue = max_requests;
3965 shost->max_lun = max_lun;
3966 shost->max_id = max_targets;
3967 shost->max_sectors = IBMVFC_MAX_SECTORS;
3968 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
3969 shost->unique_id = shost->host_no;
3970
3971 vhost = shost_priv(shost);
3972 INIT_LIST_HEAD(&vhost->sent);
3973 INIT_LIST_HEAD(&vhost->free);
3974 INIT_LIST_HEAD(&vhost->targets);
3975 sprintf(vhost->name, IBMVFC_NAME);
3976 vhost->host = shost;
3977 vhost->dev = dev;
3978 vhost->partition_number = -1;
3979 vhost->log_level = log_level;
3980 strcpy(vhost->partition_name, "UNKNOWN");
3981 init_waitqueue_head(&vhost->work_wait_q);
3982 init_waitqueue_head(&vhost->init_wait_q);
3983
3984 if ((rc = ibmvfc_alloc_mem(vhost)))
3985 goto free_scsi_host;
3986
3987 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
3988 shost->host_no);
3989
3990 if (IS_ERR(vhost->work_thread)) {
3991 dev_err(dev, "Couldn't create kernel thread: %ld\n",
3992 PTR_ERR(vhost->work_thread));
3993 goto free_host_mem;
3994 }
3995
3996 if ((rc = ibmvfc_init_crq(vhost))) {
3997 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3998 goto kill_kthread;
3999 }
4000
4001 if ((rc = ibmvfc_init_event_pool(vhost))) {
4002 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4003 goto release_crq;
4004 }
4005
4006 if ((rc = scsi_add_host(shost, dev)))
4007 goto release_event_pool;
4008
4009 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4010 &ibmvfc_trace_attr))) {
4011 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4012 goto remove_shost;
4013 }
4014
4015 dev_set_drvdata(dev, vhost);
4016 spin_lock(&ibmvfc_driver_lock);
4017 list_add_tail(&vhost->queue, &ibmvfc_head);
4018 spin_unlock(&ibmvfc_driver_lock);
4019
4020 ibmvfc_send_crq_init(vhost);
4021 scsi_scan_host(shost);
4022 return 0;
4023
4024remove_shost:
4025 scsi_remove_host(shost);
4026release_event_pool:
4027 ibmvfc_free_event_pool(vhost);
4028release_crq:
4029 ibmvfc_release_crq_queue(vhost);
4030kill_kthread:
4031 kthread_stop(vhost->work_thread);
4032free_host_mem:
4033 ibmvfc_free_mem(vhost);
4034free_scsi_host:
4035 scsi_host_put(shost);
4036out:
4037 LEAVE;
4038 return rc;
4039}
4040
4041/**
4042 * ibmvfc_remove - Adapter hot plug remove entry point
4043 * @vdev: vio device struct
4044 *
4045 * Return value:
4046 * 0
4047 **/
4048static int ibmvfc_remove(struct vio_dev *vdev)
4049{
4050 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4051 unsigned long flags;
4052
4053 ENTER;
4054 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
2d0da2a4
BK
4055 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
4056 ibmvfc_wait_while_resetting(vhost);
4057 ibmvfc_release_crq_queue(vhost);
072b91f9
BK
4058 kthread_stop(vhost->work_thread);
4059 fc_remove_host(vhost->host);
4060 scsi_remove_host(vhost->host);
072b91f9
BK
4061
4062 spin_lock_irqsave(vhost->host->host_lock, flags);
4063 ibmvfc_purge_requests(vhost, DID_ERROR);
4064 ibmvfc_free_event_pool(vhost);
4065 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4066
4067 ibmvfc_free_mem(vhost);
4068 spin_lock(&ibmvfc_driver_lock);
4069 list_del(&vhost->queue);
4070 spin_unlock(&ibmvfc_driver_lock);
4071 scsi_host_put(vhost->host);
4072 LEAVE;
4073 return 0;
4074}
4075
39c1ffec
BK
4076/**
4077 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4078 * @vdev: vio device struct
4079 *
4080 * Return value:
4081 * Number of bytes the driver will need to DMA map at the same time in
4082 * order to perform well.
4083 */
4084static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4085{
4086 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4087 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4088}
4089
072b91f9
BK
4090static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
4091 {"fcp", "IBM,vfc-client"},
4092 { "", "" }
4093};
4094MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4095
4096static struct vio_driver ibmvfc_driver = {
4097 .id_table = ibmvfc_device_table,
4098 .probe = ibmvfc_probe,
4099 .remove = ibmvfc_remove,
39c1ffec 4100 .get_desired_dma = ibmvfc_get_desired_dma,
072b91f9
BK
4101 .driver = {
4102 .name = IBMVFC_NAME,
4103 .owner = THIS_MODULE,
4104 }
4105};
4106
4107static struct fc_function_template ibmvfc_transport_functions = {
4108 .show_host_fabric_name = 1,
4109 .show_host_node_name = 1,
4110 .show_host_port_name = 1,
4111 .show_host_supported_classes = 1,
4112 .show_host_port_type = 1,
4113 .show_host_port_id = 1,
4114
4115 .get_host_port_state = ibmvfc_get_host_port_state,
4116 .show_host_port_state = 1,
4117
4118 .get_host_speed = ibmvfc_get_host_speed,
4119 .show_host_speed = 1,
4120
4121 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4122 .terminate_rport_io = ibmvfc_terminate_rport_io,
4123
4124 .show_rport_maxframe_size = 1,
4125 .show_rport_supported_classes = 1,
4126
4127 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4128 .show_rport_dev_loss_tmo = 1,
4129
4130 .get_starget_node_name = ibmvfc_get_starget_node_name,
4131 .show_starget_node_name = 1,
4132
4133 .get_starget_port_name = ibmvfc_get_starget_port_name,
4134 .show_starget_port_name = 1,
4135
4136 .get_starget_port_id = ibmvfc_get_starget_port_id,
4137 .show_starget_port_id = 1,
4138};
4139
4140/**
4141 * ibmvfc_module_init - Initialize the ibmvfc module
4142 *
4143 * Return value:
4144 * 0 on success / other on failure
4145 **/
4146static int __init ibmvfc_module_init(void)
4147{
4148 int rc;
4149
4150 if (!firmware_has_feature(FW_FEATURE_VIO))
4151 return -ENODEV;
4152
4153 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4154 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4155
4156 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4157 if (!ibmvfc_transport_template)
4158 return -ENOMEM;
4159
4160 rc = vio_register_driver(&ibmvfc_driver);
4161 if (rc)
4162 fc_release_transport(ibmvfc_transport_template);
4163 return rc;
4164}
4165
4166/**
4167 * ibmvfc_module_exit - Teardown the ibmvfc module
4168 *
4169 * Return value:
4170 * nothing
4171 **/
4172static void __exit ibmvfc_module_exit(void)
4173{
4174 vio_unregister_driver(&ibmvfc_driver);
4175 fc_release_transport(ibmvfc_transport_template);
4176}
4177
4178module_init(ibmvfc_module_init);
4179module_exit(ibmvfc_module_exit);