qla_bsg.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/kthread.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/delay.h>
  11. /* BSG support for ELS/CT pass through */
  12. inline srb_t *
  13. qla2x00_get_ctx_bsg_sp(scsi_qla_host_t *vha, fc_port_t *fcport, size_t size)
  14. {
  15. srb_t *sp;
  16. struct qla_hw_data *ha = vha->hw;
  17. struct srb_ctx *ctx;
  18. sp = mempool_alloc(ha->srb_mempool, GFP_KERNEL);
  19. if (!sp)
  20. goto done;
  21. ctx = kzalloc(size, GFP_KERNEL);
  22. if (!ctx) {
  23. mempool_free(sp, ha->srb_mempool);
  24. sp = NULL;
  25. goto done;
  26. }
  27. memset(sp, 0, sizeof(*sp));
  28. sp->fcport = fcport;
  29. sp->ctx = ctx;
  30. done:
  31. return sp;
  32. }
  33. int
  34. qla24xx_fcp_prio_cfg_valid(struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
  35. {
  36. int i, ret, num_valid;
  37. uint8_t *bcode;
  38. struct qla_fcp_prio_entry *pri_entry;
  39. uint32_t *bcode_val_ptr, bcode_val;
  40. ret = 1;
  41. num_valid = 0;
  42. bcode = (uint8_t *)pri_cfg;
  43. bcode_val_ptr = (uint32_t *)pri_cfg;
  44. bcode_val = (uint32_t)(*bcode_val_ptr);
  45. if (bcode_val == 0xFFFFFFFF) {
  46. /* No FCP Priority config data in flash */
  47. DEBUG2(printk(KERN_INFO
  48. "%s: No FCP priority config data.\n",
  49. __func__));
  50. return 0;
  51. }
  52. if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
  53. bcode[3] != 'S') {
  54. /* Invalid FCP priority data header*/
  55. DEBUG2(printk(KERN_ERR
  56. "%s: Invalid FCP Priority data header. bcode=0x%x\n",
  57. __func__, bcode_val));
  58. return 0;
  59. }
  60. if (flag != 1)
  61. return ret;
  62. pri_entry = &pri_cfg->entry[0];
  63. for (i = 0; i < pri_cfg->num_entries; i++) {
  64. if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
  65. num_valid++;
  66. pri_entry++;
  67. }
  68. if (num_valid == 0) {
  69. /* No valid FCP priority data entries */
  70. DEBUG2(printk(KERN_ERR
  71. "%s: No valid FCP Priority data entries.\n",
  72. __func__));
  73. ret = 0;
  74. } else {
  75. /* FCP priority data is valid */
  76. DEBUG2(printk(KERN_INFO
  77. "%s: Valid FCP priority data. num entries = %d\n",
  78. __func__, num_valid));
  79. }
  80. return ret;
  81. }
  82. static int
  83. qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
  84. {
  85. struct Scsi_Host *host = bsg_job->shost;
  86. scsi_qla_host_t *vha = shost_priv(host);
  87. struct qla_hw_data *ha = vha->hw;
  88. int ret = 0;
  89. uint32_t len;
  90. uint32_t oper;
  91. bsg_job->reply->reply_payload_rcv_len = 0;
  92. if (!IS_QLA24XX_TYPE(ha) || !IS_QLA25XX(ha)) {
  93. ret = -EINVAL;
  94. goto exit_fcp_prio_cfg;
  95. }
  96. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  97. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  98. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  99. ret = -EBUSY;
  100. goto exit_fcp_prio_cfg;
  101. }
  102. /* Get the sub command */
  103. oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  104. /* Only set config is allowed if config memory is not allocated */
  105. if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
  106. ret = -EINVAL;
  107. goto exit_fcp_prio_cfg;
  108. }
  109. switch (oper) {
  110. case QLFC_FCP_PRIO_DISABLE:
  111. if (ha->flags.fcp_prio_enabled) {
  112. ha->flags.fcp_prio_enabled = 0;
  113. ha->fcp_prio_cfg->attributes &=
  114. ~FCP_PRIO_ATTR_ENABLE;
  115. qla24xx_update_all_fcp_prio(vha);
  116. bsg_job->reply->result = DID_OK;
  117. } else {
  118. ret = -EINVAL;
  119. bsg_job->reply->result = (DID_ERROR << 16);
  120. goto exit_fcp_prio_cfg;
  121. }
  122. break;
  123. case QLFC_FCP_PRIO_ENABLE:
  124. if (!ha->flags.fcp_prio_enabled) {
  125. if (ha->fcp_prio_cfg) {
  126. ha->flags.fcp_prio_enabled = 1;
  127. ha->fcp_prio_cfg->attributes |=
  128. FCP_PRIO_ATTR_ENABLE;
  129. qla24xx_update_all_fcp_prio(vha);
  130. bsg_job->reply->result = DID_OK;
  131. } else {
  132. ret = -EINVAL;
  133. bsg_job->reply->result = (DID_ERROR << 16);
  134. goto exit_fcp_prio_cfg;
  135. }
  136. }
  137. break;
  138. case QLFC_FCP_PRIO_GET_CONFIG:
  139. len = bsg_job->reply_payload.payload_len;
  140. if (!len || len > FCP_PRIO_CFG_SIZE) {
  141. ret = -EINVAL;
  142. bsg_job->reply->result = (DID_ERROR << 16);
  143. goto exit_fcp_prio_cfg;
  144. }
  145. bsg_job->reply->result = DID_OK;
  146. bsg_job->reply->reply_payload_rcv_len =
  147. sg_copy_from_buffer(
  148. bsg_job->reply_payload.sg_list,
  149. bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
  150. len);
  151. break;
  152. case QLFC_FCP_PRIO_SET_CONFIG:
  153. len = bsg_job->request_payload.payload_len;
  154. if (!len || len > FCP_PRIO_CFG_SIZE) {
  155. bsg_job->reply->result = (DID_ERROR << 16);
  156. ret = -EINVAL;
  157. goto exit_fcp_prio_cfg;
  158. }
  159. if (!ha->fcp_prio_cfg) {
  160. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  161. if (!ha->fcp_prio_cfg) {
  162. qla_printk(KERN_WARNING, ha,
  163. "Unable to allocate memory "
  164. "for fcp prio config data (%x).\n",
  165. FCP_PRIO_CFG_SIZE);
  166. bsg_job->reply->result = (DID_ERROR << 16);
  167. ret = -ENOMEM;
  168. goto exit_fcp_prio_cfg;
  169. }
  170. }
  171. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  172. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  173. bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
  174. FCP_PRIO_CFG_SIZE);
  175. /* validate fcp priority data */
  176. if (!qla24xx_fcp_prio_cfg_valid(
  177. (struct qla_fcp_prio_cfg *)
  178. ha->fcp_prio_cfg, 1)) {
  179. bsg_job->reply->result = (DID_ERROR << 16);
  180. ret = -EINVAL;
  181. /* If buffer was invalidatic int
  182. * fcp_prio_cfg is of no use
  183. */
  184. vfree(ha->fcp_prio_cfg);
  185. ha->fcp_prio_cfg = NULL;
  186. goto exit_fcp_prio_cfg;
  187. }
  188. ha->flags.fcp_prio_enabled = 0;
  189. if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
  190. ha->flags.fcp_prio_enabled = 1;
  191. qla24xx_update_all_fcp_prio(vha);
  192. bsg_job->reply->result = DID_OK;
  193. break;
  194. default:
  195. ret = -EINVAL;
  196. break;
  197. }
  198. exit_fcp_prio_cfg:
  199. bsg_job->job_done(bsg_job);
  200. return ret;
  201. }
  202. static int
  203. qla2x00_process_els(struct fc_bsg_job *bsg_job)
  204. {
  205. struct fc_rport *rport;
  206. fc_port_t *fcport = NULL;
  207. struct Scsi_Host *host;
  208. scsi_qla_host_t *vha;
  209. struct qla_hw_data *ha;
  210. srb_t *sp;
  211. const char *type;
  212. int req_sg_cnt, rsp_sg_cnt;
  213. int rval = (DRIVER_ERROR << 16);
  214. uint16_t nextlid = 0;
  215. struct srb_ctx *els;
  216. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  217. rport = bsg_job->rport;
  218. fcport = *(fc_port_t **) rport->dd_data;
  219. host = rport_to_shost(rport);
  220. vha = shost_priv(host);
  221. ha = vha->hw;
  222. type = "FC_BSG_RPT_ELS";
  223. } else {
  224. host = bsg_job->shost;
  225. vha = shost_priv(host);
  226. ha = vha->hw;
  227. type = "FC_BSG_HST_ELS_NOLOGIN";
  228. }
  229. /* pass through is supported only for ISP 4Gb or higher */
  230. if (!IS_FWI2_CAPABLE(ha)) {
  231. DEBUG2(qla_printk(KERN_INFO, ha,
  232. "scsi(%ld):ELS passthru not supported for ISP23xx based "
  233. "adapters\n", vha->host_no));
  234. rval = -EPERM;
  235. goto done;
  236. }
  237. /* Multiple SG's are not supported for ELS requests */
  238. if (bsg_job->request_payload.sg_cnt > 1 ||
  239. bsg_job->reply_payload.sg_cnt > 1) {
  240. DEBUG2(printk(KERN_INFO
  241. "multiple SG's are not supported for ELS requests"
  242. " [request_sg_cnt: %x reply_sg_cnt: %x]\n",
  243. bsg_job->request_payload.sg_cnt,
  244. bsg_job->reply_payload.sg_cnt));
  245. rval = -EPERM;
  246. goto done;
  247. }
  248. /* ELS request for rport */
  249. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  250. /* make sure the rport is logged in,
  251. * if not perform fabric login
  252. */
  253. if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
  254. DEBUG2(qla_printk(KERN_WARNING, ha,
  255. "failed to login port %06X for ELS passthru\n",
  256. fcport->d_id.b24));
  257. rval = -EIO;
  258. goto done;
  259. }
  260. } else {
  261. /* Allocate a dummy fcport structure, since functions
  262. * preparing the IOCB and mailbox command retrieves port
  263. * specific information from fcport structure. For Host based
  264. * ELS commands there will be no fcport structure allocated
  265. */
  266. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  267. if (!fcport) {
  268. rval = -ENOMEM;
  269. goto done;
  270. }
  271. /* Initialize all required fields of fcport */
  272. fcport->vha = vha;
  273. fcport->vp_idx = vha->vp_idx;
  274. fcport->d_id.b.al_pa =
  275. bsg_job->request->rqst_data.h_els.port_id[0];
  276. fcport->d_id.b.area =
  277. bsg_job->request->rqst_data.h_els.port_id[1];
  278. fcport->d_id.b.domain =
  279. bsg_job->request->rqst_data.h_els.port_id[2];
  280. fcport->loop_id =
  281. (fcport->d_id.b.al_pa == 0xFD) ?
  282. NPH_FABRIC_CONTROLLER : NPH_F_PORT;
  283. }
  284. if (!vha->flags.online) {
  285. DEBUG2(qla_printk(KERN_WARNING, ha,
  286. "host not online\n"));
  287. rval = -EIO;
  288. goto done;
  289. }
  290. req_sg_cnt =
  291. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  292. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  293. if (!req_sg_cnt) {
  294. rval = -ENOMEM;
  295. goto done_free_fcport;
  296. }
  297. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  298. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  299. if (!rsp_sg_cnt) {
  300. rval = -ENOMEM;
  301. goto done_free_fcport;
  302. }
  303. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  304. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  305. DEBUG2(printk(KERN_INFO
  306. "dma mapping resulted in different sg counts \
  307. [request_sg_cnt: %x dma_request_sg_cnt: %x\
  308. reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
  309. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  310. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
  311. rval = -EAGAIN;
  312. goto done_unmap_sg;
  313. }
  314. /* Alloc SRB structure */
  315. sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_ctx));
  316. if (!sp) {
  317. rval = -ENOMEM;
  318. goto done_unmap_sg;
  319. }
  320. els = sp->ctx;
  321. els->type =
  322. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  323. SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
  324. els->name =
  325. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  326. "bsg_els_rpt" : "bsg_els_hst");
  327. els->u.bsg_job = bsg_job;
  328. DEBUG2(qla_printk(KERN_INFO, ha,
  329. "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
  330. "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
  331. bsg_job->request->rqst_data.h_els.command_code,
  332. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  333. fcport->d_id.b.al_pa));
  334. rval = qla2x00_start_sp(sp);
  335. if (rval != QLA_SUCCESS) {
  336. kfree(sp->ctx);
  337. mempool_free(sp, ha->srb_mempool);
  338. rval = -EIO;
  339. goto done_unmap_sg;
  340. }
  341. return rval;
  342. done_unmap_sg:
  343. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  344. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  345. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  346. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  347. goto done_free_fcport;
  348. done_free_fcport:
  349. if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
  350. kfree(fcport);
  351. done:
  352. return rval;
  353. }
  354. static int
  355. qla2x00_process_ct(struct fc_bsg_job *bsg_job)
  356. {
  357. srb_t *sp;
  358. struct Scsi_Host *host = bsg_job->shost;
  359. scsi_qla_host_t *vha = shost_priv(host);
  360. struct qla_hw_data *ha = vha->hw;
  361. int rval = (DRIVER_ERROR << 16);
  362. int req_sg_cnt, rsp_sg_cnt;
  363. uint16_t loop_id;
  364. struct fc_port *fcport;
  365. char *type = "FC_BSG_HST_CT";
  366. struct srb_ctx *ct;
  367. req_sg_cnt =
  368. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  369. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  370. if (!req_sg_cnt) {
  371. rval = -ENOMEM;
  372. goto done;
  373. }
  374. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  375. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  376. if (!rsp_sg_cnt) {
  377. rval = -ENOMEM;
  378. goto done;
  379. }
  380. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  381. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  382. DEBUG2(qla_printk(KERN_WARNING, ha,
  383. "[request_sg_cnt: %x dma_request_sg_cnt: %x\
  384. reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
  385. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  386. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
  387. rval = -EAGAIN;
  388. goto done_unmap_sg;
  389. }
  390. if (!vha->flags.online) {
  391. DEBUG2(qla_printk(KERN_WARNING, ha,
  392. "host not online\n"));
  393. rval = -EIO;
  394. goto done_unmap_sg;
  395. }
  396. loop_id =
  397. (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
  398. >> 24;
  399. switch (loop_id) {
  400. case 0xFC:
  401. loop_id = cpu_to_le16(NPH_SNS);
  402. break;
  403. case 0xFA:
  404. loop_id = vha->mgmt_svr_loop_id;
  405. break;
  406. default:
  407. DEBUG2(qla_printk(KERN_INFO, ha,
  408. "Unknown loop id: %x\n", loop_id));
  409. rval = -EINVAL;
  410. goto done_unmap_sg;
  411. }
  412. /* Allocate a dummy fcport structure, since functions preparing the
  413. * IOCB and mailbox command retrieves port specific information
  414. * from fcport structure. For Host based ELS commands there will be
  415. * no fcport structure allocated
  416. */
  417. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  418. if (!fcport) {
  419. rval = -ENOMEM;
  420. goto done_unmap_sg;
  421. }
  422. /* Initialize all required fields of fcport */
  423. fcport->vha = vha;
  424. fcport->vp_idx = vha->vp_idx;
  425. fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
  426. fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
  427. fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
  428. fcport->loop_id = loop_id;
  429. /* Alloc SRB structure */
  430. sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_ctx));
  431. if (!sp) {
  432. rval = -ENOMEM;
  433. goto done_free_fcport;
  434. }
  435. ct = sp->ctx;
  436. ct->type = SRB_CT_CMD;
  437. ct->name = "bsg_ct";
  438. ct->u.bsg_job = bsg_job;
  439. DEBUG2(qla_printk(KERN_INFO, ha,
  440. "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
  441. "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
  442. (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
  443. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  444. fcport->d_id.b.al_pa));
  445. rval = qla2x00_start_sp(sp);
  446. if (rval != QLA_SUCCESS) {
  447. kfree(sp->ctx);
  448. mempool_free(sp, ha->srb_mempool);
  449. rval = -EIO;
  450. goto done_free_fcport;
  451. }
  452. return rval;
  453. done_free_fcport:
  454. kfree(fcport);
  455. done_unmap_sg:
  456. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  457. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  458. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  459. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  460. done:
  461. return rval;
  462. }
  463. /* Set the port configuration to enable the
  464. * internal loopback on ISP81XX
  465. */
  466. static inline int
  467. qla81xx_set_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
  468. uint16_t *new_config)
  469. {
  470. int ret = 0;
  471. int rval = 0;
  472. struct qla_hw_data *ha = vha->hw;
  473. if (!IS_QLA81XX(ha))
  474. goto done_set_internal;
  475. new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
  476. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  477. ha->notify_dcbx_comp = 1;
  478. ret = qla81xx_set_port_config(vha, new_config);
  479. if (ret != QLA_SUCCESS) {
  480. DEBUG2(printk(KERN_ERR
  481. "%s(%lu): Set port config failed\n",
  482. __func__, vha->host_no));
  483. ha->notify_dcbx_comp = 0;
  484. rval = -EINVAL;
  485. goto done_set_internal;
  486. }
  487. /* Wait for DCBX complete event */
  488. if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
  489. DEBUG2(qla_printk(KERN_WARNING, ha,
  490. "State change notificaition not received.\n"));
  491. } else
  492. DEBUG2(qla_printk(KERN_INFO, ha,
  493. "State change RECEIVED\n"));
  494. ha->notify_dcbx_comp = 0;
  495. done_set_internal:
  496. return rval;
  497. }
  498. /* Set the port configuration to disable the
  499. * internal loopback on ISP81XX
  500. */
  501. static inline int
  502. qla81xx_reset_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
  503. int wait)
  504. {
  505. int ret = 0;
  506. int rval = 0;
  507. uint16_t new_config[4];
  508. struct qla_hw_data *ha = vha->hw;
  509. if (!IS_QLA81XX(ha))
  510. goto done_reset_internal;
  511. memset(new_config, 0 , sizeof(new_config));
  512. if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  513. ENABLE_INTERNAL_LOOPBACK) {
  514. new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
  515. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  516. ha->notify_dcbx_comp = wait;
  517. ret = qla81xx_set_port_config(vha, new_config);
  518. if (ret != QLA_SUCCESS) {
  519. DEBUG2(printk(KERN_ERR
  520. "%s(%lu): Set port config failed\n",
  521. __func__, vha->host_no));
  522. ha->notify_dcbx_comp = 0;
  523. rval = -EINVAL;
  524. goto done_reset_internal;
  525. }
  526. /* Wait for DCBX complete event */
  527. if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
  528. (20 * HZ))) {
  529. DEBUG2(qla_printk(KERN_WARNING, ha,
  530. "State change notificaition not received.\n"));
  531. ha->notify_dcbx_comp = 0;
  532. rval = -EINVAL;
  533. goto done_reset_internal;
  534. } else
  535. DEBUG2(qla_printk(KERN_INFO, ha,
  536. "State change RECEIVED\n"));
  537. ha->notify_dcbx_comp = 0;
  538. }
  539. done_reset_internal:
  540. return rval;
  541. }
  542. static int
  543. qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
  544. {
  545. struct Scsi_Host *host = bsg_job->shost;
  546. scsi_qla_host_t *vha = shost_priv(host);
  547. struct qla_hw_data *ha = vha->hw;
  548. int rval;
  549. uint8_t command_sent;
  550. char *type;
  551. struct msg_echo_lb elreq;
  552. uint16_t response[MAILBOX_REGISTER_COUNT];
  553. uint16_t config[4], new_config[4];
  554. uint8_t *fw_sts_ptr;
  555. uint8_t *req_data = NULL;
  556. dma_addr_t req_data_dma;
  557. uint32_t req_data_len;
  558. uint8_t *rsp_data = NULL;
  559. dma_addr_t rsp_data_dma;
  560. uint32_t rsp_data_len;
  561. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  562. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  563. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  564. return -EBUSY;
  565. if (!vha->flags.online) {
  566. DEBUG2(qla_printk(KERN_WARNING, ha, "host not online\n"));
  567. return -EIO;
  568. }
  569. elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  570. bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
  571. DMA_TO_DEVICE);
  572. if (!elreq.req_sg_cnt)
  573. return -ENOMEM;
  574. elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  575. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  576. DMA_FROM_DEVICE);
  577. if (!elreq.rsp_sg_cnt) {
  578. rval = -ENOMEM;
  579. goto done_unmap_req_sg;
  580. }
  581. if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  582. (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  583. DEBUG2(printk(KERN_INFO
  584. "dma mapping resulted in different sg counts "
  585. "[request_sg_cnt: %x dma_request_sg_cnt: %x "
  586. "reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
  587. bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
  588. bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt));
  589. rval = -EAGAIN;
  590. goto done_unmap_sg;
  591. }
  592. req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
  593. req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
  594. &req_data_dma, GFP_KERNEL);
  595. if (!req_data) {
  596. DEBUG2(printk(KERN_ERR "%s: dma alloc for req_data "
  597. "failed for host=%lu\n", __func__, vha->host_no));
  598. rval = -ENOMEM;
  599. goto done_unmap_sg;
  600. }
  601. rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
  602. &rsp_data_dma, GFP_KERNEL);
  603. if (!rsp_data) {
  604. DEBUG2(printk(KERN_ERR "%s: dma alloc for rsp_data "
  605. "failed for host=%lu\n", __func__, vha->host_no));
  606. rval = -ENOMEM;
  607. goto done_free_dma_req;
  608. }
  609. /* Copy the request buffer in req_data now */
  610. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  611. bsg_job->request_payload.sg_cnt, req_data, req_data_len);
  612. elreq.send_dma = req_data_dma;
  613. elreq.rcv_dma = rsp_data_dma;
  614. elreq.transfer_size = req_data_len;
  615. elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  616. if ((ha->current_topology == ISP_CFG_F ||
  617. (IS_QLA81XX(ha) &&
  618. le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
  619. && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
  620. elreq.options == EXTERNAL_LOOPBACK) {
  621. type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
  622. DEBUG2(qla_printk(KERN_INFO, ha,
  623. "scsi(%ld) bsg rqst type: %s\n", vha->host_no, type));
  624. command_sent = INT_DEF_LB_ECHO_CMD;
  625. rval = qla2x00_echo_test(vha, &elreq, response);
  626. } else {
  627. if (IS_QLA81XX(ha)) {
  628. memset(config, 0, sizeof(config));
  629. memset(new_config, 0, sizeof(new_config));
  630. if (qla81xx_get_port_config(vha, config)) {
  631. DEBUG2(printk(KERN_ERR
  632. "%s(%lu): Get port config failed\n",
  633. __func__, vha->host_no));
  634. bsg_job->reply->reply_payload_rcv_len = 0;
  635. bsg_job->reply->result = (DID_ERROR << 16);
  636. rval = -EPERM;
  637. goto done_free_dma_req;
  638. }
  639. if (elreq.options != EXTERNAL_LOOPBACK) {
  640. DEBUG2(qla_printk(KERN_INFO, ha,
  641. "Internal: current port config = %x\n",
  642. config[0]));
  643. if (qla81xx_set_internal_loopback(vha, config,
  644. new_config)) {
  645. bsg_job->reply->reply_payload_rcv_len =
  646. 0;
  647. bsg_job->reply->result =
  648. (DID_ERROR << 16);
  649. rval = -EPERM;
  650. goto done_free_dma_req;
  651. }
  652. } else {
  653. /* For external loopback to work
  654. * ensure internal loopback is disabled
  655. */
  656. if (qla81xx_reset_internal_loopback(vha,
  657. config, 1)) {
  658. bsg_job->reply->reply_payload_rcv_len =
  659. 0;
  660. bsg_job->reply->result =
  661. (DID_ERROR << 16);
  662. rval = -EPERM;
  663. goto done_free_dma_req;
  664. }
  665. }
  666. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  667. DEBUG2(qla_printk(KERN_INFO, ha,
  668. "scsi(%ld) bsg rqst type: %s\n",
  669. vha->host_no, type));
  670. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  671. rval = qla2x00_loopback_test(vha, &elreq, response);
  672. if (new_config[1]) {
  673. /* Revert back to original port config
  674. * Also clear internal loopback
  675. */
  676. qla81xx_reset_internal_loopback(vha,
  677. new_config, 0);
  678. }
  679. if (response[0] == MBS_COMMAND_ERROR &&
  680. response[1] == MBS_LB_RESET) {
  681. DEBUG2(printk(KERN_ERR "%s(%ld): ABORTing "
  682. "ISP\n", __func__, vha->host_no));
  683. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  684. qla2xxx_wake_dpc(vha);
  685. qla2x00_wait_for_chip_reset(vha);
  686. /* Also reset the MPI */
  687. if (qla81xx_restart_mpi_firmware(vha) !=
  688. QLA_SUCCESS) {
  689. qla_printk(KERN_INFO, ha,
  690. "MPI reset failed for host%ld.\n",
  691. vha->host_no);
  692. }
  693. bsg_job->reply->reply_payload_rcv_len = 0;
  694. bsg_job->reply->result = (DID_ERROR << 16);
  695. rval = -EIO;
  696. goto done_free_dma_req;
  697. }
  698. } else {
  699. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  700. DEBUG2(qla_printk(KERN_INFO, ha,
  701. "scsi(%ld) bsg rqst type: %s\n",
  702. vha->host_no, type));
  703. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  704. rval = qla2x00_loopback_test(vha, &elreq, response);
  705. }
  706. }
  707. if (rval) {
  708. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  709. "request %s failed\n", vha->host_no, type));
  710. fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
  711. sizeof(struct fc_bsg_reply);
  712. memcpy(fw_sts_ptr, response, sizeof(response));
  713. fw_sts_ptr += sizeof(response);
  714. *fw_sts_ptr = command_sent;
  715. rval = 0;
  716. bsg_job->reply->reply_payload_rcv_len = 0;
  717. bsg_job->reply->result = (DID_ERROR << 16);
  718. } else {
  719. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  720. "request %s completed\n", vha->host_no, type));
  721. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  722. sizeof(response) + sizeof(uint8_t);
  723. bsg_job->reply->reply_payload_rcv_len =
  724. bsg_job->reply_payload.payload_len;
  725. fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
  726. sizeof(struct fc_bsg_reply);
  727. memcpy(fw_sts_ptr, response, sizeof(response));
  728. fw_sts_ptr += sizeof(response);
  729. *fw_sts_ptr = command_sent;
  730. bsg_job->reply->result = DID_OK;
  731. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  732. bsg_job->reply_payload.sg_cnt, rsp_data,
  733. rsp_data_len);
  734. }
  735. bsg_job->job_done(bsg_job);
  736. dma_free_coherent(&ha->pdev->dev, rsp_data_len,
  737. rsp_data, rsp_data_dma);
  738. done_free_dma_req:
  739. dma_free_coherent(&ha->pdev->dev, req_data_len,
  740. req_data, req_data_dma);
  741. done_unmap_sg:
  742. dma_unmap_sg(&ha->pdev->dev,
  743. bsg_job->reply_payload.sg_list,
  744. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  745. done_unmap_req_sg:
  746. dma_unmap_sg(&ha->pdev->dev,
  747. bsg_job->request_payload.sg_list,
  748. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  749. return rval;
  750. }
  751. static int
  752. qla84xx_reset(struct fc_bsg_job *bsg_job)
  753. {
  754. struct Scsi_Host *host = bsg_job->shost;
  755. scsi_qla_host_t *vha = shost_priv(host);
  756. struct qla_hw_data *ha = vha->hw;
  757. int rval = 0;
  758. uint32_t flag;
  759. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  760. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  761. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  762. return -EBUSY;
  763. if (!IS_QLA84XX(ha)) {
  764. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
  765. "exiting.\n", vha->host_no));
  766. return -EINVAL;
  767. }
  768. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  769. rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
  770. if (rval) {
  771. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  772. "request 84xx reset failed\n", vha->host_no));
  773. rval = bsg_job->reply->reply_payload_rcv_len = 0;
  774. bsg_job->reply->result = (DID_ERROR << 16);
  775. } else {
  776. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  777. "request 84xx reset completed\n", vha->host_no));
  778. bsg_job->reply->result = DID_OK;
  779. }
  780. bsg_job->job_done(bsg_job);
  781. return rval;
  782. }
  783. static int
  784. qla84xx_updatefw(struct fc_bsg_job *bsg_job)
  785. {
  786. struct Scsi_Host *host = bsg_job->shost;
  787. scsi_qla_host_t *vha = shost_priv(host);
  788. struct qla_hw_data *ha = vha->hw;
  789. struct verify_chip_entry_84xx *mn = NULL;
  790. dma_addr_t mn_dma, fw_dma;
  791. void *fw_buf = NULL;
  792. int rval = 0;
  793. uint32_t sg_cnt;
  794. uint32_t data_len;
  795. uint16_t options;
  796. uint32_t flag;
  797. uint32_t fw_ver;
  798. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  799. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  800. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  801. return -EBUSY;
  802. if (!IS_QLA84XX(ha)) {
  803. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
  804. "exiting.\n", vha->host_no));
  805. return -EINVAL;
  806. }
  807. sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  808. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  809. if (!sg_cnt)
  810. return -ENOMEM;
  811. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  812. DEBUG2(printk(KERN_INFO
  813. "dma mapping resulted in different sg counts "
  814. "request_sg_cnt: %x dma_request_sg_cnt: %x ",
  815. bsg_job->request_payload.sg_cnt, sg_cnt));
  816. rval = -EAGAIN;
  817. goto done_unmap_sg;
  818. }
  819. data_len = bsg_job->request_payload.payload_len;
  820. fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
  821. &fw_dma, GFP_KERNEL);
  822. if (!fw_buf) {
  823. DEBUG2(printk(KERN_ERR "%s: dma alloc for fw_buf "
  824. "failed for host=%lu\n", __func__, vha->host_no));
  825. rval = -ENOMEM;
  826. goto done_unmap_sg;
  827. }
  828. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  829. bsg_job->request_payload.sg_cnt, fw_buf, data_len);
  830. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  831. if (!mn) {
  832. DEBUG2(printk(KERN_ERR "%s: dma alloc for fw buffer "
  833. "failed for host=%lu\n", __func__, vha->host_no));
  834. rval = -ENOMEM;
  835. goto done_free_fw_buf;
  836. }
  837. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  838. fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
  839. memset(mn, 0, sizeof(struct access_chip_84xx));
  840. mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
  841. mn->entry_count = 1;
  842. options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
  843. if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
  844. options |= VCO_DIAG_FW;
  845. mn->options = cpu_to_le16(options);
  846. mn->fw_ver = cpu_to_le32(fw_ver);
  847. mn->fw_size = cpu_to_le32(data_len);
  848. mn->fw_seq_size = cpu_to_le32(data_len);
  849. mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
  850. mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
  851. mn->dseg_length = cpu_to_le32(data_len);
  852. mn->data_seg_cnt = cpu_to_le16(1);
  853. rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
  854. if (rval) {
  855. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  856. "request 84xx updatefw failed\n", vha->host_no));
  857. rval = bsg_job->reply->reply_payload_rcv_len = 0;
  858. bsg_job->reply->result = (DID_ERROR << 16);
  859. } else {
  860. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  861. "request 84xx updatefw completed\n", vha->host_no));
  862. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  863. bsg_job->reply->result = DID_OK;
  864. }
  865. bsg_job->job_done(bsg_job);
  866. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  867. done_free_fw_buf:
  868. dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
  869. done_unmap_sg:
  870. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  871. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  872. return rval;
  873. }
  874. static int
  875. qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
  876. {
  877. struct Scsi_Host *host = bsg_job->shost;
  878. scsi_qla_host_t *vha = shost_priv(host);
  879. struct qla_hw_data *ha = vha->hw;
  880. struct access_chip_84xx *mn = NULL;
  881. dma_addr_t mn_dma, mgmt_dma;
  882. void *mgmt_b = NULL;
  883. int rval = 0;
  884. struct qla_bsg_a84_mgmt *ql84_mgmt;
  885. uint32_t sg_cnt;
  886. uint32_t data_len = 0;
  887. uint32_t dma_direction = DMA_NONE;
  888. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  889. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  890. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  891. return -EBUSY;
  892. if (!IS_QLA84XX(ha)) {
  893. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
  894. "exiting.\n", vha->host_no));
  895. return -EINVAL;
  896. }
  897. ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
  898. sizeof(struct fc_bsg_request));
  899. if (!ql84_mgmt) {
  900. DEBUG2(printk("%s(%ld): mgmt header not provided, exiting.\n",
  901. __func__, vha->host_no));
  902. return -EINVAL;
  903. }
  904. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  905. if (!mn) {
  906. DEBUG2(printk(KERN_ERR "%s: dma alloc for fw buffer "
  907. "failed for host=%lu\n", __func__, vha->host_no));
  908. return -ENOMEM;
  909. }
  910. memset(mn, 0, sizeof(struct access_chip_84xx));
  911. mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
  912. mn->entry_count = 1;
  913. switch (ql84_mgmt->mgmt.cmd) {
  914. case QLA84_MGMT_READ_MEM:
  915. case QLA84_MGMT_GET_INFO:
  916. sg_cnt = dma_map_sg(&ha->pdev->dev,
  917. bsg_job->reply_payload.sg_list,
  918. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  919. if (!sg_cnt) {
  920. rval = -ENOMEM;
  921. goto exit_mgmt;
  922. }
  923. dma_direction = DMA_FROM_DEVICE;
  924. if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
  925. DEBUG2(printk(KERN_INFO
  926. "dma mapping resulted in different sg counts "
  927. "reply_sg_cnt: %x dma_reply_sg_cnt: %x\n",
  928. bsg_job->reply_payload.sg_cnt, sg_cnt));
  929. rval = -EAGAIN;
  930. goto done_unmap_sg;
  931. }
  932. data_len = bsg_job->reply_payload.payload_len;
  933. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  934. &mgmt_dma, GFP_KERNEL);
  935. if (!mgmt_b) {
  936. DEBUG2(printk(KERN_ERR "%s: dma alloc for mgmt_b "
  937. "failed for host=%lu\n",
  938. __func__, vha->host_no));
  939. rval = -ENOMEM;
  940. goto done_unmap_sg;
  941. }
  942. if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
  943. mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
  944. mn->parameter1 =
  945. cpu_to_le32(
  946. ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  947. } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
  948. mn->options = cpu_to_le16(ACO_REQUEST_INFO);
  949. mn->parameter1 =
  950. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
  951. mn->parameter2 =
  952. cpu_to_le32(
  953. ql84_mgmt->mgmt.mgmtp.u.info.context);
  954. }
  955. break;
  956. case QLA84_MGMT_WRITE_MEM:
  957. sg_cnt = dma_map_sg(&ha->pdev->dev,
  958. bsg_job->request_payload.sg_list,
  959. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  960. if (!sg_cnt) {
  961. rval = -ENOMEM;
  962. goto exit_mgmt;
  963. }
  964. dma_direction = DMA_TO_DEVICE;
  965. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  966. DEBUG2(printk(KERN_INFO
  967. "dma mapping resulted in different sg counts "
  968. "request_sg_cnt: %x dma_request_sg_cnt: %x ",
  969. bsg_job->request_payload.sg_cnt, sg_cnt));
  970. rval = -EAGAIN;
  971. goto done_unmap_sg;
  972. }
  973. data_len = bsg_job->request_payload.payload_len;
  974. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  975. &mgmt_dma, GFP_KERNEL);
  976. if (!mgmt_b) {
  977. DEBUG2(printk(KERN_ERR "%s: dma alloc for mgmt_b "
  978. "failed for host=%lu\n",
  979. __func__, vha->host_no));
  980. rval = -ENOMEM;
  981. goto done_unmap_sg;
  982. }
  983. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  984. bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
  985. mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
  986. mn->parameter1 =
  987. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  988. break;
  989. case QLA84_MGMT_CHNG_CONFIG:
  990. mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
  991. mn->parameter1 =
  992. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
  993. mn->parameter2 =
  994. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
  995. mn->parameter3 =
  996. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
  997. break;
  998. default:
  999. rval = -EIO;
  1000. goto exit_mgmt;
  1001. }
  1002. if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
  1003. mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
  1004. mn->dseg_count = cpu_to_le16(1);
  1005. mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
  1006. mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
  1007. mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
  1008. }
  1009. rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
  1010. if (rval) {
  1011. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  1012. "request 84xx mgmt failed\n", vha->host_no));
  1013. rval = bsg_job->reply->reply_payload_rcv_len = 0;
  1014. bsg_job->reply->result = (DID_ERROR << 16);
  1015. } else {
  1016. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  1017. "request 84xx mgmt completed\n", vha->host_no));
  1018. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1019. bsg_job->reply->result = DID_OK;
  1020. if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
  1021. (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
  1022. bsg_job->reply->reply_payload_rcv_len =
  1023. bsg_job->reply_payload.payload_len;
  1024. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1025. bsg_job->reply_payload.sg_cnt, mgmt_b,
  1026. data_len);
  1027. }
  1028. }
  1029. bsg_job->job_done(bsg_job);
  1030. done_unmap_sg:
  1031. if (mgmt_b)
  1032. dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
  1033. if (dma_direction == DMA_TO_DEVICE)
  1034. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  1035. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1036. else if (dma_direction == DMA_FROM_DEVICE)
  1037. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  1038. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1039. exit_mgmt:
  1040. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  1041. return rval;
  1042. }
  1043. static int
  1044. qla24xx_iidma(struct fc_bsg_job *bsg_job)
  1045. {
  1046. struct Scsi_Host *host = bsg_job->shost;
  1047. scsi_qla_host_t *vha = shost_priv(host);
  1048. struct qla_hw_data *ha = vha->hw;
  1049. int rval = 0;
  1050. struct qla_port_param *port_param = NULL;
  1051. fc_port_t *fcport = NULL;
  1052. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1053. uint8_t *rsp_ptr = NULL;
  1054. bsg_job->reply->reply_payload_rcv_len = 0;
  1055. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  1056. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  1057. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  1058. return -EBUSY;
  1059. if (!IS_IIDMA_CAPABLE(vha->hw)) {
  1060. DEBUG2(qla_printk(KERN_WARNING, ha, "%s(%lu): iiDMA not "
  1061. "supported\n", __func__, vha->host_no));
  1062. return -EINVAL;
  1063. }
  1064. port_param = (struct qla_port_param *)((char *)bsg_job->request +
  1065. sizeof(struct fc_bsg_request));
  1066. if (!port_param) {
  1067. DEBUG2(printk("%s(%ld): port_param header not provided, "
  1068. "exiting.\n", __func__, vha->host_no));
  1069. return -EINVAL;
  1070. }
  1071. if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
  1072. DEBUG2(printk(KERN_ERR "%s(%ld): Invalid destination type\n",
  1073. __func__, vha->host_no));
  1074. return -EINVAL;
  1075. }
  1076. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  1077. if (fcport->port_type != FCT_TARGET)
  1078. continue;
  1079. if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
  1080. fcport->port_name, sizeof(fcport->port_name)))
  1081. continue;
  1082. break;
  1083. }
  1084. if (!fcport) {
  1085. DEBUG2(printk(KERN_ERR "%s(%ld): Failed to find port\n",
  1086. __func__, vha->host_no));
  1087. return -EINVAL;
  1088. }
  1089. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  1090. DEBUG2(printk(KERN_ERR "%s(%ld): Port not online\n",
  1091. __func__, vha->host_no));
  1092. return -EINVAL;
  1093. }
  1094. if (fcport->flags & FCF_LOGIN_NEEDED) {
  1095. DEBUG2(printk(KERN_ERR "%s(%ld): Remote port not logged in, "
  1096. "flags = 0x%x\n",
  1097. __func__, vha->host_no, fcport->flags));
  1098. return -EINVAL;
  1099. }
  1100. if (port_param->mode)
  1101. rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
  1102. port_param->speed, mb);
  1103. else
  1104. rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
  1105. &port_param->speed, mb);
  1106. if (rval) {
  1107. DEBUG16(printk(KERN_ERR "scsi(%ld): iIDMA cmd failed for "
  1108. "%02x%02x%02x%02x%02x%02x%02x%02x -- "
  1109. "%04x %x %04x %04x.\n",
  1110. vha->host_no, fcport->port_name[0],
  1111. fcport->port_name[1],
  1112. fcport->port_name[2], fcport->port_name[3],
  1113. fcport->port_name[4], fcport->port_name[5],
  1114. fcport->port_name[6], fcport->port_name[7], rval,
  1115. fcport->fp_speed, mb[0], mb[1]));
  1116. rval = 0;
  1117. bsg_job->reply->result = (DID_ERROR << 16);
  1118. } else {
  1119. if (!port_param->mode) {
  1120. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  1121. sizeof(struct qla_port_param);
  1122. rsp_ptr = ((uint8_t *)bsg_job->reply) +
  1123. sizeof(struct fc_bsg_reply);
  1124. memcpy(rsp_ptr, port_param,
  1125. sizeof(struct qla_port_param));
  1126. }
  1127. bsg_job->reply->result = DID_OK;
  1128. }
  1129. bsg_job->job_done(bsg_job);
  1130. return rval;
  1131. }
  1132. static int
  1133. qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
  1134. {
  1135. switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
  1136. case QL_VND_LOOPBACK:
  1137. return qla2x00_process_loopback(bsg_job);
  1138. case QL_VND_A84_RESET:
  1139. return qla84xx_reset(bsg_job);
  1140. case QL_VND_A84_UPDATE_FW:
  1141. return qla84xx_updatefw(bsg_job);
  1142. case QL_VND_A84_MGMT_CMD:
  1143. return qla84xx_mgmt_cmd(bsg_job);
  1144. case QL_VND_IIDMA:
  1145. return qla24xx_iidma(bsg_job);
  1146. case QL_VND_FCP_PRIO_CFG_CMD:
  1147. return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
  1148. default:
  1149. bsg_job->reply->result = (DID_ERROR << 16);
  1150. bsg_job->job_done(bsg_job);
  1151. return -ENOSYS;
  1152. }
  1153. }
  1154. int
  1155. qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
  1156. {
  1157. int ret = -EINVAL;
  1158. switch (bsg_job->request->msgcode) {
  1159. case FC_BSG_RPT_ELS:
  1160. case FC_BSG_HST_ELS_NOLOGIN:
  1161. ret = qla2x00_process_els(bsg_job);
  1162. break;
  1163. case FC_BSG_HST_CT:
  1164. ret = qla2x00_process_ct(bsg_job);
  1165. break;
  1166. case FC_BSG_HST_VENDOR:
  1167. ret = qla2x00_process_vendor_specific(bsg_job);
  1168. break;
  1169. case FC_BSG_HST_ADD_RPORT:
  1170. case FC_BSG_HST_DEL_RPORT:
  1171. case FC_BSG_RPT_CT:
  1172. default:
  1173. DEBUG2(printk("qla2xxx: unsupported BSG request\n"));
  1174. break;
  1175. }
  1176. return ret;
  1177. }
  1178. int
  1179. qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
  1180. {
  1181. scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
  1182. struct qla_hw_data *ha = vha->hw;
  1183. srb_t *sp;
  1184. int cnt, que;
  1185. unsigned long flags;
  1186. struct req_que *req;
  1187. struct srb_ctx *sp_bsg;
  1188. /* find the bsg job from the active list of commands */
  1189. spin_lock_irqsave(&ha->hardware_lock, flags);
  1190. for (que = 0; que < ha->max_req_queues; que++) {
  1191. req = ha->req_q_map[que];
  1192. if (!req)
  1193. continue;
  1194. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  1195. sp = req->outstanding_cmds[cnt];
  1196. if (sp) {
  1197. sp_bsg = sp->ctx;
  1198. if (((sp_bsg->type == SRB_CT_CMD) ||
  1199. (sp_bsg->type == SRB_ELS_CMD_HST))
  1200. && (sp_bsg->u.bsg_job == bsg_job)) {
  1201. if (ha->isp_ops->abort_command(sp)) {
  1202. DEBUG2(qla_printk(KERN_INFO, ha,
  1203. "scsi(%ld): mbx "
  1204. "abort_command failed\n",
  1205. vha->host_no));
  1206. bsg_job->req->errors =
  1207. bsg_job->reply->result = -EIO;
  1208. } else {
  1209. DEBUG2(qla_printk(KERN_INFO, ha,
  1210. "scsi(%ld): mbx "
  1211. "abort_command success\n",
  1212. vha->host_no));
  1213. bsg_job->req->errors =
  1214. bsg_job->reply->result = 0;
  1215. }
  1216. goto done;
  1217. }
  1218. }
  1219. }
  1220. }
  1221. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1222. DEBUG2(qla_printk(KERN_INFO, ha,
  1223. "scsi(%ld) SRB not found to abort\n", vha->host_no));
  1224. bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
  1225. return 0;
  1226. done:
  1227. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1228. if (bsg_job->request->msgcode == FC_BSG_HST_CT)
  1229. kfree(sp->fcport);
  1230. kfree(sp->ctx);
  1231. mempool_free(sp, ha->srb_mempool);
  1232. return 0;
  1233. }