/drivers/scsi/qla2xxx/qla_mid.c

http://github.com/mirrors/linux · C · 1003 lines · 790 code · 136 blank · 77 comment · 117 complexity · a8523ed9508ac3404c647300f18319a0 MD5 · raw file

  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2014 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include "qla_gbl.h"
  9. #include "qla_target.h"
  10. #include <linux/moduleparam.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/slab.h>
  13. #include <linux/list.h>
  14. #include <scsi/scsi_tcq.h>
  15. #include <scsi/scsicam.h>
  16. #include <linux/delay.h>
  17. void
  18. qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
  19. {
  20. if (vha->vp_idx && vha->timer_active) {
  21. del_timer_sync(&vha->timer);
  22. vha->timer_active = 0;
  23. }
  24. }
  25. static uint32_t
  26. qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
  27. {
  28. uint32_t vp_id;
  29. struct qla_hw_data *ha = vha->hw;
  30. unsigned long flags;
  31. /* Find an empty slot and assign an vp_id */
  32. mutex_lock(&ha->vport_lock);
  33. vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
  34. if (vp_id > ha->max_npiv_vports) {
  35. ql_dbg(ql_dbg_vport, vha, 0xa000,
  36. "vp_id %d is bigger than max-supported %d.\n",
  37. vp_id, ha->max_npiv_vports);
  38. mutex_unlock(&ha->vport_lock);
  39. return vp_id;
  40. }
  41. set_bit(vp_id, ha->vp_idx_map);
  42. ha->num_vhosts++;
  43. vha->vp_idx = vp_id;
  44. spin_lock_irqsave(&ha->vport_slock, flags);
  45. list_add_tail(&vha->list, &ha->vp_list);
  46. spin_unlock_irqrestore(&ha->vport_slock, flags);
  47. spin_lock_irqsave(&ha->hardware_lock, flags);
  48. qlt_update_vp_map(vha, SET_VP_IDX);
  49. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  50. mutex_unlock(&ha->vport_lock);
  51. return vp_id;
  52. }
  53. void
  54. qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
  55. {
  56. uint16_t vp_id;
  57. struct qla_hw_data *ha = vha->hw;
  58. unsigned long flags = 0;
  59. u8 i;
  60. mutex_lock(&ha->vport_lock);
  61. /*
  62. * Wait for all pending activities to finish before removing vport from
  63. * the list.
  64. * Lock needs to be held for safe removal from the list (it
  65. * ensures no active vp_list traversal while the vport is removed
  66. * from the queue)
  67. */
  68. for (i = 0; i < 10; i++) {
  69. if (wait_event_timeout(vha->vref_waitq,
  70. !atomic_read(&vha->vref_count), HZ) > 0)
  71. break;
  72. }
  73. spin_lock_irqsave(&ha->vport_slock, flags);
  74. if (atomic_read(&vha->vref_count)) {
  75. ql_dbg(ql_dbg_vport, vha, 0xfffa,
  76. "vha->vref_count=%u timeout\n", vha->vref_count.counter);
  77. vha->vref_count = (atomic_t)ATOMIC_INIT(0);
  78. }
  79. list_del(&vha->list);
  80. qlt_update_vp_map(vha, RESET_VP_IDX);
  81. spin_unlock_irqrestore(&ha->vport_slock, flags);
  82. vp_id = vha->vp_idx;
  83. ha->num_vhosts--;
  84. clear_bit(vp_id, ha->vp_idx_map);
  85. mutex_unlock(&ha->vport_lock);
  86. }
  87. static scsi_qla_host_t *
  88. qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
  89. {
  90. scsi_qla_host_t *vha;
  91. struct scsi_qla_host *tvha;
  92. unsigned long flags;
  93. spin_lock_irqsave(&ha->vport_slock, flags);
  94. /* Locate matching device in database. */
  95. list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
  96. if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
  97. spin_unlock_irqrestore(&ha->vport_slock, flags);
  98. return vha;
  99. }
  100. }
  101. spin_unlock_irqrestore(&ha->vport_slock, flags);
  102. return NULL;
  103. }
  104. /*
  105. * qla2x00_mark_vp_devices_dead
  106. * Updates fcport state when device goes offline.
  107. *
  108. * Input:
  109. * ha = adapter block pointer.
  110. * fcport = port structure pointer.
  111. *
  112. * Return:
  113. * None.
  114. *
  115. * Context:
  116. */
  117. static void
  118. qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
  119. {
  120. /*
  121. * !!! NOTE !!!
  122. * This function, if called in contexts other than vp create, disable
  123. * or delete, please make sure this is synchronized with the
  124. * delete thread.
  125. */
  126. fc_port_t *fcport;
  127. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  128. ql_dbg(ql_dbg_vport, vha, 0xa001,
  129. "Marking port dead, loop_id=0x%04x : %x.\n",
  130. fcport->loop_id, fcport->vha->vp_idx);
  131. qla2x00_mark_device_lost(vha, fcport, 0);
  132. qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
  133. }
  134. }
  135. int
  136. qla24xx_disable_vp(scsi_qla_host_t *vha)
  137. {
  138. unsigned long flags;
  139. int ret = QLA_SUCCESS;
  140. fc_port_t *fcport;
  141. if (vha->hw->flags.fw_started)
  142. ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
  143. atomic_set(&vha->loop_state, LOOP_DOWN);
  144. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  145. list_for_each_entry(fcport, &vha->vp_fcports, list)
  146. fcport->logout_on_delete = 0;
  147. qla2x00_mark_all_devices_lost(vha);
  148. /* Remove port id from vp target map */
  149. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  150. qlt_update_vp_map(vha, RESET_AL_PA);
  151. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  152. qla2x00_mark_vp_devices_dead(vha);
  153. atomic_set(&vha->vp_state, VP_FAILED);
  154. vha->flags.management_server_logged_in = 0;
  155. if (ret == QLA_SUCCESS) {
  156. fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
  157. } else {
  158. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. int
  164. qla24xx_enable_vp(scsi_qla_host_t *vha)
  165. {
  166. int ret;
  167. struct qla_hw_data *ha = vha->hw;
  168. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  169. /* Check if physical ha port is Up */
  170. if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
  171. atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
  172. !(ha->current_topology & ISP_CFG_F)) {
  173. vha->vp_err_state = VP_ERR_PORTDWN;
  174. fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
  175. ql_dbg(ql_dbg_taskm, vha, 0x800b,
  176. "%s skip enable. loop_state %x topo %x\n",
  177. __func__, base_vha->loop_state.counter,
  178. ha->current_topology);
  179. goto enable_failed;
  180. }
  181. /* Initialize the new vport unless it is a persistent port */
  182. mutex_lock(&ha->vport_lock);
  183. ret = qla24xx_modify_vp_config(vha);
  184. mutex_unlock(&ha->vport_lock);
  185. if (ret != QLA_SUCCESS) {
  186. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  187. goto enable_failed;
  188. }
  189. ql_dbg(ql_dbg_taskm, vha, 0x801a,
  190. "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
  191. return 0;
  192. enable_failed:
  193. ql_dbg(ql_dbg_taskm, vha, 0x801b,
  194. "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
  195. return 1;
  196. }
  197. static void
  198. qla24xx_configure_vp(scsi_qla_host_t *vha)
  199. {
  200. struct fc_vport *fc_vport;
  201. int ret;
  202. fc_vport = vha->fc_vport;
  203. ql_dbg(ql_dbg_vport, vha, 0xa002,
  204. "%s: change request #3.\n", __func__);
  205. ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
  206. if (ret != QLA_SUCCESS) {
  207. ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
  208. "receiving of RSCN requests: 0x%x.\n", ret);
  209. return;
  210. } else {
  211. /* Corresponds to SCR enabled */
  212. clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
  213. }
  214. vha->flags.online = 1;
  215. if (qla24xx_configure_vhba(vha))
  216. return;
  217. atomic_set(&vha->vp_state, VP_ACTIVE);
  218. fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
  219. }
  220. void
  221. qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
  222. {
  223. scsi_qla_host_t *vha;
  224. struct qla_hw_data *ha = rsp->hw;
  225. int i = 0;
  226. unsigned long flags;
  227. spin_lock_irqsave(&ha->vport_slock, flags);
  228. list_for_each_entry(vha, &ha->vp_list, list) {
  229. if (vha->vp_idx) {
  230. if (test_bit(VPORT_DELETE, &vha->dpc_flags))
  231. continue;
  232. atomic_inc(&vha->vref_count);
  233. spin_unlock_irqrestore(&ha->vport_slock, flags);
  234. switch (mb[0]) {
  235. case MBA_LIP_OCCURRED:
  236. case MBA_LOOP_UP:
  237. case MBA_LOOP_DOWN:
  238. case MBA_LIP_RESET:
  239. case MBA_POINT_TO_POINT:
  240. case MBA_CHG_IN_CONNECTION:
  241. ql_dbg(ql_dbg_async, vha, 0x5024,
  242. "Async_event for VP[%d], mb=0x%x vha=%p.\n",
  243. i, *mb, vha);
  244. qla2x00_async_event(vha, rsp, mb);
  245. break;
  246. case MBA_PORT_UPDATE:
  247. case MBA_RSCN_UPDATE:
  248. if ((mb[3] & 0xff) == vha->vp_idx) {
  249. ql_dbg(ql_dbg_async, vha, 0x5024,
  250. "Async_event for VP[%d], mb=0x%x vha=%p\n",
  251. i, *mb, vha);
  252. qla2x00_async_event(vha, rsp, mb);
  253. }
  254. break;
  255. }
  256. spin_lock_irqsave(&ha->vport_slock, flags);
  257. atomic_dec(&vha->vref_count);
  258. wake_up(&vha->vref_waitq);
  259. }
  260. i++;
  261. }
  262. spin_unlock_irqrestore(&ha->vport_slock, flags);
  263. }
  264. int
  265. qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
  266. {
  267. fc_port_t *fcport;
  268. /*
  269. * To exclusively reset vport, we need to log it out first.
  270. * Note: This control_vp can fail if ISP reset is already
  271. * issued, this is expected, as the vp would be already
  272. * logged out due to ISP reset.
  273. */
  274. if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
  275. qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
  276. list_for_each_entry(fcport, &vha->vp_fcports, list)
  277. fcport->logout_on_delete = 0;
  278. }
  279. /*
  280. * Physical port will do most of the abort and recovery work. We can
  281. * just treat it as a loop down
  282. */
  283. if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
  284. atomic_set(&vha->loop_state, LOOP_DOWN);
  285. qla2x00_mark_all_devices_lost(vha);
  286. } else {
  287. if (!atomic_read(&vha->loop_down_timer))
  288. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  289. }
  290. ql_dbg(ql_dbg_taskm, vha, 0x801d,
  291. "Scheduling enable of Vport %d.\n", vha->vp_idx);
  292. return qla24xx_enable_vp(vha);
  293. }
  294. static int
  295. qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
  296. {
  297. struct qla_hw_data *ha = vha->hw;
  298. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  299. ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
  300. "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
  301. /* Check if Fw is ready to configure VP first */
  302. if (test_bit(VP_CONFIG_OK, &base_vha->vp_flags)) {
  303. if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
  304. /* VP acquired. complete port configuration */
  305. ql_dbg(ql_dbg_dpc, vha, 0x4014,
  306. "Configure VP scheduled.\n");
  307. qla24xx_configure_vp(vha);
  308. ql_dbg(ql_dbg_dpc, vha, 0x4015,
  309. "Configure VP end.\n");
  310. return 0;
  311. }
  312. }
  313. if (test_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags)) {
  314. if (atomic_read(&vha->loop_state) == LOOP_READY) {
  315. qla24xx_process_purex_list(&vha->purex_list);
  316. clear_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags);
  317. }
  318. }
  319. if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
  320. ql_dbg(ql_dbg_dpc, vha, 0x4016,
  321. "FCPort update scheduled.\n");
  322. qla2x00_update_fcports(vha);
  323. clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
  324. ql_dbg(ql_dbg_dpc, vha, 0x4017,
  325. "FCPort update end.\n");
  326. }
  327. if (test_bit(RELOGIN_NEEDED, &vha->dpc_flags) &&
  328. !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
  329. atomic_read(&vha->loop_state) != LOOP_DOWN) {
  330. if (!vha->relogin_jif ||
  331. time_after_eq(jiffies, vha->relogin_jif)) {
  332. vha->relogin_jif = jiffies + HZ;
  333. clear_bit(RELOGIN_NEEDED, &vha->dpc_flags);
  334. ql_dbg(ql_dbg_dpc, vha, 0x4018,
  335. "Relogin needed scheduled.\n");
  336. qla24xx_post_relogin_work(vha);
  337. }
  338. }
  339. if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
  340. (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
  341. clear_bit(RESET_ACTIVE, &vha->dpc_flags);
  342. }
  343. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
  344. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
  345. ql_dbg(ql_dbg_dpc, vha, 0x401a,
  346. "Loop resync scheduled.\n");
  347. qla2x00_loop_resync(vha);
  348. clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
  349. ql_dbg(ql_dbg_dpc, vha, 0x401b,
  350. "Loop resync end.\n");
  351. }
  352. }
  353. ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
  354. "Exiting %s.\n", __func__);
  355. return 0;
  356. }
  357. void
  358. qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
  359. {
  360. struct qla_hw_data *ha = vha->hw;
  361. scsi_qla_host_t *vp;
  362. unsigned long flags = 0;
  363. if (vha->vp_idx)
  364. return;
  365. if (list_empty(&ha->vp_list))
  366. return;
  367. clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
  368. if (!(ha->current_topology & ISP_CFG_F))
  369. return;
  370. spin_lock_irqsave(&ha->vport_slock, flags);
  371. list_for_each_entry(vp, &ha->vp_list, list) {
  372. if (vp->vp_idx) {
  373. atomic_inc(&vp->vref_count);
  374. spin_unlock_irqrestore(&ha->vport_slock, flags);
  375. qla2x00_do_dpc_vp(vp);
  376. spin_lock_irqsave(&ha->vport_slock, flags);
  377. atomic_dec(&vp->vref_count);
  378. }
  379. }
  380. spin_unlock_irqrestore(&ha->vport_slock, flags);
  381. }
  382. int
  383. qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
  384. {
  385. scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
  386. struct qla_hw_data *ha = base_vha->hw;
  387. scsi_qla_host_t *vha;
  388. uint8_t port_name[WWN_SIZE];
  389. if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
  390. return VPCERR_UNSUPPORTED;
  391. /* Check up the F/W and H/W support NPIV */
  392. if (!ha->flags.npiv_supported)
  393. return VPCERR_UNSUPPORTED;
  394. /* Check up whether npiv supported switch presented */
  395. if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
  396. return VPCERR_NO_FABRIC_SUPP;
  397. /* Check up unique WWPN */
  398. u64_to_wwn(fc_vport->port_name, port_name);
  399. if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
  400. return VPCERR_BAD_WWN;
  401. vha = qla24xx_find_vhost_by_name(ha, port_name);
  402. if (vha)
  403. return VPCERR_BAD_WWN;
  404. /* Check up max-npiv-supports */
  405. if (ha->num_vhosts > ha->max_npiv_vports) {
  406. ql_dbg(ql_dbg_vport, vha, 0xa004,
  407. "num_vhosts %ud is bigger "
  408. "than max_npiv_vports %ud.\n",
  409. ha->num_vhosts, ha->max_npiv_vports);
  410. return VPCERR_UNSUPPORTED;
  411. }
  412. return 0;
  413. }
  414. scsi_qla_host_t *
  415. qla24xx_create_vhost(struct fc_vport *fc_vport)
  416. {
  417. scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
  418. struct qla_hw_data *ha = base_vha->hw;
  419. scsi_qla_host_t *vha;
  420. struct scsi_host_template *sht = &qla2xxx_driver_template;
  421. struct Scsi_Host *host;
  422. vha = qla2x00_create_host(sht, ha);
  423. if (!vha) {
  424. ql_log(ql_log_warn, vha, 0xa005,
  425. "scsi_host_alloc() failed for vport.\n");
  426. return(NULL);
  427. }
  428. host = vha->host;
  429. fc_vport->dd_data = vha;
  430. /* New host info */
  431. u64_to_wwn(fc_vport->node_name, vha->node_name);
  432. u64_to_wwn(fc_vport->port_name, vha->port_name);
  433. vha->fc_vport = fc_vport;
  434. vha->device_flags = 0;
  435. vha->vp_idx = qla24xx_allocate_vp_id(vha);
  436. if (vha->vp_idx > ha->max_npiv_vports) {
  437. ql_dbg(ql_dbg_vport, vha, 0xa006,
  438. "Couldn't allocate vp_id.\n");
  439. goto create_vhost_failed;
  440. }
  441. vha->mgmt_svr_loop_id = qla2x00_reserve_mgmt_server_loop_id(vha);
  442. vha->dpc_flags = 0L;
  443. ha->dpc_active = 0;
  444. set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
  445. set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
  446. /*
  447. * To fix the issue of processing a parent's RSCN for the vport before
  448. * its SCR is complete.
  449. */
  450. set_bit(VP_SCR_NEEDED, &vha->vp_flags);
  451. atomic_set(&vha->loop_state, LOOP_DOWN);
  452. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  453. qla2x00_start_timer(vha, WATCH_INTERVAL);
  454. vha->req = base_vha->req;
  455. vha->flags.nvme_enabled = base_vha->flags.nvme_enabled;
  456. host->can_queue = base_vha->req->length + 128;
  457. host->cmd_per_lun = 3;
  458. if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
  459. host->max_cmd_len = 32;
  460. else
  461. host->max_cmd_len = MAX_CMDSZ;
  462. host->max_channel = MAX_BUSES - 1;
  463. host->max_lun = ql2xmaxlun;
  464. host->unique_id = host->host_no;
  465. host->max_id = ha->max_fibre_devices;
  466. host->transportt = qla2xxx_transport_vport_template;
  467. ql_dbg(ql_dbg_vport, vha, 0xa007,
  468. "Detect vport hba %ld at address = %p.\n",
  469. vha->host_no, vha);
  470. vha->flags.init_done = 1;
  471. mutex_lock(&ha->vport_lock);
  472. set_bit(vha->vp_idx, ha->vp_idx_map);
  473. ha->cur_vport_count++;
  474. mutex_unlock(&ha->vport_lock);
  475. return vha;
  476. create_vhost_failed:
  477. return NULL;
  478. }
  479. static void
  480. qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
  481. {
  482. struct qla_hw_data *ha = vha->hw;
  483. uint16_t que_id = req->id;
  484. dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
  485. sizeof(request_t), req->ring, req->dma);
  486. req->ring = NULL;
  487. req->dma = 0;
  488. if (que_id) {
  489. ha->req_q_map[que_id] = NULL;
  490. mutex_lock(&ha->vport_lock);
  491. clear_bit(que_id, ha->req_qid_map);
  492. mutex_unlock(&ha->vport_lock);
  493. }
  494. kfree(req->outstanding_cmds);
  495. kfree(req);
  496. req = NULL;
  497. }
  498. static void
  499. qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
  500. {
  501. struct qla_hw_data *ha = vha->hw;
  502. uint16_t que_id = rsp->id;
  503. if (rsp->msix && rsp->msix->have_irq) {
  504. free_irq(rsp->msix->vector, rsp->msix->handle);
  505. rsp->msix->have_irq = 0;
  506. rsp->msix->in_use = 0;
  507. rsp->msix->handle = NULL;
  508. }
  509. dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
  510. sizeof(response_t), rsp->ring, rsp->dma);
  511. rsp->ring = NULL;
  512. rsp->dma = 0;
  513. if (que_id) {
  514. ha->rsp_q_map[que_id] = NULL;
  515. mutex_lock(&ha->vport_lock);
  516. clear_bit(que_id, ha->rsp_qid_map);
  517. mutex_unlock(&ha->vport_lock);
  518. }
  519. kfree(rsp);
  520. rsp = NULL;
  521. }
  522. int
  523. qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
  524. {
  525. int ret = QLA_SUCCESS;
  526. if (req && vha->flags.qpairs_req_created) {
  527. req->options |= BIT_0;
  528. ret = qla25xx_init_req_que(vha, req);
  529. if (ret != QLA_SUCCESS)
  530. return QLA_FUNCTION_FAILED;
  531. qla25xx_free_req_que(vha, req);
  532. }
  533. return ret;
  534. }
  535. int
  536. qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
  537. {
  538. int ret = QLA_SUCCESS;
  539. if (rsp && vha->flags.qpairs_rsp_created) {
  540. rsp->options |= BIT_0;
  541. ret = qla25xx_init_rsp_que(vha, rsp);
  542. if (ret != QLA_SUCCESS)
  543. return QLA_FUNCTION_FAILED;
  544. qla25xx_free_rsp_que(vha, rsp);
  545. }
  546. return ret;
  547. }
  548. /* Delete all queues for a given vhost */
  549. int
  550. qla25xx_delete_queues(struct scsi_qla_host *vha)
  551. {
  552. int cnt, ret = 0;
  553. struct req_que *req = NULL;
  554. struct rsp_que *rsp = NULL;
  555. struct qla_hw_data *ha = vha->hw;
  556. struct qla_qpair *qpair, *tqpair;
  557. if (ql2xmqsupport || ql2xnvmeenable) {
  558. list_for_each_entry_safe(qpair, tqpair, &vha->qp_list,
  559. qp_list_elem)
  560. qla2xxx_delete_qpair(vha, qpair);
  561. } else {
  562. /* Delete request queues */
  563. for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
  564. req = ha->req_q_map[cnt];
  565. if (req && test_bit(cnt, ha->req_qid_map)) {
  566. ret = qla25xx_delete_req_que(vha, req);
  567. if (ret != QLA_SUCCESS) {
  568. ql_log(ql_log_warn, vha, 0x00ea,
  569. "Couldn't delete req que %d.\n",
  570. req->id);
  571. return ret;
  572. }
  573. }
  574. }
  575. /* Delete response queues */
  576. for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
  577. rsp = ha->rsp_q_map[cnt];
  578. if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
  579. ret = qla25xx_delete_rsp_que(vha, rsp);
  580. if (ret != QLA_SUCCESS) {
  581. ql_log(ql_log_warn, vha, 0x00eb,
  582. "Couldn't delete rsp que %d.\n",
  583. rsp->id);
  584. return ret;
  585. }
  586. }
  587. }
  588. }
  589. return ret;
  590. }
  591. int
  592. qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
  593. uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos, bool startqp)
  594. {
  595. int ret = 0;
  596. struct req_que *req = NULL;
  597. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  598. struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
  599. uint16_t que_id = 0;
  600. device_reg_t *reg;
  601. uint32_t cnt;
  602. req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
  603. if (req == NULL) {
  604. ql_log(ql_log_fatal, base_vha, 0x00d9,
  605. "Failed to allocate memory for request queue.\n");
  606. goto failed;
  607. }
  608. req->length = REQUEST_ENTRY_CNT_24XX;
  609. req->ring = dma_alloc_coherent(&ha->pdev->dev,
  610. (req->length + 1) * sizeof(request_t),
  611. &req->dma, GFP_KERNEL);
  612. if (req->ring == NULL) {
  613. ql_log(ql_log_fatal, base_vha, 0x00da,
  614. "Failed to allocate memory for request_ring.\n");
  615. goto que_failed;
  616. }
  617. ret = qla2x00_alloc_outstanding_cmds(ha, req);
  618. if (ret != QLA_SUCCESS)
  619. goto que_failed;
  620. mutex_lock(&ha->mq_lock);
  621. que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
  622. if (que_id >= ha->max_req_queues) {
  623. mutex_unlock(&ha->mq_lock);
  624. ql_log(ql_log_warn, base_vha, 0x00db,
  625. "No resources to create additional request queue.\n");
  626. goto que_failed;
  627. }
  628. set_bit(que_id, ha->req_qid_map);
  629. ha->req_q_map[que_id] = req;
  630. req->rid = rid;
  631. req->vp_idx = vp_idx;
  632. req->qos = qos;
  633. ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
  634. "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
  635. que_id, req->rid, req->vp_idx, req->qos);
  636. ql_dbg(ql_dbg_init, base_vha, 0x00dc,
  637. "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
  638. que_id, req->rid, req->vp_idx, req->qos);
  639. if (rsp_que < 0)
  640. req->rsp = NULL;
  641. else
  642. req->rsp = ha->rsp_q_map[rsp_que];
  643. /* Use alternate PCI bus number */
  644. if (MSB(req->rid))
  645. options |= BIT_4;
  646. /* Use alternate PCI devfn */
  647. if (LSB(req->rid))
  648. options |= BIT_5;
  649. req->options = options;
  650. ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
  651. "options=0x%x.\n", req->options);
  652. ql_dbg(ql_dbg_init, base_vha, 0x00dd,
  653. "options=0x%x.\n", req->options);
  654. for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
  655. req->outstanding_cmds[cnt] = NULL;
  656. req->current_outstanding_cmd = 1;
  657. req->ring_ptr = req->ring;
  658. req->ring_index = 0;
  659. req->cnt = req->length;
  660. req->id = que_id;
  661. reg = ISP_QUE_REG(ha, que_id);
  662. req->req_q_in = &reg->isp25mq.req_q_in;
  663. req->req_q_out = &reg->isp25mq.req_q_out;
  664. req->max_q_depth = ha->req_q_map[0]->max_q_depth;
  665. req->out_ptr = (void *)(req->ring + req->length);
  666. mutex_unlock(&ha->mq_lock);
  667. ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
  668. "ring_ptr=%p ring_index=%d, "
  669. "cnt=%d id=%d max_q_depth=%d.\n",
  670. req->ring_ptr, req->ring_index,
  671. req->cnt, req->id, req->max_q_depth);
  672. ql_dbg(ql_dbg_init, base_vha, 0x00de,
  673. "ring_ptr=%p ring_index=%d, "
  674. "cnt=%d id=%d max_q_depth=%d.\n",
  675. req->ring_ptr, req->ring_index, req->cnt,
  676. req->id, req->max_q_depth);
  677. if (startqp) {
  678. ret = qla25xx_init_req_que(base_vha, req);
  679. if (ret != QLA_SUCCESS) {
  680. ql_log(ql_log_fatal, base_vha, 0x00df,
  681. "%s failed.\n", __func__);
  682. mutex_lock(&ha->mq_lock);
  683. clear_bit(que_id, ha->req_qid_map);
  684. mutex_unlock(&ha->mq_lock);
  685. goto que_failed;
  686. }
  687. vha->flags.qpairs_req_created = 1;
  688. }
  689. return req->id;
  690. que_failed:
  691. qla25xx_free_req_que(base_vha, req);
  692. failed:
  693. return 0;
  694. }
  695. static void qla_do_work(struct work_struct *work)
  696. {
  697. unsigned long flags;
  698. struct qla_qpair *qpair = container_of(work, struct qla_qpair, q_work);
  699. struct scsi_qla_host *vha;
  700. struct qla_hw_data *ha = qpair->hw;
  701. spin_lock_irqsave(&qpair->qp_lock, flags);
  702. vha = pci_get_drvdata(ha->pdev);
  703. qla24xx_process_response_queue(vha, qpair->rsp);
  704. spin_unlock_irqrestore(&qpair->qp_lock, flags);
  705. }
  706. /* create response queue */
  707. int
  708. qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
  709. uint8_t vp_idx, uint16_t rid, struct qla_qpair *qpair, bool startqp)
  710. {
  711. int ret = 0;
  712. struct rsp_que *rsp = NULL;
  713. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  714. struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
  715. uint16_t que_id = 0;
  716. device_reg_t *reg;
  717. rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
  718. if (rsp == NULL) {
  719. ql_log(ql_log_warn, base_vha, 0x0066,
  720. "Failed to allocate memory for response queue.\n");
  721. goto failed;
  722. }
  723. rsp->length = RESPONSE_ENTRY_CNT_MQ;
  724. rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
  725. (rsp->length + 1) * sizeof(response_t),
  726. &rsp->dma, GFP_KERNEL);
  727. if (rsp->ring == NULL) {
  728. ql_log(ql_log_warn, base_vha, 0x00e1,
  729. "Failed to allocate memory for response ring.\n");
  730. goto que_failed;
  731. }
  732. mutex_lock(&ha->mq_lock);
  733. que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
  734. if (que_id >= ha->max_rsp_queues) {
  735. mutex_unlock(&ha->mq_lock);
  736. ql_log(ql_log_warn, base_vha, 0x00e2,
  737. "No resources to create additional request queue.\n");
  738. goto que_failed;
  739. }
  740. set_bit(que_id, ha->rsp_qid_map);
  741. rsp->msix = qpair->msix;
  742. ha->rsp_q_map[que_id] = rsp;
  743. rsp->rid = rid;
  744. rsp->vp_idx = vp_idx;
  745. rsp->hw = ha;
  746. ql_dbg(ql_dbg_init, base_vha, 0x00e4,
  747. "rsp queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
  748. que_id, rsp->rid, rsp->vp_idx, rsp->hw);
  749. /* Use alternate PCI bus number */
  750. if (MSB(rsp->rid))
  751. options |= BIT_4;
  752. /* Use alternate PCI devfn */
  753. if (LSB(rsp->rid))
  754. options |= BIT_5;
  755. /* Enable MSIX handshake mode on for uncapable adapters */
  756. if (!IS_MSIX_NACK_CAPABLE(ha))
  757. options |= BIT_6;
  758. /* Set option to indicate response queue creation */
  759. options |= BIT_1;
  760. rsp->options = options;
  761. rsp->id = que_id;
  762. reg = ISP_QUE_REG(ha, que_id);
  763. rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
  764. rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
  765. rsp->in_ptr = (void *)(rsp->ring + rsp->length);
  766. mutex_unlock(&ha->mq_lock);
  767. ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
  768. "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
  769. rsp->options, rsp->id, rsp->rsp_q_in,
  770. rsp->rsp_q_out);
  771. ql_dbg(ql_dbg_init, base_vha, 0x00e5,
  772. "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
  773. rsp->options, rsp->id, rsp->rsp_q_in,
  774. rsp->rsp_q_out);
  775. ret = qla25xx_request_irq(ha, qpair, qpair->msix,
  776. ha->flags.disable_msix_handshake ?
  777. QLA_MSIX_QPAIR_MULTIQ_RSP_Q : QLA_MSIX_QPAIR_MULTIQ_RSP_Q_HS);
  778. if (ret)
  779. goto que_failed;
  780. if (startqp) {
  781. ret = qla25xx_init_rsp_que(base_vha, rsp);
  782. if (ret != QLA_SUCCESS) {
  783. ql_log(ql_log_fatal, base_vha, 0x00e7,
  784. "%s failed.\n", __func__);
  785. mutex_lock(&ha->mq_lock);
  786. clear_bit(que_id, ha->rsp_qid_map);
  787. mutex_unlock(&ha->mq_lock);
  788. goto que_failed;
  789. }
  790. vha->flags.qpairs_rsp_created = 1;
  791. }
  792. rsp->req = NULL;
  793. qla2x00_init_response_q_entries(rsp);
  794. if (qpair->hw->wq)
  795. INIT_WORK(&qpair->q_work, qla_do_work);
  796. return rsp->id;
  797. que_failed:
  798. qla25xx_free_rsp_que(base_vha, rsp);
  799. failed:
  800. return 0;
  801. }
  802. static void qla_ctrlvp_sp_done(srb_t *sp, int res)
  803. {
  804. if (sp->comp)
  805. complete(sp->comp);
  806. /* don't free sp here. Let the caller do the free */
  807. }
  808. /**
  809. * qla24xx_control_vp() - Enable a virtual port for given host
  810. * @vha: adapter block pointer
  811. * @cmd: command type to be sent for enable virtual port
  812. *
  813. * Return: qla2xxx local function return status code.
  814. */
  815. int qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
  816. {
  817. int rval = QLA_MEMORY_ALLOC_FAILED;
  818. struct qla_hw_data *ha = vha->hw;
  819. int vp_index = vha->vp_idx;
  820. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  821. DECLARE_COMPLETION_ONSTACK(comp);
  822. srb_t *sp;
  823. ql_dbg(ql_dbg_vport, vha, 0x10c1,
  824. "Entered %s cmd %x index %d.\n", __func__, cmd, vp_index);
  825. if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
  826. return QLA_PARAMETER_ERROR;
  827. sp = qla2x00_get_sp(base_vha, NULL, GFP_KERNEL);
  828. if (!sp)
  829. return rval;
  830. sp->type = SRB_CTRL_VP;
  831. sp->name = "ctrl_vp";
  832. sp->comp = &comp;
  833. sp->done = qla_ctrlvp_sp_done;
  834. sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
  835. qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
  836. sp->u.iocb_cmd.u.ctrlvp.cmd = cmd;
  837. sp->u.iocb_cmd.u.ctrlvp.vp_index = vp_index;
  838. rval = qla2x00_start_sp(sp);
  839. if (rval != QLA_SUCCESS) {
  840. ql_dbg(ql_dbg_async, vha, 0xffff,
  841. "%s: %s Failed submission. %x.\n",
  842. __func__, sp->name, rval);
  843. goto done;
  844. }
  845. ql_dbg(ql_dbg_vport, vha, 0x113f, "%s hndl %x submitted\n",
  846. sp->name, sp->handle);
  847. wait_for_completion(&comp);
  848. sp->comp = NULL;
  849. rval = sp->rc;
  850. switch (rval) {
  851. case QLA_FUNCTION_TIMEOUT:
  852. ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Timeout. %x.\n",
  853. __func__, sp->name, rval);
  854. break;
  855. case QLA_SUCCESS:
  856. ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s done.\n",
  857. __func__, sp->name);
  858. break;
  859. default:
  860. ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Failed. %x.\n",
  861. __func__, sp->name, rval);
  862. break;
  863. }
  864. done:
  865. sp->free(sp);
  866. return rval;
  867. }