fc_fcp.c 53 KB

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