libiscsi.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  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. /* we need sth like iscsi_async_event_rsp() */
  427. rc = ISCSI_ERR_BAD_OPCODE;
  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. /**
  510. * iscsi_data_xmit - xmit any command into the scheduled connection
  511. * @conn: iscsi connection
  512. *
  513. * Notes:
  514. * The function can return -EAGAIN in which case the caller must
  515. * re-schedule it again later or recover. '0' return code means
  516. * successful xmit.
  517. **/
  518. static int iscsi_data_xmit(struct iscsi_conn *conn)
  519. {
  520. struct iscsi_transport *tt;
  521. int rc = 0;
  522. if (unlikely(conn->suspend_tx)) {
  523. debug_scsi("conn %d Tx suspended!\n", conn->id);
  524. return -ENODATA;
  525. }
  526. tt = conn->session->tt;
  527. /*
  528. * Transmit in the following order:
  529. *
  530. * 1) un-finished xmit (ctask or mtask)
  531. * 2) immediate control PDUs
  532. * 3) write data
  533. * 4) SCSI commands
  534. * 5) non-immediate control PDUs
  535. *
  536. * No need to lock around __kfifo_get as long as
  537. * there's one producer and one consumer.
  538. */
  539. BUG_ON(conn->ctask && conn->mtask);
  540. if (conn->ctask) {
  541. iscsi_get_ctask(conn->ctask);
  542. rc = tt->xmit_cmd_task(conn, conn->ctask);
  543. iscsi_put_ctask(conn->ctask);
  544. if (rc)
  545. goto again;
  546. /* done with this in-progress ctask */
  547. conn->ctask = NULL;
  548. }
  549. if (conn->mtask) {
  550. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  551. if (rc)
  552. goto again;
  553. /* done with this in-progress mtask */
  554. conn->mtask = NULL;
  555. }
  556. /* process immediate first */
  557. if (unlikely(__kfifo_len(conn->immqueue))) {
  558. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  559. sizeof(void*))) {
  560. spin_lock_bh(&conn->session->lock);
  561. list_add_tail(&conn->mtask->running,
  562. &conn->mgmt_run_list);
  563. spin_unlock_bh(&conn->session->lock);
  564. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  565. if (rc)
  566. goto again;
  567. }
  568. /* done with this mtask */
  569. conn->mtask = NULL;
  570. }
  571. /* process command queue */
  572. spin_lock_bh(&conn->session->lock);
  573. while (!list_empty(&conn->xmitqueue)) {
  574. /*
  575. * iscsi tcp may readd the task to the xmitqueue to send
  576. * write data
  577. */
  578. conn->ctask = list_entry(conn->xmitqueue.next,
  579. struct iscsi_cmd_task, running);
  580. conn->ctask->state = ISCSI_TASK_RUNNING;
  581. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  582. __iscsi_get_ctask(conn->ctask);
  583. spin_unlock_bh(&conn->session->lock);
  584. rc = tt->xmit_cmd_task(conn, conn->ctask);
  585. if (rc)
  586. goto again;
  587. spin_lock_bh(&conn->session->lock);
  588. __iscsi_put_ctask(conn->ctask);
  589. if (rc) {
  590. spin_unlock_bh(&conn->session->lock);
  591. goto again;
  592. }
  593. }
  594. spin_unlock_bh(&conn->session->lock);
  595. /* done with this ctask */
  596. conn->ctask = NULL;
  597. /* process the rest control plane PDUs, if any */
  598. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  599. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  600. sizeof(void*))) {
  601. spin_lock_bh(&conn->session->lock);
  602. list_add_tail(&conn->mtask->running,
  603. &conn->mgmt_run_list);
  604. spin_unlock_bh(&conn->session->lock);
  605. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  606. if (rc)
  607. goto again;
  608. }
  609. /* done with this mtask */
  610. conn->mtask = NULL;
  611. }
  612. return -ENODATA;
  613. again:
  614. if (unlikely(conn->suspend_tx))
  615. return -ENODATA;
  616. return rc;
  617. }
  618. static void iscsi_xmitworker(void *data)
  619. {
  620. struct iscsi_conn *conn = data;
  621. int rc;
  622. /*
  623. * serialize Xmit worker on a per-connection basis.
  624. */
  625. mutex_lock(&conn->xmitmutex);
  626. do {
  627. rc = iscsi_data_xmit(conn);
  628. } while (rc >= 0 || rc == -EAGAIN);
  629. mutex_unlock(&conn->xmitmutex);
  630. }
  631. enum {
  632. FAILURE_BAD_HOST = 1,
  633. FAILURE_SESSION_FAILED,
  634. FAILURE_SESSION_FREED,
  635. FAILURE_WINDOW_CLOSED,
  636. FAILURE_OOM,
  637. FAILURE_SESSION_TERMINATE,
  638. FAILURE_SESSION_IN_RECOVERY,
  639. FAILURE_SESSION_RECOVERY_TIMEOUT,
  640. };
  641. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  642. {
  643. struct Scsi_Host *host;
  644. int reason = 0;
  645. struct iscsi_session *session;
  646. struct iscsi_conn *conn;
  647. struct iscsi_cmd_task *ctask = NULL;
  648. sc->scsi_done = done;
  649. sc->result = 0;
  650. sc->SCp.ptr = NULL;
  651. host = sc->device->host;
  652. session = iscsi_hostdata(host->hostdata);
  653. spin_lock(&session->lock);
  654. /*
  655. * ISCSI_STATE_FAILED is a temp. state. The recovery
  656. * code will decide what is best to do with command queued
  657. * during this time
  658. */
  659. if (session->state != ISCSI_STATE_LOGGED_IN &&
  660. session->state != ISCSI_STATE_FAILED) {
  661. /*
  662. * to handle the race between when we set the recovery state
  663. * and block the session we requeue here (commands could
  664. * be entering our queuecommand while a block is starting
  665. * up because the block code is not locked)
  666. */
  667. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  668. reason = FAILURE_SESSION_IN_RECOVERY;
  669. goto reject;
  670. }
  671. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  672. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  673. else if (session->state == ISCSI_STATE_TERMINATE)
  674. reason = FAILURE_SESSION_TERMINATE;
  675. else
  676. reason = FAILURE_SESSION_FREED;
  677. goto fault;
  678. }
  679. /*
  680. * Check for iSCSI window and take care of CmdSN wrap-around
  681. */
  682. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  683. reason = FAILURE_WINDOW_CLOSED;
  684. goto reject;
  685. }
  686. conn = session->leadconn;
  687. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  688. sizeof(void*))) {
  689. reason = FAILURE_OOM;
  690. goto reject;
  691. }
  692. sc->SCp.phase = session->age;
  693. sc->SCp.ptr = (char *)ctask;
  694. atomic_set(&ctask->refcount, 1);
  695. ctask->state = ISCSI_TASK_PENDING;
  696. ctask->mtask = NULL;
  697. ctask->conn = conn;
  698. ctask->sc = sc;
  699. INIT_LIST_HEAD(&ctask->running);
  700. ctask->total_length = sc->request_bufflen;
  701. iscsi_prep_scsi_cmd_pdu(ctask);
  702. session->tt->init_cmd_task(ctask);
  703. list_add_tail(&ctask->running, &conn->xmitqueue);
  704. debug_scsi(
  705. "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
  706. "win %d]\n",
  707. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  708. conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
  709. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  710. spin_unlock(&session->lock);
  711. scsi_queue_work(host, &conn->xmitwork);
  712. return 0;
  713. reject:
  714. spin_unlock(&session->lock);
  715. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  716. return SCSI_MLQUEUE_HOST_BUSY;
  717. fault:
  718. spin_unlock(&session->lock);
  719. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  720. sc->cmnd[0], reason);
  721. sc->result = (DID_NO_CONNECT << 16);
  722. sc->resid = sc->request_bufflen;
  723. sc->scsi_done(sc);
  724. return 0;
  725. }
  726. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  727. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  728. {
  729. if (depth > ISCSI_MAX_CMD_PER_LUN)
  730. depth = ISCSI_MAX_CMD_PER_LUN;
  731. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  732. return sdev->queue_depth;
  733. }
  734. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  735. static int
  736. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  737. char *data, uint32_t data_size)
  738. {
  739. struct iscsi_session *session = conn->session;
  740. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  741. struct iscsi_mgmt_task *mtask;
  742. spin_lock_bh(&session->lock);
  743. if (session->state == ISCSI_STATE_TERMINATE) {
  744. spin_unlock_bh(&session->lock);
  745. return -EPERM;
  746. }
  747. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  748. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  749. /*
  750. * Login and Text are sent serially, in
  751. * request-followed-by-response sequence.
  752. * Same mtask can be used. Same ITT must be used.
  753. * Note that login_mtask is preallocated at conn_create().
  754. */
  755. mtask = conn->login_mtask;
  756. else {
  757. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  758. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  759. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  760. if (!__kfifo_get(session->mgmtpool.queue,
  761. (void*)&mtask, sizeof(void*))) {
  762. spin_unlock_bh(&session->lock);
  763. return -ENOSPC;
  764. }
  765. }
  766. /*
  767. * pre-format CmdSN for outgoing PDU.
  768. */
  769. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  770. hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
  771. (session->age << ISCSI_AGE_SHIFT);
  772. nop->cmdsn = cpu_to_be32(session->cmdsn);
  773. if (conn->c_stage == ISCSI_CONN_STARTED &&
  774. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  775. session->cmdsn++;
  776. } else
  777. /* do not advance CmdSN */
  778. nop->cmdsn = cpu_to_be32(session->cmdsn);
  779. if (data_size) {
  780. memcpy(mtask->data, data, data_size);
  781. mtask->data_count = data_size;
  782. } else
  783. mtask->data_count = 0;
  784. INIT_LIST_HEAD(&mtask->running);
  785. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  786. if (session->tt->init_mgmt_task)
  787. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  788. spin_unlock_bh(&session->lock);
  789. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  790. hdr->opcode, hdr->itt, data_size);
  791. /*
  792. * since send_pdu() could be called at least from two contexts,
  793. * we need to serialize __kfifo_put, so we don't have to take
  794. * additional lock on fast data-path
  795. */
  796. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  797. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  798. else
  799. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  800. scsi_queue_work(session->host, &conn->xmitwork);
  801. return 0;
  802. }
  803. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  804. char *data, uint32_t data_size)
  805. {
  806. struct iscsi_conn *conn = cls_conn->dd_data;
  807. int rc;
  808. mutex_lock(&conn->xmitmutex);
  809. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  810. mutex_unlock(&conn->xmitmutex);
  811. return rc;
  812. }
  813. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  814. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  815. {
  816. struct iscsi_session *session = class_to_transport_session(cls_session);
  817. struct iscsi_conn *conn = session->leadconn;
  818. spin_lock_bh(&session->lock);
  819. if (session->state != ISCSI_STATE_LOGGED_IN) {
  820. session->state = ISCSI_STATE_RECOVERY_FAILED;
  821. if (conn)
  822. wake_up(&conn->ehwait);
  823. }
  824. spin_unlock_bh(&session->lock);
  825. }
  826. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  827. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  828. {
  829. struct Scsi_Host *host = sc->device->host;
  830. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  831. struct iscsi_conn *conn = session->leadconn;
  832. int fail_session = 0;
  833. spin_lock_bh(&session->lock);
  834. if (session->state == ISCSI_STATE_TERMINATE) {
  835. failed:
  836. debug_scsi("failing host reset: session terminated "
  837. "[CID %d age %d]", conn->id, session->age);
  838. spin_unlock_bh(&session->lock);
  839. return FAILED;
  840. }
  841. if (sc->SCp.phase == session->age) {
  842. debug_scsi("failing connection CID %d due to SCSI host reset",
  843. conn->id);
  844. fail_session = 1;
  845. }
  846. spin_unlock_bh(&session->lock);
  847. /*
  848. * we drop the lock here but the leadconn cannot be destoyed while
  849. * we are in the scsi eh
  850. */
  851. if (fail_session)
  852. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  853. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  854. wait_event_interruptible(conn->ehwait,
  855. session->state == ISCSI_STATE_TERMINATE ||
  856. session->state == ISCSI_STATE_LOGGED_IN ||
  857. session->state == ISCSI_STATE_RECOVERY_FAILED);
  858. if (signal_pending(current))
  859. flush_signals(current);
  860. spin_lock_bh(&session->lock);
  861. if (session->state == ISCSI_STATE_LOGGED_IN)
  862. printk(KERN_INFO "iscsi: host reset succeeded\n");
  863. else
  864. goto failed;
  865. spin_unlock_bh(&session->lock);
  866. return SUCCESS;
  867. }
  868. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  869. static void iscsi_tmabort_timedout(unsigned long data)
  870. {
  871. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  872. struct iscsi_conn *conn = ctask->conn;
  873. struct iscsi_session *session = conn->session;
  874. spin_lock(&session->lock);
  875. if (conn->tmabort_state == TMABORT_INITIAL) {
  876. conn->tmabort_state = TMABORT_TIMEDOUT;
  877. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  878. ctask->sc, ctask->itt);
  879. /* unblock eh_abort() */
  880. wake_up(&conn->ehwait);
  881. }
  882. spin_unlock(&session->lock);
  883. }
  884. /* must be called with the mutex lock */
  885. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  886. struct iscsi_cmd_task *ctask)
  887. {
  888. struct iscsi_conn *conn = ctask->conn;
  889. struct iscsi_session *session = conn->session;
  890. struct iscsi_tm *hdr = &conn->tmhdr;
  891. int rc;
  892. /*
  893. * ctask timed out but session is OK requests must be serialized.
  894. */
  895. memset(hdr, 0, sizeof(struct iscsi_tm));
  896. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  897. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  898. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  899. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  900. hdr->rtt = ctask->hdr->itt;
  901. hdr->refcmdsn = ctask->hdr->cmdsn;
  902. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  903. NULL, 0);
  904. if (rc) {
  905. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  906. debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
  907. return rc;
  908. }
  909. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  910. spin_lock_bh(&session->lock);
  911. ctask->mtask = (struct iscsi_mgmt_task *)
  912. session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
  913. ISCSI_MGMT_ITT_OFFSET];
  914. if (conn->tmabort_state == TMABORT_INITIAL) {
  915. conn->tmfcmd_pdus_cnt++;
  916. conn->tmabort_timer.expires = 10*HZ + jiffies;
  917. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  918. conn->tmabort_timer.data = (unsigned long)ctask;
  919. add_timer(&conn->tmabort_timer);
  920. debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
  921. }
  922. spin_unlock_bh(&session->lock);
  923. mutex_unlock(&conn->xmitmutex);
  924. /*
  925. * block eh thread until:
  926. *
  927. * 1) abort response
  928. * 2) abort timeout
  929. * 3) session is terminated or restarted or userspace has
  930. * given up on recovery
  931. */
  932. wait_event_interruptible(conn->ehwait,
  933. sc->SCp.phase != session->age ||
  934. session->state != ISCSI_STATE_LOGGED_IN ||
  935. conn->tmabort_state != TMABORT_INITIAL);
  936. if (signal_pending(current))
  937. flush_signals(current);
  938. del_timer_sync(&conn->tmabort_timer);
  939. mutex_lock(&conn->xmitmutex);
  940. return 0;
  941. }
  942. /*
  943. * xmit mutex and session lock must be held
  944. */
  945. static struct iscsi_mgmt_task *
  946. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  947. {
  948. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  949. struct iscsi_mgmt_task *task;
  950. debug_scsi("searching %d tasks\n", nr_tasks);
  951. for (i = 0; i < nr_tasks; i++) {
  952. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  953. debug_scsi("check task %u\n", task->itt);
  954. if (task->itt == itt) {
  955. debug_scsi("matched task\n");
  956. return task;
  957. }
  958. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  959. }
  960. return NULL;
  961. }
  962. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  963. {
  964. struct iscsi_conn *conn = ctask->conn;
  965. struct iscsi_session *session = conn->session;
  966. if (!ctask->mtask)
  967. return -EINVAL;
  968. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  969. list_del(&ctask->mtask->running);
  970. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  971. sizeof(void*));
  972. ctask->mtask = NULL;
  973. return 0;
  974. }
  975. /*
  976. * session lock and xmitmutex must be held
  977. */
  978. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  979. int err)
  980. {
  981. struct scsi_cmnd *sc;
  982. sc = ctask->sc;
  983. if (!sc)
  984. return;
  985. conn->session->tt->cleanup_cmd_task(conn, ctask);
  986. iscsi_ctask_mtask_cleanup(ctask);
  987. sc->result = err;
  988. sc->resid = sc->request_bufflen;
  989. /* release ref from queuecommand */
  990. __iscsi_put_ctask(ctask);
  991. }
  992. int iscsi_eh_abort(struct scsi_cmnd *sc)
  993. {
  994. struct iscsi_cmd_task *ctask;
  995. struct iscsi_conn *conn;
  996. struct iscsi_session *session;
  997. int rc;
  998. /*
  999. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1000. * got the command.
  1001. */
  1002. if (!sc->SCp.ptr) {
  1003. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1004. return SUCCESS;
  1005. }
  1006. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1007. conn = ctask->conn;
  1008. session = conn->session;
  1009. conn->eh_abort_cnt++;
  1010. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1011. mutex_lock(&conn->xmitmutex);
  1012. spin_lock_bh(&session->lock);
  1013. /*
  1014. * If we are not logged in or we have started a new session
  1015. * then let the host reset code handle this
  1016. */
  1017. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1018. sc->SCp.phase != session->age)
  1019. goto failed;
  1020. /* ctask completed before time out */
  1021. if (!ctask->sc) {
  1022. spin_unlock_bh(&session->lock);
  1023. debug_scsi("sc completed while abort in progress\n");
  1024. goto success_rel_mutex;
  1025. }
  1026. /* what should we do here ? */
  1027. if (conn->ctask == ctask) {
  1028. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1029. "Failing abort\n", sc, ctask->itt);
  1030. goto failed;
  1031. }
  1032. if (ctask->state == ISCSI_TASK_PENDING)
  1033. goto success_cleanup;
  1034. conn->tmabort_state = TMABORT_INITIAL;
  1035. spin_unlock_bh(&session->lock);
  1036. rc = iscsi_exec_abort_task(sc, ctask);
  1037. spin_lock_bh(&session->lock);
  1038. if (rc || sc->SCp.phase != session->age ||
  1039. session->state != ISCSI_STATE_LOGGED_IN)
  1040. goto failed;
  1041. iscsi_ctask_mtask_cleanup(ctask);
  1042. switch (conn->tmabort_state) {
  1043. case TMABORT_SUCCESS:
  1044. goto success_cleanup;
  1045. case TMABORT_NOT_FOUND:
  1046. if (!ctask->sc) {
  1047. /* ctask completed before tmf abort response */
  1048. spin_unlock_bh(&session->lock);
  1049. debug_scsi("sc completed while abort in progress\n");
  1050. goto success_rel_mutex;
  1051. }
  1052. /* fall through */
  1053. default:
  1054. /* timedout or failed */
  1055. spin_unlock_bh(&session->lock);
  1056. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1057. spin_lock_bh(&session->lock);
  1058. goto failed;
  1059. }
  1060. success_cleanup:
  1061. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1062. spin_unlock_bh(&session->lock);
  1063. /*
  1064. * clean up task if aborted. we have the xmitmutex so grab
  1065. * the recv lock as a writer
  1066. */
  1067. write_lock_bh(conn->recv_lock);
  1068. spin_lock(&session->lock);
  1069. fail_command(conn, ctask, DID_ABORT << 16);
  1070. spin_unlock(&session->lock);
  1071. write_unlock_bh(conn->recv_lock);
  1072. success_rel_mutex:
  1073. mutex_unlock(&conn->xmitmutex);
  1074. return SUCCESS;
  1075. failed:
  1076. spin_unlock_bh(&session->lock);
  1077. mutex_unlock(&conn->xmitmutex);
  1078. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1079. return FAILED;
  1080. }
  1081. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1082. int
  1083. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1084. {
  1085. int i;
  1086. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1087. if (*items == NULL)
  1088. return -ENOMEM;
  1089. q->max = max;
  1090. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1091. if (q->pool == NULL) {
  1092. kfree(*items);
  1093. return -ENOMEM;
  1094. }
  1095. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1096. GFP_KERNEL, NULL);
  1097. if (q->queue == ERR_PTR(-ENOMEM)) {
  1098. kfree(q->pool);
  1099. kfree(*items);
  1100. return -ENOMEM;
  1101. }
  1102. for (i = 0; i < max; i++) {
  1103. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1104. if (q->pool[i] == NULL) {
  1105. int j;
  1106. for (j = 0; j < i; j++)
  1107. kfree(q->pool[j]);
  1108. kfifo_free(q->queue);
  1109. kfree(q->pool);
  1110. kfree(*items);
  1111. return -ENOMEM;
  1112. }
  1113. memset(q->pool[i], 0, item_size);
  1114. (*items)[i] = q->pool[i];
  1115. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1116. }
  1117. return 0;
  1118. }
  1119. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1120. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1121. {
  1122. int i;
  1123. for (i = 0; i < q->max; i++)
  1124. kfree(items[i]);
  1125. kfree(q->pool);
  1126. kfree(items);
  1127. }
  1128. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1129. /*
  1130. * iSCSI Session's hostdata organization:
  1131. *
  1132. * *------------------* <== hostdata_session(host->hostdata)
  1133. * | ptr to class sess|
  1134. * |------------------| <== iscsi_hostdata(host->hostdata)
  1135. * | iscsi_session |
  1136. * *------------------*
  1137. */
  1138. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1139. _sz % sizeof(unsigned long))
  1140. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1141. /**
  1142. * iscsi_session_setup - create iscsi cls session and host and session
  1143. * @scsit: scsi transport template
  1144. * @iscsit: iscsi transport template
  1145. * @initial_cmdsn: initial CmdSN
  1146. * @hostno: host no allocated
  1147. *
  1148. * This can be used by software iscsi_transports that allocate
  1149. * a session per scsi host.
  1150. **/
  1151. struct iscsi_cls_session *
  1152. iscsi_session_setup(struct iscsi_transport *iscsit,
  1153. struct scsi_transport_template *scsit,
  1154. int cmd_task_size, int mgmt_task_size,
  1155. uint32_t initial_cmdsn, uint32_t *hostno)
  1156. {
  1157. struct Scsi_Host *shost;
  1158. struct iscsi_session *session;
  1159. struct iscsi_cls_session *cls_session;
  1160. int cmd_i;
  1161. shost = scsi_host_alloc(iscsit->host_template,
  1162. hostdata_privsize(sizeof(*session)));
  1163. if (!shost)
  1164. return NULL;
  1165. shost->max_id = 1;
  1166. shost->max_channel = 0;
  1167. shost->max_lun = iscsit->max_lun;
  1168. shost->max_cmd_len = iscsit->max_cmd_len;
  1169. shost->transportt = scsit;
  1170. shost->transportt->create_work_queue = 1;
  1171. *hostno = shost->host_no;
  1172. session = iscsi_hostdata(shost->hostdata);
  1173. memset(session, 0, sizeof(struct iscsi_session));
  1174. session->host = shost;
  1175. session->state = ISCSI_STATE_FREE;
  1176. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1177. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1178. session->cmdsn = initial_cmdsn;
  1179. session->exp_cmdsn = initial_cmdsn + 1;
  1180. session->max_cmdsn = initial_cmdsn + 1;
  1181. session->max_r2t = 1;
  1182. session->tt = iscsit;
  1183. /* initialize SCSI PDU commands pool */
  1184. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1185. (void***)&session->cmds,
  1186. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1187. goto cmdpool_alloc_fail;
  1188. /* pre-format cmds pool with ITT */
  1189. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1190. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1191. if (cmd_task_size)
  1192. ctask->dd_data = &ctask[1];
  1193. ctask->itt = cmd_i;
  1194. INIT_LIST_HEAD(&ctask->running);
  1195. }
  1196. spin_lock_init(&session->lock);
  1197. INIT_LIST_HEAD(&session->connections);
  1198. /* initialize immediate command pool */
  1199. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1200. (void***)&session->mgmt_cmds,
  1201. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1202. goto mgmtpool_alloc_fail;
  1203. /* pre-format immediate cmds pool with ITT */
  1204. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1205. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1206. if (mgmt_task_size)
  1207. mtask->dd_data = &mtask[1];
  1208. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1209. INIT_LIST_HEAD(&mtask->running);
  1210. }
  1211. if (scsi_add_host(shost, NULL))
  1212. goto add_host_fail;
  1213. if (!try_module_get(iscsit->owner))
  1214. goto cls_session_fail;
  1215. cls_session = iscsi_create_session(shost, iscsit, 0);
  1216. if (!cls_session)
  1217. goto module_put;
  1218. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1219. return cls_session;
  1220. module_put:
  1221. module_put(iscsit->owner);
  1222. cls_session_fail:
  1223. scsi_remove_host(shost);
  1224. add_host_fail:
  1225. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1226. mgmtpool_alloc_fail:
  1227. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1228. cmdpool_alloc_fail:
  1229. scsi_host_put(shost);
  1230. return NULL;
  1231. }
  1232. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1233. /**
  1234. * iscsi_session_teardown - destroy session, host, and cls_session
  1235. * shost: scsi host
  1236. *
  1237. * This can be used by software iscsi_transports that allocate
  1238. * a session per scsi host.
  1239. **/
  1240. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1241. {
  1242. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1243. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1244. struct module *owner = cls_session->transport->owner;
  1245. scsi_remove_host(shost);
  1246. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1247. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1248. kfree(session->targetname);
  1249. iscsi_destroy_session(cls_session);
  1250. scsi_host_put(shost);
  1251. module_put(owner);
  1252. }
  1253. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1254. /**
  1255. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1256. * @cls_session: iscsi_cls_session
  1257. * @conn_idx: cid
  1258. **/
  1259. struct iscsi_cls_conn *
  1260. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1261. {
  1262. struct iscsi_session *session = class_to_transport_session(cls_session);
  1263. struct iscsi_conn *conn;
  1264. struct iscsi_cls_conn *cls_conn;
  1265. char *data;
  1266. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1267. if (!cls_conn)
  1268. return NULL;
  1269. conn = cls_conn->dd_data;
  1270. memset(conn, 0, sizeof(*conn));
  1271. conn->session = session;
  1272. conn->cls_conn = cls_conn;
  1273. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1274. conn->id = conn_idx;
  1275. conn->exp_statsn = 0;
  1276. conn->tmabort_state = TMABORT_INITIAL;
  1277. INIT_LIST_HEAD(&conn->run_list);
  1278. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1279. INIT_LIST_HEAD(&conn->xmitqueue);
  1280. /* initialize general immediate & non-immediate PDU commands queue */
  1281. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1282. GFP_KERNEL, NULL);
  1283. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1284. goto immqueue_alloc_fail;
  1285. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1286. GFP_KERNEL, NULL);
  1287. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1288. goto mgmtqueue_alloc_fail;
  1289. INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
  1290. /* allocate login_mtask used for the login/text sequences */
  1291. spin_lock_bh(&session->lock);
  1292. if (!__kfifo_get(session->mgmtpool.queue,
  1293. (void*)&conn->login_mtask,
  1294. sizeof(void*))) {
  1295. spin_unlock_bh(&session->lock);
  1296. goto login_mtask_alloc_fail;
  1297. }
  1298. spin_unlock_bh(&session->lock);
  1299. data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
  1300. if (!data)
  1301. goto login_mtask_data_alloc_fail;
  1302. conn->login_mtask->data = conn->data = data;
  1303. init_timer(&conn->tmabort_timer);
  1304. mutex_init(&conn->xmitmutex);
  1305. init_waitqueue_head(&conn->ehwait);
  1306. return cls_conn;
  1307. login_mtask_data_alloc_fail:
  1308. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1309. sizeof(void*));
  1310. login_mtask_alloc_fail:
  1311. kfifo_free(conn->mgmtqueue);
  1312. mgmtqueue_alloc_fail:
  1313. kfifo_free(conn->immqueue);
  1314. immqueue_alloc_fail:
  1315. iscsi_destroy_conn(cls_conn);
  1316. return NULL;
  1317. }
  1318. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1319. /**
  1320. * iscsi_conn_teardown - teardown iscsi connection
  1321. * cls_conn: iscsi class connection
  1322. *
  1323. * TODO: we may need to make this into a two step process
  1324. * like scsi-mls remove + put host
  1325. */
  1326. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1327. {
  1328. struct iscsi_conn *conn = cls_conn->dd_data;
  1329. struct iscsi_session *session = conn->session;
  1330. unsigned long flags;
  1331. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1332. mutex_lock(&conn->xmitmutex);
  1333. spin_lock_bh(&session->lock);
  1334. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1335. if (session->leadconn == conn) {
  1336. /*
  1337. * leading connection? then give up on recovery.
  1338. */
  1339. session->state = ISCSI_STATE_TERMINATE;
  1340. wake_up(&conn->ehwait);
  1341. }
  1342. spin_unlock_bh(&session->lock);
  1343. mutex_unlock(&conn->xmitmutex);
  1344. /*
  1345. * Block until all in-progress commands for this connection
  1346. * time out or fail.
  1347. */
  1348. for (;;) {
  1349. spin_lock_irqsave(session->host->host_lock, flags);
  1350. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1351. spin_unlock_irqrestore(session->host->host_lock, flags);
  1352. break;
  1353. }
  1354. spin_unlock_irqrestore(session->host->host_lock, flags);
  1355. msleep_interruptible(500);
  1356. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1357. "host_failed %d\n", session->host->host_busy,
  1358. session->host->host_failed);
  1359. /*
  1360. * force eh_abort() to unblock
  1361. */
  1362. wake_up(&conn->ehwait);
  1363. }
  1364. spin_lock_bh(&session->lock);
  1365. kfree(conn->data);
  1366. kfree(conn->persistent_address);
  1367. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1368. sizeof(void*));
  1369. list_del(&conn->item);
  1370. if (list_empty(&session->connections))
  1371. session->leadconn = NULL;
  1372. if (session->leadconn && session->leadconn == conn)
  1373. session->leadconn = container_of(session->connections.next,
  1374. struct iscsi_conn, item);
  1375. if (session->leadconn == NULL)
  1376. /* no connections exits.. reset sequencing */
  1377. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1378. spin_unlock_bh(&session->lock);
  1379. kfifo_free(conn->immqueue);
  1380. kfifo_free(conn->mgmtqueue);
  1381. iscsi_destroy_conn(cls_conn);
  1382. }
  1383. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1384. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1385. {
  1386. struct iscsi_conn *conn = cls_conn->dd_data;
  1387. struct iscsi_session *session = conn->session;
  1388. if (!session) {
  1389. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1390. return -EPERM;
  1391. }
  1392. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1393. session->first_burst > session->max_burst) {
  1394. printk("iscsi: invalid burst lengths: "
  1395. "first_burst %d max_burst %d\n",
  1396. session->first_burst, session->max_burst);
  1397. return -EINVAL;
  1398. }
  1399. spin_lock_bh(&session->lock);
  1400. conn->c_stage = ISCSI_CONN_STARTED;
  1401. session->state = ISCSI_STATE_LOGGED_IN;
  1402. switch(conn->stop_stage) {
  1403. case STOP_CONN_RECOVER:
  1404. /*
  1405. * unblock eh_abort() if it is blocked. re-try all
  1406. * commands after successful recovery
  1407. */
  1408. conn->stop_stage = 0;
  1409. conn->tmabort_state = TMABORT_INITIAL;
  1410. session->age++;
  1411. spin_unlock_bh(&session->lock);
  1412. iscsi_unblock_session(session_to_cls(session));
  1413. wake_up(&conn->ehwait);
  1414. return 0;
  1415. case STOP_CONN_TERM:
  1416. conn->stop_stage = 0;
  1417. break;
  1418. default:
  1419. break;
  1420. }
  1421. spin_unlock_bh(&session->lock);
  1422. return 0;
  1423. }
  1424. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1425. static void
  1426. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1427. {
  1428. struct iscsi_mgmt_task *mtask, *tmp;
  1429. /* handle pending */
  1430. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1431. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1432. if (mtask == conn->login_mtask)
  1433. continue;
  1434. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1435. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1436. sizeof(void*));
  1437. }
  1438. /* handle running */
  1439. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1440. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1441. list_del(&mtask->running);
  1442. if (mtask == conn->login_mtask)
  1443. continue;
  1444. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1445. sizeof(void*));
  1446. }
  1447. conn->mtask = NULL;
  1448. }
  1449. /* Fail commands. Mutex and session lock held and recv side suspended */
  1450. static void fail_all_commands(struct iscsi_conn *conn)
  1451. {
  1452. struct iscsi_cmd_task *ctask, *tmp;
  1453. /* flush pending */
  1454. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1455. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1456. ctask->itt);
  1457. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1458. }
  1459. /* fail all other running */
  1460. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1461. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1462. ctask->sc, ctask->itt);
  1463. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1464. }
  1465. conn->ctask = NULL;
  1466. }
  1467. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1468. struct iscsi_conn *conn, int flag)
  1469. {
  1470. int old_stop_stage;
  1471. spin_lock_bh(&session->lock);
  1472. if (conn->stop_stage == STOP_CONN_TERM) {
  1473. spin_unlock_bh(&session->lock);
  1474. return;
  1475. }
  1476. /*
  1477. * When this is called for the in_login state, we only want to clean
  1478. * up the login task and connection. We do not need to block and set
  1479. * the recovery state again
  1480. */
  1481. if (flag == STOP_CONN_TERM)
  1482. session->state = ISCSI_STATE_TERMINATE;
  1483. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1484. session->state = ISCSI_STATE_IN_RECOVERY;
  1485. old_stop_stage = conn->stop_stage;
  1486. conn->stop_stage = flag;
  1487. conn->c_stage = ISCSI_CONN_STOPPED;
  1488. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1489. spin_unlock_bh(&session->lock);
  1490. write_lock_bh(conn->recv_lock);
  1491. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1492. write_unlock_bh(conn->recv_lock);
  1493. mutex_lock(&conn->xmitmutex);
  1494. /*
  1495. * for connection level recovery we should not calculate
  1496. * header digest. conn->hdr_size used for optimization
  1497. * in hdr_extract() and will be re-negotiated at
  1498. * set_param() time.
  1499. */
  1500. if (flag == STOP_CONN_RECOVER) {
  1501. conn->hdrdgst_en = 0;
  1502. conn->datadgst_en = 0;
  1503. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1504. old_stop_stage != STOP_CONN_RECOVER) {
  1505. debug_scsi("blocking session\n");
  1506. iscsi_block_session(session_to_cls(session));
  1507. }
  1508. }
  1509. /*
  1510. * flush queues.
  1511. */
  1512. spin_lock_bh(&session->lock);
  1513. fail_all_commands(conn);
  1514. flush_control_queues(session, conn);
  1515. spin_unlock_bh(&session->lock);
  1516. mutex_unlock(&conn->xmitmutex);
  1517. }
  1518. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1519. {
  1520. struct iscsi_conn *conn = cls_conn->dd_data;
  1521. struct iscsi_session *session = conn->session;
  1522. switch (flag) {
  1523. case STOP_CONN_RECOVER:
  1524. case STOP_CONN_TERM:
  1525. iscsi_start_session_recovery(session, conn, flag);
  1526. break;
  1527. default:
  1528. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1529. }
  1530. }
  1531. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1532. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1533. struct iscsi_cls_conn *cls_conn, int is_leading)
  1534. {
  1535. struct iscsi_session *session = class_to_transport_session(cls_session);
  1536. struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
  1537. /* lookup for existing connection */
  1538. spin_lock_bh(&session->lock);
  1539. list_for_each_entry(tmp, &session->connections, item) {
  1540. if (tmp == conn) {
  1541. if (conn->c_stage != ISCSI_CONN_STOPPED ||
  1542. conn->stop_stage == STOP_CONN_TERM) {
  1543. printk(KERN_ERR "iscsi: can't bind "
  1544. "non-stopped connection (%d:%d)\n",
  1545. conn->c_stage, conn->stop_stage);
  1546. spin_unlock_bh(&session->lock);
  1547. return -EIO;
  1548. }
  1549. break;
  1550. }
  1551. }
  1552. if (tmp != conn) {
  1553. /* bind new iSCSI connection to session */
  1554. conn->session = session;
  1555. list_add(&conn->item, &session->connections);
  1556. }
  1557. spin_unlock_bh(&session->lock);
  1558. if (is_leading)
  1559. session->leadconn = conn;
  1560. /*
  1561. * Unblock xmitworker(), Login Phase will pass through.
  1562. */
  1563. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1564. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1565. return 0;
  1566. }
  1567. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1568. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1569. enum iscsi_param param, char *buf, int buflen)
  1570. {
  1571. struct iscsi_conn *conn = cls_conn->dd_data;
  1572. struct iscsi_session *session = conn->session;
  1573. uint32_t value;
  1574. switch(param) {
  1575. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1576. sscanf(buf, "%d", &conn->max_recv_dlength);
  1577. break;
  1578. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1579. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1580. break;
  1581. case ISCSI_PARAM_HDRDGST_EN:
  1582. sscanf(buf, "%d", &conn->hdrdgst_en);
  1583. break;
  1584. case ISCSI_PARAM_DATADGST_EN:
  1585. sscanf(buf, "%d", &conn->datadgst_en);
  1586. break;
  1587. case ISCSI_PARAM_INITIAL_R2T_EN:
  1588. sscanf(buf, "%d", &session->initial_r2t_en);
  1589. break;
  1590. case ISCSI_PARAM_MAX_R2T:
  1591. sscanf(buf, "%d", &session->max_r2t);
  1592. break;
  1593. case ISCSI_PARAM_IMM_DATA_EN:
  1594. sscanf(buf, "%d", &session->imm_data_en);
  1595. break;
  1596. case ISCSI_PARAM_FIRST_BURST:
  1597. sscanf(buf, "%d", &session->first_burst);
  1598. break;
  1599. case ISCSI_PARAM_MAX_BURST:
  1600. sscanf(buf, "%d", &session->max_burst);
  1601. break;
  1602. case ISCSI_PARAM_PDU_INORDER_EN:
  1603. sscanf(buf, "%d", &session->pdu_inorder_en);
  1604. break;
  1605. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1606. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1607. break;
  1608. case ISCSI_PARAM_ERL:
  1609. sscanf(buf, "%d", &session->erl);
  1610. break;
  1611. case ISCSI_PARAM_IFMARKER_EN:
  1612. sscanf(buf, "%d", &value);
  1613. BUG_ON(value);
  1614. break;
  1615. case ISCSI_PARAM_OFMARKER_EN:
  1616. sscanf(buf, "%d", &value);
  1617. BUG_ON(value);
  1618. break;
  1619. case ISCSI_PARAM_EXP_STATSN:
  1620. sscanf(buf, "%u", &conn->exp_statsn);
  1621. break;
  1622. case ISCSI_PARAM_TARGET_NAME:
  1623. /* this should not change between logins */
  1624. if (session->targetname)
  1625. break;
  1626. session->targetname = kstrdup(buf, GFP_KERNEL);
  1627. if (!session->targetname)
  1628. return -ENOMEM;
  1629. break;
  1630. case ISCSI_PARAM_TPGT:
  1631. sscanf(buf, "%d", &session->tpgt);
  1632. break;
  1633. case ISCSI_PARAM_PERSISTENT_PORT:
  1634. sscanf(buf, "%d", &conn->persistent_port);
  1635. break;
  1636. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1637. /*
  1638. * this is the address returned in discovery so it should
  1639. * not change between logins.
  1640. */
  1641. if (conn->persistent_address)
  1642. break;
  1643. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1644. if (!conn->persistent_address)
  1645. return -ENOMEM;
  1646. break;
  1647. default:
  1648. return -ENOSYS;
  1649. }
  1650. return 0;
  1651. }
  1652. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1653. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1654. enum iscsi_param param, char *buf)
  1655. {
  1656. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1657. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1658. int len;
  1659. switch(param) {
  1660. case ISCSI_PARAM_INITIAL_R2T_EN:
  1661. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1662. break;
  1663. case ISCSI_PARAM_MAX_R2T:
  1664. len = sprintf(buf, "%hu\n", session->max_r2t);
  1665. break;
  1666. case ISCSI_PARAM_IMM_DATA_EN:
  1667. len = sprintf(buf, "%d\n", session->imm_data_en);
  1668. break;
  1669. case ISCSI_PARAM_FIRST_BURST:
  1670. len = sprintf(buf, "%u\n", session->first_burst);
  1671. break;
  1672. case ISCSI_PARAM_MAX_BURST:
  1673. len = sprintf(buf, "%u\n", session->max_burst);
  1674. break;
  1675. case ISCSI_PARAM_PDU_INORDER_EN:
  1676. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1677. break;
  1678. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1679. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1680. break;
  1681. case ISCSI_PARAM_ERL:
  1682. len = sprintf(buf, "%d\n", session->erl);
  1683. break;
  1684. case ISCSI_PARAM_TARGET_NAME:
  1685. len = sprintf(buf, "%s\n", session->targetname);
  1686. break;
  1687. case ISCSI_PARAM_TPGT:
  1688. len = sprintf(buf, "%d\n", session->tpgt);
  1689. break;
  1690. default:
  1691. return -ENOSYS;
  1692. }
  1693. return len;
  1694. }
  1695. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1696. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1697. enum iscsi_param param, char *buf)
  1698. {
  1699. struct iscsi_conn *conn = cls_conn->dd_data;
  1700. int len;
  1701. switch(param) {
  1702. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1703. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1704. break;
  1705. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1706. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1707. break;
  1708. case ISCSI_PARAM_HDRDGST_EN:
  1709. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1710. break;
  1711. case ISCSI_PARAM_DATADGST_EN:
  1712. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1713. break;
  1714. case ISCSI_PARAM_IFMARKER_EN:
  1715. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1716. break;
  1717. case ISCSI_PARAM_OFMARKER_EN:
  1718. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1719. break;
  1720. case ISCSI_PARAM_EXP_STATSN:
  1721. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1722. break;
  1723. case ISCSI_PARAM_PERSISTENT_PORT:
  1724. len = sprintf(buf, "%d\n", conn->persistent_port);
  1725. break;
  1726. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1727. len = sprintf(buf, "%s\n", conn->persistent_address);
  1728. break;
  1729. default:
  1730. return -ENOSYS;
  1731. }
  1732. return len;
  1733. }
  1734. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1735. MODULE_AUTHOR("Mike Christie");
  1736. MODULE_DESCRIPTION("iSCSI library functions");
  1737. MODULE_LICENSE("GPL");