fc_fcp.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/err.h>
  28. #include <linux/crc32.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/fc/fc_fc2.h>
  35. #include <scsi/libfc.h>
  36. #include <scsi/fc_encode.h>
  37. #include "fc_libfc.h"
  38. struct kmem_cache *scsi_pkt_cachep;
  39. /* SRB state definitions */
  40. #define FC_SRB_FREE 0 /* cmd is free */
  41. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  42. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  43. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  44. #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
  45. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  46. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  47. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  48. #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
  49. #define FC_SRB_READ (1 << 1)
  50. #define FC_SRB_WRITE (1 << 0)
  51. /*
  52. * The SCp.ptr should be tested and set under the host lock. NULL indicates
  53. * that the command has been retruned to the scsi layer.
  54. */
  55. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  56. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  57. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  58. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  59. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  60. struct fc_fcp_internal {
  61. mempool_t *scsi_pkt_pool;
  62. struct list_head scsi_pkt_queue;
  63. u8 throttled;
  64. };
  65. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  66. /*
  67. * function prototypes
  68. * FC scsi I/O related functions
  69. */
  70. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  71. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  72. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  73. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  74. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  75. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp);
  76. static void fc_timeout_error(struct fc_fcp_pkt *);
  77. static void fc_fcp_timeout(unsigned long data);
  78. static void fc_fcp_rec(struct fc_fcp_pkt *);
  79. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  80. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  81. static void fc_io_compl(struct fc_fcp_pkt *);
  82. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  83. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  84. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  85. /*
  86. * command status codes
  87. */
  88. #define FC_COMPLETE 0
  89. #define FC_CMD_ABORTED 1
  90. #define FC_CMD_RESET 2
  91. #define FC_CMD_PLOGO 3
  92. #define FC_SNS_RCV 4
  93. #define FC_TRANS_ERR 5
  94. #define FC_DATA_OVRRUN 6
  95. #define FC_DATA_UNDRUN 7
  96. #define FC_ERROR 8
  97. #define FC_HRD_ERROR 9
  98. #define FC_CMD_TIME_OUT 10
  99. /*
  100. * Error recovery timeout values.
  101. */
  102. #define FC_SCSI_ER_TIMEOUT (10 * HZ)
  103. #define FC_SCSI_TM_TOV (10 * HZ)
  104. #define FC_SCSI_REC_TOV (2 * HZ)
  105. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  106. #define FC_MAX_ERROR_CNT 5
  107. #define FC_MAX_RECOV_RETRY 3
  108. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  109. /**
  110. * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
  111. * @lp: fc lport struct
  112. * @gfp: gfp flags for allocation
  113. *
  114. * This is used by upper layer scsi driver.
  115. * Return Value : scsi_pkt structure or null on allocation failure.
  116. * Context : call from process context. no locking required.
  117. */
  118. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp)
  119. {
  120. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  121. struct fc_fcp_pkt *fsp;
  122. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  123. if (fsp) {
  124. memset(fsp, 0, sizeof(*fsp));
  125. fsp->lp = lp;
  126. atomic_set(&fsp->ref_cnt, 1);
  127. init_timer(&fsp->timer);
  128. INIT_LIST_HEAD(&fsp->list);
  129. spin_lock_init(&fsp->scsi_pkt_lock);
  130. }
  131. return fsp;
  132. }
  133. /**
  134. * fc_fcp_pkt_release() - release hold on scsi_pkt packet
  135. * @fsp: fcp packet struct
  136. *
  137. * This is used by upper layer scsi driver.
  138. * Context : call from process and interrupt context.
  139. * no locking required
  140. */
  141. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  142. {
  143. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  144. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  145. mempool_free(fsp, si->scsi_pkt_pool);
  146. }
  147. }
  148. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  149. {
  150. atomic_inc(&fsp->ref_cnt);
  151. }
  152. /**
  153. * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
  154. * @seq: exchange sequence
  155. * @fsp: fcp packet struct
  156. *
  157. * Release hold on scsi_pkt packet set to keep scsi_pkt
  158. * till EM layer exch resource is not freed.
  159. * Context : called from from EM layer.
  160. * no locking required
  161. */
  162. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  163. {
  164. fc_fcp_pkt_release(fsp);
  165. }
  166. /**
  167. * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
  168. * @fsp: fcp packet
  169. *
  170. * We should only return error if we return a command to scsi-ml before
  171. * getting a response. This can happen in cases where we send a abort, but
  172. * do not wait for the response and the abort and command can be passing
  173. * each other on the wire/network-layer.
  174. *
  175. * Note: this function locks the packet and gets a reference to allow
  176. * callers to call the completion function while the lock is held and
  177. * not have to worry about the packets refcount.
  178. *
  179. * TODO: Maybe we should just have callers grab/release the lock and
  180. * have a function that they call to verify the fsp and grab a ref if
  181. * needed.
  182. */
  183. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  184. {
  185. spin_lock_bh(&fsp->scsi_pkt_lock);
  186. if (fsp->state & FC_SRB_COMPL) {
  187. spin_unlock_bh(&fsp->scsi_pkt_lock);
  188. return -EPERM;
  189. }
  190. fc_fcp_pkt_hold(fsp);
  191. return 0;
  192. }
  193. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  194. {
  195. spin_unlock_bh(&fsp->scsi_pkt_lock);
  196. fc_fcp_pkt_release(fsp);
  197. }
  198. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  199. {
  200. if (!(fsp->state & FC_SRB_COMPL))
  201. mod_timer(&fsp->timer, jiffies + delay);
  202. }
  203. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  204. {
  205. if (!fsp->seq_ptr)
  206. return -EINVAL;
  207. fsp->state |= FC_SRB_ABORT_PENDING;
  208. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  209. }
  210. /*
  211. * Retry command.
  212. * An abort isn't needed.
  213. */
  214. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  215. {
  216. if (fsp->seq_ptr) {
  217. fsp->lp->tt.exch_done(fsp->seq_ptr);
  218. fsp->seq_ptr = NULL;
  219. }
  220. fsp->state &= ~FC_SRB_ABORT_PENDING;
  221. fsp->io_status = 0;
  222. fsp->status_code = FC_ERROR;
  223. fc_fcp_complete_locked(fsp);
  224. }
  225. /*
  226. * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
  227. * transfer for a read I/O indicated by the fc_fcp_pkt.
  228. * @fsp: ptr to the fc_fcp_pkt
  229. *
  230. * This is called in exch_seq_send() when we have a newly allocated
  231. * exchange with a valid exchange id to setup ddp.
  232. *
  233. * returns: none
  234. */
  235. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  236. {
  237. struct fc_lport *lp;
  238. if (!fsp)
  239. return;
  240. lp = fsp->lp;
  241. if ((fsp->req_flags & FC_SRB_READ) &&
  242. (lp->lro_enabled) && (lp->tt.ddp_setup)) {
  243. if (lp->tt.ddp_setup(lp, xid, scsi_sglist(fsp->cmd),
  244. scsi_sg_count(fsp->cmd)))
  245. fsp->xfer_ddp = xid;
  246. }
  247. }
  248. /*
  249. * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
  250. * DDP related resources for this I/O if it is initialized
  251. * as a ddp transfer
  252. * @fsp: ptr to the fc_fcp_pkt
  253. *
  254. * returns: none
  255. */
  256. static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  257. {
  258. struct fc_lport *lp;
  259. if (!fsp)
  260. return;
  261. if (fsp->xfer_ddp == FC_XID_UNKNOWN)
  262. return;
  263. lp = fsp->lp;
  264. if (lp->tt.ddp_done) {
  265. fsp->xfer_len = lp->tt.ddp_done(lp, fsp->xfer_ddp);
  266. fsp->xfer_ddp = FC_XID_UNKNOWN;
  267. }
  268. }
  269. /*
  270. * Receive SCSI data from target.
  271. * Called after receiving solicited data.
  272. */
  273. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  274. {
  275. struct scsi_cmnd *sc = fsp->cmd;
  276. struct fc_lport *lp = fsp->lp;
  277. struct fcoe_dev_stats *stats;
  278. struct fc_frame_header *fh;
  279. size_t start_offset;
  280. size_t offset;
  281. u32 crc;
  282. u32 copy_len = 0;
  283. size_t len;
  284. void *buf;
  285. struct scatterlist *sg;
  286. u32 nents;
  287. fh = fc_frame_header_get(fp);
  288. offset = ntohl(fh->fh_parm_offset);
  289. start_offset = offset;
  290. len = fr_len(fp) - sizeof(*fh);
  291. buf = fc_frame_payload_get(fp, 0);
  292. /* if this I/O is ddped, update xfer len */
  293. fc_fcp_ddp_done(fsp);
  294. if (offset + len > fsp->data_len) {
  295. /* this should never happen */
  296. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  297. fc_frame_crc_check(fp))
  298. goto crc_err;
  299. FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
  300. "data_len %x\n", len, offset, fsp->data_len);
  301. fc_fcp_retry_cmd(fsp);
  302. return;
  303. }
  304. if (offset != fsp->xfer_len)
  305. fsp->state |= FC_SRB_DISCONTIG;
  306. sg = scsi_sglist(sc);
  307. nents = scsi_sg_count(sc);
  308. if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
  309. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  310. &offset, KM_SOFTIRQ0, NULL);
  311. } else {
  312. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  313. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  314. &offset, KM_SOFTIRQ0, &crc);
  315. buf = fc_frame_payload_get(fp, 0);
  316. if (len % 4)
  317. crc = crc32(crc, buf + len, 4 - (len % 4));
  318. if (~crc != le32_to_cpu(fr_crc(fp))) {
  319. crc_err:
  320. stats = fc_lport_get_stats(lp);
  321. stats->ErrorFrames++;
  322. /* FIXME - per cpu count, not total count! */
  323. if (stats->InvalidCRCCount++ < 5)
  324. printk(KERN_WARNING "libfc: CRC error on data "
  325. "frame for port (%6x)\n",
  326. fc_host_port_id(lp->host));
  327. /*
  328. * Assume the frame is total garbage.
  329. * We may have copied it over the good part
  330. * of the buffer.
  331. * If so, we need to retry the entire operation.
  332. * Otherwise, ignore it.
  333. */
  334. if (fsp->state & FC_SRB_DISCONTIG)
  335. fc_fcp_retry_cmd(fsp);
  336. return;
  337. }
  338. }
  339. if (fsp->xfer_contig_end == start_offset)
  340. fsp->xfer_contig_end += copy_len;
  341. fsp->xfer_len += copy_len;
  342. /*
  343. * In the very rare event that this data arrived after the response
  344. * and completes the transfer, call the completion handler.
  345. */
  346. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  347. fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
  348. fc_fcp_complete_locked(fsp);
  349. }
  350. /**
  351. * fc_fcp_send_data() - Send SCSI data to target.
  352. * @fsp: ptr to fc_fcp_pkt
  353. * @sp: ptr to this sequence
  354. * @offset: starting offset for this data request
  355. * @seq_blen: the burst length for this data request
  356. *
  357. * Called after receiving a Transfer Ready data descriptor.
  358. * if LLD is capable of seq offload then send down seq_blen
  359. * size of data in single frame, otherwise send multiple FC
  360. * frames of max FC frame payload supported by target port.
  361. *
  362. * Returns : 0 for success.
  363. */
  364. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  365. size_t offset, size_t seq_blen)
  366. {
  367. struct fc_exch *ep;
  368. struct scsi_cmnd *sc;
  369. struct scatterlist *sg;
  370. struct fc_frame *fp = NULL;
  371. struct fc_lport *lp = fsp->lp;
  372. size_t remaining;
  373. size_t t_blen;
  374. size_t tlen;
  375. size_t sg_bytes;
  376. size_t frame_offset, fh_parm_offset;
  377. int error;
  378. void *data = NULL;
  379. void *page_addr;
  380. int using_sg = lp->sg_supp;
  381. u32 f_ctl;
  382. WARN_ON(seq_blen <= 0);
  383. if (unlikely(offset + seq_blen > fsp->data_len)) {
  384. /* this should never happen */
  385. FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
  386. "offset %zx\n", seq_blen, offset);
  387. fc_fcp_send_abort(fsp);
  388. return 0;
  389. } else if (offset != fsp->xfer_len) {
  390. /* Out of Order Data Request - no problem, but unexpected. */
  391. FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
  392. "seq_blen %zx offset %zx\n", seq_blen, offset);
  393. }
  394. /*
  395. * if LLD is capable of seq_offload then set transport
  396. * burst length (t_blen) to seq_blen, otherwise set t_blen
  397. * to max FC frame payload previously set in fsp->max_payload.
  398. */
  399. t_blen = fsp->max_payload;
  400. if (lp->seq_offload) {
  401. t_blen = min(seq_blen, (size_t)lp->lso_max);
  402. FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  403. fsp, seq_blen, lp->lso_max, t_blen);
  404. }
  405. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
  406. if (t_blen > 512)
  407. t_blen &= ~(512 - 1); /* round down to block size */
  408. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
  409. sc = fsp->cmd;
  410. remaining = seq_blen;
  411. fh_parm_offset = frame_offset = offset;
  412. tlen = 0;
  413. seq = lp->tt.seq_start_next(seq);
  414. f_ctl = FC_FC_REL_OFF;
  415. WARN_ON(!seq);
  416. sg = scsi_sglist(sc);
  417. while (remaining > 0 && sg) {
  418. if (offset >= sg->length) {
  419. offset -= sg->length;
  420. sg = sg_next(sg);
  421. continue;
  422. }
  423. if (!fp) {
  424. tlen = min(t_blen, remaining);
  425. /*
  426. * TODO. Temporary workaround. fc_seq_send() can't
  427. * handle odd lengths in non-linear skbs.
  428. * This will be the final fragment only.
  429. */
  430. if (tlen % 4)
  431. using_sg = 0;
  432. if (using_sg) {
  433. fp = _fc_frame_alloc(lp, 0);
  434. if (!fp)
  435. return -ENOMEM;
  436. } else {
  437. fp = fc_frame_alloc(lp, tlen);
  438. if (!fp)
  439. return -ENOMEM;
  440. data = (void *)(fr_hdr(fp)) +
  441. sizeof(struct fc_frame_header);
  442. }
  443. fh_parm_offset = frame_offset;
  444. fr_max_payload(fp) = fsp->max_payload;
  445. }
  446. sg_bytes = min(tlen, sg->length - offset);
  447. if (using_sg) {
  448. get_page(sg_page(sg));
  449. skb_fill_page_desc(fp_skb(fp),
  450. skb_shinfo(fp_skb(fp))->nr_frags,
  451. sg_page(sg), sg->offset + offset,
  452. sg_bytes);
  453. fp_skb(fp)->data_len += sg_bytes;
  454. fr_len(fp) += sg_bytes;
  455. fp_skb(fp)->truesize += PAGE_SIZE;
  456. } else {
  457. size_t off = offset + sg->offset;
  458. /*
  459. * The scatterlist item may be bigger than PAGE_SIZE,
  460. * but we must not cross pages inside the kmap.
  461. */
  462. sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
  463. (off & ~PAGE_MASK)));
  464. page_addr = kmap_atomic(sg_page(sg) +
  465. (off >> PAGE_SHIFT),
  466. KM_SOFTIRQ0);
  467. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  468. sg_bytes);
  469. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  470. data += sg_bytes;
  471. }
  472. offset += sg_bytes;
  473. frame_offset += sg_bytes;
  474. tlen -= sg_bytes;
  475. remaining -= sg_bytes;
  476. if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
  477. (tlen))
  478. continue;
  479. /*
  480. * Send sequence with transfer sequence initiative in case
  481. * this is last FCP frame of the sequence.
  482. */
  483. if (remaining == 0)
  484. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  485. ep = fc_seq_exch(seq);
  486. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  487. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  488. /*
  489. * send fragment using for a sequence.
  490. */
  491. error = lp->tt.seq_send(lp, seq, fp);
  492. if (error) {
  493. WARN_ON(1); /* send error should be rare */
  494. fc_fcp_retry_cmd(fsp);
  495. return 0;
  496. }
  497. fp = NULL;
  498. }
  499. fsp->xfer_len += seq_blen; /* premature count? */
  500. return 0;
  501. }
  502. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  503. {
  504. int ba_done = 1;
  505. struct fc_ba_rjt *brp;
  506. struct fc_frame_header *fh;
  507. fh = fc_frame_header_get(fp);
  508. switch (fh->fh_r_ctl) {
  509. case FC_RCTL_BA_ACC:
  510. break;
  511. case FC_RCTL_BA_RJT:
  512. brp = fc_frame_payload_get(fp, sizeof(*brp));
  513. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  514. break;
  515. /* fall thru */
  516. default:
  517. /*
  518. * we will let the command timeout
  519. * and scsi-ml recover in this case,
  520. * therefore cleared the ba_done flag.
  521. */
  522. ba_done = 0;
  523. }
  524. if (ba_done) {
  525. fsp->state |= FC_SRB_ABORTED;
  526. fsp->state &= ~FC_SRB_ABORT_PENDING;
  527. if (fsp->wait_for_comp)
  528. complete(&fsp->tm_done);
  529. else
  530. fc_fcp_complete_locked(fsp);
  531. }
  532. }
  533. /**
  534. * fc_fcp_reduce_can_queue() - drop can_queue
  535. * @lp: lport to drop queueing for
  536. *
  537. * If we are getting memory allocation failures, then we may
  538. * be trying to execute too many commands. We let the running
  539. * commands complete or timeout, then try again with a reduced
  540. * can_queue. Eventually we will hit the point where we run
  541. * on all reserved structs.
  542. */
  543. static void fc_fcp_reduce_can_queue(struct fc_lport *lp)
  544. {
  545. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  546. unsigned long flags;
  547. int can_queue;
  548. spin_lock_irqsave(lp->host->host_lock, flags);
  549. if (si->throttled)
  550. goto done;
  551. si->throttled = 1;
  552. can_queue = lp->host->can_queue;
  553. can_queue >>= 1;
  554. if (!can_queue)
  555. can_queue = 1;
  556. lp->host->can_queue = can_queue;
  557. shost_printk(KERN_ERR, lp->host, "libfc: Could not allocate frame.\n"
  558. "Reducing can_queue to %d.\n", can_queue);
  559. done:
  560. spin_unlock_irqrestore(lp->host->host_lock, flags);
  561. }
  562. /**
  563. * fc_fcp_recv() - Reveive FCP frames
  564. * @seq: The sequence the frame is on
  565. * @fp: The FC frame
  566. * @arg: The related FCP packet
  567. *
  568. * Return : None
  569. * Context : called from Soft IRQ context
  570. * can not called holding list lock
  571. */
  572. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  573. {
  574. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  575. struct fc_lport *lport = fsp->lp;
  576. struct fc_frame_header *fh;
  577. struct fcp_txrdy *dd;
  578. u8 r_ctl;
  579. int rc = 0;
  580. if (IS_ERR(fp))
  581. goto errout;
  582. fh = fc_frame_header_get(fp);
  583. r_ctl = fh->fh_r_ctl;
  584. if (!(lport->state & LPORT_ST_READY))
  585. goto out;
  586. if (fc_fcp_lock_pkt(fsp))
  587. goto out;
  588. fsp->last_pkt_time = jiffies;
  589. if (fh->fh_type == FC_TYPE_BLS) {
  590. fc_fcp_abts_resp(fsp, fp);
  591. goto unlock;
  592. }
  593. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  594. goto unlock;
  595. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  596. /*
  597. * received XFER RDY from the target
  598. * need to send data to the target
  599. */
  600. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  601. dd = fc_frame_payload_get(fp, sizeof(*dd));
  602. WARN_ON(!dd);
  603. rc = fc_fcp_send_data(fsp, seq,
  604. (size_t) ntohl(dd->ft_data_ro),
  605. (size_t) ntohl(dd->ft_burst_len));
  606. if (!rc)
  607. seq->rec_data = fsp->xfer_len;
  608. else if (rc == -ENOMEM)
  609. fsp->state |= FC_SRB_NOMEM;
  610. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  611. /*
  612. * received a DATA frame
  613. * next we will copy the data to the system buffer
  614. */
  615. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  616. fc_fcp_recv_data(fsp, fp);
  617. seq->rec_data = fsp->xfer_contig_end;
  618. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  619. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  620. fc_fcp_resp(fsp, fp);
  621. } else {
  622. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  623. }
  624. unlock:
  625. fc_fcp_unlock_pkt(fsp);
  626. out:
  627. fc_frame_free(fp);
  628. errout:
  629. if (IS_ERR(fp))
  630. fc_fcp_error(fsp, fp);
  631. else if (rc == -ENOMEM)
  632. fc_fcp_reduce_can_queue(lport);
  633. }
  634. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  635. {
  636. struct fc_frame_header *fh;
  637. struct fcp_resp *fc_rp;
  638. struct fcp_resp_ext *rp_ex;
  639. struct fcp_resp_rsp_info *fc_rp_info;
  640. u32 plen;
  641. u32 expected_len;
  642. u32 respl = 0;
  643. u32 snsl = 0;
  644. u8 flags = 0;
  645. plen = fr_len(fp);
  646. fh = (struct fc_frame_header *)fr_hdr(fp);
  647. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  648. goto len_err;
  649. plen -= sizeof(*fh);
  650. fc_rp = (struct fcp_resp *)(fh + 1);
  651. fsp->cdb_status = fc_rp->fr_status;
  652. flags = fc_rp->fr_flags;
  653. fsp->scsi_comp_flags = flags;
  654. expected_len = fsp->data_len;
  655. /* if ddp, update xfer len */
  656. fc_fcp_ddp_done(fsp);
  657. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  658. rp_ex = (void *)(fc_rp + 1);
  659. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  660. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  661. goto len_err;
  662. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  663. if (flags & FCP_RSP_LEN_VAL) {
  664. respl = ntohl(rp_ex->fr_rsp_len);
  665. if (respl != sizeof(*fc_rp_info))
  666. goto len_err;
  667. if (fsp->wait_for_comp) {
  668. /* Abuse cdb_status for rsp code */
  669. fsp->cdb_status = fc_rp_info->rsp_code;
  670. complete(&fsp->tm_done);
  671. /*
  672. * tmfs will not have any scsi cmd so
  673. * exit here
  674. */
  675. return;
  676. } else
  677. goto err;
  678. }
  679. if (flags & FCP_SNS_LEN_VAL) {
  680. snsl = ntohl(rp_ex->fr_sns_len);
  681. if (snsl > SCSI_SENSE_BUFFERSIZE)
  682. snsl = SCSI_SENSE_BUFFERSIZE;
  683. memcpy(fsp->cmd->sense_buffer,
  684. (char *)fc_rp_info + respl, snsl);
  685. }
  686. }
  687. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  688. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  689. goto len_err;
  690. if (flags & FCP_RESID_UNDER) {
  691. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  692. /*
  693. * The cmnd->underflow is the minimum number of
  694. * bytes that must be transfered for this
  695. * command. Provided a sense condition is not
  696. * present, make sure the actual amount
  697. * transferred is at least the underflow value
  698. * or fail.
  699. */
  700. if (!(flags & FCP_SNS_LEN_VAL) &&
  701. (fc_rp->fr_status == 0) &&
  702. (scsi_bufflen(fsp->cmd) -
  703. fsp->scsi_resid) < fsp->cmd->underflow)
  704. goto err;
  705. expected_len -= fsp->scsi_resid;
  706. } else {
  707. fsp->status_code = FC_ERROR;
  708. }
  709. }
  710. }
  711. fsp->state |= FC_SRB_RCV_STATUS;
  712. /*
  713. * Check for missing or extra data frames.
  714. */
  715. if (unlikely(fsp->xfer_len != expected_len)) {
  716. if (fsp->xfer_len < expected_len) {
  717. /*
  718. * Some data may be queued locally,
  719. * Wait a at least one jiffy to see if it is delivered.
  720. * If this expires without data, we may do SRR.
  721. */
  722. fc_fcp_timer_set(fsp, 2);
  723. return;
  724. }
  725. fsp->status_code = FC_DATA_OVRRUN;
  726. FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
  727. "len %x, data len %x\n",
  728. fsp->rport->port_id,
  729. fsp->xfer_len, expected_len, fsp->data_len);
  730. }
  731. fc_fcp_complete_locked(fsp);
  732. return;
  733. len_err:
  734. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  735. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  736. err:
  737. fsp->status_code = FC_ERROR;
  738. fc_fcp_complete_locked(fsp);
  739. }
  740. /**
  741. * fc_fcp_complete_locked() - complete processing of a fcp packet
  742. * @fsp: fcp packet
  743. *
  744. * This function may sleep if a timer is pending. The packet lock must be
  745. * held, and the host lock must not be held.
  746. */
  747. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  748. {
  749. struct fc_lport *lp = fsp->lp;
  750. struct fc_seq *seq;
  751. struct fc_exch *ep;
  752. u32 f_ctl;
  753. if (fsp->state & FC_SRB_ABORT_PENDING)
  754. return;
  755. if (fsp->state & FC_SRB_ABORTED) {
  756. if (!fsp->status_code)
  757. fsp->status_code = FC_CMD_ABORTED;
  758. } else {
  759. /*
  760. * Test for transport underrun, independent of response
  761. * underrun status.
  762. */
  763. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  764. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  765. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  766. fsp->status_code = FC_DATA_UNDRUN;
  767. fsp->io_status = 0;
  768. }
  769. }
  770. seq = fsp->seq_ptr;
  771. if (seq) {
  772. fsp->seq_ptr = NULL;
  773. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  774. struct fc_frame *conf_frame;
  775. struct fc_seq *csp;
  776. csp = lp->tt.seq_start_next(seq);
  777. conf_frame = fc_frame_alloc(fsp->lp, 0);
  778. if (conf_frame) {
  779. f_ctl = FC_FC_SEQ_INIT;
  780. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  781. ep = fc_seq_exch(seq);
  782. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  783. ep->did, ep->sid,
  784. FC_TYPE_FCP, f_ctl, 0);
  785. lp->tt.seq_send(lp, csp, conf_frame);
  786. }
  787. }
  788. lp->tt.exch_done(seq);
  789. }
  790. fc_io_compl(fsp);
  791. }
  792. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  793. {
  794. struct fc_lport *lp = fsp->lp;
  795. if (fsp->seq_ptr) {
  796. lp->tt.exch_done(fsp->seq_ptr);
  797. fsp->seq_ptr = NULL;
  798. }
  799. fsp->status_code = error;
  800. }
  801. /**
  802. * fc_fcp_cleanup_each_cmd() - Cleanup active commads
  803. * @lp: logical port
  804. * @id: target id
  805. * @lun: lun
  806. * @error: fsp status code
  807. *
  808. * If lun or id is -1, they are ignored.
  809. */
  810. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lp, unsigned int id,
  811. unsigned int lun, int error)
  812. {
  813. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  814. struct fc_fcp_pkt *fsp;
  815. struct scsi_cmnd *sc_cmd;
  816. unsigned long flags;
  817. spin_lock_irqsave(lp->host->host_lock, flags);
  818. restart:
  819. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  820. sc_cmd = fsp->cmd;
  821. if (id != -1 && scmd_id(sc_cmd) != id)
  822. continue;
  823. if (lun != -1 && sc_cmd->device->lun != lun)
  824. continue;
  825. fc_fcp_pkt_hold(fsp);
  826. spin_unlock_irqrestore(lp->host->host_lock, flags);
  827. if (!fc_fcp_lock_pkt(fsp)) {
  828. fc_fcp_cleanup_cmd(fsp, error);
  829. fc_io_compl(fsp);
  830. fc_fcp_unlock_pkt(fsp);
  831. }
  832. fc_fcp_pkt_release(fsp);
  833. spin_lock_irqsave(lp->host->host_lock, flags);
  834. /*
  835. * while we dropped the lock multiple pkts could
  836. * have been released, so we have to start over.
  837. */
  838. goto restart;
  839. }
  840. spin_unlock_irqrestore(lp->host->host_lock, flags);
  841. }
  842. static void fc_fcp_abort_io(struct fc_lport *lp)
  843. {
  844. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_HRD_ERROR);
  845. }
  846. /**
  847. * fc_fcp_pkt_send() - send a fcp packet to the lower level.
  848. * @lp: fc lport
  849. * @fsp: fc packet.
  850. *
  851. * This is called by upper layer protocol.
  852. * Return : zero for success and -1 for failure
  853. * Context : called from queuecommand which can be called from process
  854. * or scsi soft irq.
  855. * Locks : called with the host lock and irqs disabled.
  856. */
  857. static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
  858. {
  859. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  860. int rc;
  861. fsp->cmd->SCp.ptr = (char *)fsp;
  862. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  863. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  864. int_to_scsilun(fsp->cmd->device->lun,
  865. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  866. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  867. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  868. spin_unlock_irq(lp->host->host_lock);
  869. rc = lp->tt.fcp_cmd_send(lp, fsp, fc_fcp_recv);
  870. spin_lock_irq(lp->host->host_lock);
  871. if (rc)
  872. list_del(&fsp->list);
  873. return rc;
  874. }
  875. static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  876. void (*resp)(struct fc_seq *,
  877. struct fc_frame *fp,
  878. void *arg))
  879. {
  880. struct fc_frame *fp;
  881. struct fc_seq *seq;
  882. struct fc_rport *rport;
  883. struct fc_rport_libfc_priv *rp;
  884. const size_t len = sizeof(fsp->cdb_cmd);
  885. int rc = 0;
  886. if (fc_fcp_lock_pkt(fsp))
  887. return 0;
  888. fp = fc_frame_alloc(lp, sizeof(fsp->cdb_cmd));
  889. if (!fp) {
  890. rc = -1;
  891. goto unlock;
  892. }
  893. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  894. fr_fsp(fp) = fsp;
  895. rport = fsp->rport;
  896. fsp->max_payload = rport->maxframe_size;
  897. rp = rport->dd_data;
  898. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  899. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  900. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  901. seq = lp->tt.exch_seq_send(lp, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
  902. if (!seq) {
  903. rc = -1;
  904. goto unlock;
  905. }
  906. fsp->last_pkt_time = jiffies;
  907. fsp->seq_ptr = seq;
  908. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  909. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  910. fc_fcp_timer_set(fsp,
  911. (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
  912. FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
  913. unlock:
  914. fc_fcp_unlock_pkt(fsp);
  915. return rc;
  916. }
  917. /*
  918. * transport error handler
  919. */
  920. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  921. {
  922. int error = PTR_ERR(fp);
  923. if (fc_fcp_lock_pkt(fsp))
  924. return;
  925. if (error == -FC_EX_CLOSED) {
  926. fc_fcp_retry_cmd(fsp);
  927. goto unlock;
  928. }
  929. /*
  930. * clear abort pending, because the lower layer
  931. * decided to force completion.
  932. */
  933. fsp->state &= ~FC_SRB_ABORT_PENDING;
  934. fsp->status_code = FC_CMD_PLOGO;
  935. fc_fcp_complete_locked(fsp);
  936. unlock:
  937. fc_fcp_unlock_pkt(fsp);
  938. }
  939. /*
  940. * Scsi abort handler- calls to send an abort
  941. * and then wait for abort completion
  942. */
  943. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  944. {
  945. int rc = FAILED;
  946. if (fc_fcp_send_abort(fsp))
  947. return FAILED;
  948. init_completion(&fsp->tm_done);
  949. fsp->wait_for_comp = 1;
  950. spin_unlock_bh(&fsp->scsi_pkt_lock);
  951. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  952. spin_lock_bh(&fsp->scsi_pkt_lock);
  953. fsp->wait_for_comp = 0;
  954. if (!rc) {
  955. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  956. rc = FAILED;
  957. } else if (fsp->state & FC_SRB_ABORTED) {
  958. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  959. rc = SUCCESS;
  960. fc_fcp_complete_locked(fsp);
  961. }
  962. return rc;
  963. }
  964. /*
  965. * Retry LUN reset after resource allocation failed.
  966. */
  967. static void fc_lun_reset_send(unsigned long data)
  968. {
  969. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  970. struct fc_lport *lp = fsp->lp;
  971. if (lp->tt.fcp_cmd_send(lp, fsp, fc_tm_done)) {
  972. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  973. return;
  974. if (fc_fcp_lock_pkt(fsp))
  975. return;
  976. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  977. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  978. fc_fcp_unlock_pkt(fsp);
  979. }
  980. }
  981. /*
  982. * Scsi device reset handler- send a LUN RESET to the device
  983. * and wait for reset reply
  984. */
  985. static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  986. unsigned int id, unsigned int lun)
  987. {
  988. int rc;
  989. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  990. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  991. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  992. fsp->wait_for_comp = 1;
  993. init_completion(&fsp->tm_done);
  994. fc_lun_reset_send((unsigned long)fsp);
  995. /*
  996. * wait for completion of reset
  997. * after that make sure all commands are terminated
  998. */
  999. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1000. spin_lock_bh(&fsp->scsi_pkt_lock);
  1001. fsp->state |= FC_SRB_COMPL;
  1002. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1003. del_timer_sync(&fsp->timer);
  1004. spin_lock_bh(&fsp->scsi_pkt_lock);
  1005. if (fsp->seq_ptr) {
  1006. lp->tt.exch_done(fsp->seq_ptr);
  1007. fsp->seq_ptr = NULL;
  1008. }
  1009. fsp->wait_for_comp = 0;
  1010. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1011. if (!rc) {
  1012. FC_SCSI_DBG(lp, "lun reset failed\n");
  1013. return FAILED;
  1014. }
  1015. /* cdb_status holds the tmf's rsp code */
  1016. if (fsp->cdb_status != FCP_TMF_CMPL)
  1017. return FAILED;
  1018. FC_SCSI_DBG(lp, "lun reset to lun %u completed\n", lun);
  1019. fc_fcp_cleanup_each_cmd(lp, id, lun, FC_CMD_ABORTED);
  1020. return SUCCESS;
  1021. }
  1022. /*
  1023. * Task Managment response handler
  1024. */
  1025. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1026. {
  1027. struct fc_fcp_pkt *fsp = arg;
  1028. struct fc_frame_header *fh;
  1029. if (IS_ERR(fp)) {
  1030. /*
  1031. * If there is an error just let it timeout or wait
  1032. * for TMF to be aborted if it timedout.
  1033. *
  1034. * scsi-eh will escalate for when either happens.
  1035. */
  1036. return;
  1037. }
  1038. if (fc_fcp_lock_pkt(fsp))
  1039. return;
  1040. /*
  1041. * raced with eh timeout handler.
  1042. */
  1043. if (!fsp->seq_ptr || !fsp->wait_for_comp) {
  1044. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1045. return;
  1046. }
  1047. fh = fc_frame_header_get(fp);
  1048. if (fh->fh_type != FC_TYPE_BLS)
  1049. fc_fcp_resp(fsp, fp);
  1050. fsp->seq_ptr = NULL;
  1051. fsp->lp->tt.exch_done(seq);
  1052. fc_frame_free(fp);
  1053. fc_fcp_unlock_pkt(fsp);
  1054. }
  1055. static void fc_fcp_cleanup(struct fc_lport *lp)
  1056. {
  1057. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_ERROR);
  1058. }
  1059. /*
  1060. * fc_fcp_timeout: called by OS timer function.
  1061. *
  1062. * The timer has been inactivated and must be reactivated if desired
  1063. * using fc_fcp_timer_set().
  1064. *
  1065. * Algorithm:
  1066. *
  1067. * If REC is supported, just issue it, and return. The REC exchange will
  1068. * complete or time out, and recovery can continue at that point.
  1069. *
  1070. * Otherwise, if the response has been received without all the data,
  1071. * it has been ER_TIMEOUT since the response was received.
  1072. *
  1073. * If the response has not been received,
  1074. * we see if data was received recently. If it has been, we continue waiting,
  1075. * otherwise, we abort the command.
  1076. */
  1077. static void fc_fcp_timeout(unsigned long data)
  1078. {
  1079. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1080. struct fc_rport *rport = fsp->rport;
  1081. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1082. if (fc_fcp_lock_pkt(fsp))
  1083. return;
  1084. if (fsp->cdb_cmd.fc_tm_flags)
  1085. goto unlock;
  1086. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1087. if (rp->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1088. fc_fcp_rec(fsp);
  1089. else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
  1090. jiffies))
  1091. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1092. else if (fsp->state & FC_SRB_RCV_STATUS)
  1093. fc_fcp_complete_locked(fsp);
  1094. else
  1095. fc_timeout_error(fsp);
  1096. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1097. unlock:
  1098. fc_fcp_unlock_pkt(fsp);
  1099. }
  1100. /*
  1101. * Send a REC ELS request
  1102. */
  1103. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1104. {
  1105. struct fc_lport *lp;
  1106. struct fc_frame *fp;
  1107. struct fc_rport *rport;
  1108. struct fc_rport_libfc_priv *rp;
  1109. lp = fsp->lp;
  1110. rport = fsp->rport;
  1111. rp = rport->dd_data;
  1112. if (!fsp->seq_ptr || rp->rp_state != RPORT_ST_READY) {
  1113. fsp->status_code = FC_HRD_ERROR;
  1114. fsp->io_status = 0;
  1115. fc_fcp_complete_locked(fsp);
  1116. return;
  1117. }
  1118. fp = fc_frame_alloc(lp, sizeof(struct fc_els_rec));
  1119. if (!fp)
  1120. goto retry;
  1121. fr_seq(fp) = fsp->seq_ptr;
  1122. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1123. fc_host_port_id(rp->local_port->host), FC_TYPE_ELS,
  1124. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1125. if (lp->tt.elsct_send(lp, rport->port_id, fp, ELS_REC, fc_fcp_rec_resp,
  1126. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV))) {
  1127. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1128. return;
  1129. }
  1130. retry:
  1131. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1132. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1133. else
  1134. fc_timeout_error(fsp);
  1135. }
  1136. /*
  1137. * Receive handler for REC ELS frame
  1138. * if it is a reject then let the scsi layer to handle
  1139. * the timeout. if it is a LS_ACC then if the io was not completed
  1140. * then set the timeout and return otherwise complete the exchange
  1141. * and tell the scsi layer to restart the I/O.
  1142. */
  1143. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1144. {
  1145. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1146. struct fc_els_rec_acc *recp;
  1147. struct fc_els_ls_rjt *rjt;
  1148. u32 e_stat;
  1149. u8 opcode;
  1150. u32 offset;
  1151. enum dma_data_direction data_dir;
  1152. enum fc_rctl r_ctl;
  1153. struct fc_rport_libfc_priv *rp;
  1154. if (IS_ERR(fp)) {
  1155. fc_fcp_rec_error(fsp, fp);
  1156. return;
  1157. }
  1158. if (fc_fcp_lock_pkt(fsp))
  1159. goto out;
  1160. fsp->recov_retry = 0;
  1161. opcode = fc_frame_payload_op(fp);
  1162. if (opcode == ELS_LS_RJT) {
  1163. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1164. switch (rjt->er_reason) {
  1165. default:
  1166. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1167. "reason %d expl %d\n",
  1168. fsp->rport->port_id, rjt->er_reason,
  1169. rjt->er_explan);
  1170. /* fall through */
  1171. case ELS_RJT_UNSUP:
  1172. FC_FCP_DBG(fsp, "device does not support REC\n");
  1173. rp = fsp->rport->dd_data;
  1174. /*
  1175. * if we do not spport RECs or got some bogus
  1176. * reason then resetup timer so we check for
  1177. * making progress.
  1178. */
  1179. rp->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1180. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1181. break;
  1182. case ELS_RJT_LOGIC:
  1183. case ELS_RJT_UNAB:
  1184. /*
  1185. * If no data transfer, the command frame got dropped
  1186. * so we just retry. If data was transferred, we
  1187. * lost the response but the target has no record,
  1188. * so we abort and retry.
  1189. */
  1190. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1191. fsp->xfer_len == 0) {
  1192. fc_fcp_retry_cmd(fsp);
  1193. break;
  1194. }
  1195. fc_timeout_error(fsp);
  1196. break;
  1197. }
  1198. } else if (opcode == ELS_LS_ACC) {
  1199. if (fsp->state & FC_SRB_ABORTED)
  1200. goto unlock_out;
  1201. data_dir = fsp->cmd->sc_data_direction;
  1202. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1203. offset = ntohl(recp->reca_fc4value);
  1204. e_stat = ntohl(recp->reca_e_stat);
  1205. if (e_stat & ESB_ST_COMPLETE) {
  1206. /*
  1207. * The exchange is complete.
  1208. *
  1209. * For output, we must've lost the response.
  1210. * For input, all data must've been sent.
  1211. * We lost may have lost the response
  1212. * (and a confirmation was requested) and maybe
  1213. * some data.
  1214. *
  1215. * If all data received, send SRR
  1216. * asking for response. If partial data received,
  1217. * or gaps, SRR requests data at start of gap.
  1218. * Recovery via SRR relies on in-order-delivery.
  1219. */
  1220. if (data_dir == DMA_TO_DEVICE) {
  1221. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1222. } else if (fsp->xfer_contig_end == offset) {
  1223. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1224. } else {
  1225. offset = fsp->xfer_contig_end;
  1226. r_ctl = FC_RCTL_DD_SOL_DATA;
  1227. }
  1228. fc_fcp_srr(fsp, r_ctl, offset);
  1229. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1230. /*
  1231. * The remote port has the initiative, so just
  1232. * keep waiting for it to complete.
  1233. */
  1234. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1235. } else {
  1236. /*
  1237. * The exchange is incomplete, we have seq. initiative.
  1238. * Lost response with requested confirmation,
  1239. * lost confirmation, lost transfer ready or
  1240. * lost write data.
  1241. *
  1242. * For output, if not all data was received, ask
  1243. * for transfer ready to be repeated.
  1244. *
  1245. * If we received or sent all the data, send SRR to
  1246. * request response.
  1247. *
  1248. * If we lost a response, we may have lost some read
  1249. * data as well.
  1250. */
  1251. r_ctl = FC_RCTL_DD_SOL_DATA;
  1252. if (data_dir == DMA_TO_DEVICE) {
  1253. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1254. if (offset < fsp->data_len)
  1255. r_ctl = FC_RCTL_DD_DATA_DESC;
  1256. } else if (offset == fsp->xfer_contig_end) {
  1257. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1258. } else if (fsp->xfer_contig_end < offset) {
  1259. offset = fsp->xfer_contig_end;
  1260. }
  1261. fc_fcp_srr(fsp, r_ctl, offset);
  1262. }
  1263. }
  1264. unlock_out:
  1265. fc_fcp_unlock_pkt(fsp);
  1266. out:
  1267. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1268. fc_frame_free(fp);
  1269. }
  1270. /*
  1271. * Handle error response or timeout for REC exchange.
  1272. */
  1273. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1274. {
  1275. int error = PTR_ERR(fp);
  1276. if (fc_fcp_lock_pkt(fsp))
  1277. goto out;
  1278. switch (error) {
  1279. case -FC_EX_CLOSED:
  1280. fc_fcp_retry_cmd(fsp);
  1281. break;
  1282. default:
  1283. FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
  1284. fsp, fsp->rport->port_id, error);
  1285. fsp->status_code = FC_CMD_PLOGO;
  1286. /* fall through */
  1287. case -FC_EX_TIMEOUT:
  1288. /*
  1289. * Assume REC or LS_ACC was lost.
  1290. * The exchange manager will have aborted REC, so retry.
  1291. */
  1292. FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
  1293. fsp->rport->port_id, error, fsp->recov_retry,
  1294. FC_MAX_RECOV_RETRY);
  1295. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1296. fc_fcp_rec(fsp);
  1297. else
  1298. fc_timeout_error(fsp);
  1299. break;
  1300. }
  1301. fc_fcp_unlock_pkt(fsp);
  1302. out:
  1303. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1304. }
  1305. /*
  1306. * Time out error routine:
  1307. * abort's the I/O close the exchange and
  1308. * send completion notification to scsi layer
  1309. */
  1310. static void fc_timeout_error(struct fc_fcp_pkt *fsp)
  1311. {
  1312. fsp->status_code = FC_CMD_TIME_OUT;
  1313. fsp->cdb_status = 0;
  1314. fsp->io_status = 0;
  1315. /*
  1316. * if this fails then we let the scsi command timer fire and
  1317. * scsi-ml escalate.
  1318. */
  1319. fc_fcp_send_abort(fsp);
  1320. }
  1321. /*
  1322. * Sequence retransmission request.
  1323. * This is called after receiving status but insufficient data, or
  1324. * when expecting status but the request has timed out.
  1325. */
  1326. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1327. {
  1328. struct fc_lport *lp = fsp->lp;
  1329. struct fc_rport *rport;
  1330. struct fc_rport_libfc_priv *rp;
  1331. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1332. struct fc_seq *seq;
  1333. struct fcp_srr *srr;
  1334. struct fc_frame *fp;
  1335. u8 cdb_op;
  1336. rport = fsp->rport;
  1337. rp = rport->dd_data;
  1338. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1339. if (!(rp->flags & FC_RP_FLAGS_RETRY) || rp->rp_state != RPORT_ST_READY)
  1340. goto retry; /* shouldn't happen */
  1341. fp = fc_frame_alloc(lp, sizeof(*srr));
  1342. if (!fp)
  1343. goto retry;
  1344. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1345. memset(srr, 0, sizeof(*srr));
  1346. srr->srr_op = ELS_SRR;
  1347. srr->srr_ox_id = htons(ep->oxid);
  1348. srr->srr_rx_id = htons(ep->rxid);
  1349. srr->srr_r_ctl = r_ctl;
  1350. srr->srr_rel_off = htonl(offset);
  1351. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1352. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  1353. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1354. seq = lp->tt.exch_seq_send(lp, fp, fc_fcp_srr_resp, NULL,
  1355. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
  1356. if (!seq)
  1357. goto retry;
  1358. fsp->recov_seq = seq;
  1359. fsp->xfer_len = offset;
  1360. fsp->xfer_contig_end = offset;
  1361. fsp->state &= ~FC_SRB_RCV_STATUS;
  1362. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1363. return;
  1364. retry:
  1365. fc_fcp_retry_cmd(fsp);
  1366. }
  1367. /*
  1368. * Handle response from SRR.
  1369. */
  1370. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1371. {
  1372. struct fc_fcp_pkt *fsp = arg;
  1373. struct fc_frame_header *fh;
  1374. if (IS_ERR(fp)) {
  1375. fc_fcp_srr_error(fsp, fp);
  1376. return;
  1377. }
  1378. if (fc_fcp_lock_pkt(fsp))
  1379. goto out;
  1380. fh = fc_frame_header_get(fp);
  1381. /*
  1382. * BUG? fc_fcp_srr_error calls exch_done which would release
  1383. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1384. * then fc_exch_timeout would be sending an abort. The exch_done
  1385. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1386. * an abort response though.
  1387. */
  1388. if (fh->fh_type == FC_TYPE_BLS) {
  1389. fc_fcp_unlock_pkt(fsp);
  1390. return;
  1391. }
  1392. fsp->recov_seq = NULL;
  1393. switch (fc_frame_payload_op(fp)) {
  1394. case ELS_LS_ACC:
  1395. fsp->recov_retry = 0;
  1396. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1397. break;
  1398. case ELS_LS_RJT:
  1399. default:
  1400. fc_timeout_error(fsp);
  1401. break;
  1402. }
  1403. fc_fcp_unlock_pkt(fsp);
  1404. fsp->lp->tt.exch_done(seq);
  1405. out:
  1406. fc_frame_free(fp);
  1407. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1408. }
  1409. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1410. {
  1411. if (fc_fcp_lock_pkt(fsp))
  1412. goto out;
  1413. fsp->lp->tt.exch_done(fsp->recov_seq);
  1414. fsp->recov_seq = NULL;
  1415. switch (PTR_ERR(fp)) {
  1416. case -FC_EX_TIMEOUT:
  1417. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1418. fc_fcp_rec(fsp);
  1419. else
  1420. fc_timeout_error(fsp);
  1421. break;
  1422. case -FC_EX_CLOSED: /* e.g., link failure */
  1423. /* fall through */
  1424. default:
  1425. fc_fcp_retry_cmd(fsp);
  1426. break;
  1427. }
  1428. fc_fcp_unlock_pkt(fsp);
  1429. out:
  1430. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1431. }
  1432. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lp)
  1433. {
  1434. /* lock ? */
  1435. return (lp->state == LPORT_ST_READY) && lp->link_up && !lp->qfull;
  1436. }
  1437. /**
  1438. * fc_queuecommand - The queuecommand function of the scsi template
  1439. * @cmd: struct scsi_cmnd to be executed
  1440. * @done: Callback function to be called when cmd is completed
  1441. *
  1442. * this is the i/o strategy routine, called by the scsi layer
  1443. * this routine is called with holding the host_lock.
  1444. */
  1445. int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
  1446. {
  1447. struct fc_lport *lp;
  1448. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1449. struct fc_fcp_pkt *fsp;
  1450. struct fc_rport_libfc_priv *rp;
  1451. int rval;
  1452. int rc = 0;
  1453. struct fcoe_dev_stats *stats;
  1454. lp = shost_priv(sc_cmd->device->host);
  1455. rval = fc_remote_port_chkready(rport);
  1456. if (rval) {
  1457. sc_cmd->result = rval;
  1458. done(sc_cmd);
  1459. goto out;
  1460. }
  1461. if (!*(struct fc_remote_port **)rport->dd_data) {
  1462. /*
  1463. * rport is transitioning from blocked/deleted to
  1464. * online
  1465. */
  1466. sc_cmd->result = DID_IMM_RETRY << 16;
  1467. done(sc_cmd);
  1468. goto out;
  1469. }
  1470. rp = rport->dd_data;
  1471. if (!fc_fcp_lport_queue_ready(lp)) {
  1472. rc = SCSI_MLQUEUE_HOST_BUSY;
  1473. goto out;
  1474. }
  1475. fsp = fc_fcp_pkt_alloc(lp, GFP_ATOMIC);
  1476. if (fsp == NULL) {
  1477. rc = SCSI_MLQUEUE_HOST_BUSY;
  1478. goto out;
  1479. }
  1480. /*
  1481. * build the libfc request pkt
  1482. */
  1483. fsp->cmd = sc_cmd; /* save the cmd */
  1484. fsp->lp = lp; /* save the softc ptr */
  1485. fsp->rport = rport; /* set the remote port ptr */
  1486. fsp->xfer_ddp = FC_XID_UNKNOWN;
  1487. sc_cmd->scsi_done = done;
  1488. /*
  1489. * set up the transfer length
  1490. */
  1491. fsp->data_len = scsi_bufflen(sc_cmd);
  1492. fsp->xfer_len = 0;
  1493. /*
  1494. * setup the data direction
  1495. */
  1496. stats = fc_lport_get_stats(lp);
  1497. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1498. fsp->req_flags = FC_SRB_READ;
  1499. stats->InputRequests++;
  1500. stats->InputMegabytes = fsp->data_len;
  1501. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1502. fsp->req_flags = FC_SRB_WRITE;
  1503. stats->OutputRequests++;
  1504. stats->OutputMegabytes = fsp->data_len;
  1505. } else {
  1506. fsp->req_flags = 0;
  1507. stats->ControlRequests++;
  1508. }
  1509. fsp->tgt_flags = rp->flags;
  1510. init_timer(&fsp->timer);
  1511. fsp->timer.data = (unsigned long)fsp;
  1512. /*
  1513. * send it to the lower layer
  1514. * if we get -1 return then put the request in the pending
  1515. * queue.
  1516. */
  1517. rval = fc_fcp_pkt_send(lp, fsp);
  1518. if (rval != 0) {
  1519. fsp->state = FC_SRB_FREE;
  1520. fc_fcp_pkt_release(fsp);
  1521. rc = SCSI_MLQUEUE_HOST_BUSY;
  1522. }
  1523. out:
  1524. return rc;
  1525. }
  1526. EXPORT_SYMBOL(fc_queuecommand);
  1527. /**
  1528. * fc_io_compl() - Handle responses for completed commands
  1529. * @fsp: scsi packet
  1530. *
  1531. * Translates a error to a Linux SCSI error.
  1532. *
  1533. * The fcp packet lock must be held when calling.
  1534. */
  1535. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1536. {
  1537. struct fc_fcp_internal *si;
  1538. struct scsi_cmnd *sc_cmd;
  1539. struct fc_lport *lp;
  1540. unsigned long flags;
  1541. /* release outstanding ddp context */
  1542. fc_fcp_ddp_done(fsp);
  1543. fsp->state |= FC_SRB_COMPL;
  1544. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1545. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1546. del_timer_sync(&fsp->timer);
  1547. spin_lock_bh(&fsp->scsi_pkt_lock);
  1548. }
  1549. lp = fsp->lp;
  1550. si = fc_get_scsi_internal(lp);
  1551. spin_lock_irqsave(lp->host->host_lock, flags);
  1552. if (!fsp->cmd) {
  1553. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1554. return;
  1555. }
  1556. /*
  1557. * if a command timed out while we had to try and throttle IO
  1558. * and it is now getting cleaned up, then we are about to
  1559. * try again so clear the throttled flag incase we get more
  1560. * time outs.
  1561. */
  1562. if (si->throttled && fsp->state & FC_SRB_NOMEM)
  1563. si->throttled = 0;
  1564. sc_cmd = fsp->cmd;
  1565. fsp->cmd = NULL;
  1566. if (!sc_cmd->SCp.ptr) {
  1567. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1568. return;
  1569. }
  1570. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1571. switch (fsp->status_code) {
  1572. case FC_COMPLETE:
  1573. if (fsp->cdb_status == 0) {
  1574. /*
  1575. * good I/O status
  1576. */
  1577. sc_cmd->result = DID_OK << 16;
  1578. if (fsp->scsi_resid)
  1579. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1580. } else {
  1581. /*
  1582. * transport level I/O was ok but scsi
  1583. * has non zero status
  1584. */
  1585. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1586. }
  1587. break;
  1588. case FC_ERROR:
  1589. sc_cmd->result = DID_ERROR << 16;
  1590. break;
  1591. case FC_DATA_UNDRUN:
  1592. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1593. /*
  1594. * scsi status is good but transport level
  1595. * underrun.
  1596. */
  1597. sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
  1598. DID_OK : DID_ERROR) << 16;
  1599. } else {
  1600. /*
  1601. * scsi got underrun, this is an error
  1602. */
  1603. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1604. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1605. }
  1606. break;
  1607. case FC_DATA_OVRRUN:
  1608. /*
  1609. * overrun is an error
  1610. */
  1611. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1612. break;
  1613. case FC_CMD_ABORTED:
  1614. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1615. break;
  1616. case FC_CMD_TIME_OUT:
  1617. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1618. break;
  1619. case FC_CMD_RESET:
  1620. sc_cmd->result = (DID_RESET << 16);
  1621. break;
  1622. case FC_HRD_ERROR:
  1623. sc_cmd->result = (DID_NO_CONNECT << 16);
  1624. break;
  1625. default:
  1626. sc_cmd->result = (DID_ERROR << 16);
  1627. break;
  1628. }
  1629. list_del(&fsp->list);
  1630. sc_cmd->SCp.ptr = NULL;
  1631. sc_cmd->scsi_done(sc_cmd);
  1632. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1633. /* release ref from initial allocation in queue command */
  1634. fc_fcp_pkt_release(fsp);
  1635. }
  1636. /**
  1637. * fc_eh_abort() - Abort a command
  1638. * @sc_cmd: scsi command to abort
  1639. *
  1640. * From scsi host template.
  1641. * send ABTS to the target device and wait for the response
  1642. * sc_cmd is the pointer to the command to be aborted.
  1643. */
  1644. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1645. {
  1646. struct fc_fcp_pkt *fsp;
  1647. struct fc_lport *lp;
  1648. int rc = FAILED;
  1649. unsigned long flags;
  1650. lp = shost_priv(sc_cmd->device->host);
  1651. if (lp->state != LPORT_ST_READY)
  1652. return rc;
  1653. else if (!lp->link_up)
  1654. return rc;
  1655. spin_lock_irqsave(lp->host->host_lock, flags);
  1656. fsp = CMD_SP(sc_cmd);
  1657. if (!fsp) {
  1658. /* command completed while scsi eh was setting up */
  1659. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1660. return SUCCESS;
  1661. }
  1662. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1663. fc_fcp_pkt_hold(fsp);
  1664. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1665. if (fc_fcp_lock_pkt(fsp)) {
  1666. /* completed while we were waiting for timer to be deleted */
  1667. rc = SUCCESS;
  1668. goto release_pkt;
  1669. }
  1670. rc = fc_fcp_pkt_abort(fsp);
  1671. fc_fcp_unlock_pkt(fsp);
  1672. release_pkt:
  1673. fc_fcp_pkt_release(fsp);
  1674. return rc;
  1675. }
  1676. EXPORT_SYMBOL(fc_eh_abort);
  1677. /**
  1678. * fc_eh_device_reset() Reset a single LUN
  1679. * @sc_cmd: scsi command
  1680. *
  1681. * Set from scsi host template to send tm cmd to the target and wait for the
  1682. * response.
  1683. */
  1684. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1685. {
  1686. struct fc_lport *lp;
  1687. struct fc_fcp_pkt *fsp;
  1688. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1689. int rc = FAILED;
  1690. struct fc_rport_libfc_priv *rp;
  1691. int rval;
  1692. rval = fc_remote_port_chkready(rport);
  1693. if (rval)
  1694. goto out;
  1695. rp = rport->dd_data;
  1696. lp = shost_priv(sc_cmd->device->host);
  1697. if (lp->state != LPORT_ST_READY)
  1698. return rc;
  1699. FC_SCSI_DBG(lp, "Resetting rport (%6x)\n", rport->port_id);
  1700. fsp = fc_fcp_pkt_alloc(lp, GFP_NOIO);
  1701. if (fsp == NULL) {
  1702. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1703. sc_cmd->result = DID_NO_CONNECT << 16;
  1704. goto out;
  1705. }
  1706. /*
  1707. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1708. * the sc passed in is not setup for execution like when sent
  1709. * through the queuecommand callout.
  1710. */
  1711. fsp->lp = lp; /* save the softc ptr */
  1712. fsp->rport = rport; /* set the remote port ptr */
  1713. /*
  1714. * flush outstanding commands
  1715. */
  1716. rc = fc_lun_reset(lp, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1717. fsp->state = FC_SRB_FREE;
  1718. fc_fcp_pkt_release(fsp);
  1719. out:
  1720. return rc;
  1721. }
  1722. EXPORT_SYMBOL(fc_eh_device_reset);
  1723. /**
  1724. * fc_eh_host_reset() - The reset function will reset the ports on the host.
  1725. * @sc_cmd: scsi command
  1726. */
  1727. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1728. {
  1729. struct Scsi_Host *shost = sc_cmd->device->host;
  1730. struct fc_lport *lp = shost_priv(shost);
  1731. unsigned long wait_tmo;
  1732. FC_SCSI_DBG(lp, "Resetting host\n");
  1733. lp->tt.lport_reset(lp);
  1734. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1735. while (!fc_fcp_lport_queue_ready(lp) && time_before(jiffies, wait_tmo))
  1736. msleep(1000);
  1737. if (fc_fcp_lport_queue_ready(lp)) {
  1738. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1739. "on port (%6x)\n", fc_host_port_id(lp->host));
  1740. return SUCCESS;
  1741. } else {
  1742. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1743. "port (%6x) is not ready.\n",
  1744. fc_host_port_id(lp->host));
  1745. return FAILED;
  1746. }
  1747. }
  1748. EXPORT_SYMBOL(fc_eh_host_reset);
  1749. /**
  1750. * fc_slave_alloc() - configure queue depth
  1751. * @sdev: scsi device
  1752. *
  1753. * Configures queue depth based on host's cmd_per_len. If not set
  1754. * then we use the libfc default.
  1755. */
  1756. int fc_slave_alloc(struct scsi_device *sdev)
  1757. {
  1758. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1759. if (!rport || fc_remote_port_chkready(rport))
  1760. return -ENXIO;
  1761. if (sdev->tagged_supported)
  1762. scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
  1763. else
  1764. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
  1765. FC_FCP_DFLT_QUEUE_DEPTH);
  1766. return 0;
  1767. }
  1768. EXPORT_SYMBOL(fc_slave_alloc);
  1769. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  1770. {
  1771. switch (reason) {
  1772. case SCSI_QDEPTH_DEFAULT:
  1773. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1774. break;
  1775. case SCSI_QDEPTH_QFULL:
  1776. scsi_track_queue_full(sdev, qdepth);
  1777. break;
  1778. case SCSI_QDEPTH_RAMP_UP:
  1779. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1780. break;
  1781. default:
  1782. return -EOPNOTSUPP;
  1783. }
  1784. return sdev->queue_depth;
  1785. }
  1786. EXPORT_SYMBOL(fc_change_queue_depth);
  1787. int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
  1788. {
  1789. if (sdev->tagged_supported) {
  1790. scsi_set_tag_type(sdev, tag_type);
  1791. if (tag_type)
  1792. scsi_activate_tcq(sdev, sdev->queue_depth);
  1793. else
  1794. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1795. } else
  1796. tag_type = 0;
  1797. return tag_type;
  1798. }
  1799. EXPORT_SYMBOL(fc_change_queue_type);
  1800. void fc_fcp_destroy(struct fc_lport *lp)
  1801. {
  1802. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  1803. if (!list_empty(&si->scsi_pkt_queue))
  1804. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  1805. "port (%6x)\n", fc_host_port_id(lp->host));
  1806. mempool_destroy(si->scsi_pkt_pool);
  1807. kfree(si);
  1808. lp->scsi_priv = NULL;
  1809. }
  1810. EXPORT_SYMBOL(fc_fcp_destroy);
  1811. int fc_setup_fcp()
  1812. {
  1813. int rc = 0;
  1814. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1815. sizeof(struct fc_fcp_pkt),
  1816. 0, SLAB_HWCACHE_ALIGN, NULL);
  1817. if (!scsi_pkt_cachep) {
  1818. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  1819. "module load failed!");
  1820. rc = -ENOMEM;
  1821. }
  1822. return rc;
  1823. }
  1824. void fc_destroy_fcp()
  1825. {
  1826. if (scsi_pkt_cachep)
  1827. kmem_cache_destroy(scsi_pkt_cachep);
  1828. }
  1829. int fc_fcp_init(struct fc_lport *lp)
  1830. {
  1831. int rc;
  1832. struct fc_fcp_internal *si;
  1833. if (!lp->tt.fcp_cmd_send)
  1834. lp->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1835. if (!lp->tt.fcp_cleanup)
  1836. lp->tt.fcp_cleanup = fc_fcp_cleanup;
  1837. if (!lp->tt.fcp_abort_io)
  1838. lp->tt.fcp_abort_io = fc_fcp_abort_io;
  1839. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  1840. if (!si)
  1841. return -ENOMEM;
  1842. lp->scsi_priv = si;
  1843. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  1844. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  1845. if (!si->scsi_pkt_pool) {
  1846. rc = -ENOMEM;
  1847. goto free_internal;
  1848. }
  1849. return 0;
  1850. free_internal:
  1851. kfree(si);
  1852. return rc;
  1853. }
  1854. EXPORT_SYMBOL(fc_fcp_init);