/drivers/scsi/ibmvscsi/ibmvfc.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 4976 lines · 3450 code · 564 blank · 962 comment · 502 complexity · ad5922a1a8e04238825d56a83cc050d6 MD5 · raw file

Large files are truncated click here to view the full file

  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/slab.h>
  31. #include <linux/of.h>
  32. #include <linux/pm.h>
  33. #include <linux/stringify.h>
  34. #include <asm/firmware.h>
  35. #include <asm/irq.h>
  36. #include <asm/vio.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_host.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_tcq.h>
  42. #include <scsi/scsi_transport_fc.h>
  43. #include <scsi/scsi_bsg_fc.h>
  44. #include "ibmvfc.h"
  45. static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
  46. static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
  47. static unsigned int max_lun = IBMVFC_MAX_LUN;
  48. static unsigned int max_targets = IBMVFC_MAX_TARGETS;
  49. static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
  50. static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
  51. static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
  52. static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
  53. static LIST_HEAD(ibmvfc_head);
  54. static DEFINE_SPINLOCK(ibmvfc_driver_lock);
  55. static struct scsi_transport_template *ibmvfc_transport_template;
  56. MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
  57. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  58. MODULE_LICENSE("GPL");
  59. MODULE_VERSION(IBMVFC_DRIVER_VERSION);
  60. module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
  61. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
  62. "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
  63. module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
  64. MODULE_PARM_DESC(default_timeout,
  65. "Default timeout in seconds for initialization and EH commands. "
  66. "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
  67. module_param_named(max_requests, max_requests, uint, S_IRUGO);
  68. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
  69. "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
  70. module_param_named(max_lun, max_lun, uint, S_IRUGO);
  71. MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
  72. "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
  73. module_param_named(max_targets, max_targets, uint, S_IRUGO);
  74. MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
  75. "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
  76. module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
  77. MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
  78. "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
  79. module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
  80. MODULE_PARM_DESC(debug, "Enable driver debug information. "
  81. "[Default=" __stringify(IBMVFC_DEBUG) "]");
  82. module_param_named(log_level, log_level, uint, 0);
  83. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
  84. "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
  85. static const struct {
  86. u16 status;
  87. u16 error;
  88. u8 result;
  89. u8 retry;
  90. int log;
  91. char *name;
  92. } cmd_status [] = {
  93. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
  94. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
  95. { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
  96. { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
  97. { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
  98. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
  99. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
  100. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
  101. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
  102. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
  103. { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
  104. { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
  105. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
  106. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
  107. { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
  108. { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
  109. { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
  110. { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
  111. { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
  112. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
  113. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
  114. { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
  115. { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
  116. { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
  117. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
  118. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
  119. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
  120. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
  121. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
  122. { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
  123. { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
  124. { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
  125. { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
  126. { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
  127. { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
  128. { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
  129. };
  130. static void ibmvfc_npiv_login(struct ibmvfc_host *);
  131. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
  132. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
  133. static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
  134. static void ibmvfc_npiv_logout(struct ibmvfc_host *);
  135. static const char *unknown_error = "unknown error";
  136. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  137. /**
  138. * ibmvfc_trc_start - Log a start trace entry
  139. * @evt: ibmvfc event struct
  140. *
  141. **/
  142. static void ibmvfc_trc_start(struct ibmvfc_event *evt)
  143. {
  144. struct ibmvfc_host *vhost = evt->vhost;
  145. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  146. struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
  147. struct ibmvfc_trace_entry *entry;
  148. entry = &vhost->trace[vhost->trace_index++];
  149. entry->evt = evt;
  150. entry->time = jiffies;
  151. entry->fmt = evt->crq.format;
  152. entry->type = IBMVFC_TRC_START;
  153. switch (entry->fmt) {
  154. case IBMVFC_CMD_FORMAT:
  155. entry->op_code = vfc_cmd->iu.cdb[0];
  156. entry->scsi_id = vfc_cmd->tgt_scsi_id;
  157. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  158. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  159. entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
  160. break;
  161. case IBMVFC_MAD_FORMAT:
  162. entry->op_code = mad->opcode;
  163. break;
  164. default:
  165. break;
  166. };
  167. }
  168. /**
  169. * ibmvfc_trc_end - Log an end trace entry
  170. * @evt: ibmvfc event struct
  171. *
  172. **/
  173. static void ibmvfc_trc_end(struct ibmvfc_event *evt)
  174. {
  175. struct ibmvfc_host *vhost = evt->vhost;
  176. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  177. struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
  178. struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
  179. entry->evt = evt;
  180. entry->time = jiffies;
  181. entry->fmt = evt->crq.format;
  182. entry->type = IBMVFC_TRC_END;
  183. switch (entry->fmt) {
  184. case IBMVFC_CMD_FORMAT:
  185. entry->op_code = vfc_cmd->iu.cdb[0];
  186. entry->scsi_id = vfc_cmd->tgt_scsi_id;
  187. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  188. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  189. entry->u.end.status = vfc_cmd->status;
  190. entry->u.end.error = vfc_cmd->error;
  191. entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
  192. entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
  193. entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
  194. break;
  195. case IBMVFC_MAD_FORMAT:
  196. entry->op_code = mad->opcode;
  197. entry->u.end.status = mad->status;
  198. break;
  199. default:
  200. break;
  201. };
  202. }
  203. #else
  204. #define ibmvfc_trc_start(evt) do { } while (0)
  205. #define ibmvfc_trc_end(evt) do { } while (0)
  206. #endif
  207. /**
  208. * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
  209. * @status: status / error class
  210. * @error: error
  211. *
  212. * Return value:
  213. * index into cmd_status / -EINVAL on failure
  214. **/
  215. static int ibmvfc_get_err_index(u16 status, u16 error)
  216. {
  217. int i;
  218. for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
  219. if ((cmd_status[i].status & status) == cmd_status[i].status &&
  220. cmd_status[i].error == error)
  221. return i;
  222. return -EINVAL;
  223. }
  224. /**
  225. * ibmvfc_get_cmd_error - Find the error description for the fcp response
  226. * @status: status / error class
  227. * @error: error
  228. *
  229. * Return value:
  230. * error description string
  231. **/
  232. static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
  233. {
  234. int rc = ibmvfc_get_err_index(status, error);
  235. if (rc >= 0)
  236. return cmd_status[rc].name;
  237. return unknown_error;
  238. }
  239. /**
  240. * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
  241. * @vfc_cmd: ibmvfc command struct
  242. *
  243. * Return value:
  244. * SCSI result value to return for completed command
  245. **/
  246. static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
  247. {
  248. int err;
  249. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  250. int fc_rsp_len = rsp->fcp_rsp_len;
  251. if ((rsp->flags & FCP_RSP_LEN_VALID) &&
  252. ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
  253. rsp->data.info.rsp_code))
  254. return DID_ERROR << 16;
  255. err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
  256. if (err >= 0)
  257. return rsp->scsi_status | (cmd_status[err].result << 16);
  258. return rsp->scsi_status | (DID_ERROR << 16);
  259. }
  260. /**
  261. * ibmvfc_retry_cmd - Determine if error status is retryable
  262. * @status: status / error class
  263. * @error: error
  264. *
  265. * Return value:
  266. * 1 if error should be retried / 0 if it should not
  267. **/
  268. static int ibmvfc_retry_cmd(u16 status, u16 error)
  269. {
  270. int rc = ibmvfc_get_err_index(status, error);
  271. if (rc >= 0)
  272. return cmd_status[rc].retry;
  273. return 1;
  274. }
  275. static const char *unknown_fc_explain = "unknown fc explain";
  276. static const struct {
  277. u16 fc_explain;
  278. char *name;
  279. } ls_explain [] = {
  280. { 0x00, "no additional explanation" },
  281. { 0x01, "service parameter error - options" },
  282. { 0x03, "service parameter error - initiator control" },
  283. { 0x05, "service parameter error - recipient control" },
  284. { 0x07, "service parameter error - received data field size" },
  285. { 0x09, "service parameter error - concurrent seq" },
  286. { 0x0B, "service parameter error - credit" },
  287. { 0x0D, "invalid N_Port/F_Port_Name" },
  288. { 0x0E, "invalid node/Fabric Name" },
  289. { 0x0F, "invalid common service parameters" },
  290. { 0x11, "invalid association header" },
  291. { 0x13, "association header required" },
  292. { 0x15, "invalid originator S_ID" },
  293. { 0x17, "invalid OX_ID-RX-ID combination" },
  294. { 0x19, "command (request) already in progress" },
  295. { 0x1E, "N_Port Login requested" },
  296. { 0x1F, "Invalid N_Port_ID" },
  297. };
  298. static const struct {
  299. u16 fc_explain;
  300. char *name;
  301. } gs_explain [] = {
  302. { 0x00, "no additional explanation" },
  303. { 0x01, "port identifier not registered" },
  304. { 0x02, "port name not registered" },
  305. { 0x03, "node name not registered" },
  306. { 0x04, "class of service not registered" },
  307. { 0x06, "initial process associator not registered" },
  308. { 0x07, "FC-4 TYPEs not registered" },
  309. { 0x08, "symbolic port name not registered" },
  310. { 0x09, "symbolic node name not registered" },
  311. { 0x0A, "port type not registered" },
  312. { 0xF0, "authorization exception" },
  313. { 0xF1, "authentication exception" },
  314. { 0xF2, "data base full" },
  315. { 0xF3, "data base empty" },
  316. { 0xF4, "processing request" },
  317. { 0xF5, "unable to verify connection" },
  318. { 0xF6, "devices not in a common zone" },
  319. };
  320. /**
  321. * ibmvfc_get_ls_explain - Return the FC Explain description text
  322. * @status: FC Explain status
  323. *
  324. * Returns:
  325. * error string
  326. **/
  327. static const char *ibmvfc_get_ls_explain(u16 status)
  328. {
  329. int i;
  330. for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
  331. if (ls_explain[i].fc_explain == status)
  332. return ls_explain[i].name;
  333. return unknown_fc_explain;
  334. }
  335. /**
  336. * ibmvfc_get_gs_explain - Return the FC Explain description text
  337. * @status: FC Explain status
  338. *
  339. * Returns:
  340. * error string
  341. **/
  342. static const char *ibmvfc_get_gs_explain(u16 status)
  343. {
  344. int i;
  345. for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
  346. if (gs_explain[i].fc_explain == status)
  347. return gs_explain[i].name;
  348. return unknown_fc_explain;
  349. }
  350. static const struct {
  351. enum ibmvfc_fc_type fc_type;
  352. char *name;
  353. } fc_type [] = {
  354. { IBMVFC_FABRIC_REJECT, "fabric reject" },
  355. { IBMVFC_PORT_REJECT, "port reject" },
  356. { IBMVFC_LS_REJECT, "ELS reject" },
  357. { IBMVFC_FABRIC_BUSY, "fabric busy" },
  358. { IBMVFC_PORT_BUSY, "port busy" },
  359. { IBMVFC_BASIC_REJECT, "basic reject" },
  360. };
  361. static const char *unknown_fc_type = "unknown fc type";
  362. /**
  363. * ibmvfc_get_fc_type - Return the FC Type description text
  364. * @status: FC Type error status
  365. *
  366. * Returns:
  367. * error string
  368. **/
  369. static const char *ibmvfc_get_fc_type(u16 status)
  370. {
  371. int i;
  372. for (i = 0; i < ARRAY_SIZE(fc_type); i++)
  373. if (fc_type[i].fc_type == status)
  374. return fc_type[i].name;
  375. return unknown_fc_type;
  376. }
  377. /**
  378. * ibmvfc_set_tgt_action - Set the next init action for the target
  379. * @tgt: ibmvfc target struct
  380. * @action: action to perform
  381. *
  382. **/
  383. static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
  384. enum ibmvfc_target_action action)
  385. {
  386. switch (tgt->action) {
  387. case IBMVFC_TGT_ACTION_DEL_RPORT:
  388. if (action == IBMVFC_TGT_ACTION_DELETED_RPORT)
  389. tgt->action = action;
  390. case IBMVFC_TGT_ACTION_DELETED_RPORT:
  391. break;
  392. default:
  393. if (action == IBMVFC_TGT_ACTION_DEL_RPORT)
  394. tgt->add_rport = 0;
  395. tgt->action = action;
  396. break;
  397. }
  398. }
  399. /**
  400. * ibmvfc_set_host_state - Set the state for the host
  401. * @vhost: ibmvfc host struct
  402. * @state: state to set host to
  403. *
  404. * Returns:
  405. * 0 if state changed / non-zero if not changed
  406. **/
  407. static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
  408. enum ibmvfc_host_state state)
  409. {
  410. int rc = 0;
  411. switch (vhost->state) {
  412. case IBMVFC_HOST_OFFLINE:
  413. rc = -EINVAL;
  414. break;
  415. default:
  416. vhost->state = state;
  417. break;
  418. };
  419. return rc;
  420. }
  421. /**
  422. * ibmvfc_set_host_action - Set the next init action for the host
  423. * @vhost: ibmvfc host struct
  424. * @action: action to perform
  425. *
  426. **/
  427. static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
  428. enum ibmvfc_host_action action)
  429. {
  430. switch (action) {
  431. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  432. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
  433. vhost->action = action;
  434. break;
  435. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  436. if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
  437. vhost->action = action;
  438. break;
  439. case IBMVFC_HOST_ACTION_INIT_WAIT:
  440. if (vhost->action == IBMVFC_HOST_ACTION_INIT)
  441. vhost->action = action;
  442. break;
  443. case IBMVFC_HOST_ACTION_QUERY:
  444. switch (vhost->action) {
  445. case IBMVFC_HOST_ACTION_INIT_WAIT:
  446. case IBMVFC_HOST_ACTION_NONE:
  447. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  448. vhost->action = action;
  449. break;
  450. default:
  451. break;
  452. };
  453. break;
  454. case IBMVFC_HOST_ACTION_TGT_INIT:
  455. if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
  456. vhost->action = action;
  457. break;
  458. case IBMVFC_HOST_ACTION_INIT:
  459. case IBMVFC_HOST_ACTION_TGT_DEL:
  460. switch (vhost->action) {
  461. case IBMVFC_HOST_ACTION_RESET:
  462. case IBMVFC_HOST_ACTION_REENABLE:
  463. break;
  464. default:
  465. vhost->action = action;
  466. break;
  467. };
  468. break;
  469. case IBMVFC_HOST_ACTION_LOGO:
  470. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  471. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  472. case IBMVFC_HOST_ACTION_NONE:
  473. case IBMVFC_HOST_ACTION_RESET:
  474. case IBMVFC_HOST_ACTION_REENABLE:
  475. default:
  476. vhost->action = action;
  477. break;
  478. };
  479. }
  480. /**
  481. * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
  482. * @vhost: ibmvfc host struct
  483. *
  484. * Return value:
  485. * nothing
  486. **/
  487. static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
  488. {
  489. if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
  490. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  491. scsi_block_requests(vhost->host);
  492. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  493. }
  494. } else
  495. vhost->reinit = 1;
  496. wake_up(&vhost->work_wait_q);
  497. }
  498. /**
  499. * ibmvfc_link_down - Handle a link down event from the adapter
  500. * @vhost: ibmvfc host struct
  501. * @state: ibmvfc host state to enter
  502. *
  503. **/
  504. static void ibmvfc_link_down(struct ibmvfc_host *vhost,
  505. enum ibmvfc_host_state state)
  506. {
  507. struct ibmvfc_target *tgt;
  508. ENTER;
  509. scsi_block_requests(vhost->host);
  510. list_for_each_entry(tgt, &vhost->targets, queue)
  511. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  512. ibmvfc_set_host_state(vhost, state);
  513. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  514. vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
  515. wake_up(&vhost->work_wait_q);
  516. LEAVE;
  517. }
  518. /**
  519. * ibmvfc_init_host - Start host initialization
  520. * @vhost: ibmvfc host struct
  521. *
  522. * Return value:
  523. * nothing
  524. **/
  525. static void ibmvfc_init_host(struct ibmvfc_host *vhost)
  526. {
  527. struct ibmvfc_target *tgt;
  528. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  529. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  530. dev_err(vhost->dev,
  531. "Host initialization retries exceeded. Taking adapter offline\n");
  532. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  533. return;
  534. }
  535. }
  536. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  537. memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
  538. vhost->async_crq.cur = 0;
  539. list_for_each_entry(tgt, &vhost->targets, queue)
  540. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  541. scsi_block_requests(vhost->host);
  542. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  543. vhost->job_step = ibmvfc_npiv_login;
  544. wake_up(&vhost->work_wait_q);
  545. }
  546. }
  547. /**
  548. * ibmvfc_send_crq - Send a CRQ
  549. * @vhost: ibmvfc host struct
  550. * @word1: the first 64 bits of the data
  551. * @word2: the second 64 bits of the data
  552. *
  553. * Return value:
  554. * 0 on success / other on failure
  555. **/
  556. static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
  557. {
  558. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  559. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  560. }
  561. /**
  562. * ibmvfc_send_crq_init - Send a CRQ init message
  563. * @vhost: ibmvfc host struct
  564. *
  565. * Return value:
  566. * 0 on success / other on failure
  567. **/
  568. static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
  569. {
  570. ibmvfc_dbg(vhost, "Sending CRQ init\n");
  571. return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
  572. }
  573. /**
  574. * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
  575. * @vhost: ibmvfc host struct
  576. *
  577. * Return value:
  578. * 0 on success / other on failure
  579. **/
  580. static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
  581. {
  582. ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
  583. return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
  584. }
  585. /**
  586. * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
  587. * @vhost: ibmvfc host struct
  588. *
  589. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  590. * the crq with the hypervisor.
  591. **/
  592. static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
  593. {
  594. long rc = 0;
  595. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  596. struct ibmvfc_crq_queue *crq = &vhost->crq;
  597. ibmvfc_dbg(vhost, "Releasing CRQ\n");
  598. free_irq(vdev->irq, vhost);
  599. tasklet_kill(&vhost->tasklet);
  600. do {
  601. if (rc)
  602. msleep(100);
  603. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  604. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  605. vhost->state = IBMVFC_NO_CRQ;
  606. vhost->logged_in = 0;
  607. dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  608. free_page((unsigned long)crq->msgs);
  609. }
  610. /**
  611. * ibmvfc_reenable_crq_queue - reenables the CRQ
  612. * @vhost: ibmvfc host struct
  613. *
  614. * Return value:
  615. * 0 on success / other on failure
  616. **/
  617. static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
  618. {
  619. int rc = 0;
  620. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  621. /* Re-enable the CRQ */
  622. do {
  623. if (rc)
  624. msleep(100);
  625. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  626. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  627. if (rc)
  628. dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
  629. return rc;
  630. }
  631. /**
  632. * ibmvfc_reset_crq - resets a crq after a failure
  633. * @vhost: ibmvfc host struct
  634. *
  635. * Return value:
  636. * 0 on success / other on failure
  637. **/
  638. static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
  639. {
  640. int rc = 0;
  641. unsigned long flags;
  642. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  643. struct ibmvfc_crq_queue *crq = &vhost->crq;
  644. /* Close the CRQ */
  645. do {
  646. if (rc)
  647. msleep(100);
  648. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  649. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  650. spin_lock_irqsave(vhost->host->host_lock, flags);
  651. vhost->state = IBMVFC_NO_CRQ;
  652. vhost->logged_in = 0;
  653. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  654. /* Clean out the queue */
  655. memset(crq->msgs, 0, PAGE_SIZE);
  656. crq->cur = 0;
  657. /* And re-open it again */
  658. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  659. crq->msg_token, PAGE_SIZE);
  660. if (rc == H_CLOSED)
  661. /* Adapter is good, but other end is not ready */
  662. dev_warn(vhost->dev, "Partner adapter not ready\n");
  663. else if (rc != 0)
  664. dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
  665. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  666. return rc;
  667. }
  668. /**
  669. * ibmvfc_valid_event - Determines if event is valid.
  670. * @pool: event_pool that contains the event
  671. * @evt: ibmvfc event to be checked for validity
  672. *
  673. * Return value:
  674. * 1 if event is valid / 0 if event is not valid
  675. **/
  676. static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
  677. struct ibmvfc_event *evt)
  678. {
  679. int index = evt - pool->events;
  680. if (index < 0 || index >= pool->size) /* outside of bounds */
  681. return 0;
  682. if (evt != pool->events + index) /* unaligned */
  683. return 0;
  684. return 1;
  685. }
  686. /**
  687. * ibmvfc_free_event - Free the specified event
  688. * @evt: ibmvfc_event to be freed
  689. *
  690. **/
  691. static void ibmvfc_free_event(struct ibmvfc_event *evt)
  692. {
  693. struct ibmvfc_host *vhost = evt->vhost;
  694. struct ibmvfc_event_pool *pool = &vhost->pool;
  695. BUG_ON(!ibmvfc_valid_event(pool, evt));
  696. BUG_ON(atomic_inc_return(&evt->free) != 1);
  697. list_add_tail(&evt->queue, &vhost->free);
  698. }
  699. /**
  700. * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
  701. * @evt: ibmvfc event struct
  702. *
  703. * This function does not setup any error status, that must be done
  704. * before this function gets called.
  705. **/
  706. static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
  707. {
  708. struct scsi_cmnd *cmnd = evt->cmnd;
  709. if (cmnd) {
  710. scsi_dma_unmap(cmnd);
  711. cmnd->scsi_done(cmnd);
  712. }
  713. if (evt->eh_comp)
  714. complete(evt->eh_comp);
  715. ibmvfc_free_event(evt);
  716. }
  717. /**
  718. * ibmvfc_fail_request - Fail request with specified error code
  719. * @evt: ibmvfc event struct
  720. * @error_code: error code to fail request with
  721. *
  722. * Return value:
  723. * none
  724. **/
  725. static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
  726. {
  727. if (evt->cmnd) {
  728. evt->cmnd->result = (error_code << 16);
  729. evt->done = ibmvfc_scsi_eh_done;
  730. } else
  731. evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
  732. list_del(&evt->queue);
  733. del_timer(&evt->timer);
  734. ibmvfc_trc_end(evt);
  735. evt->done(evt);
  736. }
  737. /**
  738. * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
  739. * @vhost: ibmvfc host struct
  740. * @error_code: error code to fail requests with
  741. *
  742. * Return value:
  743. * none
  744. **/
  745. static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
  746. {
  747. struct ibmvfc_event *evt, *pos;
  748. ibmvfc_dbg(vhost, "Purging all requests\n");
  749. list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
  750. ibmvfc_fail_request(evt, error_code);
  751. }
  752. /**
  753. * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
  754. * @vhost: struct ibmvfc host to reset
  755. **/
  756. static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
  757. {
  758. ibmvfc_purge_requests(vhost, DID_ERROR);
  759. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  760. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  761. }
  762. /**
  763. * __ibmvfc_reset_host - Reset the connection to the server (no locking)
  764. * @vhost: struct ibmvfc host to reset
  765. **/
  766. static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
  767. {
  768. if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
  769. !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  770. scsi_block_requests(vhost->host);
  771. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
  772. vhost->job_step = ibmvfc_npiv_logout;
  773. wake_up(&vhost->work_wait_q);
  774. } else
  775. ibmvfc_hard_reset_host(vhost);
  776. }
  777. /**
  778. * ibmvfc_reset_host - Reset the connection to the server
  779. * @vhost: ibmvfc host struct
  780. **/
  781. static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
  782. {
  783. unsigned long flags;
  784. spin_lock_irqsave(vhost->host->host_lock, flags);
  785. __ibmvfc_reset_host(vhost);
  786. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  787. }
  788. /**
  789. * ibmvfc_retry_host_init - Retry host initialization if allowed
  790. * @vhost: ibmvfc host struct
  791. *
  792. * Returns: 1 if init will be retried / 0 if not
  793. *
  794. **/
  795. static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
  796. {
  797. int retry = 0;
  798. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  799. vhost->delay_init = 1;
  800. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  801. dev_err(vhost->dev,
  802. "Host initialization retries exceeded. Taking adapter offline\n");
  803. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  804. } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
  805. __ibmvfc_reset_host(vhost);
  806. else {
  807. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  808. retry = 1;
  809. }
  810. }
  811. wake_up(&vhost->work_wait_q);
  812. return retry;
  813. }
  814. /**
  815. * __ibmvfc_get_target - Find the specified scsi_target (no locking)
  816. * @starget: scsi target struct
  817. *
  818. * Return value:
  819. * ibmvfc_target struct / NULL if not found
  820. **/
  821. static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
  822. {
  823. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  824. struct ibmvfc_host *vhost = shost_priv(shost);
  825. struct ibmvfc_target *tgt;
  826. list_for_each_entry(tgt, &vhost->targets, queue)
  827. if (tgt->target_id == starget->id) {
  828. kref_get(&tgt->kref);
  829. return tgt;
  830. }
  831. return NULL;
  832. }
  833. /**
  834. * ibmvfc_get_target - Find the specified scsi_target
  835. * @starget: scsi target struct
  836. *
  837. * Return value:
  838. * ibmvfc_target struct / NULL if not found
  839. **/
  840. static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
  841. {
  842. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  843. struct ibmvfc_target *tgt;
  844. unsigned long flags;
  845. spin_lock_irqsave(shost->host_lock, flags);
  846. tgt = __ibmvfc_get_target(starget);
  847. spin_unlock_irqrestore(shost->host_lock, flags);
  848. return tgt;
  849. }
  850. /**
  851. * ibmvfc_get_host_speed - Get host port speed
  852. * @shost: scsi host struct
  853. *
  854. * Return value:
  855. * none
  856. **/
  857. static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
  858. {
  859. struct ibmvfc_host *vhost = shost_priv(shost);
  860. unsigned long flags;
  861. spin_lock_irqsave(shost->host_lock, flags);
  862. if (vhost->state == IBMVFC_ACTIVE) {
  863. switch (vhost->login_buf->resp.link_speed / 100) {
  864. case 1:
  865. fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
  866. break;
  867. case 2:
  868. fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
  869. break;
  870. case 4:
  871. fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
  872. break;
  873. case 8:
  874. fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
  875. break;
  876. case 10:
  877. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  878. break;
  879. case 16:
  880. fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
  881. break;
  882. default:
  883. ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
  884. vhost->login_buf->resp.link_speed / 100);
  885. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  886. break;
  887. }
  888. } else
  889. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  890. spin_unlock_irqrestore(shost->host_lock, flags);
  891. }
  892. /**
  893. * ibmvfc_get_host_port_state - Get host port state
  894. * @shost: scsi host struct
  895. *
  896. * Return value:
  897. * none
  898. **/
  899. static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
  900. {
  901. struct ibmvfc_host *vhost = shost_priv(shost);
  902. unsigned long flags;
  903. spin_lock_irqsave(shost->host_lock, flags);
  904. switch (vhost->state) {
  905. case IBMVFC_INITIALIZING:
  906. case IBMVFC_ACTIVE:
  907. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  908. break;
  909. case IBMVFC_LINK_DOWN:
  910. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  911. break;
  912. case IBMVFC_LINK_DEAD:
  913. case IBMVFC_HOST_OFFLINE:
  914. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  915. break;
  916. case IBMVFC_HALTED:
  917. fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
  918. break;
  919. case IBMVFC_NO_CRQ:
  920. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  921. break;
  922. default:
  923. ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
  924. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  925. break;
  926. }
  927. spin_unlock_irqrestore(shost->host_lock, flags);
  928. }
  929. /**
  930. * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
  931. * @rport: rport struct
  932. * @timeout: timeout value
  933. *
  934. * Return value:
  935. * none
  936. **/
  937. static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  938. {
  939. if (timeout)
  940. rport->dev_loss_tmo = timeout;
  941. else
  942. rport->dev_loss_tmo = 1;
  943. }
  944. /**
  945. * ibmvfc_release_tgt - Free memory allocated for a target
  946. * @kref: kref struct
  947. *
  948. **/
  949. static void ibmvfc_release_tgt(struct kref *kref)
  950. {
  951. struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
  952. kfree(tgt);
  953. }
  954. /**
  955. * ibmvfc_get_starget_node_name - Get SCSI target's node name
  956. * @starget: scsi target struct
  957. *
  958. * Return value:
  959. * none
  960. **/
  961. static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
  962. {
  963. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  964. fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
  965. if (tgt)
  966. kref_put(&tgt->kref, ibmvfc_release_tgt);
  967. }
  968. /**
  969. * ibmvfc_get_starget_port_name - Get SCSI target's port name
  970. * @starget: scsi target struct
  971. *
  972. * Return value:
  973. * none
  974. **/
  975. static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
  976. {
  977. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  978. fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
  979. if (tgt)
  980. kref_put(&tgt->kref, ibmvfc_release_tgt);
  981. }
  982. /**
  983. * ibmvfc_get_starget_port_id - Get SCSI target's port ID
  984. * @starget: scsi target struct
  985. *
  986. * Return value:
  987. * none
  988. **/
  989. static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
  990. {
  991. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  992. fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
  993. if (tgt)
  994. kref_put(&tgt->kref, ibmvfc_release_tgt);
  995. }
  996. /**
  997. * ibmvfc_wait_while_resetting - Wait while the host resets
  998. * @vhost: ibmvfc host struct
  999. *
  1000. * Return value:
  1001. * 0 on success / other on failure
  1002. **/
  1003. static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
  1004. {
  1005. long timeout = wait_event_timeout(vhost->init_wait_q,
  1006. ((vhost->state == IBMVFC_ACTIVE ||
  1007. vhost->state == IBMVFC_HOST_OFFLINE ||
  1008. vhost->state == IBMVFC_LINK_DEAD) &&
  1009. vhost->action == IBMVFC_HOST_ACTION_NONE),
  1010. (init_timeout * HZ));
  1011. return timeout ? 0 : -EIO;
  1012. }
  1013. /**
  1014. * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
  1015. * @shost: scsi host struct
  1016. *
  1017. * Return value:
  1018. * 0 on success / other on failure
  1019. **/
  1020. static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
  1021. {
  1022. struct ibmvfc_host *vhost = shost_priv(shost);
  1023. dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
  1024. ibmvfc_reset_host(vhost);
  1025. return ibmvfc_wait_while_resetting(vhost);
  1026. }
  1027. /**
  1028. * ibmvfc_gather_partition_info - Gather info about the LPAR
  1029. *
  1030. * Return value:
  1031. * none
  1032. **/
  1033. static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
  1034. {
  1035. struct device_node *rootdn;
  1036. const char *name;
  1037. const unsigned int *num;
  1038. rootdn = of_find_node_by_path("/");
  1039. if (!rootdn)
  1040. return;
  1041. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  1042. if (name)
  1043. strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
  1044. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  1045. if (num)
  1046. vhost->partition_number = *num;
  1047. of_node_put(rootdn);
  1048. }
  1049. /**
  1050. * ibmvfc_set_login_info - Setup info for NPIV login
  1051. * @vhost: ibmvfc host struct
  1052. *
  1053. * Return value:
  1054. * none
  1055. **/
  1056. static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
  1057. {
  1058. struct ibmvfc_npiv_login *login_info = &vhost->login_info;
  1059. struct device_node *of_node = vhost->dev->of_node;
  1060. const char *location;
  1061. memset(login_info, 0, sizeof(*login_info));
  1062. login_info->ostype = IBMVFC_OS_LINUX;
  1063. login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
  1064. login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
  1065. login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
  1066. login_info->partition_num = vhost->partition_number;
  1067. login_info->vfc_frame_version = 1;
  1068. login_info->fcp_version = 3;
  1069. login_info->flags = IBMVFC_FLUSH_ON_HALT;
  1070. if (vhost->client_migrated)
  1071. login_info->flags |= IBMVFC_CLIENT_MIGRATED;
  1072. login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1073. login_info->capabilities = IBMVFC_CAN_MIGRATE;
  1074. login_info->async.va = vhost->async_crq.msg_token;
  1075. login_info->async.len = vhost->async_crq.size * sizeof(*vhost->async_crq.msgs);
  1076. strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
  1077. strncpy(login_info->device_name,
  1078. dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
  1079. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1080. location = location ? location : dev_name(vhost->dev);
  1081. strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
  1082. }
  1083. /**
  1084. * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
  1085. * @vhost: ibmvfc host who owns the event pool
  1086. *
  1087. * Returns zero on success.
  1088. **/
  1089. static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
  1090. {
  1091. int i;
  1092. struct ibmvfc_event_pool *pool = &vhost->pool;
  1093. ENTER;
  1094. pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1095. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  1096. if (!pool->events)
  1097. return -ENOMEM;
  1098. pool->iu_storage = dma_alloc_coherent(vhost->dev,
  1099. pool->size * sizeof(*pool->iu_storage),
  1100. &pool->iu_token, 0);
  1101. if (!pool->iu_storage) {
  1102. kfree(pool->events);
  1103. return -ENOMEM;
  1104. }
  1105. for (i = 0; i < pool->size; ++i) {
  1106. struct ibmvfc_event *evt = &pool->events[i];
  1107. atomic_set(&evt->free, 1);
  1108. evt->crq.valid = 0x80;
  1109. evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
  1110. evt->xfer_iu = pool->iu_storage + i;
  1111. evt->vhost = vhost;
  1112. evt->ext_list = NULL;
  1113. list_add_tail(&evt->queue, &vhost->free);
  1114. }
  1115. LEAVE;
  1116. return 0;
  1117. }
  1118. /**
  1119. * ibmvfc_free_event_pool - Frees memory of the event pool of a host
  1120. * @vhost: ibmvfc host who owns the event pool
  1121. *
  1122. **/
  1123. static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
  1124. {
  1125. int i;
  1126. struct ibmvfc_event_pool *pool = &vhost->pool;
  1127. ENTER;
  1128. for (i = 0; i < pool->size; ++i) {
  1129. list_del(&pool->events[i].queue);
  1130. BUG_ON(atomic_read(&pool->events[i].free) != 1);
  1131. if (pool->events[i].ext_list)
  1132. dma_pool_free(vhost->sg_pool,
  1133. pool->events[i].ext_list,
  1134. pool->events[i].ext_list_token);
  1135. }
  1136. kfree(pool->events);
  1137. dma_free_coherent(vhost->dev,
  1138. pool->size * sizeof(*pool->iu_storage),
  1139. pool->iu_storage, pool->iu_token);
  1140. LEAVE;
  1141. }
  1142. /**
  1143. * ibmvfc_get_event - Gets the next free event in pool
  1144. * @vhost: ibmvfc host struct
  1145. *
  1146. * Returns a free event from the pool.
  1147. **/
  1148. static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
  1149. {
  1150. struct ibmvfc_event *evt;
  1151. BUG_ON(list_empty(&vhost->free));
  1152. evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
  1153. atomic_set(&evt->free, 0);
  1154. list_del(&evt->queue);
  1155. return evt;
  1156. }
  1157. /**
  1158. * ibmvfc_init_event - Initialize fields in an event struct that are always
  1159. * required.
  1160. * @evt: The event
  1161. * @done: Routine to call when the event is responded to
  1162. * @format: SRP or MAD format
  1163. **/
  1164. static void ibmvfc_init_event(struct ibmvfc_event *evt,
  1165. void (*done) (struct ibmvfc_event *), u8 format)
  1166. {
  1167. evt->cmnd = NULL;
  1168. evt->sync_iu = NULL;
  1169. evt->crq.format = format;
  1170. evt->done = done;
  1171. evt->eh_comp = NULL;
  1172. }
  1173. /**
  1174. * ibmvfc_map_sg_list - Initialize scatterlist
  1175. * @scmd: scsi command struct
  1176. * @nseg: number of scatterlist segments
  1177. * @md: memory descriptor list to initialize
  1178. **/
  1179. static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
  1180. struct srp_direct_buf *md)
  1181. {
  1182. int i;
  1183. struct scatterlist *sg;
  1184. scsi_for_each_sg(scmd, sg, nseg, i) {
  1185. md[i].va = sg_dma_address(sg);
  1186. md[i].len = sg_dma_len(sg);
  1187. md[i].key = 0;
  1188. }
  1189. }
  1190. /**
  1191. * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
  1192. * @scmd: Scsi_Cmnd with the scatterlist
  1193. * @evt: ibmvfc event struct
  1194. * @vfc_cmd: vfc_cmd that contains the memory descriptor
  1195. * @dev: device for which to map dma memory
  1196. *
  1197. * Returns:
  1198. * 0 on success / non-zero on failure
  1199. **/
  1200. static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
  1201. struct ibmvfc_event *evt,
  1202. struct ibmvfc_cmd *vfc_cmd, struct device *dev)
  1203. {
  1204. int sg_mapped;
  1205. struct srp_direct_buf *data = &vfc_cmd->ioba;
  1206. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  1207. sg_mapped = scsi_dma_map(scmd);
  1208. if (!sg_mapped) {
  1209. vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
  1210. return 0;
  1211. } else if (unlikely(sg_mapped < 0)) {
  1212. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1213. scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
  1214. return sg_mapped;
  1215. }
  1216. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  1217. vfc_cmd->flags |= IBMVFC_WRITE;
  1218. vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
  1219. } else {
  1220. vfc_cmd->flags |= IBMVFC_READ;
  1221. vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
  1222. }
  1223. if (sg_mapped == 1) {
  1224. ibmvfc_map_sg_list(scmd, sg_mapped, data);
  1225. return 0;
  1226. }
  1227. vfc_cmd->flags |= IBMVFC_SCATTERLIST;
  1228. if (!evt->ext_list) {
  1229. evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
  1230. &evt->ext_list_token);
  1231. if (!evt->ext_list) {
  1232. scsi_dma_unmap(scmd);
  1233. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1234. scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
  1235. return -ENOMEM;
  1236. }
  1237. }
  1238. ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
  1239. data->va = evt->ext_list_token;
  1240. data->len = sg_mapped * sizeof(struct srp_direct_buf);
  1241. data->key = 0;
  1242. return 0;
  1243. }
  1244. /**
  1245. * ibmvfc_timeout - Internal command timeout handler
  1246. * @evt: struct ibmvfc_event that timed out
  1247. *
  1248. * Called when an internally generated command times out
  1249. **/
  1250. static void ibmvfc_timeout(struct ibmvfc_event *evt)
  1251. {
  1252. struct ibmvfc_host *vhost = evt->vhost;
  1253. dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
  1254. ibmvfc_reset_host(vhost);
  1255. }
  1256. /**
  1257. * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
  1258. * @evt: event to be sent
  1259. * @vhost: ibmvfc host struct
  1260. * @timeout: timeout in seconds - 0 means do not time command
  1261. *
  1262. * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
  1263. **/
  1264. static int ibmvfc_send_event(struct ibmvfc_event *evt,
  1265. struct ibmvfc_host *vhost, unsigned long timeout)
  1266. {
  1267. u64 *crq_as_u64 = (u64 *) &evt->crq;
  1268. int rc;
  1269. /* Copy the IU into the transfer area */
  1270. *evt->xfer_iu = evt->iu;
  1271. if (evt->crq.format == IBMVFC_CMD_FORMAT)
  1272. evt->xfer_iu->cmd.tag = (u64)evt;
  1273. else if (evt->crq.format == IBMVFC_MAD_FORMAT)
  1274. evt->xfer_iu->mad_common.tag = (u64)evt;
  1275. else
  1276. BUG();
  1277. list_add_tail(&evt->queue, &vhost->sent);
  1278. init_timer(&evt->timer);
  1279. if (timeout) {
  1280. evt->timer.data = (unsigned long) evt;
  1281. evt->timer.expires = jiffies + (timeout * HZ);
  1282. evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
  1283. add_timer(&evt->timer);
  1284. }
  1285. mb();
  1286. if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
  1287. list_del(&evt->queue);
  1288. del_timer(&evt->timer);
  1289. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  1290. * Firmware will send a CRQ with a transport event (0xFF) to
  1291. * tell this client what has happened to the transport. This
  1292. * will be handled in ibmvfc_handle_crq()
  1293. */
  1294. if (rc == H_CLOSED) {
  1295. if (printk_ratelimit())
  1296. dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
  1297. if (evt->cmnd)
  1298. scsi_dma_unmap(evt->cmnd);
  1299. ibmvfc_free_event(evt);
  1300. return SCSI_MLQUEUE_HOST_BUSY;
  1301. }
  1302. dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
  1303. if (evt->cmnd) {
  1304. evt->cmnd->result = DID_ERROR << 16;
  1305. evt->done = ibmvfc_scsi_eh_done;
  1306. } else
  1307. evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
  1308. evt->done(evt);
  1309. } else
  1310. ibmvfc_trc_start(evt);
  1311. return 0;
  1312. }
  1313. /**
  1314. * ibmvfc_log_error - Log an error for the failed command if appropriate
  1315. * @evt: ibmvfc event to log
  1316. *
  1317. **/
  1318. static void ibmvfc_log_error(struct ibmvfc_event *evt)
  1319. {
  1320. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1321. struct ibmvfc_host *vhost = evt->vhost;
  1322. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1323. struct scsi_cmnd *cmnd = evt->cmnd;
  1324. const char *err = unknown_error;
  1325. int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
  1326. int logerr = 0;
  1327. int rsp_code = 0;
  1328. if (index >= 0) {
  1329. logerr = cmd_status[index].log;
  1330. err = cmd_status[index].name;
  1331. }
  1332. if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
  1333. return;
  1334. if (rsp->flags & FCP_RSP_LEN_VALID)
  1335. rsp_code = rsp->data.info.rsp_code;
  1336. scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
  1337. "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
  1338. cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
  1339. rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
  1340. }
  1341. /**
  1342. * ibmvfc_relogin - Log back into the specified device
  1343. * @sdev: scsi device struct
  1344. *
  1345. **/
  1346. static void ibmvfc_relogin(struct scsi_device *sdev)
  1347. {
  1348. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1349. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1350. struct ibmvfc_target *tgt;
  1351. list_for_each_entry(tgt, &vhost->targets, queue) {
  1352. if (rport == tgt->rport) {
  1353. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  1354. break;
  1355. }
  1356. }
  1357. ibmvfc_reinit_host(vhost);
  1358. }
  1359. /**
  1360. * ibmvfc_scsi_done - Handle responses from commands
  1361. * @evt: ibmvfc event to be handled
  1362. *
  1363. * Used as a callback when sending scsi cmds.
  1364. **/
  1365. static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
  1366. {
  1367. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1368. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1369. struct scsi_cmnd *cmnd = evt->cmnd;
  1370. u32 rsp_len = 0;
  1371. u32 sense_len = rsp->fcp_sense_len;
  1372. if (cmnd) {
  1373. if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
  1374. scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
  1375. else if (rsp->flags & FCP_RESID_UNDER)
  1376. scsi_set_resid(cmnd, rsp->fcp_resid);
  1377. else
  1378. scsi_set_resid(cmnd, 0);
  1379. if (vfc_cmd->status) {
  1380. cmnd->result = ibmvfc_get_err_result(vfc_cmd);
  1381. if (rsp->flags & FCP_RSP_LEN_VALID)
  1382. rsp_len = rsp->fcp_rsp_len;
  1383. if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
  1384. sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
  1385. if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
  1386. memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
  1387. if ((vfc_cmd->status & IBMVFC_VIOS_FAILURE) && (vfc_cmd->error == IBMVFC_PLOGI_REQUIRED))
  1388. ibmvfc_relogin(cmnd->device);
  1389. if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
  1390. cmnd->result = (DID_ERROR << 16);
  1391. ibmvfc_log_error(evt);
  1392. }
  1393. if (!cmnd->result &&
  1394. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
  1395. cmnd->result = (DID_ERROR << 16);
  1396. scsi_dma_unmap(cmnd);
  1397. cmnd->scsi_done(cmnd);
  1398. }
  1399. if (evt->eh_comp)
  1400. complete(evt->eh_comp);
  1401. ibmvfc_free_event(evt);
  1402. }
  1403. /**
  1404. * ibmvfc_host_chkready - Check if the host can accept commands
  1405. * @vhost: struct ibmvfc host
  1406. *
  1407. * Returns:
  1408. * 1 if host can accept command / 0 if not
  1409. **/
  1410. static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
  1411. {
  1412. int result = 0;
  1413. switch (vhost->state) {
  1414. case IBMVFC_LINK_DEAD:
  1415. case IBMVFC_HOST_OFFLINE:
  1416. result = DID_NO_CONNECT << 16;
  1417. break;
  1418. case IBMVFC_NO_CRQ:
  1419. case IBMVFC_INITIALIZING:
  1420. case IBMVFC_HALTED:
  1421. case IBMVFC_LINK_DOWN:
  1422. result = DID_REQUEUE << 16;
  1423. break;
  1424. case IBMVFC_ACTIVE:
  1425. result = 0;
  1426. break;
  1427. };
  1428. return result;
  1429. }
  1430. /**
  1431. * ibmvfc_queuecommand - The queuecommand function of the scsi template
  1432. * @cmnd: struct scsi_cmnd to be executed
  1433. * @done: Callback function to be called when cmnd is completed
  1434. *
  1435. * Returns:
  1436. * 0 on success / other on failure
  1437. **/
  1438. static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
  1439. void (*done) (struct scsi_cmnd *))
  1440. {
  1441. struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
  1442. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1443. struct ibmvfc_cmd *vfc_cmd;
  1444. struct ibmvfc_event *evt;
  1445. u8 tag[2];
  1446. int rc;
  1447. if (unlikely((rc = fc_remote_port_chkready(rport))) ||
  1448. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1449. cmnd->result = rc;
  1450. done(cmnd);
  1451. return 0;
  1452. }
  1453. cmnd->result = (DID_OK << 16);
  1454. evt = ibmvfc_get_event(vhost);
  1455. ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
  1456. evt->cmnd = cmnd;
  1457. cmnd->scsi_done = done;
  1458. vfc_cmd = &evt->iu.cmd;
  1459. memset(vfc_cmd, 0, sizeof(*vfc_cmd));
  1460. vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
  1461. vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
  1462. vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
  1463. vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
  1464. vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
  1465. vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
  1466. vfc_cmd->tgt_scsi_id = rport->port_id;
  1467. vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
  1468. int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
  1469. memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
  1470. if (scsi_populate_tag_msg(cmnd, tag)) {
  1471. vfc_cmd->task_tag = tag[1];
  1472. switch (tag[0]) {
  1473. case MSG_SIMPLE_TAG:
  1474. vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
  1475. break;
  1476. case MSG_HEAD_TAG:
  1477. vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
  1478. break;
  1479. case MSG_ORDERED_TAG:
  1480. vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
  1481. break;
  1482. };
  1483. }
  1484. if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
  1485. return ibmvfc_send_event(evt, vhost, 0);
  1486. ibmvfc_free_event(evt);
  1487. if (rc == -ENOMEM)
  1488. return SCSI_MLQUEUE_HOST_BUSY;
  1489. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1490. scmd_printk(KERN_ERR, cmnd,
  1491. "Failed to map DMA buffer for command. rc=%d\n", rc);
  1492. cmnd->result = DID_ERROR << 16;
  1493. done(cmnd);
  1494. return 0;
  1495. }
  1496. static DEF_SCSI_QCMD(ibmvfc_queuecommand)
  1497. /**
  1498. * ibmvfc_sync_completion - Signal that a synchronous command has completed
  1499. * @evt: ibmvfc event struct
  1500. *
  1501. **/
  1502. static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
  1503. {
  1504. /* copy the response back */
  1505. if (evt->sync_iu)
  1506. *evt->sync_iu = *evt->xfer_iu;
  1507. complete(&evt->comp);
  1508. }
  1509. /**
  1510. * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
  1511. * @evt: struct ibmvfc_event
  1512. *
  1513. **/
  1514. static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
  1515. {
  1516. struct ibmvfc_host *vhost = evt->vhost;
  1517. ibmvfc_free_event(evt);
  1518. vhost->aborting_passthru = 0;
  1519. dev_info(vhost->dev, "Passthru command cancelled\n");
  1520. }
  1521. /**
  1522. * ibmvfc_bsg_timeout - Handle a BSG timeout
  1523. * @job: struct fc_bsg_job that timed out
  1524. *
  1525. * Returns:
  1526. * 0 on success / other on failure
  1527. **/
  1528. static int ibmvfc_bsg_timeout(struct fc_bsg_job *job)
  1529. {
  1530. struct ibmvfc_host *vhost = shost_priv(job->shost);
  1531. unsigned long port_id = (unsigned long)job->dd_data;
  1532. struct ibmvfc_event *evt;
  1533. struct ibmvfc_tmf *tmf;
  1534. unsigned long flags;
  1535. int rc;
  1536. ENTER;
  1537. spin_lock_irqsave(vhost->host->host_lock, flags);
  1538. if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
  1539. __ibmvfc_reset_host(vhost);
  1540. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1541. return 0;
  1542. }
  1543. vhost->aborting_passthru = 1;
  1544. evt = ibmvfc_get_event(vhost);
  1545. ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
  1546. tmf = &evt->iu.tmf;
  1547. memset(tmf, 0, sizeof(*tmf));
  1548. tmf->common.version = 1;
  1549. tmf->common.opcode = IBMVFC_TMF_MAD;
  1550. tmf->common.length = sizeof(*tmf);
  1551. tmf->scsi_id = port_id;
  1552. tmf->cancel_key = IBMVFC_PASSTHRU_CANCEL_KEY;
  1553. tmf->my_cancel_key = IBMVFC_INTERNAL_CANCEL_KEY;
  1554. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1555. if (rc != 0) {
  1556. vhost->aborting_passthru = 0;
  1557. dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
  1558. rc = -EIO;
  1559. } else
  1560. dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
  1561. port_id);
  1562. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1563. LEAVE;
  1564. return rc;
  1565. }
  1566. /**
  1567. * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
  1568. * @vhost: struct ibmvfc_host to send command
  1569. * @port_id: port ID to send command
  1570. *
  1571. * Returns:
  1572. * 0 on success / other on failure
  1573. **/
  1574. static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
  1575. {
  1576. struct ibmvfc_port_login *plogi;
  1577. struct ibmvfc_target *tgt;
  1578. struct ibmvfc_event *evt;
  1579. union ibmvfc_iu rsp_iu;
  1580. unsigned long flags;
  1581. int rc = 0, issue_login = 1;
  1582. ENTER;
  1583. spin_lock_irqsave(vhost->host->host_lock, flags);
  1584. list_for_each_entry(tgt, &vhost->targets, queue) {
  1585. if (tgt->scsi_id == port_id) {
  1586. issue_login = 0;
  1587. break;
  1588. }
  1589. }
  1590. if (!issue_login)
  1591. goto unlock_out;
  1592. if (unlikely((rc = ibmvfc_host_chkready(vhost))))
  1593. goto unlock_out;
  1594. evt = ibmvfc_get_event(vhost);
  1595. ibmvfc_init_event(evt, ibmvfc_sync_completion,