libiscsi.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  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/mutex.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/delay.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/iscsi_proto.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_iscsi.h>
  38. #include <scsi/libiscsi.h>
  39. struct iscsi_session *
  40. class_to_transport_session(struct iscsi_cls_session *cls_session)
  41. {
  42. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  43. return iscsi_hostdata(shost->hostdata);
  44. }
  45. EXPORT_SYMBOL_GPL(class_to_transport_session);
  46. #define INVALID_SN_DELTA 0xffff
  47. int
  48. iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  49. {
  50. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  51. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  52. if (max_cmdsn < exp_cmdsn -1 &&
  53. max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
  54. return ISCSI_ERR_MAX_CMDSN;
  55. if (max_cmdsn > session->max_cmdsn ||
  56. max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
  57. session->max_cmdsn = max_cmdsn;
  58. if (exp_cmdsn > session->exp_cmdsn ||
  59. exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
  60. session->exp_cmdsn = exp_cmdsn;
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
  64. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  65. struct iscsi_data *hdr)
  66. {
  67. struct iscsi_conn *conn = ctask->conn;
  68. memset(hdr, 0, sizeof(struct iscsi_data));
  69. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  70. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  71. ctask->unsol_datasn++;
  72. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  73. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  74. hdr->itt = ctask->hdr->itt;
  75. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  76. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  77. if (ctask->unsol_count > conn->max_xmit_dlength) {
  78. hton24(hdr->dlength, conn->max_xmit_dlength);
  79. ctask->data_count = conn->max_xmit_dlength;
  80. ctask->unsol_offset += ctask->data_count;
  81. hdr->flags = 0;
  82. } else {
  83. hton24(hdr->dlength, ctask->unsol_count);
  84. ctask->data_count = ctask->unsol_count;
  85. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  86. }
  87. }
  88. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  89. /**
  90. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  91. * @ctask: iscsi cmd task
  92. *
  93. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  94. * fields like dlength or final based on how much data it sends
  95. */
  96. static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  97. {
  98. struct iscsi_conn *conn = ctask->conn;
  99. struct iscsi_session *session = conn->session;
  100. struct iscsi_cmd *hdr = ctask->hdr;
  101. struct scsi_cmnd *sc = ctask->sc;
  102. hdr->opcode = ISCSI_OP_SCSI_CMD;
  103. hdr->flags = ISCSI_ATTR_SIMPLE;
  104. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  105. hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
  106. (session->age << ISCSI_AGE_SHIFT);
  107. hdr->data_length = cpu_to_be32(sc->request_bufflen);
  108. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  109. session->cmdsn++;
  110. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  111. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  112. memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
  113. ctask->data_count = 0;
  114. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  115. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  116. /*
  117. * Write counters:
  118. *
  119. * imm_count bytes to be sent right after
  120. * SCSI PDU Header
  121. *
  122. * unsol_count bytes(as Data-Out) to be sent
  123. * without R2T ack right after
  124. * immediate data
  125. *
  126. * r2t_data_count bytes to be sent via R2T ack's
  127. *
  128. * pad_count bytes to be sent as zero-padding
  129. */
  130. ctask->imm_count = 0;
  131. ctask->unsol_count = 0;
  132. ctask->unsol_offset = 0;
  133. ctask->unsol_datasn = 0;
  134. if (session->imm_data_en) {
  135. if (ctask->total_length >= session->first_burst)
  136. ctask->imm_count = min(session->first_burst,
  137. conn->max_xmit_dlength);
  138. else
  139. ctask->imm_count = min(ctask->total_length,
  140. conn->max_xmit_dlength);
  141. hton24(ctask->hdr->dlength, ctask->imm_count);
  142. } else
  143. zero_data(ctask->hdr->dlength);
  144. if (!session->initial_r2t_en) {
  145. ctask->unsol_count = min(session->first_burst,
  146. ctask->total_length) - ctask->imm_count;
  147. ctask->unsol_offset = ctask->imm_count;
  148. }
  149. if (!ctask->unsol_count)
  150. /* No unsolicit Data-Out's */
  151. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  152. } else {
  153. ctask->datasn = 0;
  154. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  155. zero_data(hdr->dlength);
  156. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  157. hdr->flags |= ISCSI_FLAG_CMD_READ;
  158. }
  159. conn->scsicmd_pdus_cnt++;
  160. }
  161. EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
  162. /**
  163. * iscsi_complete_command - return command back to scsi-ml
  164. * @ctask: iscsi cmd task
  165. *
  166. * Must be called with session lock.
  167. * This function returns the scsi command to scsi-ml and returns
  168. * the cmd task to the pool of available cmd tasks.
  169. */
  170. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  171. {
  172. struct iscsi_session *session = ctask->conn->session;
  173. struct scsi_cmnd *sc = ctask->sc;
  174. ctask->state = ISCSI_TASK_COMPLETED;
  175. ctask->sc = NULL;
  176. /* SCSI eh reuses commands to verify us */
  177. sc->SCp.ptr = NULL;
  178. list_del_init(&ctask->running);
  179. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  180. sc->scsi_done(sc);
  181. }
  182. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  183. {
  184. atomic_inc(&ctask->refcount);
  185. }
  186. static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  187. {
  188. spin_lock_bh(&ctask->conn->session->lock);
  189. __iscsi_get_ctask(ctask);
  190. spin_unlock_bh(&ctask->conn->session->lock);
  191. }
  192. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  193. {
  194. if (atomic_dec_and_test(&ctask->refcount))
  195. iscsi_complete_command(ctask);
  196. }
  197. static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  198. {
  199. spin_lock_bh(&ctask->conn->session->lock);
  200. __iscsi_put_ctask(ctask);
  201. spin_unlock_bh(&ctask->conn->session->lock);
  202. }
  203. /**
  204. * iscsi_cmd_rsp - SCSI Command Response processing
  205. * @conn: iscsi connection
  206. * @hdr: iscsi header
  207. * @ctask: scsi command task
  208. * @data: cmd data buffer
  209. * @datalen: len of buffer
  210. *
  211. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  212. * then completes the command and task.
  213. **/
  214. static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  215. struct iscsi_cmd_task *ctask, char *data,
  216. int datalen)
  217. {
  218. int rc;
  219. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  220. struct iscsi_session *session = conn->session;
  221. struct scsi_cmnd *sc = ctask->sc;
  222. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  223. if (rc) {
  224. sc->result = DID_ERROR << 16;
  225. goto out;
  226. }
  227. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  228. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  229. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  230. sc->result = DID_ERROR << 16;
  231. goto out;
  232. }
  233. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  234. int senselen;
  235. if (datalen < 2) {
  236. invalid_datalen:
  237. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  238. "invalid data buffer size of %d\n", datalen);
  239. sc->result = DID_BAD_TARGET << 16;
  240. goto out;
  241. }
  242. senselen = (data[0] << 8) | data[1];
  243. if (datalen < senselen)
  244. goto invalid_datalen;
  245. memcpy(sc->sense_buffer, data + 2,
  246. min(senselen, SCSI_SENSE_BUFFERSIZE));
  247. debug_scsi("copied %d bytes of sense\n",
  248. min(senselen, SCSI_SENSE_BUFFERSIZE));
  249. }
  250. if (sc->sc_data_direction == DMA_TO_DEVICE)
  251. goto out;
  252. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  253. int res_count = be32_to_cpu(rhdr->residual_count);
  254. if (res_count > 0 && res_count <= sc->request_bufflen)
  255. sc->resid = res_count;
  256. else
  257. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  258. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  259. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  260. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  261. sc->resid = be32_to_cpu(rhdr->residual_count);
  262. out:
  263. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  264. (long)sc, sc->result, ctask->itt);
  265. conn->scsirsp_pdus_cnt++;
  266. __iscsi_put_ctask(ctask);
  267. return rc;
  268. }
  269. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  270. {
  271. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  272. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  273. conn->tmfrsp_pdus_cnt++;
  274. if (conn->tmabort_state != TMABORT_INITIAL)
  275. return;
  276. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  277. conn->tmabort_state = TMABORT_SUCCESS;
  278. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  279. conn->tmabort_state = TMABORT_NOT_FOUND;
  280. else
  281. conn->tmabort_state = TMABORT_FAILED;
  282. wake_up(&conn->ehwait);
  283. }
  284. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  285. char *data, int datalen)
  286. {
  287. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  288. struct iscsi_hdr rejected_pdu;
  289. uint32_t itt;
  290. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  291. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  292. if (ntoh24(reject->dlength) > datalen)
  293. return ISCSI_ERR_PROTO;
  294. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  295. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  296. itt = rejected_pdu.itt & ISCSI_ITT_MASK;
  297. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  298. "due to DataDigest error.\n", itt,
  299. rejected_pdu.opcode);
  300. }
  301. }
  302. return 0;
  303. }
  304. /**
  305. * __iscsi_complete_pdu - complete pdu
  306. * @conn: iscsi conn
  307. * @hdr: iscsi header
  308. * @data: data buffer
  309. * @datalen: len of data buffer
  310. *
  311. * Completes pdu processing by freeing any resources allocated at
  312. * queuecommand or send generic. session lock must be held and verify
  313. * itt must have been called.
  314. */
  315. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  316. char *data, int datalen)
  317. {
  318. struct iscsi_session *session = conn->session;
  319. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  320. struct iscsi_cmd_task *ctask;
  321. struct iscsi_mgmt_task *mtask;
  322. uint32_t itt;
  323. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
  324. itt = hdr->itt & ISCSI_ITT_MASK;
  325. else
  326. itt = hdr->itt;
  327. if (itt < session->cmds_max) {
  328. ctask = session->cmds[itt];
  329. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  330. opcode, conn->id, ctask->itt, datalen);
  331. switch(opcode) {
  332. case ISCSI_OP_SCSI_CMD_RSP:
  333. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  334. rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  335. datalen);
  336. break;
  337. case ISCSI_OP_SCSI_DATA_IN:
  338. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  339. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  340. conn->scsirsp_pdus_cnt++;
  341. __iscsi_put_ctask(ctask);
  342. }
  343. break;
  344. case ISCSI_OP_R2T:
  345. /* LLD handles this for now */
  346. break;
  347. default:
  348. rc = ISCSI_ERR_BAD_OPCODE;
  349. break;
  350. }
  351. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  352. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  353. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  354. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  355. opcode, conn->id, mtask->itt, datalen);
  356. rc = iscsi_check_assign_cmdsn(session,
  357. (struct iscsi_nopin*)hdr);
  358. if (rc)
  359. goto done;
  360. switch(opcode) {
  361. case ISCSI_OP_LOGOUT_RSP:
  362. if (datalen) {
  363. rc = ISCSI_ERR_PROTO;
  364. break;
  365. }
  366. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  367. /* fall through */
  368. case ISCSI_OP_LOGIN_RSP:
  369. case ISCSI_OP_TEXT_RSP:
  370. /*
  371. * login related PDU's exp_statsn is handled in
  372. * userspace
  373. */
  374. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  375. rc = ISCSI_ERR_CONN_FAILED;
  376. list_del(&mtask->running);
  377. if (conn->login_mtask != mtask)
  378. __kfifo_put(session->mgmtpool.queue,
  379. (void*)&mtask, sizeof(void*));
  380. break;
  381. case ISCSI_OP_SCSI_TMFUNC_RSP:
  382. if (datalen) {
  383. rc = ISCSI_ERR_PROTO;
  384. break;
  385. }
  386. iscsi_tmf_rsp(conn, hdr);
  387. break;
  388. case ISCSI_OP_NOOP_IN:
  389. if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
  390. rc = ISCSI_ERR_PROTO;
  391. break;
  392. }
  393. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  394. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  395. rc = ISCSI_ERR_CONN_FAILED;
  396. list_del(&mtask->running);
  397. if (conn->login_mtask != mtask)
  398. __kfifo_put(session->mgmtpool.queue,
  399. (void*)&mtask, sizeof(void*));
  400. break;
  401. default:
  402. rc = ISCSI_ERR_BAD_OPCODE;
  403. break;
  404. }
  405. } else if (itt == ISCSI_RESERVED_TAG) {
  406. rc = iscsi_check_assign_cmdsn(session,
  407. (struct iscsi_nopin*)hdr);
  408. if (rc)
  409. goto done;
  410. switch(opcode) {
  411. case ISCSI_OP_NOOP_IN:
  412. if (datalen) {
  413. rc = ISCSI_ERR_PROTO;
  414. break;
  415. }
  416. if (hdr->ttt == ISCSI_RESERVED_TAG)
  417. break;
  418. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  419. rc = ISCSI_ERR_CONN_FAILED;
  420. break;
  421. case ISCSI_OP_REJECT:
  422. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  423. break;
  424. case ISCSI_OP_ASYNC_EVENT:
  425. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  426. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  427. rc = ISCSI_ERR_CONN_FAILED;
  428. break;
  429. default:
  430. rc = ISCSI_ERR_BAD_OPCODE;
  431. break;
  432. }
  433. } else
  434. rc = ISCSI_ERR_BAD_ITT;
  435. done:
  436. return rc;
  437. }
  438. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  439. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  440. char *data, int datalen)
  441. {
  442. int rc;
  443. spin_lock(&conn->session->lock);
  444. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  445. spin_unlock(&conn->session->lock);
  446. return rc;
  447. }
  448. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  449. /* verify itt (itt encoding: age+cid+itt) */
  450. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  451. uint32_t *ret_itt)
  452. {
  453. struct iscsi_session *session = conn->session;
  454. struct iscsi_cmd_task *ctask;
  455. uint32_t itt;
  456. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  457. if ((hdr->itt & ISCSI_AGE_MASK) !=
  458. (session->age << ISCSI_AGE_SHIFT)) {
  459. printk(KERN_ERR "iscsi: received itt %x expected "
  460. "session age (%x)\n", hdr->itt,
  461. session->age & ISCSI_AGE_MASK);
  462. return ISCSI_ERR_BAD_ITT;
  463. }
  464. if ((hdr->itt & ISCSI_CID_MASK) !=
  465. (conn->id << ISCSI_CID_SHIFT)) {
  466. printk(KERN_ERR "iscsi: received itt %x, expected "
  467. "CID (%x)\n", hdr->itt, conn->id);
  468. return ISCSI_ERR_BAD_ITT;
  469. }
  470. itt = hdr->itt & ISCSI_ITT_MASK;
  471. } else
  472. itt = hdr->itt;
  473. if (itt < session->cmds_max) {
  474. ctask = session->cmds[itt];
  475. if (!ctask->sc) {
  476. printk(KERN_INFO "iscsi: dropping ctask with "
  477. "itt 0x%x\n", ctask->itt);
  478. /* force drop */
  479. return ISCSI_ERR_NO_SCSI_CMD;
  480. }
  481. if (ctask->sc->SCp.phase != session->age) {
  482. printk(KERN_ERR "iscsi: ctask's session age %d, "
  483. "expected %d\n", ctask->sc->SCp.phase,
  484. session->age);
  485. return ISCSI_ERR_SESSION_FAILED;
  486. }
  487. }
  488. *ret_itt = itt;
  489. return 0;
  490. }
  491. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  492. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  493. {
  494. struct iscsi_session *session = conn->session;
  495. unsigned long flags;
  496. spin_lock_irqsave(&session->lock, flags);
  497. if (session->state == ISCSI_STATE_FAILED) {
  498. spin_unlock_irqrestore(&session->lock, flags);
  499. return;
  500. }
  501. if (conn->stop_stage == 0)
  502. session->state = ISCSI_STATE_FAILED;
  503. spin_unlock_irqrestore(&session->lock, flags);
  504. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  505. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  506. iscsi_conn_error(conn->cls_conn, err);
  507. }
  508. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  509. static int iscsi_xmit_imm_task(struct iscsi_conn *conn)
  510. {
  511. struct iscsi_hdr *hdr = conn->mtask->hdr;
  512. int rc, was_logout = 0;
  513. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
  514. conn->session->state = ISCSI_STATE_IN_RECOVERY;
  515. iscsi_block_session(session_to_cls(conn->session));
  516. was_logout = 1;
  517. }
  518. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  519. if (rc)
  520. return rc;
  521. if (was_logout) {
  522. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  523. return -ENODATA;
  524. }
  525. return 0;
  526. }
  527. /**
  528. * iscsi_data_xmit - xmit any command into the scheduled connection
  529. * @conn: iscsi connection
  530. *
  531. * Notes:
  532. * The function can return -EAGAIN in which case the caller must
  533. * re-schedule it again later or recover. '0' return code means
  534. * successful xmit.
  535. **/
  536. static int iscsi_data_xmit(struct iscsi_conn *conn)
  537. {
  538. struct iscsi_transport *tt;
  539. int rc = 0;
  540. if (unlikely(conn->suspend_tx)) {
  541. debug_scsi("conn %d Tx suspended!\n", conn->id);
  542. return -ENODATA;
  543. }
  544. tt = conn->session->tt;
  545. /*
  546. * Transmit in the following order:
  547. *
  548. * 1) un-finished xmit (ctask or mtask)
  549. * 2) immediate control PDUs
  550. * 3) write data
  551. * 4) SCSI commands
  552. * 5) non-immediate control PDUs
  553. *
  554. * No need to lock around __kfifo_get as long as
  555. * there's one producer and one consumer.
  556. */
  557. BUG_ON(conn->ctask && conn->mtask);
  558. if (conn->ctask) {
  559. iscsi_get_ctask(conn->ctask);
  560. rc = tt->xmit_cmd_task(conn, conn->ctask);
  561. iscsi_put_ctask(conn->ctask);
  562. if (rc)
  563. goto again;
  564. /* done with this in-progress ctask */
  565. conn->ctask = NULL;
  566. }
  567. if (conn->mtask) {
  568. rc = iscsi_xmit_imm_task(conn);
  569. if (rc)
  570. goto again;
  571. /* done with this in-progress mtask */
  572. conn->mtask = NULL;
  573. }
  574. /* process immediate first */
  575. if (unlikely(__kfifo_len(conn->immqueue))) {
  576. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  577. sizeof(void*))) {
  578. spin_lock_bh(&conn->session->lock);
  579. list_add_tail(&conn->mtask->running,
  580. &conn->mgmt_run_list);
  581. spin_unlock_bh(&conn->session->lock);
  582. rc = iscsi_xmit_imm_task(conn);
  583. if (rc)
  584. goto again;
  585. }
  586. /* done with this mtask */
  587. conn->mtask = NULL;
  588. }
  589. /* process command queue */
  590. spin_lock_bh(&conn->session->lock);
  591. while (!list_empty(&conn->xmitqueue)) {
  592. /*
  593. * iscsi tcp may readd the task to the xmitqueue to send
  594. * write data
  595. */
  596. conn->ctask = list_entry(conn->xmitqueue.next,
  597. struct iscsi_cmd_task, running);
  598. conn->ctask->state = ISCSI_TASK_RUNNING;
  599. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  600. __iscsi_get_ctask(conn->ctask);
  601. spin_unlock_bh(&conn->session->lock);
  602. rc = tt->xmit_cmd_task(conn, conn->ctask);
  603. spin_lock_bh(&conn->session->lock);
  604. __iscsi_put_ctask(conn->ctask);
  605. if (rc) {
  606. spin_unlock_bh(&conn->session->lock);
  607. goto again;
  608. }
  609. }
  610. spin_unlock_bh(&conn->session->lock);
  611. /* done with this ctask */
  612. conn->ctask = NULL;
  613. /* process the rest control plane PDUs, if any */
  614. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  615. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  616. sizeof(void*))) {
  617. spin_lock_bh(&conn->session->lock);
  618. list_add_tail(&conn->mtask->running,
  619. &conn->mgmt_run_list);
  620. spin_unlock_bh(&conn->session->lock);
  621. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  622. if (rc)
  623. goto again;
  624. }
  625. /* done with this mtask */
  626. conn->mtask = NULL;
  627. }
  628. return -ENODATA;
  629. again:
  630. if (unlikely(conn->suspend_tx))
  631. return -ENODATA;
  632. return rc;
  633. }
  634. static void iscsi_xmitworker(struct work_struct *work)
  635. {
  636. struct iscsi_conn *conn =
  637. container_of(work, struct iscsi_conn, xmitwork);
  638. int rc;
  639. /*
  640. * serialize Xmit worker on a per-connection basis.
  641. */
  642. mutex_lock(&conn->xmitmutex);
  643. do {
  644. rc = iscsi_data_xmit(conn);
  645. } while (rc >= 0 || rc == -EAGAIN);
  646. mutex_unlock(&conn->xmitmutex);
  647. }
  648. enum {
  649. FAILURE_BAD_HOST = 1,
  650. FAILURE_SESSION_FAILED,
  651. FAILURE_SESSION_FREED,
  652. FAILURE_WINDOW_CLOSED,
  653. FAILURE_OOM,
  654. FAILURE_SESSION_TERMINATE,
  655. FAILURE_SESSION_IN_RECOVERY,
  656. FAILURE_SESSION_RECOVERY_TIMEOUT,
  657. };
  658. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  659. {
  660. struct Scsi_Host *host;
  661. int reason = 0;
  662. struct iscsi_session *session;
  663. struct iscsi_conn *conn;
  664. struct iscsi_cmd_task *ctask = NULL;
  665. sc->scsi_done = done;
  666. sc->result = 0;
  667. sc->SCp.ptr = NULL;
  668. host = sc->device->host;
  669. session = iscsi_hostdata(host->hostdata);
  670. spin_lock(&session->lock);
  671. /*
  672. * ISCSI_STATE_FAILED is a temp. state. The recovery
  673. * code will decide what is best to do with command queued
  674. * during this time
  675. */
  676. if (session->state != ISCSI_STATE_LOGGED_IN &&
  677. session->state != ISCSI_STATE_FAILED) {
  678. /*
  679. * to handle the race between when we set the recovery state
  680. * and block the session we requeue here (commands could
  681. * be entering our queuecommand while a block is starting
  682. * up because the block code is not locked)
  683. */
  684. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  685. reason = FAILURE_SESSION_IN_RECOVERY;
  686. goto reject;
  687. }
  688. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  689. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  690. else if (session->state == ISCSI_STATE_TERMINATE)
  691. reason = FAILURE_SESSION_TERMINATE;
  692. else
  693. reason = FAILURE_SESSION_FREED;
  694. goto fault;
  695. }
  696. /*
  697. * Check for iSCSI window and take care of CmdSN wrap-around
  698. */
  699. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  700. reason = FAILURE_WINDOW_CLOSED;
  701. goto reject;
  702. }
  703. conn = session->leadconn;
  704. if (!conn) {
  705. reason = FAILURE_SESSION_FREED;
  706. goto fault;
  707. }
  708. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  709. sizeof(void*))) {
  710. reason = FAILURE_OOM;
  711. goto reject;
  712. }
  713. sc->SCp.phase = session->age;
  714. sc->SCp.ptr = (char *)ctask;
  715. atomic_set(&ctask->refcount, 1);
  716. ctask->state = ISCSI_TASK_PENDING;
  717. ctask->mtask = NULL;
  718. ctask->conn = conn;
  719. ctask->sc = sc;
  720. INIT_LIST_HEAD(&ctask->running);
  721. ctask->total_length = sc->request_bufflen;
  722. iscsi_prep_scsi_cmd_pdu(ctask);
  723. session->tt->init_cmd_task(ctask);
  724. list_add_tail(&ctask->running, &conn->xmitqueue);
  725. debug_scsi(
  726. "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
  727. "win %d]\n",
  728. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  729. conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
  730. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  731. spin_unlock(&session->lock);
  732. scsi_queue_work(host, &conn->xmitwork);
  733. return 0;
  734. reject:
  735. spin_unlock(&session->lock);
  736. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  737. return SCSI_MLQUEUE_HOST_BUSY;
  738. fault:
  739. spin_unlock(&session->lock);
  740. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  741. sc->cmnd[0], reason);
  742. sc->result = (DID_NO_CONNECT << 16);
  743. sc->resid = sc->request_bufflen;
  744. sc->scsi_done(sc);
  745. return 0;
  746. }
  747. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  748. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  749. {
  750. if (depth > ISCSI_MAX_CMD_PER_LUN)
  751. depth = ISCSI_MAX_CMD_PER_LUN;
  752. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  753. return sdev->queue_depth;
  754. }
  755. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  756. static int
  757. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  758. char *data, uint32_t data_size)
  759. {
  760. struct iscsi_session *session = conn->session;
  761. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  762. struct iscsi_mgmt_task *mtask;
  763. spin_lock_bh(&session->lock);
  764. if (session->state == ISCSI_STATE_TERMINATE) {
  765. spin_unlock_bh(&session->lock);
  766. return -EPERM;
  767. }
  768. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  769. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  770. /*
  771. * Login and Text are sent serially, in
  772. * request-followed-by-response sequence.
  773. * Same mtask can be used. Same ITT must be used.
  774. * Note that login_mtask is preallocated at conn_create().
  775. */
  776. mtask = conn->login_mtask;
  777. else {
  778. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  779. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  780. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  781. if (!__kfifo_get(session->mgmtpool.queue,
  782. (void*)&mtask, sizeof(void*))) {
  783. spin_unlock_bh(&session->lock);
  784. return -ENOSPC;
  785. }
  786. }
  787. /*
  788. * pre-format CmdSN for outgoing PDU.
  789. */
  790. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  791. hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
  792. (session->age << ISCSI_AGE_SHIFT);
  793. nop->cmdsn = cpu_to_be32(session->cmdsn);
  794. if (conn->c_stage == ISCSI_CONN_STARTED &&
  795. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  796. session->cmdsn++;
  797. } else
  798. /* do not advance CmdSN */
  799. nop->cmdsn = cpu_to_be32(session->cmdsn);
  800. if (data_size) {
  801. memcpy(mtask->data, data, data_size);
  802. mtask->data_count = data_size;
  803. } else
  804. mtask->data_count = 0;
  805. INIT_LIST_HEAD(&mtask->running);
  806. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  807. if (session->tt->init_mgmt_task)
  808. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  809. spin_unlock_bh(&session->lock);
  810. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  811. hdr->opcode, hdr->itt, data_size);
  812. /*
  813. * since send_pdu() could be called at least from two contexts,
  814. * we need to serialize __kfifo_put, so we don't have to take
  815. * additional lock on fast data-path
  816. */
  817. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  818. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  819. else
  820. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  821. scsi_queue_work(session->host, &conn->xmitwork);
  822. return 0;
  823. }
  824. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  825. char *data, uint32_t data_size)
  826. {
  827. struct iscsi_conn *conn = cls_conn->dd_data;
  828. int rc;
  829. mutex_lock(&conn->xmitmutex);
  830. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  831. mutex_unlock(&conn->xmitmutex);
  832. return rc;
  833. }
  834. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  835. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  836. {
  837. struct iscsi_session *session = class_to_transport_session(cls_session);
  838. struct iscsi_conn *conn = session->leadconn;
  839. spin_lock_bh(&session->lock);
  840. if (session->state != ISCSI_STATE_LOGGED_IN) {
  841. session->state = ISCSI_STATE_RECOVERY_FAILED;
  842. if (conn)
  843. wake_up(&conn->ehwait);
  844. }
  845. spin_unlock_bh(&session->lock);
  846. }
  847. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  848. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  849. {
  850. struct Scsi_Host *host = sc->device->host;
  851. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  852. struct iscsi_conn *conn = session->leadconn;
  853. int fail_session = 0;
  854. spin_lock_bh(&session->lock);
  855. if (session->state == ISCSI_STATE_TERMINATE) {
  856. failed:
  857. debug_scsi("failing host reset: session terminated "
  858. "[CID %d age %d]\n", conn->id, session->age);
  859. spin_unlock_bh(&session->lock);
  860. return FAILED;
  861. }
  862. if (sc->SCp.phase == session->age) {
  863. debug_scsi("failing connection CID %d due to SCSI host reset\n",
  864. conn->id);
  865. fail_session = 1;
  866. }
  867. spin_unlock_bh(&session->lock);
  868. /*
  869. * we drop the lock here but the leadconn cannot be destoyed while
  870. * we are in the scsi eh
  871. */
  872. if (fail_session)
  873. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  874. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  875. wait_event_interruptible(conn->ehwait,
  876. session->state == ISCSI_STATE_TERMINATE ||
  877. session->state == ISCSI_STATE_LOGGED_IN ||
  878. session->state == ISCSI_STATE_RECOVERY_FAILED);
  879. if (signal_pending(current))
  880. flush_signals(current);
  881. spin_lock_bh(&session->lock);
  882. if (session->state == ISCSI_STATE_LOGGED_IN)
  883. printk(KERN_INFO "iscsi: host reset succeeded\n");
  884. else
  885. goto failed;
  886. spin_unlock_bh(&session->lock);
  887. return SUCCESS;
  888. }
  889. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  890. static void iscsi_tmabort_timedout(unsigned long data)
  891. {
  892. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  893. struct iscsi_conn *conn = ctask->conn;
  894. struct iscsi_session *session = conn->session;
  895. spin_lock(&session->lock);
  896. if (conn->tmabort_state == TMABORT_INITIAL) {
  897. conn->tmabort_state = TMABORT_TIMEDOUT;
  898. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  899. ctask->sc, ctask->itt);
  900. /* unblock eh_abort() */
  901. wake_up(&conn->ehwait);
  902. }
  903. spin_unlock(&session->lock);
  904. }
  905. /* must be called with the mutex lock */
  906. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  907. struct iscsi_cmd_task *ctask)
  908. {
  909. struct iscsi_conn *conn = ctask->conn;
  910. struct iscsi_session *session = conn->session;
  911. struct iscsi_tm *hdr = &conn->tmhdr;
  912. int rc;
  913. /*
  914. * ctask timed out but session is OK requests must be serialized.
  915. */
  916. memset(hdr, 0, sizeof(struct iscsi_tm));
  917. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  918. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  919. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  920. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  921. hdr->rtt = ctask->hdr->itt;
  922. hdr->refcmdsn = ctask->hdr->cmdsn;
  923. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  924. NULL, 0);
  925. if (rc) {
  926. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  927. debug_scsi("abort sent failure [itt 0x%x] %d\n", ctask->itt,
  928. rc);
  929. return rc;
  930. }
  931. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  932. spin_lock_bh(&session->lock);
  933. ctask->mtask = (struct iscsi_mgmt_task *)
  934. session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
  935. ISCSI_MGMT_ITT_OFFSET];
  936. if (conn->tmabort_state == TMABORT_INITIAL) {
  937. conn->tmfcmd_pdus_cnt++;
  938. conn->tmabort_timer.expires = 10*HZ + jiffies;
  939. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  940. conn->tmabort_timer.data = (unsigned long)ctask;
  941. add_timer(&conn->tmabort_timer);
  942. debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
  943. }
  944. spin_unlock_bh(&session->lock);
  945. mutex_unlock(&conn->xmitmutex);
  946. /*
  947. * block eh thread until:
  948. *
  949. * 1) abort response
  950. * 2) abort timeout
  951. * 3) session is terminated or restarted or userspace has
  952. * given up on recovery
  953. */
  954. wait_event_interruptible(conn->ehwait,
  955. sc->SCp.phase != session->age ||
  956. session->state != ISCSI_STATE_LOGGED_IN ||
  957. conn->tmabort_state != TMABORT_INITIAL);
  958. if (signal_pending(current))
  959. flush_signals(current);
  960. del_timer_sync(&conn->tmabort_timer);
  961. mutex_lock(&conn->xmitmutex);
  962. return 0;
  963. }
  964. /*
  965. * xmit mutex and session lock must be held
  966. */
  967. static struct iscsi_mgmt_task *
  968. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  969. {
  970. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  971. struct iscsi_mgmt_task *task;
  972. debug_scsi("searching %d tasks\n", nr_tasks);
  973. for (i = 0; i < nr_tasks; i++) {
  974. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  975. debug_scsi("check task %u\n", task->itt);
  976. if (task->itt == itt) {
  977. debug_scsi("matched task\n");
  978. return task;
  979. }
  980. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  981. }
  982. return NULL;
  983. }
  984. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  985. {
  986. struct iscsi_conn *conn = ctask->conn;
  987. struct iscsi_session *session = conn->session;
  988. if (!ctask->mtask)
  989. return -EINVAL;
  990. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  991. list_del(&ctask->mtask->running);
  992. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  993. sizeof(void*));
  994. ctask->mtask = NULL;
  995. return 0;
  996. }
  997. /*
  998. * session lock and xmitmutex must be held
  999. */
  1000. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1001. int err)
  1002. {
  1003. struct scsi_cmnd *sc;
  1004. sc = ctask->sc;
  1005. if (!sc)
  1006. return;
  1007. conn->session->tt->cleanup_cmd_task(conn, ctask);
  1008. iscsi_ctask_mtask_cleanup(ctask);
  1009. sc->result = err;
  1010. sc->resid = sc->request_bufflen;
  1011. /* release ref from queuecommand */
  1012. __iscsi_put_ctask(ctask);
  1013. }
  1014. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1015. {
  1016. struct iscsi_cmd_task *ctask;
  1017. struct iscsi_conn *conn;
  1018. struct iscsi_session *session;
  1019. int rc;
  1020. /*
  1021. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1022. * got the command.
  1023. */
  1024. if (!sc->SCp.ptr) {
  1025. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1026. return SUCCESS;
  1027. }
  1028. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1029. conn = ctask->conn;
  1030. session = conn->session;
  1031. conn->eh_abort_cnt++;
  1032. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1033. mutex_lock(&conn->xmitmutex);
  1034. spin_lock_bh(&session->lock);
  1035. /*
  1036. * If we are not logged in or we have started a new session
  1037. * then let the host reset code handle this
  1038. */
  1039. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1040. sc->SCp.phase != session->age)
  1041. goto failed;
  1042. /* ctask completed before time out */
  1043. if (!ctask->sc) {
  1044. spin_unlock_bh(&session->lock);
  1045. debug_scsi("sc completed while abort in progress\n");
  1046. goto success_rel_mutex;
  1047. }
  1048. /* what should we do here ? */
  1049. if (conn->ctask == ctask) {
  1050. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1051. "Failing abort\n", sc, ctask->itt);
  1052. goto failed;
  1053. }
  1054. if (ctask->state == ISCSI_TASK_PENDING)
  1055. goto success_cleanup;
  1056. conn->tmabort_state = TMABORT_INITIAL;
  1057. spin_unlock_bh(&session->lock);
  1058. rc = iscsi_exec_abort_task(sc, ctask);
  1059. spin_lock_bh(&session->lock);
  1060. if (rc || sc->SCp.phase != session->age ||
  1061. session->state != ISCSI_STATE_LOGGED_IN)
  1062. goto failed;
  1063. iscsi_ctask_mtask_cleanup(ctask);
  1064. switch (conn->tmabort_state) {
  1065. case TMABORT_SUCCESS:
  1066. goto success_cleanup;
  1067. case TMABORT_NOT_FOUND:
  1068. if (!ctask->sc) {
  1069. /* ctask completed before tmf abort response */
  1070. spin_unlock_bh(&session->lock);
  1071. debug_scsi("sc completed while abort in progress\n");
  1072. goto success_rel_mutex;
  1073. }
  1074. /* fall through */
  1075. default:
  1076. /* timedout or failed */
  1077. spin_unlock_bh(&session->lock);
  1078. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1079. spin_lock_bh(&session->lock);
  1080. goto failed;
  1081. }
  1082. success_cleanup:
  1083. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1084. spin_unlock_bh(&session->lock);
  1085. /*
  1086. * clean up task if aborted. we have the xmitmutex so grab
  1087. * the recv lock as a writer
  1088. */
  1089. write_lock_bh(conn->recv_lock);
  1090. spin_lock(&session->lock);
  1091. fail_command(conn, ctask, DID_ABORT << 16);
  1092. spin_unlock(&session->lock);
  1093. write_unlock_bh(conn->recv_lock);
  1094. success_rel_mutex:
  1095. mutex_unlock(&conn->xmitmutex);
  1096. return SUCCESS;
  1097. failed:
  1098. spin_unlock_bh(&session->lock);
  1099. mutex_unlock(&conn->xmitmutex);
  1100. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1101. return FAILED;
  1102. }
  1103. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1104. int
  1105. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1106. {
  1107. int i;
  1108. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1109. if (*items == NULL)
  1110. return -ENOMEM;
  1111. q->max = max;
  1112. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1113. if (q->pool == NULL) {
  1114. kfree(*items);
  1115. return -ENOMEM;
  1116. }
  1117. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1118. GFP_KERNEL, NULL);
  1119. if (q->queue == ERR_PTR(-ENOMEM)) {
  1120. kfree(q->pool);
  1121. kfree(*items);
  1122. return -ENOMEM;
  1123. }
  1124. for (i = 0; i < max; i++) {
  1125. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1126. if (q->pool[i] == NULL) {
  1127. int j;
  1128. for (j = 0; j < i; j++)
  1129. kfree(q->pool[j]);
  1130. kfifo_free(q->queue);
  1131. kfree(q->pool);
  1132. kfree(*items);
  1133. return -ENOMEM;
  1134. }
  1135. memset(q->pool[i], 0, item_size);
  1136. (*items)[i] = q->pool[i];
  1137. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1138. }
  1139. return 0;
  1140. }
  1141. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1142. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1143. {
  1144. int i;
  1145. for (i = 0; i < q->max; i++)
  1146. kfree(items[i]);
  1147. kfree(q->pool);
  1148. kfree(items);
  1149. }
  1150. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1151. /*
  1152. * iSCSI Session's hostdata organization:
  1153. *
  1154. * *------------------* <== hostdata_session(host->hostdata)
  1155. * | ptr to class sess|
  1156. * |------------------| <== iscsi_hostdata(host->hostdata)
  1157. * | iscsi_session |
  1158. * *------------------*
  1159. */
  1160. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1161. _sz % sizeof(unsigned long))
  1162. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1163. /**
  1164. * iscsi_session_setup - create iscsi cls session and host and session
  1165. * @scsit: scsi transport template
  1166. * @iscsit: iscsi transport template
  1167. * @initial_cmdsn: initial CmdSN
  1168. * @hostno: host no allocated
  1169. *
  1170. * This can be used by software iscsi_transports that allocate
  1171. * a session per scsi host.
  1172. **/
  1173. struct iscsi_cls_session *
  1174. iscsi_session_setup(struct iscsi_transport *iscsit,
  1175. struct scsi_transport_template *scsit,
  1176. int cmd_task_size, int mgmt_task_size,
  1177. uint32_t initial_cmdsn, uint32_t *hostno)
  1178. {
  1179. struct Scsi_Host *shost;
  1180. struct iscsi_session *session;
  1181. struct iscsi_cls_session *cls_session;
  1182. int cmd_i;
  1183. shost = scsi_host_alloc(iscsit->host_template,
  1184. hostdata_privsize(sizeof(*session)));
  1185. if (!shost)
  1186. return NULL;
  1187. shost->max_id = 1;
  1188. shost->max_channel = 0;
  1189. shost->max_lun = iscsit->max_lun;
  1190. shost->max_cmd_len = iscsit->max_cmd_len;
  1191. shost->transportt = scsit;
  1192. shost->transportt->create_work_queue = 1;
  1193. *hostno = shost->host_no;
  1194. session = iscsi_hostdata(shost->hostdata);
  1195. memset(session, 0, sizeof(struct iscsi_session));
  1196. session->host = shost;
  1197. session->state = ISCSI_STATE_FREE;
  1198. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1199. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1200. session->cmdsn = initial_cmdsn;
  1201. session->exp_cmdsn = initial_cmdsn + 1;
  1202. session->max_cmdsn = initial_cmdsn + 1;
  1203. session->max_r2t = 1;
  1204. session->tt = iscsit;
  1205. /* initialize SCSI PDU commands pool */
  1206. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1207. (void***)&session->cmds,
  1208. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1209. goto cmdpool_alloc_fail;
  1210. /* pre-format cmds pool with ITT */
  1211. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1212. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1213. if (cmd_task_size)
  1214. ctask->dd_data = &ctask[1];
  1215. ctask->itt = cmd_i;
  1216. INIT_LIST_HEAD(&ctask->running);
  1217. }
  1218. spin_lock_init(&session->lock);
  1219. /* initialize immediate command pool */
  1220. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1221. (void***)&session->mgmt_cmds,
  1222. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1223. goto mgmtpool_alloc_fail;
  1224. /* pre-format immediate cmds pool with ITT */
  1225. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1226. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1227. if (mgmt_task_size)
  1228. mtask->dd_data = &mtask[1];
  1229. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1230. INIT_LIST_HEAD(&mtask->running);
  1231. }
  1232. if (scsi_add_host(shost, NULL))
  1233. goto add_host_fail;
  1234. if (!try_module_get(iscsit->owner))
  1235. goto cls_session_fail;
  1236. cls_session = iscsi_create_session(shost, iscsit, 0);
  1237. if (!cls_session)
  1238. goto module_put;
  1239. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1240. return cls_session;
  1241. module_put:
  1242. module_put(iscsit->owner);
  1243. cls_session_fail:
  1244. scsi_remove_host(shost);
  1245. add_host_fail:
  1246. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1247. mgmtpool_alloc_fail:
  1248. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1249. cmdpool_alloc_fail:
  1250. scsi_host_put(shost);
  1251. return NULL;
  1252. }
  1253. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1254. /**
  1255. * iscsi_session_teardown - destroy session, host, and cls_session
  1256. * shost: scsi host
  1257. *
  1258. * This can be used by software iscsi_transports that allocate
  1259. * a session per scsi host.
  1260. **/
  1261. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1262. {
  1263. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1264. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1265. struct module *owner = cls_session->transport->owner;
  1266. scsi_remove_host(shost);
  1267. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1268. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1269. kfree(session->targetname);
  1270. iscsi_destroy_session(cls_session);
  1271. scsi_host_put(shost);
  1272. module_put(owner);
  1273. }
  1274. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1275. /**
  1276. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1277. * @cls_session: iscsi_cls_session
  1278. * @conn_idx: cid
  1279. **/
  1280. struct iscsi_cls_conn *
  1281. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1282. {
  1283. struct iscsi_session *session = class_to_transport_session(cls_session);
  1284. struct iscsi_conn *conn;
  1285. struct iscsi_cls_conn *cls_conn;
  1286. char *data;
  1287. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1288. if (!cls_conn)
  1289. return NULL;
  1290. conn = cls_conn->dd_data;
  1291. memset(conn, 0, sizeof(*conn));
  1292. conn->session = session;
  1293. conn->cls_conn = cls_conn;
  1294. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1295. conn->id = conn_idx;
  1296. conn->exp_statsn = 0;
  1297. conn->tmabort_state = TMABORT_INITIAL;
  1298. INIT_LIST_HEAD(&conn->run_list);
  1299. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1300. INIT_LIST_HEAD(&conn->xmitqueue);
  1301. /* initialize general immediate & non-immediate PDU commands queue */
  1302. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1303. GFP_KERNEL, NULL);
  1304. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1305. goto immqueue_alloc_fail;
  1306. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1307. GFP_KERNEL, NULL);
  1308. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1309. goto mgmtqueue_alloc_fail;
  1310. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1311. /* allocate login_mtask used for the login/text sequences */
  1312. spin_lock_bh(&session->lock);
  1313. if (!__kfifo_get(session->mgmtpool.queue,
  1314. (void*)&conn->login_mtask,
  1315. sizeof(void*))) {
  1316. spin_unlock_bh(&session->lock);
  1317. goto login_mtask_alloc_fail;
  1318. }
  1319. spin_unlock_bh(&session->lock);
  1320. data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
  1321. if (!data)
  1322. goto login_mtask_data_alloc_fail;
  1323. conn->login_mtask->data = conn->data = data;
  1324. init_timer(&conn->tmabort_timer);
  1325. mutex_init(&conn->xmitmutex);
  1326. init_waitqueue_head(&conn->ehwait);
  1327. return cls_conn;
  1328. login_mtask_data_alloc_fail:
  1329. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1330. sizeof(void*));
  1331. login_mtask_alloc_fail:
  1332. kfifo_free(conn->mgmtqueue);
  1333. mgmtqueue_alloc_fail:
  1334. kfifo_free(conn->immqueue);
  1335. immqueue_alloc_fail:
  1336. iscsi_destroy_conn(cls_conn);
  1337. return NULL;
  1338. }
  1339. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1340. /**
  1341. * iscsi_conn_teardown - teardown iscsi connection
  1342. * cls_conn: iscsi class connection
  1343. *
  1344. * TODO: we may need to make this into a two step process
  1345. * like scsi-mls remove + put host
  1346. */
  1347. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1348. {
  1349. struct iscsi_conn *conn = cls_conn->dd_data;
  1350. struct iscsi_session *session = conn->session;
  1351. unsigned long flags;
  1352. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1353. mutex_lock(&conn->xmitmutex);
  1354. spin_lock_bh(&session->lock);
  1355. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1356. if (session->leadconn == conn) {
  1357. /*
  1358. * leading connection? then give up on recovery.
  1359. */
  1360. session->state = ISCSI_STATE_TERMINATE;
  1361. wake_up(&conn->ehwait);
  1362. }
  1363. spin_unlock_bh(&session->lock);
  1364. mutex_unlock(&conn->xmitmutex);
  1365. /*
  1366. * Block until all in-progress commands for this connection
  1367. * time out or fail.
  1368. */
  1369. for (;;) {
  1370. spin_lock_irqsave(session->host->host_lock, flags);
  1371. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1372. spin_unlock_irqrestore(session->host->host_lock, flags);
  1373. break;
  1374. }
  1375. spin_unlock_irqrestore(session->host->host_lock, flags);
  1376. msleep_interruptible(500);
  1377. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1378. "host_failed %d\n", session->host->host_busy,
  1379. session->host->host_failed);
  1380. /*
  1381. * force eh_abort() to unblock
  1382. */
  1383. wake_up(&conn->ehwait);
  1384. }
  1385. spin_lock_bh(&session->lock);
  1386. kfree(conn->data);
  1387. kfree(conn->persistent_address);
  1388. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1389. sizeof(void*));
  1390. if (session->leadconn == conn) {
  1391. session->leadconn = NULL;
  1392. /* no connections exits.. reset sequencing */
  1393. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1394. }
  1395. spin_unlock_bh(&session->lock);
  1396. kfifo_free(conn->immqueue);
  1397. kfifo_free(conn->mgmtqueue);
  1398. iscsi_destroy_conn(cls_conn);
  1399. }
  1400. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1401. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1402. {
  1403. struct iscsi_conn *conn = cls_conn->dd_data;
  1404. struct iscsi_session *session = conn->session;
  1405. if (!session) {
  1406. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1407. return -EPERM;
  1408. }
  1409. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1410. session->first_burst > session->max_burst) {
  1411. printk("iscsi: invalid burst lengths: "
  1412. "first_burst %d max_burst %d\n",
  1413. session->first_burst, session->max_burst);
  1414. return -EINVAL;
  1415. }
  1416. spin_lock_bh(&session->lock);
  1417. conn->c_stage = ISCSI_CONN_STARTED;
  1418. session->state = ISCSI_STATE_LOGGED_IN;
  1419. switch(conn->stop_stage) {
  1420. case STOP_CONN_RECOVER:
  1421. /*
  1422. * unblock eh_abort() if it is blocked. re-try all
  1423. * commands after successful recovery
  1424. */
  1425. conn->stop_stage = 0;
  1426. conn->tmabort_state = TMABORT_INITIAL;
  1427. session->age++;
  1428. spin_unlock_bh(&session->lock);
  1429. iscsi_unblock_session(session_to_cls(session));
  1430. wake_up(&conn->ehwait);
  1431. return 0;
  1432. case STOP_CONN_TERM:
  1433. conn->stop_stage = 0;
  1434. break;
  1435. default:
  1436. break;
  1437. }
  1438. spin_unlock_bh(&session->lock);
  1439. return 0;
  1440. }
  1441. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1442. static void
  1443. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1444. {
  1445. struct iscsi_mgmt_task *mtask, *tmp;
  1446. /* handle pending */
  1447. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1448. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1449. if (mtask == conn->login_mtask)
  1450. continue;
  1451. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1452. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1453. sizeof(void*));
  1454. }
  1455. /* handle running */
  1456. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1457. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1458. list_del(&mtask->running);
  1459. if (mtask == conn->login_mtask)
  1460. continue;
  1461. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1462. sizeof(void*));
  1463. }
  1464. conn->mtask = NULL;
  1465. }
  1466. /* Fail commands. Mutex and session lock held and recv side suspended */
  1467. static void fail_all_commands(struct iscsi_conn *conn)
  1468. {
  1469. struct iscsi_cmd_task *ctask, *tmp;
  1470. /* flush pending */
  1471. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1472. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1473. ctask->itt);
  1474. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1475. }
  1476. /* fail all other running */
  1477. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1478. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1479. ctask->sc, ctask->itt);
  1480. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1481. }
  1482. conn->ctask = NULL;
  1483. }
  1484. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1485. struct iscsi_conn *conn, int flag)
  1486. {
  1487. int old_stop_stage;
  1488. spin_lock_bh(&session->lock);
  1489. if (conn->stop_stage == STOP_CONN_TERM) {
  1490. spin_unlock_bh(&session->lock);
  1491. return;
  1492. }
  1493. /*
  1494. * When this is called for the in_login state, we only want to clean
  1495. * up the login task and connection. We do not need to block and set
  1496. * the recovery state again
  1497. */
  1498. if (flag == STOP_CONN_TERM)
  1499. session->state = ISCSI_STATE_TERMINATE;
  1500. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1501. session->state = ISCSI_STATE_IN_RECOVERY;
  1502. old_stop_stage = conn->stop_stage;
  1503. conn->stop_stage = flag;
  1504. conn->c_stage = ISCSI_CONN_STOPPED;
  1505. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1506. spin_unlock_bh(&session->lock);
  1507. write_lock_bh(conn->recv_lock);
  1508. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1509. write_unlock_bh(conn->recv_lock);
  1510. mutex_lock(&conn->xmitmutex);
  1511. /*
  1512. * for connection level recovery we should not calculate
  1513. * header digest. conn->hdr_size used for optimization
  1514. * in hdr_extract() and will be re-negotiated at
  1515. * set_param() time.
  1516. */
  1517. if (flag == STOP_CONN_RECOVER) {
  1518. conn->hdrdgst_en = 0;
  1519. conn->datadgst_en = 0;
  1520. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1521. old_stop_stage != STOP_CONN_RECOVER) {
  1522. debug_scsi("blocking session\n");
  1523. iscsi_block_session(session_to_cls(session));
  1524. }
  1525. }
  1526. /*
  1527. * flush queues.
  1528. */
  1529. spin_lock_bh(&session->lock);
  1530. fail_all_commands(conn);
  1531. flush_control_queues(session, conn);
  1532. spin_unlock_bh(&session->lock);
  1533. mutex_unlock(&conn->xmitmutex);
  1534. }
  1535. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1536. {
  1537. struct iscsi_conn *conn = cls_conn->dd_data;
  1538. struct iscsi_session *session = conn->session;
  1539. switch (flag) {
  1540. case STOP_CONN_RECOVER:
  1541. case STOP_CONN_TERM:
  1542. iscsi_start_session_recovery(session, conn, flag);
  1543. break;
  1544. default:
  1545. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1546. }
  1547. }
  1548. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1549. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1550. struct iscsi_cls_conn *cls_conn, int is_leading)
  1551. {
  1552. struct iscsi_session *session = class_to_transport_session(cls_session);
  1553. struct iscsi_conn *conn = cls_conn->dd_data;
  1554. spin_lock_bh(&session->lock);
  1555. if (is_leading)
  1556. session->leadconn = conn;
  1557. spin_unlock_bh(&session->lock);
  1558. /*
  1559. * Unblock xmitworker(), Login Phase will pass through.
  1560. */
  1561. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1562. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1563. return 0;
  1564. }
  1565. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1566. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1567. enum iscsi_param param, char *buf, int buflen)
  1568. {
  1569. struct iscsi_conn *conn = cls_conn->dd_data;
  1570. struct iscsi_session *session = conn->session;
  1571. uint32_t value;
  1572. switch(param) {
  1573. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1574. sscanf(buf, "%d", &conn->max_recv_dlength);
  1575. break;
  1576. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1577. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1578. break;
  1579. case ISCSI_PARAM_HDRDGST_EN:
  1580. sscanf(buf, "%d", &conn->hdrdgst_en);
  1581. break;
  1582. case ISCSI_PARAM_DATADGST_EN:
  1583. sscanf(buf, "%d", &conn->datadgst_en);
  1584. break;
  1585. case ISCSI_PARAM_INITIAL_R2T_EN:
  1586. sscanf(buf, "%d", &session->initial_r2t_en);
  1587. break;
  1588. case ISCSI_PARAM_MAX_R2T:
  1589. sscanf(buf, "%d", &session->max_r2t);
  1590. break;
  1591. case ISCSI_PARAM_IMM_DATA_EN:
  1592. sscanf(buf, "%d", &session->imm_data_en);
  1593. break;
  1594. case ISCSI_PARAM_FIRST_BURST:
  1595. sscanf(buf, "%d", &session->first_burst);
  1596. break;
  1597. case ISCSI_PARAM_MAX_BURST:
  1598. sscanf(buf, "%d", &session->max_burst);
  1599. break;
  1600. case ISCSI_PARAM_PDU_INORDER_EN:
  1601. sscanf(buf, "%d", &session->pdu_inorder_en);
  1602. break;
  1603. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1604. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1605. break;
  1606. case ISCSI_PARAM_ERL:
  1607. sscanf(buf, "%d", &session->erl);
  1608. break;
  1609. case ISCSI_PARAM_IFMARKER_EN:
  1610. sscanf(buf, "%d", &value);
  1611. BUG_ON(value);
  1612. break;
  1613. case ISCSI_PARAM_OFMARKER_EN:
  1614. sscanf(buf, "%d", &value);
  1615. BUG_ON(value);
  1616. break;
  1617. case ISCSI_PARAM_EXP_STATSN:
  1618. sscanf(buf, "%u", &conn->exp_statsn);
  1619. break;
  1620. case ISCSI_PARAM_TARGET_NAME:
  1621. /* this should not change between logins */
  1622. if (session->targetname)
  1623. break;
  1624. session->targetname = kstrdup(buf, GFP_KERNEL);
  1625. if (!session->targetname)
  1626. return -ENOMEM;
  1627. break;
  1628. case ISCSI_PARAM_TPGT:
  1629. sscanf(buf, "%d", &session->tpgt);
  1630. break;
  1631. case ISCSI_PARAM_PERSISTENT_PORT:
  1632. sscanf(buf, "%d", &conn->persistent_port);
  1633. break;
  1634. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1635. /*
  1636. * this is the address returned in discovery so it should
  1637. * not change between logins.
  1638. */
  1639. if (conn->persistent_address)
  1640. break;
  1641. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1642. if (!conn->persistent_address)
  1643. return -ENOMEM;
  1644. break;
  1645. default:
  1646. return -ENOSYS;
  1647. }
  1648. return 0;
  1649. }
  1650. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1651. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1652. enum iscsi_param param, char *buf)
  1653. {
  1654. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1655. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1656. int len;
  1657. switch(param) {
  1658. case ISCSI_PARAM_INITIAL_R2T_EN:
  1659. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1660. break;
  1661. case ISCSI_PARAM_MAX_R2T:
  1662. len = sprintf(buf, "%hu\n", session->max_r2t);
  1663. break;
  1664. case ISCSI_PARAM_IMM_DATA_EN:
  1665. len = sprintf(buf, "%d\n", session->imm_data_en);
  1666. break;
  1667. case ISCSI_PARAM_FIRST_BURST:
  1668. len = sprintf(buf, "%u\n", session->first_burst);
  1669. break;
  1670. case ISCSI_PARAM_MAX_BURST:
  1671. len = sprintf(buf, "%u\n", session->max_burst);
  1672. break;
  1673. case ISCSI_PARAM_PDU_INORDER_EN:
  1674. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1675. break;
  1676. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1677. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1678. break;
  1679. case ISCSI_PARAM_ERL:
  1680. len = sprintf(buf, "%d\n", session->erl);
  1681. break;
  1682. case ISCSI_PARAM_TARGET_NAME:
  1683. len = sprintf(buf, "%s\n", session->targetname);
  1684. break;
  1685. case ISCSI_PARAM_TPGT:
  1686. len = sprintf(buf, "%d\n", session->tpgt);
  1687. break;
  1688. default:
  1689. return -ENOSYS;
  1690. }
  1691. return len;
  1692. }
  1693. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1694. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1695. enum iscsi_param param, char *buf)
  1696. {
  1697. struct iscsi_conn *conn = cls_conn->dd_data;
  1698. int len;
  1699. switch(param) {
  1700. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1701. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1702. break;
  1703. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1704. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1705. break;
  1706. case ISCSI_PARAM_HDRDGST_EN:
  1707. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1708. break;
  1709. case ISCSI_PARAM_DATADGST_EN:
  1710. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1711. break;
  1712. case ISCSI_PARAM_IFMARKER_EN:
  1713. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1714. break;
  1715. case ISCSI_PARAM_OFMARKER_EN:
  1716. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1717. break;
  1718. case ISCSI_PARAM_EXP_STATSN:
  1719. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1720. break;
  1721. case ISCSI_PARAM_PERSISTENT_PORT:
  1722. len = sprintf(buf, "%d\n", conn->persistent_port);
  1723. break;
  1724. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1725. len = sprintf(buf, "%s\n", conn->persistent_address);
  1726. break;
  1727. default:
  1728. return -ENOSYS;
  1729. }
  1730. return len;
  1731. }
  1732. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1733. MODULE_AUTHOR("Mike Christie");
  1734. MODULE_DESCRIPTION("iSCSI library functions");
  1735. MODULE_LICENSE("GPL");