qla_iocb.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2011 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/blkdev.h>
  9. #include <linux/delay.h>
  10. #include <scsi/scsi_tcq.h>
  11. static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
  12. static void qla25xx_set_que(srb_t *, struct rsp_que **);
  13. /**
  14. * qla2x00_get_cmd_direction() - Determine control_flag data direction.
  15. * @cmd: SCSI command
  16. *
  17. * Returns the proper CF_* direction based on CDB.
  18. */
  19. static inline uint16_t
  20. qla2x00_get_cmd_direction(srb_t *sp)
  21. {
  22. uint16_t cflags;
  23. cflags = 0;
  24. /* Set transfer direction */
  25. if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
  26. cflags = CF_WRITE;
  27. sp->fcport->vha->hw->qla_stats.output_bytes +=
  28. scsi_bufflen(sp->cmd);
  29. } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
  30. cflags = CF_READ;
  31. sp->fcport->vha->hw->qla_stats.input_bytes +=
  32. scsi_bufflen(sp->cmd);
  33. }
  34. return (cflags);
  35. }
  36. /**
  37. * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
  38. * Continuation Type 0 IOCBs to allocate.
  39. *
  40. * @dsds: number of data segment decriptors needed
  41. *
  42. * Returns the number of IOCB entries needed to store @dsds.
  43. */
  44. uint16_t
  45. qla2x00_calc_iocbs_32(uint16_t dsds)
  46. {
  47. uint16_t iocbs;
  48. iocbs = 1;
  49. if (dsds > 3) {
  50. iocbs += (dsds - 3) / 7;
  51. if ((dsds - 3) % 7)
  52. iocbs++;
  53. }
  54. return (iocbs);
  55. }
  56. /**
  57. * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
  58. * Continuation Type 1 IOCBs to allocate.
  59. *
  60. * @dsds: number of data segment decriptors needed
  61. *
  62. * Returns the number of IOCB entries needed to store @dsds.
  63. */
  64. uint16_t
  65. qla2x00_calc_iocbs_64(uint16_t dsds)
  66. {
  67. uint16_t iocbs;
  68. iocbs = 1;
  69. if (dsds > 2) {
  70. iocbs += (dsds - 2) / 5;
  71. if ((dsds - 2) % 5)
  72. iocbs++;
  73. }
  74. return (iocbs);
  75. }
  76. /**
  77. * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
  78. * @ha: HA context
  79. *
  80. * Returns a pointer to the Continuation Type 0 IOCB packet.
  81. */
  82. static inline cont_entry_t *
  83. qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
  84. {
  85. cont_entry_t *cont_pkt;
  86. struct req_que *req = vha->req;
  87. /* Adjust ring index. */
  88. req->ring_index++;
  89. if (req->ring_index == req->length) {
  90. req->ring_index = 0;
  91. req->ring_ptr = req->ring;
  92. } else {
  93. req->ring_ptr++;
  94. }
  95. cont_pkt = (cont_entry_t *)req->ring_ptr;
  96. /* Load packet defaults. */
  97. *((uint32_t *)(&cont_pkt->entry_type)) =
  98. __constant_cpu_to_le32(CONTINUE_TYPE);
  99. return (cont_pkt);
  100. }
  101. /**
  102. * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
  103. * @ha: HA context
  104. *
  105. * Returns a pointer to the continuation type 1 IOCB packet.
  106. */
  107. static inline cont_a64_entry_t *
  108. qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
  109. {
  110. cont_a64_entry_t *cont_pkt;
  111. struct req_que *req = vha->req;
  112. /* Adjust ring index. */
  113. req->ring_index++;
  114. if (req->ring_index == req->length) {
  115. req->ring_index = 0;
  116. req->ring_ptr = req->ring;
  117. } else {
  118. req->ring_ptr++;
  119. }
  120. cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
  121. /* Load packet defaults. */
  122. *((uint32_t *)(&cont_pkt->entry_type)) =
  123. __constant_cpu_to_le32(CONTINUE_A64_TYPE);
  124. return (cont_pkt);
  125. }
  126. static inline int
  127. qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
  128. {
  129. uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
  130. /* We only support T10 DIF right now */
  131. if (guard != SHOST_DIX_GUARD_CRC) {
  132. ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3007,
  133. "Unsupported guard: %d for cmd=%p.\n", guard, sp->cmd);
  134. return 0;
  135. }
  136. /* We always use DIFF Bundling for best performance */
  137. *fw_prot_opts = 0;
  138. /* Translate SCSI opcode to a protection opcode */
  139. switch (scsi_get_prot_op(sp->cmd)) {
  140. case SCSI_PROT_READ_STRIP:
  141. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  142. break;
  143. case SCSI_PROT_WRITE_INSERT:
  144. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  145. break;
  146. case SCSI_PROT_READ_INSERT:
  147. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  148. break;
  149. case SCSI_PROT_WRITE_STRIP:
  150. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  151. break;
  152. case SCSI_PROT_READ_PASS:
  153. *fw_prot_opts |= PO_MODE_DIF_PASS;
  154. break;
  155. case SCSI_PROT_WRITE_PASS:
  156. *fw_prot_opts |= PO_MODE_DIF_PASS;
  157. break;
  158. default: /* Normal Request */
  159. *fw_prot_opts |= PO_MODE_DIF_PASS;
  160. break;
  161. }
  162. return scsi_prot_sg_count(sp->cmd);
  163. }
  164. /*
  165. * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
  166. * capable IOCB types.
  167. *
  168. * @sp: SRB command to process
  169. * @cmd_pkt: Command type 2 IOCB
  170. * @tot_dsds: Total number of segments to transfer
  171. */
  172. void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
  173. uint16_t tot_dsds)
  174. {
  175. uint16_t avail_dsds;
  176. uint32_t *cur_dsd;
  177. scsi_qla_host_t *vha;
  178. struct scsi_cmnd *cmd;
  179. struct scatterlist *sg;
  180. int i;
  181. cmd = sp->cmd;
  182. /* Update entry type to indicate Command Type 2 IOCB */
  183. *((uint32_t *)(&cmd_pkt->entry_type)) =
  184. __constant_cpu_to_le32(COMMAND_TYPE);
  185. /* No data transfer */
  186. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  187. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  188. return;
  189. }
  190. vha = sp->fcport->vha;
  191. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  192. /* Three DSDs are available in the Command Type 2 IOCB */
  193. avail_dsds = 3;
  194. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  195. /* Load data segments */
  196. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  197. cont_entry_t *cont_pkt;
  198. /* Allocate additional continuation packets? */
  199. if (avail_dsds == 0) {
  200. /*
  201. * Seven DSDs are available in the Continuation
  202. * Type 0 IOCB.
  203. */
  204. cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
  205. cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
  206. avail_dsds = 7;
  207. }
  208. *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
  209. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  210. avail_dsds--;
  211. }
  212. }
  213. /**
  214. * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
  215. * capable IOCB types.
  216. *
  217. * @sp: SRB command to process
  218. * @cmd_pkt: Command type 3 IOCB
  219. * @tot_dsds: Total number of segments to transfer
  220. */
  221. void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
  222. uint16_t tot_dsds)
  223. {
  224. uint16_t avail_dsds;
  225. uint32_t *cur_dsd;
  226. scsi_qla_host_t *vha;
  227. struct scsi_cmnd *cmd;
  228. struct scatterlist *sg;
  229. int i;
  230. cmd = sp->cmd;
  231. /* Update entry type to indicate Command Type 3 IOCB */
  232. *((uint32_t *)(&cmd_pkt->entry_type)) =
  233. __constant_cpu_to_le32(COMMAND_A64_TYPE);
  234. /* No data transfer */
  235. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  236. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  237. return;
  238. }
  239. vha = sp->fcport->vha;
  240. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  241. /* Two DSDs are available in the Command Type 3 IOCB */
  242. avail_dsds = 2;
  243. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  244. /* Load data segments */
  245. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  246. dma_addr_t sle_dma;
  247. cont_a64_entry_t *cont_pkt;
  248. /* Allocate additional continuation packets? */
  249. if (avail_dsds == 0) {
  250. /*
  251. * Five DSDs are available in the Continuation
  252. * Type 1 IOCB.
  253. */
  254. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  255. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  256. avail_dsds = 5;
  257. }
  258. sle_dma = sg_dma_address(sg);
  259. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  260. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  261. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  262. avail_dsds--;
  263. }
  264. }
  265. /**
  266. * qla2x00_start_scsi() - Send a SCSI command to the ISP
  267. * @sp: command to send to the ISP
  268. *
  269. * Returns non-zero if a failure occurred, else zero.
  270. */
  271. int
  272. qla2x00_start_scsi(srb_t *sp)
  273. {
  274. int ret, nseg;
  275. unsigned long flags;
  276. scsi_qla_host_t *vha;
  277. struct scsi_cmnd *cmd;
  278. uint32_t *clr_ptr;
  279. uint32_t index;
  280. uint32_t handle;
  281. cmd_entry_t *cmd_pkt;
  282. uint16_t cnt;
  283. uint16_t req_cnt;
  284. uint16_t tot_dsds;
  285. struct device_reg_2xxx __iomem *reg;
  286. struct qla_hw_data *ha;
  287. struct req_que *req;
  288. struct rsp_que *rsp;
  289. char tag[2];
  290. /* Setup device pointers. */
  291. ret = 0;
  292. vha = sp->fcport->vha;
  293. ha = vha->hw;
  294. reg = &ha->iobase->isp;
  295. cmd = sp->cmd;
  296. req = ha->req_q_map[0];
  297. rsp = ha->rsp_q_map[0];
  298. /* So we know we haven't pci_map'ed anything yet */
  299. tot_dsds = 0;
  300. /* Send marker if required */
  301. if (vha->marker_needed != 0) {
  302. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  303. QLA_SUCCESS) {
  304. return (QLA_FUNCTION_FAILED);
  305. }
  306. vha->marker_needed = 0;
  307. }
  308. /* Acquire ring specific lock */
  309. spin_lock_irqsave(&ha->hardware_lock, flags);
  310. /* Check for room in outstanding command list. */
  311. handle = req->current_outstanding_cmd;
  312. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  313. handle++;
  314. if (handle == MAX_OUTSTANDING_COMMANDS)
  315. handle = 1;
  316. if (!req->outstanding_cmds[handle])
  317. break;
  318. }
  319. if (index == MAX_OUTSTANDING_COMMANDS)
  320. goto queuing_error;
  321. /* Map the sg table so we have an accurate count of sg entries needed */
  322. if (scsi_sg_count(cmd)) {
  323. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  324. scsi_sg_count(cmd), cmd->sc_data_direction);
  325. if (unlikely(!nseg))
  326. goto queuing_error;
  327. } else
  328. nseg = 0;
  329. tot_dsds = nseg;
  330. /* Calculate the number of request entries needed. */
  331. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  332. if (req->cnt < (req_cnt + 2)) {
  333. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  334. if (req->ring_index < cnt)
  335. req->cnt = cnt - req->ring_index;
  336. else
  337. req->cnt = req->length -
  338. (req->ring_index - cnt);
  339. }
  340. if (req->cnt < (req_cnt + 2))
  341. goto queuing_error;
  342. /* Build command packet */
  343. req->current_outstanding_cmd = handle;
  344. req->outstanding_cmds[handle] = sp;
  345. sp->handle = handle;
  346. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  347. req->cnt -= req_cnt;
  348. cmd_pkt = (cmd_entry_t *)req->ring_ptr;
  349. cmd_pkt->handle = handle;
  350. /* Zero out remaining portion of packet. */
  351. clr_ptr = (uint32_t *)cmd_pkt + 2;
  352. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  353. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  354. /* Set target ID and LUN number*/
  355. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  356. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  357. /* Update tagged queuing modifier */
  358. if (scsi_populate_tag_msg(cmd, tag)) {
  359. switch (tag[0]) {
  360. case HEAD_OF_QUEUE_TAG:
  361. cmd_pkt->control_flags =
  362. __constant_cpu_to_le16(CF_HEAD_TAG);
  363. break;
  364. case ORDERED_QUEUE_TAG:
  365. cmd_pkt->control_flags =
  366. __constant_cpu_to_le16(CF_ORDERED_TAG);
  367. break;
  368. default:
  369. cmd_pkt->control_flags =
  370. __constant_cpu_to_le16(CF_SIMPLE_TAG);
  371. break;
  372. }
  373. }
  374. /* Load SCSI command packet. */
  375. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  376. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  377. /* Build IOCB segments */
  378. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  379. /* Set total data segment count. */
  380. cmd_pkt->entry_count = (uint8_t)req_cnt;
  381. wmb();
  382. /* Adjust ring index. */
  383. req->ring_index++;
  384. if (req->ring_index == req->length) {
  385. req->ring_index = 0;
  386. req->ring_ptr = req->ring;
  387. } else
  388. req->ring_ptr++;
  389. sp->flags |= SRB_DMA_VALID;
  390. /* Set chip new ring index. */
  391. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
  392. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  393. /* Manage unprocessed RIO/ZIO commands in response queue. */
  394. if (vha->flags.process_response_queue &&
  395. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  396. qla2x00_process_response_queue(rsp);
  397. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  398. return (QLA_SUCCESS);
  399. queuing_error:
  400. if (tot_dsds)
  401. scsi_dma_unmap(cmd);
  402. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  403. return (QLA_FUNCTION_FAILED);
  404. }
  405. /**
  406. * qla2x00_marker() - Send a marker IOCB to the firmware.
  407. * @ha: HA context
  408. * @loop_id: loop ID
  409. * @lun: LUN
  410. * @type: marker modifier
  411. *
  412. * Can be called from both normal and interrupt context.
  413. *
  414. * Returns non-zero if a failure occurred, else zero.
  415. */
  416. static int
  417. __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  418. struct rsp_que *rsp, uint16_t loop_id,
  419. uint16_t lun, uint8_t type)
  420. {
  421. mrk_entry_t *mrk;
  422. struct mrk_entry_24xx *mrk24;
  423. struct qla_hw_data *ha = vha->hw;
  424. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  425. mrk24 = NULL;
  426. mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
  427. if (mrk == NULL) {
  428. ql_log(ql_log_warn, base_vha, 0x3026,
  429. "Failed to allocate Marker IOCB.\n");
  430. return (QLA_FUNCTION_FAILED);
  431. }
  432. mrk->entry_type = MARKER_TYPE;
  433. mrk->modifier = type;
  434. if (type != MK_SYNC_ALL) {
  435. if (IS_FWI2_CAPABLE(ha)) {
  436. mrk24 = (struct mrk_entry_24xx *) mrk;
  437. mrk24->nport_handle = cpu_to_le16(loop_id);
  438. mrk24->lun[1] = LSB(lun);
  439. mrk24->lun[2] = MSB(lun);
  440. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  441. mrk24->vp_index = vha->vp_idx;
  442. mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
  443. } else {
  444. SET_TARGET_ID(ha, mrk->target, loop_id);
  445. mrk->lun = cpu_to_le16(lun);
  446. }
  447. }
  448. wmb();
  449. qla2x00_isp_cmd(vha, req);
  450. return (QLA_SUCCESS);
  451. }
  452. int
  453. qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  454. struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
  455. uint8_t type)
  456. {
  457. int ret;
  458. unsigned long flags = 0;
  459. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  460. ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
  461. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  462. return (ret);
  463. }
  464. /**
  465. * qla2x00_isp_cmd() - Modify the request ring pointer.
  466. * @ha: HA context
  467. *
  468. * Note: The caller must hold the hardware lock before calling this routine.
  469. */
  470. static void
  471. qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
  472. {
  473. struct qla_hw_data *ha = vha->hw;
  474. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  475. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  476. ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x302d,
  477. "IOCB data:\n");
  478. ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302e,
  479. (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE);
  480. /* Adjust ring index. */
  481. req->ring_index++;
  482. if (req->ring_index == req->length) {
  483. req->ring_index = 0;
  484. req->ring_ptr = req->ring;
  485. } else
  486. req->ring_ptr++;
  487. /* Set chip new ring index. */
  488. if (IS_QLA82XX(ha)) {
  489. uint32_t dbval = 0x04 | (ha->portnum << 5);
  490. /* write, read and verify logic */
  491. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  492. if (ql2xdbwr)
  493. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  494. else {
  495. WRT_REG_DWORD(
  496. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  497. dbval);
  498. wmb();
  499. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  500. WRT_REG_DWORD((unsigned long __iomem *)
  501. ha->nxdb_wr_ptr, dbval);
  502. wmb();
  503. }
  504. }
  505. } else if (ha->mqenable) {
  506. /* Set chip new ring index. */
  507. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  508. RD_REG_DWORD(&ioreg->hccr);
  509. } else {
  510. if (IS_FWI2_CAPABLE(ha)) {
  511. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  512. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  513. } else {
  514. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  515. req->ring_index);
  516. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  517. }
  518. }
  519. }
  520. /**
  521. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  522. * Continuation Type 1 IOCBs to allocate.
  523. *
  524. * @dsds: number of data segment decriptors needed
  525. *
  526. * Returns the number of IOCB entries needed to store @dsds.
  527. */
  528. inline uint16_t
  529. qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
  530. {
  531. uint16_t iocbs;
  532. iocbs = 1;
  533. if (dsds > 1) {
  534. iocbs += (dsds - 1) / 5;
  535. if ((dsds - 1) % 5)
  536. iocbs++;
  537. }
  538. return iocbs;
  539. }
  540. /**
  541. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  542. * IOCB types.
  543. *
  544. * @sp: SRB command to process
  545. * @cmd_pkt: Command type 3 IOCB
  546. * @tot_dsds: Total number of segments to transfer
  547. */
  548. inline void
  549. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  550. uint16_t tot_dsds)
  551. {
  552. uint16_t avail_dsds;
  553. uint32_t *cur_dsd;
  554. scsi_qla_host_t *vha;
  555. struct scsi_cmnd *cmd;
  556. struct scatterlist *sg;
  557. int i;
  558. struct req_que *req;
  559. cmd = sp->cmd;
  560. /* Update entry type to indicate Command Type 3 IOCB */
  561. *((uint32_t *)(&cmd_pkt->entry_type)) =
  562. __constant_cpu_to_le32(COMMAND_TYPE_7);
  563. /* No data transfer */
  564. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  565. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  566. return;
  567. }
  568. vha = sp->fcport->vha;
  569. req = vha->req;
  570. /* Set transfer direction */
  571. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  572. cmd_pkt->task_mgmt_flags =
  573. __constant_cpu_to_le16(TMF_WRITE_DATA);
  574. sp->fcport->vha->hw->qla_stats.output_bytes +=
  575. scsi_bufflen(sp->cmd);
  576. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  577. cmd_pkt->task_mgmt_flags =
  578. __constant_cpu_to_le16(TMF_READ_DATA);
  579. sp->fcport->vha->hw->qla_stats.input_bytes +=
  580. scsi_bufflen(sp->cmd);
  581. }
  582. /* One DSD is available in the Command Type 3 IOCB */
  583. avail_dsds = 1;
  584. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  585. /* Load data segments */
  586. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  587. dma_addr_t sle_dma;
  588. cont_a64_entry_t *cont_pkt;
  589. /* Allocate additional continuation packets? */
  590. if (avail_dsds == 0) {
  591. /*
  592. * Five DSDs are available in the Continuation
  593. * Type 1 IOCB.
  594. */
  595. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  596. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  597. avail_dsds = 5;
  598. }
  599. sle_dma = sg_dma_address(sg);
  600. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  601. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  602. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  603. avail_dsds--;
  604. }
  605. }
  606. struct fw_dif_context {
  607. uint32_t ref_tag;
  608. uint16_t app_tag;
  609. uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
  610. uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
  611. };
  612. /*
  613. * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
  614. *
  615. */
  616. static inline void
  617. qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
  618. unsigned int protcnt)
  619. {
  620. struct sd_dif_tuple *spt;
  621. scsi_qla_host_t *vha = shost_priv(cmd->device->host);
  622. unsigned char op = scsi_get_prot_op(cmd);
  623. switch (scsi_get_prot_type(cmd)) {
  624. /* For TYPE 0 protection: no checking */
  625. case SCSI_PROT_DIF_TYPE0:
  626. pkt->ref_tag_mask[0] = 0x00;
  627. pkt->ref_tag_mask[1] = 0x00;
  628. pkt->ref_tag_mask[2] = 0x00;
  629. pkt->ref_tag_mask[3] = 0x00;
  630. break;
  631. /*
  632. * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
  633. * match LBA in CDB + N
  634. */
  635. case SCSI_PROT_DIF_TYPE2:
  636. if (!ql2xenablehba_err_chk)
  637. break;
  638. if (scsi_prot_sg_count(cmd)) {
  639. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  640. scsi_prot_sglist(cmd)[0].offset;
  641. pkt->app_tag = swab32(spt->app_tag);
  642. pkt->app_tag_mask[0] = 0xff;
  643. pkt->app_tag_mask[1] = 0xff;
  644. }
  645. pkt->ref_tag = cpu_to_le32((uint32_t)
  646. (0xffffffff & scsi_get_lba(cmd)));
  647. /* enable ALL bytes of the ref tag */
  648. pkt->ref_tag_mask[0] = 0xff;
  649. pkt->ref_tag_mask[1] = 0xff;
  650. pkt->ref_tag_mask[2] = 0xff;
  651. pkt->ref_tag_mask[3] = 0xff;
  652. break;
  653. /* For Type 3 protection: 16 bit GUARD only */
  654. case SCSI_PROT_DIF_TYPE3:
  655. pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
  656. pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
  657. 0x00;
  658. break;
  659. /*
  660. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  661. * 16 bit app tag.
  662. */
  663. case SCSI_PROT_DIF_TYPE1:
  664. if (!ql2xenablehba_err_chk)
  665. break;
  666. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  667. op == SCSI_PROT_WRITE_PASS)) {
  668. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  669. scsi_prot_sglist(cmd)[0].offset;
  670. ql_dbg(ql_dbg_io, vha, 0x3008,
  671. "LBA from user %p, lba = 0x%x for cmd=%p.\n",
  672. spt, (int)spt->ref_tag, cmd);
  673. pkt->ref_tag = swab32(spt->ref_tag);
  674. pkt->app_tag_mask[0] = 0x0;
  675. pkt->app_tag_mask[1] = 0x0;
  676. } else {
  677. pkt->ref_tag = cpu_to_le32((uint32_t)
  678. (0xffffffff & scsi_get_lba(cmd)));
  679. pkt->app_tag = __constant_cpu_to_le16(0);
  680. pkt->app_tag_mask[0] = 0x0;
  681. pkt->app_tag_mask[1] = 0x0;
  682. }
  683. /* enable ALL bytes of the ref tag */
  684. pkt->ref_tag_mask[0] = 0xff;
  685. pkt->ref_tag_mask[1] = 0xff;
  686. pkt->ref_tag_mask[2] = 0xff;
  687. pkt->ref_tag_mask[3] = 0xff;
  688. break;
  689. }
  690. ql_dbg(ql_dbg_io, vha, 0x3009,
  691. "Setting protection Tags: (BIG) ref tag = 0x%x, app tag = 0x%x, "
  692. "prot SG count %d, cmd lba 0x%x, prot_type=%u cmd=%p.\n",
  693. pkt->ref_tag, pkt->app_tag, protcnt, (int)scsi_get_lba(cmd),
  694. scsi_get_prot_type(cmd), cmd);
  695. }
  696. static int
  697. qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
  698. uint16_t tot_dsds)
  699. {
  700. void *next_dsd;
  701. uint8_t avail_dsds = 0;
  702. uint32_t dsd_list_len;
  703. struct dsd_dma *dsd_ptr;
  704. struct scatterlist *sg;
  705. uint32_t *cur_dsd = dsd;
  706. int i;
  707. uint16_t used_dsds = tot_dsds;
  708. scsi_qla_host_t *vha = shost_priv(sp->cmd->device->host);
  709. uint8_t *cp;
  710. scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
  711. dma_addr_t sle_dma;
  712. /* Allocate additional continuation packets? */
  713. if (avail_dsds == 0) {
  714. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  715. QLA_DSDS_PER_IOCB : used_dsds;
  716. dsd_list_len = (avail_dsds + 1) * 12;
  717. used_dsds -= avail_dsds;
  718. /* allocate tracking DS */
  719. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  720. if (!dsd_ptr)
  721. return 1;
  722. /* allocate new list */
  723. dsd_ptr->dsd_addr = next_dsd =
  724. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  725. &dsd_ptr->dsd_list_dma);
  726. if (!next_dsd) {
  727. /*
  728. * Need to cleanup only this dsd_ptr, rest
  729. * will be done by sp_free_dma()
  730. */
  731. kfree(dsd_ptr);
  732. return 1;
  733. }
  734. list_add_tail(&dsd_ptr->list,
  735. &((struct crc_context *)sp->ctx)->dsd_list);
  736. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  737. /* add new list to cmd iocb or last list */
  738. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  739. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  740. *cur_dsd++ = dsd_list_len;
  741. cur_dsd = (uint32_t *)next_dsd;
  742. }
  743. sle_dma = sg_dma_address(sg);
  744. ql_dbg(ql_dbg_io, vha, 0x300a,
  745. "sg entry %d - addr=0x%x 0x%x, " "len=%d for cmd=%p.\n",
  746. cur_dsd, i, LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg),
  747. sp->cmd);
  748. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  749. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  750. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  751. avail_dsds--;
  752. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  753. cp = page_address(sg_page(sg)) + sg->offset;
  754. ql_dbg(ql_dbg_io, vha, 0x300b,
  755. "User data buffer=%p for cmd=%p.\n", cp, sp->cmd);
  756. }
  757. }
  758. /* Null termination */
  759. *cur_dsd++ = 0;
  760. *cur_dsd++ = 0;
  761. *cur_dsd++ = 0;
  762. return 0;
  763. }
  764. static int
  765. qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
  766. uint32_t *dsd,
  767. uint16_t tot_dsds)
  768. {
  769. void *next_dsd;
  770. uint8_t avail_dsds = 0;
  771. uint32_t dsd_list_len;
  772. struct dsd_dma *dsd_ptr;
  773. struct scatterlist *sg;
  774. int i;
  775. struct scsi_cmnd *cmd;
  776. uint32_t *cur_dsd = dsd;
  777. uint16_t used_dsds = tot_dsds;
  778. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  779. uint8_t *cp;
  780. cmd = sp->cmd;
  781. scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
  782. dma_addr_t sle_dma;
  783. /* Allocate additional continuation packets? */
  784. if (avail_dsds == 0) {
  785. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  786. QLA_DSDS_PER_IOCB : used_dsds;
  787. dsd_list_len = (avail_dsds + 1) * 12;
  788. used_dsds -= avail_dsds;
  789. /* allocate tracking DS */
  790. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  791. if (!dsd_ptr)
  792. return 1;
  793. /* allocate new list */
  794. dsd_ptr->dsd_addr = next_dsd =
  795. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  796. &dsd_ptr->dsd_list_dma);
  797. if (!next_dsd) {
  798. /*
  799. * Need to cleanup only this dsd_ptr, rest
  800. * will be done by sp_free_dma()
  801. */
  802. kfree(dsd_ptr);
  803. return 1;
  804. }
  805. list_add_tail(&dsd_ptr->list,
  806. &((struct crc_context *)sp->ctx)->dsd_list);
  807. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  808. /* add new list to cmd iocb or last list */
  809. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  810. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  811. *cur_dsd++ = dsd_list_len;
  812. cur_dsd = (uint32_t *)next_dsd;
  813. }
  814. sle_dma = sg_dma_address(sg);
  815. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  816. ql_dbg(ql_dbg_io, vha, 0x3027,
  817. "%s(): %p, sg_entry %d - "
  818. "addr=0x%x0x%x, len=%d.\n",
  819. __func__, cur_dsd, i,
  820. LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg));
  821. }
  822. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  823. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  824. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  825. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  826. cp = page_address(sg_page(sg)) + sg->offset;
  827. ql_dbg(ql_dbg_io, vha, 0x3028,
  828. "%s(): Protection Data buffer = %p.\n", __func__,
  829. cp);
  830. }
  831. avail_dsds--;
  832. }
  833. /* Null termination */
  834. *cur_dsd++ = 0;
  835. *cur_dsd++ = 0;
  836. *cur_dsd++ = 0;
  837. return 0;
  838. }
  839. /**
  840. * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
  841. * Type 6 IOCB types.
  842. *
  843. * @sp: SRB command to process
  844. * @cmd_pkt: Command type 3 IOCB
  845. * @tot_dsds: Total number of segments to transfer
  846. */
  847. static inline int
  848. qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
  849. uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
  850. {
  851. uint32_t *cur_dsd, *fcp_dl;
  852. scsi_qla_host_t *vha;
  853. struct scsi_cmnd *cmd;
  854. struct scatterlist *cur_seg;
  855. int sgc;
  856. uint32_t total_bytes;
  857. uint32_t data_bytes;
  858. uint32_t dif_bytes;
  859. uint8_t bundling = 1;
  860. uint16_t blk_size;
  861. uint8_t *clr_ptr;
  862. struct crc_context *crc_ctx_pkt = NULL;
  863. struct qla_hw_data *ha;
  864. uint8_t additional_fcpcdb_len;
  865. uint16_t fcp_cmnd_len;
  866. struct fcp_cmnd *fcp_cmnd;
  867. dma_addr_t crc_ctx_dma;
  868. char tag[2];
  869. cmd = sp->cmd;
  870. sgc = 0;
  871. /* Update entry type to indicate Command Type CRC_2 IOCB */
  872. *((uint32_t *)(&cmd_pkt->entry_type)) =
  873. __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
  874. vha = sp->fcport->vha;
  875. ha = vha->hw;
  876. /* No data transfer */
  877. data_bytes = scsi_bufflen(cmd);
  878. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  879. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  880. return QLA_SUCCESS;
  881. }
  882. cmd_pkt->vp_index = sp->fcport->vp_idx;
  883. /* Set transfer direction */
  884. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  885. cmd_pkt->control_flags =
  886. __constant_cpu_to_le16(CF_WRITE_DATA);
  887. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  888. cmd_pkt->control_flags =
  889. __constant_cpu_to_le16(CF_READ_DATA);
  890. }
  891. tot_prot_dsds = scsi_prot_sg_count(cmd);
  892. if (!tot_prot_dsds)
  893. bundling = 0;
  894. /* Allocate CRC context from global pool */
  895. crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
  896. GFP_ATOMIC, &crc_ctx_dma);
  897. if (!crc_ctx_pkt)
  898. goto crc_queuing_error;
  899. /* Zero out CTX area. */
  900. clr_ptr = (uint8_t *)crc_ctx_pkt;
  901. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  902. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  903. sp->flags |= SRB_CRC_CTX_DMA_VALID;
  904. /* Set handle */
  905. crc_ctx_pkt->handle = cmd_pkt->handle;
  906. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  907. qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
  908. &crc_ctx_pkt->ref_tag, tot_prot_dsds);
  909. cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  910. cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  911. cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  912. /* Determine SCSI command length -- align to 4 byte boundary */
  913. if (cmd->cmd_len > 16) {
  914. additional_fcpcdb_len = cmd->cmd_len - 16;
  915. if ((cmd->cmd_len % 4) != 0) {
  916. /* SCSI cmd > 16 bytes must be multiple of 4 */
  917. goto crc_queuing_error;
  918. }
  919. fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  920. } else {
  921. additional_fcpcdb_len = 0;
  922. fcp_cmnd_len = 12 + 16 + 4;
  923. }
  924. fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
  925. fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
  926. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  927. fcp_cmnd->additional_cdb_len |= 1;
  928. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  929. fcp_cmnd->additional_cdb_len |= 2;
  930. int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
  931. host_to_fcp_swap((uint8_t *)&fcp_cmnd->lun, sizeof(fcp_cmnd->lun));
  932. memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  933. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
  934. cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
  935. LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  936. cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
  937. MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  938. fcp_cmnd->task_management = 0;
  939. /*
  940. * Update tagged queuing modifier if using command tag queuing
  941. */
  942. if (scsi_populate_tag_msg(cmd, tag)) {
  943. switch (tag[0]) {
  944. case HEAD_OF_QUEUE_TAG:
  945. fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
  946. break;
  947. case ORDERED_QUEUE_TAG:
  948. fcp_cmnd->task_attribute = TSK_ORDERED;
  949. break;
  950. default:
  951. fcp_cmnd->task_attribute = 0;
  952. break;
  953. }
  954. } else {
  955. fcp_cmnd->task_attribute = 0;
  956. }
  957. cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
  958. /* Compute dif len and adjust data len to incude protection */
  959. total_bytes = data_bytes;
  960. dif_bytes = 0;
  961. blk_size = cmd->device->sector_size;
  962. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  963. dif_bytes = (data_bytes / blk_size) * 8;
  964. total_bytes += dif_bytes;
  965. }
  966. if (!ql2xenablehba_err_chk)
  967. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  968. if (!bundling) {
  969. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  970. } else {
  971. /*
  972. * Configure Bundling if we need to fetch interlaving
  973. * protection PCI accesses
  974. */
  975. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  976. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  977. crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
  978. tot_prot_dsds);
  979. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  980. }
  981. /* Finish the common fields of CRC pkt */
  982. crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
  983. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  984. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  985. crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
  986. /* Fibre channel byte count */
  987. cmd_pkt->byte_count = cpu_to_le32(total_bytes);
  988. fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
  989. additional_fcpcdb_len);
  990. *fcp_dl = htonl(total_bytes);
  991. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  992. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  993. return QLA_SUCCESS;
  994. }
  995. /* Walks data segments */
  996. cmd_pkt->control_flags |=
  997. __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
  998. if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
  999. (tot_dsds - tot_prot_dsds)))
  1000. goto crc_queuing_error;
  1001. if (bundling && tot_prot_dsds) {
  1002. /* Walks dif segments */
  1003. cur_seg = scsi_prot_sglist(cmd);
  1004. cmd_pkt->control_flags |=
  1005. __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  1006. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  1007. if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
  1008. tot_prot_dsds))
  1009. goto crc_queuing_error;
  1010. }
  1011. return QLA_SUCCESS;
  1012. crc_queuing_error:
  1013. /* Cleanup will be performed by the caller */
  1014. return QLA_FUNCTION_FAILED;
  1015. }
  1016. /**
  1017. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  1018. * @sp: command to send to the ISP
  1019. *
  1020. * Returns non-zero if a failure occurred, else zero.
  1021. */
  1022. int
  1023. qla24xx_start_scsi(srb_t *sp)
  1024. {
  1025. int ret, nseg;
  1026. unsigned long flags;
  1027. uint32_t *clr_ptr;
  1028. uint32_t index;
  1029. uint32_t handle;
  1030. struct cmd_type_7 *cmd_pkt;
  1031. uint16_t cnt;
  1032. uint16_t req_cnt;
  1033. uint16_t tot_dsds;
  1034. struct req_que *req = NULL;
  1035. struct rsp_que *rsp = NULL;
  1036. struct scsi_cmnd *cmd = sp->cmd;
  1037. struct scsi_qla_host *vha = sp->fcport->vha;
  1038. struct qla_hw_data *ha = vha->hw;
  1039. char tag[2];
  1040. /* Setup device pointers. */
  1041. ret = 0;
  1042. qla25xx_set_que(sp, &rsp);
  1043. req = vha->req;
  1044. /* So we know we haven't pci_map'ed anything yet */
  1045. tot_dsds = 0;
  1046. /* Send marker if required */
  1047. if (vha->marker_needed != 0) {
  1048. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1049. QLA_SUCCESS)
  1050. return QLA_FUNCTION_FAILED;
  1051. vha->marker_needed = 0;
  1052. }
  1053. /* Acquire ring specific lock */
  1054. spin_lock_irqsave(&ha->hardware_lock, flags);
  1055. /* Check for room in outstanding command list. */
  1056. handle = req->current_outstanding_cmd;
  1057. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1058. handle++;
  1059. if (handle == MAX_OUTSTANDING_COMMANDS)
  1060. handle = 1;
  1061. if (!req->outstanding_cmds[handle])
  1062. break;
  1063. }
  1064. if (index == MAX_OUTSTANDING_COMMANDS) {
  1065. goto queuing_error;
  1066. }
  1067. /* Map the sg table so we have an accurate count of sg entries needed */
  1068. if (scsi_sg_count(cmd)) {
  1069. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1070. scsi_sg_count(cmd), cmd->sc_data_direction);
  1071. if (unlikely(!nseg))
  1072. goto queuing_error;
  1073. } else
  1074. nseg = 0;
  1075. tot_dsds = nseg;
  1076. req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
  1077. if (req->cnt < (req_cnt + 2)) {
  1078. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1079. if (req->ring_index < cnt)
  1080. req->cnt = cnt - req->ring_index;
  1081. else
  1082. req->cnt = req->length -
  1083. (req->ring_index - cnt);
  1084. }
  1085. if (req->cnt < (req_cnt + 2))
  1086. goto queuing_error;
  1087. /* Build command packet. */
  1088. req->current_outstanding_cmd = handle;
  1089. req->outstanding_cmds[handle] = sp;
  1090. sp->handle = handle;
  1091. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1092. req->cnt -= req_cnt;
  1093. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  1094. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1095. /* Zero out remaining portion of packet. */
  1096. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1097. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1098. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1099. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1100. /* Set NPORT-ID and LUN number*/
  1101. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1102. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1103. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1104. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1105. cmd_pkt->vp_index = sp->fcport->vp_idx;
  1106. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1107. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1108. /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1109. if (scsi_populate_tag_msg(cmd, tag)) {
  1110. switch (tag[0]) {
  1111. case HEAD_OF_QUEUE_TAG:
  1112. cmd_pkt->task = TSK_HEAD_OF_QUEUE;
  1113. break;
  1114. case ORDERED_QUEUE_TAG:
  1115. cmd_pkt->task = TSK_ORDERED;
  1116. break;
  1117. }
  1118. }
  1119. /* Load SCSI command packet. */
  1120. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  1121. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  1122. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  1123. /* Build IOCB segments */
  1124. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  1125. /* Set total data segment count. */
  1126. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1127. /* Specify response queue number where completion should happen */
  1128. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1129. wmb();
  1130. /* Adjust ring index. */
  1131. req->ring_index++;
  1132. if (req->ring_index == req->length) {
  1133. req->ring_index = 0;
  1134. req->ring_ptr = req->ring;
  1135. } else
  1136. req->ring_ptr++;
  1137. sp->flags |= SRB_DMA_VALID;
  1138. /* Set chip new ring index. */
  1139. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1140. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1141. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1142. if (vha->flags.process_response_queue &&
  1143. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1144. qla24xx_process_response_queue(vha, rsp);
  1145. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1146. return QLA_SUCCESS;
  1147. queuing_error:
  1148. if (tot_dsds)
  1149. scsi_dma_unmap(cmd);
  1150. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1151. return QLA_FUNCTION_FAILED;
  1152. }
  1153. /**
  1154. * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
  1155. * @sp: command to send to the ISP
  1156. *
  1157. * Returns non-zero if a failure occurred, else zero.
  1158. */
  1159. int
  1160. qla24xx_dif_start_scsi(srb_t *sp)
  1161. {
  1162. int nseg;
  1163. unsigned long flags;
  1164. uint32_t *clr_ptr;
  1165. uint32_t index;
  1166. uint32_t handle;
  1167. uint16_t cnt;
  1168. uint16_t req_cnt = 0;
  1169. uint16_t tot_dsds;
  1170. uint16_t tot_prot_dsds;
  1171. uint16_t fw_prot_opts = 0;
  1172. struct req_que *req = NULL;
  1173. struct rsp_que *rsp = NULL;
  1174. struct scsi_cmnd *cmd = sp->cmd;
  1175. struct scsi_qla_host *vha = sp->fcport->vha;
  1176. struct qla_hw_data *ha = vha->hw;
  1177. struct cmd_type_crc_2 *cmd_pkt;
  1178. uint32_t status = 0;
  1179. #define QDSS_GOT_Q_SPACE BIT_0
  1180. /* Only process protection or >16 cdb in this routine */
  1181. if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  1182. if (cmd->cmd_len <= 16)
  1183. return qla24xx_start_scsi(sp);
  1184. }
  1185. /* Setup device pointers. */
  1186. qla25xx_set_que(sp, &rsp);
  1187. req = vha->req;
  1188. /* So we know we haven't pci_map'ed anything yet */
  1189. tot_dsds = 0;
  1190. /* Send marker if required */
  1191. if (vha->marker_needed != 0) {
  1192. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1193. QLA_SUCCESS)
  1194. return QLA_FUNCTION_FAILED;
  1195. vha->marker_needed = 0;
  1196. }
  1197. /* Acquire ring specific lock */
  1198. spin_lock_irqsave(&ha->hardware_lock, flags);
  1199. /* Check for room in outstanding command list. */
  1200. handle = req->current_outstanding_cmd;
  1201. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1202. handle++;
  1203. if (handle == MAX_OUTSTANDING_COMMANDS)
  1204. handle = 1;
  1205. if (!req->outstanding_cmds[handle])
  1206. break;
  1207. }
  1208. if (index == MAX_OUTSTANDING_COMMANDS)
  1209. goto queuing_error;
  1210. /* Compute number of required data segments */
  1211. /* Map the sg table so we have an accurate count of sg entries needed */
  1212. if (scsi_sg_count(cmd)) {
  1213. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1214. scsi_sg_count(cmd), cmd->sc_data_direction);
  1215. if (unlikely(!nseg))
  1216. goto queuing_error;
  1217. else
  1218. sp->flags |= SRB_DMA_VALID;
  1219. } else
  1220. nseg = 0;
  1221. /* number of required data segments */
  1222. tot_dsds = nseg;
  1223. /* Compute number of required protection segments */
  1224. if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  1225. nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
  1226. scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  1227. if (unlikely(!nseg))
  1228. goto queuing_error;
  1229. else
  1230. sp->flags |= SRB_CRC_PROT_DMA_VALID;
  1231. } else {
  1232. nseg = 0;
  1233. }
  1234. req_cnt = 1;
  1235. /* Total Data and protection sg segment(s) */
  1236. tot_prot_dsds = nseg;
  1237. tot_dsds += nseg;
  1238. if (req->cnt < (req_cnt + 2)) {
  1239. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1240. if (req->ring_index < cnt)
  1241. req->cnt = cnt - req->ring_index;
  1242. else
  1243. req->cnt = req->length -
  1244. (req->ring_index - cnt);
  1245. }
  1246. if (req->cnt < (req_cnt + 2))
  1247. goto queuing_error;
  1248. status |= QDSS_GOT_Q_SPACE;
  1249. /* Build header part of command packet (excluding the OPCODE). */
  1250. req->current_outstanding_cmd = handle;
  1251. req->outstanding_cmds[handle] = sp;
  1252. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1253. req->cnt -= req_cnt;
  1254. /* Fill-in common area */
  1255. cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
  1256. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1257. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1258. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1259. /* Set NPORT-ID and LUN number*/
  1260. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1261. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1262. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1263. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1264. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1265. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1266. /* Total Data and protection segment(s) */
  1267. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1268. /* Build IOCB segments and adjust for data protection segments */
  1269. if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
  1270. req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  1271. QLA_SUCCESS)
  1272. goto queuing_error;
  1273. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1274. /* Specify response queue number where completion should happen */
  1275. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1276. cmd_pkt->timeout = __constant_cpu_to_le16(0);
  1277. wmb();
  1278. /* Adjust ring index. */
  1279. req->ring_index++;
  1280. if (req->ring_index == req->length) {
  1281. req->ring_index = 0;
  1282. req->ring_ptr = req->ring;
  1283. } else
  1284. req->ring_ptr++;
  1285. /* Set chip new ring index. */
  1286. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1287. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1288. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1289. if (vha->flags.process_response_queue &&
  1290. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1291. qla24xx_process_response_queue(vha, rsp);
  1292. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1293. return QLA_SUCCESS;
  1294. queuing_error:
  1295. if (status & QDSS_GOT_Q_SPACE) {
  1296. req->outstanding_cmds[handle] = NULL;
  1297. req->cnt += req_cnt;
  1298. }
  1299. /* Cleanup will be performed by the caller (queuecommand) */
  1300. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1301. return QLA_FUNCTION_FAILED;
  1302. }
  1303. static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
  1304. {
  1305. struct scsi_cmnd *cmd = sp->cmd;
  1306. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1307. int affinity = cmd->request->cpu;
  1308. if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
  1309. affinity < ha->max_rsp_queues - 1)
  1310. *rsp = ha->rsp_q_map[affinity + 1];
  1311. else
  1312. *rsp = ha->rsp_q_map[0];
  1313. }
  1314. /* Generic Control-SRB manipulation functions. */
  1315. void *
  1316. qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
  1317. {
  1318. struct qla_hw_data *ha = vha->hw;
  1319. struct req_que *req = ha->req_q_map[0];
  1320. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1321. uint32_t index, handle;
  1322. request_t *pkt;
  1323. uint16_t cnt, req_cnt;
  1324. pkt = NULL;
  1325. req_cnt = 1;
  1326. handle = 0;
  1327. if (!sp)
  1328. goto skip_cmd_array;
  1329. /* Check for room in outstanding command list. */
  1330. handle = req->current_outstanding_cmd;
  1331. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1332. handle++;
  1333. if (handle == MAX_OUTSTANDING_COMMANDS)
  1334. handle = 1;
  1335. if (!req->outstanding_cmds[handle])
  1336. break;
  1337. }
  1338. if (index == MAX_OUTSTANDING_COMMANDS) {
  1339. ql_log(ql_log_warn, vha, 0x700b,
  1340. "No room on oustanding cmd array.\n");
  1341. goto queuing_error;
  1342. }
  1343. /* Prep command array. */
  1344. req->current_outstanding_cmd = handle;
  1345. req->outstanding_cmds[handle] = sp;
  1346. sp->handle = handle;
  1347. skip_cmd_array:
  1348. /* Check for room on request queue. */
  1349. if (req->cnt < req_cnt) {
  1350. if (ha->mqenable)
  1351. cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
  1352. else if (IS_QLA82XX(ha))
  1353. cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
  1354. else if (IS_FWI2_CAPABLE(ha))
  1355. cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
  1356. else
  1357. cnt = qla2x00_debounce_register(
  1358. ISP_REQ_Q_OUT(ha, &reg->isp));
  1359. if (req->ring_index < cnt)
  1360. req->cnt = cnt - req->ring_index;
  1361. else
  1362. req->cnt = req->length -
  1363. (req->ring_index - cnt);
  1364. }
  1365. if (req->cnt < req_cnt)
  1366. goto queuing_error;
  1367. /* Prep packet */
  1368. req->cnt -= req_cnt;
  1369. pkt = req->ring_ptr;
  1370. memset(pkt, 0, REQUEST_ENTRY_SIZE);
  1371. pkt->entry_count = req_cnt;
  1372. pkt->handle = handle;
  1373. queuing_error:
  1374. return pkt;
  1375. }
  1376. static void
  1377. qla2x00_start_iocbs(srb_t *sp)
  1378. {
  1379. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1380. struct req_que *req = ha->req_q_map[0];
  1381. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1382. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  1383. if (IS_QLA82XX(ha)) {
  1384. qla82xx_start_iocbs(sp);
  1385. } else {
  1386. /* Adjust ring index. */
  1387. req->ring_index++;
  1388. if (req->ring_index == req->length) {
  1389. req->ring_index = 0;
  1390. req->ring_ptr = req->ring;
  1391. } else
  1392. req->ring_ptr++;
  1393. /* Set chip new ring index. */
  1394. if (ha->mqenable) {
  1395. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  1396. RD_REG_DWORD(&ioreg->hccr);
  1397. } else if (IS_QLA82XX(ha)) {
  1398. qla82xx_start_iocbs(sp);
  1399. } else if (IS_FWI2_CAPABLE(ha)) {
  1400. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  1401. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  1402. } else {
  1403. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  1404. req->ring_index);
  1405. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  1406. }
  1407. }
  1408. }
  1409. static void
  1410. qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1411. {
  1412. struct srb_ctx *ctx = sp->ctx;
  1413. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1414. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1415. logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  1416. if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
  1417. logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  1418. if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
  1419. logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  1420. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1421. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1422. logio->port_id[1] = sp->fcport->d_id.b.area;
  1423. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1424. logio->vp_index = sp->fcport->vp_idx;
  1425. }
  1426. static void
  1427. qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
  1428. {
  1429. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1430. struct srb_ctx *ctx = sp->ctx;
  1431. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1432. uint16_t opts;
  1433. mbx->entry_type = MBX_IOCB_TYPE;
  1434. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1435. mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
  1436. opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
  1437. opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
  1438. if (HAS_EXTENDED_IDS(ha)) {
  1439. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1440. mbx->mb10 = cpu_to_le16(opts);
  1441. } else {
  1442. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
  1443. }
  1444. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1445. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1446. sp->fcport->d_id.b.al_pa);
  1447. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1448. }
  1449. static void
  1450. qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1451. {
  1452. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1453. logio->control_flags =
  1454. cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
  1455. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1456. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1457. logio->port_id[1] = sp->fcport->d_id.b.area;
  1458. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1459. logio->vp_index = sp->fcport->vp_idx;
  1460. }
  1461. static void
  1462. qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
  1463. {
  1464. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1465. mbx->entry_type = MBX_IOCB_TYPE;
  1466. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1467. mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
  1468. mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
  1469. cpu_to_le16(sp->fcport->loop_id):
  1470. cpu_to_le16(sp->fcport->loop_id << 8);
  1471. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1472. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1473. sp->fcport->d_id.b.al_pa);
  1474. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1475. /* Implicit: mbx->mbx10 = 0. */
  1476. }
  1477. static void
  1478. qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1479. {
  1480. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1481. logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
  1482. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1483. logio->vp_index = sp->fcport->vp_idx;
  1484. }
  1485. static void
  1486. qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
  1487. {
  1488. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1489. mbx->entry_type = MBX_IOCB_TYPE;
  1490. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1491. mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
  1492. if (HAS_EXTENDED_IDS(ha)) {
  1493. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1494. mbx->mb10 = cpu_to_le16(BIT_0);
  1495. } else {
  1496. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
  1497. }
  1498. mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
  1499. mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
  1500. mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
  1501. mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
  1502. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1503. }
  1504. static void
  1505. qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
  1506. {
  1507. uint32_t flags;
  1508. unsigned int lun;
  1509. struct fc_port *fcport = sp->fcport;
  1510. scsi_qla_host_t *vha = fcport->vha;
  1511. struct qla_hw_data *ha = vha->hw;
  1512. struct srb_ctx *ctx = sp->ctx;
  1513. struct srb_iocb *iocb = ctx->u.iocb_cmd;
  1514. struct req_que *req = vha->req;
  1515. flags = iocb->u.tmf.flags;
  1516. lun = iocb->u.tmf.lun;
  1517. tsk->entry_type = TSK_MGMT_IOCB_TYPE;
  1518. tsk->entry_count = 1;
  1519. tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
  1520. tsk->nport_handle = cpu_to_le16(fcport->loop_id);
  1521. tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
  1522. tsk->control_flags = cpu_to_le32(flags);
  1523. tsk->port_id[0] = fcport->d_id.b.al_pa;
  1524. tsk->port_id[1] = fcport->d_id.b.area;
  1525. tsk->port_id[2] = fcport->d_id.b.domain;
  1526. tsk->vp_index = fcport->vp_idx;
  1527. if (flags == TCF_LUN_RESET) {
  1528. int_to_scsilun(lun, &tsk->lun);
  1529. host_to_fcp_swap((uint8_t *)&tsk->lun,
  1530. sizeof(tsk->lun));
  1531. }
  1532. }
  1533. static void
  1534. qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
  1535. {
  1536. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1537. els_iocb->entry_type = ELS_IOCB_TYPE;
  1538. els_iocb->entry_count = 1;
  1539. els_iocb->sys_define = 0;
  1540. els_iocb->entry_status = 0;
  1541. els_iocb->handle = sp->handle;
  1542. els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1543. els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1544. els_iocb->vp_index = sp->fcport->vp_idx;
  1545. els_iocb->sof_type = EST_SOFI3;
  1546. els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1547. els_iocb->opcode =
  1548. (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
  1549. bsg_job->request->rqst_data.r_els.els_code :
  1550. bsg_job->request->rqst_data.h_els.command_code;
  1551. els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
  1552. els_iocb->port_id[1] = sp->fcport->d_id.b.area;
  1553. els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
  1554. els_iocb->control_flags = 0;
  1555. els_iocb->rx_byte_count =
  1556. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1557. els_iocb->tx_byte_count =
  1558. cpu_to_le32(bsg_job->request_payload.payload_len);
  1559. els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1560. (bsg_job->request_payload.sg_list)));
  1561. els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1562. (bsg_job->request_payload.sg_list)));
  1563. els_iocb->tx_len = cpu_to_le32(sg_dma_len
  1564. (bsg_job->request_payload.sg_list));
  1565. els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1566. (bsg_job->reply_payload.sg_list)));
  1567. els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1568. (bsg_job->reply_payload.sg_list)));
  1569. els_iocb->rx_len = cpu_to_le32(sg_dma_len
  1570. (bsg_job->reply_payload.sg_list));
  1571. }
  1572. static void
  1573. qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
  1574. {
  1575. uint16_t avail_dsds;
  1576. uint32_t *cur_dsd;
  1577. struct scatterlist *sg;
  1578. int index;
  1579. uint16_t tot_dsds;
  1580. scsi_qla_host_t *vha = sp->fcport->vha;
  1581. struct qla_hw_data *ha = vha->hw;
  1582. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1583. int loop_iterartion = 0;
  1584. int cont_iocb_prsnt = 0;
  1585. int entry_count = 1;
  1586. memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
  1587. ct_iocb->entry_type = CT_IOCB_TYPE;
  1588. ct_iocb->entry_status = 0;
  1589. ct_iocb->handle1 = sp->handle;
  1590. SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
  1591. ct_iocb->status = __constant_cpu_to_le16(0);
  1592. ct_iocb->control_flags = __constant_cpu_to_le16(0);
  1593. ct_iocb->timeout = 0;
  1594. ct_iocb->cmd_dsd_count =
  1595. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1596. ct_iocb->total_dsd_count =
  1597. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
  1598. ct_iocb->req_bytecount =
  1599. cpu_to_le32(bsg_job->request_payload.payload_len);
  1600. ct_iocb->rsp_bytecount =
  1601. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1602. ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
  1603. (bsg_job->request_payload.sg_list)));
  1604. ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
  1605. (bsg_job->request_payload.sg_list)));
  1606. ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
  1607. ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
  1608. (bsg_job->reply_payload.sg_list)));
  1609. ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
  1610. (bsg_job->reply_payload.sg_list)));
  1611. ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
  1612. avail_dsds = 1;
  1613. cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
  1614. index = 0;
  1615. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1616. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1617. dma_addr_t sle_dma;
  1618. cont_a64_entry_t *cont_pkt;
  1619. /* Allocate additional continuation packets? */
  1620. if (avail_dsds == 0) {
  1621. /*
  1622. * Five DSDs are available in the Cont.
  1623. * Type 1 IOCB.
  1624. */
  1625. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1626. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1627. avail_dsds = 5;
  1628. cont_iocb_prsnt = 1;
  1629. entry_count++;
  1630. }
  1631. sle_dma = sg_dma_address(sg);
  1632. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1633. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1634. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1635. loop_iterartion++;
  1636. avail_dsds--;
  1637. }
  1638. ct_iocb->entry_count = entry_count;
  1639. }
  1640. static void
  1641. qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
  1642. {
  1643. uint16_t avail_dsds;
  1644. uint32_t *cur_dsd;
  1645. struct scatterlist *sg;
  1646. int index;
  1647. uint16_t tot_dsds;
  1648. scsi_qla_host_t *vha = sp->fcport->vha;
  1649. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1650. int loop_iterartion = 0;
  1651. int cont_iocb_prsnt = 0;
  1652. int entry_count = 1;
  1653. ct_iocb->entry_type = CT_IOCB_TYPE;
  1654. ct_iocb->entry_status = 0;
  1655. ct_iocb->sys_define = 0;
  1656. ct_iocb->handle = sp->handle;
  1657. ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1658. ct_iocb->vp_index = sp->fcport->vp_idx;
  1659. ct_iocb->comp_status = __constant_cpu_to_le16(0);
  1660. ct_iocb->cmd_dsd_count =
  1661. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1662. ct_iocb->timeout = 0;
  1663. ct_iocb->rsp_dsd_count =
  1664. __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1665. ct_iocb->rsp_byte_count =
  1666. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1667. ct_iocb->cmd_byte_count =
  1668. cpu_to_le32(bsg_job->request_payload.payload_len);
  1669. ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
  1670. (bsg_job->request_payload.sg_list)));
  1671. ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
  1672. (bsg_job->request_payload.sg_list)));
  1673. ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
  1674. (bsg_job->request_payload.sg_list));
  1675. avail_dsds = 1;
  1676. cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
  1677. index = 0;
  1678. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1679. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1680. dma_addr_t sle_dma;
  1681. cont_a64_entry_t *cont_pkt;
  1682. /* Allocate additional continuation packets? */
  1683. if (avail_dsds == 0) {
  1684. /*
  1685. * Five DSDs are available in the Cont.
  1686. * Type 1 IOCB.
  1687. */
  1688. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1689. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1690. avail_dsds = 5;
  1691. cont_iocb_prsnt = 1;
  1692. entry_count++;
  1693. }
  1694. sle_dma = sg_dma_address(sg);
  1695. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1696. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1697. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1698. loop_iterartion++;
  1699. avail_dsds--;
  1700. }
  1701. ct_iocb->entry_count = entry_count;
  1702. }
  1703. int
  1704. qla2x00_start_sp(srb_t *sp)
  1705. {
  1706. int rval;
  1707. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1708. void *pkt;
  1709. struct srb_ctx *ctx = sp->ctx;
  1710. unsigned long flags;
  1711. rval = QLA_FUNCTION_FAILED;
  1712. spin_lock_irqsave(&ha->hardware_lock, flags);
  1713. pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
  1714. if (!pkt) {
  1715. ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
  1716. "qla2x00_alloc_iocbs failed.\n");
  1717. goto done;
  1718. }
  1719. rval = QLA_SUCCESS;
  1720. switch (ctx->type) {
  1721. case SRB_LOGIN_CMD:
  1722. IS_FWI2_CAPABLE(ha) ?
  1723. qla24xx_login_iocb(sp, pkt) :
  1724. qla2x00_login_iocb(sp, pkt);
  1725. break;
  1726. case SRB_LOGOUT_CMD:
  1727. IS_FWI2_CAPABLE(ha) ?
  1728. qla24xx_logout_iocb(sp, pkt) :
  1729. qla2x00_logout_iocb(sp, pkt);
  1730. break;
  1731. case SRB_ELS_CMD_RPT:
  1732. case SRB_ELS_CMD_HST:
  1733. qla24xx_els_iocb(sp, pkt);
  1734. break;
  1735. case SRB_CT_CMD:
  1736. IS_FWI2_CAPABLE(ha) ?
  1737. qla24xx_ct_iocb(sp, pkt) :
  1738. qla2x00_ct_iocb(sp, pkt);
  1739. break;
  1740. case SRB_ADISC_CMD:
  1741. IS_FWI2_CAPABLE(ha) ?
  1742. qla24xx_adisc_iocb(sp, pkt) :
  1743. qla2x00_adisc_iocb(sp, pkt);
  1744. break;
  1745. case SRB_TM_CMD:
  1746. qla24xx_tm_iocb(sp, pkt);
  1747. break;
  1748. default:
  1749. break;
  1750. }
  1751. wmb();
  1752. qla2x00_start_iocbs(sp);
  1753. done:
  1754. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1755. return rval;
  1756. }