qla_mid.c 10 KB

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