qla_mid.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QLogic Fibre Channel HBA Driver
  4. * Copyright (c) 2003-2014 QLogic Corporation
  5. */
  6. #include "qla_def.h"
  7. #include "qla_gbl.h"
  8. #include "qla_target.h"
  9. #include <linux/moduleparam.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/slab.h>
  12. #include <linux/list.h>
  13. #include <scsi/scsi_tcq.h>
  14. #include <scsi/scsicam.h>
  15. #include <linux/delay.h>
  16. void
  17. qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
  18. {
  19. if (vha->vp_idx && vha->timer_active) {
  20. timer_delete_sync(&vha->timer);
  21. vha->timer_active = 0;
  22. }
  23. }
  24. static uint32_t
  25. qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
  26. {
  27. uint32_t vp_id;
  28. struct qla_hw_data *ha = vha->hw;
  29. unsigned long flags;
  30. /* Find an empty slot and assign an vp_id */
  31. mutex_lock(&ha->vport_lock);
  32. vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
  33. if (vp_id > ha->max_npiv_vports) {
  34. ql_dbg(ql_dbg_vport, vha, 0xa000,
  35. "vp_id %d is bigger than max-supported %d.\n",
  36. vp_id, ha->max_npiv_vports);
  37. mutex_unlock(&ha->vport_lock);
  38. return vp_id;
  39. }
  40. set_bit(vp_id, ha->vp_idx_map);
  41. ha->num_vhosts++;
  42. vha->vp_idx = vp_id;
  43. spin_lock_irqsave(&ha->vport_slock, flags);
  44. list_add_tail(&vha->list, &ha->vp_list);
  45. spin_unlock_irqrestore(&ha->vport_slock, flags);
  46. spin_lock_irqsave(&ha->hardware_lock, flags);
  47. qla_update_vp_map(vha, SET_VP_IDX);
  48. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  49. mutex_unlock(&ha->vport_lock);
  50. return vp_id;
  51. }
  52. void
  53. qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
  54. {
  55. uint16_t vp_id;
  56. struct qla_hw_data *ha = vha->hw;
  57. unsigned long flags = 0;
  58. u32 i, bailout;
  59. mutex_lock(&ha->vport_lock);
  60. /*
  61. * Wait for all pending activities to finish before removing vport from
  62. * the list.
  63. * Lock needs to be held for safe removal from the list (it
  64. * ensures no active vp_list traversal while the vport is removed
  65. * from the queue)
  66. */
  67. bailout = 0;
  68. for (i = 0; i < 500; i++) {
  69. spin_lock_irqsave(&ha->vport_slock, flags);
  70. if (atomic_read(&vha->vref_count) == 0) {
  71. list_del(&vha->list);
  72. qla_update_vp_map(vha, RESET_VP_IDX);
  73. bailout = 1;
  74. }
  75. spin_unlock_irqrestore(&ha->vport_slock, flags);
  76. if (bailout)
  77. break;
  78. else
  79. msleep(20);
  80. }
  81. if (!bailout) {
  82. ql_log(ql_log_info, vha, 0xfffa,
  83. "vha->vref_count=%u timeout\n", vha->vref_count.counter);
  84. spin_lock_irqsave(&ha->vport_slock, flags);
  85. list_del(&vha->list);
  86. qla_update_vp_map(vha, RESET_VP_IDX);
  87. spin_unlock_irqrestore(&ha->vport_slock, flags);
  88. }
  89. vp_id = vha->vp_idx;
  90. ha->num_vhosts--;
  91. clear_bit(vp_id, ha->vp_idx_map);
  92. mutex_unlock(&ha->vport_lock);
  93. }
  94. static scsi_qla_host_t *
  95. qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
  96. {
  97. scsi_qla_host_t *vha;
  98. struct scsi_qla_host *tvha;
  99. unsigned long flags;
  100. spin_lock_irqsave(&ha->vport_slock, flags);
  101. /* Locate matching device in database. */
  102. list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
  103. if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
  104. spin_unlock_irqrestore(&ha->vport_slock, flags);
  105. return vha;
  106. }
  107. }
  108. spin_unlock_irqrestore(&ha->vport_slock, flags);
  109. return NULL;
  110. }
  111. /*
  112. * qla2x00_mark_vp_devices_dead
  113. * Updates fcport state when device goes offline.
  114. *
  115. * Input:
  116. * ha = adapter block pointer.
  117. * fcport = port structure pointer.
  118. *
  119. * Return:
  120. * None.
  121. *
  122. * Context:
  123. */
  124. static void
  125. qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
  126. {
  127. /*
  128. * !!! NOTE !!!
  129. * This function, if called in contexts other than vp create, disable
  130. * or delete, please make sure this is synchronized with the
  131. * delete thread.
  132. */
  133. fc_port_t *fcport;
  134. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  135. ql_dbg(ql_dbg_vport, vha, 0xa001,
  136. "Marking port dead, loop_id=0x%04x : %x.\n",
  137. fcport->loop_id, fcport->vha->vp_idx);
  138. qla2x00_mark_device_lost(vha, fcport, 0);
  139. qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
  140. }
  141. }
  142. int
  143. qla24xx_disable_vp(scsi_qla_host_t *vha)
  144. {
  145. unsigned long flags;
  146. int ret = QLA_SUCCESS;
  147. fc_port_t *fcport;
  148. if (vha->hw->flags.edif_enabled) {
  149. if (DBELL_ACTIVE(vha))
  150. qla2x00_post_aen_work(vha, FCH_EVT_VENDOR_UNIQUE,
  151. FCH_EVT_VENDOR_UNIQUE_VPORT_DOWN);
  152. /* delete sessions and flush sa_indexes */
  153. qla2x00_wait_for_sess_deletion(vha);
  154. }
  155. if (vha->hw->flags.fw_started)
  156. ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
  157. atomic_set(&vha->loop_state, LOOP_DOWN);
  158. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  159. list_for_each_entry(fcport, &vha->vp_fcports, list)
  160. fcport->logout_on_delete = 1;
  161. if (!vha->hw->flags.edif_enabled)
  162. qla2x00_wait_for_sess_deletion(vha);
  163. /* Remove port id from vp target map */
  164. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  165. qla_update_vp_map(vha, RESET_AL_PA);
  166. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  167. qla2x00_mark_vp_devices_dead(vha);
  168. atomic_set(&vha->vp_state, VP_FAILED);
  169. vha->flags.management_server_logged_in = 0;
  170. if (ret == QLA_SUCCESS) {
  171. fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
  172. } else {
  173. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. int
  179. qla24xx_enable_vp(scsi_qla_host_t *vha)
  180. {
  181. int ret;
  182. struct qla_hw_data *ha = vha->hw;
  183. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  184. /* Check if physical ha port is Up */
  185. if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
  186. atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
  187. !(ha->current_topology & ISP_CFG_F)) {
  188. vha->vp_err_state = VP_ERR_PORTDWN;
  189. fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
  190. ql_dbg(ql_dbg_taskm, vha, 0x800b,
  191. "%s skip enable. loop_state %x topo %x\n",
  192. __func__, base_vha->loop_state.counter,
  193. ha->current_topology);
  194. goto enable_failed;
  195. }
  196. /* Initialize the new vport unless it is a persistent port */
  197. mutex_lock(&ha->vport_lock);
  198. ret = qla24xx_modify_vp_config(vha);
  199. mutex_unlock(&ha->vport_lock);
  200. if (ret != QLA_SUCCESS) {
  201. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  202. goto enable_failed;
  203. }
  204. ql_dbg(ql_dbg_taskm, vha, 0x801a,
  205. "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
  206. return 0;
  207. enable_failed:
  208. ql_dbg(ql_dbg_taskm, vha, 0x801b,
  209. "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
  210. return 1;
  211. }
  212. static void
  213. qla24xx_configure_vp(scsi_qla_host_t *vha)
  214. {
  215. struct fc_vport *fc_vport;
  216. int ret;
  217. fc_vport = vha->fc_vport;
  218. ql_dbg(ql_dbg_vport, vha, 0xa002,
  219. "%s: change request #3.\n", __func__);
  220. ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
  221. if (ret != QLA_SUCCESS) {
  222. ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
  223. "receiving of RSCN requests: 0x%x.\n", ret);
  224. return;
  225. } else {
  226. /* Corresponds to SCR enabled */
  227. clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
  228. }
  229. vha->flags.online = 1;
  230. if (qla24xx_configure_vhba(vha))
  231. return;
  232. atomic_set(&vha->vp_state, VP_ACTIVE);
  233. fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
  234. }
  235. void
  236. qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
  237. {
  238. scsi_qla_host_t *vha, *tvp;
  239. struct qla_hw_data *ha = rsp->hw;
  240. int i = 0;
  241. unsigned long flags;
  242. spin_lock_irqsave(&ha->vport_slock, flags);
  243. list_for_each_entry_safe(vha, tvp, &ha->vp_list, list) {
  244. if (vha->vp_idx) {
  245. if (test_bit(VPORT_DELETE, &vha->dpc_flags))
  246. continue;
  247. atomic_inc(&vha->vref_count);
  248. spin_unlock_irqrestore(&ha->vport_slock, flags);
  249. switch (mb[0]) {
  250. case MBA_LIP_OCCURRED:
  251. case MBA_LOOP_UP:
  252. case MBA_LOOP_DOWN:
  253. case MBA_LIP_RESET:
  254. case MBA_POINT_TO_POINT:
  255. case MBA_CHG_IN_CONNECTION:
  256. ql_dbg(ql_dbg_async, vha, 0x5024,
  257. "Async_event for VP[%d], mb=0x%x vha=%p.\n",
  258. i, *mb, vha);
  259. qla2x00_async_event(vha, rsp, mb);
  260. break;
  261. case MBA_PORT_UPDATE:
  262. case MBA_RSCN_UPDATE:
  263. if ((mb[3] & 0xff) == vha->vp_idx) {
  264. ql_dbg(ql_dbg_async, vha, 0x5024,
  265. "Async_event for VP[%d], mb=0x%x vha=%p\n",
  266. i, *mb, vha);
  267. qla2x00_async_event(vha, rsp, mb);
  268. }
  269. break;
  270. }
  271. spin_lock_irqsave(&ha->vport_slock, flags);
  272. atomic_dec(&vha->vref_count);
  273. wake_up(&vha->vref_waitq);
  274. }
  275. i++;
  276. }
  277. spin_unlock_irqrestore(&ha->vport_slock, flags);
  278. }
  279. int
  280. qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
  281. {
  282. fc_port_t *fcport;
  283. /*
  284. * To exclusively reset vport, we need to log it out first.
  285. * Note: This control_vp can fail if ISP reset is already
  286. * issued, this is expected, as the vp would be already
  287. * logged out due to ISP reset.
  288. */
  289. if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
  290. qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
  291. list_for_each_entry(fcport, &vha->vp_fcports, list)
  292. fcport->logout_on_delete = 0;
  293. }
  294. /*
  295. * Physical port will do most of the abort and recovery work. We can
  296. * just treat it as a loop down
  297. */
  298. if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
  299. atomic_set(&vha->loop_state, LOOP_DOWN);
  300. qla2x00_mark_all_devices_lost(vha);
  301. } else {
  302. if (!atomic_read(&vha->loop_down_timer))
  303. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  304. }
  305. ql_dbg(ql_dbg_taskm, vha, 0x801d,
  306. "Scheduling enable of Vport %d.\n", vha->vp_idx);
  307. return qla24xx_enable_vp(vha);
  308. }
  309. static int
  310. qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
  311. {
  312. struct qla_hw_data *ha = vha->hw;
  313. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  314. ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
  315. "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
  316. /* Check if Fw is ready to configure VP first */
  317. if (test_bit(VP_CONFIG_OK, &base_vha->vp_flags)) {
  318. if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
  319. /* VP acquired. complete port configuration */
  320. ql_dbg(ql_dbg_dpc, vha, 0x4014,
  321. "Configure VP scheduled.\n");
  322. qla24xx_configure_vp(vha);
  323. ql_dbg(ql_dbg_dpc, vha, 0x4015,
  324. "Configure VP end.\n");
  325. return 0;
  326. }
  327. }
  328. if (test_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags)) {
  329. if (atomic_read(&vha->loop_state) == LOOP_READY) {
  330. qla24xx_process_purex_list(&vha->purex_list);
  331. clear_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags);
  332. }
  333. }
  334. if (test_bit(RELOGIN_NEEDED, &vha->dpc_flags) &&
  335. !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
  336. atomic_read(&vha->loop_state) != LOOP_DOWN) {
  337. if (!vha->relogin_jif ||
  338. time_after_eq(jiffies, vha->relogin_jif)) {
  339. vha->relogin_jif = jiffies + HZ;
  340. clear_bit(RELOGIN_NEEDED, &vha->dpc_flags);
  341. ql_dbg(ql_dbg_dpc, vha, 0x4018,
  342. "Relogin needed scheduled.\n");
  343. qla24xx_post_relogin_work(vha);
  344. }
  345. }
  346. if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
  347. (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
  348. clear_bit(RESET_ACTIVE, &vha->dpc_flags);
  349. }
  350. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
  351. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
  352. ql_dbg(ql_dbg_dpc, vha, 0x401a,
  353. "Loop resync scheduled.\n");
  354. qla2x00_loop_resync(vha);
  355. clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
  356. ql_dbg(ql_dbg_dpc, vha, 0x401b,
  357. "Loop resync end.\n");
  358. }
  359. }
  360. ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
  361. "Exiting %s.\n", __func__);
  362. return 0;
  363. }
  364. void
  365. qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
  366. {
  367. struct qla_hw_data *ha = vha->hw;
  368. scsi_qla_host_t *vp, *tvp;
  369. unsigned long flags = 0;
  370. if (vha->vp_idx)
  371. return;
  372. if (list_empty(&ha->vp_list))
  373. return;
  374. clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
  375. if (!(ha->current_topology & ISP_CFG_F))
  376. return;
  377. spin_lock_irqsave(&ha->vport_slock, flags);
  378. list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
  379. if (vp->vp_idx) {
  380. atomic_inc(&vp->vref_count);
  381. spin_unlock_irqrestore(&ha->vport_slock, flags);
  382. qla2x00_do_dpc_vp(vp);
  383. spin_lock_irqsave(&ha->vport_slock, flags);
  384. atomic_dec(&vp->vref_count);
  385. }
  386. }
  387. spin_unlock_irqrestore(&ha->vport_slock, flags);
  388. }
  389. int
  390. qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
  391. {
  392. scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
  393. struct qla_hw_data *ha = base_vha->hw;
  394. scsi_qla_host_t *vha;
  395. uint8_t port_name[WWN_SIZE];
  396. if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
  397. return VPCERR_UNSUPPORTED;
  398. /* Check up the F/W and H/W support NPIV */
  399. if (!ha->flags.npiv_supported)
  400. return VPCERR_UNSUPPORTED;
  401. /* Check up whether npiv supported switch presented */
  402. if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
  403. return VPCERR_NO_FABRIC_SUPP;
  404. /* Check up unique WWPN */
  405. u64_to_wwn(fc_vport->port_name, port_name);
  406. if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
  407. return VPCERR_BAD_WWN;
  408. vha = qla24xx_find_vhost_by_name(ha, port_name);
  409. if (vha)
  410. return VPCERR_BAD_WWN;
  411. /* Check up max-npiv-supports */
  412. if (ha->num_vhosts > ha->max_npiv_vports) {
  413. ql_dbg(ql_dbg_vport, vha, 0xa004,
  414. "num_vhosts %ud is bigger "
  415. "than max_npiv_vports %ud.\n",
  416. ha->num_vhosts, ha->max_npiv_vports);
  417. return VPCERR_UNSUPPORTED;
  418. }
  419. return 0;
  420. }
  421. scsi_qla_host_t *
  422. qla24xx_create_vhost(struct fc_vport *fc_vport)
  423. {
  424. scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
  425. struct qla_hw_data *ha = base_vha->hw;
  426. scsi_qla_host_t *vha;
  427. const struct scsi_host_template *sht = &qla2xxx_driver_template;
  428. struct Scsi_Host *host;
  429. vha = qla2x00_create_host(sht, ha);
  430. if (!vha) {
  431. ql_log(ql_log_warn, vha, 0xa005,
  432. "scsi_host_alloc() failed for vport.\n");
  433. return(NULL);
  434. }
  435. vha->irq_offset = QLA_BASE_VECTORS;
  436. host = vha->host;
  437. fc_vport->dd_data = vha;
  438. /* New host info */
  439. u64_to_wwn(fc_vport->node_name, vha->node_name);
  440. u64_to_wwn(fc_vport->port_name, vha->port_name);
  441. vha->fc_vport = fc_vport;
  442. vha->device_flags = 0;
  443. vha->vp_idx = qla24xx_allocate_vp_id(vha);
  444. if (vha->vp_idx > ha->max_npiv_vports) {
  445. ql_dbg(ql_dbg_vport, vha, 0xa006,
  446. "Couldn't allocate vp_id.\n");
  447. goto create_vhost_failed;
  448. }
  449. vha->mgmt_svr_loop_id = qla2x00_reserve_mgmt_server_loop_id(vha);
  450. vha->dpc_flags = 0L;
  451. ha->dpc_active = 0;
  452. set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
  453. set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
  454. /*
  455. * To fix the issue of processing a parent's RSCN for the vport before
  456. * its SCR is complete.
  457. */
  458. set_bit(VP_SCR_NEEDED, &vha->vp_flags);
  459. atomic_set(&vha->loop_state, LOOP_DOWN);
  460. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  461. qla2x00_start_timer(vha, WATCH_INTERVAL);
  462. vha->req = base_vha->req;
  463. vha->flags.nvme_enabled = base_vha->flags.nvme_enabled;
  464. host->can_queue = base_vha->req->length + 128;
  465. host->cmd_per_lun = 3;
  466. if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
  467. host->max_cmd_len = 32;
  468. else
  469. host->max_cmd_len = MAX_CMDSZ;
  470. host->max_channel = MAX_BUSES - 1;
  471. host->max_lun = ql2xmaxlun;
  472. host->unique_id = host->host_no;
  473. host->max_id = ha->max_fibre_devices;
  474. host->transportt = qla2xxx_transport_vport_template;
  475. ql_dbg(ql_dbg_vport, vha, 0xa007,
  476. "Detect vport hba %ld at address = %p.\n",
  477. vha->host_no, vha);
  478. vha->flags.init_done = 1;
  479. mutex_lock(&ha->vport_lock);
  480. set_bit(vha->vp_idx, ha->vp_idx_map);
  481. ha->cur_vport_count++;
  482. mutex_unlock(&ha->vport_lock);
  483. return vha;
  484. create_vhost_failed:
  485. return NULL;
  486. }
  487. static void
  488. qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
  489. {
  490. struct qla_hw_data *ha = vha->hw;
  491. uint16_t que_id = req->id;
  492. dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
  493. sizeof(request_t), req->ring, req->dma);
  494. req->ring = NULL;
  495. req->dma = 0;
  496. if (que_id) {
  497. ha->req_q_map[que_id] = NULL;
  498. mutex_lock(&ha->vport_lock);
  499. clear_bit(que_id, ha->req_qid_map);
  500. mutex_unlock(&ha->vport_lock);
  501. }
  502. kfree(req->outstanding_cmds);
  503. kfree(req);
  504. }
  505. static void
  506. qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
  507. {
  508. struct qla_hw_data *ha = vha->hw;
  509. uint16_t que_id = rsp->id;
  510. if (rsp->msix && rsp->msix->have_irq) {
  511. free_irq(rsp->msix->vector, rsp->msix->handle);
  512. rsp->msix->have_irq = 0;
  513. rsp->msix->in_use = 0;
  514. rsp->msix->handle = NULL;
  515. }
  516. dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
  517. sizeof(response_t), rsp->ring, rsp->dma);
  518. rsp->ring = NULL;
  519. rsp->dma = 0;
  520. if (que_id) {
  521. ha->rsp_q_map[que_id] = NULL;
  522. mutex_lock(&ha->vport_lock);
  523. clear_bit(que_id, ha->rsp_qid_map);
  524. mutex_unlock(&ha->vport_lock);
  525. }
  526. kfree(rsp);
  527. }
  528. int
  529. qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
  530. {
  531. int ret = QLA_SUCCESS;
  532. if (req && vha->flags.qpairs_req_created) {
  533. req->options |= BIT_0;
  534. ret = qla25xx_init_req_que(vha, req);
  535. if (ret != QLA_SUCCESS)
  536. return QLA_FUNCTION_FAILED;
  537. qla25xx_free_req_que(vha, req);
  538. }
  539. return ret;
  540. }
  541. int
  542. qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
  543. {
  544. int ret = QLA_SUCCESS;
  545. if (rsp && vha->flags.qpairs_rsp_created) {
  546. rsp->options |= BIT_0;
  547. ret = qla25xx_init_rsp_que(vha, rsp);
  548. if (ret != QLA_SUCCESS)
  549. return QLA_FUNCTION_FAILED;
  550. qla25xx_free_rsp_que(vha, rsp);
  551. }
  552. return ret;
  553. }
  554. /* Delete all queues for a given vhost */
  555. int
  556. qla25xx_delete_queues(struct scsi_qla_host *vha)
  557. {
  558. int cnt, ret = 0;
  559. struct req_que *req = NULL;
  560. struct rsp_que *rsp = NULL;
  561. struct qla_hw_data *ha = vha->hw;
  562. struct qla_qpair *qpair, *tqpair;
  563. if (ql2xmqsupport || ql2xnvmeenable) {
  564. list_for_each_entry_safe(qpair, tqpair, &vha->qp_list,
  565. qp_list_elem)
  566. qla2xxx_delete_qpair(vha, qpair);
  567. } else {
  568. /* Delete request queues */
  569. for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
  570. req = ha->req_q_map[cnt];
  571. if (req && test_bit(cnt, ha->req_qid_map)) {
  572. ret = qla25xx_delete_req_que(vha, req);
  573. if (ret != QLA_SUCCESS) {
  574. ql_log(ql_log_warn, vha, 0x00ea,
  575. "Couldn't delete req que %d.\n",
  576. req->id);
  577. return ret;
  578. }
  579. }
  580. }
  581. /* Delete response queues */
  582. for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
  583. rsp = ha->rsp_q_map[cnt];
  584. if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
  585. ret = qla25xx_delete_rsp_que(vha, rsp);
  586. if (ret != QLA_SUCCESS) {
  587. ql_log(ql_log_warn, vha, 0x00eb,
  588. "Couldn't delete rsp que %d.\n",
  589. rsp->id);
  590. return ret;
  591. }
  592. }
  593. }
  594. }
  595. return ret;
  596. }
  597. int
  598. qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
  599. uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos, bool startqp)
  600. {
  601. int ret = 0;
  602. struct req_que *req = NULL;
  603. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  604. struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
  605. uint16_t que_id = 0;
  606. device_reg_t *reg;
  607. uint32_t cnt;
  608. req = kzalloc_obj(struct req_que);
  609. if (req == NULL) {
  610. ql_log(ql_log_fatal, base_vha, 0x00d9,
  611. "Failed to allocate memory for request queue.\n");
  612. goto failed;
  613. }
  614. req->length = REQUEST_ENTRY_CNT_24XX;
  615. req->ring = dma_alloc_coherent(&ha->pdev->dev,
  616. (req->length + 1) * sizeof(request_t),
  617. &req->dma, GFP_KERNEL);
  618. if (req->ring == NULL) {
  619. ql_log(ql_log_fatal, base_vha, 0x00da,
  620. "Failed to allocate memory for request_ring.\n");
  621. goto que_failed;
  622. }
  623. ret = qla2x00_alloc_outstanding_cmds(ha, req);
  624. if (ret != QLA_SUCCESS)
  625. goto que_failed;
  626. mutex_lock(&ha->mq_lock);
  627. que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
  628. if (que_id >= ha->max_req_queues) {
  629. mutex_unlock(&ha->mq_lock);
  630. ql_log(ql_log_warn, base_vha, 0x00db,
  631. "No resources to create additional request queue.\n");
  632. goto que_failed;
  633. }
  634. set_bit(que_id, ha->req_qid_map);
  635. ha->req_q_map[que_id] = req;
  636. req->rid = rid;
  637. req->vp_idx = vp_idx;
  638. req->qos = qos;
  639. ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
  640. "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
  641. que_id, req->rid, req->vp_idx, req->qos);
  642. ql_dbg(ql_dbg_init, base_vha, 0x00dc,
  643. "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
  644. que_id, req->rid, req->vp_idx, req->qos);
  645. if (rsp_que < 0)
  646. req->rsp = NULL;
  647. else
  648. req->rsp = ha->rsp_q_map[rsp_que];
  649. /* Use alternate PCI bus number */
  650. if (MSB(req->rid))
  651. options |= BIT_4;
  652. /* Use alternate PCI devfn */
  653. if (LSB(req->rid))
  654. options |= BIT_5;
  655. req->options = options;
  656. ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
  657. "options=0x%x.\n", req->options);
  658. ql_dbg(ql_dbg_init, base_vha, 0x00dd,
  659. "options=0x%x.\n", req->options);
  660. for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
  661. req->outstanding_cmds[cnt] = NULL;
  662. req->current_outstanding_cmd = 1;
  663. req->ring_ptr = req->ring;
  664. req->ring_index = 0;
  665. req->cnt = req->length;
  666. req->id = que_id;
  667. reg = ISP_QUE_REG(ha, que_id);
  668. req->req_q_in = &reg->isp25mq.req_q_in;
  669. req->req_q_out = &reg->isp25mq.req_q_out;
  670. req->max_q_depth = ha->req_q_map[0]->max_q_depth;
  671. req->out_ptr = (uint16_t *)(req->ring + req->length);
  672. mutex_unlock(&ha->mq_lock);
  673. ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
  674. "ring_ptr=%p ring_index=%d, "
  675. "cnt=%d id=%d max_q_depth=%d.\n",
  676. req->ring_ptr, req->ring_index,
  677. req->cnt, req->id, req->max_q_depth);
  678. ql_dbg(ql_dbg_init, base_vha, 0x00de,
  679. "ring_ptr=%p ring_index=%d, "
  680. "cnt=%d id=%d max_q_depth=%d.\n",
  681. req->ring_ptr, req->ring_index, req->cnt,
  682. req->id, req->max_q_depth);
  683. if (startqp) {
  684. ret = qla25xx_init_req_que(base_vha, req);
  685. if (ret != QLA_SUCCESS) {
  686. ql_log(ql_log_fatal, base_vha, 0x00df,
  687. "%s failed.\n", __func__);
  688. mutex_lock(&ha->mq_lock);
  689. clear_bit(que_id, ha->req_qid_map);
  690. mutex_unlock(&ha->mq_lock);
  691. goto que_failed;
  692. }
  693. vha->flags.qpairs_req_created = 1;
  694. }
  695. return req->id;
  696. que_failed:
  697. qla25xx_free_req_que(base_vha, req);
  698. failed:
  699. return 0;
  700. }
  701. static void qla_do_work(struct work_struct *work)
  702. {
  703. unsigned long flags;
  704. struct qla_qpair *qpair = container_of(work, struct qla_qpair, q_work);
  705. struct scsi_qla_host *vha = qpair->vha;
  706. spin_lock_irqsave(&qpair->qp_lock, flags);
  707. qla24xx_process_response_queue(vha, qpair->rsp);
  708. spin_unlock_irqrestore(&qpair->qp_lock, flags);
  709. }
  710. /* create response queue */
  711. int
  712. qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
  713. uint8_t vp_idx, uint16_t rid, struct qla_qpair *qpair, bool startqp)
  714. {
  715. int ret = 0;
  716. struct rsp_que *rsp = NULL;
  717. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  718. struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
  719. uint16_t que_id = 0;
  720. device_reg_t *reg;
  721. rsp = kzalloc_obj(struct rsp_que);
  722. if (rsp == NULL) {
  723. ql_log(ql_log_warn, base_vha, 0x0066,
  724. "Failed to allocate memory for response queue.\n");
  725. goto failed;
  726. }
  727. rsp->length = RESPONSE_ENTRY_CNT_MQ;
  728. rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
  729. (rsp->length + 1) * sizeof(response_t),
  730. &rsp->dma, GFP_KERNEL);
  731. if (rsp->ring == NULL) {
  732. ql_log(ql_log_warn, base_vha, 0x00e1,
  733. "Failed to allocate memory for response ring.\n");
  734. goto que_failed;
  735. }
  736. mutex_lock(&ha->mq_lock);
  737. que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
  738. if (que_id >= ha->max_rsp_queues) {
  739. mutex_unlock(&ha->mq_lock);
  740. ql_log(ql_log_warn, base_vha, 0x00e2,
  741. "No resources to create additional request queue.\n");
  742. goto que_failed;
  743. }
  744. set_bit(que_id, ha->rsp_qid_map);
  745. rsp->msix = qpair->msix;
  746. ha->rsp_q_map[que_id] = rsp;
  747. rsp->rid = rid;
  748. rsp->vp_idx = vp_idx;
  749. rsp->hw = ha;
  750. ql_dbg(ql_dbg_init, base_vha, 0x00e4,
  751. "rsp queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
  752. que_id, rsp->rid, rsp->vp_idx, rsp->hw);
  753. /* Use alternate PCI bus number */
  754. if (MSB(rsp->rid))
  755. options |= BIT_4;
  756. /* Use alternate PCI devfn */
  757. if (LSB(rsp->rid))
  758. options |= BIT_5;
  759. /* Enable MSIX handshake mode on for uncapable adapters */
  760. if (!IS_MSIX_NACK_CAPABLE(ha))
  761. options |= BIT_6;
  762. /* Set option to indicate response queue creation */
  763. options |= BIT_1;
  764. rsp->options = options;
  765. rsp->id = que_id;
  766. reg = ISP_QUE_REG(ha, que_id);
  767. rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
  768. rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
  769. rsp->in_ptr = (uint16_t *)(rsp->ring + rsp->length);
  770. mutex_unlock(&ha->mq_lock);
  771. ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
  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. ql_dbg(ql_dbg_init, base_vha, 0x00e5,
  776. "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
  777. rsp->options, rsp->id, rsp->rsp_q_in,
  778. rsp->rsp_q_out);
  779. ret = qla25xx_request_irq(ha, qpair, qpair->msix);
  780. if (ret)
  781. goto que_failed;
  782. if (startqp) {
  783. ret = qla25xx_init_rsp_que(base_vha, rsp);
  784. if (ret != QLA_SUCCESS) {
  785. ql_log(ql_log_fatal, base_vha, 0x00e7,
  786. "%s failed.\n", __func__);
  787. mutex_lock(&ha->mq_lock);
  788. clear_bit(que_id, ha->rsp_qid_map);
  789. mutex_unlock(&ha->mq_lock);
  790. goto que_failed;
  791. }
  792. vha->flags.qpairs_rsp_created = 1;
  793. }
  794. rsp->req = NULL;
  795. qla2x00_init_response_q_entries(rsp);
  796. if (qpair->hw->wq)
  797. INIT_WORK(&qpair->q_work, qla_do_work);
  798. return rsp->id;
  799. que_failed:
  800. qla25xx_free_rsp_que(base_vha, rsp);
  801. failed:
  802. return 0;
  803. }
  804. static void qla_ctrlvp_sp_done(srb_t *sp, int res)
  805. {
  806. if (sp->comp)
  807. complete(sp->comp);
  808. /* don't free sp here. Let the caller do the free */
  809. }
  810. /**
  811. * qla24xx_control_vp() - Enable a virtual port for given host
  812. * @vha: adapter block pointer
  813. * @cmd: command type to be sent for enable virtual port
  814. *
  815. * Return: qla2xxx local function return status code.
  816. */
  817. int qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
  818. {
  819. int rval = QLA_MEMORY_ALLOC_FAILED;
  820. struct qla_hw_data *ha = vha->hw;
  821. int vp_index = vha->vp_idx;
  822. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  823. DECLARE_COMPLETION_ONSTACK(comp);
  824. srb_t *sp;
  825. ql_dbg(ql_dbg_vport, vha, 0x10c1,
  826. "Entered %s cmd %x index %d.\n", __func__, cmd, vp_index);
  827. if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
  828. return QLA_PARAMETER_ERROR;
  829. /* ref: INIT */
  830. sp = qla2x00_get_sp(base_vha, NULL, GFP_KERNEL);
  831. if (!sp)
  832. return rval;
  833. sp->type = SRB_CTRL_VP;
  834. sp->name = "ctrl_vp";
  835. sp->comp = &comp;
  836. qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
  837. qla_ctrlvp_sp_done);
  838. sp->u.iocb_cmd.u.ctrlvp.cmd = cmd;
  839. sp->u.iocb_cmd.u.ctrlvp.vp_index = vp_index;
  840. rval = qla2x00_start_sp(sp);
  841. if (rval != QLA_SUCCESS) {
  842. ql_dbg(ql_dbg_async, vha, 0xffff,
  843. "%s: %s Failed submission. %x.\n",
  844. __func__, sp->name, rval);
  845. goto done;
  846. }
  847. ql_dbg(ql_dbg_vport, vha, 0x113f, "%s hndl %x submitted\n",
  848. sp->name, sp->handle);
  849. wait_for_completion(&comp);
  850. sp->comp = NULL;
  851. rval = sp->rc;
  852. switch (rval) {
  853. case QLA_FUNCTION_TIMEOUT:
  854. ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Timeout. %x.\n",
  855. __func__, sp->name, rval);
  856. break;
  857. case QLA_SUCCESS:
  858. ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s done.\n",
  859. __func__, sp->name);
  860. break;
  861. default:
  862. ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Failed. %x.\n",
  863. __func__, sp->name, rval);
  864. break;
  865. }
  866. done:
  867. /* ref: INIT */
  868. kref_put(&sp->cmd_kref, qla2x00_sp_release);
  869. return rval;
  870. }
  871. struct scsi_qla_host *qla_find_host_by_vp_idx(struct scsi_qla_host *vha, uint16_t vp_idx)
  872. {
  873. struct qla_hw_data *ha = vha->hw;
  874. if (vha->vp_idx == vp_idx)
  875. return vha;
  876. BUG_ON(ha->vp_map == NULL);
  877. if (likely(test_bit(vp_idx, ha->vp_idx_map)))
  878. return ha->vp_map[vp_idx].vha;
  879. return NULL;
  880. }
  881. /* vport_slock to be held by the caller */
  882. void
  883. qla_update_vp_map(struct scsi_qla_host *vha, int cmd)
  884. {
  885. void *slot;
  886. u32 key;
  887. int rc;
  888. if (!vha->hw->vp_map)
  889. return;
  890. key = vha->d_id.b24;
  891. switch (cmd) {
  892. case SET_VP_IDX:
  893. vha->hw->vp_map[vha->vp_idx].vha = vha;
  894. break;
  895. case SET_AL_PA:
  896. slot = btree_lookup32(&vha->hw->host_map, key);
  897. if (!slot) {
  898. ql_dbg(ql_dbg_disc, vha, 0xf018,
  899. "Save vha in host_map %p %06x\n", vha, key);
  900. rc = btree_insert32(&vha->hw->host_map,
  901. key, vha, GFP_ATOMIC);
  902. if (rc)
  903. ql_log(ql_log_info, vha, 0xd03e,
  904. "Unable to insert s_id into host_map: %06x\n",
  905. key);
  906. return;
  907. }
  908. ql_dbg(ql_dbg_disc, vha, 0xf019,
  909. "replace existing vha in host_map %p %06x\n", vha, key);
  910. btree_update32(&vha->hw->host_map, key, vha);
  911. break;
  912. case RESET_VP_IDX:
  913. vha->hw->vp_map[vha->vp_idx].vha = NULL;
  914. break;
  915. case RESET_AL_PA:
  916. ql_dbg(ql_dbg_disc, vha, 0xf01a,
  917. "clear vha in host_map %p %06x\n", vha, key);
  918. slot = btree_lookup32(&vha->hw->host_map, key);
  919. if (slot)
  920. btree_remove32(&vha->hw->host_map, key);
  921. vha->d_id.b24 = 0;
  922. break;
  923. }
  924. }
  925. void qla_update_host_map(struct scsi_qla_host *vha, port_id_t id)
  926. {
  927. if (!vha->d_id.b24) {
  928. vha->d_id = id;
  929. qla_update_vp_map(vha, SET_AL_PA);
  930. } else if (vha->d_id.b24 != id.b24) {
  931. qla_update_vp_map(vha, RESET_AL_PA);
  932. vha->d_id = id;
  933. qla_update_vp_map(vha, SET_AL_PA);
  934. }
  935. }
  936. int qla_create_buf_pool(struct scsi_qla_host *vha, struct qla_qpair *qp)
  937. {
  938. int sz;
  939. qp->buf_pool.num_bufs = qp->req->length;
  940. sz = BITS_TO_LONGS(qp->req->length);
  941. qp->buf_pool.buf_map = kcalloc(sz, sizeof(long), GFP_KERNEL);
  942. if (!qp->buf_pool.buf_map) {
  943. ql_log(ql_log_warn, vha, 0x0186,
  944. "Failed to allocate buf_map(%zd).\n", sz * sizeof(unsigned long));
  945. return -ENOMEM;
  946. }
  947. sz = qp->req->length * sizeof(void *);
  948. qp->buf_pool.buf_array = kcalloc(qp->req->length, sizeof(void *), GFP_KERNEL);
  949. if (!qp->buf_pool.buf_array) {
  950. ql_log(ql_log_warn, vha, 0x0186,
  951. "Failed to allocate buf_array(%d).\n", sz);
  952. kfree(qp->buf_pool.buf_map);
  953. return -ENOMEM;
  954. }
  955. sz = qp->req->length * sizeof(dma_addr_t);
  956. qp->buf_pool.dma_array = kzalloc_objs(dma_addr_t, qp->req->length);
  957. if (!qp->buf_pool.dma_array) {
  958. ql_log(ql_log_warn, vha, 0x0186,
  959. "Failed to allocate dma_array(%d).\n", sz);
  960. kfree(qp->buf_pool.buf_map);
  961. kfree(qp->buf_pool.buf_array);
  962. return -ENOMEM;
  963. }
  964. set_bit(0, qp->buf_pool.buf_map);
  965. return 0;
  966. }
  967. void qla_free_buf_pool(struct qla_qpair *qp)
  968. {
  969. int i;
  970. struct qla_hw_data *ha = qp->vha->hw;
  971. for (i = 0; i < qp->buf_pool.num_bufs; i++) {
  972. if (qp->buf_pool.buf_array[i] && qp->buf_pool.dma_array[i])
  973. dma_pool_free(ha->fcp_cmnd_dma_pool, qp->buf_pool.buf_array[i],
  974. qp->buf_pool.dma_array[i]);
  975. qp->buf_pool.buf_array[i] = NULL;
  976. qp->buf_pool.dma_array[i] = 0;
  977. }
  978. kfree(qp->buf_pool.dma_array);
  979. kfree(qp->buf_pool.buf_array);
  980. kfree(qp->buf_pool.buf_map);
  981. }
  982. /* it is assume qp->qp_lock is held at this point */
  983. int qla_get_buf(struct scsi_qla_host *vha, struct qla_qpair *qp, struct qla_buf_dsc *dsc)
  984. {
  985. u16 tag, i = 0;
  986. void *buf;
  987. dma_addr_t buf_dma;
  988. struct qla_hw_data *ha = vha->hw;
  989. dsc->tag = TAG_FREED;
  990. again:
  991. tag = find_first_zero_bit(qp->buf_pool.buf_map, qp->buf_pool.num_bufs);
  992. if (tag >= qp->buf_pool.num_bufs) {
  993. ql_dbg(ql_dbg_io, vha, 0x00e2,
  994. "qp(%d) ran out of buf resource.\n", qp->id);
  995. return -EIO;
  996. }
  997. if (tag == 0) {
  998. set_bit(0, qp->buf_pool.buf_map);
  999. i++;
  1000. if (i == 5) {
  1001. ql_dbg(ql_dbg_io, vha, 0x00e3,
  1002. "qp(%d) unable to get tag.\n", qp->id);
  1003. return -EIO;
  1004. }
  1005. goto again;
  1006. }
  1007. if (!qp->buf_pool.buf_array[tag]) {
  1008. buf = dma_pool_zalloc(ha->fcp_cmnd_dma_pool, GFP_ATOMIC, &buf_dma);
  1009. if (!buf) {
  1010. ql_log(ql_log_fatal, vha, 0x13b1,
  1011. "Failed to allocate buf.\n");
  1012. return -ENOMEM;
  1013. }
  1014. dsc->buf = qp->buf_pool.buf_array[tag] = buf;
  1015. dsc->buf_dma = qp->buf_pool.dma_array[tag] = buf_dma;
  1016. qp->buf_pool.num_alloc++;
  1017. } else {
  1018. dsc->buf = qp->buf_pool.buf_array[tag];
  1019. dsc->buf_dma = qp->buf_pool.dma_array[tag];
  1020. memset(dsc->buf, 0, FCP_CMND_DMA_POOL_SIZE);
  1021. }
  1022. qp->buf_pool.num_active++;
  1023. if (qp->buf_pool.num_active > qp->buf_pool.max_used)
  1024. qp->buf_pool.max_used = qp->buf_pool.num_active;
  1025. dsc->tag = tag;
  1026. set_bit(tag, qp->buf_pool.buf_map);
  1027. return 0;
  1028. }
  1029. static void qla_trim_buf(struct qla_qpair *qp, u16 trim)
  1030. {
  1031. int i, j;
  1032. struct qla_hw_data *ha = qp->vha->hw;
  1033. if (!trim)
  1034. return;
  1035. for (i = 0; i < trim; i++) {
  1036. j = qp->buf_pool.num_alloc - 1;
  1037. if (test_bit(j, qp->buf_pool.buf_map)) {
  1038. ql_dbg(ql_dbg_io + ql_dbg_verbose, qp->vha, 0x300b,
  1039. "QP id(%d): trim active buf[%d]. Remain %d bufs\n",
  1040. qp->id, j, qp->buf_pool.num_alloc);
  1041. return;
  1042. }
  1043. if (qp->buf_pool.buf_array[j]) {
  1044. dma_pool_free(ha->fcp_cmnd_dma_pool, qp->buf_pool.buf_array[j],
  1045. qp->buf_pool.dma_array[j]);
  1046. qp->buf_pool.buf_array[j] = NULL;
  1047. qp->buf_pool.dma_array[j] = 0;
  1048. }
  1049. qp->buf_pool.num_alloc--;
  1050. if (!qp->buf_pool.num_alloc)
  1051. break;
  1052. }
  1053. ql_dbg(ql_dbg_io + ql_dbg_verbose, qp->vha, 0x3010,
  1054. "QP id(%d): trimmed %d bufs. Remain %d bufs\n",
  1055. qp->id, trim, qp->buf_pool.num_alloc);
  1056. }
  1057. static void __qla_adjust_buf(struct qla_qpair *qp)
  1058. {
  1059. u32 trim;
  1060. qp->buf_pool.take_snapshot = 0;
  1061. qp->buf_pool.prev_max = qp->buf_pool.max_used;
  1062. qp->buf_pool.max_used = qp->buf_pool.num_active;
  1063. if (qp->buf_pool.prev_max > qp->buf_pool.max_used &&
  1064. qp->buf_pool.num_alloc > qp->buf_pool.max_used) {
  1065. /* down trend */
  1066. trim = qp->buf_pool.num_alloc - qp->buf_pool.max_used;
  1067. trim = (trim * 10) / 100;
  1068. trim = trim ? trim : 1;
  1069. qla_trim_buf(qp, trim);
  1070. } else if (!qp->buf_pool.prev_max && !qp->buf_pool.max_used) {
  1071. /* 2 periods of no io */
  1072. qla_trim_buf(qp, qp->buf_pool.num_alloc);
  1073. }
  1074. }
  1075. /* it is assume qp->qp_lock is held at this point */
  1076. void qla_put_buf(struct qla_qpair *qp, struct qla_buf_dsc *dsc)
  1077. {
  1078. if (dsc->tag == TAG_FREED)
  1079. return;
  1080. lockdep_assert_held(qp->qp_lock_ptr);
  1081. clear_bit(dsc->tag, qp->buf_pool.buf_map);
  1082. qp->buf_pool.num_active--;
  1083. dsc->tag = TAG_FREED;
  1084. if (qp->buf_pool.take_snapshot)
  1085. __qla_adjust_buf(qp);
  1086. }
  1087. #define EXPIRE (60 * HZ)
  1088. void qla_adjust_buf(struct scsi_qla_host *vha)
  1089. {
  1090. unsigned long flags;
  1091. int i;
  1092. struct qla_qpair *qp;
  1093. if (vha->vp_idx)
  1094. return;
  1095. if (!vha->buf_expired) {
  1096. vha->buf_expired = jiffies + EXPIRE;
  1097. return;
  1098. }
  1099. if (time_before(jiffies, vha->buf_expired))
  1100. return;
  1101. vha->buf_expired = jiffies + EXPIRE;
  1102. for (i = 0; i < vha->hw->num_qpairs; i++) {
  1103. qp = vha->hw->queue_pair_map[i];
  1104. if (!qp)
  1105. continue;
  1106. if (!qp->buf_pool.num_alloc)
  1107. continue;
  1108. if (qp->buf_pool.take_snapshot) {
  1109. /* no io has gone through in the last EXPIRE period */
  1110. spin_lock_irqsave(qp->qp_lock_ptr, flags);
  1111. __qla_adjust_buf(qp);
  1112. spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
  1113. } else {
  1114. qp->buf_pool.take_snapshot = 1;
  1115. }
  1116. }
  1117. }