qla_mid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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/version.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->parent && 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. scsi_qla_host_t *ha = vha->parent;
  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->vp_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. scsi_qla_host_t *ha = vha->parent;
  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->vp_list);
  57. mutex_unlock(&ha->vport_lock);
  58. }
  59. static scsi_qla_host_t *
  60. qla24xx_find_vhost_by_name(scsi_qla_host_t *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, vp_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. scsi_qla_host_t *pha = to_qla_parent(vha);
  88. list_for_each_entry(fcport, &pha->fcports, list) {
  89. if (fcport->vp_idx != vha->vp_idx)
  90. continue;
  91. DEBUG15(printk("scsi(%ld): Marking port dead, "
  92. "loop_id=0x%04x :%x\n",
  93. vha->host_no, fcport->loop_id, fcport->vp_idx));
  94. qla2x00_mark_device_lost(vha, fcport, 0, 0);
  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 (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
  240. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
  241. qla2x00_loop_resync(vha);
  242. clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
  243. }
  244. }
  245. return 0;
  246. }
  247. void
  248. qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
  249. {
  250. int ret;
  251. int i, vp_idx_matched;
  252. scsi_qla_host_t *vha;
  253. if (ha->parent)
  254. return;
  255. if (list_empty(&ha->vp_list))
  256. return;
  257. clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
  258. for_each_mapped_vp_idx(ha, i) {
  259. vp_idx_matched = 0;
  260. list_for_each_entry(vha, &ha->vp_list, vp_list) {
  261. if (i == vha->vp_idx) {
  262. vp_idx_matched = 1;
  263. break;
  264. }
  265. }
  266. if (vp_idx_matched)
  267. ret = qla2x00_do_dpc_vp(vha);
  268. }
  269. }
  270. int
  271. qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
  272. {
  273. scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
  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, ha->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", ha->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 *ha = shost_priv(fc_vport->shost);
  304. scsi_qla_host_t *vha;
  305. struct Scsi_Host *host;
  306. host = scsi_host_alloc(&qla24xx_driver_template,
  307. sizeof(scsi_qla_host_t));
  308. if (!host) {
  309. printk(KERN_WARNING
  310. "qla2xxx: scsi_host_alloc() failed for vport\n");
  311. return(NULL);
  312. }
  313. vha = shost_priv(host);
  314. /* clone the parent hba */
  315. memcpy(vha, ha, sizeof (scsi_qla_host_t));
  316. fc_vport->dd_data = vha;
  317. vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
  318. if (!vha->node_name)
  319. goto create_vhost_failed_1;
  320. vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
  321. if (!vha->port_name)
  322. goto create_vhost_failed_2;
  323. /* New host info */
  324. u64_to_wwn(fc_vport->node_name, vha->node_name);
  325. u64_to_wwn(fc_vport->port_name, vha->port_name);
  326. vha->host = host;
  327. vha->host_no = host->host_no;
  328. vha->parent = ha;
  329. vha->fc_vport = fc_vport;
  330. vha->device_flags = 0;
  331. vha->vp_idx = qla24xx_allocate_vp_id(vha);
  332. if (vha->vp_idx > ha->max_npiv_vports) {
  333. DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
  334. vha->host_no));
  335. goto create_vhost_failed_3;
  336. }
  337. vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
  338. init_completion(&vha->mbx_cmd_comp);
  339. complete(&vha->mbx_cmd_comp);
  340. init_completion(&vha->mbx_intr_comp);
  341. INIT_LIST_HEAD(&vha->list);
  342. INIT_LIST_HEAD(&vha->fcports);
  343. INIT_LIST_HEAD(&vha->vp_fcports);
  344. INIT_LIST_HEAD(&vha->work_list);
  345. vha->dpc_flags = 0L;
  346. set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
  347. set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
  348. /*
  349. * To fix the issue of processing a parent's RSCN for the vport before
  350. * its SCR is complete.
  351. */
  352. set_bit(VP_SCR_NEEDED, &vha->vp_flags);
  353. atomic_set(&vha->loop_state, LOOP_DOWN);
  354. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  355. qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
  356. host->can_queue = vha->request_q_length + 128;
  357. host->this_id = 255;
  358. host->cmd_per_lun = 3;
  359. host->max_cmd_len = MAX_CMDSZ;
  360. host->max_channel = MAX_BUSES - 1;
  361. host->max_lun = MAX_LUNS;
  362. host->unique_id = host->host_no;
  363. host->max_id = MAX_TARGETS_2200;
  364. host->transportt = qla2xxx_transport_vport_template;
  365. DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
  366. vha->host_no, vha));
  367. vha->flags.init_done = 1;
  368. return vha;
  369. create_vhost_failed_3:
  370. kfree(vha->port_name);
  371. create_vhost_failed_2:
  372. kfree(vha->node_name);
  373. create_vhost_failed_1:
  374. return NULL;
  375. }