qla_mid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. 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. clear_bit(vp_id, ha->vp_idx_map);
  54. list_del(&vha->vp_list);
  55. mutex_unlock(&ha->vport_lock);
  56. }
  57. static scsi_qla_host_t *
  58. qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
  59. {
  60. scsi_qla_host_t *vha;
  61. /* Locate matching device in database. */
  62. list_for_each_entry(vha, &ha->vp_list, vp_list) {
  63. if (!memcmp(port_name, vha->port_name, WWN_SIZE))
  64. return vha;
  65. }
  66. return NULL;
  67. }
  68. /*
  69. * qla2x00_mark_vp_devices_dead
  70. * Updates fcport state when device goes offline.
  71. *
  72. * Input:
  73. * ha = adapter block pointer.
  74. * fcport = port structure pointer.
  75. *
  76. * Return:
  77. * None.
  78. *
  79. * Context:
  80. */
  81. static void
  82. qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
  83. {
  84. fc_port_t *fcport;
  85. scsi_qla_host_t *pha = to_qla_parent(vha);
  86. list_for_each_entry(fcport, &pha->fcports, list) {
  87. if (fcport->vp_idx != vha->vp_idx)
  88. continue;
  89. DEBUG15(printk("scsi(%ld): Marking port dead, "
  90. "loop_id=0x%04x :%x\n",
  91. vha->host_no, fcport->loop_id, fcport->vp_idx));
  92. atomic_set(&fcport->state, FCS_DEVICE_DEAD);
  93. qla2x00_mark_device_lost(vha, fcport, 0, 0);
  94. }
  95. }
  96. int
  97. qla24xx_disable_vp(scsi_qla_host_t *vha)
  98. {
  99. int ret;
  100. ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
  101. atomic_set(&vha->loop_state, LOOP_DOWN);
  102. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  103. /* Delete all vp's fcports from parent's list */
  104. qla2x00_mark_vp_devices_dead(vha);
  105. atomic_set(&vha->vp_state, VP_FAILED);
  106. vha->flags.management_server_logged_in = 0;
  107. if (ret == QLA_SUCCESS) {
  108. fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
  109. } else {
  110. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. int
  116. qla24xx_enable_vp(scsi_qla_host_t *vha)
  117. {
  118. int ret;
  119. scsi_qla_host_t *ha = vha->parent;
  120. /* Check if physical ha port is Up */
  121. if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
  122. atomic_read(&ha->loop_state) == LOOP_DEAD ) {
  123. vha->vp_err_state = VP_ERR_PORTDWN;
  124. fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
  125. goto enable_failed;
  126. }
  127. /* Initialize the new vport unless it is a persistent port */
  128. mutex_lock(&ha->vport_lock);
  129. ret = qla24xx_modify_vp_config(vha);
  130. mutex_unlock(&ha->vport_lock);
  131. if (ret != QLA_SUCCESS) {
  132. fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
  133. goto enable_failed;
  134. }
  135. DEBUG15(qla_printk(KERN_INFO, ha,
  136. "Virtual port with id: %d - Enabled\n", vha->vp_idx));
  137. return 0;
  138. enable_failed:
  139. DEBUG15(qla_printk(KERN_INFO, ha,
  140. "Virtual port with id: %d - Disabled\n", vha->vp_idx));
  141. return 1;
  142. }
  143. static void
  144. qla24xx_configure_vp(scsi_qla_host_t *vha)
  145. {
  146. struct fc_vport *fc_vport;
  147. int ret;
  148. fc_vport = vha->fc_vport;
  149. DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
  150. vha->host_no, __func__));
  151. ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
  152. if (ret != QLA_SUCCESS) {
  153. DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
  154. " of RSCN requests: 0x%x\n", ret));
  155. return;
  156. } else {
  157. /* Corresponds to SCR enabled */
  158. clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
  159. }
  160. vha->flags.online = 1;
  161. if (qla24xx_configure_vhba(vha))
  162. return;
  163. atomic_set(&vha->vp_state, VP_ACTIVE);
  164. fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
  165. }
  166. void
  167. qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
  168. {
  169. int i, vp_idx_matched;
  170. scsi_qla_host_t *vha;
  171. if (ha->parent)
  172. return;
  173. for_each_mapped_vp_idx(ha, i) {
  174. vp_idx_matched = 0;
  175. list_for_each_entry(vha, &ha->vp_list, vp_list) {
  176. if (i == vha->vp_idx) {
  177. vp_idx_matched = 1;
  178. break;
  179. }
  180. }
  181. if (vp_idx_matched) {
  182. switch (mb[0]) {
  183. case MBA_LIP_OCCURRED:
  184. case MBA_LOOP_UP:
  185. case MBA_LOOP_DOWN:
  186. case MBA_LIP_RESET:
  187. case MBA_POINT_TO_POINT:
  188. case MBA_CHG_IN_CONNECTION:
  189. case MBA_PORT_UPDATE:
  190. case MBA_RSCN_UPDATE:
  191. DEBUG15(printk("scsi(%ld)%s: Async_event for"
  192. " VP[%d], mb = 0x%x, vha=%p\n",
  193. vha->host_no, __func__,i, *mb, vha));
  194. qla2x00_async_event(vha, mb);
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. void
  201. qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
  202. {
  203. /*
  204. * Physical port will do most of the abort and recovery work. We can
  205. * just treat it as a loop down
  206. */
  207. if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
  208. atomic_set(&vha->loop_state, LOOP_DOWN);
  209. qla2x00_mark_all_devices_lost(vha, 0);
  210. } else {
  211. if (!atomic_read(&vha->loop_down_timer))
  212. atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
  213. }
  214. DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
  215. vha->host_no, vha->vp_idx));
  216. qla24xx_enable_vp(vha);
  217. }
  218. static int
  219. qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
  220. {
  221. scsi_qla_host_t *ha = vha->parent;
  222. if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
  223. /* VP acquired. complete port configuration */
  224. if (atomic_read(&ha->loop_state) == LOOP_READY) {
  225. qla24xx_configure_vp(vha);
  226. } else {
  227. set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
  228. set_bit(VP_DPC_NEEDED, &ha->dpc_flags);
  229. }
  230. return 0;
  231. }
  232. if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
  233. qla2x00_vp_abort_isp(vha);
  234. if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
  235. (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
  236. clear_bit(RESET_ACTIVE, &vha->dpc_flags);
  237. }
  238. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
  239. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
  240. qla2x00_loop_resync(vha);
  241. clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
  242. }
  243. }
  244. return 0;
  245. }
  246. void
  247. qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
  248. {
  249. int ret;
  250. int i, vp_idx_matched;
  251. scsi_qla_host_t *vha;
  252. if (ha->parent)
  253. return;
  254. if (list_empty(&ha->vp_list))
  255. return;
  256. clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
  257. for_each_mapped_vp_idx(ha, i) {
  258. vp_idx_matched = 0;
  259. list_for_each_entry(vha, &ha->vp_list, vp_list) {
  260. if (i == vha->vp_idx) {
  261. vp_idx_matched = 1;
  262. break;
  263. }
  264. }
  265. if (vp_idx_matched)
  266. ret = qla2x00_do_dpc_vp(vha);
  267. }
  268. }
  269. int
  270. qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
  271. {
  272. scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
  273. scsi_qla_host_t *vha;
  274. uint8_t port_name[WWN_SIZE];
  275. if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
  276. return VPCERR_UNSUPPORTED;
  277. /* Check up the F/W and H/W support NPIV */
  278. if (!ha->flags.npiv_supported)
  279. return VPCERR_UNSUPPORTED;
  280. /* Check up whether npiv supported switch presented */
  281. if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
  282. return VPCERR_NO_FABRIC_SUPP;
  283. /* Check up unique WWPN */
  284. u64_to_wwn(fc_vport->port_name, port_name);
  285. if (!memcmp(port_name, ha->port_name, WWN_SIZE))
  286. return VPCERR_BAD_WWN;
  287. vha = qla24xx_find_vhost_by_name(ha, port_name);
  288. if (vha)
  289. return VPCERR_BAD_WWN;
  290. /* Check up max-npiv-supports */
  291. if (ha->num_vhosts > ha->max_npiv_vports) {
  292. DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
  293. "max_npv_vports %ud.\n", ha->host_no,
  294. ha->num_vhosts, ha->max_npiv_vports));
  295. return VPCERR_UNSUPPORTED;
  296. }
  297. return 0;
  298. }
  299. scsi_qla_host_t *
  300. qla24xx_create_vhost(struct fc_vport *fc_vport)
  301. {
  302. scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
  303. scsi_qla_host_t *vha;
  304. struct Scsi_Host *host;
  305. host = scsi_host_alloc(&qla24xx_driver_template,
  306. sizeof(scsi_qla_host_t));
  307. if (!host) {
  308. printk(KERN_WARNING
  309. "qla2xxx: scsi_host_alloc() failed for vport\n");
  310. return(NULL);
  311. }
  312. vha = shost_priv(host);
  313. /* clone the parent hba */
  314. memcpy(vha, ha, sizeof (scsi_qla_host_t));
  315. fc_vport->dd_data = vha;
  316. vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
  317. if (!vha->node_name)
  318. goto create_vhost_failed_1;
  319. vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
  320. if (!vha->port_name)
  321. goto create_vhost_failed_2;
  322. /* New host info */
  323. u64_to_wwn(fc_vport->node_name, vha->node_name);
  324. u64_to_wwn(fc_vport->port_name, vha->port_name);
  325. vha->host = host;
  326. vha->host_no = host->host_no;
  327. vha->parent = ha;
  328. vha->fc_vport = fc_vport;
  329. vha->device_flags = 0;
  330. vha->instance = num_hosts;
  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 = vha->instance;
  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. num_hosts++;
  369. mutex_lock(&ha->vport_lock);
  370. set_bit(vha->vp_idx, ha->vp_idx_map);
  371. ha->cur_vport_count++;
  372. mutex_unlock(&ha->vport_lock);
  373. return vha;
  374. create_vhost_failed_3:
  375. kfree(vha->port_name);
  376. create_vhost_failed_2:
  377. kfree(vha->node_name);
  378. create_vhost_failed_1:
  379. return NULL;
  380. }