libiscsi.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <asm/unaligned.h>
  29. #include <net/tcp.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/iscsi_proto.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_iscsi.h>
  39. #include <scsi/libiscsi.h>
  40. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  41. #define SNA32_CHECK 2147483648UL
  42. static int iscsi_sna_lt(u32 n1, u32 n2)
  43. {
  44. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  45. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  46. }
  47. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  48. static int iscsi_sna_lte(u32 n1, u32 n2)
  49. {
  50. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  51. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  52. }
  53. void
  54. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  55. {
  56. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  57. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  58. /*
  59. * standard specifies this check for when to update expected and
  60. * max sequence numbers
  61. */
  62. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  63. return;
  64. if (exp_cmdsn != session->exp_cmdsn &&
  65. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  66. session->exp_cmdsn = exp_cmdsn;
  67. if (max_cmdsn != session->max_cmdsn &&
  68. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  69. session->max_cmdsn = max_cmdsn;
  70. /*
  71. * if the window closed with IO queued, then kick the
  72. * xmit thread
  73. */
  74. if (!list_empty(&session->leadconn->xmitqueue) ||
  75. !list_empty(&session->leadconn->mgmtqueue))
  76. scsi_queue_work(session->host,
  77. &session->leadconn->xmitwork);
  78. }
  79. }
  80. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  81. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  82. struct iscsi_data *hdr)
  83. {
  84. struct iscsi_conn *conn = ctask->conn;
  85. memset(hdr, 0, sizeof(struct iscsi_data));
  86. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  87. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  88. ctask->unsol_datasn++;
  89. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  90. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  91. hdr->itt = ctask->hdr->itt;
  92. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  93. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  94. if (ctask->unsol_count > conn->max_xmit_dlength) {
  95. hton24(hdr->dlength, conn->max_xmit_dlength);
  96. ctask->data_count = conn->max_xmit_dlength;
  97. ctask->unsol_offset += ctask->data_count;
  98. hdr->flags = 0;
  99. } else {
  100. hton24(hdr->dlength, ctask->unsol_count);
  101. ctask->data_count = ctask->unsol_count;
  102. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  103. }
  104. }
  105. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  106. static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
  107. {
  108. unsigned exp_len = ctask->hdr_len + len;
  109. if (exp_len > ctask->hdr_max) {
  110. WARN_ON(1);
  111. return -EINVAL;
  112. }
  113. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  114. ctask->hdr_len = exp_len;
  115. return 0;
  116. }
  117. /*
  118. * make an extended cdb AHS
  119. */
  120. static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
  121. {
  122. struct scsi_cmnd *cmd = ctask->sc;
  123. unsigned rlen, pad_len;
  124. unsigned short ahslength;
  125. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  126. int rc;
  127. ecdb_ahdr = iscsi_next_hdr(ctask);
  128. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  129. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  130. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  131. pad_len = iscsi_padding(rlen);
  132. rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
  133. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  134. if (rc)
  135. return rc;
  136. if (pad_len)
  137. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  138. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  139. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  140. ecdb_ahdr->reserved = 0;
  141. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  142. debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  143. "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
  144. cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len);
  145. return 0;
  146. }
  147. static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
  148. {
  149. struct scsi_cmnd *sc = ctask->sc;
  150. struct iscsi_rlength_ahdr *rlen_ahdr;
  151. int rc;
  152. rlen_ahdr = iscsi_next_hdr(ctask);
  153. rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
  154. if (rc)
  155. return rc;
  156. rlen_ahdr->ahslength =
  157. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  158. sizeof(rlen_ahdr->reserved));
  159. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  160. rlen_ahdr->reserved = 0;
  161. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  162. debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
  163. "rlen_ahdr->ahslength(%d)\n",
  164. be32_to_cpu(rlen_ahdr->read_length),
  165. be16_to_cpu(rlen_ahdr->ahslength));
  166. return 0;
  167. }
  168. /**
  169. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  170. * @ctask: iscsi cmd task
  171. *
  172. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  173. * fields like dlength or final based on how much data it sends
  174. */
  175. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  176. {
  177. struct iscsi_conn *conn = ctask->conn;
  178. struct iscsi_session *session = conn->session;
  179. struct iscsi_cmd *hdr = ctask->hdr;
  180. struct scsi_cmnd *sc = ctask->sc;
  181. unsigned hdrlength, cmd_len;
  182. int rc;
  183. ctask->hdr_len = 0;
  184. rc = iscsi_add_hdr(ctask, sizeof(*hdr));
  185. if (rc)
  186. return rc;
  187. hdr->opcode = ISCSI_OP_SCSI_CMD;
  188. hdr->flags = ISCSI_ATTR_SIMPLE;
  189. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  190. hdr->itt = build_itt(ctask->itt, session->age);
  191. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  192. session->cmdsn++;
  193. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  194. cmd_len = sc->cmd_len;
  195. if (cmd_len < ISCSI_CDB_SIZE)
  196. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  197. else if (cmd_len > ISCSI_CDB_SIZE) {
  198. rc = iscsi_prep_ecdb_ahs(ctask);
  199. if (rc)
  200. return rc;
  201. cmd_len = ISCSI_CDB_SIZE;
  202. }
  203. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  204. ctask->imm_count = 0;
  205. if (scsi_bidi_cmnd(sc)) {
  206. hdr->flags |= ISCSI_FLAG_CMD_READ;
  207. rc = iscsi_prep_bidi_ahs(ctask);
  208. if (rc)
  209. return rc;
  210. }
  211. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  212. unsigned out_len = scsi_out(sc)->length;
  213. hdr->data_length = cpu_to_be32(out_len);
  214. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  215. /*
  216. * Write counters:
  217. *
  218. * imm_count bytes to be sent right after
  219. * SCSI PDU Header
  220. *
  221. * unsol_count bytes(as Data-Out) to be sent
  222. * without R2T ack right after
  223. * immediate data
  224. *
  225. * r2t_data_count bytes to be sent via R2T ack's
  226. *
  227. * pad_count bytes to be sent as zero-padding
  228. */
  229. ctask->unsol_count = 0;
  230. ctask->unsol_offset = 0;
  231. ctask->unsol_datasn = 0;
  232. if (session->imm_data_en) {
  233. if (out_len >= session->first_burst)
  234. ctask->imm_count = min(session->first_burst,
  235. conn->max_xmit_dlength);
  236. else
  237. ctask->imm_count = min(out_len,
  238. conn->max_xmit_dlength);
  239. hton24(hdr->dlength, ctask->imm_count);
  240. } else
  241. zero_data(hdr->dlength);
  242. if (!session->initial_r2t_en) {
  243. ctask->unsol_count = min(session->first_burst, out_len)
  244. - ctask->imm_count;
  245. ctask->unsol_offset = ctask->imm_count;
  246. }
  247. if (!ctask->unsol_count)
  248. /* No unsolicit Data-Out's */
  249. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  250. } else {
  251. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  252. zero_data(hdr->dlength);
  253. hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
  254. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  255. hdr->flags |= ISCSI_FLAG_CMD_READ;
  256. }
  257. /* calculate size of additional header segments (AHSs) */
  258. hdrlength = ctask->hdr_len - sizeof(*hdr);
  259. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  260. hdrlength /= ISCSI_PAD_LEN;
  261. WARN_ON(hdrlength >= 256);
  262. hdr->hlength = hdrlength & 0xFF;
  263. if (conn->session->tt->init_cmd_task(conn->ctask))
  264. return EIO;
  265. conn->scsicmd_pdus_cnt++;
  266. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x "
  267. "len %d bidi_len %d cmdsn %d win %d]\n",
  268. scsi_bidi_cmnd(sc) ? "bidirectional" :
  269. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  270. conn->id, sc, sc->cmnd[0], ctask->itt,
  271. scsi_bufflen(sc), scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  272. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  273. return 0;
  274. }
  275. /**
  276. * iscsi_complete_command - return command back to scsi-ml
  277. * @ctask: iscsi cmd task
  278. *
  279. * Must be called with session lock.
  280. * This function returns the scsi command to scsi-ml and returns
  281. * the cmd task to the pool of available cmd tasks.
  282. */
  283. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  284. {
  285. struct iscsi_conn *conn = ctask->conn;
  286. struct iscsi_session *session = conn->session;
  287. struct scsi_cmnd *sc = ctask->sc;
  288. ctask->state = ISCSI_TASK_COMPLETED;
  289. ctask->sc = NULL;
  290. /* SCSI eh reuses commands to verify us */
  291. sc->SCp.ptr = NULL;
  292. if (conn->ctask == ctask)
  293. conn->ctask = NULL;
  294. list_del_init(&ctask->running);
  295. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  296. sc->scsi_done(sc);
  297. }
  298. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  299. {
  300. atomic_inc(&ctask->refcount);
  301. }
  302. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  303. {
  304. if (atomic_dec_and_test(&ctask->refcount))
  305. iscsi_complete_command(ctask);
  306. }
  307. /*
  308. * session lock must be held
  309. */
  310. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  311. int err)
  312. {
  313. struct scsi_cmnd *sc;
  314. sc = ctask->sc;
  315. if (!sc)
  316. return;
  317. if (ctask->state == ISCSI_TASK_PENDING)
  318. /*
  319. * cmd never made it to the xmit thread, so we should not count
  320. * the cmd in the sequencing
  321. */
  322. conn->session->queued_cmdsn--;
  323. else
  324. conn->session->tt->cleanup_cmd_task(conn, ctask);
  325. sc->result = err;
  326. if (!scsi_bidi_cmnd(sc))
  327. scsi_set_resid(sc, scsi_bufflen(sc));
  328. else {
  329. scsi_out(sc)->resid = scsi_out(sc)->length;
  330. scsi_in(sc)->resid = scsi_in(sc)->length;
  331. }
  332. if (conn->ctask == ctask)
  333. conn->ctask = NULL;
  334. /* release ref from queuecommand */
  335. __iscsi_put_ctask(ctask);
  336. }
  337. /**
  338. * iscsi_free_mgmt_task - return mgmt task back to pool
  339. * @conn: iscsi connection
  340. * @mtask: mtask
  341. *
  342. * Must be called with session lock.
  343. */
  344. void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  345. struct iscsi_mgmt_task *mtask)
  346. {
  347. list_del_init(&mtask->running);
  348. if (conn->login_mtask == mtask)
  349. return;
  350. if (conn->ping_mtask == mtask)
  351. conn->ping_mtask = NULL;
  352. __kfifo_put(conn->session->mgmtpool.queue,
  353. (void*)&mtask, sizeof(void*));
  354. }
  355. EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
  356. static struct iscsi_mgmt_task *
  357. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  358. char *data, uint32_t data_size)
  359. {
  360. struct iscsi_session *session = conn->session;
  361. struct iscsi_mgmt_task *mtask;
  362. if (session->state == ISCSI_STATE_TERMINATE)
  363. return NULL;
  364. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  365. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  366. /*
  367. * Login and Text are sent serially, in
  368. * request-followed-by-response sequence.
  369. * Same mtask can be used. Same ITT must be used.
  370. * Note that login_mtask is preallocated at conn_create().
  371. */
  372. mtask = conn->login_mtask;
  373. else {
  374. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  375. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  376. if (!__kfifo_get(session->mgmtpool.queue,
  377. (void*)&mtask, sizeof(void*)))
  378. return NULL;
  379. }
  380. if (data_size) {
  381. memcpy(mtask->data, data, data_size);
  382. mtask->data_count = data_size;
  383. } else
  384. mtask->data_count = 0;
  385. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  386. INIT_LIST_HEAD(&mtask->running);
  387. list_add_tail(&mtask->running, &conn->mgmtqueue);
  388. return mtask;
  389. }
  390. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  391. char *data, uint32_t data_size)
  392. {
  393. struct iscsi_conn *conn = cls_conn->dd_data;
  394. struct iscsi_session *session = conn->session;
  395. int err = 0;
  396. spin_lock_bh(&session->lock);
  397. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  398. err = -EPERM;
  399. spin_unlock_bh(&session->lock);
  400. scsi_queue_work(session->host, &conn->xmitwork);
  401. return err;
  402. }
  403. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  404. /**
  405. * iscsi_cmd_rsp - SCSI Command Response processing
  406. * @conn: iscsi connection
  407. * @hdr: iscsi header
  408. * @ctask: scsi command task
  409. * @data: cmd data buffer
  410. * @datalen: len of buffer
  411. *
  412. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  413. * then completes the command and task.
  414. **/
  415. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  416. struct iscsi_cmd_task *ctask, char *data,
  417. int datalen)
  418. {
  419. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  420. struct iscsi_session *session = conn->session;
  421. struct scsi_cmnd *sc = ctask->sc;
  422. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  423. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  424. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  425. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  426. sc->result = DID_ERROR << 16;
  427. goto out;
  428. }
  429. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  430. uint16_t senselen;
  431. if (datalen < 2) {
  432. invalid_datalen:
  433. iscsi_conn_printk(KERN_ERR, conn,
  434. "Got CHECK_CONDITION but invalid data "
  435. "buffer size of %d\n", datalen);
  436. sc->result = DID_BAD_TARGET << 16;
  437. goto out;
  438. }
  439. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  440. if (datalen < senselen)
  441. goto invalid_datalen;
  442. memcpy(sc->sense_buffer, data + 2,
  443. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  444. debug_scsi("copied %d bytes of sense\n",
  445. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  446. }
  447. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  448. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  449. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  450. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  451. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  452. res_count <= scsi_in(sc)->length))
  453. scsi_in(sc)->resid = res_count;
  454. else
  455. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  456. }
  457. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  458. ISCSI_FLAG_CMD_OVERFLOW)) {
  459. int res_count = be32_to_cpu(rhdr->residual_count);
  460. if (res_count > 0 &&
  461. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  462. res_count <= scsi_bufflen(sc)))
  463. /* write side for bidi or uni-io set_resid */
  464. scsi_set_resid(sc, res_count);
  465. else
  466. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  467. }
  468. out:
  469. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  470. (long)sc, sc->result, ctask->itt);
  471. conn->scsirsp_pdus_cnt++;
  472. __iscsi_put_ctask(ctask);
  473. }
  474. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  475. {
  476. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  477. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  478. conn->tmfrsp_pdus_cnt++;
  479. if (conn->tmf_state != TMF_QUEUED)
  480. return;
  481. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  482. conn->tmf_state = TMF_SUCCESS;
  483. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  484. conn->tmf_state = TMF_NOT_FOUND;
  485. else
  486. conn->tmf_state = TMF_FAILED;
  487. wake_up(&conn->ehwait);
  488. }
  489. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  490. {
  491. struct iscsi_nopout hdr;
  492. struct iscsi_mgmt_task *mtask;
  493. if (!rhdr && conn->ping_mtask)
  494. return;
  495. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  496. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  497. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  498. if (rhdr) {
  499. memcpy(hdr.lun, rhdr->lun, 8);
  500. hdr.ttt = rhdr->ttt;
  501. hdr.itt = RESERVED_ITT;
  502. } else
  503. hdr.ttt = RESERVED_ITT;
  504. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  505. if (!mtask) {
  506. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  507. return;
  508. }
  509. /* only track our nops */
  510. if (!rhdr) {
  511. conn->ping_mtask = mtask;
  512. conn->last_ping = jiffies;
  513. }
  514. scsi_queue_work(conn->session->host, &conn->xmitwork);
  515. }
  516. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  517. char *data, int datalen)
  518. {
  519. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  520. struct iscsi_hdr rejected_pdu;
  521. uint32_t itt;
  522. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  523. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  524. if (ntoh24(reject->dlength) > datalen)
  525. return ISCSI_ERR_PROTO;
  526. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  527. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  528. itt = get_itt(rejected_pdu.itt);
  529. iscsi_conn_printk(KERN_ERR, conn,
  530. "itt 0x%x had pdu (op 0x%x) rejected "
  531. "due to DataDigest error.\n", itt,
  532. rejected_pdu.opcode);
  533. }
  534. }
  535. return 0;
  536. }
  537. /**
  538. * __iscsi_complete_pdu - complete pdu
  539. * @conn: iscsi conn
  540. * @hdr: iscsi header
  541. * @data: data buffer
  542. * @datalen: len of data buffer
  543. *
  544. * Completes pdu processing by freeing any resources allocated at
  545. * queuecommand or send generic. session lock must be held and verify
  546. * itt must have been called.
  547. */
  548. static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  549. char *data, int datalen)
  550. {
  551. struct iscsi_session *session = conn->session;
  552. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  553. struct iscsi_cmd_task *ctask;
  554. struct iscsi_mgmt_task *mtask;
  555. uint32_t itt;
  556. conn->last_recv = jiffies;
  557. if (hdr->itt != RESERVED_ITT)
  558. itt = get_itt(hdr->itt);
  559. else
  560. itt = ~0U;
  561. if (itt < session->cmds_max) {
  562. ctask = session->cmds[itt];
  563. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  564. opcode, conn->id, ctask->itt, datalen);
  565. switch(opcode) {
  566. case ISCSI_OP_SCSI_CMD_RSP:
  567. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  568. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  569. datalen);
  570. break;
  571. case ISCSI_OP_SCSI_DATA_IN:
  572. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  573. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  574. conn->scsirsp_pdus_cnt++;
  575. __iscsi_put_ctask(ctask);
  576. }
  577. break;
  578. case ISCSI_OP_R2T:
  579. /* LLD handles this for now */
  580. break;
  581. default:
  582. rc = ISCSI_ERR_BAD_OPCODE;
  583. break;
  584. }
  585. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  586. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  587. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  588. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  589. opcode, conn->id, mtask->itt, datalen);
  590. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  591. switch(opcode) {
  592. case ISCSI_OP_LOGOUT_RSP:
  593. if (datalen) {
  594. rc = ISCSI_ERR_PROTO;
  595. break;
  596. }
  597. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  598. /* fall through */
  599. case ISCSI_OP_LOGIN_RSP:
  600. case ISCSI_OP_TEXT_RSP:
  601. /*
  602. * login related PDU's exp_statsn is handled in
  603. * userspace
  604. */
  605. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  606. rc = ISCSI_ERR_CONN_FAILED;
  607. iscsi_free_mgmt_task(conn, mtask);
  608. break;
  609. case ISCSI_OP_SCSI_TMFUNC_RSP:
  610. if (datalen) {
  611. rc = ISCSI_ERR_PROTO;
  612. break;
  613. }
  614. iscsi_tmf_rsp(conn, hdr);
  615. iscsi_free_mgmt_task(conn, mtask);
  616. break;
  617. case ISCSI_OP_NOOP_IN:
  618. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
  619. datalen) {
  620. rc = ISCSI_ERR_PROTO;
  621. break;
  622. }
  623. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  624. if (conn->ping_mtask != mtask) {
  625. /*
  626. * If this is not in response to one of our
  627. * nops then it must be from userspace.
  628. */
  629. if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
  630. datalen))
  631. rc = ISCSI_ERR_CONN_FAILED;
  632. } else
  633. mod_timer(&conn->transport_timer,
  634. jiffies + conn->recv_timeout);
  635. iscsi_free_mgmt_task(conn, mtask);
  636. break;
  637. default:
  638. rc = ISCSI_ERR_BAD_OPCODE;
  639. break;
  640. }
  641. } else if (itt == ~0U) {
  642. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  643. switch(opcode) {
  644. case ISCSI_OP_NOOP_IN:
  645. if (datalen) {
  646. rc = ISCSI_ERR_PROTO;
  647. break;
  648. }
  649. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  650. break;
  651. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  652. break;
  653. case ISCSI_OP_REJECT:
  654. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  655. break;
  656. case ISCSI_OP_ASYNC_EVENT:
  657. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  658. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  659. rc = ISCSI_ERR_CONN_FAILED;
  660. break;
  661. default:
  662. rc = ISCSI_ERR_BAD_OPCODE;
  663. break;
  664. }
  665. } else
  666. rc = ISCSI_ERR_BAD_ITT;
  667. return rc;
  668. }
  669. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  670. char *data, int datalen)
  671. {
  672. int rc;
  673. spin_lock(&conn->session->lock);
  674. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  675. spin_unlock(&conn->session->lock);
  676. return rc;
  677. }
  678. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  679. /* verify itt (itt encoding: age+cid+itt) */
  680. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  681. uint32_t *ret_itt)
  682. {
  683. struct iscsi_session *session = conn->session;
  684. struct iscsi_cmd_task *ctask;
  685. uint32_t itt;
  686. if (hdr->itt != RESERVED_ITT) {
  687. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  688. (session->age << ISCSI_AGE_SHIFT)) {
  689. iscsi_conn_printk(KERN_ERR, conn,
  690. "received itt %x expected session "
  691. "age (%x)\n", (__force u32)hdr->itt,
  692. session->age & ISCSI_AGE_MASK);
  693. return ISCSI_ERR_BAD_ITT;
  694. }
  695. itt = get_itt(hdr->itt);
  696. } else
  697. itt = ~0U;
  698. if (itt < session->cmds_max) {
  699. ctask = session->cmds[itt];
  700. if (!ctask->sc) {
  701. iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
  702. "with itt 0x%x\n", ctask->itt);
  703. /* force drop */
  704. return ISCSI_ERR_NO_SCSI_CMD;
  705. }
  706. if (ctask->sc->SCp.phase != session->age) {
  707. iscsi_conn_printk(KERN_ERR, conn,
  708. "iscsi: ctask's session age %d, "
  709. "expected %d\n", ctask->sc->SCp.phase,
  710. session->age);
  711. return ISCSI_ERR_SESSION_FAILED;
  712. }
  713. }
  714. *ret_itt = itt;
  715. return 0;
  716. }
  717. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  718. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  719. {
  720. struct iscsi_session *session = conn->session;
  721. unsigned long flags;
  722. spin_lock_irqsave(&session->lock, flags);
  723. if (session->state == ISCSI_STATE_FAILED) {
  724. spin_unlock_irqrestore(&session->lock, flags);
  725. return;
  726. }
  727. if (conn->stop_stage == 0)
  728. session->state = ISCSI_STATE_FAILED;
  729. spin_unlock_irqrestore(&session->lock, flags);
  730. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  731. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  732. iscsi_conn_error(conn->cls_conn, err);
  733. }
  734. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  735. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  736. struct iscsi_mgmt_task *mtask)
  737. {
  738. struct iscsi_session *session = conn->session;
  739. struct iscsi_hdr *hdr = mtask->hdr;
  740. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  741. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  742. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  743. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  744. /*
  745. * pre-format CmdSN for outgoing PDU.
  746. */
  747. nop->cmdsn = cpu_to_be32(session->cmdsn);
  748. if (hdr->itt != RESERVED_ITT) {
  749. hdr->itt = build_itt(mtask->itt, session->age);
  750. /*
  751. * TODO: We always use immediate, so we never hit this.
  752. * If we start to send tmfs or nops as non-immediate then
  753. * we should start checking the cmdsn numbers for mgmt tasks.
  754. */
  755. if (conn->c_stage == ISCSI_CONN_STARTED &&
  756. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  757. session->queued_cmdsn++;
  758. session->cmdsn++;
  759. }
  760. }
  761. if (session->tt->init_mgmt_task)
  762. session->tt->init_mgmt_task(conn, mtask);
  763. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  764. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  765. mtask->data_count);
  766. }
  767. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  768. {
  769. struct iscsi_hdr *hdr = conn->mtask->hdr;
  770. int rc;
  771. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  772. conn->session->state = ISCSI_STATE_LOGGING_OUT;
  773. spin_unlock_bh(&conn->session->lock);
  774. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  775. spin_lock_bh(&conn->session->lock);
  776. if (rc)
  777. return rc;
  778. /* done with this in-progress mtask */
  779. conn->mtask = NULL;
  780. return 0;
  781. }
  782. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  783. {
  784. struct iscsi_session *session = conn->session;
  785. /*
  786. * Check for iSCSI window and take care of CmdSN wrap-around
  787. */
  788. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  789. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  790. "CmdSN %u/%u\n", session->exp_cmdsn,
  791. session->max_cmdsn, session->cmdsn,
  792. session->queued_cmdsn);
  793. return -ENOSPC;
  794. }
  795. return 0;
  796. }
  797. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  798. {
  799. struct iscsi_cmd_task *ctask = conn->ctask;
  800. int rc;
  801. __iscsi_get_ctask(ctask);
  802. spin_unlock_bh(&conn->session->lock);
  803. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  804. spin_lock_bh(&conn->session->lock);
  805. __iscsi_put_ctask(ctask);
  806. if (!rc)
  807. /* done with this ctask */
  808. conn->ctask = NULL;
  809. return rc;
  810. }
  811. /**
  812. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  813. * @ctask: ctask to requeue
  814. *
  815. * LLDs that need to run a ctask from the session workqueue should call
  816. * this. The session lock must be held.
  817. */
  818. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  819. {
  820. struct iscsi_conn *conn = ctask->conn;
  821. list_move_tail(&ctask->running, &conn->requeue);
  822. scsi_queue_work(conn->session->host, &conn->xmitwork);
  823. }
  824. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  825. /**
  826. * iscsi_data_xmit - xmit any command into the scheduled connection
  827. * @conn: iscsi connection
  828. *
  829. * Notes:
  830. * The function can return -EAGAIN in which case the caller must
  831. * re-schedule it again later or recover. '0' return code means
  832. * successful xmit.
  833. **/
  834. static int iscsi_data_xmit(struct iscsi_conn *conn)
  835. {
  836. int rc = 0;
  837. spin_lock_bh(&conn->session->lock);
  838. if (unlikely(conn->suspend_tx)) {
  839. debug_scsi("conn %d Tx suspended!\n", conn->id);
  840. spin_unlock_bh(&conn->session->lock);
  841. return -ENODATA;
  842. }
  843. if (conn->ctask) {
  844. rc = iscsi_xmit_ctask(conn);
  845. if (rc)
  846. goto again;
  847. }
  848. if (conn->mtask) {
  849. rc = iscsi_xmit_mtask(conn);
  850. if (rc)
  851. goto again;
  852. }
  853. /*
  854. * process mgmt pdus like nops before commands since we should
  855. * only have one nop-out as a ping from us and targets should not
  856. * overflow us with nop-ins
  857. */
  858. check_mgmt:
  859. while (!list_empty(&conn->mgmtqueue)) {
  860. conn->mtask = list_entry(conn->mgmtqueue.next,
  861. struct iscsi_mgmt_task, running);
  862. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  863. iscsi_free_mgmt_task(conn, conn->mtask);
  864. conn->mtask = NULL;
  865. continue;
  866. }
  867. iscsi_prep_mtask(conn, conn->mtask);
  868. list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
  869. rc = iscsi_xmit_mtask(conn);
  870. if (rc)
  871. goto again;
  872. }
  873. /* process pending command queue */
  874. while (!list_empty(&conn->xmitqueue)) {
  875. if (conn->tmf_state == TMF_QUEUED)
  876. break;
  877. conn->ctask = list_entry(conn->xmitqueue.next,
  878. struct iscsi_cmd_task, running);
  879. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  880. fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
  881. continue;
  882. }
  883. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  884. fail_command(conn, conn->ctask, DID_ABORT << 16);
  885. continue;
  886. }
  887. conn->ctask->state = ISCSI_TASK_RUNNING;
  888. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  889. rc = iscsi_xmit_ctask(conn);
  890. if (rc)
  891. goto again;
  892. /*
  893. * we could continuously get new ctask requests so
  894. * we need to check the mgmt queue for nops that need to
  895. * be sent to aviod starvation
  896. */
  897. if (!list_empty(&conn->mgmtqueue))
  898. goto check_mgmt;
  899. }
  900. while (!list_empty(&conn->requeue)) {
  901. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  902. break;
  903. /*
  904. * we always do fastlogout - conn stop code will clean up.
  905. */
  906. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  907. break;
  908. conn->ctask = list_entry(conn->requeue.next,
  909. struct iscsi_cmd_task, running);
  910. conn->ctask->state = ISCSI_TASK_RUNNING;
  911. list_move_tail(conn->requeue.next, &conn->run_list);
  912. rc = iscsi_xmit_ctask(conn);
  913. if (rc)
  914. goto again;
  915. if (!list_empty(&conn->mgmtqueue))
  916. goto check_mgmt;
  917. }
  918. spin_unlock_bh(&conn->session->lock);
  919. return -ENODATA;
  920. again:
  921. if (unlikely(conn->suspend_tx))
  922. rc = -ENODATA;
  923. spin_unlock_bh(&conn->session->lock);
  924. return rc;
  925. }
  926. static void iscsi_xmitworker(struct work_struct *work)
  927. {
  928. struct iscsi_conn *conn =
  929. container_of(work, struct iscsi_conn, xmitwork);
  930. int rc;
  931. /*
  932. * serialize Xmit worker on a per-connection basis.
  933. */
  934. do {
  935. rc = iscsi_data_xmit(conn);
  936. } while (rc >= 0 || rc == -EAGAIN);
  937. }
  938. enum {
  939. FAILURE_BAD_HOST = 1,
  940. FAILURE_SESSION_FAILED,
  941. FAILURE_SESSION_FREED,
  942. FAILURE_WINDOW_CLOSED,
  943. FAILURE_OOM,
  944. FAILURE_SESSION_TERMINATE,
  945. FAILURE_SESSION_IN_RECOVERY,
  946. FAILURE_SESSION_RECOVERY_TIMEOUT,
  947. FAILURE_SESSION_LOGGING_OUT,
  948. FAILURE_SESSION_NOT_READY,
  949. };
  950. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  951. {
  952. struct iscsi_cls_session *cls_session;
  953. struct Scsi_Host *host;
  954. int reason = 0;
  955. struct iscsi_session *session;
  956. struct iscsi_conn *conn;
  957. struct iscsi_cmd_task *ctask = NULL;
  958. sc->scsi_done = done;
  959. sc->result = 0;
  960. sc->SCp.ptr = NULL;
  961. host = sc->device->host;
  962. spin_unlock(host->host_lock);
  963. cls_session = starget_to_session(scsi_target(sc->device));
  964. session = cls_session->dd_data;
  965. spin_lock(&session->lock);
  966. reason = iscsi_session_chkready(cls_session);
  967. if (reason) {
  968. sc->result = reason;
  969. goto fault;
  970. }
  971. /*
  972. * ISCSI_STATE_FAILED is a temp. state. The recovery
  973. * code will decide what is best to do with command queued
  974. * during this time
  975. */
  976. if (session->state != ISCSI_STATE_LOGGED_IN &&
  977. session->state != ISCSI_STATE_FAILED) {
  978. /*
  979. * to handle the race between when we set the recovery state
  980. * and block the session we requeue here (commands could
  981. * be entering our queuecommand while a block is starting
  982. * up because the block code is not locked)
  983. */
  984. switch (session->state) {
  985. case ISCSI_STATE_IN_RECOVERY:
  986. reason = FAILURE_SESSION_IN_RECOVERY;
  987. sc->result = DID_IMM_RETRY << 16;
  988. break;
  989. case ISCSI_STATE_LOGGING_OUT:
  990. reason = FAILURE_SESSION_LOGGING_OUT;
  991. sc->result = DID_IMM_RETRY << 16;
  992. break;
  993. case ISCSI_STATE_RECOVERY_FAILED:
  994. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  995. sc->result = DID_NO_CONNECT << 16;
  996. break;
  997. case ISCSI_STATE_TERMINATE:
  998. reason = FAILURE_SESSION_TERMINATE;
  999. sc->result = DID_NO_CONNECT << 16;
  1000. break;
  1001. default:
  1002. reason = FAILURE_SESSION_FREED;
  1003. sc->result = DID_NO_CONNECT << 16;
  1004. }
  1005. goto fault;
  1006. }
  1007. conn = session->leadconn;
  1008. if (!conn) {
  1009. reason = FAILURE_SESSION_FREED;
  1010. sc->result = DID_NO_CONNECT << 16;
  1011. goto fault;
  1012. }
  1013. if (iscsi_check_cmdsn_window_closed(conn)) {
  1014. reason = FAILURE_WINDOW_CLOSED;
  1015. goto reject;
  1016. }
  1017. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  1018. sizeof(void*))) {
  1019. reason = FAILURE_OOM;
  1020. goto reject;
  1021. }
  1022. session->queued_cmdsn++;
  1023. sc->SCp.phase = session->age;
  1024. sc->SCp.ptr = (char *)ctask;
  1025. atomic_set(&ctask->refcount, 1);
  1026. ctask->state = ISCSI_TASK_PENDING;
  1027. ctask->conn = conn;
  1028. ctask->sc = sc;
  1029. INIT_LIST_HEAD(&ctask->running);
  1030. list_add_tail(&ctask->running, &conn->xmitqueue);
  1031. spin_unlock(&session->lock);
  1032. scsi_queue_work(host, &conn->xmitwork);
  1033. spin_lock(host->host_lock);
  1034. return 0;
  1035. reject:
  1036. spin_unlock(&session->lock);
  1037. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  1038. spin_lock(host->host_lock);
  1039. return SCSI_MLQUEUE_HOST_BUSY;
  1040. fault:
  1041. spin_unlock(&session->lock);
  1042. debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
  1043. if (!scsi_bidi_cmnd(sc))
  1044. scsi_set_resid(sc, scsi_bufflen(sc));
  1045. else {
  1046. scsi_out(sc)->resid = scsi_out(sc)->length;
  1047. scsi_in(sc)->resid = scsi_in(sc)->length;
  1048. }
  1049. sc->scsi_done(sc);
  1050. spin_lock(host->host_lock);
  1051. return 0;
  1052. }
  1053. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1054. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  1055. {
  1056. if (depth > ISCSI_MAX_CMD_PER_LUN)
  1057. depth = ISCSI_MAX_CMD_PER_LUN;
  1058. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1059. return sdev->queue_depth;
  1060. }
  1061. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1062. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  1063. {
  1064. struct iscsi_session *session = cls_session->dd_data;
  1065. spin_lock_bh(&session->lock);
  1066. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1067. session->state = ISCSI_STATE_RECOVERY_FAILED;
  1068. if (session->leadconn)
  1069. wake_up(&session->leadconn->ehwait);
  1070. }
  1071. spin_unlock_bh(&session->lock);
  1072. }
  1073. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  1074. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  1075. {
  1076. struct iscsi_cls_session *cls_session;
  1077. struct iscsi_session *session;
  1078. struct iscsi_conn *conn;
  1079. cls_session = starget_to_session(scsi_target(sc->device));
  1080. session = cls_session->dd_data;
  1081. conn = session->leadconn;
  1082. mutex_lock(&session->eh_mutex);
  1083. spin_lock_bh(&session->lock);
  1084. if (session->state == ISCSI_STATE_TERMINATE) {
  1085. failed:
  1086. debug_scsi("failing host reset: session terminated "
  1087. "[CID %d age %d]\n", conn->id, session->age);
  1088. spin_unlock_bh(&session->lock);
  1089. mutex_unlock(&session->eh_mutex);
  1090. return FAILED;
  1091. }
  1092. spin_unlock_bh(&session->lock);
  1093. mutex_unlock(&session->eh_mutex);
  1094. /*
  1095. * we drop the lock here but the leadconn cannot be destoyed while
  1096. * we are in the scsi eh
  1097. */
  1098. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1099. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  1100. wait_event_interruptible(conn->ehwait,
  1101. session->state == ISCSI_STATE_TERMINATE ||
  1102. session->state == ISCSI_STATE_LOGGED_IN ||
  1103. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1104. if (signal_pending(current))
  1105. flush_signals(current);
  1106. mutex_lock(&session->eh_mutex);
  1107. spin_lock_bh(&session->lock);
  1108. if (session->state == ISCSI_STATE_LOGGED_IN)
  1109. iscsi_session_printk(KERN_INFO, session,
  1110. "host reset succeeded\n");
  1111. else
  1112. goto failed;
  1113. spin_unlock_bh(&session->lock);
  1114. mutex_unlock(&session->eh_mutex);
  1115. return SUCCESS;
  1116. }
  1117. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  1118. static void iscsi_tmf_timedout(unsigned long data)
  1119. {
  1120. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1121. struct iscsi_session *session = conn->session;
  1122. spin_lock(&session->lock);
  1123. if (conn->tmf_state == TMF_QUEUED) {
  1124. conn->tmf_state = TMF_TIMEDOUT;
  1125. debug_scsi("tmf timedout\n");
  1126. /* unblock eh_abort() */
  1127. wake_up(&conn->ehwait);
  1128. }
  1129. spin_unlock(&session->lock);
  1130. }
  1131. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1132. struct iscsi_tm *hdr, int age,
  1133. int timeout)
  1134. {
  1135. struct iscsi_session *session = conn->session;
  1136. struct iscsi_mgmt_task *mtask;
  1137. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1138. NULL, 0);
  1139. if (!mtask) {
  1140. spin_unlock_bh(&session->lock);
  1141. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1142. spin_lock_bh(&session->lock);
  1143. debug_scsi("tmf exec failure\n");
  1144. return -EPERM;
  1145. }
  1146. conn->tmfcmd_pdus_cnt++;
  1147. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1148. conn->tmf_timer.function = iscsi_tmf_timedout;
  1149. conn->tmf_timer.data = (unsigned long)conn;
  1150. add_timer(&conn->tmf_timer);
  1151. debug_scsi("tmf set timeout\n");
  1152. spin_unlock_bh(&session->lock);
  1153. mutex_unlock(&session->eh_mutex);
  1154. scsi_queue_work(session->host, &conn->xmitwork);
  1155. /*
  1156. * block eh thread until:
  1157. *
  1158. * 1) tmf response
  1159. * 2) tmf timeout
  1160. * 3) session is terminated or restarted or userspace has
  1161. * given up on recovery
  1162. */
  1163. wait_event_interruptible(conn->ehwait, age != session->age ||
  1164. session->state != ISCSI_STATE_LOGGED_IN ||
  1165. conn->tmf_state != TMF_QUEUED);
  1166. if (signal_pending(current))
  1167. flush_signals(current);
  1168. del_timer_sync(&conn->tmf_timer);
  1169. mutex_lock(&session->eh_mutex);
  1170. spin_lock_bh(&session->lock);
  1171. /* if the session drops it will clean up the mtask */
  1172. if (age != session->age ||
  1173. session->state != ISCSI_STATE_LOGGED_IN)
  1174. return -ENOTCONN;
  1175. return 0;
  1176. }
  1177. /*
  1178. * Fail commands. session lock held and recv side suspended and xmit
  1179. * thread flushed
  1180. */
  1181. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
  1182. int error)
  1183. {
  1184. struct iscsi_cmd_task *ctask, *tmp;
  1185. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1186. conn->ctask = NULL;
  1187. /* flush pending */
  1188. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1189. if (lun == ctask->sc->device->lun || lun == -1) {
  1190. debug_scsi("failing pending sc %p itt 0x%x\n",
  1191. ctask->sc, ctask->itt);
  1192. fail_command(conn, ctask, error << 16);
  1193. }
  1194. }
  1195. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1196. if (lun == ctask->sc->device->lun || lun == -1) {
  1197. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1198. ctask->sc, ctask->itt);
  1199. fail_command(conn, ctask, error << 16);
  1200. }
  1201. }
  1202. /* fail all other running */
  1203. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1204. if (lun == ctask->sc->device->lun || lun == -1) {
  1205. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1206. ctask->sc, ctask->itt);
  1207. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1208. }
  1209. }
  1210. }
  1211. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1212. {
  1213. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1214. scsi_flush_work(conn->session->host);
  1215. }
  1216. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1217. static void iscsi_start_tx(struct iscsi_conn *conn)
  1218. {
  1219. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1220. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1221. }
  1222. static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1223. {
  1224. struct iscsi_cls_session *cls_session;
  1225. struct iscsi_session *session;
  1226. struct iscsi_conn *conn;
  1227. enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
  1228. cls_session = starget_to_session(scsi_target(scmd->device));
  1229. session = cls_session->dd_data;
  1230. debug_scsi("scsi cmd %p timedout\n", scmd);
  1231. spin_lock(&session->lock);
  1232. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1233. /*
  1234. * We are probably in the middle of iscsi recovery so let
  1235. * that complete and handle the error.
  1236. */
  1237. rc = EH_RESET_TIMER;
  1238. goto done;
  1239. }
  1240. conn = session->leadconn;
  1241. if (!conn) {
  1242. /* In the middle of shuting down */
  1243. rc = EH_RESET_TIMER;
  1244. goto done;
  1245. }
  1246. if (!conn->recv_timeout && !conn->ping_timeout)
  1247. goto done;
  1248. /*
  1249. * if the ping timedout then we are in the middle of cleaning up
  1250. * and can let the iscsi eh handle it
  1251. */
  1252. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1253. (conn->ping_timeout * HZ), jiffies))
  1254. rc = EH_RESET_TIMER;
  1255. /*
  1256. * if we are about to check the transport then give the command
  1257. * more time
  1258. */
  1259. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1260. jiffies))
  1261. rc = EH_RESET_TIMER;
  1262. /* if in the middle of checking the transport then give us more time */
  1263. if (conn->ping_mtask)
  1264. rc = EH_RESET_TIMER;
  1265. done:
  1266. spin_unlock(&session->lock);
  1267. debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
  1268. return rc;
  1269. }
  1270. static void iscsi_check_transport_timeouts(unsigned long data)
  1271. {
  1272. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1273. struct iscsi_session *session = conn->session;
  1274. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1275. spin_lock(&session->lock);
  1276. if (session->state != ISCSI_STATE_LOGGED_IN)
  1277. goto done;
  1278. recv_timeout = conn->recv_timeout;
  1279. if (!recv_timeout)
  1280. goto done;
  1281. recv_timeout *= HZ;
  1282. last_recv = conn->last_recv;
  1283. if (conn->ping_mtask &&
  1284. time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
  1285. jiffies)) {
  1286. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1287. "expired, last rx %lu, last ping %lu, "
  1288. "now %lu\n", conn->ping_timeout, last_recv,
  1289. conn->last_ping, jiffies);
  1290. spin_unlock(&session->lock);
  1291. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1292. return;
  1293. }
  1294. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1295. /* send a ping to try to provoke some traffic */
  1296. debug_scsi("Sending nopout as ping on conn %p\n", conn);
  1297. iscsi_send_nopout(conn, NULL);
  1298. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1299. } else
  1300. next_timeout = last_recv + recv_timeout;
  1301. debug_scsi("Setting next tmo %lu\n", next_timeout);
  1302. mod_timer(&conn->transport_timer, next_timeout);
  1303. done:
  1304. spin_unlock(&session->lock);
  1305. }
  1306. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1307. struct iscsi_tm *hdr)
  1308. {
  1309. memset(hdr, 0, sizeof(*hdr));
  1310. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1311. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1312. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1313. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1314. hdr->rtt = ctask->hdr->itt;
  1315. hdr->refcmdsn = ctask->hdr->cmdsn;
  1316. }
  1317. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1318. {
  1319. struct iscsi_cls_session *cls_session;
  1320. struct iscsi_session *session;
  1321. struct iscsi_conn *conn;
  1322. struct iscsi_cmd_task *ctask;
  1323. struct iscsi_tm *hdr;
  1324. int rc, age;
  1325. cls_session = starget_to_session(scsi_target(sc->device));
  1326. session = cls_session->dd_data;
  1327. mutex_lock(&session->eh_mutex);
  1328. spin_lock_bh(&session->lock);
  1329. /*
  1330. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1331. * got the command.
  1332. */
  1333. if (!sc->SCp.ptr) {
  1334. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1335. spin_unlock_bh(&session->lock);
  1336. mutex_unlock(&session->eh_mutex);
  1337. return SUCCESS;
  1338. }
  1339. /*
  1340. * If we are not logged in or we have started a new session
  1341. * then let the host reset code handle this
  1342. */
  1343. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1344. sc->SCp.phase != session->age) {
  1345. spin_unlock_bh(&session->lock);
  1346. mutex_unlock(&session->eh_mutex);
  1347. return FAILED;
  1348. }
  1349. conn = session->leadconn;
  1350. conn->eh_abort_cnt++;
  1351. age = session->age;
  1352. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1353. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1354. /* ctask completed before time out */
  1355. if (!ctask->sc) {
  1356. debug_scsi("sc completed while abort in progress\n");
  1357. goto success;
  1358. }
  1359. if (ctask->state == ISCSI_TASK_PENDING) {
  1360. fail_command(conn, ctask, DID_ABORT << 16);
  1361. goto success;
  1362. }
  1363. /* only have one tmf outstanding at a time */
  1364. if (conn->tmf_state != TMF_INITIAL)
  1365. goto failed;
  1366. conn->tmf_state = TMF_QUEUED;
  1367. hdr = &conn->tmhdr;
  1368. iscsi_prep_abort_task_pdu(ctask, hdr);
  1369. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1370. rc = FAILED;
  1371. goto failed;
  1372. }
  1373. switch (conn->tmf_state) {
  1374. case TMF_SUCCESS:
  1375. spin_unlock_bh(&session->lock);
  1376. iscsi_suspend_tx(conn);
  1377. /*
  1378. * clean up task if aborted. grab the recv lock as a writer
  1379. */
  1380. write_lock_bh(conn->recv_lock);
  1381. spin_lock(&session->lock);
  1382. fail_command(conn, ctask, DID_ABORT << 16);
  1383. conn->tmf_state = TMF_INITIAL;
  1384. spin_unlock(&session->lock);
  1385. write_unlock_bh(conn->recv_lock);
  1386. iscsi_start_tx(conn);
  1387. goto success_unlocked;
  1388. case TMF_TIMEDOUT:
  1389. spin_unlock_bh(&session->lock);
  1390. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1391. goto failed_unlocked;
  1392. case TMF_NOT_FOUND:
  1393. if (!sc->SCp.ptr) {
  1394. conn->tmf_state = TMF_INITIAL;
  1395. /* ctask completed before tmf abort response */
  1396. debug_scsi("sc completed while abort in progress\n");
  1397. goto success;
  1398. }
  1399. /* fall through */
  1400. default:
  1401. conn->tmf_state = TMF_INITIAL;
  1402. goto failed;
  1403. }
  1404. success:
  1405. spin_unlock_bh(&session->lock);
  1406. success_unlocked:
  1407. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1408. mutex_unlock(&session->eh_mutex);
  1409. return SUCCESS;
  1410. failed:
  1411. spin_unlock_bh(&session->lock);
  1412. failed_unlocked:
  1413. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1414. ctask ? ctask->itt : 0);
  1415. mutex_unlock(&session->eh_mutex);
  1416. return FAILED;
  1417. }
  1418. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1419. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1420. {
  1421. memset(hdr, 0, sizeof(*hdr));
  1422. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1423. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1424. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1425. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1426. hdr->rtt = RESERVED_ITT;
  1427. }
  1428. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1429. {
  1430. struct iscsi_cls_session *cls_session;
  1431. struct iscsi_session *session;
  1432. struct iscsi_conn *conn;
  1433. struct iscsi_tm *hdr;
  1434. int rc = FAILED;
  1435. cls_session = starget_to_session(scsi_target(sc->device));
  1436. session = cls_session->dd_data;
  1437. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1438. mutex_lock(&session->eh_mutex);
  1439. spin_lock_bh(&session->lock);
  1440. /*
  1441. * Just check if we are not logged in. We cannot check for
  1442. * the phase because the reset could come from a ioctl.
  1443. */
  1444. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1445. goto unlock;
  1446. conn = session->leadconn;
  1447. /* only have one tmf outstanding at a time */
  1448. if (conn->tmf_state != TMF_INITIAL)
  1449. goto unlock;
  1450. conn->tmf_state = TMF_QUEUED;
  1451. hdr = &conn->tmhdr;
  1452. iscsi_prep_lun_reset_pdu(sc, hdr);
  1453. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1454. session->lu_reset_timeout)) {
  1455. rc = FAILED;
  1456. goto unlock;
  1457. }
  1458. switch (conn->tmf_state) {
  1459. case TMF_SUCCESS:
  1460. break;
  1461. case TMF_TIMEDOUT:
  1462. spin_unlock_bh(&session->lock);
  1463. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1464. goto done;
  1465. default:
  1466. conn->tmf_state = TMF_INITIAL;
  1467. goto unlock;
  1468. }
  1469. rc = SUCCESS;
  1470. spin_unlock_bh(&session->lock);
  1471. iscsi_suspend_tx(conn);
  1472. /* need to grab the recv lock then session lock */
  1473. write_lock_bh(conn->recv_lock);
  1474. spin_lock(&session->lock);
  1475. fail_all_commands(conn, sc->device->lun, DID_ERROR);
  1476. conn->tmf_state = TMF_INITIAL;
  1477. spin_unlock(&session->lock);
  1478. write_unlock_bh(conn->recv_lock);
  1479. iscsi_start_tx(conn);
  1480. goto done;
  1481. unlock:
  1482. spin_unlock_bh(&session->lock);
  1483. done:
  1484. debug_scsi("iscsi_eh_device_reset %s\n",
  1485. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1486. mutex_unlock(&session->eh_mutex);
  1487. return rc;
  1488. }
  1489. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1490. /*
  1491. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1492. * should be accessed via kfifo_{get,put} on q->queue.
  1493. * Optionally, the caller can obtain the array of object pointers
  1494. * by passing in a non-NULL @items pointer
  1495. */
  1496. int
  1497. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1498. {
  1499. int i, num_arrays = 1;
  1500. memset(q, 0, sizeof(*q));
  1501. q->max = max;
  1502. /* If the user passed an items pointer, he wants a copy of
  1503. * the array. */
  1504. if (items)
  1505. num_arrays++;
  1506. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1507. if (q->pool == NULL)
  1508. goto enomem;
  1509. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1510. GFP_KERNEL, NULL);
  1511. if (q->queue == ERR_PTR(-ENOMEM))
  1512. goto enomem;
  1513. for (i = 0; i < max; i++) {
  1514. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1515. if (q->pool[i] == NULL) {
  1516. q->max = i;
  1517. goto enomem;
  1518. }
  1519. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1520. }
  1521. if (items) {
  1522. *items = q->pool + max;
  1523. memcpy(*items, q->pool, max * sizeof(void *));
  1524. }
  1525. return 0;
  1526. enomem:
  1527. iscsi_pool_free(q);
  1528. return -ENOMEM;
  1529. }
  1530. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1531. void iscsi_pool_free(struct iscsi_pool *q)
  1532. {
  1533. int i;
  1534. for (i = 0; i < q->max; i++)
  1535. kfree(q->pool[i]);
  1536. if (q->pool)
  1537. kfree(q->pool);
  1538. }
  1539. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1540. /**
  1541. * iscsi_host_add - add host to system
  1542. * @shost: scsi host
  1543. * @pdev: parent device
  1544. *
  1545. * This should be called by partial offload and software iscsi drivers
  1546. * to add a host to the system.
  1547. */
  1548. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  1549. {
  1550. return scsi_add_host(shost, pdev);
  1551. }
  1552. EXPORT_SYMBOL_GPL(iscsi_host_add);
  1553. /**
  1554. * iscsi_host_alloc - allocate a host and driver data
  1555. * @sht: scsi host template
  1556. * @dd_data_size: driver host data size
  1557. * @qdepth: default device queue depth
  1558. *
  1559. * This should be called by partial offload and software iscsi drivers.
  1560. * To access the driver specific memory use the iscsi_host_priv() macro.
  1561. */
  1562. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  1563. int dd_data_size, uint16_t qdepth)
  1564. {
  1565. struct Scsi_Host *shost;
  1566. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  1567. if (!shost)
  1568. return NULL;
  1569. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1570. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1571. if (qdepth != 0)
  1572. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1573. "Queue depth must be between 1 and %d.\n",
  1574. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1575. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1576. }
  1577. shost->cmd_per_lun = qdepth;
  1578. return shost;
  1579. }
  1580. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  1581. /**
  1582. * iscsi_host_remove - remove host and sessions
  1583. * @shost: scsi host
  1584. *
  1585. * This will also remove any sessions attached to the host, but if userspace
  1586. * is managing the session at the same time this will break. TODO: add
  1587. * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
  1588. * does not remove the memory from under us.
  1589. */
  1590. void iscsi_host_remove(struct Scsi_Host *shost)
  1591. {
  1592. iscsi_host_for_each_session(shost, iscsi_session_teardown);
  1593. scsi_remove_host(shost);
  1594. }
  1595. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  1596. void iscsi_host_free(struct Scsi_Host *shost)
  1597. {
  1598. struct iscsi_host *ihost = shost_priv(shost);
  1599. kfree(ihost->netdev);
  1600. kfree(ihost->hwaddress);
  1601. kfree(ihost->initiatorname);
  1602. scsi_host_put(shost);
  1603. }
  1604. EXPORT_SYMBOL_GPL(iscsi_host_free);
  1605. /**
  1606. * iscsi_session_setup - create iscsi cls session and host and session
  1607. * @iscsit: iscsi transport template
  1608. * @shost: scsi host
  1609. * @cmds_max: session can queue
  1610. * @cmd_task_size: LLD ctask private data size
  1611. * @mgmt_task_size: LLD mtask private data size
  1612. * @initial_cmdsn: initial CmdSN
  1613. *
  1614. * This can be used by software iscsi_transports that allocate
  1615. * a session per scsi host.
  1616. */
  1617. struct iscsi_cls_session *
  1618. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  1619. uint16_t cmds_max, int cmd_task_size, int mgmt_task_size,
  1620. uint32_t initial_cmdsn)
  1621. {
  1622. struct iscsi_session *session;
  1623. struct iscsi_cls_session *cls_session;
  1624. int cmd_i;
  1625. if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
  1626. cmds_max < 2) {
  1627. if (cmds_max != 0)
  1628. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1629. "can_queue must be a power of 2 and between "
  1630. "2 and %d - setting to %d.\n", cmds_max,
  1631. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1632. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1633. }
  1634. cls_session = iscsi_alloc_session(shost, iscsit,
  1635. sizeof(struct iscsi_session));
  1636. if (!cls_session)
  1637. return NULL;
  1638. session = cls_session->dd_data;
  1639. session->cls_session = cls_session;
  1640. session->host = shost;
  1641. session->state = ISCSI_STATE_FREE;
  1642. session->fast_abort = 1;
  1643. session->lu_reset_timeout = 15;
  1644. session->abort_timeout = 10;
  1645. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1646. session->cmds_max = cmds_max;
  1647. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1648. session->exp_cmdsn = initial_cmdsn + 1;
  1649. session->max_cmdsn = initial_cmdsn + 1;
  1650. session->max_r2t = 1;
  1651. session->tt = iscsit;
  1652. mutex_init(&session->eh_mutex);
  1653. spin_lock_init(&session->lock);
  1654. /* initialize SCSI PDU commands pool */
  1655. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1656. (void***)&session->cmds,
  1657. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1658. goto cmdpool_alloc_fail;
  1659. /* pre-format cmds pool with ITT */
  1660. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1661. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1662. if (cmd_task_size)
  1663. ctask->dd_data = &ctask[1];
  1664. ctask->itt = cmd_i;
  1665. INIT_LIST_HEAD(&ctask->running);
  1666. }
  1667. /* initialize immediate command pool */
  1668. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1669. (void***)&session->mgmt_cmds,
  1670. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1671. goto mgmtpool_alloc_fail;
  1672. /* pre-format immediate cmds pool with ITT */
  1673. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1674. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1675. if (mgmt_task_size)
  1676. mtask->dd_data = &mtask[1];
  1677. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1678. INIT_LIST_HEAD(&mtask->running);
  1679. }
  1680. if (!try_module_get(iscsit->owner))
  1681. goto module_get_fail;
  1682. if (iscsi_add_session(cls_session, 0))
  1683. goto cls_session_fail;
  1684. return cls_session;
  1685. cls_session_fail:
  1686. module_put(iscsit->owner);
  1687. module_get_fail:
  1688. iscsi_pool_free(&session->mgmtpool);
  1689. mgmtpool_alloc_fail:
  1690. iscsi_pool_free(&session->cmdpool);
  1691. cmdpool_alloc_fail:
  1692. iscsi_free_session(cls_session);
  1693. return NULL;
  1694. }
  1695. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1696. /**
  1697. * iscsi_session_teardown - destroy session, host, and cls_session
  1698. * @cls_session: iscsi session
  1699. *
  1700. * The driver must have called iscsi_remove_session before
  1701. * calling this.
  1702. */
  1703. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1704. {
  1705. struct iscsi_session *session = cls_session->dd_data;
  1706. struct module *owner = cls_session->transport->owner;
  1707. iscsi_pool_free(&session->mgmtpool);
  1708. iscsi_pool_free(&session->cmdpool);
  1709. kfree(session->password);
  1710. kfree(session->password_in);
  1711. kfree(session->username);
  1712. kfree(session->username_in);
  1713. kfree(session->targetname);
  1714. iscsi_destroy_session(cls_session);
  1715. module_put(owner);
  1716. }
  1717. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1718. /**
  1719. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1720. * @cls_session: iscsi_cls_session
  1721. * @dd_size: private driver data size
  1722. * @conn_idx: cid
  1723. */
  1724. struct iscsi_cls_conn *
  1725. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  1726. uint32_t conn_idx)
  1727. {
  1728. struct iscsi_session *session = cls_session->dd_data;
  1729. struct iscsi_conn *conn;
  1730. struct iscsi_cls_conn *cls_conn;
  1731. char *data;
  1732. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  1733. conn_idx);
  1734. if (!cls_conn)
  1735. return NULL;
  1736. conn = cls_conn->dd_data;
  1737. memset(conn, 0, sizeof(*conn) + dd_size);
  1738. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  1739. conn->session = session;
  1740. conn->cls_conn = cls_conn;
  1741. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1742. conn->id = conn_idx;
  1743. conn->exp_statsn = 0;
  1744. conn->tmf_state = TMF_INITIAL;
  1745. init_timer(&conn->transport_timer);
  1746. conn->transport_timer.data = (unsigned long)conn;
  1747. conn->transport_timer.function = iscsi_check_transport_timeouts;
  1748. INIT_LIST_HEAD(&conn->run_list);
  1749. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1750. INIT_LIST_HEAD(&conn->mgmtqueue);
  1751. INIT_LIST_HEAD(&conn->xmitqueue);
  1752. INIT_LIST_HEAD(&conn->requeue);
  1753. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1754. /* allocate login_mtask used for the login/text sequences */
  1755. spin_lock_bh(&session->lock);
  1756. if (!__kfifo_get(session->mgmtpool.queue,
  1757. (void*)&conn->login_mtask,
  1758. sizeof(void*))) {
  1759. spin_unlock_bh(&session->lock);
  1760. goto login_mtask_alloc_fail;
  1761. }
  1762. spin_unlock_bh(&session->lock);
  1763. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1764. if (!data)
  1765. goto login_mtask_data_alloc_fail;
  1766. conn->login_mtask->data = conn->data = data;
  1767. init_timer(&conn->tmf_timer);
  1768. init_waitqueue_head(&conn->ehwait);
  1769. return cls_conn;
  1770. login_mtask_data_alloc_fail:
  1771. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1772. sizeof(void*));
  1773. login_mtask_alloc_fail:
  1774. iscsi_destroy_conn(cls_conn);
  1775. return NULL;
  1776. }
  1777. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1778. /**
  1779. * iscsi_conn_teardown - teardown iscsi connection
  1780. * cls_conn: iscsi class connection
  1781. *
  1782. * TODO: we may need to make this into a two step process
  1783. * like scsi-mls remove + put host
  1784. */
  1785. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1786. {
  1787. struct iscsi_conn *conn = cls_conn->dd_data;
  1788. struct iscsi_session *session = conn->session;
  1789. unsigned long flags;
  1790. del_timer_sync(&conn->transport_timer);
  1791. spin_lock_bh(&session->lock);
  1792. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1793. if (session->leadconn == conn) {
  1794. /*
  1795. * leading connection? then give up on recovery.
  1796. */
  1797. session->state = ISCSI_STATE_TERMINATE;
  1798. wake_up(&conn->ehwait);
  1799. }
  1800. spin_unlock_bh(&session->lock);
  1801. /*
  1802. * Block until all in-progress commands for this connection
  1803. * time out or fail.
  1804. */
  1805. for (;;) {
  1806. spin_lock_irqsave(session->host->host_lock, flags);
  1807. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1808. spin_unlock_irqrestore(session->host->host_lock, flags);
  1809. break;
  1810. }
  1811. spin_unlock_irqrestore(session->host->host_lock, flags);
  1812. msleep_interruptible(500);
  1813. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  1814. "host_busy %d host_failed %d\n",
  1815. session->host->host_busy,
  1816. session->host->host_failed);
  1817. /*
  1818. * force eh_abort() to unblock
  1819. */
  1820. wake_up(&conn->ehwait);
  1821. }
  1822. /* flush queued up work because we free the connection below */
  1823. iscsi_suspend_tx(conn);
  1824. spin_lock_bh(&session->lock);
  1825. kfree(conn->data);
  1826. kfree(conn->persistent_address);
  1827. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1828. sizeof(void*));
  1829. if (session->leadconn == conn)
  1830. session->leadconn = NULL;
  1831. spin_unlock_bh(&session->lock);
  1832. iscsi_destroy_conn(cls_conn);
  1833. }
  1834. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1835. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1836. {
  1837. struct iscsi_conn *conn = cls_conn->dd_data;
  1838. struct iscsi_session *session = conn->session;
  1839. if (!session) {
  1840. iscsi_conn_printk(KERN_ERR, conn,
  1841. "can't start unbound connection\n");
  1842. return -EPERM;
  1843. }
  1844. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1845. session->first_burst > session->max_burst) {
  1846. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  1847. "first_burst %d max_burst %d\n",
  1848. session->first_burst, session->max_burst);
  1849. return -EINVAL;
  1850. }
  1851. if (conn->ping_timeout && !conn->recv_timeout) {
  1852. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  1853. "zero. Using 5 seconds\n.");
  1854. conn->recv_timeout = 5;
  1855. }
  1856. if (conn->recv_timeout && !conn->ping_timeout) {
  1857. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  1858. "zero. Using 5 seconds.\n");
  1859. conn->ping_timeout = 5;
  1860. }
  1861. spin_lock_bh(&session->lock);
  1862. conn->c_stage = ISCSI_CONN_STARTED;
  1863. session->state = ISCSI_STATE_LOGGED_IN;
  1864. session->queued_cmdsn = session->cmdsn;
  1865. conn->last_recv = jiffies;
  1866. conn->last_ping = jiffies;
  1867. if (conn->recv_timeout && conn->ping_timeout)
  1868. mod_timer(&conn->transport_timer,
  1869. jiffies + (conn->recv_timeout * HZ));
  1870. switch(conn->stop_stage) {
  1871. case STOP_CONN_RECOVER:
  1872. /*
  1873. * unblock eh_abort() if it is blocked. re-try all
  1874. * commands after successful recovery
  1875. */
  1876. conn->stop_stage = 0;
  1877. conn->tmf_state = TMF_INITIAL;
  1878. session->age++;
  1879. if (session->age == 16)
  1880. session->age = 0;
  1881. break;
  1882. case STOP_CONN_TERM:
  1883. conn->stop_stage = 0;
  1884. break;
  1885. default:
  1886. break;
  1887. }
  1888. spin_unlock_bh(&session->lock);
  1889. iscsi_unblock_session(session->cls_session);
  1890. wake_up(&conn->ehwait);
  1891. return 0;
  1892. }
  1893. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1894. static void
  1895. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1896. {
  1897. struct iscsi_mgmt_task *mtask, *tmp;
  1898. /* handle pending */
  1899. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1900. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1901. iscsi_free_mgmt_task(conn, mtask);
  1902. }
  1903. /* handle running */
  1904. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1905. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1906. iscsi_free_mgmt_task(conn, mtask);
  1907. }
  1908. conn->mtask = NULL;
  1909. }
  1910. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1911. struct iscsi_conn *conn, int flag)
  1912. {
  1913. int old_stop_stage;
  1914. del_timer_sync(&conn->transport_timer);
  1915. mutex_lock(&session->eh_mutex);
  1916. spin_lock_bh(&session->lock);
  1917. if (conn->stop_stage == STOP_CONN_TERM) {
  1918. spin_unlock_bh(&session->lock);
  1919. mutex_unlock(&session->eh_mutex);
  1920. return;
  1921. }
  1922. /*
  1923. * The LLD either freed/unset the lock on us, or userspace called
  1924. * stop but did not create a proper connection (connection was never
  1925. * bound or it was unbound then stop was called).
  1926. */
  1927. if (!conn->recv_lock) {
  1928. spin_unlock_bh(&session->lock);
  1929. mutex_unlock(&session->eh_mutex);
  1930. return;
  1931. }
  1932. /*
  1933. * When this is called for the in_login state, we only want to clean
  1934. * up the login task and connection. We do not need to block and set
  1935. * the recovery state again
  1936. */
  1937. if (flag == STOP_CONN_TERM)
  1938. session->state = ISCSI_STATE_TERMINATE;
  1939. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1940. session->state = ISCSI_STATE_IN_RECOVERY;
  1941. old_stop_stage = conn->stop_stage;
  1942. conn->stop_stage = flag;
  1943. conn->c_stage = ISCSI_CONN_STOPPED;
  1944. spin_unlock_bh(&session->lock);
  1945. iscsi_suspend_tx(conn);
  1946. write_lock_bh(conn->recv_lock);
  1947. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1948. write_unlock_bh(conn->recv_lock);
  1949. /*
  1950. * for connection level recovery we should not calculate
  1951. * header digest. conn->hdr_size used for optimization
  1952. * in hdr_extract() and will be re-negotiated at
  1953. * set_param() time.
  1954. */
  1955. if (flag == STOP_CONN_RECOVER) {
  1956. conn->hdrdgst_en = 0;
  1957. conn->datadgst_en = 0;
  1958. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1959. old_stop_stage != STOP_CONN_RECOVER) {
  1960. debug_scsi("blocking session\n");
  1961. iscsi_block_session(session->cls_session);
  1962. }
  1963. }
  1964. /*
  1965. * flush queues.
  1966. */
  1967. spin_lock_bh(&session->lock);
  1968. fail_all_commands(conn, -1,
  1969. STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
  1970. flush_control_queues(session, conn);
  1971. spin_unlock_bh(&session->lock);
  1972. mutex_unlock(&session->eh_mutex);
  1973. }
  1974. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1975. {
  1976. struct iscsi_conn *conn = cls_conn->dd_data;
  1977. struct iscsi_session *session = conn->session;
  1978. switch (flag) {
  1979. case STOP_CONN_RECOVER:
  1980. case STOP_CONN_TERM:
  1981. iscsi_start_session_recovery(session, conn, flag);
  1982. break;
  1983. default:
  1984. iscsi_conn_printk(KERN_ERR, conn,
  1985. "invalid stop flag %d\n", flag);
  1986. }
  1987. }
  1988. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1989. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1990. struct iscsi_cls_conn *cls_conn, int is_leading)
  1991. {
  1992. struct iscsi_session *session = cls_session->dd_data;
  1993. struct iscsi_conn *conn = cls_conn->dd_data;
  1994. spin_lock_bh(&session->lock);
  1995. if (is_leading)
  1996. session->leadconn = conn;
  1997. spin_unlock_bh(&session->lock);
  1998. /*
  1999. * Unblock xmitworker(), Login Phase will pass through.
  2000. */
  2001. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2002. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2003. return 0;
  2004. }
  2005. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2006. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2007. enum iscsi_param param, char *buf, int buflen)
  2008. {
  2009. struct iscsi_conn *conn = cls_conn->dd_data;
  2010. struct iscsi_session *session = conn->session;
  2011. uint32_t value;
  2012. switch(param) {
  2013. case ISCSI_PARAM_FAST_ABORT:
  2014. sscanf(buf, "%d", &session->fast_abort);
  2015. break;
  2016. case ISCSI_PARAM_ABORT_TMO:
  2017. sscanf(buf, "%d", &session->abort_timeout);
  2018. break;
  2019. case ISCSI_PARAM_LU_RESET_TMO:
  2020. sscanf(buf, "%d", &session->lu_reset_timeout);
  2021. break;
  2022. case ISCSI_PARAM_PING_TMO:
  2023. sscanf(buf, "%d", &conn->ping_timeout);
  2024. break;
  2025. case ISCSI_PARAM_RECV_TMO:
  2026. sscanf(buf, "%d", &conn->recv_timeout);
  2027. break;
  2028. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2029. sscanf(buf, "%d", &conn->max_recv_dlength);
  2030. break;
  2031. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2032. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2033. break;
  2034. case ISCSI_PARAM_HDRDGST_EN:
  2035. sscanf(buf, "%d", &conn->hdrdgst_en);
  2036. break;
  2037. case ISCSI_PARAM_DATADGST_EN:
  2038. sscanf(buf, "%d", &conn->datadgst_en);
  2039. break;
  2040. case ISCSI_PARAM_INITIAL_R2T_EN:
  2041. sscanf(buf, "%d", &session->initial_r2t_en);
  2042. break;
  2043. case ISCSI_PARAM_MAX_R2T:
  2044. sscanf(buf, "%d", &session->max_r2t);
  2045. break;
  2046. case ISCSI_PARAM_IMM_DATA_EN:
  2047. sscanf(buf, "%d", &session->imm_data_en);
  2048. break;
  2049. case ISCSI_PARAM_FIRST_BURST:
  2050. sscanf(buf, "%d", &session->first_burst);
  2051. break;
  2052. case ISCSI_PARAM_MAX_BURST:
  2053. sscanf(buf, "%d", &session->max_burst);
  2054. break;
  2055. case ISCSI_PARAM_PDU_INORDER_EN:
  2056. sscanf(buf, "%d", &session->pdu_inorder_en);
  2057. break;
  2058. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2059. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2060. break;
  2061. case ISCSI_PARAM_ERL:
  2062. sscanf(buf, "%d", &session->erl);
  2063. break;
  2064. case ISCSI_PARAM_IFMARKER_EN:
  2065. sscanf(buf, "%d", &value);
  2066. BUG_ON(value);
  2067. break;
  2068. case ISCSI_PARAM_OFMARKER_EN:
  2069. sscanf(buf, "%d", &value);
  2070. BUG_ON(value);
  2071. break;
  2072. case ISCSI_PARAM_EXP_STATSN:
  2073. sscanf(buf, "%u", &conn->exp_statsn);
  2074. break;
  2075. case ISCSI_PARAM_USERNAME:
  2076. kfree(session->username);
  2077. session->username = kstrdup(buf, GFP_KERNEL);
  2078. if (!session->username)
  2079. return -ENOMEM;
  2080. break;
  2081. case ISCSI_PARAM_USERNAME_IN:
  2082. kfree(session->username_in);
  2083. session->username_in = kstrdup(buf, GFP_KERNEL);
  2084. if (!session->username_in)
  2085. return -ENOMEM;
  2086. break;
  2087. case ISCSI_PARAM_PASSWORD:
  2088. kfree(session->password);
  2089. session->password = kstrdup(buf, GFP_KERNEL);
  2090. if (!session->password)
  2091. return -ENOMEM;
  2092. break;
  2093. case ISCSI_PARAM_PASSWORD_IN:
  2094. kfree(session->password_in);
  2095. session->password_in = kstrdup(buf, GFP_KERNEL);
  2096. if (!session->password_in)
  2097. return -ENOMEM;
  2098. break;
  2099. case ISCSI_PARAM_TARGET_NAME:
  2100. /* this should not change between logins */
  2101. if (session->targetname)
  2102. break;
  2103. session->targetname = kstrdup(buf, GFP_KERNEL);
  2104. if (!session->targetname)
  2105. return -ENOMEM;
  2106. break;
  2107. case ISCSI_PARAM_TPGT:
  2108. sscanf(buf, "%d", &session->tpgt);
  2109. break;
  2110. case ISCSI_PARAM_PERSISTENT_PORT:
  2111. sscanf(buf, "%d", &conn->persistent_port);
  2112. break;
  2113. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2114. /*
  2115. * this is the address returned in discovery so it should
  2116. * not change between logins.
  2117. */
  2118. if (conn->persistent_address)
  2119. break;
  2120. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  2121. if (!conn->persistent_address)
  2122. return -ENOMEM;
  2123. break;
  2124. default:
  2125. return -ENOSYS;
  2126. }
  2127. return 0;
  2128. }
  2129. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2130. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2131. enum iscsi_param param, char *buf)
  2132. {
  2133. struct iscsi_session *session = cls_session->dd_data;
  2134. int len;
  2135. switch(param) {
  2136. case ISCSI_PARAM_FAST_ABORT:
  2137. len = sprintf(buf, "%d\n", session->fast_abort);
  2138. break;
  2139. case ISCSI_PARAM_ABORT_TMO:
  2140. len = sprintf(buf, "%d\n", session->abort_timeout);
  2141. break;
  2142. case ISCSI_PARAM_LU_RESET_TMO:
  2143. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2144. break;
  2145. case ISCSI_PARAM_INITIAL_R2T_EN:
  2146. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2147. break;
  2148. case ISCSI_PARAM_MAX_R2T:
  2149. len = sprintf(buf, "%hu\n", session->max_r2t);
  2150. break;
  2151. case ISCSI_PARAM_IMM_DATA_EN:
  2152. len = sprintf(buf, "%d\n", session->imm_data_en);
  2153. break;
  2154. case ISCSI_PARAM_FIRST_BURST:
  2155. len = sprintf(buf, "%u\n", session->first_burst);
  2156. break;
  2157. case ISCSI_PARAM_MAX_BURST:
  2158. len = sprintf(buf, "%u\n", session->max_burst);
  2159. break;
  2160. case ISCSI_PARAM_PDU_INORDER_EN:
  2161. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2162. break;
  2163. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2164. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2165. break;
  2166. case ISCSI_PARAM_ERL:
  2167. len = sprintf(buf, "%d\n", session->erl);
  2168. break;
  2169. case ISCSI_PARAM_TARGET_NAME:
  2170. len = sprintf(buf, "%s\n", session->targetname);
  2171. break;
  2172. case ISCSI_PARAM_TPGT:
  2173. len = sprintf(buf, "%d\n", session->tpgt);
  2174. break;
  2175. case ISCSI_PARAM_USERNAME:
  2176. len = sprintf(buf, "%s\n", session->username);
  2177. break;
  2178. case ISCSI_PARAM_USERNAME_IN:
  2179. len = sprintf(buf, "%s\n", session->username_in);
  2180. break;
  2181. case ISCSI_PARAM_PASSWORD:
  2182. len = sprintf(buf, "%s\n", session->password);
  2183. break;
  2184. case ISCSI_PARAM_PASSWORD_IN:
  2185. len = sprintf(buf, "%s\n", session->password_in);
  2186. break;
  2187. default:
  2188. return -ENOSYS;
  2189. }
  2190. return len;
  2191. }
  2192. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2193. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2194. enum iscsi_param param, char *buf)
  2195. {
  2196. struct iscsi_conn *conn = cls_conn->dd_data;
  2197. int len;
  2198. switch(param) {
  2199. case ISCSI_PARAM_PING_TMO:
  2200. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2201. break;
  2202. case ISCSI_PARAM_RECV_TMO:
  2203. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2204. break;
  2205. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2206. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2207. break;
  2208. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2209. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2210. break;
  2211. case ISCSI_PARAM_HDRDGST_EN:
  2212. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2213. break;
  2214. case ISCSI_PARAM_DATADGST_EN:
  2215. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2216. break;
  2217. case ISCSI_PARAM_IFMARKER_EN:
  2218. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2219. break;
  2220. case ISCSI_PARAM_OFMARKER_EN:
  2221. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2222. break;
  2223. case ISCSI_PARAM_EXP_STATSN:
  2224. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2225. break;
  2226. case ISCSI_PARAM_PERSISTENT_PORT:
  2227. len = sprintf(buf, "%d\n", conn->persistent_port);
  2228. break;
  2229. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2230. len = sprintf(buf, "%s\n", conn->persistent_address);
  2231. break;
  2232. default:
  2233. return -ENOSYS;
  2234. }
  2235. return len;
  2236. }
  2237. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2238. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2239. char *buf)
  2240. {
  2241. struct iscsi_host *ihost = shost_priv(shost);
  2242. int len;
  2243. switch (param) {
  2244. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2245. if (!ihost->netdev)
  2246. len = sprintf(buf, "%s\n", "default");
  2247. else
  2248. len = sprintf(buf, "%s\n", ihost->netdev);
  2249. break;
  2250. case ISCSI_HOST_PARAM_HWADDRESS:
  2251. if (!ihost->hwaddress)
  2252. len = sprintf(buf, "%s\n", "default");
  2253. else
  2254. len = sprintf(buf, "%s\n", ihost->hwaddress);
  2255. break;
  2256. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2257. if (!ihost->initiatorname)
  2258. len = sprintf(buf, "%s\n", "unknown");
  2259. else
  2260. len = sprintf(buf, "%s\n", ihost->initiatorname);
  2261. break;
  2262. case ISCSI_HOST_PARAM_IPADDRESS:
  2263. if (!strlen(ihost->local_address))
  2264. len = sprintf(buf, "%s\n", "unknown");
  2265. else
  2266. len = sprintf(buf, "%s\n",
  2267. ihost->local_address);
  2268. default:
  2269. return -ENOSYS;
  2270. }
  2271. return len;
  2272. }
  2273. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2274. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2275. char *buf, int buflen)
  2276. {
  2277. struct iscsi_host *ihost = shost_priv(shost);
  2278. switch (param) {
  2279. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2280. if (!ihost->netdev)
  2281. ihost->netdev = kstrdup(buf, GFP_KERNEL);
  2282. break;
  2283. case ISCSI_HOST_PARAM_HWADDRESS:
  2284. if (!ihost->hwaddress)
  2285. ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
  2286. break;
  2287. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2288. if (!ihost->initiatorname)
  2289. ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
  2290. break;
  2291. default:
  2292. return -ENOSYS;
  2293. }
  2294. return 0;
  2295. }
  2296. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2297. MODULE_AUTHOR("Mike Christie");
  2298. MODULE_DESCRIPTION("iSCSI library functions");
  2299. MODULE_LICENSE("GPL");