fc_fcp.c 53 KB

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