libiscsi.c 68 KB

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