qla_bsg.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2012 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. void
  13. qla2x00_bsg_job_done(void *data, void *ptr, int res)
  14. {
  15. srb_t *sp = (srb_t *)ptr;
  16. struct scsi_qla_host *vha = (scsi_qla_host_t *)data;
  17. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  18. bsg_job->reply->result = res;
  19. bsg_job->job_done(bsg_job);
  20. sp->free(vha, sp);
  21. }
  22. void
  23. qla2x00_bsg_sp_free(void *data, void *ptr)
  24. {
  25. srb_t *sp = (srb_t *)ptr;
  26. struct scsi_qla_host *vha = (scsi_qla_host_t *)data;
  27. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  28. struct qla_hw_data *ha = vha->hw;
  29. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  30. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  31. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  32. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  33. if (sp->type == SRB_CT_CMD ||
  34. sp->type == SRB_ELS_CMD_HST)
  35. kfree(sp->fcport);
  36. mempool_free(sp, vha->hw->srb_mempool);
  37. }
  38. int
  39. qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
  40. struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
  41. {
  42. int i, ret, num_valid;
  43. uint8_t *bcode;
  44. struct qla_fcp_prio_entry *pri_entry;
  45. uint32_t *bcode_val_ptr, bcode_val;
  46. ret = 1;
  47. num_valid = 0;
  48. bcode = (uint8_t *)pri_cfg;
  49. bcode_val_ptr = (uint32_t *)pri_cfg;
  50. bcode_val = (uint32_t)(*bcode_val_ptr);
  51. if (bcode_val == 0xFFFFFFFF) {
  52. /* No FCP Priority config data in flash */
  53. ql_dbg(ql_dbg_user, vha, 0x7051,
  54. "No FCP Priority config data.\n");
  55. return 0;
  56. }
  57. if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
  58. bcode[3] != 'S') {
  59. /* Invalid FCP priority data header*/
  60. ql_dbg(ql_dbg_user, vha, 0x7052,
  61. "Invalid FCP Priority data header. bcode=0x%x.\n",
  62. bcode_val);
  63. return 0;
  64. }
  65. if (flag != 1)
  66. return ret;
  67. pri_entry = &pri_cfg->entry[0];
  68. for (i = 0; i < pri_cfg->num_entries; i++) {
  69. if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
  70. num_valid++;
  71. pri_entry++;
  72. }
  73. if (num_valid == 0) {
  74. /* No valid FCP priority data entries */
  75. ql_dbg(ql_dbg_user, vha, 0x7053,
  76. "No valid FCP Priority data entries.\n");
  77. ret = 0;
  78. } else {
  79. /* FCP priority data is valid */
  80. ql_dbg(ql_dbg_user, vha, 0x7054,
  81. "Valid FCP priority data. num entries = %d.\n",
  82. num_valid);
  83. }
  84. return ret;
  85. }
  86. static int
  87. qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
  88. {
  89. struct Scsi_Host *host = bsg_job->shost;
  90. scsi_qla_host_t *vha = shost_priv(host);
  91. struct qla_hw_data *ha = vha->hw;
  92. int ret = 0;
  93. uint32_t len;
  94. uint32_t oper;
  95. if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_QLA82XX(ha))) {
  96. ret = -EINVAL;
  97. goto exit_fcp_prio_cfg;
  98. }
  99. /* Get the sub command */
  100. oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  101. /* Only set config is allowed if config memory is not allocated */
  102. if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
  103. ret = -EINVAL;
  104. goto exit_fcp_prio_cfg;
  105. }
  106. switch (oper) {
  107. case QLFC_FCP_PRIO_DISABLE:
  108. if (ha->flags.fcp_prio_enabled) {
  109. ha->flags.fcp_prio_enabled = 0;
  110. ha->fcp_prio_cfg->attributes &=
  111. ~FCP_PRIO_ATTR_ENABLE;
  112. qla24xx_update_all_fcp_prio(vha);
  113. bsg_job->reply->result = DID_OK;
  114. } else {
  115. ret = -EINVAL;
  116. bsg_job->reply->result = (DID_ERROR << 16);
  117. goto exit_fcp_prio_cfg;
  118. }
  119. break;
  120. case QLFC_FCP_PRIO_ENABLE:
  121. if (!ha->flags.fcp_prio_enabled) {
  122. if (ha->fcp_prio_cfg) {
  123. ha->flags.fcp_prio_enabled = 1;
  124. ha->fcp_prio_cfg->attributes |=
  125. FCP_PRIO_ATTR_ENABLE;
  126. qla24xx_update_all_fcp_prio(vha);
  127. bsg_job->reply->result = DID_OK;
  128. } else {
  129. ret = -EINVAL;
  130. bsg_job->reply->result = (DID_ERROR << 16);
  131. goto exit_fcp_prio_cfg;
  132. }
  133. }
  134. break;
  135. case QLFC_FCP_PRIO_GET_CONFIG:
  136. len = bsg_job->reply_payload.payload_len;
  137. if (!len || len > FCP_PRIO_CFG_SIZE) {
  138. ret = -EINVAL;
  139. bsg_job->reply->result = (DID_ERROR << 16);
  140. goto exit_fcp_prio_cfg;
  141. }
  142. bsg_job->reply->result = DID_OK;
  143. bsg_job->reply->reply_payload_rcv_len =
  144. sg_copy_from_buffer(
  145. bsg_job->reply_payload.sg_list,
  146. bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
  147. len);
  148. break;
  149. case QLFC_FCP_PRIO_SET_CONFIG:
  150. len = bsg_job->request_payload.payload_len;
  151. if (!len || len > FCP_PRIO_CFG_SIZE) {
  152. bsg_job->reply->result = (DID_ERROR << 16);
  153. ret = -EINVAL;
  154. goto exit_fcp_prio_cfg;
  155. }
  156. if (!ha->fcp_prio_cfg) {
  157. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  158. if (!ha->fcp_prio_cfg) {
  159. ql_log(ql_log_warn, vha, 0x7050,
  160. "Unable to allocate memory for fcp prio "
  161. "config data (%x).\n", FCP_PRIO_CFG_SIZE);
  162. bsg_job->reply->result = (DID_ERROR << 16);
  163. ret = -ENOMEM;
  164. goto exit_fcp_prio_cfg;
  165. }
  166. }
  167. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  168. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  169. bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
  170. FCP_PRIO_CFG_SIZE);
  171. /* validate fcp priority data */
  172. if (!qla24xx_fcp_prio_cfg_valid(vha,
  173. (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
  174. bsg_job->reply->result = (DID_ERROR << 16);
  175. ret = -EINVAL;
  176. /* If buffer was invalidatic int
  177. * fcp_prio_cfg is of no use
  178. */
  179. vfree(ha->fcp_prio_cfg);
  180. ha->fcp_prio_cfg = NULL;
  181. goto exit_fcp_prio_cfg;
  182. }
  183. ha->flags.fcp_prio_enabled = 0;
  184. if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
  185. ha->flags.fcp_prio_enabled = 1;
  186. qla24xx_update_all_fcp_prio(vha);
  187. bsg_job->reply->result = DID_OK;
  188. break;
  189. default:
  190. ret = -EINVAL;
  191. break;
  192. }
  193. exit_fcp_prio_cfg:
  194. if (!ret)
  195. bsg_job->job_done(bsg_job);
  196. return ret;
  197. }
  198. static int
  199. qla2x00_process_els(struct fc_bsg_job *bsg_job)
  200. {
  201. struct fc_rport *rport;
  202. fc_port_t *fcport = NULL;
  203. struct Scsi_Host *host;
  204. scsi_qla_host_t *vha;
  205. struct qla_hw_data *ha;
  206. srb_t *sp;
  207. const char *type;
  208. int req_sg_cnt, rsp_sg_cnt;
  209. int rval = (DRIVER_ERROR << 16);
  210. uint16_t nextlid = 0;
  211. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  212. rport = bsg_job->rport;
  213. fcport = *(fc_port_t **) rport->dd_data;
  214. host = rport_to_shost(rport);
  215. vha = shost_priv(host);
  216. ha = vha->hw;
  217. type = "FC_BSG_RPT_ELS";
  218. } else {
  219. host = bsg_job->shost;
  220. vha = shost_priv(host);
  221. ha = vha->hw;
  222. type = "FC_BSG_HST_ELS_NOLOGIN";
  223. }
  224. /* pass through is supported only for ISP 4Gb or higher */
  225. if (!IS_FWI2_CAPABLE(ha)) {
  226. ql_dbg(ql_dbg_user, vha, 0x7001,
  227. "ELS passthru not supported for ISP23xx based adapters.\n");
  228. rval = -EPERM;
  229. goto done;
  230. }
  231. /* Multiple SG's are not supported for ELS requests */
  232. if (bsg_job->request_payload.sg_cnt > 1 ||
  233. bsg_job->reply_payload.sg_cnt > 1) {
  234. ql_dbg(ql_dbg_user, vha, 0x7002,
  235. "Multiple SG's are not suppored for ELS requests, "
  236. "request_sg_cnt=%x reply_sg_cnt=%x.\n",
  237. bsg_job->request_payload.sg_cnt,
  238. bsg_job->reply_payload.sg_cnt);
  239. rval = -EPERM;
  240. goto done;
  241. }
  242. /* ELS request for rport */
  243. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  244. /* make sure the rport is logged in,
  245. * if not perform fabric login
  246. */
  247. if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
  248. ql_dbg(ql_dbg_user, vha, 0x7003,
  249. "Failed to login port %06X for ELS passthru.\n",
  250. fcport->d_id.b24);
  251. rval = -EIO;
  252. goto done;
  253. }
  254. } else {
  255. /* Allocate a dummy fcport structure, since functions
  256. * preparing the IOCB and mailbox command retrieves port
  257. * specific information from fcport structure. For Host based
  258. * ELS commands there will be no fcport structure allocated
  259. */
  260. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  261. if (!fcport) {
  262. rval = -ENOMEM;
  263. goto done;
  264. }
  265. /* Initialize all required fields of fcport */
  266. fcport->vha = vha;
  267. fcport->d_id.b.al_pa =
  268. bsg_job->request->rqst_data.h_els.port_id[0];
  269. fcport->d_id.b.area =
  270. bsg_job->request->rqst_data.h_els.port_id[1];
  271. fcport->d_id.b.domain =
  272. bsg_job->request->rqst_data.h_els.port_id[2];
  273. fcport->loop_id =
  274. (fcport->d_id.b.al_pa == 0xFD) ?
  275. NPH_FABRIC_CONTROLLER : NPH_F_PORT;
  276. }
  277. if (!vha->flags.online) {
  278. ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
  279. rval = -EIO;
  280. goto done;
  281. }
  282. req_sg_cnt =
  283. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  284. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  285. if (!req_sg_cnt) {
  286. rval = -ENOMEM;
  287. goto done_free_fcport;
  288. }
  289. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  290. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  291. if (!rsp_sg_cnt) {
  292. rval = -ENOMEM;
  293. goto done_free_fcport;
  294. }
  295. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  296. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  297. ql_log(ql_log_warn, vha, 0x7008,
  298. "dma mapping resulted in different sg counts, "
  299. "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
  300. "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
  301. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  302. rval = -EAGAIN;
  303. goto done_unmap_sg;
  304. }
  305. /* Alloc SRB structure */
  306. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  307. if (!sp) {
  308. rval = -ENOMEM;
  309. goto done_unmap_sg;
  310. }
  311. sp->type =
  312. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  313. SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
  314. sp->name =
  315. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  316. "bsg_els_rpt" : "bsg_els_hst");
  317. sp->u.bsg_job = bsg_job;
  318. sp->free = qla2x00_bsg_sp_free;
  319. sp->done = qla2x00_bsg_job_done;
  320. ql_dbg(ql_dbg_user, vha, 0x700a,
  321. "bsg rqst type: %s els type: %x - loop-id=%x "
  322. "portid=%-2x%02x%02x.\n", type,
  323. bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
  324. fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
  325. rval = qla2x00_start_sp(sp);
  326. if (rval != QLA_SUCCESS) {
  327. ql_log(ql_log_warn, vha, 0x700e,
  328. "qla2x00_start_sp failed = %d\n", rval);
  329. mempool_free(sp, ha->srb_mempool);
  330. rval = -EIO;
  331. goto done_unmap_sg;
  332. }
  333. return rval;
  334. done_unmap_sg:
  335. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  336. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  337. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  338. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  339. goto done_free_fcport;
  340. done_free_fcport:
  341. if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
  342. kfree(fcport);
  343. done:
  344. return rval;
  345. }
  346. inline uint16_t
  347. qla24xx_calc_ct_iocbs(uint16_t dsds)
  348. {
  349. uint16_t iocbs;
  350. iocbs = 1;
  351. if (dsds > 2) {
  352. iocbs += (dsds - 2) / 5;
  353. if ((dsds - 2) % 5)
  354. iocbs++;
  355. }
  356. return iocbs;
  357. }
  358. static int
  359. qla2x00_process_ct(struct fc_bsg_job *bsg_job)
  360. {
  361. srb_t *sp;
  362. struct Scsi_Host *host = bsg_job->shost;
  363. scsi_qla_host_t *vha = shost_priv(host);
  364. struct qla_hw_data *ha = vha->hw;
  365. int rval = (DRIVER_ERROR << 16);
  366. int req_sg_cnt, rsp_sg_cnt;
  367. uint16_t loop_id;
  368. struct fc_port *fcport;
  369. char *type = "FC_BSG_HST_CT";
  370. req_sg_cnt =
  371. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  372. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  373. if (!req_sg_cnt) {
  374. ql_log(ql_log_warn, vha, 0x700f,
  375. "dma_map_sg return %d for request\n", req_sg_cnt);
  376. rval = -ENOMEM;
  377. goto done;
  378. }
  379. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  380. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  381. if (!rsp_sg_cnt) {
  382. ql_log(ql_log_warn, vha, 0x7010,
  383. "dma_map_sg return %d for reply\n", rsp_sg_cnt);
  384. rval = -ENOMEM;
  385. goto done;
  386. }
  387. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  388. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  389. ql_log(ql_log_warn, vha, 0x7011,
  390. "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
  391. "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
  392. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  393. rval = -EAGAIN;
  394. goto done_unmap_sg;
  395. }
  396. if (!vha->flags.online) {
  397. ql_log(ql_log_warn, vha, 0x7012,
  398. "Host is not online.\n");
  399. rval = -EIO;
  400. goto done_unmap_sg;
  401. }
  402. loop_id =
  403. (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
  404. >> 24;
  405. switch (loop_id) {
  406. case 0xFC:
  407. loop_id = cpu_to_le16(NPH_SNS);
  408. break;
  409. case 0xFA:
  410. loop_id = vha->mgmt_svr_loop_id;
  411. break;
  412. default:
  413. ql_dbg(ql_dbg_user, vha, 0x7013,
  414. "Unknown loop id: %x.\n", loop_id);
  415. rval = -EINVAL;
  416. goto done_unmap_sg;
  417. }
  418. /* Allocate a dummy fcport structure, since functions preparing the
  419. * IOCB and mailbox command retrieves port specific information
  420. * from fcport structure. For Host based ELS commands there will be
  421. * no fcport structure allocated
  422. */
  423. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  424. if (!fcport) {
  425. ql_log(ql_log_warn, vha, 0x7014,
  426. "Failed to allocate fcport.\n");
  427. rval = -ENOMEM;
  428. goto done_unmap_sg;
  429. }
  430. /* Initialize all required fields of fcport */
  431. fcport->vha = vha;
  432. fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
  433. fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
  434. fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
  435. fcport->loop_id = loop_id;
  436. /* Alloc SRB structure */
  437. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  438. if (!sp) {
  439. ql_log(ql_log_warn, vha, 0x7015,
  440. "qla2x00_get_sp failed.\n");
  441. rval = -ENOMEM;
  442. goto done_free_fcport;
  443. }
  444. sp->type = SRB_CT_CMD;
  445. sp->name = "bsg_ct";
  446. sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
  447. sp->u.bsg_job = bsg_job;
  448. sp->free = qla2x00_bsg_sp_free;
  449. sp->done = qla2x00_bsg_job_done;
  450. ql_dbg(ql_dbg_user, vha, 0x7016,
  451. "bsg rqst type: %s else type: %x - "
  452. "loop-id=%x portid=%02x%02x%02x.\n", type,
  453. (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
  454. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  455. fcport->d_id.b.al_pa);
  456. rval = qla2x00_start_sp(sp);
  457. if (rval != QLA_SUCCESS) {
  458. ql_log(ql_log_warn, vha, 0x7017,
  459. "qla2x00_start_sp failed=%d.\n", rval);
  460. mempool_free(sp, ha->srb_mempool);
  461. rval = -EIO;
  462. goto done_free_fcport;
  463. }
  464. return rval;
  465. done_free_fcport:
  466. kfree(fcport);
  467. done_unmap_sg:
  468. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  469. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  470. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  471. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  472. done:
  473. return rval;
  474. }
  475. /*
  476. * Set the port configuration to enable the internal or external loopback
  477. * depending on the loopback mode.
  478. */
  479. static inline int
  480. qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  481. uint16_t *new_config, uint16_t mode)
  482. {
  483. int ret = 0;
  484. int rval = 0;
  485. struct qla_hw_data *ha = vha->hw;
  486. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
  487. goto done_set_internal;
  488. if (mode == INTERNAL_LOOPBACK)
  489. new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
  490. else if (mode == EXTERNAL_LOOPBACK)
  491. new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
  492. ql_dbg(ql_dbg_user, vha, 0x70be,
  493. "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
  494. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
  495. ha->notify_dcbx_comp = 1;
  496. ret = qla81xx_set_port_config(vha, new_config);
  497. if (ret != QLA_SUCCESS) {
  498. ql_log(ql_log_warn, vha, 0x7021,
  499. "set port config failed.\n");
  500. ha->notify_dcbx_comp = 0;
  501. rval = -EINVAL;
  502. goto done_set_internal;
  503. }
  504. /* Wait for DCBX complete event */
  505. if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
  506. ql_dbg(ql_dbg_user, vha, 0x7022,
  507. "State change notification not received.\n");
  508. rval = -EINVAL;
  509. } else {
  510. if (ha->flags.idc_compl_status) {
  511. ql_dbg(ql_dbg_user, vha, 0x70c3,
  512. "Bad status in IDC Completion AEN\n");
  513. rval = -EINVAL;
  514. ha->flags.idc_compl_status = 0;
  515. } else
  516. ql_dbg(ql_dbg_user, vha, 0x7023,
  517. "State change received.\n");
  518. }
  519. ha->notify_dcbx_comp = 0;
  520. done_set_internal:
  521. return rval;
  522. }
  523. /* Disable loopback mode */
  524. static inline int
  525. qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  526. int wait)
  527. {
  528. int ret = 0;
  529. int rval = 0;
  530. uint16_t new_config[4];
  531. struct qla_hw_data *ha = vha->hw;
  532. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
  533. goto done_reset_internal;
  534. memset(new_config, 0 , sizeof(new_config));
  535. if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  536. ENABLE_INTERNAL_LOOPBACK ||
  537. (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  538. ENABLE_EXTERNAL_LOOPBACK) {
  539. new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
  540. ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
  541. (new_config[0] & INTERNAL_LOOPBACK_MASK));
  542. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  543. ha->notify_dcbx_comp = wait;
  544. ret = qla81xx_set_port_config(vha, new_config);
  545. if (ret != QLA_SUCCESS) {
  546. ql_log(ql_log_warn, vha, 0x7025,
  547. "Set port config failed.\n");
  548. ha->notify_dcbx_comp = 0;
  549. rval = -EINVAL;
  550. goto done_reset_internal;
  551. }
  552. /* Wait for DCBX complete event */
  553. if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
  554. (20 * HZ))) {
  555. ql_dbg(ql_dbg_user, vha, 0x7026,
  556. "State change notification not received.\n");
  557. ha->notify_dcbx_comp = 0;
  558. rval = -EINVAL;
  559. goto done_reset_internal;
  560. } else
  561. ql_dbg(ql_dbg_user, vha, 0x7027,
  562. "State change received.\n");
  563. ha->notify_dcbx_comp = 0;
  564. }
  565. done_reset_internal:
  566. return rval;
  567. }
  568. static int
  569. qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
  570. {
  571. struct Scsi_Host *host = bsg_job->shost;
  572. scsi_qla_host_t *vha = shost_priv(host);
  573. struct qla_hw_data *ha = vha->hw;
  574. int rval;
  575. uint8_t command_sent;
  576. char *type;
  577. struct msg_echo_lb elreq;
  578. uint16_t response[MAILBOX_REGISTER_COUNT];
  579. uint16_t config[4], new_config[4];
  580. uint8_t *fw_sts_ptr;
  581. uint8_t *req_data = NULL;
  582. dma_addr_t req_data_dma;
  583. uint32_t req_data_len;
  584. uint8_t *rsp_data = NULL;
  585. dma_addr_t rsp_data_dma;
  586. uint32_t rsp_data_len;
  587. if (!vha->flags.online) {
  588. ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
  589. return -EIO;
  590. }
  591. elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  592. bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
  593. DMA_TO_DEVICE);
  594. if (!elreq.req_sg_cnt) {
  595. ql_log(ql_log_warn, vha, 0x701a,
  596. "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
  597. return -ENOMEM;
  598. }
  599. elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  600. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  601. DMA_FROM_DEVICE);
  602. if (!elreq.rsp_sg_cnt) {
  603. ql_log(ql_log_warn, vha, 0x701b,
  604. "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
  605. rval = -ENOMEM;
  606. goto done_unmap_req_sg;
  607. }
  608. if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  609. (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  610. ql_log(ql_log_warn, vha, 0x701c,
  611. "dma mapping resulted in different sg counts, "
  612. "request_sg_cnt: %x dma_request_sg_cnt: %x "
  613. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  614. bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
  615. bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
  616. rval = -EAGAIN;
  617. goto done_unmap_sg;
  618. }
  619. req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
  620. req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
  621. &req_data_dma, GFP_KERNEL);
  622. if (!req_data) {
  623. ql_log(ql_log_warn, vha, 0x701d,
  624. "dma alloc failed for req_data.\n");
  625. rval = -ENOMEM;
  626. goto done_unmap_sg;
  627. }
  628. rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
  629. &rsp_data_dma, GFP_KERNEL);
  630. if (!rsp_data) {
  631. ql_log(ql_log_warn, vha, 0x7004,
  632. "dma alloc failed for rsp_data.\n");
  633. rval = -ENOMEM;
  634. goto done_free_dma_req;
  635. }
  636. /* Copy the request buffer in req_data now */
  637. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  638. bsg_job->request_payload.sg_cnt, req_data, req_data_len);
  639. elreq.send_dma = req_data_dma;
  640. elreq.rcv_dma = rsp_data_dma;
  641. elreq.transfer_size = req_data_len;
  642. elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  643. if (atomic_read(&vha->loop_state) == LOOP_READY &&
  644. (ha->current_topology == ISP_CFG_F ||
  645. ((IS_QLA81XX(ha) || IS_QLA8031(ha)) &&
  646. le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
  647. && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
  648. elreq.options == EXTERNAL_LOOPBACK) {
  649. type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
  650. ql_dbg(ql_dbg_user, vha, 0x701e,
  651. "BSG request type: %s.\n", type);
  652. command_sent = INT_DEF_LB_ECHO_CMD;
  653. rval = qla2x00_echo_test(vha, &elreq, response);
  654. } else {
  655. if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
  656. memset(config, 0, sizeof(config));
  657. memset(new_config, 0, sizeof(new_config));
  658. if (qla81xx_get_port_config(vha, config)) {
  659. ql_log(ql_log_warn, vha, 0x701f,
  660. "Get port config failed.\n");
  661. rval = -EPERM;
  662. goto done_free_dma_rsp;
  663. }
  664. ql_dbg(ql_dbg_user, vha, 0x70c0,
  665. "elreq.options=%04x\n", elreq.options);
  666. if (elreq.options == EXTERNAL_LOOPBACK)
  667. if (IS_QLA8031(ha))
  668. rval = qla81xx_set_loopback_mode(vha,
  669. config, new_config, elreq.options);
  670. else
  671. rval = qla81xx_reset_loopback_mode(vha,
  672. config, 1);
  673. else
  674. rval = qla81xx_set_loopback_mode(vha, config,
  675. new_config, elreq.options);
  676. if (rval) {
  677. rval = -EPERM;
  678. goto done_free_dma_rsp;
  679. }
  680. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  681. ql_dbg(ql_dbg_user, vha, 0x7028,
  682. "BSG request type: %s.\n", type);
  683. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  684. rval = qla2x00_loopback_test(vha, &elreq, response);
  685. if (new_config[0]) {
  686. /* Revert back to original port config
  687. * Also clear internal loopback
  688. */
  689. qla81xx_reset_loopback_mode(vha,
  690. new_config, 0);
  691. }
  692. if (response[0] == MBS_COMMAND_ERROR &&
  693. response[1] == MBS_LB_RESET) {
  694. ql_log(ql_log_warn, vha, 0x7029,
  695. "MBX command error, Aborting ISP.\n");
  696. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  697. qla2xxx_wake_dpc(vha);
  698. qla2x00_wait_for_chip_reset(vha);
  699. /* Also reset the MPI */
  700. if (qla81xx_restart_mpi_firmware(vha) !=
  701. QLA_SUCCESS) {
  702. ql_log(ql_log_warn, vha, 0x702a,
  703. "MPI reset failed.\n");
  704. }
  705. rval = -EIO;
  706. goto done_free_dma_rsp;
  707. }
  708. } else {
  709. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  710. ql_dbg(ql_dbg_user, vha, 0x702b,
  711. "BSG request type: %s.\n", type);
  712. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  713. rval = qla2x00_loopback_test(vha, &elreq, response);
  714. }
  715. }
  716. if (rval) {
  717. ql_log(ql_log_warn, vha, 0x702c,
  718. "Vendor request %s failed.\n", type);
  719. rval = 0;
  720. bsg_job->reply->result = (DID_ERROR << 16);
  721. bsg_job->reply->reply_payload_rcv_len = 0;
  722. } else {
  723. ql_dbg(ql_dbg_user, vha, 0x702d,
  724. "Vendor request %s completed.\n", type);
  725. bsg_job->reply->result = (DID_OK << 16);
  726. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  727. bsg_job->reply_payload.sg_cnt, rsp_data,
  728. rsp_data_len);
  729. }
  730. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  731. sizeof(response) + sizeof(uint8_t);
  732. fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
  733. sizeof(struct fc_bsg_reply);
  734. memcpy(fw_sts_ptr, response, sizeof(response));
  735. fw_sts_ptr += sizeof(response);
  736. *fw_sts_ptr = command_sent;
  737. done_free_dma_rsp:
  738. dma_free_coherent(&ha->pdev->dev, rsp_data_len,
  739. rsp_data, rsp_data_dma);
  740. done_free_dma_req:
  741. dma_free_coherent(&ha->pdev->dev, req_data_len,
  742. req_data, req_data_dma);
  743. done_unmap_sg:
  744. dma_unmap_sg(&ha->pdev->dev,
  745. bsg_job->reply_payload.sg_list,
  746. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  747. done_unmap_req_sg:
  748. dma_unmap_sg(&ha->pdev->dev,
  749. bsg_job->request_payload.sg_list,
  750. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  751. if (!rval)
  752. bsg_job->job_done(bsg_job);
  753. return rval;
  754. }
  755. static int
  756. qla84xx_reset(struct fc_bsg_job *bsg_job)
  757. {
  758. struct Scsi_Host *host = bsg_job->shost;
  759. scsi_qla_host_t *vha = shost_priv(host);
  760. struct qla_hw_data *ha = vha->hw;
  761. int rval = 0;
  762. uint32_t flag;
  763. if (!IS_QLA84XX(ha)) {
  764. ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
  765. return -EINVAL;
  766. }
  767. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  768. rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
  769. if (rval) {
  770. ql_log(ql_log_warn, vha, 0x7030,
  771. "Vendor request 84xx reset failed.\n");
  772. rval = (DID_ERROR << 16);
  773. } else {
  774. ql_dbg(ql_dbg_user, vha, 0x7031,
  775. "Vendor request 84xx reset completed.\n");
  776. bsg_job->reply->result = DID_OK;
  777. bsg_job->job_done(bsg_job);
  778. }
  779. return rval;
  780. }
  781. static int
  782. qla84xx_updatefw(struct fc_bsg_job *bsg_job)
  783. {
  784. struct Scsi_Host *host = bsg_job->shost;
  785. scsi_qla_host_t *vha = shost_priv(host);
  786. struct qla_hw_data *ha = vha->hw;
  787. struct verify_chip_entry_84xx *mn = NULL;
  788. dma_addr_t mn_dma, fw_dma;
  789. void *fw_buf = NULL;
  790. int rval = 0;
  791. uint32_t sg_cnt;
  792. uint32_t data_len;
  793. uint16_t options;
  794. uint32_t flag;
  795. uint32_t fw_ver;
  796. if (!IS_QLA84XX(ha)) {
  797. ql_dbg(ql_dbg_user, vha, 0x7032,
  798. "Not 84xx, exiting.\n");
  799. return -EINVAL;
  800. }
  801. sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  802. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  803. if (!sg_cnt) {
  804. ql_log(ql_log_warn, vha, 0x7033,
  805. "dma_map_sg returned %d for request.\n", sg_cnt);
  806. return -ENOMEM;
  807. }
  808. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  809. ql_log(ql_log_warn, vha, 0x7034,
  810. "DMA mapping resulted in different sg counts, "
  811. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  812. bsg_job->request_payload.sg_cnt, sg_cnt);
  813. rval = -EAGAIN;
  814. goto done_unmap_sg;
  815. }
  816. data_len = bsg_job->request_payload.payload_len;
  817. fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
  818. &fw_dma, GFP_KERNEL);
  819. if (!fw_buf) {
  820. ql_log(ql_log_warn, vha, 0x7035,
  821. "DMA alloc failed for fw_buf.\n");
  822. rval = -ENOMEM;
  823. goto done_unmap_sg;
  824. }
  825. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  826. bsg_job->request_payload.sg_cnt, fw_buf, data_len);
  827. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  828. if (!mn) {
  829. ql_log(ql_log_warn, vha, 0x7036,
  830. "DMA alloc failed for fw buffer.\n");
  831. rval = -ENOMEM;
  832. goto done_free_fw_buf;
  833. }
  834. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  835. fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
  836. memset(mn, 0, sizeof(struct access_chip_84xx));
  837. mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
  838. mn->entry_count = 1;
  839. options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
  840. if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
  841. options |= VCO_DIAG_FW;
  842. mn->options = cpu_to_le16(options);
  843. mn->fw_ver = cpu_to_le32(fw_ver);
  844. mn->fw_size = cpu_to_le32(data_len);
  845. mn->fw_seq_size = cpu_to_le32(data_len);
  846. mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
  847. mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
  848. mn->dseg_length = cpu_to_le32(data_len);
  849. mn->data_seg_cnt = cpu_to_le16(1);
  850. rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
  851. if (rval) {
  852. ql_log(ql_log_warn, vha, 0x7037,
  853. "Vendor request 84xx updatefw failed.\n");
  854. rval = (DID_ERROR << 16);
  855. } else {
  856. ql_dbg(ql_dbg_user, vha, 0x7038,
  857. "Vendor request 84xx updatefw completed.\n");
  858. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  859. bsg_job->reply->result = DID_OK;
  860. }
  861. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  862. done_free_fw_buf:
  863. dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
  864. done_unmap_sg:
  865. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  866. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  867. if (!rval)
  868. bsg_job->job_done(bsg_job);
  869. return rval;
  870. }
  871. static int
  872. qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
  873. {
  874. struct Scsi_Host *host = bsg_job->shost;
  875. scsi_qla_host_t *vha = shost_priv(host);
  876. struct qla_hw_data *ha = vha->hw;
  877. struct access_chip_84xx *mn = NULL;
  878. dma_addr_t mn_dma, mgmt_dma;
  879. void *mgmt_b = NULL;
  880. int rval = 0;
  881. struct qla_bsg_a84_mgmt *ql84_mgmt;
  882. uint32_t sg_cnt;
  883. uint32_t data_len = 0;
  884. uint32_t dma_direction = DMA_NONE;
  885. if (!IS_QLA84XX(ha)) {
  886. ql_log(ql_log_warn, vha, 0x703a,
  887. "Not 84xx, exiting.\n");
  888. return -EINVAL;
  889. }
  890. ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
  891. sizeof(struct fc_bsg_request));
  892. if (!ql84_mgmt) {
  893. ql_log(ql_log_warn, vha, 0x703b,
  894. "MGMT header not provided, exiting.\n");
  895. return -EINVAL;
  896. }
  897. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  898. if (!mn) {
  899. ql_log(ql_log_warn, vha, 0x703c,
  900. "DMA alloc failed for fw buffer.\n");
  901. return -ENOMEM;
  902. }
  903. memset(mn, 0, sizeof(struct access_chip_84xx));
  904. mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
  905. mn->entry_count = 1;
  906. switch (ql84_mgmt->mgmt.cmd) {
  907. case QLA84_MGMT_READ_MEM:
  908. case QLA84_MGMT_GET_INFO:
  909. sg_cnt = dma_map_sg(&ha->pdev->dev,
  910. bsg_job->reply_payload.sg_list,
  911. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  912. if (!sg_cnt) {
  913. ql_log(ql_log_warn, vha, 0x703d,
  914. "dma_map_sg returned %d for reply.\n", sg_cnt);
  915. rval = -ENOMEM;
  916. goto exit_mgmt;
  917. }
  918. dma_direction = DMA_FROM_DEVICE;
  919. if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
  920. ql_log(ql_log_warn, vha, 0x703e,
  921. "DMA mapping resulted in different sg counts, "
  922. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  923. bsg_job->reply_payload.sg_cnt, sg_cnt);
  924. rval = -EAGAIN;
  925. goto done_unmap_sg;
  926. }
  927. data_len = bsg_job->reply_payload.payload_len;
  928. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  929. &mgmt_dma, GFP_KERNEL);
  930. if (!mgmt_b) {
  931. ql_log(ql_log_warn, vha, 0x703f,
  932. "DMA alloc failed for mgmt_b.\n");
  933. rval = -ENOMEM;
  934. goto done_unmap_sg;
  935. }
  936. if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
  937. mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
  938. mn->parameter1 =
  939. cpu_to_le32(
  940. ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  941. } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
  942. mn->options = cpu_to_le16(ACO_REQUEST_INFO);
  943. mn->parameter1 =
  944. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
  945. mn->parameter2 =
  946. cpu_to_le32(
  947. ql84_mgmt->mgmt.mgmtp.u.info.context);
  948. }
  949. break;
  950. case QLA84_MGMT_WRITE_MEM:
  951. sg_cnt = dma_map_sg(&ha->pdev->dev,
  952. bsg_job->request_payload.sg_list,
  953. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  954. if (!sg_cnt) {
  955. ql_log(ql_log_warn, vha, 0x7040,
  956. "dma_map_sg returned %d.\n", sg_cnt);
  957. rval = -ENOMEM;
  958. goto exit_mgmt;
  959. }
  960. dma_direction = DMA_TO_DEVICE;
  961. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  962. ql_log(ql_log_warn, vha, 0x7041,
  963. "DMA mapping resulted in different sg counts, "
  964. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  965. bsg_job->request_payload.sg_cnt, sg_cnt);
  966. rval = -EAGAIN;
  967. goto done_unmap_sg;
  968. }
  969. data_len = bsg_job->request_payload.payload_len;
  970. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  971. &mgmt_dma, GFP_KERNEL);
  972. if (!mgmt_b) {
  973. ql_log(ql_log_warn, vha, 0x7042,
  974. "DMA alloc failed for mgmt_b.\n");
  975. rval = -ENOMEM;
  976. goto done_unmap_sg;
  977. }
  978. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  979. bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
  980. mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
  981. mn->parameter1 =
  982. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  983. break;
  984. case QLA84_MGMT_CHNG_CONFIG:
  985. mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
  986. mn->parameter1 =
  987. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
  988. mn->parameter2 =
  989. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
  990. mn->parameter3 =
  991. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
  992. break;
  993. default:
  994. rval = -EIO;
  995. goto exit_mgmt;
  996. }
  997. if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
  998. mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
  999. mn->dseg_count = cpu_to_le16(1);
  1000. mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
  1001. mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
  1002. mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
  1003. }
  1004. rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
  1005. if (rval) {
  1006. ql_log(ql_log_warn, vha, 0x7043,
  1007. "Vendor request 84xx mgmt failed.\n");
  1008. rval = (DID_ERROR << 16);
  1009. } else {
  1010. ql_dbg(ql_dbg_user, vha, 0x7044,
  1011. "Vendor request 84xx mgmt completed.\n");
  1012. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1013. bsg_job->reply->result = DID_OK;
  1014. if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
  1015. (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
  1016. bsg_job->reply->reply_payload_rcv_len =
  1017. bsg_job->reply_payload.payload_len;
  1018. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1019. bsg_job->reply_payload.sg_cnt, mgmt_b,
  1020. data_len);
  1021. }
  1022. }
  1023. done_unmap_sg:
  1024. if (mgmt_b)
  1025. dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
  1026. if (dma_direction == DMA_TO_DEVICE)
  1027. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  1028. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1029. else if (dma_direction == DMA_FROM_DEVICE)
  1030. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  1031. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1032. exit_mgmt:
  1033. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  1034. if (!rval)
  1035. bsg_job->job_done(bsg_job);
  1036. return rval;
  1037. }
  1038. static int
  1039. qla24xx_iidma(struct fc_bsg_job *bsg_job)
  1040. {
  1041. struct Scsi_Host *host = bsg_job->shost;
  1042. scsi_qla_host_t *vha = shost_priv(host);
  1043. int rval = 0;
  1044. struct qla_port_param *port_param = NULL;
  1045. fc_port_t *fcport = NULL;
  1046. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1047. uint8_t *rsp_ptr = NULL;
  1048. if (!IS_IIDMA_CAPABLE(vha->hw)) {
  1049. ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
  1050. return -EINVAL;
  1051. }
  1052. port_param = (struct qla_port_param *)((char *)bsg_job->request +
  1053. sizeof(struct fc_bsg_request));
  1054. if (!port_param) {
  1055. ql_log(ql_log_warn, vha, 0x7047,
  1056. "port_param header not provided.\n");
  1057. return -EINVAL;
  1058. }
  1059. if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
  1060. ql_log(ql_log_warn, vha, 0x7048,
  1061. "Invalid destination type.\n");
  1062. return -EINVAL;
  1063. }
  1064. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  1065. if (fcport->port_type != FCT_TARGET)
  1066. continue;
  1067. if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
  1068. fcport->port_name, sizeof(fcport->port_name)))
  1069. continue;
  1070. break;
  1071. }
  1072. if (!fcport) {
  1073. ql_log(ql_log_warn, vha, 0x7049,
  1074. "Failed to find port.\n");
  1075. return -EINVAL;
  1076. }
  1077. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  1078. ql_log(ql_log_warn, vha, 0x704a,
  1079. "Port is not online.\n");
  1080. return -EINVAL;
  1081. }
  1082. if (fcport->flags & FCF_LOGIN_NEEDED) {
  1083. ql_log(ql_log_warn, vha, 0x704b,
  1084. "Remote port not logged in flags = 0x%x.\n", fcport->flags);
  1085. return -EINVAL;
  1086. }
  1087. if (port_param->mode)
  1088. rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
  1089. port_param->speed, mb);
  1090. else
  1091. rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
  1092. &port_param->speed, mb);
  1093. if (rval) {
  1094. ql_log(ql_log_warn, vha, 0x704c,
  1095. "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
  1096. "%04x %x %04x %04x.\n", fcport->port_name[0],
  1097. fcport->port_name[1], fcport->port_name[2],
  1098. fcport->port_name[3], fcport->port_name[4],
  1099. fcport->port_name[5], fcport->port_name[6],
  1100. fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
  1101. rval = (DID_ERROR << 16);
  1102. } else {
  1103. if (!port_param->mode) {
  1104. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  1105. sizeof(struct qla_port_param);
  1106. rsp_ptr = ((uint8_t *)bsg_job->reply) +
  1107. sizeof(struct fc_bsg_reply);
  1108. memcpy(rsp_ptr, port_param,
  1109. sizeof(struct qla_port_param));
  1110. }
  1111. bsg_job->reply->result = DID_OK;
  1112. bsg_job->job_done(bsg_job);
  1113. }
  1114. return rval;
  1115. }
  1116. static int
  1117. qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
  1118. uint8_t is_update)
  1119. {
  1120. uint32_t start = 0;
  1121. int valid = 0;
  1122. struct qla_hw_data *ha = vha->hw;
  1123. if (unlikely(pci_channel_offline(ha->pdev)))
  1124. return -EINVAL;
  1125. start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  1126. if (start > ha->optrom_size) {
  1127. ql_log(ql_log_warn, vha, 0x7055,
  1128. "start %d > optrom_size %d.\n", start, ha->optrom_size);
  1129. return -EINVAL;
  1130. }
  1131. if (ha->optrom_state != QLA_SWAITING) {
  1132. ql_log(ql_log_info, vha, 0x7056,
  1133. "optrom_state %d.\n", ha->optrom_state);
  1134. return -EBUSY;
  1135. }
  1136. ha->optrom_region_start = start;
  1137. ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
  1138. if (is_update) {
  1139. if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
  1140. valid = 1;
  1141. else if (start == (ha->flt_region_boot * 4) ||
  1142. start == (ha->flt_region_fw * 4))
  1143. valid = 1;
  1144. else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
  1145. IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
  1146. valid = 1;
  1147. if (!valid) {
  1148. ql_log(ql_log_warn, vha, 0x7058,
  1149. "Invalid start region 0x%x/0x%x.\n", start,
  1150. bsg_job->request_payload.payload_len);
  1151. return -EINVAL;
  1152. }
  1153. ha->optrom_region_size = start +
  1154. bsg_job->request_payload.payload_len > ha->optrom_size ?
  1155. ha->optrom_size - start :
  1156. bsg_job->request_payload.payload_len;
  1157. ha->optrom_state = QLA_SWRITING;
  1158. } else {
  1159. ha->optrom_region_size = start +
  1160. bsg_job->reply_payload.payload_len > ha->optrom_size ?
  1161. ha->optrom_size - start :
  1162. bsg_job->reply_payload.payload_len;
  1163. ha->optrom_state = QLA_SREADING;
  1164. }
  1165. ha->optrom_buffer = vmalloc(ha->optrom_region_size);
  1166. if (!ha->optrom_buffer) {
  1167. ql_log(ql_log_warn, vha, 0x7059,
  1168. "Read: Unable to allocate memory for optrom retrieval "
  1169. "(%x)\n", ha->optrom_region_size);
  1170. ha->optrom_state = QLA_SWAITING;
  1171. return -ENOMEM;
  1172. }
  1173. memset(ha->optrom_buffer, 0, ha->optrom_region_size);
  1174. return 0;
  1175. }
  1176. static int
  1177. qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
  1178. {
  1179. struct Scsi_Host *host = bsg_job->shost;
  1180. scsi_qla_host_t *vha = shost_priv(host);
  1181. struct qla_hw_data *ha = vha->hw;
  1182. int rval = 0;
  1183. if (ha->flags.nic_core_reset_hdlr_active)
  1184. return -EBUSY;
  1185. rval = qla2x00_optrom_setup(bsg_job, vha, 0);
  1186. if (rval)
  1187. return rval;
  1188. ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
  1189. ha->optrom_region_start, ha->optrom_region_size);
  1190. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1191. bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
  1192. ha->optrom_region_size);
  1193. bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
  1194. bsg_job->reply->result = DID_OK;
  1195. vfree(ha->optrom_buffer);
  1196. ha->optrom_buffer = NULL;
  1197. ha->optrom_state = QLA_SWAITING;
  1198. bsg_job->job_done(bsg_job);
  1199. return rval;
  1200. }
  1201. static int
  1202. qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
  1203. {
  1204. struct Scsi_Host *host = bsg_job->shost;
  1205. scsi_qla_host_t *vha = shost_priv(host);
  1206. struct qla_hw_data *ha = vha->hw;
  1207. int rval = 0;
  1208. rval = qla2x00_optrom_setup(bsg_job, vha, 1);
  1209. if (rval)
  1210. return rval;
  1211. /* Set the isp82xx_no_md_cap not to capture minidump */
  1212. ha->flags.isp82xx_no_md_cap = 1;
  1213. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1214. bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
  1215. ha->optrom_region_size);
  1216. ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
  1217. ha->optrom_region_start, ha->optrom_region_size);
  1218. bsg_job->reply->result = DID_OK;
  1219. vfree(ha->optrom_buffer);
  1220. ha->optrom_buffer = NULL;
  1221. ha->optrom_state = QLA_SWAITING;
  1222. bsg_job->job_done(bsg_job);
  1223. return rval;
  1224. }
  1225. static int
  1226. qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
  1227. {
  1228. struct Scsi_Host *host = bsg_job->shost;
  1229. scsi_qla_host_t *vha = shost_priv(host);
  1230. struct qla_hw_data *ha = vha->hw;
  1231. int rval = 0;
  1232. uint8_t bsg[DMA_POOL_SIZE];
  1233. struct qla_image_version_list *list = (void *)bsg;
  1234. struct qla_image_version *image;
  1235. uint32_t count;
  1236. dma_addr_t sfp_dma;
  1237. void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1238. if (!sfp) {
  1239. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1240. EXT_STATUS_NO_MEMORY;
  1241. goto done;
  1242. }
  1243. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1244. bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
  1245. image = list->version;
  1246. count = list->count;
  1247. while (count--) {
  1248. memcpy(sfp, &image->field_info, sizeof(image->field_info));
  1249. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1250. image->field_address.device, image->field_address.offset,
  1251. sizeof(image->field_info), image->field_address.option);
  1252. if (rval) {
  1253. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1254. EXT_STATUS_MAILBOX;
  1255. goto dealloc;
  1256. }
  1257. image++;
  1258. }
  1259. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1260. dealloc:
  1261. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1262. done:
  1263. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1264. bsg_job->reply->result = DID_OK << 16;
  1265. bsg_job->job_done(bsg_job);
  1266. return 0;
  1267. }
  1268. static int
  1269. qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
  1270. {
  1271. struct Scsi_Host *host = bsg_job->shost;
  1272. scsi_qla_host_t *vha = shost_priv(host);
  1273. struct qla_hw_data *ha = vha->hw;
  1274. int rval = 0;
  1275. uint8_t bsg[DMA_POOL_SIZE];
  1276. struct qla_status_reg *sr = (void *)bsg;
  1277. dma_addr_t sfp_dma;
  1278. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1279. if (!sfp) {
  1280. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1281. EXT_STATUS_NO_MEMORY;
  1282. goto done;
  1283. }
  1284. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1285. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1286. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1287. sr->field_address.device, sr->field_address.offset,
  1288. sizeof(sr->status_reg), sr->field_address.option);
  1289. sr->status_reg = *sfp;
  1290. if (rval) {
  1291. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1292. EXT_STATUS_MAILBOX;
  1293. goto dealloc;
  1294. }
  1295. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1296. bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
  1297. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1298. dealloc:
  1299. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1300. done:
  1301. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1302. bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
  1303. bsg_job->reply->result = DID_OK << 16;
  1304. bsg_job->job_done(bsg_job);
  1305. return 0;
  1306. }
  1307. static int
  1308. qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
  1309. {
  1310. struct Scsi_Host *host = bsg_job->shost;
  1311. scsi_qla_host_t *vha = shost_priv(host);
  1312. struct qla_hw_data *ha = vha->hw;
  1313. int rval = 0;
  1314. uint8_t bsg[DMA_POOL_SIZE];
  1315. struct qla_status_reg *sr = (void *)bsg;
  1316. dma_addr_t sfp_dma;
  1317. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1318. if (!sfp) {
  1319. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1320. EXT_STATUS_NO_MEMORY;
  1321. goto done;
  1322. }
  1323. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1324. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1325. *sfp = sr->status_reg;
  1326. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1327. sr->field_address.device, sr->field_address.offset,
  1328. sizeof(sr->status_reg), sr->field_address.option);
  1329. if (rval) {
  1330. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1331. EXT_STATUS_MAILBOX;
  1332. goto dealloc;
  1333. }
  1334. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1335. dealloc:
  1336. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1337. done:
  1338. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1339. bsg_job->reply->result = DID_OK << 16;
  1340. bsg_job->job_done(bsg_job);
  1341. return 0;
  1342. }
  1343. static int
  1344. qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
  1345. {
  1346. struct Scsi_Host *host = bsg_job->shost;
  1347. scsi_qla_host_t *vha = shost_priv(host);
  1348. struct qla_hw_data *ha = vha->hw;
  1349. int rval = 0;
  1350. uint8_t bsg[DMA_POOL_SIZE];
  1351. struct qla_i2c_access *i2c = (void *)bsg;
  1352. dma_addr_t sfp_dma;
  1353. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1354. if (!sfp) {
  1355. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1356. EXT_STATUS_NO_MEMORY;
  1357. goto done;
  1358. }
  1359. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1360. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1361. memcpy(sfp, i2c->buffer, i2c->length);
  1362. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1363. i2c->device, i2c->offset, i2c->length, i2c->option);
  1364. if (rval) {
  1365. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1366. EXT_STATUS_MAILBOX;
  1367. goto dealloc;
  1368. }
  1369. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1370. dealloc:
  1371. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1372. done:
  1373. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1374. bsg_job->reply->result = DID_OK << 16;
  1375. bsg_job->job_done(bsg_job);
  1376. return 0;
  1377. }
  1378. static int
  1379. qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
  1380. {
  1381. struct Scsi_Host *host = bsg_job->shost;
  1382. scsi_qla_host_t *vha = shost_priv(host);
  1383. struct qla_hw_data *ha = vha->hw;
  1384. int rval = 0;
  1385. uint8_t bsg[DMA_POOL_SIZE];
  1386. struct qla_i2c_access *i2c = (void *)bsg;
  1387. dma_addr_t sfp_dma;
  1388. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1389. if (!sfp) {
  1390. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1391. EXT_STATUS_NO_MEMORY;
  1392. goto done;
  1393. }
  1394. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1395. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1396. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1397. i2c->device, i2c->offset, i2c->length, i2c->option);
  1398. if (rval) {
  1399. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1400. EXT_STATUS_MAILBOX;
  1401. goto dealloc;
  1402. }
  1403. memcpy(i2c->buffer, sfp, i2c->length);
  1404. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1405. bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
  1406. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1407. dealloc:
  1408. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1409. done:
  1410. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1411. bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
  1412. bsg_job->reply->result = DID_OK << 16;
  1413. bsg_job->job_done(bsg_job);
  1414. return 0;
  1415. }
  1416. static int
  1417. qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
  1418. {
  1419. struct Scsi_Host *host = bsg_job->shost;
  1420. scsi_qla_host_t *vha = shost_priv(host);
  1421. struct qla_hw_data *ha = vha->hw;
  1422. uint16_t thread_id;
  1423. uint32_t rval = EXT_STATUS_OK;
  1424. uint16_t req_sg_cnt = 0;
  1425. uint16_t rsp_sg_cnt = 0;
  1426. uint16_t nextlid = 0;
  1427. uint32_t tot_dsds;
  1428. srb_t *sp = NULL;
  1429. uint32_t req_data_len = 0;
  1430. uint32_t rsp_data_len = 0;
  1431. /* Check the type of the adapter */
  1432. if (!IS_BIDI_CAPABLE(ha)) {
  1433. ql_log(ql_log_warn, vha, 0x70a0,
  1434. "This adapter is not supported\n");
  1435. rval = EXT_STATUS_NOT_SUPPORTED;
  1436. goto done;
  1437. }
  1438. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  1439. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  1440. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  1441. rval = EXT_STATUS_BUSY;
  1442. goto done;
  1443. }
  1444. /* Check if host is online */
  1445. if (!vha->flags.online) {
  1446. ql_log(ql_log_warn, vha, 0x70a1,
  1447. "Host is not online\n");
  1448. rval = EXT_STATUS_DEVICE_OFFLINE;
  1449. goto done;
  1450. }
  1451. /* Check if cable is plugged in or not */
  1452. if (vha->device_flags & DFLG_NO_CABLE) {
  1453. ql_log(ql_log_warn, vha, 0x70a2,
  1454. "Cable is unplugged...\n");
  1455. rval = EXT_STATUS_INVALID_CFG;
  1456. goto done;
  1457. }
  1458. /* Check if the switch is connected or not */
  1459. if (ha->current_topology != ISP_CFG_F) {
  1460. ql_log(ql_log_warn, vha, 0x70a3,
  1461. "Host is not connected to the switch\n");
  1462. rval = EXT_STATUS_INVALID_CFG;
  1463. goto done;
  1464. }
  1465. /* Check if operating mode is P2P */
  1466. if (ha->operating_mode != P2P) {
  1467. ql_log(ql_log_warn, vha, 0x70a4,
  1468. "Host is operating mode is not P2p\n");
  1469. rval = EXT_STATUS_INVALID_CFG;
  1470. goto done;
  1471. }
  1472. thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  1473. mutex_lock(&ha->selflogin_lock);
  1474. if (vha->self_login_loop_id == 0) {
  1475. /* Initialize all required fields of fcport */
  1476. vha->bidir_fcport.vha = vha;
  1477. vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
  1478. vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
  1479. vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
  1480. vha->bidir_fcport.loop_id = vha->loop_id;
  1481. if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
  1482. ql_log(ql_log_warn, vha, 0x70a7,
  1483. "Failed to login port %06X for bidirectional IOCB\n",
  1484. vha->bidir_fcport.d_id.b24);
  1485. mutex_unlock(&ha->selflogin_lock);
  1486. rval = EXT_STATUS_MAILBOX;
  1487. goto done;
  1488. }
  1489. vha->self_login_loop_id = nextlid - 1;
  1490. }
  1491. /* Assign the self login loop id to fcport */
  1492. mutex_unlock(&ha->selflogin_lock);
  1493. vha->bidir_fcport.loop_id = vha->self_login_loop_id;
  1494. req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1495. bsg_job->request_payload.sg_list,
  1496. bsg_job->request_payload.sg_cnt,
  1497. DMA_TO_DEVICE);
  1498. if (!req_sg_cnt) {
  1499. rval = EXT_STATUS_NO_MEMORY;
  1500. goto done;
  1501. }
  1502. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1503. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  1504. DMA_FROM_DEVICE);
  1505. if (!rsp_sg_cnt) {
  1506. rval = EXT_STATUS_NO_MEMORY;
  1507. goto done_unmap_req_sg;
  1508. }
  1509. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  1510. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  1511. ql_dbg(ql_dbg_user, vha, 0x70a9,
  1512. "Dma mapping resulted in different sg counts "
  1513. "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
  1514. "%x dma_reply_sg_cnt: %x]\n",
  1515. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  1516. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  1517. rval = EXT_STATUS_NO_MEMORY;
  1518. goto done_unmap_sg;
  1519. }
  1520. if (req_data_len != rsp_data_len) {
  1521. rval = EXT_STATUS_BUSY;
  1522. ql_log(ql_log_warn, vha, 0x70aa,
  1523. "req_data_len != rsp_data_len\n");
  1524. goto done_unmap_sg;
  1525. }
  1526. req_data_len = bsg_job->request_payload.payload_len;
  1527. rsp_data_len = bsg_job->reply_payload.payload_len;
  1528. /* Alloc SRB structure */
  1529. sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
  1530. if (!sp) {
  1531. ql_dbg(ql_dbg_user, vha, 0x70ac,
  1532. "Alloc SRB structure failed\n");
  1533. rval = EXT_STATUS_NO_MEMORY;
  1534. goto done_unmap_sg;
  1535. }
  1536. /*Populate srb->ctx with bidir ctx*/
  1537. sp->u.bsg_job = bsg_job;
  1538. sp->free = qla2x00_bsg_sp_free;
  1539. sp->type = SRB_BIDI_CMD;
  1540. sp->done = qla2x00_bsg_job_done;
  1541. /* Add the read and write sg count */
  1542. tot_dsds = rsp_sg_cnt + req_sg_cnt;
  1543. rval = qla2x00_start_bidir(sp, vha, tot_dsds);
  1544. if (rval != EXT_STATUS_OK)
  1545. goto done_free_srb;
  1546. /* the bsg request will be completed in the interrupt handler */
  1547. return rval;
  1548. done_free_srb:
  1549. mempool_free(sp, ha->srb_mempool);
  1550. done_unmap_sg:
  1551. dma_unmap_sg(&ha->pdev->dev,
  1552. bsg_job->reply_payload.sg_list,
  1553. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1554. done_unmap_req_sg:
  1555. dma_unmap_sg(&ha->pdev->dev,
  1556. bsg_job->request_payload.sg_list,
  1557. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1558. done:
  1559. /* Return an error vendor specific response
  1560. * and complete the bsg request
  1561. */
  1562. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
  1563. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1564. bsg_job->reply->reply_payload_rcv_len = 0;
  1565. bsg_job->reply->result = (DID_OK) << 16;
  1566. bsg_job->job_done(bsg_job);
  1567. /* Always retrun success, vendor rsp carries correct status */
  1568. return 0;
  1569. }
  1570. static int
  1571. qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
  1572. {
  1573. switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
  1574. case QL_VND_LOOPBACK:
  1575. return qla2x00_process_loopback(bsg_job);
  1576. case QL_VND_A84_RESET:
  1577. return qla84xx_reset(bsg_job);
  1578. case QL_VND_A84_UPDATE_FW:
  1579. return qla84xx_updatefw(bsg_job);
  1580. case QL_VND_A84_MGMT_CMD:
  1581. return qla84xx_mgmt_cmd(bsg_job);
  1582. case QL_VND_IIDMA:
  1583. return qla24xx_iidma(bsg_job);
  1584. case QL_VND_FCP_PRIO_CFG_CMD:
  1585. return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
  1586. case QL_VND_READ_FLASH:
  1587. return qla2x00_read_optrom(bsg_job);
  1588. case QL_VND_UPDATE_FLASH:
  1589. return qla2x00_update_optrom(bsg_job);
  1590. case QL_VND_SET_FRU_VERSION:
  1591. return qla2x00_update_fru_versions(bsg_job);
  1592. case QL_VND_READ_FRU_STATUS:
  1593. return qla2x00_read_fru_status(bsg_job);
  1594. case QL_VND_WRITE_FRU_STATUS:
  1595. return qla2x00_write_fru_status(bsg_job);
  1596. case QL_VND_WRITE_I2C:
  1597. return qla2x00_write_i2c(bsg_job);
  1598. case QL_VND_READ_I2C:
  1599. return qla2x00_read_i2c(bsg_job);
  1600. case QL_VND_DIAG_IO_CMD:
  1601. return qla24xx_process_bidir_cmd(bsg_job);
  1602. default:
  1603. return -ENOSYS;
  1604. }
  1605. }
  1606. int
  1607. qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
  1608. {
  1609. int ret = -EINVAL;
  1610. struct fc_rport *rport;
  1611. fc_port_t *fcport = NULL;
  1612. struct Scsi_Host *host;
  1613. scsi_qla_host_t *vha;
  1614. /* In case no data transferred. */
  1615. bsg_job->reply->reply_payload_rcv_len = 0;
  1616. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  1617. rport = bsg_job->rport;
  1618. fcport = *(fc_port_t **) rport->dd_data;
  1619. host = rport_to_shost(rport);
  1620. vha = shost_priv(host);
  1621. } else {
  1622. host = bsg_job->shost;
  1623. vha = shost_priv(host);
  1624. }
  1625. if (qla2x00_reset_active(vha)) {
  1626. ql_dbg(ql_dbg_user, vha, 0x709f,
  1627. "BSG: ISP abort active/needed -- cmd=%d.\n",
  1628. bsg_job->request->msgcode);
  1629. return -EBUSY;
  1630. }
  1631. ql_dbg(ql_dbg_user, vha, 0x7000,
  1632. "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
  1633. switch (bsg_job->request->msgcode) {
  1634. case FC_BSG_RPT_ELS:
  1635. case FC_BSG_HST_ELS_NOLOGIN:
  1636. ret = qla2x00_process_els(bsg_job);
  1637. break;
  1638. case FC_BSG_HST_CT:
  1639. ret = qla2x00_process_ct(bsg_job);
  1640. break;
  1641. case FC_BSG_HST_VENDOR:
  1642. ret = qla2x00_process_vendor_specific(bsg_job);
  1643. break;
  1644. case FC_BSG_HST_ADD_RPORT:
  1645. case FC_BSG_HST_DEL_RPORT:
  1646. case FC_BSG_RPT_CT:
  1647. default:
  1648. ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
  1649. break;
  1650. }
  1651. return ret;
  1652. }
  1653. int
  1654. qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
  1655. {
  1656. scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
  1657. struct qla_hw_data *ha = vha->hw;
  1658. srb_t *sp;
  1659. int cnt, que;
  1660. unsigned long flags;
  1661. struct req_que *req;
  1662. /* find the bsg job from the active list of commands */
  1663. spin_lock_irqsave(&ha->hardware_lock, flags);
  1664. for (que = 0; que < ha->max_req_queues; que++) {
  1665. req = ha->req_q_map[que];
  1666. if (!req)
  1667. continue;
  1668. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  1669. sp = req->outstanding_cmds[cnt];
  1670. if (sp) {
  1671. if (((sp->type == SRB_CT_CMD) ||
  1672. (sp->type == SRB_ELS_CMD_HST))
  1673. && (sp->u.bsg_job == bsg_job)) {
  1674. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1675. if (ha->isp_ops->abort_command(sp)) {
  1676. ql_log(ql_log_warn, vha, 0x7089,
  1677. "mbx abort_command "
  1678. "failed.\n");
  1679. bsg_job->req->errors =
  1680. bsg_job->reply->result = -EIO;
  1681. } else {
  1682. ql_dbg(ql_dbg_user, vha, 0x708a,
  1683. "mbx abort_command "
  1684. "success.\n");
  1685. bsg_job->req->errors =
  1686. bsg_job->reply->result = 0;
  1687. }
  1688. spin_lock_irqsave(&ha->hardware_lock, flags);
  1689. goto done;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1695. ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
  1696. bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
  1697. return 0;
  1698. done:
  1699. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1700. if (bsg_job->request->msgcode == FC_BSG_HST_CT)
  1701. kfree(sp->fcport);
  1702. mempool_free(sp, ha->srb_mempool);
  1703. return 0;
  1704. }