qla_mid.c 10 KB

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