libiscsi.c 70 KB

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