PageRenderTime 63ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/ibmvscsi/ibmvfc.c

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