qla_mid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2008 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 <linux/moduleparam.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/smp_lock.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. del_timer_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. /* Find an empty slot and assign an vp_id */
  30. mutex_lock(&ha->vport_lock);
  31. vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
  32. if (vp_id > ha->max_npiv_vports) {
  33. DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
  34. vp_id, ha->max_npiv_vports));
  35. mutex_unlock(&ha->vport_lock);
  36. return vp_id;
  37. }
  38. set_bit(vp_id, ha->vp_idx_map);
  39. ha->num_vhosts++;
  40. ha->cur_vport_count++;
  41. vha->vp_idx = vp_id;
  42. list_add_tail(&vha->list, &ha->vp_list);
  43. mutex_unlock(&ha->vport_lock);
  44. return vp_id;
  45. }
  46. void
  47. qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
  48. {
  49. uint16_t vp_id;
  50. struct qla_hw_data *ha = vha->hw;
  51. mutex_lock(&ha->vport_lock);
  52. vp_id = vha->vp_idx;
  53. ha->num_vhosts--;
  54. ha->cur_vport_count--;
  55. clear_bit(vp_id, ha->vp_idx_map);
  56. list_del(&vha->list);
  57. mutex_unlock(&ha->vport_lock);
  58. }
  59. static scsi_qla_host_t *
  60. qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
  61. {
  62. scsi_qla_host_t *vha;
  63. /* Locate matching device in database. */
  64. list_for_each_entry(vha, &ha->vp_list, list) {
  65. if (!memcmp(port_name, vha->port_name, WWN_SIZE))
  66. return vha;
  67. }
  68. return NULL;
  69. }
  70. /*
  71. * qla2x00_mark_vp_devices_dead
  72. * Updates fcport state when device goes offline.
  73. *
  74. * Input:
  75. * ha = adapter block pointer.
  76. * fcport = port structure pointer.
  77. *
  78. * Return:
  79. * None.
  80. *
  81. * Context:
  82. */
  83. static void
  84. qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
  85. {
  86. fc_port_t *fcport;
  87. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  88. DEBUG15(printk("scsi(%ld): Marking port dead, "
  89. "loop_id=0x%04x :%x\n",
  90. vha->host_no, fcport->loop_id, fcport->vp_idx));
  91. qla2x00_mark_device_lost(vha, fcport, 0, 0);
  92. atomic_set(&fcport->state, FCS_UNCONFIGURED);
  93. }
  94. }
  95. int
  96. qla24xx_disable_vp(scsi_qla_host_t *vha)
  97. {
  98. int ret;
  99. ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
  100. atomic_set(&vha->loop_state, LOOP_DOWN);
  101. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  102. qla2x00_mark_vp_devices_dead(vha);
  103. atomic_set(&vha->vp_state, VP_FAILED);
  104. vha->flags.management_server_logged_in = 0;
  105. if (ret == QLA_SUCCESS) {
  106. fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
  107. } else {
  108. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. int
  114. qla24xx_enable_vp(scsi_qla_host_t *vha)
  115. {
  116. int ret;
  117. struct qla_hw_data *ha = vha->hw;
  118. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  119. /* Check if physical ha port is Up */
  120. if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
  121. atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
  122. vha->vp_err_state = VP_ERR_PORTDWN;
  123. fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
  124. goto enable_failed;
  125. }
  126. /* Initialize the new vport unless it is a persistent port */
  127. mutex_lock(&ha->vport_lock);
  128. ret = qla24xx_modify_vp_config(vha);
  129. mutex_unlock(&ha->vport_lock);
  130. if (ret != QLA_SUCCESS) {
  131. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  132. goto enable_failed;
  133. }
  134. DEBUG15(qla_printk(KERN_INFO, ha,
  135. "Virtual port with id: %d - Enabled\n", vha->vp_idx));
  136. return 0;
  137. enable_failed:
  138. DEBUG15(qla_printk(KERN_INFO, ha,
  139. "Virtual port with id: %d - Disabled\n", vha->vp_idx));
  140. return 1;
  141. }
  142. static void
  143. qla24xx_configure_vp(scsi_qla_host_t *vha)
  144. {
  145. struct fc_vport *fc_vport;
  146. int ret;
  147. fc_vport = vha->fc_vport;
  148. DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
  149. vha->host_no, __func__));
  150. ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
  151. if (ret != QLA_SUCCESS) {
  152. DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
  153. "receiving of RSCN requests: 0x%x\n", ret));
  154. return;
  155. } else {
  156. /* Corresponds to SCR enabled */
  157. clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
  158. }
  159. vha->flags.online = 1;
  160. if (qla24xx_configure_vhba(vha))
  161. return;
  162. atomic_set(&vha->vp_state, VP_ACTIVE);
  163. fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
  164. }
  165. void
  166. qla2x00_alert_all_vps(struct qla_hw_data *ha, uint16_t *mb)
  167. {
  168. scsi_qla_host_t *vha;
  169. int i = 0;
  170. list_for_each_entry(vha, &ha->vp_list, list) {
  171. if (vha->vp_idx) {
  172. switch (mb[0]) {
  173. case MBA_LIP_OCCURRED:
  174. case MBA_LOOP_UP:
  175. case MBA_LOOP_DOWN:
  176. case MBA_LIP_RESET:
  177. case MBA_POINT_TO_POINT:
  178. case MBA_CHG_IN_CONNECTION:
  179. case MBA_PORT_UPDATE:
  180. case MBA_RSCN_UPDATE:
  181. DEBUG15(printk("scsi(%ld)%s: Async_event for"
  182. " VP[%d], mb = 0x%x, vha=%p\n",
  183. vha->host_no, __func__, i, *mb, vha));
  184. qla2x00_async_event(vha, mb);
  185. break;
  186. }
  187. }
  188. i++;
  189. }
  190. }
  191. int
  192. qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
  193. {
  194. /*
  195. * Physical port will do most of the abort and recovery work. We can
  196. * just treat it as a loop down
  197. */
  198. if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
  199. atomic_set(&vha->loop_state, LOOP_DOWN);
  200. qla2x00_mark_all_devices_lost(vha, 0);
  201. } else {
  202. if (!atomic_read(&vha->loop_down_timer))
  203. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  204. }
  205. /* To exclusively reset vport, we need to log it out first.*/
  206. if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
  207. qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
  208. DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
  209. vha->host_no, vha->vp_idx));
  210. return qla24xx_enable_vp(vha);
  211. }
  212. static int
  213. qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
  214. {
  215. struct qla_hw_data *ha = vha->hw;
  216. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  217. if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
  218. /* VP acquired. complete port configuration */
  219. if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
  220. qla24xx_configure_vp(vha);
  221. } else {
  222. set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
  223. set_bit(VP_DPC_NEEDED, &base_vha->dpc_flags);
  224. }
  225. return 0;
  226. }
  227. if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
  228. qla2x00_update_fcports(vha);
  229. clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
  230. }
  231. if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
  232. !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
  233. atomic_read(&vha->loop_state) != LOOP_DOWN) {
  234. DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
  235. vha->host_no));
  236. qla2x00_relogin(vha);
  237. DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
  238. vha->host_no));
  239. }
  240. if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
  241. (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
  242. clear_bit(RESET_ACTIVE, &vha->dpc_flags);
  243. }
  244. if (atomic_read(&vha->vp_state) == VP_ACTIVE &&
  245. test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
  246. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
  247. qla2x00_loop_resync(vha);
  248. clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
  249. }
  250. }
  251. return 0;
  252. }
  253. void
  254. qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
  255. {
  256. int ret;
  257. struct qla_hw_data *ha = vha->hw;
  258. scsi_qla_host_t *vp;
  259. if (vha->vp_idx)
  260. return;
  261. if (list_empty(&ha->vp_list))
  262. return;
  263. clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
  264. list_for_each_entry(vp, &ha->vp_list, list) {
  265. if (vp->vp_idx)
  266. ret = qla2x00_do_dpc_vp(vp);
  267. }
  268. }
  269. int
  270. qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
  271. {
  272. scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
  273. struct qla_hw_data *ha = base_vha->hw;
  274. scsi_qla_host_t *vha;
  275. uint8_t port_name[WWN_SIZE];
  276. if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
  277. return VPCERR_UNSUPPORTED;
  278. /* Check up the F/W and H/W support NPIV */
  279. if (!ha->flags.npiv_supported)
  280. return VPCERR_UNSUPPORTED;
  281. /* Check up whether npiv supported switch presented */
  282. if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
  283. return VPCERR_NO_FABRIC_SUPP;
  284. /* Check up unique WWPN */
  285. u64_to_wwn(fc_vport->port_name, port_name);
  286. if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
  287. return VPCERR_BAD_WWN;
  288. vha = qla24xx_find_vhost_by_name(ha, port_name);
  289. if (vha)
  290. return VPCERR_BAD_WWN;
  291. /* Check up max-npiv-supports */
  292. if (ha->num_vhosts > ha->max_npiv_vports) {
  293. DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
  294. "max_npv_vports %ud.\n", base_vha->host_no,
  295. ha->num_vhosts, ha->max_npiv_vports));
  296. return VPCERR_UNSUPPORTED;
  297. }
  298. return 0;
  299. }
  300. scsi_qla_host_t *
  301. qla24xx_create_vhost(struct fc_vport *fc_vport)
  302. {
  303. scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
  304. struct qla_hw_data *ha = base_vha->hw;
  305. scsi_qla_host_t *vha;
  306. struct scsi_host_template *sht = &qla24xx_driver_template;
  307. struct Scsi_Host *host;
  308. vha = qla2x00_create_host(sht, ha);
  309. if (!vha) {
  310. DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
  311. return(NULL);
  312. }
  313. host = vha->host;
  314. fc_vport->dd_data = vha;
  315. /* New host info */
  316. u64_to_wwn(fc_vport->node_name, vha->node_name);
  317. u64_to_wwn(fc_vport->port_name, vha->port_name);
  318. vha->fc_vport = fc_vport;
  319. vha->device_flags = 0;
  320. vha->vp_idx = qla24xx_allocate_vp_id(vha);
  321. if (vha->vp_idx > ha->max_npiv_vports) {
  322. DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
  323. vha->host_no));
  324. goto create_vhost_failed;
  325. }
  326. vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
  327. vha->dpc_flags = 0L;
  328. set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
  329. set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
  330. /*
  331. * To fix the issue of processing a parent's RSCN for the vport before
  332. * its SCR is complete.
  333. */
  334. set_bit(VP_SCR_NEEDED, &vha->vp_flags);
  335. atomic_set(&vha->loop_state, LOOP_DOWN);
  336. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  337. qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
  338. host->can_queue = ha->req->length + 128;
  339. host->this_id = 255;
  340. host->cmd_per_lun = 3;
  341. host->max_cmd_len = MAX_CMDSZ;
  342. host->max_channel = MAX_BUSES - 1;
  343. host->max_lun = MAX_LUNS;
  344. host->unique_id = host->host_no;
  345. host->max_id = MAX_TARGETS_2200;
  346. host->transportt = qla2xxx_transport_vport_template;
  347. DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
  348. vha->host_no, vha));
  349. vha->flags.init_done = 1;
  350. return vha;
  351. create_vhost_failed:
  352. return NULL;
  353. }