qla_bsg.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2014 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. #include <linux/bsg-lib.h>
  12. /* BSG support for ELS/CT pass through */
  13. void
  14. qla2x00_bsg_job_done(void *ptr, int res)
  15. {
  16. srb_t *sp = ptr;
  17. struct bsg_job *bsg_job = sp->u.bsg_job;
  18. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  19. bsg_reply->result = res;
  20. bsg_job_done(bsg_job, bsg_reply->result,
  21. bsg_reply->reply_payload_rcv_len);
  22. sp->free(sp);
  23. }
  24. void
  25. qla2x00_bsg_sp_free(void *ptr)
  26. {
  27. srb_t *sp = ptr;
  28. struct qla_hw_data *ha = sp->vha->hw;
  29. struct bsg_job *bsg_job = sp->u.bsg_job;
  30. struct fc_bsg_request *bsg_request = bsg_job->request;
  31. struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
  32. if (sp->type == SRB_FXIOCB_BCMD) {
  33. piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
  34. &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  35. if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
  36. dma_unmap_sg(&ha->pdev->dev,
  37. bsg_job->request_payload.sg_list,
  38. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  39. if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
  40. dma_unmap_sg(&ha->pdev->dev,
  41. bsg_job->reply_payload.sg_list,
  42. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  43. } else {
  44. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  45. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  46. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  47. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  48. }
  49. if (sp->type == SRB_CT_CMD ||
  50. sp->type == SRB_FXIOCB_BCMD ||
  51. sp->type == SRB_ELS_CMD_HST)
  52. kfree(sp->fcport);
  53. qla2x00_rel_sp(sp);
  54. }
  55. int
  56. qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
  57. struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
  58. {
  59. int i, ret, num_valid;
  60. uint8_t *bcode;
  61. struct qla_fcp_prio_entry *pri_entry;
  62. uint32_t *bcode_val_ptr, bcode_val;
  63. ret = 1;
  64. num_valid = 0;
  65. bcode = (uint8_t *)pri_cfg;
  66. bcode_val_ptr = (uint32_t *)pri_cfg;
  67. bcode_val = (uint32_t)(*bcode_val_ptr);
  68. if (bcode_val == 0xFFFFFFFF) {
  69. /* No FCP Priority config data in flash */
  70. ql_dbg(ql_dbg_user, vha, 0x7051,
  71. "No FCP Priority config data.\n");
  72. return 0;
  73. }
  74. if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
  75. bcode[3] != 'S') {
  76. /* Invalid FCP priority data header*/
  77. ql_dbg(ql_dbg_user, vha, 0x7052,
  78. "Invalid FCP Priority data header. bcode=0x%x.\n",
  79. bcode_val);
  80. return 0;
  81. }
  82. if (flag != 1)
  83. return ret;
  84. pri_entry = &pri_cfg->entry[0];
  85. for (i = 0; i < pri_cfg->num_entries; i++) {
  86. if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
  87. num_valid++;
  88. pri_entry++;
  89. }
  90. if (num_valid == 0) {
  91. /* No valid FCP priority data entries */
  92. ql_dbg(ql_dbg_user, vha, 0x7053,
  93. "No valid FCP Priority data entries.\n");
  94. ret = 0;
  95. } else {
  96. /* FCP priority data is valid */
  97. ql_dbg(ql_dbg_user, vha, 0x7054,
  98. "Valid FCP priority data. num entries = %d.\n",
  99. num_valid);
  100. }
  101. return ret;
  102. }
  103. static int
  104. qla24xx_proc_fcp_prio_cfg_cmd(struct bsg_job *bsg_job)
  105. {
  106. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  107. struct fc_bsg_request *bsg_request = bsg_job->request;
  108. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  109. scsi_qla_host_t *vha = shost_priv(host);
  110. struct qla_hw_data *ha = vha->hw;
  111. int ret = 0;
  112. uint32_t len;
  113. uint32_t oper;
  114. if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_P3P_TYPE(ha))) {
  115. ret = -EINVAL;
  116. goto exit_fcp_prio_cfg;
  117. }
  118. /* Get the sub command */
  119. oper = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  120. /* Only set config is allowed if config memory is not allocated */
  121. if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
  122. ret = -EINVAL;
  123. goto exit_fcp_prio_cfg;
  124. }
  125. switch (oper) {
  126. case QLFC_FCP_PRIO_DISABLE:
  127. if (ha->flags.fcp_prio_enabled) {
  128. ha->flags.fcp_prio_enabled = 0;
  129. ha->fcp_prio_cfg->attributes &=
  130. ~FCP_PRIO_ATTR_ENABLE;
  131. qla24xx_update_all_fcp_prio(vha);
  132. bsg_reply->result = DID_OK;
  133. } else {
  134. ret = -EINVAL;
  135. bsg_reply->result = (DID_ERROR << 16);
  136. goto exit_fcp_prio_cfg;
  137. }
  138. break;
  139. case QLFC_FCP_PRIO_ENABLE:
  140. if (!ha->flags.fcp_prio_enabled) {
  141. if (ha->fcp_prio_cfg) {
  142. ha->flags.fcp_prio_enabled = 1;
  143. ha->fcp_prio_cfg->attributes |=
  144. FCP_PRIO_ATTR_ENABLE;
  145. qla24xx_update_all_fcp_prio(vha);
  146. bsg_reply->result = DID_OK;
  147. } else {
  148. ret = -EINVAL;
  149. bsg_reply->result = (DID_ERROR << 16);
  150. goto exit_fcp_prio_cfg;
  151. }
  152. }
  153. break;
  154. case QLFC_FCP_PRIO_GET_CONFIG:
  155. len = bsg_job->reply_payload.payload_len;
  156. if (!len || len > FCP_PRIO_CFG_SIZE) {
  157. ret = -EINVAL;
  158. bsg_reply->result = (DID_ERROR << 16);
  159. goto exit_fcp_prio_cfg;
  160. }
  161. bsg_reply->result = DID_OK;
  162. bsg_reply->reply_payload_rcv_len =
  163. sg_copy_from_buffer(
  164. bsg_job->reply_payload.sg_list,
  165. bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
  166. len);
  167. break;
  168. case QLFC_FCP_PRIO_SET_CONFIG:
  169. len = bsg_job->request_payload.payload_len;
  170. if (!len || len > FCP_PRIO_CFG_SIZE) {
  171. bsg_reply->result = (DID_ERROR << 16);
  172. ret = -EINVAL;
  173. goto exit_fcp_prio_cfg;
  174. }
  175. if (!ha->fcp_prio_cfg) {
  176. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  177. if (!ha->fcp_prio_cfg) {
  178. ql_log(ql_log_warn, vha, 0x7050,
  179. "Unable to allocate memory for fcp prio "
  180. "config data (%x).\n", FCP_PRIO_CFG_SIZE);
  181. bsg_reply->result = (DID_ERROR << 16);
  182. ret = -ENOMEM;
  183. goto exit_fcp_prio_cfg;
  184. }
  185. }
  186. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  187. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  188. bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
  189. FCP_PRIO_CFG_SIZE);
  190. /* validate fcp priority data */
  191. if (!qla24xx_fcp_prio_cfg_valid(vha,
  192. (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
  193. bsg_reply->result = (DID_ERROR << 16);
  194. ret = -EINVAL;
  195. /* If buffer was invalidatic int
  196. * fcp_prio_cfg is of no use
  197. */
  198. vfree(ha->fcp_prio_cfg);
  199. ha->fcp_prio_cfg = NULL;
  200. goto exit_fcp_prio_cfg;
  201. }
  202. ha->flags.fcp_prio_enabled = 0;
  203. if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
  204. ha->flags.fcp_prio_enabled = 1;
  205. qla24xx_update_all_fcp_prio(vha);
  206. bsg_reply->result = DID_OK;
  207. break;
  208. default:
  209. ret = -EINVAL;
  210. break;
  211. }
  212. exit_fcp_prio_cfg:
  213. if (!ret)
  214. bsg_job_done(bsg_job, bsg_reply->result,
  215. bsg_reply->reply_payload_rcv_len);
  216. return ret;
  217. }
  218. static int
  219. qla2x00_process_els(struct bsg_job *bsg_job)
  220. {
  221. struct fc_bsg_request *bsg_request = bsg_job->request;
  222. struct fc_rport *rport;
  223. fc_port_t *fcport = NULL;
  224. struct Scsi_Host *host;
  225. scsi_qla_host_t *vha;
  226. struct qla_hw_data *ha;
  227. srb_t *sp;
  228. const char *type;
  229. int req_sg_cnt, rsp_sg_cnt;
  230. int rval = (DID_ERROR << 16);
  231. uint16_t nextlid = 0;
  232. if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
  233. rport = fc_bsg_to_rport(bsg_job);
  234. fcport = *(fc_port_t **) rport->dd_data;
  235. host = rport_to_shost(rport);
  236. vha = shost_priv(host);
  237. ha = vha->hw;
  238. type = "FC_BSG_RPT_ELS";
  239. } else {
  240. host = fc_bsg_to_shost(bsg_job);
  241. vha = shost_priv(host);
  242. ha = vha->hw;
  243. type = "FC_BSG_HST_ELS_NOLOGIN";
  244. }
  245. if (!vha->flags.online) {
  246. ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
  247. rval = -EIO;
  248. goto done;
  249. }
  250. /* pass through is supported only for ISP 4Gb or higher */
  251. if (!IS_FWI2_CAPABLE(ha)) {
  252. ql_dbg(ql_dbg_user, vha, 0x7001,
  253. "ELS passthru not supported for ISP23xx based adapters.\n");
  254. rval = -EPERM;
  255. goto done;
  256. }
  257. /* Multiple SG's are not supported for ELS requests */
  258. if (bsg_job->request_payload.sg_cnt > 1 ||
  259. bsg_job->reply_payload.sg_cnt > 1) {
  260. ql_dbg(ql_dbg_user, vha, 0x7002,
  261. "Multiple SG's are not supported for ELS requests, "
  262. "request_sg_cnt=%x reply_sg_cnt=%x.\n",
  263. bsg_job->request_payload.sg_cnt,
  264. bsg_job->reply_payload.sg_cnt);
  265. rval = -EPERM;
  266. goto done;
  267. }
  268. /* ELS request for rport */
  269. if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
  270. /* make sure the rport is logged in,
  271. * if not perform fabric login
  272. */
  273. if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
  274. ql_dbg(ql_dbg_user, vha, 0x7003,
  275. "Failed to login port %06X for ELS passthru.\n",
  276. fcport->d_id.b24);
  277. rval = -EIO;
  278. goto done;
  279. }
  280. } else {
  281. /* Allocate a dummy fcport structure, since functions
  282. * preparing the IOCB and mailbox command retrieves port
  283. * specific information from fcport structure. For Host based
  284. * ELS commands there will be no fcport structure allocated
  285. */
  286. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  287. if (!fcport) {
  288. rval = -ENOMEM;
  289. goto done;
  290. }
  291. /* Initialize all required fields of fcport */
  292. fcport->vha = vha;
  293. fcport->d_id.b.al_pa =
  294. bsg_request->rqst_data.h_els.port_id[0];
  295. fcport->d_id.b.area =
  296. bsg_request->rqst_data.h_els.port_id[1];
  297. fcport->d_id.b.domain =
  298. bsg_request->rqst_data.h_els.port_id[2];
  299. fcport->loop_id =
  300. (fcport->d_id.b.al_pa == 0xFD) ?
  301. NPH_FABRIC_CONTROLLER : NPH_F_PORT;
  302. }
  303. req_sg_cnt =
  304. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  305. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  306. if (!req_sg_cnt) {
  307. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  308. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  309. rval = -ENOMEM;
  310. goto done_free_fcport;
  311. }
  312. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  313. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  314. if (!rsp_sg_cnt) {
  315. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  316. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  317. rval = -ENOMEM;
  318. goto done_free_fcport;
  319. }
  320. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  321. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  322. ql_log(ql_log_warn, vha, 0x7008,
  323. "dma mapping resulted in different sg counts, "
  324. "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
  325. "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
  326. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  327. rval = -EAGAIN;
  328. goto done_unmap_sg;
  329. }
  330. /* Alloc SRB structure */
  331. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  332. if (!sp) {
  333. rval = -ENOMEM;
  334. goto done_unmap_sg;
  335. }
  336. sp->type =
  337. (bsg_request->msgcode == FC_BSG_RPT_ELS ?
  338. SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
  339. sp->name =
  340. (bsg_request->msgcode == FC_BSG_RPT_ELS ?
  341. "bsg_els_rpt" : "bsg_els_hst");
  342. sp->u.bsg_job = bsg_job;
  343. sp->free = qla2x00_bsg_sp_free;
  344. sp->done = qla2x00_bsg_job_done;
  345. ql_dbg(ql_dbg_user, vha, 0x700a,
  346. "bsg rqst type: %s els type: %x - loop-id=%x "
  347. "portid=%-2x%02x%02x.\n", type,
  348. bsg_request->rqst_data.h_els.command_code, fcport->loop_id,
  349. fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
  350. rval = qla2x00_start_sp(sp);
  351. if (rval != QLA_SUCCESS) {
  352. ql_log(ql_log_warn, vha, 0x700e,
  353. "qla2x00_start_sp failed = %d\n", rval);
  354. qla2x00_rel_sp(sp);
  355. rval = -EIO;
  356. goto done_unmap_sg;
  357. }
  358. return rval;
  359. done_unmap_sg:
  360. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  361. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  362. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  363. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  364. goto done_free_fcport;
  365. done_free_fcport:
  366. if (bsg_request->msgcode == FC_BSG_RPT_ELS)
  367. kfree(fcport);
  368. done:
  369. return rval;
  370. }
  371. static inline uint16_t
  372. qla24xx_calc_ct_iocbs(uint16_t dsds)
  373. {
  374. uint16_t iocbs;
  375. iocbs = 1;
  376. if (dsds > 2) {
  377. iocbs += (dsds - 2) / 5;
  378. if ((dsds - 2) % 5)
  379. iocbs++;
  380. }
  381. return iocbs;
  382. }
  383. static int
  384. qla2x00_process_ct(struct bsg_job *bsg_job)
  385. {
  386. srb_t *sp;
  387. struct fc_bsg_request *bsg_request = bsg_job->request;
  388. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  389. scsi_qla_host_t *vha = shost_priv(host);
  390. struct qla_hw_data *ha = vha->hw;
  391. int rval = (DID_ERROR << 16);
  392. int req_sg_cnt, rsp_sg_cnt;
  393. uint16_t loop_id;
  394. struct fc_port *fcport;
  395. char *type = "FC_BSG_HST_CT";
  396. req_sg_cnt =
  397. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  398. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  399. if (!req_sg_cnt) {
  400. ql_log(ql_log_warn, vha, 0x700f,
  401. "dma_map_sg return %d for request\n", req_sg_cnt);
  402. rval = -ENOMEM;
  403. goto done;
  404. }
  405. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  406. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  407. if (!rsp_sg_cnt) {
  408. ql_log(ql_log_warn, vha, 0x7010,
  409. "dma_map_sg return %d for reply\n", rsp_sg_cnt);
  410. rval = -ENOMEM;
  411. goto done;
  412. }
  413. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  414. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  415. ql_log(ql_log_warn, vha, 0x7011,
  416. "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
  417. "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
  418. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  419. rval = -EAGAIN;
  420. goto done_unmap_sg;
  421. }
  422. if (!vha->flags.online) {
  423. ql_log(ql_log_warn, vha, 0x7012,
  424. "Host is not online.\n");
  425. rval = -EIO;
  426. goto done_unmap_sg;
  427. }
  428. loop_id =
  429. (bsg_request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
  430. >> 24;
  431. switch (loop_id) {
  432. case 0xFC:
  433. loop_id = cpu_to_le16(NPH_SNS);
  434. break;
  435. case 0xFA:
  436. loop_id = vha->mgmt_svr_loop_id;
  437. break;
  438. default:
  439. ql_dbg(ql_dbg_user, vha, 0x7013,
  440. "Unknown loop id: %x.\n", loop_id);
  441. rval = -EINVAL;
  442. goto done_unmap_sg;
  443. }
  444. /* Allocate a dummy fcport structure, since functions preparing the
  445. * IOCB and mailbox command retrieves port specific information
  446. * from fcport structure. For Host based ELS commands there will be
  447. * no fcport structure allocated
  448. */
  449. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  450. if (!fcport) {
  451. ql_log(ql_log_warn, vha, 0x7014,
  452. "Failed to allocate fcport.\n");
  453. rval = -ENOMEM;
  454. goto done_unmap_sg;
  455. }
  456. /* Initialize all required fields of fcport */
  457. fcport->vha = vha;
  458. fcport->d_id.b.al_pa = bsg_request->rqst_data.h_ct.port_id[0];
  459. fcport->d_id.b.area = bsg_request->rqst_data.h_ct.port_id[1];
  460. fcport->d_id.b.domain = bsg_request->rqst_data.h_ct.port_id[2];
  461. fcport->loop_id = loop_id;
  462. /* Alloc SRB structure */
  463. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  464. if (!sp) {
  465. ql_log(ql_log_warn, vha, 0x7015,
  466. "qla2x00_get_sp failed.\n");
  467. rval = -ENOMEM;
  468. goto done_free_fcport;
  469. }
  470. sp->type = SRB_CT_CMD;
  471. sp->name = "bsg_ct";
  472. sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
  473. sp->u.bsg_job = bsg_job;
  474. sp->free = qla2x00_bsg_sp_free;
  475. sp->done = qla2x00_bsg_job_done;
  476. ql_dbg(ql_dbg_user, vha, 0x7016,
  477. "bsg rqst type: %s else type: %x - "
  478. "loop-id=%x portid=%02x%02x%02x.\n", type,
  479. (bsg_request->rqst_data.h_ct.preamble_word2 >> 16),
  480. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  481. fcport->d_id.b.al_pa);
  482. rval = qla2x00_start_sp(sp);
  483. if (rval != QLA_SUCCESS) {
  484. ql_log(ql_log_warn, vha, 0x7017,
  485. "qla2x00_start_sp failed=%d.\n", rval);
  486. qla2x00_rel_sp(sp);
  487. rval = -EIO;
  488. goto done_free_fcport;
  489. }
  490. return rval;
  491. done_free_fcport:
  492. kfree(fcport);
  493. done_unmap_sg:
  494. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  495. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  496. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  497. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  498. done:
  499. return rval;
  500. }
  501. /* Disable loopback mode */
  502. static inline int
  503. qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  504. int wait, int wait2)
  505. {
  506. int ret = 0;
  507. int rval = 0;
  508. uint16_t new_config[4];
  509. struct qla_hw_data *ha = vha->hw;
  510. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
  511. goto done_reset_internal;
  512. memset(new_config, 0 , sizeof(new_config));
  513. if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  514. ENABLE_INTERNAL_LOOPBACK ||
  515. (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  516. ENABLE_EXTERNAL_LOOPBACK) {
  517. new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
  518. ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
  519. (new_config[0] & INTERNAL_LOOPBACK_MASK));
  520. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  521. ha->notify_dcbx_comp = wait;
  522. ha->notify_lb_portup_comp = wait2;
  523. ret = qla81xx_set_port_config(vha, new_config);
  524. if (ret != QLA_SUCCESS) {
  525. ql_log(ql_log_warn, vha, 0x7025,
  526. "Set port config failed.\n");
  527. ha->notify_dcbx_comp = 0;
  528. ha->notify_lb_portup_comp = 0;
  529. rval = -EINVAL;
  530. goto done_reset_internal;
  531. }
  532. /* Wait for DCBX complete event */
  533. if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
  534. (DCBX_COMP_TIMEOUT * HZ))) {
  535. ql_dbg(ql_dbg_user, vha, 0x7026,
  536. "DCBX completion not received.\n");
  537. ha->notify_dcbx_comp = 0;
  538. ha->notify_lb_portup_comp = 0;
  539. rval = -EINVAL;
  540. goto done_reset_internal;
  541. } else
  542. ql_dbg(ql_dbg_user, vha, 0x7027,
  543. "DCBX completion received.\n");
  544. if (wait2 &&
  545. !wait_for_completion_timeout(&ha->lb_portup_comp,
  546. (LB_PORTUP_COMP_TIMEOUT * HZ))) {
  547. ql_dbg(ql_dbg_user, vha, 0x70c5,
  548. "Port up completion not received.\n");
  549. ha->notify_lb_portup_comp = 0;
  550. rval = -EINVAL;
  551. goto done_reset_internal;
  552. } else
  553. ql_dbg(ql_dbg_user, vha, 0x70c6,
  554. "Port up completion received.\n");
  555. ha->notify_dcbx_comp = 0;
  556. ha->notify_lb_portup_comp = 0;
  557. }
  558. done_reset_internal:
  559. return rval;
  560. }
  561. /*
  562. * Set the port configuration to enable the internal or external loopback
  563. * depending on the loopback mode.
  564. */
  565. static inline int
  566. qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  567. uint16_t *new_config, uint16_t mode)
  568. {
  569. int ret = 0;
  570. int rval = 0;
  571. unsigned long rem_tmo = 0, current_tmo = 0;
  572. struct qla_hw_data *ha = vha->hw;
  573. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
  574. goto done_set_internal;
  575. if (mode == INTERNAL_LOOPBACK)
  576. new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
  577. else if (mode == EXTERNAL_LOOPBACK)
  578. new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
  579. ql_dbg(ql_dbg_user, vha, 0x70be,
  580. "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
  581. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
  582. ha->notify_dcbx_comp = 1;
  583. ret = qla81xx_set_port_config(vha, new_config);
  584. if (ret != QLA_SUCCESS) {
  585. ql_log(ql_log_warn, vha, 0x7021,
  586. "set port config failed.\n");
  587. ha->notify_dcbx_comp = 0;
  588. rval = -EINVAL;
  589. goto done_set_internal;
  590. }
  591. /* Wait for DCBX complete event */
  592. current_tmo = DCBX_COMP_TIMEOUT * HZ;
  593. while (1) {
  594. rem_tmo = wait_for_completion_timeout(&ha->dcbx_comp,
  595. current_tmo);
  596. if (!ha->idc_extend_tmo || rem_tmo) {
  597. ha->idc_extend_tmo = 0;
  598. break;
  599. }
  600. current_tmo = ha->idc_extend_tmo * HZ;
  601. ha->idc_extend_tmo = 0;
  602. }
  603. if (!rem_tmo) {
  604. ql_dbg(ql_dbg_user, vha, 0x7022,
  605. "DCBX completion not received.\n");
  606. ret = qla81xx_reset_loopback_mode(vha, new_config, 0, 0);
  607. /*
  608. * If the reset of the loopback mode doesn't work take a FCoE
  609. * dump and reset the chip.
  610. */
  611. if (ret) {
  612. ha->isp_ops->fw_dump(vha, 0);
  613. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  614. }
  615. rval = -EINVAL;
  616. } else {
  617. if (ha->flags.idc_compl_status) {
  618. ql_dbg(ql_dbg_user, vha, 0x70c3,
  619. "Bad status in IDC Completion AEN\n");
  620. rval = -EINVAL;
  621. ha->flags.idc_compl_status = 0;
  622. } else
  623. ql_dbg(ql_dbg_user, vha, 0x7023,
  624. "DCBX completion received.\n");
  625. }
  626. ha->notify_dcbx_comp = 0;
  627. ha->idc_extend_tmo = 0;
  628. done_set_internal:
  629. return rval;
  630. }
  631. static int
  632. qla2x00_process_loopback(struct bsg_job *bsg_job)
  633. {
  634. struct fc_bsg_request *bsg_request = bsg_job->request;
  635. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  636. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  637. scsi_qla_host_t *vha = shost_priv(host);
  638. struct qla_hw_data *ha = vha->hw;
  639. int rval;
  640. uint8_t command_sent;
  641. char *type;
  642. struct msg_echo_lb elreq;
  643. uint16_t response[MAILBOX_REGISTER_COUNT];
  644. uint16_t config[4], new_config[4];
  645. uint8_t *fw_sts_ptr;
  646. uint8_t *req_data = NULL;
  647. dma_addr_t req_data_dma;
  648. uint32_t req_data_len;
  649. uint8_t *rsp_data = NULL;
  650. dma_addr_t rsp_data_dma;
  651. uint32_t rsp_data_len;
  652. if (!vha->flags.online) {
  653. ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
  654. return -EIO;
  655. }
  656. memset(&elreq, 0, sizeof(elreq));
  657. elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  658. bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
  659. DMA_TO_DEVICE);
  660. if (!elreq.req_sg_cnt) {
  661. ql_log(ql_log_warn, vha, 0x701a,
  662. "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
  663. return -ENOMEM;
  664. }
  665. elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  666. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  667. DMA_FROM_DEVICE);
  668. if (!elreq.rsp_sg_cnt) {
  669. ql_log(ql_log_warn, vha, 0x701b,
  670. "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
  671. rval = -ENOMEM;
  672. goto done_unmap_req_sg;
  673. }
  674. if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  675. (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  676. ql_log(ql_log_warn, vha, 0x701c,
  677. "dma mapping resulted in different sg counts, "
  678. "request_sg_cnt: %x dma_request_sg_cnt: %x "
  679. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  680. bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
  681. bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
  682. rval = -EAGAIN;
  683. goto done_unmap_sg;
  684. }
  685. req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
  686. req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
  687. &req_data_dma, GFP_KERNEL);
  688. if (!req_data) {
  689. ql_log(ql_log_warn, vha, 0x701d,
  690. "dma alloc failed for req_data.\n");
  691. rval = -ENOMEM;
  692. goto done_unmap_sg;
  693. }
  694. rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
  695. &rsp_data_dma, GFP_KERNEL);
  696. if (!rsp_data) {
  697. ql_log(ql_log_warn, vha, 0x7004,
  698. "dma alloc failed for rsp_data.\n");
  699. rval = -ENOMEM;
  700. goto done_free_dma_req;
  701. }
  702. /* Copy the request buffer in req_data now */
  703. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  704. bsg_job->request_payload.sg_cnt, req_data, req_data_len);
  705. elreq.send_dma = req_data_dma;
  706. elreq.rcv_dma = rsp_data_dma;
  707. elreq.transfer_size = req_data_len;
  708. elreq.options = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  709. elreq.iteration_count =
  710. bsg_request->rqst_data.h_vendor.vendor_cmd[2];
  711. if (atomic_read(&vha->loop_state) == LOOP_READY &&
  712. (ha->current_topology == ISP_CFG_F ||
  713. (le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE &&
  714. req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
  715. elreq.options == EXTERNAL_LOOPBACK) {
  716. type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
  717. ql_dbg(ql_dbg_user, vha, 0x701e,
  718. "BSG request type: %s.\n", type);
  719. command_sent = INT_DEF_LB_ECHO_CMD;
  720. rval = qla2x00_echo_test(vha, &elreq, response);
  721. } else {
  722. if (IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) {
  723. memset(config, 0, sizeof(config));
  724. memset(new_config, 0, sizeof(new_config));
  725. if (qla81xx_get_port_config(vha, config)) {
  726. ql_log(ql_log_warn, vha, 0x701f,
  727. "Get port config failed.\n");
  728. rval = -EPERM;
  729. goto done_free_dma_rsp;
  730. }
  731. if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
  732. ql_dbg(ql_dbg_user, vha, 0x70c4,
  733. "Loopback operation already in "
  734. "progress.\n");
  735. rval = -EAGAIN;
  736. goto done_free_dma_rsp;
  737. }
  738. ql_dbg(ql_dbg_user, vha, 0x70c0,
  739. "elreq.options=%04x\n", elreq.options);
  740. if (elreq.options == EXTERNAL_LOOPBACK)
  741. if (IS_QLA8031(ha) || IS_QLA8044(ha))
  742. rval = qla81xx_set_loopback_mode(vha,
  743. config, new_config, elreq.options);
  744. else
  745. rval = qla81xx_reset_loopback_mode(vha,
  746. config, 1, 0);
  747. else
  748. rval = qla81xx_set_loopback_mode(vha, config,
  749. new_config, elreq.options);
  750. if (rval) {
  751. rval = -EPERM;
  752. goto done_free_dma_rsp;
  753. }
  754. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  755. ql_dbg(ql_dbg_user, vha, 0x7028,
  756. "BSG request type: %s.\n", type);
  757. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  758. rval = qla2x00_loopback_test(vha, &elreq, response);
  759. if (response[0] == MBS_COMMAND_ERROR &&
  760. response[1] == MBS_LB_RESET) {
  761. ql_log(ql_log_warn, vha, 0x7029,
  762. "MBX command error, Aborting ISP.\n");
  763. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  764. qla2xxx_wake_dpc(vha);
  765. qla2x00_wait_for_chip_reset(vha);
  766. /* Also reset the MPI */
  767. if (IS_QLA81XX(ha)) {
  768. if (qla81xx_restart_mpi_firmware(vha) !=
  769. QLA_SUCCESS) {
  770. ql_log(ql_log_warn, vha, 0x702a,
  771. "MPI reset failed.\n");
  772. }
  773. }
  774. rval = -EIO;
  775. goto done_free_dma_rsp;
  776. }
  777. if (new_config[0]) {
  778. int ret;
  779. /* Revert back to original port config
  780. * Also clear internal loopback
  781. */
  782. ret = qla81xx_reset_loopback_mode(vha,
  783. new_config, 0, 1);
  784. if (ret) {
  785. /*
  786. * If the reset of the loopback mode
  787. * doesn't work take FCoE dump and then
  788. * reset the chip.
  789. */
  790. ha->isp_ops->fw_dump(vha, 0);
  791. set_bit(ISP_ABORT_NEEDED,
  792. &vha->dpc_flags);
  793. }
  794. }
  795. } else {
  796. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  797. ql_dbg(ql_dbg_user, vha, 0x702b,
  798. "BSG request type: %s.\n", type);
  799. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  800. rval = qla2x00_loopback_test(vha, &elreq, response);
  801. }
  802. }
  803. if (rval) {
  804. ql_log(ql_log_warn, vha, 0x702c,
  805. "Vendor request %s failed.\n", type);
  806. rval = 0;
  807. bsg_reply->result = (DID_ERROR << 16);
  808. bsg_reply->reply_payload_rcv_len = 0;
  809. } else {
  810. ql_dbg(ql_dbg_user, vha, 0x702d,
  811. "Vendor request %s completed.\n", type);
  812. bsg_reply->result = (DID_OK << 16);
  813. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  814. bsg_job->reply_payload.sg_cnt, rsp_data,
  815. rsp_data_len);
  816. }
  817. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  818. sizeof(response) + sizeof(uint8_t);
  819. fw_sts_ptr = bsg_job->reply + sizeof(struct fc_bsg_reply);
  820. memcpy(bsg_job->reply + sizeof(struct fc_bsg_reply), response,
  821. sizeof(response));
  822. fw_sts_ptr += sizeof(response);
  823. *fw_sts_ptr = command_sent;
  824. done_free_dma_rsp:
  825. dma_free_coherent(&ha->pdev->dev, rsp_data_len,
  826. rsp_data, rsp_data_dma);
  827. done_free_dma_req:
  828. dma_free_coherent(&ha->pdev->dev, req_data_len,
  829. req_data, req_data_dma);
  830. done_unmap_sg:
  831. dma_unmap_sg(&ha->pdev->dev,
  832. bsg_job->reply_payload.sg_list,
  833. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  834. done_unmap_req_sg:
  835. dma_unmap_sg(&ha->pdev->dev,
  836. bsg_job->request_payload.sg_list,
  837. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  838. if (!rval)
  839. bsg_job_done(bsg_job, bsg_reply->result,
  840. bsg_reply->reply_payload_rcv_len);
  841. return rval;
  842. }
  843. static int
  844. qla84xx_reset(struct bsg_job *bsg_job)
  845. {
  846. struct fc_bsg_request *bsg_request = bsg_job->request;
  847. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  848. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  849. scsi_qla_host_t *vha = shost_priv(host);
  850. struct qla_hw_data *ha = vha->hw;
  851. int rval = 0;
  852. uint32_t flag;
  853. if (!IS_QLA84XX(ha)) {
  854. ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
  855. return -EINVAL;
  856. }
  857. flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  858. rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
  859. if (rval) {
  860. ql_log(ql_log_warn, vha, 0x7030,
  861. "Vendor request 84xx reset failed.\n");
  862. rval = (DID_ERROR << 16);
  863. } else {
  864. ql_dbg(ql_dbg_user, vha, 0x7031,
  865. "Vendor request 84xx reset completed.\n");
  866. bsg_reply->result = DID_OK;
  867. bsg_job_done(bsg_job, bsg_reply->result,
  868. bsg_reply->reply_payload_rcv_len);
  869. }
  870. return rval;
  871. }
  872. static int
  873. qla84xx_updatefw(struct bsg_job *bsg_job)
  874. {
  875. struct fc_bsg_request *bsg_request = bsg_job->request;
  876. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  877. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  878. scsi_qla_host_t *vha = shost_priv(host);
  879. struct qla_hw_data *ha = vha->hw;
  880. struct verify_chip_entry_84xx *mn = NULL;
  881. dma_addr_t mn_dma, fw_dma;
  882. void *fw_buf = NULL;
  883. int rval = 0;
  884. uint32_t sg_cnt;
  885. uint32_t data_len;
  886. uint16_t options;
  887. uint32_t flag;
  888. uint32_t fw_ver;
  889. if (!IS_QLA84XX(ha)) {
  890. ql_dbg(ql_dbg_user, vha, 0x7032,
  891. "Not 84xx, exiting.\n");
  892. return -EINVAL;
  893. }
  894. sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  895. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  896. if (!sg_cnt) {
  897. ql_log(ql_log_warn, vha, 0x7033,
  898. "dma_map_sg returned %d for request.\n", sg_cnt);
  899. return -ENOMEM;
  900. }
  901. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  902. ql_log(ql_log_warn, vha, 0x7034,
  903. "DMA mapping resulted in different sg counts, "
  904. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  905. bsg_job->request_payload.sg_cnt, sg_cnt);
  906. rval = -EAGAIN;
  907. goto done_unmap_sg;
  908. }
  909. data_len = bsg_job->request_payload.payload_len;
  910. fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
  911. &fw_dma, GFP_KERNEL);
  912. if (!fw_buf) {
  913. ql_log(ql_log_warn, vha, 0x7035,
  914. "DMA alloc failed for fw_buf.\n");
  915. rval = -ENOMEM;
  916. goto done_unmap_sg;
  917. }
  918. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  919. bsg_job->request_payload.sg_cnt, fw_buf, data_len);
  920. mn = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  921. if (!mn) {
  922. ql_log(ql_log_warn, vha, 0x7036,
  923. "DMA alloc failed for fw buffer.\n");
  924. rval = -ENOMEM;
  925. goto done_free_fw_buf;
  926. }
  927. flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  928. fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
  929. mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
  930. mn->entry_count = 1;
  931. options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
  932. if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
  933. options |= VCO_DIAG_FW;
  934. mn->options = cpu_to_le16(options);
  935. mn->fw_ver = cpu_to_le32(fw_ver);
  936. mn->fw_size = cpu_to_le32(data_len);
  937. mn->fw_seq_size = cpu_to_le32(data_len);
  938. mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
  939. mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
  940. mn->dseg_length = cpu_to_le32(data_len);
  941. mn->data_seg_cnt = cpu_to_le16(1);
  942. rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
  943. if (rval) {
  944. ql_log(ql_log_warn, vha, 0x7037,
  945. "Vendor request 84xx updatefw failed.\n");
  946. rval = (DID_ERROR << 16);
  947. } else {
  948. ql_dbg(ql_dbg_user, vha, 0x7038,
  949. "Vendor request 84xx updatefw completed.\n");
  950. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  951. bsg_reply->result = DID_OK;
  952. }
  953. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  954. done_free_fw_buf:
  955. dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
  956. done_unmap_sg:
  957. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  958. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  959. if (!rval)
  960. bsg_job_done(bsg_job, bsg_reply->result,
  961. bsg_reply->reply_payload_rcv_len);
  962. return rval;
  963. }
  964. static int
  965. qla84xx_mgmt_cmd(struct bsg_job *bsg_job)
  966. {
  967. struct fc_bsg_request *bsg_request = bsg_job->request;
  968. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  969. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  970. scsi_qla_host_t *vha = shost_priv(host);
  971. struct qla_hw_data *ha = vha->hw;
  972. struct access_chip_84xx *mn = NULL;
  973. dma_addr_t mn_dma, mgmt_dma;
  974. void *mgmt_b = NULL;
  975. int rval = 0;
  976. struct qla_bsg_a84_mgmt *ql84_mgmt;
  977. uint32_t sg_cnt;
  978. uint32_t data_len = 0;
  979. uint32_t dma_direction = DMA_NONE;
  980. if (!IS_QLA84XX(ha)) {
  981. ql_log(ql_log_warn, vha, 0x703a,
  982. "Not 84xx, exiting.\n");
  983. return -EINVAL;
  984. }
  985. mn = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  986. if (!mn) {
  987. ql_log(ql_log_warn, vha, 0x703c,
  988. "DMA alloc failed for fw buffer.\n");
  989. return -ENOMEM;
  990. }
  991. mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
  992. mn->entry_count = 1;
  993. ql84_mgmt = (void *)bsg_request + sizeof(struct fc_bsg_request);
  994. switch (ql84_mgmt->mgmt.cmd) {
  995. case QLA84_MGMT_READ_MEM:
  996. case QLA84_MGMT_GET_INFO:
  997. sg_cnt = dma_map_sg(&ha->pdev->dev,
  998. bsg_job->reply_payload.sg_list,
  999. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1000. if (!sg_cnt) {
  1001. ql_log(ql_log_warn, vha, 0x703d,
  1002. "dma_map_sg returned %d for reply.\n", sg_cnt);
  1003. rval = -ENOMEM;
  1004. goto exit_mgmt;
  1005. }
  1006. dma_direction = DMA_FROM_DEVICE;
  1007. if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
  1008. ql_log(ql_log_warn, vha, 0x703e,
  1009. "DMA mapping resulted in different sg counts, "
  1010. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  1011. bsg_job->reply_payload.sg_cnt, sg_cnt);
  1012. rval = -EAGAIN;
  1013. goto done_unmap_sg;
  1014. }
  1015. data_len = bsg_job->reply_payload.payload_len;
  1016. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  1017. &mgmt_dma, GFP_KERNEL);
  1018. if (!mgmt_b) {
  1019. ql_log(ql_log_warn, vha, 0x703f,
  1020. "DMA alloc failed for mgmt_b.\n");
  1021. rval = -ENOMEM;
  1022. goto done_unmap_sg;
  1023. }
  1024. if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
  1025. mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
  1026. mn->parameter1 =
  1027. cpu_to_le32(
  1028. ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  1029. } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
  1030. mn->options = cpu_to_le16(ACO_REQUEST_INFO);
  1031. mn->parameter1 =
  1032. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
  1033. mn->parameter2 =
  1034. cpu_to_le32(
  1035. ql84_mgmt->mgmt.mgmtp.u.info.context);
  1036. }
  1037. break;
  1038. case QLA84_MGMT_WRITE_MEM:
  1039. sg_cnt = dma_map_sg(&ha->pdev->dev,
  1040. bsg_job->request_payload.sg_list,
  1041. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1042. if (!sg_cnt) {
  1043. ql_log(ql_log_warn, vha, 0x7040,
  1044. "dma_map_sg returned %d.\n", sg_cnt);
  1045. rval = -ENOMEM;
  1046. goto exit_mgmt;
  1047. }
  1048. dma_direction = DMA_TO_DEVICE;
  1049. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  1050. ql_log(ql_log_warn, vha, 0x7041,
  1051. "DMA mapping resulted in different sg counts, "
  1052. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  1053. bsg_job->request_payload.sg_cnt, sg_cnt);
  1054. rval = -EAGAIN;
  1055. goto done_unmap_sg;
  1056. }
  1057. data_len = bsg_job->request_payload.payload_len;
  1058. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  1059. &mgmt_dma, GFP_KERNEL);
  1060. if (!mgmt_b) {
  1061. ql_log(ql_log_warn, vha, 0x7042,
  1062. "DMA alloc failed for mgmt_b.\n");
  1063. rval = -ENOMEM;
  1064. goto done_unmap_sg;
  1065. }
  1066. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1067. bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
  1068. mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
  1069. mn->parameter1 =
  1070. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  1071. break;
  1072. case QLA84_MGMT_CHNG_CONFIG:
  1073. mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
  1074. mn->parameter1 =
  1075. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
  1076. mn->parameter2 =
  1077. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
  1078. mn->parameter3 =
  1079. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
  1080. break;
  1081. default:
  1082. rval = -EIO;
  1083. goto exit_mgmt;
  1084. }
  1085. if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
  1086. mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
  1087. mn->dseg_count = cpu_to_le16(1);
  1088. mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
  1089. mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
  1090. mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
  1091. }
  1092. rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
  1093. if (rval) {
  1094. ql_log(ql_log_warn, vha, 0x7043,
  1095. "Vendor request 84xx mgmt failed.\n");
  1096. rval = (DID_ERROR << 16);
  1097. } else {
  1098. ql_dbg(ql_dbg_user, vha, 0x7044,
  1099. "Vendor request 84xx mgmt completed.\n");
  1100. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1101. bsg_reply->result = DID_OK;
  1102. if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
  1103. (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
  1104. bsg_reply->reply_payload_rcv_len =
  1105. bsg_job->reply_payload.payload_len;
  1106. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1107. bsg_job->reply_payload.sg_cnt, mgmt_b,
  1108. data_len);
  1109. }
  1110. }
  1111. done_unmap_sg:
  1112. if (mgmt_b)
  1113. dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
  1114. if (dma_direction == DMA_TO_DEVICE)
  1115. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  1116. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1117. else if (dma_direction == DMA_FROM_DEVICE)
  1118. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  1119. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1120. exit_mgmt:
  1121. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  1122. if (!rval)
  1123. bsg_job_done(bsg_job, bsg_reply->result,
  1124. bsg_reply->reply_payload_rcv_len);
  1125. return rval;
  1126. }
  1127. static int
  1128. qla24xx_iidma(struct bsg_job *bsg_job)
  1129. {
  1130. struct fc_bsg_request *bsg_request = bsg_job->request;
  1131. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1132. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1133. scsi_qla_host_t *vha = shost_priv(host);
  1134. int rval = 0;
  1135. struct qla_port_param *port_param = NULL;
  1136. fc_port_t *fcport = NULL;
  1137. int found = 0;
  1138. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1139. uint8_t *rsp_ptr = NULL;
  1140. if (!IS_IIDMA_CAPABLE(vha->hw)) {
  1141. ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
  1142. return -EINVAL;
  1143. }
  1144. port_param = (void *)bsg_request + sizeof(struct fc_bsg_request);
  1145. if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
  1146. ql_log(ql_log_warn, vha, 0x7048,
  1147. "Invalid destination type.\n");
  1148. return -EINVAL;
  1149. }
  1150. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  1151. if (fcport->port_type != FCT_TARGET)
  1152. continue;
  1153. if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
  1154. fcport->port_name, sizeof(fcport->port_name)))
  1155. continue;
  1156. found = 1;
  1157. break;
  1158. }
  1159. if (!found) {
  1160. ql_log(ql_log_warn, vha, 0x7049,
  1161. "Failed to find port.\n");
  1162. return -EINVAL;
  1163. }
  1164. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  1165. ql_log(ql_log_warn, vha, 0x704a,
  1166. "Port is not online.\n");
  1167. return -EINVAL;
  1168. }
  1169. if (fcport->flags & FCF_LOGIN_NEEDED) {
  1170. ql_log(ql_log_warn, vha, 0x704b,
  1171. "Remote port not logged in flags = 0x%x.\n", fcport->flags);
  1172. return -EINVAL;
  1173. }
  1174. if (port_param->mode)
  1175. rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
  1176. port_param->speed, mb);
  1177. else
  1178. rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
  1179. &port_param->speed, mb);
  1180. if (rval) {
  1181. ql_log(ql_log_warn, vha, 0x704c,
  1182. "iIDMA cmd failed for %8phN -- "
  1183. "%04x %x %04x %04x.\n", fcport->port_name,
  1184. rval, fcport->fp_speed, mb[0], mb[1]);
  1185. rval = (DID_ERROR << 16);
  1186. } else {
  1187. if (!port_param->mode) {
  1188. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  1189. sizeof(struct qla_port_param);
  1190. rsp_ptr = ((uint8_t *)bsg_reply) +
  1191. sizeof(struct fc_bsg_reply);
  1192. memcpy(rsp_ptr, port_param,
  1193. sizeof(struct qla_port_param));
  1194. }
  1195. bsg_reply->result = DID_OK;
  1196. bsg_job_done(bsg_job, bsg_reply->result,
  1197. bsg_reply->reply_payload_rcv_len);
  1198. }
  1199. return rval;
  1200. }
  1201. static int
  1202. qla2x00_optrom_setup(struct bsg_job *bsg_job, scsi_qla_host_t *vha,
  1203. uint8_t is_update)
  1204. {
  1205. struct fc_bsg_request *bsg_request = bsg_job->request;
  1206. uint32_t start = 0;
  1207. int valid = 0;
  1208. struct qla_hw_data *ha = vha->hw;
  1209. if (unlikely(pci_channel_offline(ha->pdev)))
  1210. return -EINVAL;
  1211. start = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  1212. if (start > ha->optrom_size) {
  1213. ql_log(ql_log_warn, vha, 0x7055,
  1214. "start %d > optrom_size %d.\n", start, ha->optrom_size);
  1215. return -EINVAL;
  1216. }
  1217. if (ha->optrom_state != QLA_SWAITING) {
  1218. ql_log(ql_log_info, vha, 0x7056,
  1219. "optrom_state %d.\n", ha->optrom_state);
  1220. return -EBUSY;
  1221. }
  1222. ha->optrom_region_start = start;
  1223. ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
  1224. if (is_update) {
  1225. if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
  1226. valid = 1;
  1227. else if (start == (ha->flt_region_boot * 4) ||
  1228. start == (ha->flt_region_fw * 4))
  1229. valid = 1;
  1230. else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
  1231. IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha))
  1232. valid = 1;
  1233. if (!valid) {
  1234. ql_log(ql_log_warn, vha, 0x7058,
  1235. "Invalid start region 0x%x/0x%x.\n", start,
  1236. bsg_job->request_payload.payload_len);
  1237. return -EINVAL;
  1238. }
  1239. ha->optrom_region_size = start +
  1240. bsg_job->request_payload.payload_len > ha->optrom_size ?
  1241. ha->optrom_size - start :
  1242. bsg_job->request_payload.payload_len;
  1243. ha->optrom_state = QLA_SWRITING;
  1244. } else {
  1245. ha->optrom_region_size = start +
  1246. bsg_job->reply_payload.payload_len > ha->optrom_size ?
  1247. ha->optrom_size - start :
  1248. bsg_job->reply_payload.payload_len;
  1249. ha->optrom_state = QLA_SREADING;
  1250. }
  1251. ha->optrom_buffer = vzalloc(ha->optrom_region_size);
  1252. if (!ha->optrom_buffer) {
  1253. ql_log(ql_log_warn, vha, 0x7059,
  1254. "Read: Unable to allocate memory for optrom retrieval "
  1255. "(%x)\n", ha->optrom_region_size);
  1256. ha->optrom_state = QLA_SWAITING;
  1257. return -ENOMEM;
  1258. }
  1259. return 0;
  1260. }
  1261. static int
  1262. qla2x00_read_optrom(struct bsg_job *bsg_job)
  1263. {
  1264. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1265. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1266. scsi_qla_host_t *vha = shost_priv(host);
  1267. struct qla_hw_data *ha = vha->hw;
  1268. int rval = 0;
  1269. if (ha->flags.nic_core_reset_hdlr_active)
  1270. return -EBUSY;
  1271. mutex_lock(&ha->optrom_mutex);
  1272. rval = qla2x00_optrom_setup(bsg_job, vha, 0);
  1273. if (rval) {
  1274. mutex_unlock(&ha->optrom_mutex);
  1275. return rval;
  1276. }
  1277. ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
  1278. ha->optrom_region_start, ha->optrom_region_size);
  1279. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1280. bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
  1281. ha->optrom_region_size);
  1282. bsg_reply->reply_payload_rcv_len = ha->optrom_region_size;
  1283. bsg_reply->result = DID_OK;
  1284. vfree(ha->optrom_buffer);
  1285. ha->optrom_buffer = NULL;
  1286. ha->optrom_state = QLA_SWAITING;
  1287. mutex_unlock(&ha->optrom_mutex);
  1288. bsg_job_done(bsg_job, bsg_reply->result,
  1289. bsg_reply->reply_payload_rcv_len);
  1290. return rval;
  1291. }
  1292. static int
  1293. qla2x00_update_optrom(struct bsg_job *bsg_job)
  1294. {
  1295. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1296. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1297. scsi_qla_host_t *vha = shost_priv(host);
  1298. struct qla_hw_data *ha = vha->hw;
  1299. int rval = 0;
  1300. mutex_lock(&ha->optrom_mutex);
  1301. rval = qla2x00_optrom_setup(bsg_job, vha, 1);
  1302. if (rval) {
  1303. mutex_unlock(&ha->optrom_mutex);
  1304. return rval;
  1305. }
  1306. /* Set the isp82xx_no_md_cap not to capture minidump */
  1307. ha->flags.isp82xx_no_md_cap = 1;
  1308. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1309. bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
  1310. ha->optrom_region_size);
  1311. ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
  1312. ha->optrom_region_start, ha->optrom_region_size);
  1313. bsg_reply->result = DID_OK;
  1314. vfree(ha->optrom_buffer);
  1315. ha->optrom_buffer = NULL;
  1316. ha->optrom_state = QLA_SWAITING;
  1317. mutex_unlock(&ha->optrom_mutex);
  1318. bsg_job_done(bsg_job, bsg_reply->result,
  1319. bsg_reply->reply_payload_rcv_len);
  1320. return rval;
  1321. }
  1322. static int
  1323. qla2x00_update_fru_versions(struct bsg_job *bsg_job)
  1324. {
  1325. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1326. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1327. scsi_qla_host_t *vha = shost_priv(host);
  1328. struct qla_hw_data *ha = vha->hw;
  1329. int rval = 0;
  1330. uint8_t bsg[DMA_POOL_SIZE];
  1331. struct qla_image_version_list *list = (void *)bsg;
  1332. struct qla_image_version *image;
  1333. uint32_t count;
  1334. dma_addr_t sfp_dma;
  1335. void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1336. if (!sfp) {
  1337. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1338. EXT_STATUS_NO_MEMORY;
  1339. goto done;
  1340. }
  1341. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1342. bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
  1343. image = list->version;
  1344. count = list->count;
  1345. while (count--) {
  1346. memcpy(sfp, &image->field_info, sizeof(image->field_info));
  1347. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1348. image->field_address.device, image->field_address.offset,
  1349. sizeof(image->field_info), image->field_address.option);
  1350. if (rval) {
  1351. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1352. EXT_STATUS_MAILBOX;
  1353. goto dealloc;
  1354. }
  1355. image++;
  1356. }
  1357. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1358. dealloc:
  1359. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1360. done:
  1361. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1362. bsg_reply->result = DID_OK << 16;
  1363. bsg_job_done(bsg_job, bsg_reply->result,
  1364. bsg_reply->reply_payload_rcv_len);
  1365. return 0;
  1366. }
  1367. static int
  1368. qla2x00_read_fru_status(struct bsg_job *bsg_job)
  1369. {
  1370. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1371. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1372. scsi_qla_host_t *vha = shost_priv(host);
  1373. struct qla_hw_data *ha = vha->hw;
  1374. int rval = 0;
  1375. uint8_t bsg[DMA_POOL_SIZE];
  1376. struct qla_status_reg *sr = (void *)bsg;
  1377. dma_addr_t sfp_dma;
  1378. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1379. if (!sfp) {
  1380. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1381. EXT_STATUS_NO_MEMORY;
  1382. goto done;
  1383. }
  1384. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1385. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1386. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1387. sr->field_address.device, sr->field_address.offset,
  1388. sizeof(sr->status_reg), sr->field_address.option);
  1389. sr->status_reg = *sfp;
  1390. if (rval) {
  1391. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1392. EXT_STATUS_MAILBOX;
  1393. goto dealloc;
  1394. }
  1395. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1396. bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
  1397. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1398. dealloc:
  1399. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1400. done:
  1401. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1402. bsg_reply->reply_payload_rcv_len = sizeof(*sr);
  1403. bsg_reply->result = DID_OK << 16;
  1404. bsg_job_done(bsg_job, bsg_reply->result,
  1405. bsg_reply->reply_payload_rcv_len);
  1406. return 0;
  1407. }
  1408. static int
  1409. qla2x00_write_fru_status(struct bsg_job *bsg_job)
  1410. {
  1411. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1412. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1413. scsi_qla_host_t *vha = shost_priv(host);
  1414. struct qla_hw_data *ha = vha->hw;
  1415. int rval = 0;
  1416. uint8_t bsg[DMA_POOL_SIZE];
  1417. struct qla_status_reg *sr = (void *)bsg;
  1418. dma_addr_t sfp_dma;
  1419. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1420. if (!sfp) {
  1421. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1422. EXT_STATUS_NO_MEMORY;
  1423. goto done;
  1424. }
  1425. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1426. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1427. *sfp = sr->status_reg;
  1428. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1429. sr->field_address.device, sr->field_address.offset,
  1430. sizeof(sr->status_reg), sr->field_address.option);
  1431. if (rval) {
  1432. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1433. EXT_STATUS_MAILBOX;
  1434. goto dealloc;
  1435. }
  1436. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1437. dealloc:
  1438. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1439. done:
  1440. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1441. bsg_reply->result = DID_OK << 16;
  1442. bsg_job_done(bsg_job, bsg_reply->result,
  1443. bsg_reply->reply_payload_rcv_len);
  1444. return 0;
  1445. }
  1446. static int
  1447. qla2x00_write_i2c(struct bsg_job *bsg_job)
  1448. {
  1449. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1450. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1451. scsi_qla_host_t *vha = shost_priv(host);
  1452. struct qla_hw_data *ha = vha->hw;
  1453. int rval = 0;
  1454. uint8_t bsg[DMA_POOL_SIZE];
  1455. struct qla_i2c_access *i2c = (void *)bsg;
  1456. dma_addr_t sfp_dma;
  1457. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1458. if (!sfp) {
  1459. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1460. EXT_STATUS_NO_MEMORY;
  1461. goto done;
  1462. }
  1463. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1464. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1465. memcpy(sfp, i2c->buffer, i2c->length);
  1466. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1467. i2c->device, i2c->offset, i2c->length, i2c->option);
  1468. if (rval) {
  1469. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1470. EXT_STATUS_MAILBOX;
  1471. goto dealloc;
  1472. }
  1473. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1474. dealloc:
  1475. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1476. done:
  1477. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1478. bsg_reply->result = DID_OK << 16;
  1479. bsg_job_done(bsg_job, bsg_reply->result,
  1480. bsg_reply->reply_payload_rcv_len);
  1481. return 0;
  1482. }
  1483. static int
  1484. qla2x00_read_i2c(struct bsg_job *bsg_job)
  1485. {
  1486. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1487. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1488. scsi_qla_host_t *vha = shost_priv(host);
  1489. struct qla_hw_data *ha = vha->hw;
  1490. int rval = 0;
  1491. uint8_t bsg[DMA_POOL_SIZE];
  1492. struct qla_i2c_access *i2c = (void *)bsg;
  1493. dma_addr_t sfp_dma;
  1494. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1495. if (!sfp) {
  1496. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1497. EXT_STATUS_NO_MEMORY;
  1498. goto done;
  1499. }
  1500. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1501. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1502. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1503. i2c->device, i2c->offset, i2c->length, i2c->option);
  1504. if (rval) {
  1505. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1506. EXT_STATUS_MAILBOX;
  1507. goto dealloc;
  1508. }
  1509. memcpy(i2c->buffer, sfp, i2c->length);
  1510. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1511. bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
  1512. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1513. dealloc:
  1514. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1515. done:
  1516. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1517. bsg_reply->reply_payload_rcv_len = sizeof(*i2c);
  1518. bsg_reply->result = DID_OK << 16;
  1519. bsg_job_done(bsg_job, bsg_reply->result,
  1520. bsg_reply->reply_payload_rcv_len);
  1521. return 0;
  1522. }
  1523. static int
  1524. qla24xx_process_bidir_cmd(struct bsg_job *bsg_job)
  1525. {
  1526. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1527. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1528. scsi_qla_host_t *vha = shost_priv(host);
  1529. struct qla_hw_data *ha = vha->hw;
  1530. uint32_t rval = EXT_STATUS_OK;
  1531. uint16_t req_sg_cnt = 0;
  1532. uint16_t rsp_sg_cnt = 0;
  1533. uint16_t nextlid = 0;
  1534. uint32_t tot_dsds;
  1535. srb_t *sp = NULL;
  1536. uint32_t req_data_len;
  1537. uint32_t rsp_data_len;
  1538. /* Check the type of the adapter */
  1539. if (!IS_BIDI_CAPABLE(ha)) {
  1540. ql_log(ql_log_warn, vha, 0x70a0,
  1541. "This adapter is not supported\n");
  1542. rval = EXT_STATUS_NOT_SUPPORTED;
  1543. goto done;
  1544. }
  1545. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  1546. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  1547. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  1548. rval = EXT_STATUS_BUSY;
  1549. goto done;
  1550. }
  1551. /* Check if host is online */
  1552. if (!vha->flags.online) {
  1553. ql_log(ql_log_warn, vha, 0x70a1,
  1554. "Host is not online\n");
  1555. rval = EXT_STATUS_DEVICE_OFFLINE;
  1556. goto done;
  1557. }
  1558. /* Check if cable is plugged in or not */
  1559. if (vha->device_flags & DFLG_NO_CABLE) {
  1560. ql_log(ql_log_warn, vha, 0x70a2,
  1561. "Cable is unplugged...\n");
  1562. rval = EXT_STATUS_INVALID_CFG;
  1563. goto done;
  1564. }
  1565. /* Check if the switch is connected or not */
  1566. if (ha->current_topology != ISP_CFG_F) {
  1567. ql_log(ql_log_warn, vha, 0x70a3,
  1568. "Host is not connected to the switch\n");
  1569. rval = EXT_STATUS_INVALID_CFG;
  1570. goto done;
  1571. }
  1572. /* Check if operating mode is P2P */
  1573. if (ha->operating_mode != P2P) {
  1574. ql_log(ql_log_warn, vha, 0x70a4,
  1575. "Host operating mode is not P2p\n");
  1576. rval = EXT_STATUS_INVALID_CFG;
  1577. goto done;
  1578. }
  1579. mutex_lock(&ha->selflogin_lock);
  1580. if (vha->self_login_loop_id == 0) {
  1581. /* Initialize all required fields of fcport */
  1582. vha->bidir_fcport.vha = vha;
  1583. vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
  1584. vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
  1585. vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
  1586. vha->bidir_fcport.loop_id = vha->loop_id;
  1587. if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
  1588. ql_log(ql_log_warn, vha, 0x70a7,
  1589. "Failed to login port %06X for bidirectional IOCB\n",
  1590. vha->bidir_fcport.d_id.b24);
  1591. mutex_unlock(&ha->selflogin_lock);
  1592. rval = EXT_STATUS_MAILBOX;
  1593. goto done;
  1594. }
  1595. vha->self_login_loop_id = nextlid - 1;
  1596. }
  1597. /* Assign the self login loop id to fcport */
  1598. mutex_unlock(&ha->selflogin_lock);
  1599. vha->bidir_fcport.loop_id = vha->self_login_loop_id;
  1600. req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1601. bsg_job->request_payload.sg_list,
  1602. bsg_job->request_payload.sg_cnt,
  1603. DMA_TO_DEVICE);
  1604. if (!req_sg_cnt) {
  1605. rval = EXT_STATUS_NO_MEMORY;
  1606. goto done;
  1607. }
  1608. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1609. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  1610. DMA_FROM_DEVICE);
  1611. if (!rsp_sg_cnt) {
  1612. rval = EXT_STATUS_NO_MEMORY;
  1613. goto done_unmap_req_sg;
  1614. }
  1615. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  1616. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  1617. ql_dbg(ql_dbg_user, vha, 0x70a9,
  1618. "Dma mapping resulted in different sg counts "
  1619. "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
  1620. "%x dma_reply_sg_cnt: %x]\n",
  1621. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  1622. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  1623. rval = EXT_STATUS_NO_MEMORY;
  1624. goto done_unmap_sg;
  1625. }
  1626. req_data_len = bsg_job->request_payload.payload_len;
  1627. rsp_data_len = bsg_job->reply_payload.payload_len;
  1628. if (req_data_len != rsp_data_len) {
  1629. rval = EXT_STATUS_BUSY;
  1630. ql_log(ql_log_warn, vha, 0x70aa,
  1631. "req_data_len != rsp_data_len\n");
  1632. goto done_unmap_sg;
  1633. }
  1634. /* Alloc SRB structure */
  1635. sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
  1636. if (!sp) {
  1637. ql_dbg(ql_dbg_user, vha, 0x70ac,
  1638. "Alloc SRB structure failed\n");
  1639. rval = EXT_STATUS_NO_MEMORY;
  1640. goto done_unmap_sg;
  1641. }
  1642. /*Populate srb->ctx with bidir ctx*/
  1643. sp->u.bsg_job = bsg_job;
  1644. sp->free = qla2x00_bsg_sp_free;
  1645. sp->type = SRB_BIDI_CMD;
  1646. sp->done = qla2x00_bsg_job_done;
  1647. /* Add the read and write sg count */
  1648. tot_dsds = rsp_sg_cnt + req_sg_cnt;
  1649. rval = qla2x00_start_bidir(sp, vha, tot_dsds);
  1650. if (rval != EXT_STATUS_OK)
  1651. goto done_free_srb;
  1652. /* the bsg request will be completed in the interrupt handler */
  1653. return rval;
  1654. done_free_srb:
  1655. mempool_free(sp, ha->srb_mempool);
  1656. done_unmap_sg:
  1657. dma_unmap_sg(&ha->pdev->dev,
  1658. bsg_job->reply_payload.sg_list,
  1659. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1660. done_unmap_req_sg:
  1661. dma_unmap_sg(&ha->pdev->dev,
  1662. bsg_job->request_payload.sg_list,
  1663. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1664. done:
  1665. /* Return an error vendor specific response
  1666. * and complete the bsg request
  1667. */
  1668. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
  1669. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1670. bsg_reply->reply_payload_rcv_len = 0;
  1671. bsg_reply->result = (DID_OK) << 16;
  1672. bsg_job_done(bsg_job, bsg_reply->result,
  1673. bsg_reply->reply_payload_rcv_len);
  1674. /* Always return success, vendor rsp carries correct status */
  1675. return 0;
  1676. }
  1677. static int
  1678. qlafx00_mgmt_cmd(struct bsg_job *bsg_job)
  1679. {
  1680. struct fc_bsg_request *bsg_request = bsg_job->request;
  1681. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1682. scsi_qla_host_t *vha = shost_priv(host);
  1683. struct qla_hw_data *ha = vha->hw;
  1684. int rval = (DID_ERROR << 16);
  1685. struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
  1686. srb_t *sp;
  1687. int req_sg_cnt = 0, rsp_sg_cnt = 0;
  1688. struct fc_port *fcport;
  1689. char *type = "FC_BSG_HST_FX_MGMT";
  1690. /* Copy the IOCB specific information */
  1691. piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
  1692. &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  1693. /* Dump the vendor information */
  1694. ql_dump_buffer(ql_dbg_user + ql_dbg_verbose , vha, 0x70cf,
  1695. (uint8_t *)piocb_rqst, sizeof(struct qla_mt_iocb_rqst_fx00));
  1696. if (!vha->flags.online) {
  1697. ql_log(ql_log_warn, vha, 0x70d0,
  1698. "Host is not online.\n");
  1699. rval = -EIO;
  1700. goto done;
  1701. }
  1702. if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID) {
  1703. req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1704. bsg_job->request_payload.sg_list,
  1705. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1706. if (!req_sg_cnt) {
  1707. ql_log(ql_log_warn, vha, 0x70c7,
  1708. "dma_map_sg return %d for request\n", req_sg_cnt);
  1709. rval = -ENOMEM;
  1710. goto done;
  1711. }
  1712. }
  1713. if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID) {
  1714. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1715. bsg_job->reply_payload.sg_list,
  1716. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1717. if (!rsp_sg_cnt) {
  1718. ql_log(ql_log_warn, vha, 0x70c8,
  1719. "dma_map_sg return %d for reply\n", rsp_sg_cnt);
  1720. rval = -ENOMEM;
  1721. goto done_unmap_req_sg;
  1722. }
  1723. }
  1724. ql_dbg(ql_dbg_user, vha, 0x70c9,
  1725. "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
  1726. "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
  1727. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  1728. /* Allocate a dummy fcport structure, since functions preparing the
  1729. * IOCB and mailbox command retrieves port specific information
  1730. * from fcport structure. For Host based ELS commands there will be
  1731. * no fcport structure allocated
  1732. */
  1733. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  1734. if (!fcport) {
  1735. ql_log(ql_log_warn, vha, 0x70ca,
  1736. "Failed to allocate fcport.\n");
  1737. rval = -ENOMEM;
  1738. goto done_unmap_rsp_sg;
  1739. }
  1740. /* Alloc SRB structure */
  1741. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  1742. if (!sp) {
  1743. ql_log(ql_log_warn, vha, 0x70cb,
  1744. "qla2x00_get_sp failed.\n");
  1745. rval = -ENOMEM;
  1746. goto done_free_fcport;
  1747. }
  1748. /* Initialize all required fields of fcport */
  1749. fcport->vha = vha;
  1750. fcport->loop_id = piocb_rqst->dataword;
  1751. sp->type = SRB_FXIOCB_BCMD;
  1752. sp->name = "bsg_fx_mgmt";
  1753. sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
  1754. sp->u.bsg_job = bsg_job;
  1755. sp->free = qla2x00_bsg_sp_free;
  1756. sp->done = qla2x00_bsg_job_done;
  1757. ql_dbg(ql_dbg_user, vha, 0x70cc,
  1758. "bsg rqst type: %s fx_mgmt_type: %x id=%x\n",
  1759. type, piocb_rqst->func_type, fcport->loop_id);
  1760. rval = qla2x00_start_sp(sp);
  1761. if (rval != QLA_SUCCESS) {
  1762. ql_log(ql_log_warn, vha, 0x70cd,
  1763. "qla2x00_start_sp failed=%d.\n", rval);
  1764. mempool_free(sp, ha->srb_mempool);
  1765. rval = -EIO;
  1766. goto done_free_fcport;
  1767. }
  1768. return rval;
  1769. done_free_fcport:
  1770. kfree(fcport);
  1771. done_unmap_rsp_sg:
  1772. if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
  1773. dma_unmap_sg(&ha->pdev->dev,
  1774. bsg_job->reply_payload.sg_list,
  1775. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1776. done_unmap_req_sg:
  1777. if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
  1778. dma_unmap_sg(&ha->pdev->dev,
  1779. bsg_job->request_payload.sg_list,
  1780. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1781. done:
  1782. return rval;
  1783. }
  1784. static int
  1785. qla26xx_serdes_op(struct bsg_job *bsg_job)
  1786. {
  1787. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1788. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1789. scsi_qla_host_t *vha = shost_priv(host);
  1790. int rval = 0;
  1791. struct qla_serdes_reg sr;
  1792. memset(&sr, 0, sizeof(sr));
  1793. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1794. bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
  1795. switch (sr.cmd) {
  1796. case INT_SC_SERDES_WRITE_REG:
  1797. rval = qla2x00_write_serdes_word(vha, sr.addr, sr.val);
  1798. bsg_reply->reply_payload_rcv_len = 0;
  1799. break;
  1800. case INT_SC_SERDES_READ_REG:
  1801. rval = qla2x00_read_serdes_word(vha, sr.addr, &sr.val);
  1802. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1803. bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
  1804. bsg_reply->reply_payload_rcv_len = sizeof(sr);
  1805. break;
  1806. default:
  1807. ql_dbg(ql_dbg_user, vha, 0x708c,
  1808. "Unknown serdes cmd %x.\n", sr.cmd);
  1809. rval = -EINVAL;
  1810. break;
  1811. }
  1812. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1813. rval ? EXT_STATUS_MAILBOX : 0;
  1814. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1815. bsg_reply->result = DID_OK << 16;
  1816. bsg_job_done(bsg_job, bsg_reply->result,
  1817. bsg_reply->reply_payload_rcv_len);
  1818. return 0;
  1819. }
  1820. static int
  1821. qla8044_serdes_op(struct bsg_job *bsg_job)
  1822. {
  1823. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1824. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1825. scsi_qla_host_t *vha = shost_priv(host);
  1826. int rval = 0;
  1827. struct qla_serdes_reg_ex sr;
  1828. memset(&sr, 0, sizeof(sr));
  1829. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1830. bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
  1831. switch (sr.cmd) {
  1832. case INT_SC_SERDES_WRITE_REG:
  1833. rval = qla8044_write_serdes_word(vha, sr.addr, sr.val);
  1834. bsg_reply->reply_payload_rcv_len = 0;
  1835. break;
  1836. case INT_SC_SERDES_READ_REG:
  1837. rval = qla8044_read_serdes_word(vha, sr.addr, &sr.val);
  1838. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1839. bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
  1840. bsg_reply->reply_payload_rcv_len = sizeof(sr);
  1841. break;
  1842. default:
  1843. ql_dbg(ql_dbg_user, vha, 0x7020,
  1844. "Unknown serdes cmd %x.\n", sr.cmd);
  1845. rval = -EINVAL;
  1846. break;
  1847. }
  1848. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1849. rval ? EXT_STATUS_MAILBOX : 0;
  1850. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1851. bsg_reply->result = DID_OK << 16;
  1852. bsg_job_done(bsg_job, bsg_reply->result,
  1853. bsg_reply->reply_payload_rcv_len);
  1854. return 0;
  1855. }
  1856. static int
  1857. qla27xx_get_flash_upd_cap(struct bsg_job *bsg_job)
  1858. {
  1859. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1860. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1861. scsi_qla_host_t *vha = shost_priv(host);
  1862. struct qla_hw_data *ha = vha->hw;
  1863. struct qla_flash_update_caps cap;
  1864. if (!(IS_QLA27XX(ha)))
  1865. return -EPERM;
  1866. memset(&cap, 0, sizeof(cap));
  1867. cap.capabilities = (uint64_t)ha->fw_attributes_ext[1] << 48 |
  1868. (uint64_t)ha->fw_attributes_ext[0] << 32 |
  1869. (uint64_t)ha->fw_attributes_h << 16 |
  1870. (uint64_t)ha->fw_attributes;
  1871. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1872. bsg_job->reply_payload.sg_cnt, &cap, sizeof(cap));
  1873. bsg_reply->reply_payload_rcv_len = sizeof(cap);
  1874. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1875. EXT_STATUS_OK;
  1876. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1877. bsg_reply->result = DID_OK << 16;
  1878. bsg_job_done(bsg_job, bsg_reply->result,
  1879. bsg_reply->reply_payload_rcv_len);
  1880. return 0;
  1881. }
  1882. static int
  1883. qla27xx_set_flash_upd_cap(struct bsg_job *bsg_job)
  1884. {
  1885. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1886. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1887. scsi_qla_host_t *vha = shost_priv(host);
  1888. struct qla_hw_data *ha = vha->hw;
  1889. uint64_t online_fw_attr = 0;
  1890. struct qla_flash_update_caps cap;
  1891. if (!(IS_QLA27XX(ha)))
  1892. return -EPERM;
  1893. memset(&cap, 0, sizeof(cap));
  1894. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1895. bsg_job->request_payload.sg_cnt, &cap, sizeof(cap));
  1896. online_fw_attr = (uint64_t)ha->fw_attributes_ext[1] << 48 |
  1897. (uint64_t)ha->fw_attributes_ext[0] << 32 |
  1898. (uint64_t)ha->fw_attributes_h << 16 |
  1899. (uint64_t)ha->fw_attributes;
  1900. if (online_fw_attr != cap.capabilities) {
  1901. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1902. EXT_STATUS_INVALID_PARAM;
  1903. return -EINVAL;
  1904. }
  1905. if (cap.outage_duration < MAX_LOOP_TIMEOUT) {
  1906. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1907. EXT_STATUS_INVALID_PARAM;
  1908. return -EINVAL;
  1909. }
  1910. bsg_reply->reply_payload_rcv_len = 0;
  1911. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1912. EXT_STATUS_OK;
  1913. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1914. bsg_reply->result = DID_OK << 16;
  1915. bsg_job_done(bsg_job, bsg_reply->result,
  1916. bsg_reply->reply_payload_rcv_len);
  1917. return 0;
  1918. }
  1919. static int
  1920. qla27xx_get_bbcr_data(struct bsg_job *bsg_job)
  1921. {
  1922. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1923. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1924. scsi_qla_host_t *vha = shost_priv(host);
  1925. struct qla_hw_data *ha = vha->hw;
  1926. struct qla_bbcr_data bbcr;
  1927. uint16_t loop_id, topo, sw_cap;
  1928. uint8_t domain, area, al_pa, state;
  1929. int rval;
  1930. if (!(IS_QLA27XX(ha)))
  1931. return -EPERM;
  1932. memset(&bbcr, 0, sizeof(bbcr));
  1933. if (vha->flags.bbcr_enable)
  1934. bbcr.status = QLA_BBCR_STATUS_ENABLED;
  1935. else
  1936. bbcr.status = QLA_BBCR_STATUS_DISABLED;
  1937. if (bbcr.status == QLA_BBCR_STATUS_ENABLED) {
  1938. rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
  1939. &area, &domain, &topo, &sw_cap);
  1940. if (rval != QLA_SUCCESS) {
  1941. bbcr.status = QLA_BBCR_STATUS_UNKNOWN;
  1942. bbcr.state = QLA_BBCR_STATE_OFFLINE;
  1943. bbcr.mbx1 = loop_id;
  1944. goto done;
  1945. }
  1946. state = (vha->bbcr >> 12) & 0x1;
  1947. if (state) {
  1948. bbcr.state = QLA_BBCR_STATE_OFFLINE;
  1949. bbcr.offline_reason_code = QLA_BBCR_REASON_LOGIN_REJECT;
  1950. } else {
  1951. bbcr.state = QLA_BBCR_STATE_ONLINE;
  1952. bbcr.negotiated_bbscn = (vha->bbcr >> 8) & 0xf;
  1953. }
  1954. bbcr.configured_bbscn = vha->bbcr & 0xf;
  1955. }
  1956. done:
  1957. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1958. bsg_job->reply_payload.sg_cnt, &bbcr, sizeof(bbcr));
  1959. bsg_reply->reply_payload_rcv_len = sizeof(bbcr);
  1960. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = EXT_STATUS_OK;
  1961. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1962. bsg_reply->result = DID_OK << 16;
  1963. bsg_job_done(bsg_job, bsg_reply->result,
  1964. bsg_reply->reply_payload_rcv_len);
  1965. return 0;
  1966. }
  1967. static int
  1968. qla2x00_get_priv_stats(struct bsg_job *bsg_job)
  1969. {
  1970. struct fc_bsg_request *bsg_request = bsg_job->request;
  1971. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1972. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1973. scsi_qla_host_t *vha = shost_priv(host);
  1974. struct qla_hw_data *ha = vha->hw;
  1975. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  1976. struct link_statistics *stats = NULL;
  1977. dma_addr_t stats_dma;
  1978. int rval;
  1979. uint32_t *cmd = bsg_request->rqst_data.h_vendor.vendor_cmd;
  1980. uint options = cmd[0] == QL_VND_GET_PRIV_STATS_EX ? cmd[1] : 0;
  1981. if (test_bit(UNLOADING, &vha->dpc_flags))
  1982. return -ENODEV;
  1983. if (unlikely(pci_channel_offline(ha->pdev)))
  1984. return -ENODEV;
  1985. if (qla2x00_reset_active(vha))
  1986. return -EBUSY;
  1987. if (!IS_FWI2_CAPABLE(ha))
  1988. return -EPERM;
  1989. stats = dma_zalloc_coherent(&ha->pdev->dev, sizeof(*stats),
  1990. &stats_dma, GFP_KERNEL);
  1991. if (!stats) {
  1992. ql_log(ql_log_warn, vha, 0x70e2,
  1993. "Failed to allocate memory for stats.\n");
  1994. return -ENOMEM;
  1995. }
  1996. rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, options);
  1997. if (rval == QLA_SUCCESS) {
  1998. ql_dump_buffer(ql_dbg_user + ql_dbg_verbose, vha, 0x70e3,
  1999. (uint8_t *)stats, sizeof(*stats));
  2000. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  2001. bsg_job->reply_payload.sg_cnt, stats, sizeof(*stats));
  2002. }
  2003. bsg_reply->reply_payload_rcv_len = sizeof(*stats);
  2004. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  2005. rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
  2006. bsg_job->reply_len = sizeof(*bsg_reply);
  2007. bsg_reply->result = DID_OK << 16;
  2008. bsg_job_done(bsg_job, bsg_reply->result,
  2009. bsg_reply->reply_payload_rcv_len);
  2010. dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
  2011. stats, stats_dma);
  2012. return 0;
  2013. }
  2014. static int
  2015. qla2x00_do_dport_diagnostics(struct bsg_job *bsg_job)
  2016. {
  2017. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  2018. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  2019. scsi_qla_host_t *vha = shost_priv(host);
  2020. int rval;
  2021. struct qla_dport_diag *dd;
  2022. if (!IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw))
  2023. return -EPERM;
  2024. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  2025. if (!dd) {
  2026. ql_log(ql_log_warn, vha, 0x70db,
  2027. "Failed to allocate memory for dport.\n");
  2028. return -ENOMEM;
  2029. }
  2030. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  2031. bsg_job->request_payload.sg_cnt, dd, sizeof(*dd));
  2032. rval = qla26xx_dport_diagnostics(
  2033. vha, dd->buf, sizeof(dd->buf), dd->options);
  2034. if (rval == QLA_SUCCESS) {
  2035. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  2036. bsg_job->reply_payload.sg_cnt, dd, sizeof(*dd));
  2037. }
  2038. bsg_reply->reply_payload_rcv_len = sizeof(*dd);
  2039. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  2040. rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
  2041. bsg_job->reply_len = sizeof(*bsg_reply);
  2042. bsg_reply->result = DID_OK << 16;
  2043. bsg_job_done(bsg_job, bsg_reply->result,
  2044. bsg_reply->reply_payload_rcv_len);
  2045. kfree(dd);
  2046. return 0;
  2047. }
  2048. static int
  2049. qla2x00_process_vendor_specific(struct bsg_job *bsg_job)
  2050. {
  2051. struct fc_bsg_request *bsg_request = bsg_job->request;
  2052. switch (bsg_request->rqst_data.h_vendor.vendor_cmd[0]) {
  2053. case QL_VND_LOOPBACK:
  2054. return qla2x00_process_loopback(bsg_job);
  2055. case QL_VND_A84_RESET:
  2056. return qla84xx_reset(bsg_job);
  2057. case QL_VND_A84_UPDATE_FW:
  2058. return qla84xx_updatefw(bsg_job);
  2059. case QL_VND_A84_MGMT_CMD:
  2060. return qla84xx_mgmt_cmd(bsg_job);
  2061. case QL_VND_IIDMA:
  2062. return qla24xx_iidma(bsg_job);
  2063. case QL_VND_FCP_PRIO_CFG_CMD:
  2064. return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
  2065. case QL_VND_READ_FLASH:
  2066. return qla2x00_read_optrom(bsg_job);
  2067. case QL_VND_UPDATE_FLASH:
  2068. return qla2x00_update_optrom(bsg_job);
  2069. case QL_VND_SET_FRU_VERSION:
  2070. return qla2x00_update_fru_versions(bsg_job);
  2071. case QL_VND_READ_FRU_STATUS:
  2072. return qla2x00_read_fru_status(bsg_job);
  2073. case QL_VND_WRITE_FRU_STATUS:
  2074. return qla2x00_write_fru_status(bsg_job);
  2075. case QL_VND_WRITE_I2C:
  2076. return qla2x00_write_i2c(bsg_job);
  2077. case QL_VND_READ_I2C:
  2078. return qla2x00_read_i2c(bsg_job);
  2079. case QL_VND_DIAG_IO_CMD:
  2080. return qla24xx_process_bidir_cmd(bsg_job);
  2081. case QL_VND_FX00_MGMT_CMD:
  2082. return qlafx00_mgmt_cmd(bsg_job);
  2083. case QL_VND_SERDES_OP:
  2084. return qla26xx_serdes_op(bsg_job);
  2085. case QL_VND_SERDES_OP_EX:
  2086. return qla8044_serdes_op(bsg_job);
  2087. case QL_VND_GET_FLASH_UPDATE_CAPS:
  2088. return qla27xx_get_flash_upd_cap(bsg_job);
  2089. case QL_VND_SET_FLASH_UPDATE_CAPS:
  2090. return qla27xx_set_flash_upd_cap(bsg_job);
  2091. case QL_VND_GET_BBCR_DATA:
  2092. return qla27xx_get_bbcr_data(bsg_job);
  2093. case QL_VND_GET_PRIV_STATS:
  2094. case QL_VND_GET_PRIV_STATS_EX:
  2095. return qla2x00_get_priv_stats(bsg_job);
  2096. case QL_VND_DPORT_DIAGNOSTICS:
  2097. return qla2x00_do_dport_diagnostics(bsg_job);
  2098. default:
  2099. return -ENOSYS;
  2100. }
  2101. }
  2102. int
  2103. qla24xx_bsg_request(struct bsg_job *bsg_job)
  2104. {
  2105. struct fc_bsg_request *bsg_request = bsg_job->request;
  2106. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  2107. int ret = -EINVAL;
  2108. struct fc_rport *rport;
  2109. struct Scsi_Host *host;
  2110. scsi_qla_host_t *vha;
  2111. /* In case no data transferred. */
  2112. bsg_reply->reply_payload_rcv_len = 0;
  2113. if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
  2114. rport = fc_bsg_to_rport(bsg_job);
  2115. host = rport_to_shost(rport);
  2116. vha = shost_priv(host);
  2117. } else {
  2118. host = fc_bsg_to_shost(bsg_job);
  2119. vha = shost_priv(host);
  2120. }
  2121. if (qla2x00_chip_is_down(vha)) {
  2122. ql_dbg(ql_dbg_user, vha, 0x709f,
  2123. "BSG: ISP abort active/needed -- cmd=%d.\n",
  2124. bsg_request->msgcode);
  2125. return -EBUSY;
  2126. }
  2127. ql_dbg(ql_dbg_user, vha, 0x7000,
  2128. "Entered %s msgcode=0x%x.\n", __func__, bsg_request->msgcode);
  2129. switch (bsg_request->msgcode) {
  2130. case FC_BSG_RPT_ELS:
  2131. case FC_BSG_HST_ELS_NOLOGIN:
  2132. ret = qla2x00_process_els(bsg_job);
  2133. break;
  2134. case FC_BSG_HST_CT:
  2135. ret = qla2x00_process_ct(bsg_job);
  2136. break;
  2137. case FC_BSG_HST_VENDOR:
  2138. ret = qla2x00_process_vendor_specific(bsg_job);
  2139. break;
  2140. case FC_BSG_HST_ADD_RPORT:
  2141. case FC_BSG_HST_DEL_RPORT:
  2142. case FC_BSG_RPT_CT:
  2143. default:
  2144. ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
  2145. break;
  2146. }
  2147. return ret;
  2148. }
  2149. int
  2150. qla24xx_bsg_timeout(struct bsg_job *bsg_job)
  2151. {
  2152. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  2153. scsi_qla_host_t *vha = shost_priv(fc_bsg_to_shost(bsg_job));
  2154. struct qla_hw_data *ha = vha->hw;
  2155. srb_t *sp;
  2156. int cnt, que;
  2157. unsigned long flags;
  2158. struct req_que *req;
  2159. /* find the bsg job from the active list of commands */
  2160. spin_lock_irqsave(&ha->hardware_lock, flags);
  2161. for (que = 0; que < ha->max_req_queues; que++) {
  2162. req = ha->req_q_map[que];
  2163. if (!req)
  2164. continue;
  2165. for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
  2166. sp = req->outstanding_cmds[cnt];
  2167. if (sp) {
  2168. if (((sp->type == SRB_CT_CMD) ||
  2169. (sp->type == SRB_ELS_CMD_HST) ||
  2170. (sp->type == SRB_FXIOCB_BCMD))
  2171. && (sp->u.bsg_job == bsg_job)) {
  2172. req->outstanding_cmds[cnt] = NULL;
  2173. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2174. if (ha->isp_ops->abort_command(sp)) {
  2175. ql_log(ql_log_warn, vha, 0x7089,
  2176. "mbx abort_command "
  2177. "failed.\n");
  2178. bsg_reply->result = -EIO;
  2179. } else {
  2180. ql_dbg(ql_dbg_user, vha, 0x708a,
  2181. "mbx abort_command "
  2182. "success.\n");
  2183. bsg_reply->result = 0;
  2184. }
  2185. spin_lock_irqsave(&ha->hardware_lock, flags);
  2186. goto done;
  2187. }
  2188. }
  2189. }
  2190. }
  2191. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2192. ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
  2193. bsg_reply->result = -ENXIO;
  2194. return 0;
  2195. done:
  2196. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2197. sp->free(sp);
  2198. return 0;
  2199. }