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