qla_mid.c 12 KB

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